4242 */
4343public class CSVFormatTest {
4444
45- @ Test
46- public void testFormat () {
47- final CSVFormat format = CSVFormat .DEFAULT ;
48-
49- assertEquals ("" , format .format ());
50- assertEquals ("a,b,c" , format .format ("a" , "b" , "c" ));
51- assertEquals ("\" x,y\" ,z" , format .format ("x,y" , "z" ));
45+ private static void assertNotEquals (final Object right , final Object left ) {
46+ assertFalse (right .equals (left ));
47+ assertFalse (left .equals (right ));
5248 }
5349
54- @ SuppressWarnings ("boxing" ) // no need to worry about boxing here
55- @ Test
56- public void testSerialization () throws Exception {
57- final ByteArrayOutputStream out = new ByteArrayOutputStream ();
58-
59- final ObjectOutputStream oos = new ObjectOutputStream (out );
60- oos .writeObject (CSVFormat .DEFAULT );
61- oos .flush ();
62- oos .close ();
63-
64- final ObjectInputStream in = new ObjectInputStream (new ByteArrayInputStream (out .toByteArray ()));
65- final CSVFormat format = (CSVFormat ) in .readObject ();
50+ @ Test (expected = IllegalStateException .class )
51+ public void testDelimiterSameAsCommentStartThrowsException () {
52+ CSVFormat .DEFAULT .withDelimiter ('!' ).withCommentStart ('!' ).validate ();
53+ }
6654
67- assertNotNull (format );
68- assertEquals ("delimiter" , CSVFormat .DEFAULT .getDelimiter (), format .getDelimiter ());
69- assertEquals ("encapsulator" , CSVFormat .DEFAULT .getQuoteChar (), format .getQuoteChar ());
70- assertEquals ("comment start" , CSVFormat .DEFAULT .getCommentStart (), format .getCommentStart ());
71- assertEquals ("line separator" , CSVFormat .DEFAULT .getRecordSeparator (), format .getRecordSeparator ());
72- assertEquals ("escape" , CSVFormat .DEFAULT .getEscape (), format .getEscape ());
73- assertEquals ("trim" , CSVFormat .DEFAULT .getIgnoreSurroundingSpaces (), format .getIgnoreSurroundingSpaces ());
74- assertEquals ("empty lines" , CSVFormat .DEFAULT .getIgnoreEmptyLines (), format .getIgnoreEmptyLines ());
55+ @ Test (expected = IllegalStateException .class )
56+ public void testDelimiterSameAsEscapeThrowsException () {
57+ CSVFormat .DEFAULT .withDelimiter ('!' ).withEscape ('!' ).validate ();
7558 }
7659
7760 @ Test
@@ -91,40 +74,21 @@ public void testEquals() {
9174 }
9275
9376 @ Test
94- public void testEqualsDelimiter () {
95- final CSVFormat right = CSVFormat .newFormat ('!' );
96- final CSVFormat left = CSVFormat .newFormat ('?' );
97-
98- assertNotEquals (right , left );
99- }
100-
101- @ Test
102- public void testEqualsQuoteChar () {
103- final CSVFormat right = CSVFormat .newFormat ('\'' ).withQuoteChar ('"' );
104- final CSVFormat left = CSVFormat .copy (right ).withQuoteChar ('!' );
105-
106- assertNotEquals (right , left );
107- }
108-
109- @ Test
110- public void testEqualsQuotePolicy () {
77+ public void testEqualsCommentStart () {
11178 final CSVFormat right = CSVFormat .newFormat ('\'' )
11279 .withQuoteChar ('"' )
80+ .withCommentStart ('#' )
11381 .withQuotePolicy (Quote .ALL );
11482 final CSVFormat left = CSVFormat .copy (right )
115- .withQuotePolicy ( Quote . MINIMAL );
83+ .withCommentStart ( '!' );
11684
11785 assertNotEquals (right , left );
11886 }
11987
12088 @ Test
121- public void testEqualsCommentStart () {
122- final CSVFormat right = CSVFormat .newFormat ('\'' )
123- .withQuoteChar ('"' )
124- .withCommentStart ('#' )
125- .withQuotePolicy (Quote .ALL );
126- final CSVFormat left = CSVFormat .copy (right )
127- .withCommentStart ('!' );
89+ public void testEqualsDelimiter () {
90+ final CSVFormat right = CSVFormat .newFormat ('!' );
91+ final CSVFormat left = CSVFormat .newFormat ('?' );
12892
12993 assertNotEquals (right , left );
13094 }
@@ -143,15 +107,18 @@ public void testEqualsEscape() {
143107 }
144108
145109 @ Test
146- public void testEqualsIgnoreSurroundingSpaces () {
110+ public void testEqualsHeader () {
147111 final CSVFormat right = CSVFormat .newFormat ('\'' )
112+ .withRecordSeparator ('*' )
148113 .withCommentStart ('#' )
149114 .withEscape ('+' )
115+ .withHeader ("One" , "Two" , "Three" )
116+ .withIgnoreEmptyLines (true )
150117 .withIgnoreSurroundingSpaces (true )
151118 .withQuoteChar ('"' )
152119 .withQuotePolicy (Quote .ALL );
153120 final CSVFormat left = CSVFormat .copy (right )
154- .withIgnoreSurroundingSpaces ( false );
121+ .withHeader ( "Three" , "Two" , "One" );
155122
156123 assertNotEquals (right , left );
157124 }
@@ -172,67 +139,72 @@ public void testEqualsIgnoreEmptyLines() {
172139 }
173140
174141 @ Test
175- public void testEqualsRecordSeparator () {
142+ public void testEqualsIgnoreSurroundingSpaces () {
176143 final CSVFormat right = CSVFormat .newFormat ('\'' )
177- .withRecordSeparator ('*' )
178144 .withCommentStart ('#' )
179145 .withEscape ('+' )
180- .withIgnoreEmptyLines (true )
181146 .withIgnoreSurroundingSpaces (true )
182147 .withQuoteChar ('"' )
183148 .withQuotePolicy (Quote .ALL );
184149 final CSVFormat left = CSVFormat .copy (right )
185- .withRecordSeparator ( '!' );
150+ .withIgnoreSurroundingSpaces ( false );
186151
187152 assertNotEquals (right , left );
188153 }
189154
190155 @ Test
191- public void testEqualsHeader () {
156+ public void testEqualsQuoteChar () {
157+ final CSVFormat right = CSVFormat .newFormat ('\'' ).withQuoteChar ('"' );
158+ final CSVFormat left = CSVFormat .copy (right ).withQuoteChar ('!' );
159+
160+ assertNotEquals (right , left );
161+ }
162+
163+ @ Test
164+ public void testEqualsQuotePolicy () {
165+ final CSVFormat right = CSVFormat .newFormat ('\'' )
166+ .withQuoteChar ('"' )
167+ .withQuotePolicy (Quote .ALL );
168+ final CSVFormat left = CSVFormat .copy (right )
169+ .withQuotePolicy (Quote .MINIMAL );
170+
171+ assertNotEquals (right , left );
172+ }
173+
174+ @ Test
175+ public void testEqualsRecordSeparator () {
192176 final CSVFormat right = CSVFormat .newFormat ('\'' )
193177 .withRecordSeparator ('*' )
194178 .withCommentStart ('#' )
195179 .withEscape ('+' )
196- .withHeader ("One" , "Two" , "Three" )
197180 .withIgnoreEmptyLines (true )
198181 .withIgnoreSurroundingSpaces (true )
199182 .withQuoteChar ('"' )
200183 .withQuotePolicy (Quote .ALL );
201184 final CSVFormat left = CSVFormat .copy (right )
202- .withHeader ( "Three" , "Two" , "One" );
185+ .withRecordSeparator ( '!' );
203186
204187 assertNotEquals (right , left );
205188 }
206189
207- @ Test
208- public void testWithCommentStart () throws Exception {
209- CSVFormat formatWithCommentStart = CSVFormat .DEFAULT .withCommentStart ('#' );
210- assertEquals ( Character .valueOf ('#' ), formatWithCommentStart .getCommentStart ());
190+ @ Test (expected = IllegalStateException .class )
191+ public void testEscapeSameAsCommentStartThrowsException () {
192+ CSVFormat .DEFAULT .withEscape ('!' ).withCommentStart ('!' ).validate ();
211193 }
212194
213- @ Test
214- public void testWithDelimiter () throws Exception {
215- CSVFormat formatWithDelimiter = CSVFormat . DEFAULT . withDelimiter ( '!' );
216- assertEquals ( '!' , formatWithDelimiter . getDelimiter () );
195+ @ Test ( expected = IllegalStateException . class )
196+ public void testEscapeSameAsCommentStartThrowsExceptionForWrapperType () {
197+ // Cannot assume that callers won't use different Character objects
198+ CSVFormat . DEFAULT . withEscape ( new Character ( '!' )). withCommentStart ( new Character ( '!' )). validate ( );
217199 }
218200
219201 @ Test
220- public void testWithEscape () throws Exception {
221- CSVFormat formatWithEscape = CSVFormat .DEFAULT .withEscape ('&' );
222- assertEquals (Character .valueOf ('&' ), formatWithEscape .getEscape ());
223- }
202+ public void testFormat () {
203+ final CSVFormat format = CSVFormat .DEFAULT ;
224204
225- @ Test
226- public void testWithHeader () throws Exception {
227- String [] header = new String []{"one" , "two" , "three" };
228- // withHeader() makes a copy of the header array.
229- CSVFormat formatWithHeader = CSVFormat .DEFAULT .withHeader (header );
230- assertArrayEquals (header , formatWithHeader .getHeader ());
231- assertNotSame (header , formatWithHeader .getHeader ());
232- header [0 ] = "A" ;
233- header [1 ] = "B" ;
234- header [2 ] = "C" ;
235- assertFalse (Arrays .equals (formatWithHeader .getHeader (), header ));
205+ assertEquals ("" , format .format ());
206+ assertEquals ("a,b,c" , format .format ("a" , "b" , "c" ));
207+ assertEquals ("\" x,y\" ,z" , format .format ("x,y" , "z" ));
236208 }
237209
238210 @ Test
@@ -248,63 +220,6 @@ public void testGetHeader() throws Exception {
248220 assertNotSame (formatWithHeader .getHeader (), headerCopy );
249221 }
250222
251- @ Test
252- public void testWithIgnoreEmptyLines () throws Exception {
253- assertFalse (CSVFormat .DEFAULT .withIgnoreEmptyLines (false ).getIgnoreEmptyLines ());
254- assertTrue (CSVFormat .DEFAULT .withIgnoreEmptyLines (true ).getIgnoreEmptyLines ());
255- }
256-
257- @ Test
258- public void testWithIgnoreSurround () throws Exception {
259- assertFalse (CSVFormat .DEFAULT .withIgnoreSurroundingSpaces (false ).getIgnoreSurroundingSpaces ());
260- assertTrue (CSVFormat .DEFAULT .withIgnoreSurroundingSpaces (true ).getIgnoreSurroundingSpaces ());
261- }
262-
263- @ Test
264- public void testWithNullString () throws Exception {
265- CSVFormat formatWithNullString = CSVFormat .DEFAULT .withNullString ("null" );
266- assertEquals ("null" , formatWithNullString .getNullString ());
267- }
268-
269- @ Test
270- public void testWithQuoteChar () throws Exception {
271- CSVFormat formatWithQuoteChar = CSVFormat .DEFAULT .withQuoteChar ('"' );
272- assertEquals (Character .valueOf ('"' ), formatWithQuoteChar .getQuoteChar ());
273- }
274-
275- @ Test
276- public void testWithQuotePolicy () throws Exception {
277- CSVFormat formatWithQuotePolicy = CSVFormat .DEFAULT .withQuotePolicy (Quote .ALL );
278- assertEquals (Quote .ALL , formatWithQuotePolicy .getQuotePolicy ());
279- }
280-
281- @ Test
282- public void testWithRecordSeparator () throws Exception {
283- CSVFormat formatWithRecordSeparator = CSVFormat .DEFAULT .withRecordSeparator ('!' );
284- assertEquals ("!" , formatWithRecordSeparator .getRecordSeparator ());
285- }
286-
287- @ Test (expected = IllegalStateException .class )
288- public void testDelimiterSameAsCommentStartThrowsException () {
289- CSVFormat .DEFAULT .withDelimiter ('!' ).withCommentStart ('!' ).validate ();
290- }
291-
292- @ Test (expected = IllegalStateException .class )
293- public void testDelimiterSameAsEscapeThrowsException () {
294- CSVFormat .DEFAULT .withDelimiter ('!' ).withEscape ('!' ).validate ();
295- }
296-
297- @ Test (expected = IllegalStateException .class )
298- public void testEscapeSameAsCommentStartThrowsException () {
299- CSVFormat .DEFAULT .withEscape ('!' ).withCommentStart ('!' ).validate ();
300- }
301-
302- @ Test (expected = IllegalStateException .class )
303- public void testEscapeSameAsCommentStartThrowsExceptionForWrapperType () {
304- // Cannot assume that callers won't use different Character objects
305- CSVFormat .DEFAULT .withEscape (new Character ('!' )).withCommentStart (new Character ('!' )).validate ();
306- }
307-
308223 @ Test (expected = IllegalStateException .class )
309224 public void testQuoteCharSameAsCommentStartThrowsException () {
310225 CSVFormat .DEFAULT .withQuoteChar ('!' ).withCommentStart ('!' ).validate ();
@@ -337,28 +252,113 @@ public void testRFC4180() {
337252 assertEquals ("\r \n " , RFC4180 .getRecordSeparator ());
338253 }
339254
255+ @ SuppressWarnings ("boxing" ) // no need to worry about boxing here
256+ @ Test
257+ public void testSerialization () throws Exception {
258+ final ByteArrayOutputStream out = new ByteArrayOutputStream ();
259+
260+ final ObjectOutputStream oos = new ObjectOutputStream (out );
261+ oos .writeObject (CSVFormat .DEFAULT );
262+ oos .flush ();
263+ oos .close ();
264+
265+ final ObjectInputStream in = new ObjectInputStream (new ByteArrayInputStream (out .toByteArray ()));
266+ final CSVFormat format = (CSVFormat ) in .readObject ();
267+
268+ assertNotNull (format );
269+ assertEquals ("delimiter" , CSVFormat .DEFAULT .getDelimiter (), format .getDelimiter ());
270+ assertEquals ("encapsulator" , CSVFormat .DEFAULT .getQuoteChar (), format .getQuoteChar ());
271+ assertEquals ("comment start" , CSVFormat .DEFAULT .getCommentStart (), format .getCommentStart ());
272+ assertEquals ("line separator" , CSVFormat .DEFAULT .getRecordSeparator (), format .getRecordSeparator ());
273+ assertEquals ("escape" , CSVFormat .DEFAULT .getEscape (), format .getEscape ());
274+ assertEquals ("trim" , CSVFormat .DEFAULT .getIgnoreSurroundingSpaces (), format .getIgnoreSurroundingSpaces ());
275+ assertEquals ("empty lines" , CSVFormat .DEFAULT .getIgnoreEmptyLines (), format .getIgnoreEmptyLines ());
276+ }
277+
278+ @ Test
279+ public void testWithCommentStart () throws Exception {
280+ CSVFormat formatWithCommentStart = CSVFormat .DEFAULT .withCommentStart ('#' );
281+ assertEquals ( Character .valueOf ('#' ), formatWithCommentStart .getCommentStart ());
282+ }
283+
340284 @ Test (expected = IllegalArgumentException .class )
341285 public void testWithCommentStartCRThrowsException () {
342286 CSVFormat .DEFAULT .withCommentStart (CR ).validate ();
343287 }
344288
289+ @ Test
290+ public void testWithDelimiter () throws Exception {
291+ CSVFormat formatWithDelimiter = CSVFormat .DEFAULT .withDelimiter ('!' );
292+ assertEquals ('!' , formatWithDelimiter .getDelimiter ());
293+ }
294+
345295 @ Test (expected = IllegalArgumentException .class )
346296 public void testWithDelimiterLFThrowsException () {
347297 CSVFormat .DEFAULT .withDelimiter (LF ).validate ();
348298 }
349299
300+ @ Test
301+ public void testWithEscape () throws Exception {
302+ CSVFormat formatWithEscape = CSVFormat .DEFAULT .withEscape ('&' );
303+ assertEquals (Character .valueOf ('&' ), formatWithEscape .getEscape ());
304+ }
305+
350306 @ Test (expected = IllegalArgumentException .class )
351307 public void testWithEscapeCRThrowsExceptions () {
352308 CSVFormat .DEFAULT .withEscape (CR ).validate ();
353309 }
354310
311+ @ Test
312+ public void testWithHeader () throws Exception {
313+ String [] header = new String []{"one" , "two" , "three" };
314+ // withHeader() makes a copy of the header array.
315+ CSVFormat formatWithHeader = CSVFormat .DEFAULT .withHeader (header );
316+ assertArrayEquals (header , formatWithHeader .getHeader ());
317+ assertNotSame (header , formatWithHeader .getHeader ());
318+ header [0 ] = "A" ;
319+ header [1 ] = "B" ;
320+ header [2 ] = "C" ;
321+ assertFalse (Arrays .equals (formatWithHeader .getHeader (), header ));
322+ }
323+
324+ @ Test
325+ public void testWithIgnoreEmptyLines () throws Exception {
326+ assertFalse (CSVFormat .DEFAULT .withIgnoreEmptyLines (false ).getIgnoreEmptyLines ());
327+ assertTrue (CSVFormat .DEFAULT .withIgnoreEmptyLines (true ).getIgnoreEmptyLines ());
328+ }
329+
330+ @ Test
331+ public void testWithIgnoreSurround () throws Exception {
332+ assertFalse (CSVFormat .DEFAULT .withIgnoreSurroundingSpaces (false ).getIgnoreSurroundingSpaces ());
333+ assertTrue (CSVFormat .DEFAULT .withIgnoreSurroundingSpaces (true ).getIgnoreSurroundingSpaces ());
334+ }
335+
336+ @ Test
337+ public void testWithNullString () throws Exception {
338+ CSVFormat formatWithNullString = CSVFormat .DEFAULT .withNullString ("null" );
339+ assertEquals ("null" , formatWithNullString .getNullString ());
340+ }
341+
342+ @ Test
343+ public void testWithQuoteChar () throws Exception {
344+ CSVFormat formatWithQuoteChar = CSVFormat .DEFAULT .withQuoteChar ('"' );
345+ assertEquals (Character .valueOf ('"' ), formatWithQuoteChar .getQuoteChar ());
346+ }
347+
355348 @ Test (expected = IllegalArgumentException .class )
356349 public void testWithQuoteLFThrowsException () {
357350 CSVFormat .DEFAULT .withQuoteChar (LF ).validate ();
358351 }
359352
360- private static void assertNotEquals (final Object right , final Object left ) {
361- assertFalse (right .equals (left ));
362- assertFalse (left .equals (right ));
353+ @ Test
354+ public void testWithQuotePolicy () throws Exception {
355+ CSVFormat formatWithQuotePolicy = CSVFormat .DEFAULT .withQuotePolicy (Quote .ALL );
356+ assertEquals (Quote .ALL , formatWithQuotePolicy .getQuotePolicy ());
357+ }
358+
359+ @ Test
360+ public void testWithRecordSeparator () throws Exception {
361+ CSVFormat formatWithRecordSeparator = CSVFormat .DEFAULT .withRecordSeparator ('!' );
362+ assertEquals ("!" , formatWithRecordSeparator .getRecordSeparator ());
363363 }
364364}
0 commit comments