Skip to content

Commit

Permalink
Tests CSV-141 and PR 295
Browse files Browse the repository at this point in the history
  • Loading branch information
garydgregory committed Jan 21, 2023
1 parent c22ff41 commit 1269c13
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 0 deletions.
111 changes: 111 additions & 0 deletions src/test/java/org/apache/commons/csv/CSVParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,14 @@ private BOMInputStream createBOMInputStream(final String resource) throws IOExce
return new BOMInputStream(ClassLoader.getSystemClassLoader().getResource(resource).openStream());
}

CSVRecord parse(final CSVParser parser, final int failParseRecordNo) throws IOException {
if (parser.getRecordNumber() + 1 == failParseRecordNo) {
assertThrows(IOException.class, () -> parser.nextRecord());
return null;
}
return parser.nextRecord();
}

private void parseFully(final CSVParser parser) {
parser.forEach(Assertions::assertNotNull);
}
Expand Down Expand Up @@ -265,6 +273,109 @@ public void testClose() throws Exception {
assertThrows(NoSuchElementException.class, records::next);
}

@Test
public void testCSV141_CSVFormat_DEFAULT() throws Exception {
testCSV141Failure(CSVFormat.DEFAULT, 3);
}

@Test
public void testCSV141CSVFormat_INFORMIX_UNLOAD() throws Exception {
testCSV141Failure(CSVFormat.INFORMIX_UNLOAD, 1);
}

@Test
public void testCSV141CSVFormat_INFORMIX_UNLOAD_CSV() throws Exception {
testCSV141Failure(CSVFormat.INFORMIX_UNLOAD_CSV, 3);
}

@Test
public void testCSV141CSVFormat_ORACLE() throws Exception {
testCSV141Failure(CSVFormat.ORACLE, 2);
}


@Test
public void testCSV141CSVFormat_POSTGRESQL_CSV() throws Exception {
testCSV141Failure(CSVFormat.POSTGRESQL_CSV, 3);
}

@Test
@Disabled("PR 295 does not work")
public void testCSV141Excel() throws Exception {
testCSV141Ok(CSVFormat.EXCEL);
}

private void testCSV141Failure(final CSVFormat format, final int failParseRecordNo) throws IOException {
final Path path = Paths.get("src/test/resources/org/apache/commons/csv/CSV-141/csv-141.csv");
try (final CSVParser parser = CSVParser.parse(path, StandardCharsets.UTF_8, format)) {
// row 1
CSVRecord record = parse(parser, failParseRecordNo);
if (record == null) {
return; // expected failure
}
assertEquals("1414770317901", record.get(0));
assertEquals("android.widget.EditText", record.get(1));
assertEquals("pass sem1 _84*|*", record.get(2));
assertEquals("0", record.get(3));
assertEquals("pass sem1 _8", record.get(4));
assertEquals(5, record.size());
// row 2
record = parse(parser, failParseRecordNo);
if (record == null) {
return; // expected failure
}
assertEquals("1414770318470", record.get(0));
assertEquals("android.widget.EditText", record.get(1));
assertEquals("pass sem1 _84:|", record.get(2));
assertEquals("0", record.get(3));
assertEquals("pass sem1 _84:\\", record.get(4));
assertEquals(5, record.size());
// row 3: Fail for certain
assertThrows(IOException.class, () -> parser.nextRecord());
}
}

private void testCSV141Ok(final CSVFormat format) throws IOException {
final Path path = Paths.get("src/test/resources/org/apache/commons/csv/CSV-141/csv-141.csv");
try (final CSVParser parser = CSVParser.parse(path, StandardCharsets.UTF_8, format)) {
// row 1
CSVRecord record = parser.nextRecord();
assertEquals("1414770317901", record.get(0));
assertEquals("android.widget.EditText", record.get(1));
assertEquals("pass sem1 _84*|*", record.get(2));
assertEquals("0", record.get(3));
assertEquals("pass sem1 _8", record.get(4));
assertEquals(5, record.size());
// row 2
record = parser.nextRecord();
assertEquals("1414770318470", record.get(0));
assertEquals("android.widget.EditText", record.get(1));
assertEquals("pass sem1 _84:|", record.get(2));
assertEquals("0", record.get(3));
assertEquals("pass sem1 _84:\\", record.get(4));
assertEquals(5, record.size());
// row 3
record = parser.nextRecord();
assertEquals("1414770318327", record.get(0));
assertEquals("android.widget.EditText", record.get(1));
assertEquals("pass sem1", record.get(2));
assertEquals(3, record.size());
// row 4
record = parser.nextRecord();
assertEquals("1414770318628", record.get(0));
assertEquals("android.widget.EditText", record.get(1));
assertEquals("pass sem1 _84*|*", record.get(2));
assertEquals("0", record.get(3));
assertEquals("pass sem1", record.get(4));
assertEquals(5, record.size());
}
}

@Test
public void testCSV141RFC4180() throws Exception {
testCSV141Failure(CSVFormat.RFC4180, 3);
}

@Test
public void testCSV235() throws IOException {
final String dqString = "\"aaa\",\"b\"\"bb\",\"ccc\""; // "aaa","b""bb","ccc"
Expand Down
4 changes: 4 additions & 0 deletions src/test/resources/org/apache/commons/csv/CSV-141/csv-141.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
"1414770317901","android.widget.EditText","pass sem1 _84*|*","0","pass sem1 _8"
"1414770318470","android.widget.EditText","pass sem1 _84:|","0","pass sem1 _84:\"
"1414770318327","android.widget.EditText","pass sem1
"1414770318628","android.widget.EditText","pass sem1 _84*|*","0","pass sem1

0 comments on commit 1269c13

Please sign in to comment.