@@ -1375,124 +1375,143 @@ private void validateRecordPosition(final String lineSeparator) throws IOExcepti
13751375
13761376 parser .close ();
13771377 }
1378- @ Test
1379- public void getHeaderComment () throws IOException {
1380- // File with no header comments
1381- String text_1 = "A,B" +CRLF +"1,2" +CRLF ;
1382- // File with a single line header comment
1383- String text_2 = "# comment" +CRLF +"A,B" +CRLF +"1,2" +CRLF ;
1384- // File with a multi-line header comment
1385- String text_3 = "# multi-line" + CRLF + "# comment" +CRLF +"A,B" +CRLF +"1,2" +CRLF ;
1386- // Format with auto-detected header
1387- CSVFormat format_a = CSVFormat .Builder .create (CSVFormat .DEFAULT ).setCommentMarker ('#' ).setHeader ().build ();
1388- // Format with explicit header
1389- CSVFormat format_b = CSVFormat .Builder .create (CSVFormat .DEFAULT )
1390- .setSkipHeaderRecord (true )
1391- .setCommentMarker ('#' )
1392- .setHeader ("A" ,"B" )
1393- .build ();
1394- // Format with explicit header that does not skip the header line
1395- CSVFormat format_c = CSVFormat .Builder .create (CSVFormat .DEFAULT )
1396- .setCommentMarker ('#' )
1397- .setHeader ("A" ,"B" )
1398- .build ();
1399-
1400- try (CSVParser parser = CSVParser .parse (text_1 , format_a )) {
1378+ // CSV with no header comments
1379+ static private final String CSV_INPUT_NO_COMMENT = "A,B" +CRLF +"1,2" +CRLF ;
1380+ // CSV with a header comment
1381+ static private final String CSV_INPUT_HEADER_COMMENT = "# header comment" + CRLF + "A,B" + CRLF + "1,2" + CRLF ;
1382+ // CSV with a single line header and trailer comment
1383+ static private final String CSV_INPUT_HEADER_TRAILER_COMMENT = "# header comment" + CRLF + "A,B" + CRLF + "1,2" + CRLF + "# comment" ;
1384+ // CSV with a multi-line header and trailer comment
1385+ static private final String CSV_INPUT_MULTILINE_HEADER_TRAILER_COMMENT = "# multi-line" + CRLF + "# header comment" + CRLF + "A,B" + CRLF + "1,2" + CRLF + "# multi-line" + CRLF + "# comment" ;
1386+ // Format with auto-detected header
1387+ static private final CSVFormat FORMAT_AUTO_HEADER = CSVFormat .Builder .create (CSVFormat .DEFAULT ).setCommentMarker ('#' ).setHeader ().build ();
1388+ // Format with explicit header
1389+ static private final CSVFormat FORMAT_EXPLICIT_HEADER = CSVFormat .Builder .create (CSVFormat .DEFAULT )
1390+ .setSkipHeaderRecord (true )
1391+ .setCommentMarker ('#' )
1392+ .setHeader ("A" , "B" )
1393+ .build ();
1394+ // Format with explicit header that does not skip the header line
1395+ CSVFormat FORMAT_EXPLICIT_HEADER_NOSKIP = CSVFormat .Builder .create (CSVFormat .DEFAULT )
1396+ .setCommentMarker ('#' )
1397+ .setHeader ("A" , "B" )
1398+ .build ();
1399+ @ Test
1400+ public void testGetHeaderComment_NoComment1 () throws IOException {
1401+
1402+ try (CSVParser parser = CSVParser .parse (CSV_INPUT_NO_COMMENT , FORMAT_AUTO_HEADER )) {
14011403 parser .getRecords ();
14021404 // Expect no header comment
14031405 assertFalse (parser .hasHeaderComment ());
14041406 assertNull (parser .getHeaderComment ());
14051407 }
1406- try (CSVParser parser = CSVParser .parse (text_2 , format_a )) {
1408+ }
1409+ @ Test
1410+ public void testGetHeaderComment_HeaderComment1 () throws IOException {
1411+ try (CSVParser parser = CSVParser .parse (CSV_INPUT_HEADER_COMMENT , FORMAT_AUTO_HEADER )) {
14071412 parser .getRecords ();
14081413 // Expect a header comment
14091414 assertTrue (parser .hasHeaderComment ());
1410- assertEquals ("comment" , parser .getHeaderComment ());
1415+ assertEquals ("header comment" , parser .getHeaderComment ());
14111416 }
1412- try (CSVParser parser = CSVParser .parse (text_3 , format_a )) {
1417+ }
1418+ @ Test
1419+ public void testGetHeaderComment_HeaderTrailerComment () throws IOException {
1420+ try (CSVParser parser = CSVParser .parse (CSV_INPUT_MULTILINE_HEADER_TRAILER_COMMENT , FORMAT_AUTO_HEADER )) {
14131421 parser .getRecords ();
14141422 // Expect a header comment
14151423 assertTrue (parser .hasHeaderComment ());
1416- assertEquals ("multi-line" +LF +"comment" , parser .getHeaderComment ());
1424+ assertEquals ("multi-line" +LF +"header comment" , parser .getHeaderComment ());
14171425 }
1418- try (CSVParser parser = CSVParser .parse (text_1 , format_b )) {
1426+ }
1427+ @ Test
1428+ public void testGetHeaderComment_NoComment2 () throws IOException {
1429+ try (CSVParser parser = CSVParser .parse (CSV_INPUT_NO_COMMENT , FORMAT_EXPLICIT_HEADER )) {
14191430 parser .getRecords ();
14201431 // Expect no header comment
14211432 assertFalse (parser .hasHeaderComment ());
14221433 assertNull (parser .getHeaderComment ());
14231434 }
1424- try (CSVParser parser = CSVParser .parse (text_2 , format_b )) {
1435+ }
1436+ @ Test
1437+ public void testGetHeaderComment_HeaderComment2 () throws IOException {
1438+ try (CSVParser parser = CSVParser .parse (CSV_INPUT_HEADER_COMMENT , FORMAT_EXPLICIT_HEADER )) {
14251439 parser .getRecords ();
14261440 // Expect a header comment
14271441 assertTrue (parser .hasHeaderComment ());
1428- assertEquals ("comment" , parser .getHeaderComment ());
1442+ assertEquals ("header comment" , parser .getHeaderComment ());
14291443 }
1430- try (CSVParser parser = CSVParser .parse (text_1 , format_c )) {
1444+ }
1445+ @ Test
1446+ public void testGetHeaderComment_NoComment3 () throws IOException {
1447+ try (CSVParser parser = CSVParser .parse (CSV_INPUT_NO_COMMENT , FORMAT_EXPLICIT_HEADER_NOSKIP )) {
14311448 parser .getRecords ();
14321449 // Expect no header comment
14331450 assertFalse (parser .hasHeaderComment ());
14341451 assertNull (parser .getHeaderComment ());
14351452 }
1436- try (CSVParser parser = CSVParser .parse (text_2 , format_c )) {
1453+ }
1454+ @ Test
1455+ public void testGetHeaderComment_HeaderComment3 () throws IOException {
1456+ try (CSVParser parser = CSVParser .parse (CSV_INPUT_HEADER_COMMENT , FORMAT_EXPLICIT_HEADER_NOSKIP )) {
14371457 parser .getRecords ();
14381458 // Expect no header comment - the text "comment" is attached to the first record
14391459 assertFalse (parser .hasHeaderComment ());
14401460 assertNull (parser .getHeaderComment ());
14411461 }
14421462 }
1463+
14431464 @ Test
1444- public void getTrailerComment () throws IOException {
1445- // File with a header comment
1446- String text_1 = "# header comment" +CRLF +"A,B" +CRLF +"1,2" +CRLF ;
1447- // File with a single line header and trailer comment
1448- String text_2 = "# header comment" +CRLF +"A,B" +CRLF +"1,2" +CRLF + "# comment" ;
1449- // File with a multi-line header and trailer comment
1450- String text_3 = "# multi-line" + CRLF + "# header comment" +CRLF +"A,B" +CRLF +"1,2" +CRLF +"# multi-line" +CRLF +"# comment" ;
1451- // Format with auto-detected header
1452- CSVFormat format_a = CSVFormat .Builder .create (CSVFormat .DEFAULT ).setCommentMarker ('#' ).setHeader ().build ();
1453- // Format with explicit header
1454- CSVFormat format_b = CSVFormat .Builder .create (CSVFormat .DEFAULT )
1455- .setSkipHeaderRecord (true )
1456- .setCommentMarker ('#' )
1457- .setHeader ("A" ,"B" )
1458- .build ();
1459- // Format with explicit header that does not skip the header line
1460- CSVFormat format_c = CSVFormat .Builder .create (CSVFormat .DEFAULT )
1461- .setCommentMarker ('#' )
1462- .setHeader ("A" ,"B" )
1463- .build ();
1464-
1465- try (CSVParser parser = CSVParser .parse (text_1 , format_a )) {
1465+ public void testGetTrailerComment_HeaderComment1 () throws IOException {
1466+ try (CSVParser parser = CSVParser .parse (CSV_INPUT_HEADER_COMMENT , FORMAT_AUTO_HEADER )) {
14661467 parser .getRecords ();
14671468 assertFalse (parser .hasTrailerComment ());
14681469 assertNull (parser .getTrailerComment ());
14691470 }
1470- try (CSVParser parser = CSVParser .parse (text_2 , format_a )) {
1471+ }
1472+ @ Test
1473+ public void testGetTrailerComment_HeaderTrailerComment1 () throws IOException {
1474+ try (CSVParser parser = CSVParser .parse (CSV_INPUT_HEADER_TRAILER_COMMENT , FORMAT_AUTO_HEADER )) {
14711475 parser .getRecords ();
14721476 assertTrue (parser .hasTrailerComment ());
14731477 assertEquals ("comment" , parser .getTrailerComment ());
14741478 }
1475- try (CSVParser parser = CSVParser .parse (text_3 , format_a )) {
1479+ }
1480+ @ Test
1481+ public void testGetTrailerComment_MultilineComment () throws IOException {
1482+ try (CSVParser parser = CSVParser .parse (CSV_INPUT_MULTILINE_HEADER_TRAILER_COMMENT , FORMAT_AUTO_HEADER )) {
14761483 parser .getRecords ();
14771484 assertTrue (parser .hasTrailerComment ());
14781485 assertEquals ("multi-line" +LF +"comment" , parser .getTrailerComment ());
14791486 }
1480- try (CSVParser parser = CSVParser .parse (text_1 , format_b )) {
1487+ }
1488+ @ Test
1489+ public void testGetTrailerComment_HeaderComment2 () throws IOException {
1490+ try (CSVParser parser = CSVParser .parse (CSV_INPUT_HEADER_COMMENT , FORMAT_EXPLICIT_HEADER )) {
14811491 parser .getRecords ();
14821492 assertFalse (parser .hasTrailerComment ());
14831493 assertNull (parser .getTrailerComment ());
14841494 }
1485- try (CSVParser parser = CSVParser .parse (text_2 , format_b )) {
1495+ }
1496+ @ Test
1497+ public void testGetTrailerComment_HeaderTrailerComment2 () throws IOException {
1498+ try (CSVParser parser = CSVParser .parse (CSV_INPUT_HEADER_TRAILER_COMMENT , FORMAT_EXPLICIT_HEADER )) {
14861499 parser .getRecords ();
14871500 assertTrue (parser .hasTrailerComment ());
14881501 assertEquals ("comment" , parser .getTrailerComment ());
14891502 }
1490- try (CSVParser parser = CSVParser .parse (text_1 , format_c )) {
1503+ }
1504+ @ Test
1505+ public void testGetTrailerComment_HeaderComment3 () throws IOException {
1506+ try (CSVParser parser = CSVParser .parse (CSV_INPUT_HEADER_COMMENT , FORMAT_EXPLICIT_HEADER_NOSKIP )) {
14911507 parser .getRecords ();
14921508 assertFalse (parser .hasTrailerComment ());
14931509 assertNull (parser .getTrailerComment ());
14941510 }
1495- try (CSVParser parser = CSVParser .parse (text_2 , format_c )) {
1511+ }
1512+ @ Test
1513+ public void testGetTrailerComment_HeaderTrailerComment3 () throws IOException {
1514+ try (CSVParser parser = CSVParser .parse (CSV_INPUT_HEADER_TRAILER_COMMENT , FORMAT_EXPLICIT_HEADER_NOSKIP )) {
14961515 parser .getRecords ();
14971516 assertTrue (parser .hasTrailerComment ());
14981517 assertEquals ("comment" , parser .getTrailerComment ());
0 commit comments