Skip to content

Commit 088672f

Browse files
committed
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))
1 parent 931531b commit 088672f

File tree

3 files changed

+20
-11
lines changed

3 files changed

+20
-11
lines changed

src/changes/changes.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@
4646
<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>
4747
<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>
4848
<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>
49+
<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>
50+
<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>
4951
<!-- ADD -->
5052
<action type="add" dev="ggregory" due-to="Gary Gregory">Define and use Maven property commons.jmh.version.</action>
5153
<action type="add" dev="ggregory" due-to="Gary Gregory">Add CSVFormat.Builder.setMaxRows(long).</action>

src/main/java/org/apache/commons/csv/CSVParser.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ public static Builder builder() {
307307
* @param charset
308308
* The Charset to decode the given file.
309309
* @param format
310-
* the CSVFormat used for CSV parsing. Must not be null.
310+
* the CSVFormat used for CSV parsing, {@code null} maps to {@link CSVFormat#DEFAULT}.
311311
* @return a new parser
312312
* @throws IllegalArgumentException
313313
* If the parameters of the format are inconsistent or if either file or format are null.
@@ -357,7 +357,7 @@ public static CSVParser parse(final InputStream inputStream, final Charset chars
357357
* @param charset
358358
* The Charset to decode the given file.
359359
* @param format
360-
* the CSVFormat used for CSV parsing. Must not be null.
360+
* the CSVFormat used for CSV parsing, {@code null} maps to {@link CSVFormat#DEFAULT}.
361361
* @return a new parser
362362
* @throws IllegalArgumentException
363363
* If the parameters of the format are inconsistent or if either file or format are null.
@@ -369,7 +369,6 @@ public static CSVParser parse(final InputStream inputStream, final Charset chars
369369
@SuppressWarnings("resource")
370370
public static CSVParser parse(final Path path, final Charset charset, final CSVFormat format) throws IOException {
371371
Objects.requireNonNull(path, "path");
372-
Objects.requireNonNull(format, "format");
373372
return parse(Files.newInputStream(path), charset, format);
374373
}
375374

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

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1424,8 +1424,12 @@ public void testParse() throws Exception {
14241424
}
14251425

14261426
@Test
1427-
public void testParseFileNullFormat() {
1428-
assertThrows(NullPointerException.class, () -> CSVParser.parse(new File("CSVFileParser/test.csv"), Charset.defaultCharset(), null));
1427+
public void testParseFileCharsetNullFormat() throws IOException {
1428+
final File file = new File("src/test/resources/org/apache/commons/csv/CSVFileParser/test.csv");
1429+
try (CSVParser parser = CSVParser.parse(file, Charset.defaultCharset(), null)) {
1430+
// null maps to DEFAULT.
1431+
parseFully(parser);
1432+
}
14291433
}
14301434

14311435
@Test
@@ -1448,6 +1452,15 @@ public void testParseNullUrlCharsetFormat() {
14481452
assertThrows(NullPointerException.class, () -> CSVParser.parse((URL) null, Charset.defaultCharset(), CSVFormat.DEFAULT));
14491453
}
14501454

1455+
@Test
1456+
public void testParsePathCharsetNullFormat() throws IOException {
1457+
final Path path = Paths.get("src/test/resources/org/apache/commons/csv/CSVFileParser/test.csv");
1458+
try (CSVParser parser = CSVParser.parse(path, Charset.defaultCharset(), null)) {
1459+
// null maps to DEFAULT.
1460+
parseFully(parser);
1461+
}
1462+
}
1463+
14511464
@Test
14521465
public void testParserUrlNullCharsetFormat() {
14531466
assertThrows(NullPointerException.class, () -> CSVParser.parse(new URL("https://commons.apache.org"), null, CSVFormat.DEFAULT));
@@ -1473,6 +1486,7 @@ public void testParseUrlCharsetNullFormat() throws IOException {
14731486
final URL url = loader.getResource("org/apache/commons/csv/CSVFileParser/test.csv");
14741487
try (CSVParser parser = CSVParser.parse(url, Charset.defaultCharset(), null)) {
14751488
// null maps to DEFAULT.
1489+
parseFully(parser);
14761490
}
14771491
}
14781492

@@ -1567,10 +1581,8 @@ public void testParsingPrintedEmptyFirstColumn(final CSVFormat.Predefined format
15671581
@Test
15681582
public void testProvidedHeader() throws Exception {
15691583
final Reader in = new StringReader("a,b,c\n1,2,3\nx,y,z");
1570-
15711584
try (CSVParser parser = CSVFormat.DEFAULT.withHeader("A", "B", "C").parse(in)) {
15721585
final Iterator<CSVRecord> records = parser.iterator();
1573-
15741586
for (int i = 0; i < 3; i++) {
15751587
assertTrue(records.hasNext());
15761588
final CSVRecord record = records.next();
@@ -1582,18 +1594,15 @@ public void testProvidedHeader() throws Exception {
15821594
assertEquals(record.get(1), record.get("B"));
15831595
assertEquals(record.get(2), record.get("C"));
15841596
}
1585-
15861597
assertFalse(records.hasNext());
15871598
}
15881599
}
15891600

15901601
@Test
15911602
public void testProvidedHeaderAuto() throws Exception {
15921603
final Reader in = new StringReader("a,b,c\n1,2,3\nx,y,z");
1593-
15941604
try (CSVParser parser = CSVFormat.DEFAULT.withHeader().parse(in)) {
15951605
final Iterator<CSVRecord> records = parser.iterator();
1596-
15971606
for (int i = 0; i < 2; i++) {
15981607
assertTrue(records.hasNext());
15991608
final CSVRecord record = records.next();
@@ -1605,7 +1614,6 @@ public void testProvidedHeaderAuto() throws Exception {
16051614
assertEquals(record.get(1), record.get("b"));
16061615
assertEquals(record.get(2), record.get("c"));
16071616
}
1608-
16091617
assertFalse(records.hasNext());
16101618
}
16111619
}

0 commit comments

Comments
 (0)