Skip to content

Commit 76ee73d

Browse files
committed
fixing code review changes
1 parent 2448f4f commit 76ee73d

2 files changed

Lines changed: 17 additions & 28 deletions

File tree

src/main/java/org/apache/commons/csv/CSVParser.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -805,11 +805,10 @@ CSVRecord nextRecord() throws IOException {
805805
final long startCharPosition = lexer.getCharacterPosition() + this.characterOffset;
806806
do {
807807
this.reusableToken.reset();
808-
// https://issues.apache.org/jira/browse/CSV-147
809808
try {
810809
this.lexer.nextToken(this.reusableToken);
811810
} catch (IOException ioe) {
812-
String errorMessage = "An exception occurred while parsing the CSV content. Issue in line: "
811+
String errorMessage = "Exception during parsing at line: "
813812
+ this.lexer.getCurrentLineNumber() + ", position: " + this.lexer.getCharacterPosition();
814813
throw new IOException(errorMessage, ioe);
815814
}

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

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1644,7 +1644,7 @@ private void validateRecordPosition(final String lineSeparator) throws IOExcepti
16441644
}
16451645

16461646
@Test
1647-
public void testFaultyCSVShouldThrowErrorAndDetailedMessageShouldBeAvailable_1() {
1647+
public void testFaultyCSVShouldThrowErrorAndDetailedMessageShouldBeAvailable_1() throws IOException {
16481648
String csvContent = "col1,col2,col3,col4,col5,col6,col7,col8,col9,col10\n" +
16491649
"rec1,rec2,rec3,rec4,rec5,rec6,rec7,rec8,\"\"rec9\"\",rec10";
16501650

@@ -1654,22 +1654,17 @@ public void testFaultyCSVShouldThrowErrorAndDetailedMessageShouldBeAvailable_1()
16541654
.setSkipHeaderRecord(true)
16551655
.build();
16561656

1657-
CSVParser csvParser = null;
1658-
try {
1659-
csvParser = csvFormat.parse(stringReader);
1660-
} catch (IOException e) {
1661-
fail("Failed to parse the CSV content");
1662-
}
1663-
final Iterable<CSVRecord> finalRecords = csvParser;
1657+
CSVParser csvParser = csvFormat.parse(stringReader);
16641658
Exception exception = assertThrows(UncheckedIOException.class, () -> {
1665-
for (CSVRecord record : finalRecords) {
1666-
System.out.println(record.get(0) + " " + record.get(1) + " " + record.get(2) + " " + record.get(3)
1659+
for (CSVRecord record : csvParser) {
1660+
String content = record.get(0) + " " + record.get(1) + " " + record.get(2) + " " + record.get(3)
16671661
+ " " + record.get(4) + " " + record.get(5) + " " + record.get(6) + " " + record.get(7)
1668-
+ " " + record.get(8) + " " + record.get(9));
1662+
+ " " + record.get(8) + " " + record.get(9);
1663+
assertNotNull(content);
16691664
}
16701665
});
1671-
String expectedErrorMessage = "Exception reading next record: java.io.IOException: An exception occurred " +
1672-
"while parsing the CSV content. Issue in line: 2, position: 94";
1666+
String expectedErrorMessage = "Exception reading next record: java.io.IOException: Exception during parsing at " +
1667+
"line: 2, position: 94";
16731668
String actualMessage = exception.getMessage();
16741669
assertTrue(actualMessage.contains(expectedErrorMessage));
16751670
assertNotNull(csvParser);
@@ -1678,7 +1673,7 @@ public void testFaultyCSVShouldThrowErrorAndDetailedMessageShouldBeAvailable_1()
16781673
}
16791674

16801675
@Test
1681-
public void testFaultyCSVShouldThrowErrorAndDetailedMessageShouldBeAvailable_2() {
1676+
public void testFaultyCSVShouldThrowErrorAndDetailedMessageShouldBeAvailable_2() throws IOException {
16821677
String csvContent = "col1,col2,col3,col4,col5,col6,col7,col8\n" +
16831678
"rec1,rec2,rec3,rec4,\"\"rec5\"\",rec6,rec7,rec8";
16841679

@@ -1688,21 +1683,16 @@ public void testFaultyCSVShouldThrowErrorAndDetailedMessageShouldBeAvailable_2()
16881683
.setSkipHeaderRecord(true)
16891684
.build();
16901685

1691-
CSVParser csvParser = null;
1692-
try {
1693-
csvParser = csvFormat.parse(stringReader);
1694-
} catch (IOException e) {
1695-
fail("Failed to parse the CSV content");
1696-
}
1697-
final Iterable<CSVRecord> finalRecords = csvParser;
1686+
CSVParser csvParser = csvFormat.parse(stringReader);
16981687
Exception exception = assertThrows(UncheckedIOException.class, () -> {
1699-
for (CSVRecord record : finalRecords) {
1700-
System.out.println(record.get(0) + " " + record.get(1) + " " + record.get(2) + " " + record.get(3)
1701-
+ " " + record.get(4) + " " + record.get(5) + " " + record.get(6) + " " + record.get(7));
1688+
for (CSVRecord record : csvParser) {
1689+
String content = record.get(0) + " " + record.get(1) + " " + record.get(2) + " " + record.get(3)
1690+
+ " " + record.get(4) + " " + record.get(5) + " " + record.get(6) + " " + record.get(7);
1691+
assertNotNull(content);
17021692
}
17031693
});
1704-
String expectedErrorMessage = "Exception reading next record: java.io.IOException: An exception occurred " +
1705-
"while parsing the CSV content. Issue in line: 2, position: 63";
1694+
String expectedErrorMessage = "Exception reading next record: java.io.IOException: Exception during parsing at " +
1695+
"line: 2, position: 63";
17061696
String actualMessage = exception.getMessage();
17071697
assertTrue(actualMessage.contains(expectedErrorMessage));
17081698
assertNotNull(csvParser);

0 commit comments

Comments
 (0)