Skip to content

Commit cdb9618

Browse files
committed
[CSV-225] Test case updates.
CSVParserTest: - Add basic parser test for all parse/constructor variants. - Add parser test for null Path. - Fix parser test for null URL that was cast to File. - Fix parset test for null File/format so it does not pass an illegal file name and trigger a FileNotFoundException (only prevented by the sequence of arg validation). - Convert all arg validating tests to use try resource blocks. PerformanceTest: - Fix perf test so that it doesn't look for the input file in the java.io.tmpdir but instead as a relative path. - Add basic parse timings for other constructor variants using Path and URL. - Avoid creating a redundant BufferedReader in basic parser test. - Use printf.
1 parent ddcf2cf commit cdb9618

2 files changed

Lines changed: 173 additions & 68 deletions

File tree

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

Lines changed: 64 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@
3939
import java.net.URL;
4040
import java.nio.charset.Charset;
4141
import java.nio.charset.StandardCharsets;
42+
import java.nio.file.Files;
43+
import java.nio.file.Path;
44+
import java.nio.file.Paths;
4245
import java.util.ArrayList;
4346
import java.util.Iterator;
4447
import java.util.List;
@@ -810,24 +813,78 @@ public void testNoHeaderMap() throws Exception {
810813
}
811814
}
812815

816+
@Test
817+
public void testParse() throws Exception {
818+
final ClassLoader loader = ClassLoader.getSystemClassLoader();
819+
final URL url = loader.getResource("CSVFileParser/test.csv");
820+
final CSVFormat format = CSVFormat.DEFAULT.withHeader("A", "B", "C", "D");
821+
final Charset charset = StandardCharsets.UTF_8;
822+
823+
try(final CSVParser parser = CSVParser.parse(new InputStreamReader(url.openStream(), charset), format)) {
824+
parseFully(parser);
825+
}
826+
try(final CSVParser parser = CSVParser.parse(new String(Files.readAllBytes(Paths.get(url.toURI())), charset), format)) {
827+
parseFully(parser);
828+
}
829+
try(final CSVParser parser = CSVParser.parse(new File(url.toURI()), charset, format)) {
830+
parseFully(parser);
831+
}
832+
try(final CSVParser parser = CSVParser.parse(url.openStream(), charset, format)) {
833+
parseFully(parser);
834+
}
835+
try(final CSVParser parser = CSVParser.parse(Paths.get(url.toURI()), charset, format)) {
836+
parseFully(parser);
837+
}
838+
try(final CSVParser parser = CSVParser.parse(url, charset, format)) {
839+
parseFully(parser);
840+
}
841+
try(final CSVParser parser = new CSVParser(new InputStreamReader(url.openStream(), charset), format)) {
842+
parseFully(parser);
843+
}
844+
try(final CSVParser parser = new CSVParser(new InputStreamReader(url.openStream(), charset), format, /*characterOffset=*/0, /*recordNumber=*/1)) {
845+
parseFully(parser);
846+
}
847+
}
848+
849+
private void parseFully(final CSVParser parser) {
850+
for (final Iterator<CSVRecord> records = parser.iterator(); records.hasNext(); ) {
851+
records.next();
852+
}
853+
}
854+
813855
@Test(expected = IllegalArgumentException.class)
814856
public void testParseFileNullFormat() throws Exception {
815-
CSVParser.parse(new File(""), Charset.defaultCharset(), null);
857+
try (final CSVParser parser = CSVParser.parse(new File("CSVFileParser/test.csv"), Charset.defaultCharset(), null)) {
858+
Assert.fail("This test should have thrown an exception.");
859+
}
816860
}
817861

818862
@Test(expected = IllegalArgumentException.class)
819863
public void testParseNullFileFormat() throws Exception {
820-
CSVParser.parse((File) null, Charset.defaultCharset(), CSVFormat.DEFAULT);
864+
try (final CSVParser parser = CSVParser.parse((File) null, Charset.defaultCharset(), CSVFormat.DEFAULT)) {
865+
Assert.fail("This test should have thrown an exception.");
866+
}
867+
}
868+
869+
@Test(expected = IllegalArgumentException.class)
870+
public void testParseNullPathFormat() throws Exception {
871+
try (final CSVParser parser = CSVParser.parse((Path) null, Charset.defaultCharset(), CSVFormat.DEFAULT)) {
872+
Assert.fail("This test should have thrown an exception.");
873+
}
821874
}
822875

823876
@Test(expected = IllegalArgumentException.class)
824877
public void testParseNullStringFormat() throws Exception {
825-
CSVParser.parse((String) null, CSVFormat.DEFAULT);
878+
try (final CSVParser parser = CSVParser.parse((String) null, CSVFormat.DEFAULT)) {
879+
Assert.fail("This test should have thrown an exception.");
880+
}
826881
}
827882

828883
@Test(expected = IllegalArgumentException.class)
829884
public void testParseNullUrlCharsetFormat() throws Exception {
830-
CSVParser.parse((File) null, Charset.defaultCharset(), CSVFormat.DEFAULT);
885+
try (final CSVParser parser = CSVParser.parse((URL) null, Charset.defaultCharset(), CSVFormat.DEFAULT)) {
886+
Assert.fail("This test should have thrown an exception.");
887+
}
831888
}
832889

833890
@Test(expected = IllegalArgumentException.class)
@@ -839,7 +896,9 @@ public void testParserUrlNullCharsetFormat() throws Exception {
839896

840897
@Test(expected = IllegalArgumentException.class)
841898
public void testParseStringNullFormat() throws Exception {
842-
CSVParser.parse("csv data", null);
899+
try (final CSVParser parser = CSVParser.parse("csv data", (CSVFormat) null)) {
900+
Assert.fail("This test should have thrown an exception.");
901+
}
843902
}
844903

845904
@Test(expected = IllegalArgumentException.class)

0 commit comments

Comments
 (0)