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.Table;
30  import com.healthmarketscience.jackcess.TableBuilder;
31  import com.healthmarketscience.jackcess.impl.JetFormatTest;
32  import junit.framework.TestCase;
33  import org.apache.commons.lang.SystemUtils;
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 = SystemUtils.LINE_SEPARATOR;
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.setTimeZone(TEST_TZ);
57  
58        Table t = new TableBuilder("test")
59          .addColumn(new ColumnBuilder("col1", DataType.TEXT))
60          .addColumn(new ColumnBuilder("col2", DataType.LONG))
61          .addColumn(new ColumnBuilder("col3", DataType.DOUBLE))
62          .addColumn(new ColumnBuilder("col4", DataType.OLE))
63          .addColumn(new ColumnBuilder("col5", DataType.BOOLEAN))
64          .addColumn(new ColumnBuilder("col6", DataType.SHORT_DATE_TIME))
65          .toTable(db);
66  
67        Date testDate = df.parse("19801231 00:00:00");
68        t.addRow("some text||some more", 13, 13.25, createString(30).getBytes(),
69                 true, testDate);
70  
71        t.addRow("crazy'data\"here", -345, -0.000345, createString(7).getBytes(),
72                 true, null);
73  
74        t.addRow("C:\\temp\\some_file.txt", 25, 0.0, null, false, null);
75  
76        StringWriter out = new StringWriter();
77  
78        new ExportUtil.Builder(db, "test")
79          .exportWriter(new BufferedWriter(out));
80  
81        String expected = 
82          "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 +
83          "\"crazy'data\"\"here\",-345,-3.45E-4,61 62 63 64  65 66 67,true," + NL +
84          "C:\\temp\\some_file.txt,25,0.0,,false," + NL;
85  
86        assertEquals(expected, out.toString());
87  
88        out = new StringWriter();
89        
90        new ExportUtil.Builder(db, "test")
91          .setHeader(true)
92          .setDelimiter("||")
93          .setQuote('\'')
94          .exportWriter(new BufferedWriter(out));
95  
96        expected = 
97          "col1||col2||col3||col4||col5||col6" + NL +
98          "'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 +
99          "'crazy''data\"here'||-345||-3.45E-4||61 62 63 64  65 66 67||true||" + NL +
100         "C:\\temp\\some_file.txt||25||0.0||||false||" + NL;
101       assertEquals(expected, out.toString());
102 
103       ExportFilter oddFilter = new SimpleExportFilter() {
104         private int _num;
105         @Override
106         public Object[] filterRow(Object[] row) {
107           if((_num++ % 2) == 1) {
108             return null;
109           }
110           return row;
111         }
112       };
113 
114       out = new StringWriter();
115 
116       new ExportUtil.Builder(db, "test")
117         .setFilter(oddFilter)
118         .exportWriter(new BufferedWriter(out));
119 
120       expected = 
121         "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 +
122         "C:\\temp\\some_file.txt,25,0.0,,false," + NL;
123 
124       assertEquals(expected, out.toString());
125     }
126   }
127 
128 }