1
+ package net .sf .flatpack .rfc4180 ;
2
+
3
+ import java .io .IOException ;
4
+ import java .io .StringWriter ;
5
+
6
+ import net .sf .flatpack .writer .DelimiterWriterFactory ;
7
+ import net .sf .flatpack .writer .Rfc4180TestCase ;
8
+ import net .sf .flatpack .writer .Writer ;
9
+ import net .sf .flatpack .writer .WriterOptions ;
10
+
11
+ import static org .hamcrest .CoreMatchers .is ;
12
+ import static org .hamcrest .CoreMatchers .not ;
13
+ import static org .hamcrest .CoreMatchers .notNullValue ;
14
+ import static org .junit .Assert .assertThat ;
15
+
16
+ public class CsvWriterFormatDefintionTest extends Rfc4180TestCase {
17
+ private final char FIELD_DELIMITER = ',' ;
18
+ private final char FIELD_QUALIFIER = '"' ;
19
+ private final String LINE_SEPERATOR = "\r \n " ;
20
+
21
+
22
+ /*
23
+ * 2.1 Each record is located on a separate line, delimited by a line break (CRLF). For example:
24
+ *
25
+ * aaa,bbb,ccc CRLF
26
+ * zzz,yyy,xxx CRLF
27
+ */
28
+ public void testLineSeparation () throws IOException {
29
+ final StringWriter out = new StringWriter ();
30
+
31
+ try (Writer writer = getWriterForRfc4180 (out , false )) {
32
+ // write one line of data ... not in the correct order of fields
33
+ writer .addRecordEntry ("col1" , "aaa" )
34
+ .addRecordEntry ("col2" , "bbb" )
35
+ .addRecordEntry ("col3" , "ccc" )
36
+ .nextRecord ();
37
+ writer .addRecordEntry ("col3" , "xxx" )
38
+ .addRecordEntry ("col2" , "yyy" )
39
+ .addRecordEntry ("col1" , "zzz" )
40
+ .nextRecord ();
41
+
42
+ writer .flush ();
43
+ }
44
+
45
+ String result = out .toString ();
46
+ assertThat (result , is (notNullValue ()));
47
+ assertThat (result .length (), is (26 ));
48
+
49
+ String [] lines = result .split (LINE_SEPERATOR );
50
+ assertThat (lines , is (notNullValue ()));
51
+ assertThat (lines .length , is (2 ));
52
+ assertThat (lines [0 ], is ("aaa,bbb,ccc" ));
53
+ assertThat (lines [1 ], is ("zzz,yyy,xxx" ));
54
+ }
55
+
56
+ /*
57
+ * 2.4a. Each line should contain the same number of fields throughout the file
58
+ */
59
+ public void testSameNumberOffFields () throws IOException {
60
+ final StringWriter out = new StringWriter ();
61
+
62
+ try (Writer writer = getWriterForRfc4180 (out , false )) {
63
+ // write one line of data ... not in the correct order of fields
64
+ writer .addRecordEntry ("col1" , "aaa" )
65
+ .addRecordEntry ("col2" , "bbb" )
66
+ .addRecordEntry ("col3" , "ccc" )
67
+ .nextRecord ();
68
+ writer .addRecordEntry ("col3" , "xxx" )
69
+ //.addRecordEntry("col2", "yyy")
70
+ .addRecordEntry ("col1" , "zzz" )
71
+ .nextRecord ();
72
+
73
+ writer .flush ();
74
+ }
75
+
76
+ String result = out .toString ();
77
+ assertThat (result , is (notNullValue ()));
78
+ assertThat (result .length (), is (23 ));
79
+
80
+ String [] lines = result .split (LINE_SEPERATOR );
81
+ assertThat (lines , is (notNullValue ()));
82
+ assertThat (lines .length , is (2 ));
83
+ assertThat (lines [0 ], is ("aaa,bbb,ccc" ));
84
+ assertThat (lines [0 ].split (String .valueOf (FIELD_DELIMITER )).length , is (3 ));
85
+ assertThat (lines [1 ], is ("zzz,,xxx" ));
86
+ assertThat (lines [1 ].split (String .valueOf (FIELD_DELIMITER )).length , is (3 ));
87
+ }
88
+
89
+ /*
90
+ * 2.4b. Spaces are considered part of a field and should not be ignored.
91
+ */
92
+ public void testSpacesArePartOfField () throws IOException {
93
+ final StringWriter out = new StringWriter ();
94
+
95
+ try (Writer writer = getWriterForRfc4180 (out , false )) {
96
+ // write one line of data ... not in the correct order of fields
97
+ writer .addRecordEntry ("col1" , "aaa " )
98
+ .addRecordEntry ("col2" , " bbb" )
99
+ .addRecordEntry ("col3" , "c cc" )
100
+ .nextRecord ();
101
+ writer .addRecordEntry ("col3" , "x x x" )
102
+ .addRecordEntry ("col2" , "yy y" )
103
+ .addRecordEntry ("col1" , " zzz " )
104
+ .nextRecord ();
105
+
106
+ writer .flush ();
107
+ }
108
+
109
+ String result = out .toString ();
110
+ assertThat (result , is (notNullValue ()));
111
+ assertThat (result .length (), is (34 ));
112
+
113
+ String [] lines = result .split (LINE_SEPERATOR );
114
+ assertThat (lines , is (notNullValue ()));
115
+ assertThat (lines .length , is (2 ));
116
+ assertThat (lines [0 ], is ("aaa , bbb,c cc" ));
117
+ assertThat (lines [1 ], is (" zzz ,yy y,x x x" ));
118
+ }
119
+
120
+ /*
121
+ * 2.4c. The last field in the record must not be followed by a comma
122
+ */
123
+ public void testLastFieldShouldNotHaveDelimiter () throws IOException {
124
+ final StringWriter out = new StringWriter ();
125
+
126
+ try (Writer writer = getWriterForRfc4180 (out , false )) {
127
+ // write one line of data ... not in the correct order of fields
128
+ writer .addRecordEntry ("col1" , "aaa" )
129
+ .addRecordEntry ("col2" , "bbb" )
130
+ .addRecordEntry ("col3" , "ccc" )
131
+ .nextRecord ();
132
+ writer .addRecordEntry ("col3" , "xxx" )
133
+ .addRecordEntry ("col2" , "yyy" )
134
+ .addRecordEntry ("col1" , "zzz" )
135
+ .nextRecord ();
136
+
137
+ writer .flush ();
138
+ }
139
+
140
+ String result = out .toString ();
141
+ assertThat (result , is (notNullValue ()));
142
+ assertThat (result .length (), is (26 ));
143
+
144
+ String [] lines = result .split (LINE_SEPERATOR );
145
+ assertThat (lines , is (notNullValue ()));
146
+ assertThat (lines .length , is (2 ));
147
+ assertThat (lines [0 ].length (), is (11 ));
148
+ assertThat (lines [0 ].charAt (lines [0 ].length () -1 ), is (not (FIELD_DELIMITER )));
149
+ assertThat (lines [1 ].length (), is (11 ));
150
+ assertThat (lines [1 ].charAt (lines [0 ].length () -1 ), is (not (FIELD_DELIMITER )));
151
+ }
152
+
153
+
154
+ private Writer getWriterForRfc4180 (java .io .Writer out , boolean autoPrintHeader ) throws IOException {
155
+ WriterOptions options = WriterOptions .getInstance ();
156
+ options .autoPrintHeader (autoPrintHeader );
157
+ options .setLineSeperator (LINE_SEPERATOR );
158
+
159
+ final DelimiterWriterFactory factory = new DelimiterWriterFactory (FIELD_DELIMITER , FIELD_QUALIFIER )//
160
+ .addColumnTitle ("col1" )
161
+ .addColumnTitle ("col2" )
162
+ .addColumnTitle ("col3" );
163
+
164
+ return factory .createWriter (out , options );
165
+ }
166
+ }
0 commit comments