2727import static org .junit .jupiter .api .Assertions .assertNull ;
2828import static org .junit .jupiter .api .Assertions .assertThrows ;
2929import static org .junit .jupiter .api .Assertions .assertTrue ;
30+ import static org .junit .jupiter .api .Assertions .fail ;
3031
3132import java .io .File ;
3233import java .io .IOException ;
@@ -1643,7 +1644,7 @@ private void validateRecordPosition(final String lineSeparator) throws IOExcepti
16431644 }
16441645
16451646 @ Test
1646- public void testFaultyCSVShouldThrowErrorWithDetailedMessage () {
1647+ public void testFaultyCSVShouldThrowErrorAndDetailedMessageShouldBeAvailable_1 () {
16471648 String csvContent = "col1,col2,col3,col4,col5,col6,col7,col8,col9,col10\n " +
16481649 "rec1,rec2,rec3,rec4,rec5,rec6,rec7,rec8,\" \" rec9\" \" ,rec10" ;
16491650
@@ -1653,20 +1654,59 @@ public void testFaultyCSVShouldThrowErrorWithDetailedMessage() {
16531654 .setSkipHeaderRecord (true )
16541655 .build ();
16551656
1657+ CSVParser csvParser = null ;
1658+ try {
1659+ csvParser = csvFormat .parse (stringReader );
1660+ } catch (IOException e ) {
1661+ fail ("Failed to parse the CSV content" );
1662+ }
1663+ final Iterable <CSVRecord > finalRecords = csvParser ;
16561664 Exception exception = assertThrows (UncheckedIOException .class , () -> {
1657- Iterable <CSVRecord > records = csvFormat .parse (stringReader );
1658- for (CSVRecord record : records ) {
1665+ for (CSVRecord record : finalRecords ) {
16591666 System .out .println (record .get (0 ) + " " + record .get (1 ) + " " + record .get (2 ) + " " + record .get (3 )
16601667 + " " + record .get (4 ) + " " + record .get (5 ) + " " + record .get (6 ) + " " + record .get (7 )
16611668 + " " + record .get (8 ) + " " + record .get (9 ));
16621669 }
16631670 });
16641671 String expectedErrorMessage = "Exception reading next record: java.io.IOException: An exception occurred " +
1665- "while tying to parse the CSV content. Issue in line: 2, position: 94, last parsed content: " +
1666- "...rec4,rec5,rec6,rec7,rec8" ;
1672+ "while tying to parse the CSV content. Issue in line: 2, position: 94" ;
16671673 String actualMessage = exception .getMessage ();
1668-
16691674 assertTrue (actualMessage .contains (expectedErrorMessage ));
1675+ assertNotNull (csvParser );
1676+ String expectedLastParsedContent = "...rec4,rec5,rec6,rec7,rec8" ;
1677+ assertEquals (expectedLastParsedContent , csvParser .getLastParsedContent ());
16701678 }
16711679
1680+ @ Test
1681+ public void testFaultyCSVShouldThrowErrorAndDetailedMessageShouldBeAvailable_2 () {
1682+ String csvContent = "col1,col2,col3,col4,col5,col6,col7,col8\n " +
1683+ "rec1,rec2,rec3,rec4,\" \" rec5\" \" ,rec6,rec7,rec8" ;
1684+
1685+ StringReader stringReader = new StringReader (csvContent );
1686+ CSVFormat csvFormat = CSVFormat .DEFAULT .builder ()
1687+ .setHeader ()
1688+ .setSkipHeaderRecord (true )
1689+ .build ();
1690+
1691+ CSVParser csvParser = null ;
1692+ try {
1693+ csvParser = csvFormat .parse (stringReader );
1694+ } catch (IOException e ) {
1695+ fail ("Failed to parse the CSV content" );
1696+ }
1697+ final Iterable <CSVRecord > finalRecords = csvParser ;
1698+ Exception exception = assertThrows (UncheckedIOException .class , () -> {
1699+ for (CSVRecord record : finalRecords ) {
1700+ System .out .println (record .get (0 ) + " " + record .get (1 ) + " " + record .get (2 ) + " " + record .get (3 )
1701+ + " " + record .get (4 ) + " " + record .get (5 ) + " " + record .get (6 ) + " " + record .get (7 ));
1702+ }
1703+ });
1704+ String expectedErrorMessage = "Exception reading next record: java.io.IOException: An exception occurred " +
1705+ "while tying to parse the CSV content. Issue in line: 2, position: 63" ;
1706+ String actualMessage = exception .getMessage ();
1707+ assertTrue (actualMessage .contains (expectedErrorMessage ));
1708+ assertNotNull (csvParser );
1709+ String expectedLastParsedContent = "rec1,rec2,rec3,rec4" ;
1710+ assertEquals (expectedLastParsedContent , csvParser .getLastParsedContent ());
1711+ }
16721712}
0 commit comments