@@ -258,6 +258,11 @@ public void testDefaultFormat() throws IOException {
258258 parser .close ();
259259 }
260260
261+ @ Test (expected = IllegalArgumentException .class )
262+ public void testDuplicateHeaders () throws Exception {
263+ CSVParser .parse ("a,b,a\n 1,2,3\n x,y,z" , CSVFormat .DEFAULT .withHeader (new String [] {}));
264+ }
265+
261266 @ Test
262267 public void testEmptyFile () throws Exception {
263268 final CSVParser parser = CSVParser .parse ("" , CSVFormat .DEFAULT );
@@ -299,25 +304,6 @@ public void testEmptyLineBehaviourExcel() throws Exception {
299304 }
300305 }
301306
302- @ Test
303- @ Ignore
304- public void testStartWithEmptyLinesThenHeaders () throws Exception {
305- final String [] codes = {"\r \n \r \n \r \n hello,\r \n \r \n \r \n " , "hello,\n \n \n " , "hello,\" \" \r \n \r \n \r \n " ,
306- "hello,\" \" \n \n \n " };
307- final String [][] res = {{"hello" , "" }, {"" }, // Excel format does not ignore empty lines
308- {"" }};
309- for (final String code : codes ) {
310- final CSVParser parser = CSVParser .parse (code , CSVFormat .EXCEL );
311- final List <CSVRecord > records = parser .getRecords ();
312- assertEquals (res .length , records .size ());
313- assertTrue (records .size () > 0 );
314- for (int i = 0 ; i < res .length ; i ++) {
315- assertArrayEquals (res [i ], records .get (i ).values ());
316- }
317- parser .close ();
318- }
319- }
320-
321307 @ Test
322308 public void testEndOfFileBehaviorCSV () throws Exception {
323309 final String [] codes = { "hello,\r \n \r \n world,\r \n " , "hello,\r \n \r \n world," , "hello,\r \n \r \n world,\" \" \r \n " ,
@@ -445,11 +431,6 @@ public void testGetHeaderMap() throws Exception {
445431 parser .close ();
446432 }
447433
448- @ Test (expected = IllegalArgumentException .class )
449- public void testDuplicateHeaders () throws Exception {
450- CSVParser .parse ("a,b,a\n 1,2,3\n x,y,z" , CSVFormat .DEFAULT .withHeader (new String [] {}));
451- }
452-
453434 @ Test
454435 public void testGetLine () throws IOException {
455436 final CSVParser parser = CSVParser .parse (CSV_INPUT , CSVFormat .DEFAULT .withIgnoreSurroundingSpaces ());
@@ -476,16 +457,6 @@ public void testGetLineNumberWithLF() throws Exception {
476457 this .validateLineNumbers (String .valueOf (LF ));
477458 }
478459
479- @ Test
480- public void testGetRecordPositionWithCRLF () throws Exception {
481- this .validateRecordPosition (CRLF );
482- }
483-
484- @ Test
485- public void testGetRecordPositionWithLF () throws Exception {
486- this .validateRecordPosition (String .valueOf (LF ));
487- }
488-
489460 @ Test
490461 public void testGetOneLine () throws IOException {
491462 final CSVParser parser = CSVParser .parse (CSV_INPUT_1 , CSVFormat .DEFAULT );
@@ -534,6 +505,16 @@ public void testGetRecordNumberWithLF() throws Exception {
534505 this .validateRecordNumbers (String .valueOf (LF ));
535506 }
536507
508+ @ Test
509+ public void testGetRecordPositionWithCRLF () throws Exception {
510+ this .validateRecordPosition (CRLF );
511+ }
512+
513+ @ Test
514+ public void testGetRecordPositionWithLF () throws Exception {
515+ this .validateRecordPosition (String .valueOf (LF ));
516+ }
517+
537518 @ Test
538519 public void testGetRecords () throws IOException {
539520 final CSVParser parser = CSVParser .parse (CSV_INPUT , CSVFormat .DEFAULT .withIgnoreSurroundingSpaces ());
@@ -588,6 +569,23 @@ public void testHeader() throws Exception {
588569 assertFalse (records .hasNext ());
589570 }
590571
572+ @ Test
573+ public void testHeaderComment () throws Exception {
574+ final Reader in = new StringReader ("# comment\n a,b,c\n 1,2,3\n x,y,z" );
575+
576+ final Iterator <CSVRecord > records = CSVFormat .DEFAULT .withCommentMarker ('#' ).withHeader ().parse (in ).iterator ();
577+
578+ for (int i = 0 ; i < 2 ; i ++) {
579+ assertTrue (records .hasNext ());
580+ final CSVRecord record = records .next ();
581+ assertEquals (record .get (0 ), record .get ("a" ));
582+ assertEquals (record .get (1 ), record .get ("b" ));
583+ assertEquals (record .get (2 ), record .get ("c" ));
584+ }
585+
586+ assertFalse (records .hasNext ());
587+ }
588+
591589 @ Test
592590 public void testHeaderMissing () throws Exception {
593591 final Reader in = new StringReader ("a,,c\n 1,2,3\n x,y,z" );
@@ -604,10 +602,10 @@ public void testHeaderMissing() throws Exception {
604602 assertFalse (records .hasNext ());
605603 }
606604
607- @ Test ( expected = IllegalArgumentException . class )
608- public void testHeadersMissingException () throws Exception {
605+ @ Test
606+ public void testHeaderMissingWithNull () throws Exception {
609607 final Reader in = new StringReader ("a,,c,,d\n 1,2,3,4\n x,y,z,zz" );
610- CSVFormat .DEFAULT .withHeader ().parse (in ).iterator ();
608+ CSVFormat .DEFAULT .withHeader ().withNullString ( "" ). withAllowMissingColumnNames (). parse (in ).iterator ();
611609 }
612610
613611 @ Test
@@ -616,27 +614,21 @@ public void testHeadersMissing() throws Exception {
616614 CSVFormat .DEFAULT .withHeader ().withAllowMissingColumnNames ().parse (in ).iterator ();
617615 }
618616
619- @ Test
620- public void testHeaderMissingWithNull () throws Exception {
617+ @ Test ( expected = IllegalArgumentException . class )
618+ public void testHeadersMissingException () throws Exception {
621619 final Reader in = new StringReader ("a,,c,,d\n 1,2,3,4\n x,y,z,zz" );
622- CSVFormat .DEFAULT .withHeader ().withNullString ( "" ). withAllowMissingColumnNames (). parse (in ).iterator ();
620+ CSVFormat .DEFAULT .withHeader ().parse (in ).iterator ();
623621 }
624622
625623 @ Test
626- public void testHeaderComment () throws Exception {
627- final Reader in = new StringReader ("# comment\n a,b,c\n 1,2,3\n x,y,z" );
628-
629- final Iterator <CSVRecord > records = CSVFormat .DEFAULT .withCommentMarker ('#' ).withHeader ().parse (in ).iterator ();
630-
631- for (int i = 0 ; i < 2 ; i ++) {
632- assertTrue (records .hasNext ());
633- final CSVRecord record = records .next ();
634- assertEquals (record .get (0 ), record .get ("a" ));
635- assertEquals (record .get (1 ), record .get ("b" ));
636- assertEquals (record .get (2 ), record .get ("c" ));
637- }
638-
639- assertFalse (records .hasNext ());
624+ public void testIgnoreCaseHeaderMapping () throws Exception {
625+ final Reader in = new StringReader ("1,2,3" );
626+ final Iterator <CSVRecord > records = CSVFormat .DEFAULT .withHeader ("One" , "TWO" , "three" ).withIgnoreHeaderCase ()
627+ .parse (in ).iterator ();
628+ final CSVRecord record = records .next ();
629+ assertEquals ("1" , record .get ("one" ));
630+ assertEquals ("2" , record .get ("two" ));
631+ assertEquals ("3" , record .get ("THREE" ));
640632 }
641633
642634 @ Test
@@ -868,14 +860,14 @@ public void testSkipAutoHeader() throws Exception {
868860 }
869861
870862 @ Test
871- public void testSkipSetHeader () throws Exception {
872- final Reader in = new StringReader ("a,b,c \n 1,2,3\n x,y,z" );
873- final Iterator <CSVRecord > records = CSVFormat .DEFAULT .withHeader ("a " , "b " , "c " ).withSkipHeaderRecord ()
863+ public void testSkipHeaderOverrideDuplicateHeaders () throws Exception {
864+ final Reader in = new StringReader ("a,a,a \n 1,2,3\n x,y,z" );
865+ final Iterator <CSVRecord > records = CSVFormat .DEFAULT .withHeader ("X " , "Y " , "Z " ).withSkipHeaderRecord ()
874866 .parse (in ).iterator ();
875867 final CSVRecord record = records .next ();
876- assertEquals ("1" , record .get ("a " ));
877- assertEquals ("2" , record .get ("b " ));
878- assertEquals ("3" , record .get ("c " ));
868+ assertEquals ("1" , record .get ("X " ));
869+ assertEquals ("2" , record .get ("Y " ));
870+ assertEquals ("3" , record .get ("Z " ));
879871 }
880872
881873 @ Test
@@ -890,14 +882,33 @@ public void testSkipSetAltHeaders() throws Exception {
890882 }
891883
892884 @ Test
893- public void testSkipHeaderOverrideDuplicateHeaders () throws Exception {
894- final Reader in = new StringReader ("a,a,a \n 1,2,3\n x,y,z" );
895- final Iterator <CSVRecord > records = CSVFormat .DEFAULT .withHeader ("X " , "Y " , "Z " ).withSkipHeaderRecord ()
885+ public void testSkipSetHeader () throws Exception {
886+ final Reader in = new StringReader ("a,b,c \n 1,2,3\n x,y,z" );
887+ final Iterator <CSVRecord > records = CSVFormat .DEFAULT .withHeader ("a " , "b " , "c " ).withSkipHeaderRecord ()
896888 .parse (in ).iterator ();
897889 final CSVRecord record = records .next ();
898- assertEquals ("1" , record .get ("X" ));
899- assertEquals ("2" , record .get ("Y" ));
900- assertEquals ("3" , record .get ("Z" ));
890+ assertEquals ("1" , record .get ("a" ));
891+ assertEquals ("2" , record .get ("b" ));
892+ assertEquals ("3" , record .get ("c" ));
893+ }
894+
895+ @ Test
896+ @ Ignore
897+ public void testStartWithEmptyLinesThenHeaders () throws Exception {
898+ final String [] codes = {"\r \n \r \n \r \n hello,\r \n \r \n \r \n " , "hello,\n \n \n " , "hello,\" \" \r \n \r \n \r \n " ,
899+ "hello,\" \" \n \n \n " };
900+ final String [][] res = {{"hello" , "" }, {"" }, // Excel format does not ignore empty lines
901+ {"" }};
902+ for (final String code : codes ) {
903+ final CSVParser parser = CSVParser .parse (code , CSVFormat .EXCEL );
904+ final List <CSVRecord > records = parser .getRecords ();
905+ assertEquals (res .length , records .size ());
906+ assertTrue (records .size () > 0 );
907+ for (int i = 0 ; i < res .length ; i ++) {
908+ assertArrayEquals (res [i ], records .get (i ).values ());
909+ }
910+ parser .close ();
911+ }
901912 }
902913
903914 private void validateLineNumbers (final String lineSeparator ) throws IOException {
@@ -995,15 +1006,4 @@ private void validateRecordPosition(final String lineSeparator) throws IOExcepti
9951006
9961007 parser .close ();
9971008 }
998-
999- @ Test
1000- public void testIgnoreCaseHeaderMapping () throws Exception {
1001- final Reader in = new StringReader ("1,2,3" );
1002- final Iterator <CSVRecord > records = CSVFormat .DEFAULT .withHeader ("One" , "TWO" , "three" ).withIgnoreHeaderCase ()
1003- .parse (in ).iterator ();
1004- final CSVRecord record = records .next ();
1005- assertEquals ("1" , record .get ("one" ));
1006- assertEquals ("2" , record .get ("two" ));
1007- assertEquals ("3" , record .get ("THREE" ));
1008- }
10091009}
0 commit comments