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