This is an automated email from the ASF dual-hosted git repository. ggregory pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-csv.git
commit 088672f6f93dc5784e5e01478639706ac7ec41f9 Author: Gary Gregory <garydgreg...@gmail.com> AuthorDate: Fri Mar 14 16:08:06 2025 -0400 CSVParser.parse(File, Charset, CSVFormat) with a null CSVFormat maps to CSVFormat.DEFAULT (like CSVParser.parse(Reader, CSVFormat)) CSVParser.parse(Path, Charset, CSVFormat) with a null CSVFormat maps to CSVFormat.DEFAULT (like CSVParser.parse(Reader, CSVFormat)) --- src/changes/changes.xml | 2 ++ .../java/org/apache/commons/csv/CSVParser.java | 5 ++--- .../java/org/apache/commons/csv/CSVParserTest.java | 24 ++++++++++++++-------- 3 files changed, 20 insertions(+), 11 deletions(-) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index bca27da3..03dd0a98 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -46,6 +46,8 @@ <action type="fix" dev="ggregory" due-to="Gary Gregory">Remove -nouses directive from maven-bundle-plugin. OSGi package imports now state 'uses' definitions for package imports, this doesn't affect JPMS (from org.apache.commons:commons-parent:80).</action> <action type="fix" dev="ggregory" due-to="Gary Gregory">CSVParser.parse(URL, Charset, CSVFormat) with a null CSVFormat maps to CSVFormat.DEFAULT (like CSVParser.parse(Reader, CSVFormat)).</action> <action type="fix" dev="ggregory" due-to="Gary Gregory">CSVParser.parse(String, CSVFormat) with a null CSVFormat maps to CSVFormat.DEFAULT (like CSVParser.parse(Reader, CSVFormat)).</action> + <action type="fix" dev="ggregory" due-to="Gary Gregory">CSVParser.parse(File, Charset, CSVFormat) with a null CSVFormat maps to CSVFormat.DEFAULT (like CSVParser.parse(Reader, CSVFormat)).</action> + <action type="fix" dev="ggregory" due-to="Gary Gregory">CSVParser.parse(Path, Charset, CSVFormat) with a null CSVFormat maps to CSVFormat.DEFAULT (like CSVParser.parse(Reader, CSVFormat)).</action> <!-- ADD --> <action type="add" dev="ggregory" due-to="Gary Gregory">Define and use Maven property commons.jmh.version.</action> <action type="add" dev="ggregory" due-to="Gary Gregory">Add CSVFormat.Builder.setMaxRows(long).</action> diff --git a/src/main/java/org/apache/commons/csv/CSVParser.java b/src/main/java/org/apache/commons/csv/CSVParser.java index 42c44558..afda2e4f 100644 --- a/src/main/java/org/apache/commons/csv/CSVParser.java +++ b/src/main/java/org/apache/commons/csv/CSVParser.java @@ -307,7 +307,7 @@ public final class CSVParser implements Iterable<CSVRecord>, Closeable { * @param charset * The Charset to decode the given file. * @param format - * the CSVFormat used for CSV parsing. Must not be null. + * the CSVFormat used for CSV parsing, {@code null} maps to {@link CSVFormat#DEFAULT}. * @return a new parser * @throws IllegalArgumentException * If the parameters of the format are inconsistent or if either file or format are null. @@ -357,7 +357,7 @@ public final class CSVParser implements Iterable<CSVRecord>, Closeable { * @param charset * The Charset to decode the given file. * @param format - * the CSVFormat used for CSV parsing. Must not be null. + * the CSVFormat used for CSV parsing, {@code null} maps to {@link CSVFormat#DEFAULT}. * @return a new parser * @throws IllegalArgumentException * If the parameters of the format are inconsistent or if either file or format are null. @@ -369,7 +369,6 @@ public final class CSVParser implements Iterable<CSVRecord>, Closeable { @SuppressWarnings("resource") public static CSVParser parse(final Path path, final Charset charset, final CSVFormat format) throws IOException { Objects.requireNonNull(path, "path"); - Objects.requireNonNull(format, "format"); return parse(Files.newInputStream(path), charset, format); } diff --git a/src/test/java/org/apache/commons/csv/CSVParserTest.java b/src/test/java/org/apache/commons/csv/CSVParserTest.java index df9f0d8b..45287d35 100644 --- a/src/test/java/org/apache/commons/csv/CSVParserTest.java +++ b/src/test/java/org/apache/commons/csv/CSVParserTest.java @@ -1424,8 +1424,12 @@ public class CSVParserTest { } @Test - public void testParseFileNullFormat() { - assertThrows(NullPointerException.class, () -> CSVParser.parse(new File("CSVFileParser/test.csv"), Charset.defaultCharset(), null)); + public void testParseFileCharsetNullFormat() throws IOException { + final File file = new File("src/test/resources/org/apache/commons/csv/CSVFileParser/test.csv"); + try (CSVParser parser = CSVParser.parse(file, Charset.defaultCharset(), null)) { + // null maps to DEFAULT. + parseFully(parser); + } } @Test @@ -1448,6 +1452,15 @@ public class CSVParserTest { assertThrows(NullPointerException.class, () -> CSVParser.parse((URL) null, Charset.defaultCharset(), CSVFormat.DEFAULT)); } + @Test + public void testParsePathCharsetNullFormat() throws IOException { + final Path path = Paths.get("src/test/resources/org/apache/commons/csv/CSVFileParser/test.csv"); + try (CSVParser parser = CSVParser.parse(path, Charset.defaultCharset(), null)) { + // null maps to DEFAULT. + parseFully(parser); + } + } + @Test public void testParserUrlNullCharsetFormat() { assertThrows(NullPointerException.class, () -> CSVParser.parse(new URL("https://commons.apache.org"), null, CSVFormat.DEFAULT)); @@ -1473,6 +1486,7 @@ public class CSVParserTest { final URL url = loader.getResource("org/apache/commons/csv/CSVFileParser/test.csv"); try (CSVParser parser = CSVParser.parse(url, Charset.defaultCharset(), null)) { // null maps to DEFAULT. + parseFully(parser); } } @@ -1567,10 +1581,8 @@ public class CSVParserTest { @Test public void testProvidedHeader() throws Exception { final Reader in = new StringReader("a,b,c\n1,2,3\nx,y,z"); - try (CSVParser parser = CSVFormat.DEFAULT.withHeader("A", "B", "C").parse(in)) { final Iterator<CSVRecord> records = parser.iterator(); - for (int i = 0; i < 3; i++) { assertTrue(records.hasNext()); final CSVRecord record = records.next(); @@ -1582,7 +1594,6 @@ public class CSVParserTest { assertEquals(record.get(1), record.get("B")); assertEquals(record.get(2), record.get("C")); } - assertFalse(records.hasNext()); } } @@ -1590,10 +1601,8 @@ public class CSVParserTest { @Test public void testProvidedHeaderAuto() throws Exception { final Reader in = new StringReader("a,b,c\n1,2,3\nx,y,z"); - try (CSVParser parser = CSVFormat.DEFAULT.withHeader().parse(in)) { final Iterator<CSVRecord> records = parser.iterator(); - for (int i = 0; i < 2; i++) { assertTrue(records.hasNext()); final CSVRecord record = records.next(); @@ -1605,7 +1614,6 @@ public class CSVParserTest { assertEquals(record.get(1), record.get("b")); assertEquals(record.get(2), record.get("c")); } - assertFalse(records.hasNext()); } }