From 1269c133ff4637ed658fb8ab5d78a8671ecfed4a Mon Sep 17 00:00:00 2001 From: Gary Gregory Date: Sat, 21 Jan 2023 14:35:28 -0500 Subject: [PATCH] Tests CSV-141 and PR 295 --- .../org/apache/commons/csv/CSVParserTest.java | 111 ++++++++++++++++++ .../apache/commons/csv/CSV-141/csv-141.csv | 4 + 2 files changed, 115 insertions(+) create mode 100644 src/test/resources/org/apache/commons/csv/CSV-141/csv-141.csv diff --git a/src/test/java/org/apache/commons/csv/CSVParserTest.java b/src/test/java/org/apache/commons/csv/CSVParserTest.java index 10c7b97c..851a45d9 100644 --- a/src/test/java/org/apache/commons/csv/CSVParserTest.java +++ b/src/test/java/org/apache/commons/csv/CSVParserTest.java @@ -113,6 +113,14 @@ public class CSVParserTest { 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); } @@ -265,6 +273,109 @@ public class CSVParserTest { 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" diff --git a/src/test/resources/org/apache/commons/csv/CSV-141/csv-141.csv b/src/test/resources/org/apache/commons/csv/CSV-141/csv-141.csv new file mode 100644 index 00000000..e685adc8 --- /dev/null +++ b/src/test/resources/org/apache/commons/csv/CSV-141/csv-141.csv @@ -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