1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.commons.csv;
21
22 import static org.apache.commons.csv.CSVFormat.RFC4180;
23 import static org.apache.commons.csv.Constants.CR;
24 import static org.apache.commons.csv.Constants.CRLF;
25 import static org.apache.commons.csv.Constants.LF;
26 import static org.junit.jupiter.api.Assertions.assertArrayEquals;
27 import static org.junit.jupiter.api.Assertions.assertEquals;
28 import static org.junit.jupiter.api.Assertions.assertFalse;
29 import static org.junit.jupiter.api.Assertions.assertNotNull;
30 import static org.junit.jupiter.api.Assertions.assertNotSame;
31 import static org.junit.jupiter.api.Assertions.assertNull;
32 import static org.junit.jupiter.api.Assertions.assertThrows;
33 import static org.junit.jupiter.api.Assertions.assertTrue;
34 import static org.junit.jupiter.api.Assertions.fail;
35
36 import java.io.ByteArrayInputStream;
37 import java.io.ByteArrayOutputStream;
38 import java.io.IOException;
39 import java.io.ObjectInputStream;
40 import java.io.ObjectOutputStream;
41 import java.io.Reader;
42 import java.io.StringReader;
43 import java.lang.reflect.Method;
44 import java.lang.reflect.Modifier;
45 import java.sql.ResultSet;
46 import java.sql.SQLException;
47 import java.util.Arrays;
48 import java.util.Objects;
49
50 import org.apache.commons.csv.CSVFormat.Builder;
51 import org.junit.jupiter.api.Assertions;
52 import org.junit.jupiter.api.Test;
53
54
55
56
57 public class CSVFormatTest {
58
59 public enum EmptyEnum {
60
61 }
62
63 public enum Header {
64 Name, Email, Phone
65 }
66
67 private static void assertNotEquals(final Object right, final Object left) {
68 Assertions.assertNotEquals(right, left);
69 Assertions.assertNotEquals(left, right);
70 }
71
72 private static CSVFormat copy(final CSVFormat format) {
73 return format.builder().setDelimiter(format.getDelimiter()).get();
74 }
75
76 private void assertNotEquals(final String name, final String type, final Object left, final Object right) {
77 if (left.equals(right) || right.equals(left)) {
78 fail("Objects must not compare equal for " + name + "(" + type + ")");
79 }
80 if (left.hashCode() == right.hashCode()) {
81 fail("Hash code should not be equal for " + name + "(" + type + ")");
82 }
83 }
84
85 @Test
86 public void testBuildVsGet() {
87 final Builder builder = CSVFormat.DEFAULT.builder();
88 assertNotSame(builder.get(), builder.build());
89 }
90
91 @Test
92 public void testDelimiterCharLineBreakCrThrowsException1() {
93 assertThrows(IllegalArgumentException.class, () -> CSVFormat.DEFAULT.builder().setDelimiter(Constants.CR).get());
94 }
95
96 @Test
97 public void testDelimiterCharLineBreakLfThrowsException1() {
98 assertThrows(IllegalArgumentException.class, () -> CSVFormat.DEFAULT.builder().setDelimiter(Constants.LF).get());
99 }
100
101 @Test
102 public void testDelimiterEmptyStringThrowsException1() {
103 assertThrows(IllegalArgumentException.class, () -> CSVFormat.DEFAULT.builder().setDelimiter("").get());
104 }
105
106 @SuppressWarnings("deprecation")
107 @Test
108 public void testDelimiterSameAsCommentStartThrowsException_Deprecated() {
109 assertThrows(IllegalArgumentException.class, () -> CSVFormat.DEFAULT.withDelimiter('!').withCommentMarker('!'));
110 }
111
112 @Test
113 public void testDelimiterSameAsCommentStartThrowsException1() {
114 assertThrows(IllegalArgumentException.class, () -> CSVFormat.DEFAULT.builder().setDelimiter('!').setCommentMarker('!').get());
115 }
116
117 @SuppressWarnings("deprecation")
118 @Test
119 public void testDelimiterSameAsEscapeThrowsException_Deprecated() {
120 assertThrows(IllegalArgumentException.class, () -> CSVFormat.DEFAULT.withDelimiter('!').withEscape('!'));
121 }
122
123 @Test
124 public void testDelimiterSameAsEscapeThrowsException1() {
125 assertThrows(IllegalArgumentException.class, () -> CSVFormat.DEFAULT.builder().setDelimiter('!').setEscape('!').get());
126 }
127
128 @Test
129 public void testDelimiterSameAsRecordSeparatorThrowsException() {
130 assertThrows(IllegalArgumentException.class, () -> CSVFormat.newFormat(CR));
131 }
132
133 @Test
134 public void testDelimiterStringLineBreakCrThrowsException1() {
135 assertThrows(IllegalArgumentException.class, () -> CSVFormat.DEFAULT.builder().setDelimiter(String.valueOf(Constants.CR)).get());
136 }
137
138 @Test
139 public void testDelimiterStringLineBreakLfThrowsException1() {
140 assertThrows(IllegalArgumentException.class, () -> CSVFormat.DEFAULT.builder().setDelimiter(String.valueOf(Constants.LF)).get());
141 }
142
143 @Test
144 public void testDuplicateHeaderElements() {
145 final String[] header = { "A", "A" };
146 final CSVFormat format = CSVFormat.DEFAULT.builder().setHeader(header).get();
147 assertEquals(2, format.getHeader().length);
148 assertArrayEquals(header, format.getHeader());
149 }
150
151 @SuppressWarnings("deprecation")
152 @Test
153 public void testDuplicateHeaderElements_Deprecated() {
154 final String[] header = { "A", "A" };
155 final CSVFormat format = CSVFormat.DEFAULT.withHeader(header);
156 assertEquals(2, format.getHeader().length);
157 assertArrayEquals(header, format.getHeader());
158 }
159
160 @Test
161 public void testDuplicateHeaderElementsFalse() {
162 assertThrows(IllegalArgumentException.class, () -> CSVFormat.DEFAULT.builder().setAllowDuplicateHeaderNames(false).setHeader("A", "A").get());
163 }
164
165 @SuppressWarnings("deprecation")
166 @Test
167 public void testDuplicateHeaderElementsFalse_Deprecated() {
168 assertThrows(IllegalArgumentException.class, () -> CSVFormat.DEFAULT.withAllowDuplicateHeaderNames(false).withHeader("A", "A"));
169 }
170
171 @Test
172 public void testDuplicateHeaderElementsTrue() {
173 CSVFormat.DEFAULT.builder().setAllowDuplicateHeaderNames(true).setHeader("A", "A").get();
174 }
175
176 @SuppressWarnings("deprecation")
177 @Test
178 public void testDuplicateHeaderElementsTrue_Deprecated() {
179 CSVFormat.DEFAULT.withAllowDuplicateHeaderNames(true).withHeader("A", "A");
180 }
181
182 @Test
183 public void testDuplicateHeaderElementsTrueContainsEmpty1() {
184 CSVFormat.DEFAULT.builder().setAllowDuplicateHeaderNames(false).setHeader("A", "", "B", "").get();
185 }
186
187 @Test
188 public void testDuplicateHeaderElementsTrueContainsEmpty2() {
189 CSVFormat.DEFAULT.builder().setDuplicateHeaderMode(DuplicateHeaderMode.ALLOW_EMPTY).setHeader("A", "", "B", "").get();
190 }
191
192 @Test
193 public void testDuplicateHeaderElementsTrueContainsEmpty3() {
194 CSVFormat.DEFAULT.builder().setAllowDuplicateHeaderNames(false).setAllowMissingColumnNames(true).setHeader("A", "", "B", "").get();
195 }
196
197 @Test
198 public void testEquals() {
199 final CSVFormat right = CSVFormat.DEFAULT;
200 final CSVFormat left = copy(right);
201 Assertions.assertNotEquals(null, right);
202 Assertions.assertNotEquals("A String Instance", right);
203 assertEquals(right, right);
204 assertEquals(right, left);
205 assertEquals(left, right);
206 assertEquals(right.hashCode(), right.hashCode());
207 assertEquals(right.hashCode(), left.hashCode());
208 }
209
210 @Test
211 public void testEqualsCommentStart() {
212 final CSVFormat right = CSVFormat.newFormat('\'').builder().setQuote('"').setCommentMarker('#').setQuoteMode(QuoteMode.ALL).get();
213 final CSVFormat left = right.builder().setCommentMarker('!').get();
214
215 assertNotEquals(right, left);
216 }
217
218 @SuppressWarnings("deprecation")
219 @Test
220 public void testEqualsCommentStart_Deprecated() {
221 final CSVFormat right = CSVFormat.newFormat('\'').withQuote('"').withCommentMarker('#').withQuoteMode(QuoteMode.ALL);
222 final CSVFormat left = right.withCommentMarker('!');
223
224 assertNotEquals(right, left);
225 }
226
227 @Test
228 public void testEqualsDelimiter() {
229 final CSVFormat right = CSVFormat.newFormat('!');
230 final CSVFormat left = CSVFormat.newFormat('?');
231
232 assertNotEquals(right, left);
233 }
234
235 @Test
236 public void testEqualsEscape() {
237 final CSVFormat right = CSVFormat.newFormat('\'').builder().setQuote('"').setCommentMarker('#').setEscape('+').setQuoteMode(QuoteMode.ALL).get();
238 final CSVFormat left = right.builder().setEscape('!').get();
239
240 assertNotEquals(right, left);
241 }
242
243 @SuppressWarnings("deprecation")
244 @Test
245 public void testEqualsEscape_Deprecated() {
246 final CSVFormat right = CSVFormat.newFormat('\'').withQuote('"').withCommentMarker('#').withEscape('+').withQuoteMode(QuoteMode.ALL);
247 final CSVFormat left = right.withEscape('!');
248
249 assertNotEquals(right, left);
250 }
251
252 @Test
253 public void testEqualsHash() throws Exception {
254 final Method[] methods = CSVFormat.class.getDeclaredMethods();
255 for (final Method method : methods) {
256 if (Modifier.isPublic(method.getModifiers())) {
257 final String name = method.getName();
258 if (name.startsWith("with")) {
259 for (final Class<?> cls : method.getParameterTypes()) {
260 final String type = cls.getCanonicalName();
261 switch (type) {
262 case "boolean": {
263 final Object defTrue = method.invoke(CSVFormat.DEFAULT, Boolean.TRUE);
264 final Object defFalse = method.invoke(CSVFormat.DEFAULT, Boolean.FALSE);
265 assertNotEquals(name, type, defTrue, defFalse);
266 break;
267 }
268 case "char": {
269 final Object a = method.invoke(CSVFormat.DEFAULT, 'a');
270 final Object b = method.invoke(CSVFormat.DEFAULT, 'b');
271 assertNotEquals(name, type, a, b);
272 break;
273 }
274 case "java.lang.Character": {
275 final Object a = method.invoke(CSVFormat.DEFAULT, new Object[] { null });
276 final Object b = method.invoke(CSVFormat.DEFAULT, Character.valueOf('d'));
277 assertNotEquals(name, type, a, b);
278 break;
279 }
280 case "java.lang.String": {
281 final Object a = method.invoke(CSVFormat.DEFAULT, new Object[] { null });
282 final Object b = method.invoke(CSVFormat.DEFAULT, "e");
283 assertNotEquals(name, type, a, b);
284 break;
285 }
286 case "java.lang.String[]": {
287 final Object a = method.invoke(CSVFormat.DEFAULT, new Object[] { new String[] { null, null } });
288 final Object b = method.invoke(CSVFormat.DEFAULT, new Object[] { new String[] { "f", "g" } });
289 assertNotEquals(name, type, a, b);
290 break;
291 }
292 case "org.apache.commons.csv.QuoteMode": {
293 final Object a = method.invoke(CSVFormat.DEFAULT, QuoteMode.MINIMAL);
294 final Object b = method.invoke(CSVFormat.DEFAULT, QuoteMode.ALL);
295 assertNotEquals(name, type, a, b);
296 break;
297 }
298 case "org.apache.commons.csv.DuplicateHeaderMode": {
299 final Object a = method.invoke(CSVFormat.DEFAULT, DuplicateHeaderMode.ALLOW_ALL);
300 final Object b = method.invoke(CSVFormat.DEFAULT, DuplicateHeaderMode.DISALLOW);
301 assertNotEquals(name, type, a, b);
302 break;
303 }
304 case "java.lang.Object[]": {
305 final Object a = method.invoke(CSVFormat.DEFAULT, new Object[] { new Object[] { null, null } });
306 final Object b = method.invoke(CSVFormat.DEFAULT, new Object[] { new Object[] { new Object(), new Object() } });
307 assertNotEquals(name, type, a, b);
308 break;
309 }
310 default:
311 if ("withHeader".equals(name)) {
312
313 } else {
314 fail("Unhandled method: " + name + "(" + type + ")");
315 }
316 break;
317 }
318 }
319 }
320 }
321 }
322 }
323
324 @Test
325 public void testEqualsHeader() {
326 final CSVFormat right = CSVFormat.newFormat('\'').builder().setRecordSeparator(CR).setCommentMarker('#').setEscape('+').setHeader("One", "Two", "Three")
327 .setIgnoreEmptyLines(true).setIgnoreSurroundingSpaces(true).setQuote('"').setQuoteMode(QuoteMode.ALL).get();
328 final CSVFormat left = right.builder().setHeader("Three", "Two", "One").get();
329
330 assertNotEquals(right, left);
331 }
332
333 @SuppressWarnings("deprecation")
334 @Test
335 public void testEqualsHeader_Deprecated() {
336 final CSVFormat right = CSVFormat.newFormat('\'').withRecordSeparator(CR).withCommentMarker('#').withEscape('+').withHeader("One", "Two", "Three")
337 .withIgnoreEmptyLines().withIgnoreSurroundingSpaces().withQuote('"').withQuoteMode(QuoteMode.ALL);
338 final CSVFormat left = right.withHeader("Three", "Two", "One");
339
340 assertNotEquals(right, left);
341 }
342
343 @Test
344 public void testEqualsIgnoreEmptyLines() {
345 final CSVFormat right = CSVFormat.newFormat('\'').builder().setCommentMarker('#').setEscape('+').setIgnoreEmptyLines(true)
346 .setIgnoreSurroundingSpaces(true).setQuote('"').setQuoteMode(QuoteMode.ALL).get();
347 final CSVFormat left = right.builder().setIgnoreEmptyLines(false).get();
348
349 assertNotEquals(right, left);
350 }
351
352 @SuppressWarnings("deprecation")
353 @Test
354 public void testEqualsIgnoreEmptyLines_Deprecated() {
355 final CSVFormat right = CSVFormat.newFormat('\'').withCommentMarker('#').withEscape('+').withIgnoreEmptyLines().withIgnoreSurroundingSpaces()
356 .withQuote('"').withQuoteMode(QuoteMode.ALL);
357 final CSVFormat left = right.withIgnoreEmptyLines(false);
358
359 assertNotEquals(right, left);
360 }
361
362 @Test
363 public void testEqualsIgnoreSurroundingSpaces() {
364 final CSVFormat right = CSVFormat.newFormat('\'').builder().setCommentMarker('#').setEscape('+').setIgnoreSurroundingSpaces(true).setQuote('"')
365 .setQuoteMode(QuoteMode.ALL).get();
366 final CSVFormat left = right.builder().setIgnoreSurroundingSpaces(false).get();
367
368 assertNotEquals(right, left);
369 }
370
371 @SuppressWarnings("deprecation")
372 @Test
373 public void testEqualsIgnoreSurroundingSpaces_Deprecated() {
374 final CSVFormat right = CSVFormat.newFormat('\'').withCommentMarker('#').withEscape('+').withIgnoreSurroundingSpaces().withQuote('"')
375 .withQuoteMode(QuoteMode.ALL);
376 final CSVFormat left = right.withIgnoreSurroundingSpaces(false);
377
378 assertNotEquals(right, left);
379 }
380
381 @Test
382 public void testEqualsLeftNoQuoteRightQuote() {
383 final CSVFormat left = CSVFormat.newFormat(',').builder().setQuote(null).get();
384 final CSVFormat right = left.builder().setQuote('#').get();
385
386 assertNotEquals(left, right);
387 }
388
389 @SuppressWarnings("deprecation")
390 @Test
391 public void testEqualsLeftNoQuoteRightQuote_Deprecated() {
392 final CSVFormat left = CSVFormat.newFormat(',').withQuote(null);
393 final CSVFormat right = left.withQuote('#');
394
395 assertNotEquals(left, right);
396 }
397
398 @Test
399 public void testEqualsNoQuotes() {
400 final CSVFormat left = CSVFormat.newFormat(',').builder().setQuote(null).get();
401 final CSVFormat right = left.builder().setQuote(null).get();
402
403 assertEquals(left, right);
404 }
405
406 @SuppressWarnings("deprecation")
407 @Test
408 public void testEqualsNoQuotes_Deprecated() {
409 final CSVFormat left = CSVFormat.newFormat(',').withQuote(null);
410 final CSVFormat right = left.withQuote(null);
411
412 assertEquals(left, right);
413 }
414
415 @Test
416 public void testEqualsNullString() {
417 final CSVFormat right = CSVFormat.newFormat('\'').builder().setRecordSeparator(CR).setCommentMarker('#').setEscape('+').setIgnoreEmptyLines(true)
418 .setIgnoreSurroundingSpaces(true).setQuote('"').setQuoteMode(QuoteMode.ALL).setNullString("null").get();
419 final CSVFormat left = right.builder().setNullString("---").get();
420
421 assertNotEquals(right, left);
422 }
423
424 @SuppressWarnings("deprecation")
425 @Test
426 public void testEqualsNullString_Deprecated() {
427 final CSVFormat right = CSVFormat.newFormat('\'').withRecordSeparator(CR).withCommentMarker('#').withEscape('+').withIgnoreEmptyLines()
428 .withIgnoreSurroundingSpaces().withQuote('"').withQuoteMode(QuoteMode.ALL).withNullString("null");
429 final CSVFormat left = right.withNullString("---");
430
431 assertNotEquals(right, left);
432 }
433
434 @Test
435 public void testEqualsOne() {
436
437 final CSVFormat csvFormatOne = CSVFormat.INFORMIX_UNLOAD;
438 final CSVFormat csvFormatTwo = CSVFormat.MYSQL;
439
440 assertEquals('\\', (char) csvFormatOne.getEscapeCharacter());
441 assertEquals('\\', csvFormatOne.getEscapeChar());
442 assertNull(csvFormatOne.getQuoteMode());
443
444 assertTrue(csvFormatOne.getIgnoreEmptyLines());
445 assertFalse(csvFormatOne.getSkipHeaderRecord());
446
447 assertFalse(csvFormatOne.getIgnoreHeaderCase());
448 assertNull(csvFormatOne.getCommentMarker());
449
450 assertFalse(csvFormatOne.isCommentMarkerSet());
451 assertTrue(csvFormatOne.isQuoteCharacterSet());
452
453 assertEquals('|', csvFormatOne.getDelimiter());
454 assertFalse(csvFormatOne.getAllowMissingColumnNames());
455
456 assertTrue(csvFormatOne.isEscapeCharacterSet());
457 assertEquals("\n", csvFormatOne.getRecordSeparator());
458
459 assertEquals('\"', (char) csvFormatOne.getQuoteCharacter());
460 assertFalse(csvFormatOne.getTrailingDelimiter());
461
462 assertFalse(csvFormatOne.getTrim());
463 assertFalse(csvFormatOne.isNullStringSet());
464
465 assertNull(csvFormatOne.getNullString());
466 assertFalse(csvFormatOne.getIgnoreSurroundingSpaces());
467
468 assertTrue(csvFormatTwo.isEscapeCharacterSet());
469 assertNull(csvFormatTwo.getQuoteCharacter());
470
471 assertFalse(csvFormatTwo.getAllowMissingColumnNames());
472 assertEquals(QuoteMode.ALL_NON_NULL, csvFormatTwo.getQuoteMode());
473
474 assertEquals('\t', csvFormatTwo.getDelimiter());
475 assertArrayEquals(new char[] { '\t' }, csvFormatTwo.getDelimiterCharArray());
476 assertEquals("\t", csvFormatTwo.getDelimiterString());
477 assertEquals("\n", csvFormatTwo.getRecordSeparator());
478
479 assertFalse(csvFormatTwo.isQuoteCharacterSet());
480 assertTrue(csvFormatTwo.isNullStringSet());
481
482 assertEquals('\\', (char) csvFormatTwo.getEscapeCharacter());
483 assertFalse(csvFormatTwo.getIgnoreHeaderCase());
484
485 assertFalse(csvFormatTwo.getTrim());
486 assertFalse(csvFormatTwo.getIgnoreEmptyLines());
487
488 assertEquals("\\N", csvFormatTwo.getNullString());
489 assertFalse(csvFormatTwo.getIgnoreSurroundingSpaces());
490
491 assertFalse(csvFormatTwo.getTrailingDelimiter());
492 assertFalse(csvFormatTwo.getSkipHeaderRecord());
493
494 assertNull(csvFormatTwo.getCommentMarker());
495 assertFalse(csvFormatTwo.isCommentMarkerSet());
496
497 assertNotSame(csvFormatTwo, csvFormatOne);
498 Assertions.assertNotEquals(csvFormatTwo, csvFormatOne);
499
500 assertEquals('\\', (char) csvFormatOne.getEscapeCharacter());
501 assertNull(csvFormatOne.getQuoteMode());
502
503 assertTrue(csvFormatOne.getIgnoreEmptyLines());
504 assertFalse(csvFormatOne.getSkipHeaderRecord());
505
506 assertFalse(csvFormatOne.getIgnoreHeaderCase());
507 assertNull(csvFormatOne.getCommentMarker());
508
509 assertFalse(csvFormatOne.isCommentMarkerSet());
510 assertTrue(csvFormatOne.isQuoteCharacterSet());
511
512 assertEquals('|', csvFormatOne.getDelimiter());
513 assertFalse(csvFormatOne.getAllowMissingColumnNames());
514
515 assertTrue(csvFormatOne.isEscapeCharacterSet());
516 assertEquals("\n", csvFormatOne.getRecordSeparator());
517
518 assertEquals('\"', (char) csvFormatOne.getQuoteCharacter());
519 assertFalse(csvFormatOne.getTrailingDelimiter());
520
521 assertFalse(csvFormatOne.getTrim());
522 assertFalse(csvFormatOne.isNullStringSet());
523
524 assertNull(csvFormatOne.getNullString());
525 assertFalse(csvFormatOne.getIgnoreSurroundingSpaces());
526
527 assertTrue(csvFormatTwo.isEscapeCharacterSet());
528 assertNull(csvFormatTwo.getQuoteCharacter());
529
530 assertFalse(csvFormatTwo.getAllowMissingColumnNames());
531 assertEquals(QuoteMode.ALL_NON_NULL, csvFormatTwo.getQuoteMode());
532
533 assertEquals('\t', csvFormatTwo.getDelimiter());
534 assertEquals("\n", csvFormatTwo.getRecordSeparator());
535
536 assertFalse(csvFormatTwo.isQuoteCharacterSet());
537 assertTrue(csvFormatTwo.isNullStringSet());
538
539 assertEquals('\\', (char) csvFormatTwo.getEscapeCharacter());
540 assertFalse(csvFormatTwo.getIgnoreHeaderCase());
541
542 assertFalse(csvFormatTwo.getTrim());
543 assertFalse(csvFormatTwo.getIgnoreEmptyLines());
544
545 assertEquals("\\N", csvFormatTwo.getNullString());
546 assertFalse(csvFormatTwo.getIgnoreSurroundingSpaces());
547
548 assertFalse(csvFormatTwo.getTrailingDelimiter());
549 assertFalse(csvFormatTwo.getSkipHeaderRecord());
550
551 assertNull(csvFormatTwo.getCommentMarker());
552 assertFalse(csvFormatTwo.isCommentMarkerSet());
553
554 assertNotSame(csvFormatOne, csvFormatTwo);
555 assertNotSame(csvFormatTwo, csvFormatOne);
556
557 Assertions.assertNotEquals(csvFormatOne, csvFormatTwo);
558 Assertions.assertNotEquals(csvFormatTwo, csvFormatOne);
559
560 Assertions.assertNotEquals(csvFormatTwo, csvFormatOne);
561
562 }
563
564 @Test
565 public void testEqualsQuoteChar() {
566 final CSVFormat right = CSVFormat.newFormat('\'').builder().setQuote('"').get();
567 final CSVFormat left = right.builder().setQuote('!').get();
568
569 assertNotEquals(right, left);
570 }
571
572 @SuppressWarnings("deprecation")
573 @Test
574 public void testEqualsQuoteChar_Deprecated() {
575 final CSVFormat right = CSVFormat.newFormat('\'').withQuote('"');
576 final CSVFormat left = right.withQuote('!');
577
578 assertNotEquals(right, left);
579 }
580
581 @Test
582 public void testEqualsQuotePolicy() {
583 final CSVFormat right = CSVFormat.newFormat('\'').builder().setQuote('"').setQuoteMode(QuoteMode.ALL).get();
584 final CSVFormat left = right.builder().setQuoteMode(QuoteMode.MINIMAL).get();
585
586 assertNotEquals(right, left);
587 }
588
589 @SuppressWarnings("deprecation")
590 @Test
591 public void testEqualsQuotePolicy_Deprecated() {
592 final CSVFormat right = CSVFormat.newFormat('\'').withQuote('"').withQuoteMode(QuoteMode.ALL);
593 final CSVFormat left = right.withQuoteMode(QuoteMode.MINIMAL);
594
595 assertNotEquals(right, left);
596 }
597
598 @Test
599 public void testEqualsRecordSeparator() {
600 final CSVFormat right = CSVFormat.newFormat('\'').builder().setRecordSeparator(CR).setCommentMarker('#').setEscape('+').setIgnoreEmptyLines(true)
601 .setIgnoreSurroundingSpaces(true).setQuote('"').setQuoteMode(QuoteMode.ALL).get();
602 final CSVFormat left = right.builder().setRecordSeparator(LF).get();
603
604 assertNotEquals(right, left);
605 }
606
607 @SuppressWarnings("deprecation")
608 @Test
609 public void testEqualsRecordSeparator_Deprecated() {
610 final CSVFormat right = CSVFormat.newFormat('\'').withRecordSeparator(CR).withCommentMarker('#').withEscape('+').withIgnoreEmptyLines()
611 .withIgnoreSurroundingSpaces().withQuote('"').withQuoteMode(QuoteMode.ALL);
612 final CSVFormat left = right.withRecordSeparator(LF);
613
614 assertNotEquals(right, left);
615 }
616
617 public void testEqualsSkipHeaderRecord() {
618 final CSVFormat right = CSVFormat.newFormat('\'').builder().setRecordSeparator(CR).setCommentMarker('#').setEscape('+').setIgnoreEmptyLines(true)
619 .setIgnoreSurroundingSpaces(true).setQuote('"').setQuoteMode(QuoteMode.ALL).setNullString("null").setSkipHeaderRecord(true).get();
620 final CSVFormat left = right.builder().setSkipHeaderRecord(false).get();
621
622 assertNotEquals(right, left);
623 }
624
625 @SuppressWarnings("deprecation")
626 @Test
627 public void testEqualsSkipHeaderRecord_Deprecated() {
628 final CSVFormat right = CSVFormat.newFormat('\'').withRecordSeparator(CR).withCommentMarker('#').withEscape('+').withIgnoreEmptyLines()
629 .withIgnoreSurroundingSpaces().withQuote('"').withQuoteMode(QuoteMode.ALL).withNullString("null").withSkipHeaderRecord();
630 final CSVFormat left = right.withSkipHeaderRecord(false);
631
632 assertNotEquals(right, left);
633 }
634
635 @Test
636 public void testEqualsWithNull() {
637
638 final CSVFormat csvFormat = CSVFormat.POSTGRESQL_TEXT;
639
640 assertEquals('\\', (char) csvFormat.getEscapeCharacter());
641 assertFalse(csvFormat.getIgnoreSurroundingSpaces());
642
643 assertFalse(csvFormat.getTrailingDelimiter());
644 assertFalse(csvFormat.getTrim());
645
646 assertFalse(csvFormat.isQuoteCharacterSet());
647 assertEquals("\\N", csvFormat.getNullString());
648
649 assertFalse(csvFormat.getIgnoreHeaderCase());
650 assertTrue(csvFormat.isEscapeCharacterSet());
651
652 assertFalse(csvFormat.isCommentMarkerSet());
653 assertNull(csvFormat.getCommentMarker());
654
655 assertFalse(csvFormat.getAllowMissingColumnNames());
656 assertEquals(QuoteMode.ALL_NON_NULL, csvFormat.getQuoteMode());
657
658 assertEquals('\t', csvFormat.getDelimiter());
659 assertFalse(csvFormat.getSkipHeaderRecord());
660
661 assertEquals("\n", csvFormat.getRecordSeparator());
662 assertFalse(csvFormat.getIgnoreEmptyLines());
663
664 assertNull(csvFormat.getQuoteCharacter());
665 assertTrue(csvFormat.isNullStringSet());
666
667 assertEquals('\\', (char) csvFormat.getEscapeCharacter());
668 assertFalse(csvFormat.getIgnoreSurroundingSpaces());
669
670 assertFalse(csvFormat.getTrailingDelimiter());
671 assertFalse(csvFormat.getTrim());
672
673 assertFalse(csvFormat.isQuoteCharacterSet());
674 assertEquals("\\N", csvFormat.getNullString());
675
676 assertFalse(csvFormat.getIgnoreHeaderCase());
677 assertTrue(csvFormat.isEscapeCharacterSet());
678
679 assertFalse(csvFormat.isCommentMarkerSet());
680 assertNull(csvFormat.getCommentMarker());
681
682 assertFalse(csvFormat.getAllowMissingColumnNames());
683 assertEquals(QuoteMode.ALL_NON_NULL, csvFormat.getQuoteMode());
684
685 assertEquals('\t', csvFormat.getDelimiter());
686 assertFalse(csvFormat.getSkipHeaderRecord());
687
688 assertEquals("\n", csvFormat.getRecordSeparator());
689 assertFalse(csvFormat.getIgnoreEmptyLines());
690
691 assertNull(csvFormat.getQuoteCharacter());
692 assertTrue(csvFormat.isNullStringSet());
693
694 Assertions.assertNotEquals(null, csvFormat);
695
696 }
697
698 @Test
699 public void testEscapeSameAsCommentStartThrowsException() {
700 assertThrows(IllegalArgumentException.class, () -> CSVFormat.DEFAULT.builder().setEscape('!').setCommentMarker('!').get());
701 }
702
703 @SuppressWarnings("deprecation")
704 @Test
705 public void testEscapeSameAsCommentStartThrowsException_Deprecated() {
706 assertThrows(IllegalArgumentException.class, () -> CSVFormat.DEFAULT.withEscape('!').withCommentMarker('!'));
707 }
708
709 @Test
710 public void testEscapeSameAsCommentStartThrowsExceptionForWrapperType() {
711
712 assertThrows(IllegalArgumentException.class,
713 () -> CSVFormat.DEFAULT.builder().setEscape(Character.valueOf('!')).setCommentMarker(Character.valueOf('!')).get());
714 }
715
716 @SuppressWarnings("deprecation")
717 @Test
718 public void testEscapeSameAsCommentStartThrowsExceptionForWrapperType_Deprecated() {
719
720 assertThrows(IllegalArgumentException.class, () -> CSVFormat.DEFAULT.withEscape(Character.valueOf('!')).withCommentMarker(Character.valueOf('!')));
721 }
722
723 @Test
724 public void testFormat() {
725 final CSVFormat format = CSVFormat.DEFAULT;
726
727 assertEquals("", format.format());
728 assertEquals("a,b,c", format.format("a", "b", "c"));
729 assertEquals("\"x,y\",z", format.format("x,y", "z"));
730 }
731
732 @Test
733 public void testFormatThrowsNullPointerException() {
734
735 final CSVFormat csvFormat = CSVFormat.MYSQL;
736
737 final NullPointerException e = assertThrows(NullPointerException.class, () -> csvFormat.format((Object[]) null));
738 assertEquals(Objects.class.getName(), e.getStackTrace()[0].getClassName());
739 }
740
741 @Test
742 public void testFormatToString() {
743
744 final CSVFormat format = CSVFormat.RFC4180
745 .withEscape('?')
746 .withDelimiter(',')
747 .withQuoteMode(QuoteMode.MINIMAL)
748 .withRecordSeparator(CRLF)
749 .withQuote('"')
750 .withNullString("")
751 .withIgnoreHeaderCase(true)
752 .withHeaderComments("This is HeaderComments")
753 .withHeader("col1", "col2", "col3");
754
755 assertEquals(
756 "Delimiter=<,> Escape=<?> QuoteChar=<\"> QuoteMode=<MINIMAL> NullString=<> RecordSeparator=<" + CRLF +
757 "> IgnoreHeaderCase:ignored SkipHeaderRecord:false HeaderComments:[This is HeaderComments] Header:[col1, col2, col3]",
758 format.toString());
759 }
760
761 @Test
762 public void testGetAllowDuplicateHeaderNames() {
763 final Builder builder = CSVFormat.DEFAULT.builder();
764 assertTrue(builder.get().getAllowDuplicateHeaderNames());
765 assertTrue(builder.setDuplicateHeaderMode(DuplicateHeaderMode.ALLOW_ALL).get().getAllowDuplicateHeaderNames());
766 assertFalse(builder.setDuplicateHeaderMode(DuplicateHeaderMode.ALLOW_EMPTY).get().getAllowDuplicateHeaderNames());
767 assertFalse(builder.setDuplicateHeaderMode(DuplicateHeaderMode.DISALLOW).get().getAllowDuplicateHeaderNames());
768 }
769
770 @Test
771 public void testGetDuplicateHeaderMode() {
772 final Builder builder = CSVFormat.DEFAULT.builder();
773
774 assertEquals(DuplicateHeaderMode.ALLOW_ALL, builder.get().getDuplicateHeaderMode());
775 assertEquals(DuplicateHeaderMode.ALLOW_ALL, builder.setDuplicateHeaderMode(DuplicateHeaderMode.ALLOW_ALL).get().getDuplicateHeaderMode());
776 assertEquals(DuplicateHeaderMode.ALLOW_EMPTY, builder.setDuplicateHeaderMode(DuplicateHeaderMode.ALLOW_EMPTY).get().getDuplicateHeaderMode());
777 assertEquals(DuplicateHeaderMode.DISALLOW, builder.setDuplicateHeaderMode(DuplicateHeaderMode.DISALLOW).get().getDuplicateHeaderMode());
778 }
779
780 @Test
781 public void testGetHeader() {
782 final String[] header = { "one", "two", "three" };
783 final CSVFormat formatWithHeader = CSVFormat.DEFAULT.withHeader(header);
784
785 final String[] headerCopy = formatWithHeader.getHeader();
786 headerCopy[0] = "A";
787 headerCopy[1] = "B";
788 headerCopy[2] = "C";
789 assertFalse(Arrays.equals(formatWithHeader.getHeader(), headerCopy));
790 assertNotSame(formatWithHeader.getHeader(), headerCopy);
791 }
792
793 @Test
794 public void testHashCodeAndWithIgnoreHeaderCase() {
795
796 final CSVFormat csvFormat = CSVFormat.INFORMIX_UNLOAD_CSV;
797 final CSVFormat csvFormatTwo = csvFormat.withIgnoreHeaderCase();
798 csvFormatTwo.hashCode();
799
800 assertFalse(csvFormat.getIgnoreHeaderCase());
801 assertTrue(csvFormatTwo.getIgnoreHeaderCase());
802 assertFalse(csvFormatTwo.getTrailingDelimiter());
803
804 Assertions.assertNotEquals(csvFormatTwo, csvFormat);
805 assertFalse(csvFormatTwo.getAllowMissingColumnNames());
806
807 assertFalse(csvFormatTwo.getTrim());
808
809 }
810
811 @Test
812 public void testJiraCsv236() {
813 CSVFormat.DEFAULT.builder().setAllowDuplicateHeaderNames(true).setHeader("CC", "VV", "VV").get();
814 }
815
816 @SuppressWarnings("deprecation")
817 @Test
818 public void testJiraCsv236__Deprecated() {
819 CSVFormat.DEFAULT.withAllowDuplicateHeaderNames().withHeader("CC", "VV", "VV");
820 }
821
822 @Test
823 public void testNewFormat() {
824
825 final CSVFormat csvFormat = CSVFormat.newFormat('X');
826
827 assertFalse(csvFormat.getSkipHeaderRecord());
828 assertFalse(csvFormat.isEscapeCharacterSet());
829
830 assertNull(csvFormat.getRecordSeparator());
831 assertNull(csvFormat.getQuoteMode());
832
833 assertNull(csvFormat.getCommentMarker());
834 assertFalse(csvFormat.getIgnoreHeaderCase());
835
836 assertFalse(csvFormat.getAllowMissingColumnNames());
837 assertFalse(csvFormat.getTrim());
838
839 assertFalse(csvFormat.isNullStringSet());
840 assertNull(csvFormat.getEscapeCharacter());
841
842 assertFalse(csvFormat.getIgnoreSurroundingSpaces());
843 assertFalse(csvFormat.getTrailingDelimiter());
844
845 assertEquals('X', csvFormat.getDelimiter());
846 assertNull(csvFormat.getNullString());
847
848 assertFalse(csvFormat.isQuoteCharacterSet());
849 assertFalse(csvFormat.isCommentMarkerSet());
850
851 assertNull(csvFormat.getQuoteCharacter());
852 assertFalse(csvFormat.getIgnoreEmptyLines());
853
854 assertFalse(csvFormat.getSkipHeaderRecord());
855 assertFalse(csvFormat.isEscapeCharacterSet());
856
857 assertNull(csvFormat.getRecordSeparator());
858 assertNull(csvFormat.getQuoteMode());
859
860 assertNull(csvFormat.getCommentMarker());
861 assertFalse(csvFormat.getIgnoreHeaderCase());
862
863 assertFalse(csvFormat.getAllowMissingColumnNames());
864 assertFalse(csvFormat.getTrim());
865
866 assertFalse(csvFormat.isNullStringSet());
867 assertNull(csvFormat.getEscapeCharacter());
868
869 assertFalse(csvFormat.getIgnoreSurroundingSpaces());
870 assertFalse(csvFormat.getTrailingDelimiter());
871
872 assertEquals('X', csvFormat.getDelimiter());
873 assertNull(csvFormat.getNullString());
874
875 assertFalse(csvFormat.isQuoteCharacterSet());
876 assertFalse(csvFormat.isCommentMarkerSet());
877
878 assertNull(csvFormat.getQuoteCharacter());
879 assertFalse(csvFormat.getIgnoreEmptyLines());
880
881 }
882
883 @Test
884 public void testNullRecordSeparatorCsv106() {
885 final CSVFormat format = CSVFormat.newFormat(';').builder().setSkipHeaderRecord(true).setHeader("H1", "H2").get();
886 final String formatStr = format.format("A", "B");
887 assertNotNull(formatStr);
888 assertFalse(formatStr.endsWith("null"));
889 }
890
891 @SuppressWarnings("deprecation")
892 @Test
893 public void testNullRecordSeparatorCsv106__Deprecated() {
894 final CSVFormat format = CSVFormat.newFormat(';').withSkipHeaderRecord().withHeader("H1", "H2");
895 final String formatStr = format.format("A", "B");
896 assertNotNull(formatStr);
897 assertFalse(formatStr.endsWith("null"));
898 }
899
900 @Test
901 public void testPrintRecord() throws IOException {
902 final Appendable out = new StringBuilder();
903 final CSVFormat format = CSVFormat.RFC4180;
904 format.printRecord(out, "a", "b", "c");
905 assertEquals("a,b,c" + format.getRecordSeparator(), out.toString());
906 }
907
908 @Test
909 public void testPrintRecordEmpty() throws IOException {
910 final Appendable out = new StringBuilder();
911 final CSVFormat format = CSVFormat.RFC4180;
912 format.printRecord(out);
913 assertEquals(format.getRecordSeparator(), out.toString());
914 }
915
916 @Test
917 public void testPrintWithEscapesEndWithCRLF() throws IOException {
918 final Reader in = new StringReader("x,y,x\r\na,?b,c\r\n");
919 final Appendable out = new StringBuilder();
920 final CSVFormat format = CSVFormat.RFC4180.withEscape('?').withDelimiter(',').withQuote(null).withRecordSeparator(CRLF);
921 format.print(in, out, true);
922 assertEquals("x?,y?,x?r?na?,??b?,c?r?n", out.toString());
923 }
924
925 @Test
926 public void testPrintWithEscapesEndWithoutCRLF() throws IOException {
927 final Reader in = new StringReader("x,y,x");
928 final Appendable out = new StringBuilder();
929 final CSVFormat format = CSVFormat.RFC4180.withEscape('?').withDelimiter(',').withQuote(null).withRecordSeparator(CRLF);
930 format.print(in, out, true);
931 assertEquals("x?,y?,x", out.toString());
932 }
933
934 @Test
935 public void testPrintWithoutQuotes() throws IOException {
936 final Reader in = new StringReader("");
937 final Appendable out = new StringBuilder();
938 final CSVFormat format = CSVFormat.RFC4180.withDelimiter(',').withQuote('"').withEscape('?').withQuoteMode(QuoteMode.NON_NUMERIC);
939 format.print(in, out, true);
940 assertEquals("\"\"", out.toString());
941 }
942
943 @Test
944 public void testPrintWithQuoteModeIsNONE() throws IOException {
945 final Reader in = new StringReader("a,b,c");
946 final Appendable out = new StringBuilder();
947 final CSVFormat format = CSVFormat.RFC4180.withDelimiter(',').withQuote('"').withEscape('?').withQuoteMode(QuoteMode.NONE);
948 format.print(in, out, true);
949 assertEquals("a?,b?,c", out.toString());
950 }
951
952 @Test
953 public void testPrintWithQuotes() throws IOException {
954 final Reader in = new StringReader("\"a,b,c\r\nx,y,z");
955 final Appendable out = new StringBuilder();
956 final CSVFormat format = CSVFormat.RFC4180.withDelimiter(',').withQuote('"').withEscape('?').withQuoteMode(QuoteMode.NON_NUMERIC);
957 format.print(in, out, true);
958 assertEquals("\"\"\"a,b,c\r\nx,y,z\"", out.toString());
959 }
960
961 @Test
962 public void testQuoteCharSameAsCommentStartThrowsException() {
963 assertThrows(IllegalArgumentException.class, () -> CSVFormat.DEFAULT.builder().setQuote('!').setCommentMarker('!').get());
964 }
965
966 @SuppressWarnings("deprecation")
967 @Test
968 public void testQuoteCharSameAsCommentStartThrowsException_Deprecated() {
969 assertThrows(IllegalArgumentException.class, () -> CSVFormat.DEFAULT.withQuote('!').withCommentMarker('!'));
970 }
971
972 @Test
973 public void testQuoteCharSameAsCommentStartThrowsExceptionForWrapperType() {
974
975 assertThrows(IllegalArgumentException.class, () -> CSVFormat.DEFAULT.builder().setQuote(Character.valueOf('!')).setCommentMarker('!').get());
976 }
977
978 @SuppressWarnings("deprecation")
979 @Test
980 public void testQuoteCharSameAsCommentStartThrowsExceptionForWrapperType_Deprecated() {
981
982 assertThrows(IllegalArgumentException.class, () -> CSVFormat.DEFAULT.withQuote(Character.valueOf('!')).withCommentMarker('!'));
983 }
984
985 @Test
986 public void testQuoteCharSameAsDelimiterThrowsException() {
987 assertThrows(IllegalArgumentException.class, () -> CSVFormat.DEFAULT.builder().setQuote('!').setDelimiter('!').get());
988 }
989
990 @SuppressWarnings("deprecation")
991 @Test
992 public void testQuoteCharSameAsDelimiterThrowsException_Deprecated() {
993 assertThrows(IllegalArgumentException.class, () -> CSVFormat.DEFAULT.withQuote('!').withDelimiter('!'));
994 }
995
996 @Test
997 public void testQuoteModeNoneShouldReturnMeaningfulExceptionMessage() {
998 final Exception exception = assertThrows(IllegalArgumentException.class, () ->
999
1000 CSVFormat.DEFAULT.builder()
1001 .setHeader("Col1", "Col2", "Col3", "Col4")
1002 .setQuoteMode(QuoteMode.NONE)
1003 .get()
1004
1005 );
1006 final String actualMessage = exception.getMessage();
1007 final String expectedMessage = "Quote mode set to NONE but no escape character is set";
1008 assertEquals(expectedMessage, actualMessage);
1009 }
1010
1011 @Test
1012 public void testQuotePolicyNoneWithoutEscapeThrowsException() {
1013 assertThrows(IllegalArgumentException.class, () -> CSVFormat.newFormat('!').builder().setQuoteMode(QuoteMode.NONE).get());
1014 }
1015
1016 @SuppressWarnings("deprecation")
1017 @Test
1018 public void testQuotePolicyNoneWithoutEscapeThrowsException_Deprecated() {
1019 assertThrows(IllegalArgumentException.class, () -> CSVFormat.newFormat('!').withQuoteMode(QuoteMode.NONE));
1020 }
1021
1022 @Test
1023 public void testRFC4180() {
1024 assertNull(RFC4180.getCommentMarker());
1025 assertEquals(',', RFC4180.getDelimiter());
1026 assertNull(RFC4180.getEscapeCharacter());
1027 assertFalse(RFC4180.getIgnoreEmptyLines());
1028 assertEquals(Character.valueOf('"'), RFC4180.getQuoteCharacter());
1029 assertNull(RFC4180.getQuoteMode());
1030 assertEquals("\r\n", RFC4180.getRecordSeparator());
1031 }
1032
1033 @SuppressWarnings("boxing")
1034 @Test
1035 public void testSerialization() throws Exception {
1036 final ByteArrayOutputStream out = new ByteArrayOutputStream();
1037
1038 try (ObjectOutputStream oos = new ObjectOutputStream(out)) {
1039 oos.writeObject(CSVFormat.DEFAULT);
1040 oos.flush();
1041 }
1042
1043 final ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(out.toByteArray()));
1044 final CSVFormat format = (CSVFormat) in.readObject();
1045
1046 assertNotNull(format);
1047 assertEquals(CSVFormat.DEFAULT.getDelimiter(), format.getDelimiter(), "delimiter");
1048 assertEquals(CSVFormat.DEFAULT.getQuoteCharacter(), format.getQuoteCharacter(), "encapsulator");
1049 assertEquals(CSVFormat.DEFAULT.getCommentMarker(), format.getCommentMarker(), "comment start");
1050 assertEquals(CSVFormat.DEFAULT.getRecordSeparator(), format.getRecordSeparator(), "record separator");
1051 assertEquals(CSVFormat.DEFAULT.getEscapeCharacter(), format.getEscapeCharacter(), "escape");
1052 assertEquals(CSVFormat.DEFAULT.getIgnoreSurroundingSpaces(), format.getIgnoreSurroundingSpaces(), "trim");
1053 assertEquals(CSVFormat.DEFAULT.getIgnoreEmptyLines(), format.getIgnoreEmptyLines(), "empty lines");
1054 }
1055
1056 @Test
1057 public void testToString() {
1058
1059 final String string = CSVFormat.INFORMIX_UNLOAD.toString();
1060
1061 assertEquals("Delimiter=<|> Escape=<\\> QuoteChar=<\"> RecordSeparator=<\n> EmptyLines:ignored SkipHeaderRecord:false", string);
1062
1063 }
1064
1065 @Test
1066 public void testToStringAndWithCommentMarkerTakingCharacter() {
1067
1068 final CSVFormat.Predefined csvFormatPredefined = CSVFormat.Predefined.Default;
1069 final CSVFormat csvFormat = csvFormatPredefined.getFormat();
1070
1071 assertNull(csvFormat.getEscapeCharacter());
1072 assertTrue(csvFormat.isQuoteCharacterSet());
1073
1074 assertFalse(csvFormat.getTrim());
1075 assertFalse(csvFormat.getIgnoreSurroundingSpaces());
1076
1077 assertFalse(csvFormat.getTrailingDelimiter());
1078 assertEquals(',', csvFormat.getDelimiter());
1079
1080 assertFalse(csvFormat.getIgnoreHeaderCase());
1081 assertEquals("\r\n", csvFormat.getRecordSeparator());
1082
1083 assertFalse(csvFormat.isCommentMarkerSet());
1084 assertNull(csvFormat.getCommentMarker());
1085
1086 assertFalse(csvFormat.isNullStringSet());
1087 assertFalse(csvFormat.getAllowMissingColumnNames());
1088
1089 assertFalse(csvFormat.isEscapeCharacterSet());
1090 assertFalse(csvFormat.getSkipHeaderRecord());
1091
1092 assertNull(csvFormat.getNullString());
1093 assertNull(csvFormat.getQuoteMode());
1094
1095 assertTrue(csvFormat.getIgnoreEmptyLines());
1096 assertEquals('\"', (char) csvFormat.getQuoteCharacter());
1097
1098 final Character character = Character.valueOf('n');
1099
1100 final CSVFormat csvFormatTwo = csvFormat.withCommentMarker(character);
1101
1102 assertNull(csvFormat.getEscapeCharacter());
1103 assertTrue(csvFormat.isQuoteCharacterSet());
1104
1105 assertFalse(csvFormat.getTrim());
1106 assertFalse(csvFormat.getIgnoreSurroundingSpaces());
1107
1108 assertFalse(csvFormat.getTrailingDelimiter());
1109 assertEquals(',', csvFormat.getDelimiter());
1110
1111 assertFalse(csvFormat.getIgnoreHeaderCase());
1112 assertEquals("\r\n", csvFormat.getRecordSeparator());
1113
1114 assertFalse(csvFormat.isCommentMarkerSet());
1115 assertNull(csvFormat.getCommentMarker());
1116
1117 assertFalse(csvFormat.isNullStringSet());
1118 assertFalse(csvFormat.getAllowMissingColumnNames());
1119
1120 assertFalse(csvFormat.isEscapeCharacterSet());
1121 assertFalse(csvFormat.getSkipHeaderRecord());
1122
1123 assertNull(csvFormat.getNullString());
1124 assertNull(csvFormat.getQuoteMode());
1125
1126 assertTrue(csvFormat.getIgnoreEmptyLines());
1127 assertEquals('\"', (char) csvFormat.getQuoteCharacter());
1128
1129 assertFalse(csvFormatTwo.isNullStringSet());
1130 assertFalse(csvFormatTwo.getAllowMissingColumnNames());
1131
1132 assertEquals('\"', (char) csvFormatTwo.getQuoteCharacter());
1133 assertNull(csvFormatTwo.getNullString());
1134
1135 assertEquals(',', csvFormatTwo.getDelimiter());
1136 assertFalse(csvFormatTwo.getTrailingDelimiter());
1137
1138 assertTrue(csvFormatTwo.isCommentMarkerSet());
1139 assertFalse(csvFormatTwo.getIgnoreHeaderCase());
1140
1141 assertFalse(csvFormatTwo.getTrim());
1142 assertNull(csvFormatTwo.getEscapeCharacter());
1143
1144 assertTrue(csvFormatTwo.isQuoteCharacterSet());
1145 assertFalse(csvFormatTwo.getIgnoreSurroundingSpaces());
1146
1147 assertEquals("\r\n", csvFormatTwo.getRecordSeparator());
1148 assertNull(csvFormatTwo.getQuoteMode());
1149
1150 assertEquals('n', (char) csvFormatTwo.getCommentMarker());
1151 assertFalse(csvFormatTwo.getSkipHeaderRecord());
1152
1153 assertFalse(csvFormatTwo.isEscapeCharacterSet());
1154 assertTrue(csvFormatTwo.getIgnoreEmptyLines());
1155
1156 assertNotSame(csvFormat, csvFormatTwo);
1157 assertNotSame(csvFormatTwo, csvFormat);
1158
1159 Assertions.assertNotEquals(csvFormatTwo, csvFormat);
1160
1161 assertNull(csvFormat.getEscapeCharacter());
1162 assertTrue(csvFormat.isQuoteCharacterSet());
1163
1164 assertFalse(csvFormat.getTrim());
1165 assertFalse(csvFormat.getIgnoreSurroundingSpaces());
1166
1167 assertFalse(csvFormat.getTrailingDelimiter());
1168 assertEquals(',', csvFormat.getDelimiter());
1169
1170 assertFalse(csvFormat.getIgnoreHeaderCase());
1171 assertEquals("\r\n", csvFormat.getRecordSeparator());
1172
1173 assertFalse(csvFormat.isCommentMarkerSet());
1174 assertNull(csvFormat.getCommentMarker());
1175
1176 assertFalse(csvFormat.isNullStringSet());
1177 assertFalse(csvFormat.getAllowMissingColumnNames());
1178
1179 assertFalse(csvFormat.isEscapeCharacterSet());
1180 assertFalse(csvFormat.getSkipHeaderRecord());
1181
1182 assertNull(csvFormat.getNullString());
1183 assertNull(csvFormat.getQuoteMode());
1184
1185 assertTrue(csvFormat.getIgnoreEmptyLines());
1186 assertEquals('\"', (char) csvFormat.getQuoteCharacter());
1187
1188 assertFalse(csvFormatTwo.isNullStringSet());
1189 assertFalse(csvFormatTwo.getAllowMissingColumnNames());
1190
1191 assertEquals('\"', (char) csvFormatTwo.getQuoteCharacter());
1192 assertNull(csvFormatTwo.getNullString());
1193
1194 assertEquals(',', csvFormatTwo.getDelimiter());
1195 assertFalse(csvFormatTwo.getTrailingDelimiter());
1196
1197 assertTrue(csvFormatTwo.isCommentMarkerSet());
1198 assertFalse(csvFormatTwo.getIgnoreHeaderCase());
1199
1200 assertFalse(csvFormatTwo.getTrim());
1201 assertNull(csvFormatTwo.getEscapeCharacter());
1202
1203 assertTrue(csvFormatTwo.isQuoteCharacterSet());
1204 assertFalse(csvFormatTwo.getIgnoreSurroundingSpaces());
1205
1206 assertEquals("\r\n", csvFormatTwo.getRecordSeparator());
1207 assertNull(csvFormatTwo.getQuoteMode());
1208
1209 assertEquals('n', (char) csvFormatTwo.getCommentMarker());
1210 assertFalse(csvFormatTwo.getSkipHeaderRecord());
1211
1212 assertFalse(csvFormatTwo.isEscapeCharacterSet());
1213 assertTrue(csvFormatTwo.getIgnoreEmptyLines());
1214
1215 assertNotSame(csvFormat, csvFormatTwo);
1216 assertNotSame(csvFormatTwo, csvFormat);
1217
1218 Assertions.assertNotEquals(csvFormat, csvFormatTwo);
1219
1220 Assertions.assertNotEquals(csvFormatTwo, csvFormat);
1221 assertEquals("Delimiter=<,> QuoteChar=<\"> CommentStart=<n> " + "RecordSeparator=<\r\n> EmptyLines:ignored SkipHeaderRecord:false",
1222 csvFormatTwo.toString());
1223
1224 }
1225
1226 @Test
1227 public void testTrim() throws IOException {
1228 final CSVFormat formatWithTrim = CSVFormat.DEFAULT.withDelimiter(',').withTrim().withQuote(null).withRecordSeparator(CRLF);
1229
1230 CharSequence in = "a,b,c";
1231 final StringBuilder out = new StringBuilder();
1232 formatWithTrim.print(in, out, true);
1233 assertEquals("a,b,c", out.toString());
1234
1235 in = new StringBuilder(" x,y,z");
1236 out.setLength(0);
1237 formatWithTrim.print(in, out, true);
1238 assertEquals("x,y,z", out.toString());
1239
1240 in = new StringBuilder("");
1241 out.setLength(0);
1242 formatWithTrim.print(in, out, true);
1243 assertEquals("", out.toString());
1244
1245 in = new StringBuilder("header\r\n");
1246 out.setLength(0);
1247 formatWithTrim.print(in, out, true);
1248 assertEquals("header", out.toString());
1249 }
1250
1251 @Test
1252 public void testWithCommentStart() {
1253 final CSVFormat formatWithCommentStart = CSVFormat.DEFAULT.withCommentMarker('#');
1254 assertEquals(Character.valueOf('#'), formatWithCommentStart.getCommentMarker());
1255 }
1256
1257 @Test
1258 public void testWithCommentStartCRThrowsException() {
1259 assertThrows(IllegalArgumentException.class, () -> CSVFormat.DEFAULT.withCommentMarker(CR));
1260 }
1261
1262 @Test
1263 public void testWithDelimiter() {
1264 final CSVFormat formatWithDelimiter = CSVFormat.DEFAULT.withDelimiter('!');
1265 assertEquals('!', formatWithDelimiter.getDelimiter());
1266 }
1267
1268 @Test
1269 public void testWithDelimiterLFThrowsException() {
1270 assertThrows(IllegalArgumentException.class, () -> CSVFormat.DEFAULT.withDelimiter(LF));
1271 }
1272
1273 @Test
1274 public void testWithEmptyDuplicates() {
1275 final CSVFormat formatWithEmptyDuplicates = CSVFormat.DEFAULT.builder().setDuplicateHeaderMode(DuplicateHeaderMode.ALLOW_EMPTY).get();
1276
1277 assertEquals(DuplicateHeaderMode.ALLOW_EMPTY, formatWithEmptyDuplicates.getDuplicateHeaderMode());
1278 assertFalse(formatWithEmptyDuplicates.getAllowDuplicateHeaderNames());
1279 }
1280
1281 @Test
1282 public void testWithEmptyEnum() {
1283 final CSVFormat formatWithHeader = CSVFormat.DEFAULT.withHeader(EmptyEnum.class);
1284 assertEquals(0, formatWithHeader.getHeader().length);
1285 }
1286
1287 @Test
1288 public void testWithEscape() {
1289 final CSVFormat formatWithEscape = CSVFormat.DEFAULT.withEscape('&');
1290 assertEquals(Character.valueOf('&'), formatWithEscape.getEscapeCharacter());
1291 }
1292
1293 @Test
1294 public void testWithEscapeCRThrowsExceptions() {
1295 assertThrows(IllegalArgumentException.class, () -> CSVFormat.DEFAULT.withEscape(CR));
1296 }
1297
1298 @Test
1299 public void testWithFirstRecordAsHeader() {
1300 final CSVFormat formatWithFirstRecordAsHeader = CSVFormat.DEFAULT.withFirstRecordAsHeader();
1301 assertTrue(formatWithFirstRecordAsHeader.getSkipHeaderRecord());
1302 assertEquals(0, formatWithFirstRecordAsHeader.getHeader().length);
1303 }
1304
1305 @Test
1306 public void testWithHeader() {
1307 final String[] header = { "one", "two", "three" };
1308
1309 final CSVFormat formatWithHeader = CSVFormat.DEFAULT.withHeader(header);
1310 assertArrayEquals(header, formatWithHeader.getHeader());
1311 assertNotSame(header, formatWithHeader.getHeader());
1312 }
1313
1314 @Test
1315 public void testWithHeaderComments() {
1316
1317 final CSVFormat csvFormat = CSVFormat.DEFAULT;
1318
1319 assertEquals('\"', (char) csvFormat.getQuoteCharacter());
1320 assertFalse(csvFormat.isCommentMarkerSet());
1321
1322 assertFalse(csvFormat.isEscapeCharacterSet());
1323 assertTrue(csvFormat.isQuoteCharacterSet());
1324
1325 assertFalse(csvFormat.getSkipHeaderRecord());
1326 assertNull(csvFormat.getQuoteMode());
1327
1328 assertEquals(',', csvFormat.getDelimiter());
1329 assertTrue(csvFormat.getIgnoreEmptyLines());
1330
1331 assertFalse(csvFormat.getIgnoreHeaderCase());
1332 assertNull(csvFormat.getCommentMarker());
1333
1334 assertEquals("\r\n", csvFormat.getRecordSeparator());
1335 assertFalse(csvFormat.getTrailingDelimiter());
1336
1337 assertFalse(csvFormat.getAllowMissingColumnNames());
1338 assertFalse(csvFormat.getTrim());
1339
1340 assertFalse(csvFormat.isNullStringSet());
1341 assertNull(csvFormat.getNullString());
1342
1343 assertFalse(csvFormat.getIgnoreSurroundingSpaces());
1344 assertNull(csvFormat.getEscapeCharacter());
1345
1346 final Object[] objectArray = new Object[8];
1347 final CSVFormat csvFormatTwo = csvFormat.withHeaderComments(objectArray);
1348
1349 assertEquals('\"', (char) csvFormat.getQuoteCharacter());
1350 assertFalse(csvFormat.isCommentMarkerSet());
1351
1352 assertFalse(csvFormat.isEscapeCharacterSet());
1353 assertTrue(csvFormat.isQuoteCharacterSet());
1354
1355 assertFalse(csvFormat.getSkipHeaderRecord());
1356 assertNull(csvFormat.getQuoteMode());
1357
1358 assertEquals(',', csvFormat.getDelimiter());
1359 assertTrue(csvFormat.getIgnoreEmptyLines());
1360
1361 assertFalse(csvFormat.getIgnoreHeaderCase());
1362 assertNull(csvFormat.getCommentMarker());
1363
1364 assertEquals("\r\n", csvFormat.getRecordSeparator());
1365 assertFalse(csvFormat.getTrailingDelimiter());
1366
1367 assertFalse(csvFormat.getAllowMissingColumnNames());
1368 assertFalse(csvFormat.getTrim());
1369
1370 assertFalse(csvFormat.isNullStringSet());
1371 assertNull(csvFormat.getNullString());
1372
1373 assertFalse(csvFormat.getIgnoreSurroundingSpaces());
1374 assertNull(csvFormat.getEscapeCharacter());
1375
1376 assertFalse(csvFormatTwo.getIgnoreHeaderCase());
1377 assertNull(csvFormatTwo.getQuoteMode());
1378
1379 assertTrue(csvFormatTwo.getIgnoreEmptyLines());
1380 assertFalse(csvFormatTwo.getIgnoreSurroundingSpaces());
1381
1382 assertNull(csvFormatTwo.getEscapeCharacter());
1383 assertFalse(csvFormatTwo.getTrim());
1384
1385 assertFalse(csvFormatTwo.isEscapeCharacterSet());
1386 assertTrue(csvFormatTwo.isQuoteCharacterSet());
1387
1388 assertFalse(csvFormatTwo.getSkipHeaderRecord());
1389 assertEquals('\"', (char) csvFormatTwo.getQuoteCharacter());
1390
1391 assertFalse(csvFormatTwo.getAllowMissingColumnNames());
1392 assertNull(csvFormatTwo.getNullString());
1393
1394 assertFalse(csvFormatTwo.isNullStringSet());
1395 assertFalse(csvFormatTwo.getTrailingDelimiter());
1396
1397 assertEquals("\r\n", csvFormatTwo.getRecordSeparator());
1398 assertEquals(',', csvFormatTwo.getDelimiter());
1399
1400 assertNull(csvFormatTwo.getCommentMarker());
1401 assertFalse(csvFormatTwo.isCommentMarkerSet());
1402
1403 assertNotSame(csvFormat, csvFormatTwo);
1404 assertNotSame(csvFormatTwo, csvFormat);
1405
1406 Assertions.assertNotEquals(csvFormatTwo, csvFormat);
1407
1408 final String string = csvFormatTwo.format(objectArray);
1409
1410 assertEquals('\"', (char) csvFormat.getQuoteCharacter());
1411 assertFalse(csvFormat.isCommentMarkerSet());
1412
1413 assertFalse(csvFormat.isEscapeCharacterSet());
1414 assertTrue(csvFormat.isQuoteCharacterSet());
1415
1416 assertFalse(csvFormat.getSkipHeaderRecord());
1417 assertNull(csvFormat.getQuoteMode());
1418
1419 assertEquals(',', csvFormat.getDelimiter());
1420 assertTrue(csvFormat.getIgnoreEmptyLines());
1421
1422 assertFalse(csvFormat.getIgnoreHeaderCase());
1423 assertNull(csvFormat.getCommentMarker());
1424
1425 assertEquals("\r\n", csvFormat.getRecordSeparator());
1426 assertFalse(csvFormat.getTrailingDelimiter());
1427
1428 assertFalse(csvFormat.getAllowMissingColumnNames());
1429 assertFalse(csvFormat.getTrim());
1430
1431 assertFalse(csvFormat.isNullStringSet());
1432 assertNull(csvFormat.getNullString());
1433
1434 assertFalse(csvFormat.getIgnoreSurroundingSpaces());
1435 assertNull(csvFormat.getEscapeCharacter());
1436
1437 assertFalse(csvFormatTwo.getIgnoreHeaderCase());
1438 assertNull(csvFormatTwo.getQuoteMode());
1439
1440 assertTrue(csvFormatTwo.getIgnoreEmptyLines());
1441 assertFalse(csvFormatTwo.getIgnoreSurroundingSpaces());
1442
1443 assertNull(csvFormatTwo.getEscapeCharacter());
1444 assertFalse(csvFormatTwo.getTrim());
1445
1446 assertFalse(csvFormatTwo.isEscapeCharacterSet());
1447 assertTrue(csvFormatTwo.isQuoteCharacterSet());
1448
1449 assertFalse(csvFormatTwo.getSkipHeaderRecord());
1450 assertEquals('\"', (char) csvFormatTwo.getQuoteCharacter());
1451
1452 assertFalse(csvFormatTwo.getAllowMissingColumnNames());
1453 assertNull(csvFormatTwo.getNullString());
1454
1455 assertFalse(csvFormatTwo.isNullStringSet());
1456 assertFalse(csvFormatTwo.getTrailingDelimiter());
1457
1458 assertEquals("\r\n", csvFormatTwo.getRecordSeparator());
1459 assertEquals(',', csvFormatTwo.getDelimiter());
1460
1461 assertNull(csvFormatTwo.getCommentMarker());
1462 assertFalse(csvFormatTwo.isCommentMarkerSet());
1463
1464 assertNotSame(csvFormat, csvFormatTwo);
1465 assertNotSame(csvFormatTwo, csvFormat);
1466
1467 assertNotNull(string);
1468 Assertions.assertNotEquals(csvFormat, csvFormatTwo);
1469
1470 Assertions.assertNotEquals(csvFormatTwo, csvFormat);
1471 assertEquals(",,,,,,,", string);
1472
1473 }
1474
1475 @Test
1476 public void testWithHeaderEnum() {
1477 final CSVFormat formatWithHeader = CSVFormat.DEFAULT.withHeader(Header.class);
1478 assertArrayEquals(new String[] { "Name", "Email", "Phone" }, formatWithHeader.getHeader());
1479 }
1480
1481 @Test
1482 public void testWithHeaderEnumNull() {
1483 final CSVFormat format = CSVFormat.DEFAULT;
1484 final Class<Enum<?>> simpleName = null;
1485 format.withHeader(simpleName);
1486 }
1487
1488 @Test
1489 public void testWithHeaderResultSetNull() throws SQLException {
1490 final CSVFormat format = CSVFormat.DEFAULT;
1491 final ResultSet resultSet = null;
1492 format.withHeader(resultSet);
1493 }
1494
1495 @Test
1496 public void testWithIgnoreEmptyLines() {
1497 assertFalse(CSVFormat.DEFAULT.withIgnoreEmptyLines(false).getIgnoreEmptyLines());
1498 assertTrue(CSVFormat.DEFAULT.withIgnoreEmptyLines().getIgnoreEmptyLines());
1499 }
1500
1501 @Test
1502 public void testWithIgnoreSurround() {
1503 assertFalse(CSVFormat.DEFAULT.withIgnoreSurroundingSpaces(false).getIgnoreSurroundingSpaces());
1504 assertTrue(CSVFormat.DEFAULT.withIgnoreSurroundingSpaces().getIgnoreSurroundingSpaces());
1505 }
1506
1507 @Test
1508 public void testWithNullString() {
1509 final CSVFormat formatWithNullString = CSVFormat.DEFAULT.withNullString("null");
1510 assertEquals("null", formatWithNullString.getNullString());
1511 }
1512
1513 @Test
1514 public void testWithQuoteChar() {
1515 final CSVFormat formatWithQuoteChar = CSVFormat.DEFAULT.withQuote('"');
1516 assertEquals(Character.valueOf('"'), formatWithQuoteChar.getQuoteCharacter());
1517 }
1518
1519 @Test
1520 public void testWithQuoteLFThrowsException() {
1521 assertThrows(IllegalArgumentException.class, () -> CSVFormat.DEFAULT.withQuote(LF));
1522 }
1523
1524 @Test
1525 public void testWithQuotePolicy() {
1526 final CSVFormat formatWithQuotePolicy = CSVFormat.DEFAULT.withQuoteMode(QuoteMode.ALL);
1527 assertEquals(QuoteMode.ALL, formatWithQuotePolicy.getQuoteMode());
1528 }
1529
1530 @Test
1531 public void testWithRecordSeparatorCR() {
1532 final CSVFormat formatWithRecordSeparator = CSVFormat.DEFAULT.withRecordSeparator(CR);
1533 assertEquals(String.valueOf(CR), formatWithRecordSeparator.getRecordSeparator());
1534 }
1535
1536 @Test
1537 public void testWithRecordSeparatorCRLF() {
1538 final CSVFormat formatWithRecordSeparator = CSVFormat.DEFAULT.withRecordSeparator(CRLF);
1539 assertEquals(CRLF, formatWithRecordSeparator.getRecordSeparator());
1540 }
1541
1542 @Test
1543 public void testWithRecordSeparatorLF() {
1544 final CSVFormat formatWithRecordSeparator = CSVFormat.DEFAULT.withRecordSeparator(LF);
1545 assertEquals(String.valueOf(LF), formatWithRecordSeparator.getRecordSeparator());
1546 }
1547
1548 @Test
1549 public void testWithSystemRecordSeparator() {
1550 final CSVFormat formatWithRecordSeparator = CSVFormat.DEFAULT.withSystemRecordSeparator();
1551 assertEquals(System.lineSeparator(), formatWithRecordSeparator.getRecordSeparator());
1552 }
1553 }