You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2023/09/13 12:59:07 UTC
[commons-csv] branch master updated: [CSV-147] Better error message during faulty CSV record read #347
This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-csv.git
The following commit(s) were added to refs/heads/master by this push:
new dbdd56c8 [CSV-147] Better error message during faulty CSV record read #347
dbdd56c8 is described below
commit dbdd56c8a0d47855b3a854758b0324442c6e7c52
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Wed Sep 13 08:58:58 2023 -0400
[CSV-147] Better error message during faulty CSV record read #347
Clean up new test
---
src/changes/changes.xml | 1 +
.../java/org/apache/commons/csv/CSVParserTest.java | 40 +++++++++-------------
2 files changed, 18 insertions(+), 23 deletions(-)
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 8d38fc4d..61f0ebda 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -48,6 +48,7 @@
<action type="fix" dev="ggregory" due-to="Seth Falco, Bruno P. Kinoshita">Document duplicate header behavior #309.</action>
<action type="fix" dev="ggregory" due-to="jkbkupczyk">Add missing docs #328.</action>
<action type="fix" dev="ggregory" due-to="step-security-bot">[StepSecurity] CI: Harden GitHub Actions #329, #330.</action>
+ <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>
<!-- UPDATE -->
<action type="update" dev="ggregory" due-to="Gary Gregory">Bump commons-io:commons-io: from 2.11.0 to 2.13.0.</action>
<action type="update" dev="ggregory" due-to="Gary Gregory">Bump commons-parent from 57 to 62.</action>
diff --git a/src/test/java/org/apache/commons/csv/CSVParserTest.java b/src/test/java/org/apache/commons/csv/CSVParserTest.java
index b50bdfb4..1ccac64b 100644
--- a/src/test/java/org/apache/commons/csv/CSVParserTest.java
+++ b/src/test/java/org/apache/commons/csv/CSVParserTest.java
@@ -1523,6 +1523,23 @@ public class CSVParserTest {
assertArrayEquals(new String[] { "x", "y", "z" }, list.get(2).values());
}}
+ @Test
+ public void testThrowExceptionWithLineAndPosition() throws IOException {
+ final String csvContent = "col1,col2,col3,col4,col5,col6,col7,col8,col9,col10\nrec1,rec2,rec3,rec4,rec5,rec6,rec7,rec8,\"\"rec9\"\",rec10";
+ final StringReader stringReader = new StringReader(csvContent);
+ // @formatter:off
+ final CSVFormat csvFormat = CSVFormat.DEFAULT.builder()
+ .setHeader()
+ .setSkipHeaderRecord(true)
+ .build();
+ // @formatter:on
+
+ try (CSVParser csvParser = csvFormat.parse(stringReader)) {
+ final Exception exception = assertThrows(UncheckedIOException.class, csvParser::getRecords);
+ assertTrue(exception.getMessage().contains("Invalid char between encapsulated token and delimiter at line: 2, position: 94"));
+ }
+ }
+
@Test
public void testTrailingDelimiter() throws Exception {
final Reader in = new StringReader("a,a,a,\n\"1\",\"2\",\"3\",\nx,y,z,");
@@ -1641,27 +1658,4 @@ public class CSVParserTest {
parser.close();
}
-
- @Test
- public void testFaultyCSVShouldThrowExceptionWithLineAndPosition() throws IOException {
- String csvContent = "col1,col2,col3,col4,col5,col6,col7,col8,col9,col10\n" +
- "rec1,rec2,rec3,rec4,rec5,rec6,rec7,rec8,\"\"rec9\"\",rec10";
-
- StringReader stringReader = new StringReader(csvContent);
- CSVFormat csvFormat = CSVFormat.DEFAULT.builder()
- .setHeader()
- .setSkipHeaderRecord(true)
- .build();
-
- CSVParser csvParser = csvFormat.parse(stringReader);
- Exception exception = assertThrows(UncheckedIOException.class, () -> {
- for (CSVRecord record : csvParser) {
- // this will result in exception due to parsing issue
- }
- });
- String expectedErrorMessage = "Invalid char between encapsulated token and delimiter at " +
- "line: 2, position: 94";
- String actualMessage = exception.getMessage();
- assertTrue(actualMessage.contains(expectedErrorMessage));
- }
}