View Javadoc
1   /*
2   Copyright (c) 2007 Health Market Science, Inc.
3   
4   Licensed under the Apache License, Version 2.0 (the "License");
5   you may not use this file except in compliance with the License.
6   You may obtain a copy of the License at
7   
8       http://www.apache.org/licenses/LICENSE-2.0
9   
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15  */
16  
17  package com.healthmarketscience.jackcess.util;
18  
19  import java.io.BufferedWriter;
20  import java.io.StringWriter;
21  import java.text.DateFormat;
22  import java.text.SimpleDateFormat;
23  import java.util.Date;
24  
25  import static com.healthmarketscience.jackcess.Database.*;
26  import com.healthmarketscience.jackcess.ColumnBuilder;
27  import com.healthmarketscience.jackcess.DataType;
28  import com.healthmarketscience.jackcess.Database;
29  import com.healthmarketscience.jackcess.DateTimeType;
30  import com.healthmarketscience.jackcess.Table;
31  import com.healthmarketscience.jackcess.TableBuilder;
32  import com.healthmarketscience.jackcess.impl.JetFormatTest;
33  import junit.framework.TestCase;
34  import static com.healthmarketscience.jackcess.TestUtil.*;
35  
36  /**
37   *
38   * @author James Ahlborn
39   */
40  public class ExportTest extends TestCase
41  {
42    private static final String NL = System.lineSeparator();
43  
44  
45    public ExportTest(String name) {
46      super(name);
47    }
48  
49    public void testExportToFile() throws Exception
50    {
51      DateFormat df = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
52      df.setTimeZone(TEST_TZ);
53  
54      for (final FileFormat fileFormat : JetFormatTest.SUPPORTED_FILEFORMATS) {
55        Database db = create(fileFormat);
56        db.setDateTimeType(DateTimeType.DATE);
57        db.setTimeZone(TEST_TZ);
58  
59        Table t = new TableBuilder("test")
60          .addColumn(new ColumnBuilder("col1", DataType.TEXT))
61          .addColumn(new ColumnBuilder("col2", DataType.LONG))
62          .addColumn(new ColumnBuilder("col3", DataType.DOUBLE))
63          .addColumn(new ColumnBuilder("col4", DataType.OLE))
64          .addColumn(new ColumnBuilder("col5", DataType.BOOLEAN))
65          .addColumn(new ColumnBuilder("col6", DataType.SHORT_DATE_TIME))
66          .toTable(db);
67  
68        Date testDate = df.parse("19801231 00:00:00");
69        t.addRow("some text||some more", 13, 13.25, createString(30).getBytes(),
70                 true, testDate);
71  
72        t.addRow("crazy'data\"here", -345, -0.000345, createString(7).getBytes(),
73                 true, null);
74  
75        t.addRow("C:\\temp\\some_file.txt", 25, 0.0, null, false, null);
76  
77        StringWriter out = new StringWriter();
78  
79        new ExportUtil.Builder(db, "test")
80          .exportWriter(new BufferedWriter(out));
81  
82        String expected =
83          "some text||some more,13,13.25,\"61 62 63 64  65 66 67 68  69 6A 6B 6C  6D 6E 6F 70  71 72 73 74  75 76 77 78\n79 7A 61 62  63 64\",true," + testDate + NL +
84          "\"crazy'data\"\"here\",-345,-3.45E-4,61 62 63 64  65 66 67,true," + NL +
85          "C:\\temp\\some_file.txt,25,0.0,,false," + NL;
86  
87        assertEquals(expected, out.toString());
88  
89        out = new StringWriter();
90  
91        new ExportUtil.Builder(db, "test")
92          .setHeader(true)
93          .setDelimiter("||")
94          .setQuote('\'')
95          .exportWriter(new BufferedWriter(out));
96  
97        expected =
98          "col1||col2||col3||col4||col5||col6" + NL +
99          "'some text||some more'||13||13.25||'61 62 63 64  65 66 67 68  69 6A 6B 6C  6D 6E 6F 70  71 72 73 74  75 76 77 78\n79 7A 61 62  63 64'||true||" + testDate + NL +
100         "'crazy''data\"here'||-345||-3.45E-4||61 62 63 64  65 66 67||true||" + NL +
101         "C:\\temp\\some_file.txt||25||0.0||||false||" + NL;
102       assertEquals(expected, out.toString());
103 
104       ExportFilter oddFilter = new SimpleExportFilter() {
105         private int _num;
106         @Override
107         public Object[] filterRow(Object[] row) {
108           if((_num++ % 2) == 1) {
109             return null;
110           }
111           return row;
112         }
113       };
114 
115       out = new StringWriter();
116 
117       new ExportUtil.Builder(db, "test")
118         .setFilter(oddFilter)
119         .exportWriter(new BufferedWriter(out));
120 
121       expected =
122         "some text||some more,13,13.25,\"61 62 63 64  65 66 67 68  69 6A 6B 6C  6D 6E 6F 70  71 72 73 74  75 76 77 78\n79 7A 61 62  63 64\",true," + testDate + NL +
123         "C:\\temp\\some_file.txt,25,0.0,,false," + NL;
124 
125       assertEquals(expected, out.toString());
126     }
127   }
128 
129 }