elharo commented on code in PR #347:
URL: https://github.com/apache/commons-csv/pull/347#discussion_r1311642239
##########
src/test/java/org/apache/commons/csv/CSVParserTest.java:
##########
@@ -1642,4 +1643,70 @@ private void validateRecordPosition(final String
lineSeparator) throws IOExcepti
parser.close();
}
+ @Test
+ public void
testFaultyCSVShouldThrowErrorAndDetailedMessageShouldBeAvailable_1() {
+ 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 = null;
+ try {
+ csvParser = csvFormat.parse(stringReader);
+ } catch (IOException e) {
+ fail("Failed to parse the CSV content");
+ }
+ final Iterable<CSVRecord> finalRecords = csvParser;
+ Exception exception = assertThrows(UncheckedIOException.class, () -> {
+ for (CSVRecord record : finalRecords) {
+ System.out.println(record.get(0) + " " + record.get(1) + " " +
record.get(2) + " " + record.get(3)
Review Comment:
don't print anything except test failure from tests so failures are easier
to find
##########
src/test/java/org/apache/commons/csv/CSVParserTest.java:
##########
@@ -1642,4 +1643,70 @@ private void validateRecordPosition(final String
lineSeparator) throws IOExcepti
parser.close();
}
+ @Test
+ public void
testFaultyCSVShouldThrowErrorAndDetailedMessageShouldBeAvailable_1() {
+ 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 = null;
+ try {
+ csvParser = csvFormat.parse(stringReader);
+ } catch (IOException e) {
+ fail("Failed to parse the CSV content");
+ }
+ final Iterable<CSVRecord> finalRecords = csvParser;
Review Comment:
I don't see why there's a new variable for the same thing. Can you just
csvParser?
##########
src/test/java/org/apache/commons/csv/CSVParserTest.java:
##########
@@ -1642,4 +1643,70 @@ private void validateRecordPosition(final String
lineSeparator) throws IOExcepti
parser.close();
}
+ @Test
+ public void
testFaultyCSVShouldThrowErrorAndDetailedMessageShouldBeAvailable_1() {
+ 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 = null;
+ try {
+ csvParser = csvFormat.parse(stringReader);
+ } catch (IOException e) {
+ fail("Failed to parse the CSV content");
+ }
+ final Iterable<CSVRecord> finalRecords = csvParser;
+ Exception exception = assertThrows(UncheckedIOException.class, () -> {
+ for (CSVRecord record : finalRecords) {
+ System.out.println(record.get(0) + " " + record.get(1) + " " +
record.get(2) + " " + record.get(3)
+ + " " + record.get(4) + " " + record.get(5) + " " +
record.get(6) + " " + record.get(7)
+ + " " + record.get(8) + " " + record.get(9));
+ }
+ });
+ String expectedErrorMessage = "Exception reading next record:
java.io.IOException: An exception occurred " +
+ "while parsing the CSV content. Issue in line: 2, position:
94";
+ String actualMessage = exception.getMessage();
+ assertTrue(actualMessage.contains(expectedErrorMessage));
+ assertNotNull(csvParser);
+ String expectedLastParsedContent = "...rec4,rec5,rec6,rec7,rec8";
+ assertEquals(expectedLastParsedContent,
csvParser.getLastParsedContent());
+ }
+
+ @Test
+ public void
testFaultyCSVShouldThrowErrorAndDetailedMessageShouldBeAvailable_2() {
+ String csvContent = "col1,col2,col3,col4,col5,col6,col7,col8\n" +
+ "rec1,rec2,rec3,rec4,\"\"rec5\"\",rec6,rec7,rec8";
+
+ StringReader stringReader = new StringReader(csvContent);
+ CSVFormat csvFormat = CSVFormat.DEFAULT.builder()
+ .setHeader()
+ .setSkipHeaderRecord(true)
+ .build();
+
+ CSVParser csvParser = null;
Review Comment:
same comments as above
##########
src/test/java/org/apache/commons/csv/CSVParserTest.java:
##########
@@ -1642,4 +1643,70 @@ private void validateRecordPosition(final String
lineSeparator) throws IOExcepti
parser.close();
}
+ @Test
+ public void
testFaultyCSVShouldThrowErrorAndDetailedMessageShouldBeAvailable_1() {
+ 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 = null;
+ try {
+ csvParser = csvFormat.parse(stringReader);
+ } catch (IOException e) {
+ fail("Failed to parse the CSV content");
+ }
+ final Iterable<CSVRecord> finalRecords = csvParser;
+ Exception exception = assertThrows(UncheckedIOException.class, () -> {
+ for (CSVRecord record : finalRecords) {
+ System.out.println(record.get(0) + " " + record.get(1) + " " +
record.get(2) + " " + record.get(3)
+ + " " + record.get(4) + " " + record.get(5) + " " +
record.get(6) + " " + record.get(7)
+ + " " + record.get(8) + " " + record.get(9));
+ }
+ });
+ String expectedErrorMessage = "Exception reading next record:
java.io.IOException: An exception occurred " +
+ "while parsing the CSV content. Issue in line: 2, position:
94";
+ String actualMessage = exception.getMessage();
+ assertTrue(actualMessage.contains(expectedErrorMessage));
+ assertNotNull(csvParser);
Review Comment:
this assert doesn't seem helpful
##########
src/test/java/org/apache/commons/csv/CSVParserTest.java:
##########
@@ -1642,4 +1643,70 @@ private void validateRecordPosition(final String
lineSeparator) throws IOExcepti
parser.close();
}
+ @Test
+ public void
testFaultyCSVShouldThrowErrorAndDetailedMessageShouldBeAvailable_1() {
+ 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 = null;
+ try {
+ csvParser = csvFormat.parse(stringReader);
+ } catch (IOException e) {
+ fail("Failed to parse the CSV content");
Review Comment:
not needed, an exception will fail the test. Remove the try-catch
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]