@@ -1523,6 +1523,23 @@ public void testStream() throws Exception {
15231523 assertArrayEquals (new String [] { "x" , "y" , "z" }, list .get (2 ).values ());
15241524 }}
15251525
1526+ @ Test
1527+ public void testThrowExceptionWithLineAndPosition () throws IOException {
1528+ final String csvContent = "col1,col2,col3,col4,col5,col6,col7,col8,col9,col10\n rec1,rec2,rec3,rec4,rec5,rec6,rec7,rec8,\" \" rec9\" \" ,rec10" ;
1529+ final StringReader stringReader = new StringReader (csvContent );
1530+ // @formatter:off
1531+ final CSVFormat csvFormat = CSVFormat .DEFAULT .builder ()
1532+ .setHeader ()
1533+ .setSkipHeaderRecord (true )
1534+ .build ();
1535+ // @formatter:on
1536+
1537+ try (CSVParser csvParser = csvFormat .parse (stringReader )) {
1538+ final Exception exception = assertThrows (UncheckedIOException .class , csvParser ::getRecords );
1539+ assertTrue (exception .getMessage ().contains ("Invalid char between encapsulated token and delimiter at line: 2, position: 94" ));
1540+ }
1541+ }
1542+
15261543 @ Test
15271544 public void testTrailingDelimiter () throws Exception {
15281545 final Reader in = new StringReader ("a,a,a,\n \" 1\" ,\" 2\" ,\" 3\" ,\n x,y,z," );
@@ -1641,27 +1658,4 @@ private void validateRecordPosition(final String lineSeparator) throws IOExcepti
16411658
16421659 parser .close ();
16431660 }
1644-
1645- @ Test
1646- public void testFaultyCSVShouldThrowExceptionWithLineAndPosition () throws IOException {
1647- String csvContent = "col1,col2,col3,col4,col5,col6,col7,col8,col9,col10\n " +
1648- "rec1,rec2,rec3,rec4,rec5,rec6,rec7,rec8,\" \" rec9\" \" ,rec10" ;
1649-
1650- StringReader stringReader = new StringReader (csvContent );
1651- CSVFormat csvFormat = CSVFormat .DEFAULT .builder ()
1652- .setHeader ()
1653- .setSkipHeaderRecord (true )
1654- .build ();
1655-
1656- CSVParser csvParser = csvFormat .parse (stringReader );
1657- Exception exception = assertThrows (UncheckedIOException .class , () -> {
1658- for (CSVRecord record : csvParser ) {
1659- // this will result in exception due to parsing issue
1660- }
1661- });
1662- String expectedErrorMessage = "Invalid char between encapsulated token and delimiter at " +
1663- "line: 2, position: 94" ;
1664- String actualMessage = exception .getMessage ();
1665- assertTrue (actualMessage .contains (expectedErrorMessage ));
1666- }
16671661}
0 commit comments