Skip to content

Commit dbdd56c

Browse files
committed
[CSV-147] Better error message during faulty CSV record read #347
Clean up new test
1 parent a33b24c commit dbdd56c

2 files changed

Lines changed: 18 additions & 23 deletions

File tree

src/changes/changes.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
<action type="fix" dev="ggregory" due-to="Seth Falco, Bruno P. Kinoshita">Document duplicate header behavior #309.</action>
4949
<action type="fix" dev="ggregory" due-to="jkbkupczyk">Add missing docs #328.</action>
5050
<action type="fix" dev="ggregory" due-to="step-security-bot">[StepSecurity] CI: Harden GitHub Actions #329, #330.</action>
51+
<action type="fix" issue="CSV-147" dev="ggregory" due-to="Steven Peterson, Benedikt Ritter, Gary Gregory, Joerg Schaible, Buddhi De Silva, Elliotte Rusty Harold">Better error message during faulty CSV record read #347.</action>
5152
<!-- UPDATE -->
5253
<action type="update" dev="ggregory" due-to="Gary Gregory">Bump commons-io:commons-io: from 2.11.0 to 2.13.0.</action>
5354
<action type="update" dev="ggregory" due-to="Gary Gregory">Bump commons-parent from 57 to 62.</action>

src/test/java/org/apache/commons/csv/CSVParserTest.java

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -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\nrec1,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\",\nx,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

Comments
 (0)