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