breaking the PR into to two parts by removing the new method
This commit is contained in:
parent
2f44a6b399
commit
4b1d09a490
|
@ -440,44 +440,6 @@ public final class CSVParser implements Iterable<CSVRecord>, Closeable {
|
||||||
this.recordNumber = recordNumber - 1;
|
this.recordNumber = recordNumber - 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Return the parsed CSV content of currently parsed line up until this method is called.
|
|
||||||
* <p>
|
|
||||||
* Maximum parsed token length set by the 'maxParsedTokenCount' is considered during the construction of return string.
|
|
||||||
* </p>
|
|
||||||
* <p>
|
|
||||||
* Example:
|
|
||||||
* </p>
|
|
||||||
* </p>
|
|
||||||
* If currently reading CSV record row contains following data and 'maxParsedTokenCount' is set to 5 and current reading position is col7
|
|
||||||
* </p>
|
|
||||||
* <pre>
|
|
||||||
* col1, col2, col3, col4, col5, col6, col7
|
|
||||||
* </pre>
|
|
||||||
* <p>
|
|
||||||
* then method returns following
|
|
||||||
* </p>
|
|
||||||
* <pre>
|
|
||||||
* col3, col4, col5, col6, col7
|
|
||||||
* </pre>
|
|
||||||
* @return parsed CSV content of current reading line
|
|
||||||
*/
|
|
||||||
public String getLastParsedContent() {
|
|
||||||
String parsedContent = "";
|
|
||||||
int recordListSize = this.recordList.size();
|
|
||||||
if (recordListSize > 0) {
|
|
||||||
if (recordListSize <= this.maxParsedTokenCount) {
|
|
||||||
parsedContent = String.join(this.format.getDelimiterString(), this.recordList.toArray(Constants.EMPTY_STRING_ARRAY));
|
|
||||||
} else {
|
|
||||||
// number of parsed token exceed required token count. Take the expected tokens from the end.
|
|
||||||
int startIndex = recordListSize - maxParsedTokenCount;
|
|
||||||
List<String> lastParsedTokenList = this.recordList.subList(startIndex, recordListSize);
|
|
||||||
parsedContent = "..." + String.join(this.format.getDelimiterString(), lastParsedTokenList.toArray(Constants.EMPTY_STRING_ARRAY));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return parsedContent;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void addRecordValue(final boolean lastRecord) {
|
private void addRecordValue(final boolean lastRecord) {
|
||||||
final String input = this.format.trim(this.reusableToken.content.toString());
|
final String input = this.format.trim(this.reusableToken.content.toString());
|
||||||
if (lastRecord && input.isEmpty() && this.format.getTrailingDelimiter()) {
|
if (lastRecord && input.isEmpty() && this.format.getTrailingDelimiter()) {
|
||||||
|
|
|
@ -1643,58 +1643,4 @@ public class CSVParserTest {
|
||||||
parser.close();
|
parser.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testFaultyCSVShouldThrowErrorAndDetailedMessageShouldBeAvailable_1() 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) {
|
|
||||||
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(8) + " " + record.get(9);
|
|
||||||
assertNotNull(content);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
String expectedErrorMessage = "Exception reading next record: java.io.IOException: Exception during parsing at " +
|
|
||||||
"line: 2, position: 94";
|
|
||||||
String actualMessage = exception.getMessage();
|
|
||||||
assertTrue(actualMessage.contains(expectedErrorMessage));
|
|
||||||
String expectedLastParsedContent = "...rec4,rec5,rec6,rec7,rec8";
|
|
||||||
assertEquals(expectedLastParsedContent, csvParser.getLastParsedContent());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testFaultyCSVShouldThrowErrorAndDetailedMessageShouldBeAvailable_2() throws IOException {
|
|
||||||
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 = csvFormat.parse(stringReader);
|
|
||||||
Exception exception = assertThrows(UncheckedIOException.class, () -> {
|
|
||||||
for (CSVRecord record : csvParser) {
|
|
||||||
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);
|
|
||||||
assertNotNull(content);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
String expectedErrorMessage = "Exception reading next record: java.io.IOException: Exception during parsing at " +
|
|
||||||
"line: 2, position: 63";
|
|
||||||
String actualMessage = exception.getMessage();
|
|
||||||
assertTrue(actualMessage.contains(expectedErrorMessage));
|
|
||||||
String expectedLastParsedContent = "rec1,rec2,rec3,rec4";
|
|
||||||
assertEquals(expectedLastParsedContent, csvParser.getLastParsedContent());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue