diff --git a/src/main/java/org/apache/commons/csv/CSVParser.java b/src/main/java/org/apache/commons/csv/CSVParser.java index 01634f29..57a87734 100644 --- a/src/main/java/org/apache/commons/csv/CSVParser.java +++ b/src/main/java/org/apache/commons/csv/CSVParser.java @@ -464,11 +464,11 @@ public final class CSVParser implements Iterable, Closeable { * * @return parsed CSV content of current reading line */ - private String getLastParsedContent(){ + private String getLastParsedContent() { String parsedContent = ""; int recordListSize = this.recordList.size(); - if(recordListSize > 0) { - if(recordListSize <= this.maxParsedTokenCount) { + if (recordListSize > 0) { + if (recordListSize <= this.maxParsedTokenCount) { parsedContent = String.join("", this.recordList.toArray(Constants.EMPTY_STRING_ARRAY)); } else { // number of parsed token exceed required token count. Take the expected tokens from the end. @@ -808,9 +808,9 @@ public final class CSVParser implements Iterable, Closeable { do { this.reusableToken.reset(); // https://issues.apache.org/jira/browse/CSV-147 - try{ + try { this.lexer.nextToken(this.reusableToken); - } catch (IOException ioe){ + } catch (IOException ioe) { String errorMessage = "An error occurred while tying to parse the CSV content. Error in line: " + this.lexer.getCurrentLineNumber() + ", position: " + this.lexer.getCharacterPosition() + ", last parsed content: " + this.getLastParsedContent(); diff --git a/src/test/java/org/apache/commons/csv/CSVParserTest.java b/src/test/java/org/apache/commons/csv/CSVParserTest.java index 41986342..24a1a3bf 100644 --- a/src/test/java/org/apache/commons/csv/CSVParserTest.java +++ b/src/test/java/org/apache/commons/csv/CSVParserTest.java @@ -1644,7 +1644,6 @@ public class CSVParserTest { @Test public void testFaultyCSVShouldThrowErrorWithDetailedMessage() { - String csvContent = "col1,col2,col3,col4,col5,col6,col7,col8,col9,col10\n" + "rec1,rec2,rec3,rec4,rec5,rec6,rec7,rec8,\"\"rec9\"\",rec10"; @@ -1654,18 +1653,20 @@ public class CSVParserTest { .setSkipHeaderRecord(true) .build(); - Exception exception = assertThrows(UncheckedIOException.class, ()-> { + Exception exception = assertThrows(UncheckedIOException.class, () -> { Iterable records = csvFormat.parse(stringReader); for (CSVRecord record : records) { - 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)); + 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 = "IOException reading next record: java.io.IOException: An error occurred while tying to parse the CSV content. Error in line: 2, position: 94, last parsed content: ...rec4,rec5,rec6,rec7,rec8"; + String expectedErrorMessage = "IOException reading next record: java.io.IOException: An error occurred while " + + "tying to parse the CSV content. Error in line: 2, position: 94, last parsed content: " + + "...rec4,rec5,rec6,rec7,rec8"; String actualMessage = exception.getMessage(); assertTrue(actualMessage.contains(expectedErrorMessage)); - - } }