1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
|
/*
Copyright (c) 2007 Health Market Science, Inc.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
USA
You can contact Health Market Science at info@healthmarketscience.com
or at the following address:
Health Market Science
2700 Horizon Drive
Suite 200
King of Prussia, PA 19406
*/
package com.healthmarketscience.jackcess;
import java.io.BufferedWriter;
import java.io.StringWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.TimeZone;
import junit.framework.TestCase;
import org.apache.commons.lang.SystemUtils;
import java.util.Date;
import static com.healthmarketscience.jackcess.Database.*;
import static com.healthmarketscience.jackcess.DatabaseTest.*;
/**
*
* @author James Ahlborn
*/
public class ExportTest extends TestCase
{
private static final String NL = SystemUtils.LINE_SEPARATOR;
public ExportTest(String name) {
super(name);
}
public void testExportToFile() throws Exception
{
TimeZone testTZ = TimeZone.getTimeZone("America/New_York");
DateFormat df = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
df.setTimeZone(testTZ);
for (final FileFormat fileFormat : JetFormatTest.SUPPORTED_FILEFORMATS) {
Database db = create(fileFormat);
db.setTimeZone(testTZ);
Table t = new TableBuilder("test")
.addColumn(new ColumnBuilder("col1", DataType.TEXT))
.addColumn(new ColumnBuilder("col2", DataType.LONG))
.addColumn(new ColumnBuilder("col3", DataType.DOUBLE))
.addColumn(new ColumnBuilder("col4", DataType.OLE))
.addColumn(new ColumnBuilder("col5", DataType.BOOLEAN))
.addColumn(new ColumnBuilder("col6", DataType.SHORT_DATE_TIME))
.toTable(db);
Date testDate = df.parse("19801231 00:00:00");
t.addRow("some text||some more", 13, 13.25, createString(30).getBytes(),
true, testDate);
t.addRow("crazy'data\"here", -345, -0.000345, createString(7).getBytes(),
true, null);
t.addRow("C:\\temp\\some_file.txt", 25, 0.0, null, false, null);
StringWriter out = new StringWriter();
ExportUtil.exportWriter(db, "test", new BufferedWriter(out));
String expected =
"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" + NL +
"79 7A 61 62 63 64\",true," + testDate + NL +
"\"crazy'data\"\"here\",-345,-3.45E-4,61 62 63 64 65 66 67,true," + NL +
"C:\\temp\\some_file.txt,25,0.0,,false," + NL;
assertEquals(expected, out.toString());
out = new StringWriter();
ExportUtil.exportWriter(db, "test", new BufferedWriter(out),
true, "||", '\'', SimpleExportFilter.INSTANCE);
expected =
"col1||col2||col3||col4||col5||col6" + NL +
"'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" + NL +
"79 7A 61 62 63 64'||true||" + testDate + NL +
"'crazy''data\"here'||-345||-3.45E-4||61 62 63 64 65 66 67||true||" + NL +
"C:\\temp\\some_file.txt||25||0.0||||false||" + NL;
assertEquals(expected, out.toString());
}
}
}
|