fixing code review changes

This commit is contained in:
gbidsilva 2023-08-31 19:38:32 +05:30
parent 2448f4fbe4
commit 76ee73d7e4
2 changed files with 17 additions and 28 deletions

View File

@ -805,11 +805,10 @@ public final class CSVParser implements Iterable<CSVRecord>, Closeable {
final long startCharPosition = lexer.getCharacterPosition() + this.characterOffset; final long startCharPosition = lexer.getCharacterPosition() + this.characterOffset;
do { do {
this.reusableToken.reset(); this.reusableToken.reset();
// https://issues.apache.org/jira/browse/CSV-147
try { try {
this.lexer.nextToken(this.reusableToken); this.lexer.nextToken(this.reusableToken);
} catch (IOException ioe) { } catch (IOException ioe) {
String errorMessage = "An exception occurred while parsing the CSV content. Issue in line: " String errorMessage = "Exception during parsing at line: "
+ this.lexer.getCurrentLineNumber() + ", position: " + this.lexer.getCharacterPosition(); + this.lexer.getCurrentLineNumber() + ", position: " + this.lexer.getCharacterPosition();
throw new IOException(errorMessage, ioe); throw new IOException(errorMessage, ioe);
} }

View File

@ -1644,7 +1644,7 @@ public class CSVParserTest {
} }
@Test @Test
public void testFaultyCSVShouldThrowErrorAndDetailedMessageShouldBeAvailable_1() { public void testFaultyCSVShouldThrowErrorAndDetailedMessageShouldBeAvailable_1() throws IOException {
String csvContent = "col1,col2,col3,col4,col5,col6,col7,col8,col9,col10\n" + String csvContent = "col1,col2,col3,col4,col5,col6,col7,col8,col9,col10\n" +
"rec1,rec2,rec3,rec4,rec5,rec6,rec7,rec8,\"\"rec9\"\",rec10"; "rec1,rec2,rec3,rec4,rec5,rec6,rec7,rec8,\"\"rec9\"\",rec10";
@ -1654,22 +1654,17 @@ public class CSVParserTest {
.setSkipHeaderRecord(true) .setSkipHeaderRecord(true)
.build(); .build();
CSVParser csvParser = null; CSVParser csvParser = csvFormat.parse(stringReader);
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, () -> { Exception exception = assertThrows(UncheckedIOException.class, () -> {
for (CSVRecord record : finalRecords) { for (CSVRecord record : csvParser) {
System.out.println(record.get(0) + " " + record.get(1) + " " + record.get(2) + " " + record.get(3) String content = 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(4) + " " + record.get(5) + " " + record.get(6) + " " + record.get(7)
+ " " + record.get(8) + " " + record.get(9)); + " " + record.get(8) + " " + record.get(9);
assertNotNull(content);
} }
}); });
String expectedErrorMessage = "Exception reading next record: java.io.IOException: An exception occurred " + String expectedErrorMessage = "Exception reading next record: java.io.IOException: Exception during parsing at " +
"while parsing the CSV content. Issue in line: 2, position: 94"; "line: 2, position: 94";
String actualMessage = exception.getMessage(); String actualMessage = exception.getMessage();
assertTrue(actualMessage.contains(expectedErrorMessage)); assertTrue(actualMessage.contains(expectedErrorMessage));
assertNotNull(csvParser); assertNotNull(csvParser);
@ -1678,7 +1673,7 @@ public class CSVParserTest {
} }
@Test @Test
public void testFaultyCSVShouldThrowErrorAndDetailedMessageShouldBeAvailable_2() { public void testFaultyCSVShouldThrowErrorAndDetailedMessageShouldBeAvailable_2() throws IOException {
String csvContent = "col1,col2,col3,col4,col5,col6,col7,col8\n" + String csvContent = "col1,col2,col3,col4,col5,col6,col7,col8\n" +
"rec1,rec2,rec3,rec4,\"\"rec5\"\",rec6,rec7,rec8"; "rec1,rec2,rec3,rec4,\"\"rec5\"\",rec6,rec7,rec8";
@ -1688,21 +1683,16 @@ public class CSVParserTest {
.setSkipHeaderRecord(true) .setSkipHeaderRecord(true)
.build(); .build();
CSVParser csvParser = null; CSVParser csvParser = csvFormat.parse(stringReader);
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, () -> { Exception exception = assertThrows(UncheckedIOException.class, () -> {
for (CSVRecord record : finalRecords) { for (CSVRecord record : csvParser) {
System.out.println(record.get(0) + " " + record.get(1) + " " + record.get(2) + " " + record.get(3) String content = 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(4) + " " + record.get(5) + " " + record.get(6) + " " + record.get(7);
assertNotNull(content);
} }
}); });
String expectedErrorMessage = "Exception reading next record: java.io.IOException: An exception occurred " + String expectedErrorMessage = "Exception reading next record: java.io.IOException: Exception during parsing at " +
"while parsing the CSV content. Issue in line: 2, position: 63"; "line: 2, position: 63";
String actualMessage = exception.getMessage(); String actualMessage = exception.getMessage();
assertTrue(actualMessage.contains(expectedErrorMessage)); assertTrue(actualMessage.contains(expectedErrorMessage));
assertNotNull(csvParser); assertNotNull(csvParser);