You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2025/03/14 20:29:58 UTC
(commons-csv) branch master updated (26a8442e -> d93c4940)
This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/commons-csv.git
from 26a8442e Better local variable name
new a3b4e892 Remove extra vertical whitespace
new 1ac1eec0 Javadoc
new 41362db1 CSVParser.parse(URL, Charset, CSVFormat) with a null CSVFormat maps to CSVFormat.DEFAULT (like CSVParser.parse(Reader, CSVFormat))
new 931531bc CSVParser.parse(String, CSVFormat) with a null CSVFormat maps to CSVFormat.DEFAULT (like CSVParser.parse(Reader, CSVFormat))
new 088672f6 CSVParser.parse(File, Charset, CSVFormat) with a null CSVFormat maps to CSVFormat.DEFAULT (like CSVParser.parse(Reader, CSVFormat))
new c36d6cde CSVParser.parse(InputStream, Charset, CSVFormat) with a null CSVFormat maps to CSVFormat.DEFAULT (like CSVParser.parse(Reader, CSVFormat))
new d93c4940 CSVParser.parse(*) methods with a null Charset maps to Charset.defaultCharset()
The 7 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
Summary of changes:
src/changes/changes.xml | 6 ++
.../java/org/apache/commons/csv/CSVParser.java | 81 ++++++++++------------
.../java/org/apache/commons/csv/CSVParserTest.java | 65 ++++++++++++-----
3 files changed, 91 insertions(+), 61 deletions(-)
(commons-csv) 02/07: Javadoc
Posted by gg...@apache.org.
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 1ac1eec07747101c0dd9b6a7a75ba114de5925c6
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Fri Mar 14 15:47:15 2025 -0400
Javadoc
---
src/main/java/org/apache/commons/csv/CSVParser.java | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/src/main/java/org/apache/commons/csv/CSVParser.java b/src/main/java/org/apache/commons/csv/CSVParser.java
index 954af79c..6163f2ed 100644
--- a/src/main/java/org/apache/commons/csv/CSVParser.java
+++ b/src/main/java/org/apache/commons/csv/CSVParser.java
@@ -184,7 +184,7 @@ public final class CSVParser implements Iterable<CSVRecord>, Closeable {
/**
* Sets the CSV format. A copy of the given format is kept.
*
- * @param format the CSV format, null is equivalent to {@link CSVFormat#DEFAULT}.
+ * @param format the CSV format, {@code null} resets to {@link CSVFormat#DEFAULT}.
* @return this instance.
*/
public Builder setFormat(final CSVFormat format) {
@@ -385,7 +385,7 @@ public final class CSVParser implements Iterable<CSVRecord>, Closeable {
* @param reader
* a Reader containing CSV-formatted input. Must not be null.
* @param format
- * the CSVFormat used for CSV parsing. Must not be null.
+ * the CSVFormat used for CSV parsing, {@code null} uses {@link CSVFormat#DEFAULT}.
* @return a new CSVParser configured with the given reader and format.
* @throws IllegalArgumentException
* If the parameters of the format are inconsistent or if either reader or format are null.
(commons-csv) 01/07: Remove extra vertical whitespace
Posted by gg...@apache.org.
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 a3b4e89203f73a18114ab6fd43c431e4c714a88a
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Fri Mar 14 15:41:32 2025 -0400
Remove extra vertical whitespace
---
src/main/java/org/apache/commons/csv/CSVParser.java | 12 ++----------
1 file changed, 2 insertions(+), 10 deletions(-)
diff --git a/src/main/java/org/apache/commons/csv/CSVParser.java b/src/main/java/org/apache/commons/csv/CSVParser.java
index 29e4bc73..954af79c 100644
--- a/src/main/java/org/apache/commons/csv/CSVParser.java
+++ b/src/main/java/org/apache/commons/csv/CSVParser.java
@@ -243,7 +243,6 @@ public final class CSVParser implements Iterable<CSVRecord>, Closeable {
if (current == null) {
current = getNextRecord();
}
-
return current != null;
}
@@ -254,7 +253,6 @@ public final class CSVParser implements Iterable<CSVRecord>, Closeable {
}
CSVRecord next = current;
current = null;
-
if (next == null) {
// hasNext() wasn't called before
next = getNextRecord();
@@ -262,7 +260,6 @@ public final class CSVParser implements Iterable<CSVRecord>, Closeable {
throw new NoSuchElementException("No more CSV records available");
}
}
-
return next;
}
@@ -624,7 +621,6 @@ public final class CSVParser implements Iterable<CSVRecord>, Closeable {
}
headerRecord = formatHeader;
}
-
// build the name to index mappings
if (headerRecord != null) {
// Track an occurrence of a null, empty or blank header.
@@ -633,18 +629,14 @@ public final class CSVParser implements Iterable<CSVRecord>, Closeable {
final String header = headerRecord[i];
final boolean blankHeader = CSVFormat.isBlank(header);
if (blankHeader && !format.getAllowMissingColumnNames()) {
- throw new IllegalArgumentException(
- "A header name is missing in " + Arrays.toString(headerRecord));
+ throw new IllegalArgumentException("A header name is missing in " + Arrays.toString(headerRecord));
}
-
final boolean containsHeader = blankHeader ? observedMissing : headerMap.containsKey(header);
final DuplicateHeaderMode headerMode = format.getDuplicateHeaderMode();
final boolean duplicatesAllowed = headerMode == DuplicateHeaderMode.ALLOW_ALL;
final boolean emptyDuplicatesAllowed = headerMode == DuplicateHeaderMode.ALLOW_EMPTY;
-
if (containsHeader && !duplicatesAllowed && !(blankHeader && emptyDuplicatesAllowed)) {
- throw new IllegalArgumentException(
- String.format(
+ throw new IllegalArgumentException(String.format(
"The header contains a duplicate name: \"%s\" in %s. If this is valid then use CSVFormat.Builder.setDuplicateHeaderMode().",
header, Arrays.toString(headerRecord)));
}
(commons-csv) 07/07: CSVParser.parse(*) methods with a null Charset maps to Charset.defaultCharset()
Posted by gg...@apache.org.
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 d93c4940f2673a98033457705bc5bf0d989f7f62
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Fri Mar 14 16:29:53 2025 -0400
CSVParser.parse(*) methods with a null Charset maps to
Charset.defaultCharset()
Javadoc
---
src/changes/changes.xml | 1 +
.../java/org/apache/commons/csv/CSVParser.java | 48 ++++++++++++----------
.../java/org/apache/commons/csv/CSVParserTest.java | 14 ++++---
3 files changed, 35 insertions(+), 28 deletions(-)
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 69103432..f801b350 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -49,6 +49,7 @@
<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>
<action type="fix" dev="ggregory" due-to="Gary Gregory">CSVParser.parse(InputStream, 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(*) methods with a null Charset maps to Charset.defaultCharset().</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 8357813b..42a588f0 100644
--- a/src/main/java/org/apache/commons/csv/CSVParser.java
+++ b/src/main/java/org/apache/commons/csv/CSVParser.java
@@ -49,6 +49,7 @@ import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
+import org.apache.commons.io.Charsets;
import org.apache.commons.io.build.AbstractStreamBuilder;
import org.apache.commons.io.function.Uncheck;
@@ -305,15 +306,16 @@ public final class CSVParser implements Iterable<CSVRecord>, Closeable {
* @param file
* a CSV file. Must not be null.
* @param charset
- * The Charset to decode the given file.
+ * The Charset to decode the given file, {@code null} maps to the {@link Charset#defaultCharset() default Charset}.
* @param format
* 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.
+ * If the parameters of the format are inconsistent.
* @throws IOException
* If an I/O error occurs
- * @throws CSVException Thrown on invalid input.
+ * @throws CSVException Thrown on invalid CSV input data.
+ * @throws NullPointerException if {@code file} is {@code null}.
*/
public static CSVParser parse(final File file, final Charset charset, final CSVFormat format) throws IOException {
Objects.requireNonNull(file, "file");
@@ -331,7 +333,7 @@ public final class CSVParser implements Iterable<CSVRecord>, Closeable {
* @param inputStream
* an InputStream containing CSV-formatted input, {@code null} maps to {@link CSVFormat#DEFAULT}.
* @param charset
- * The Charset to decode the given file.
+ * The Charset to decode the given file, {@code null} maps to the {@link Charset#defaultCharset() default Charset}.
* @param format
* the CSVFormat used for CSV parsing, {@code null} maps to {@link CSVFormat#DEFAULT}.
* @return a new CSVParser configured with the given reader and format.
@@ -339,12 +341,12 @@ public final class CSVParser implements Iterable<CSVRecord>, Closeable {
* If the parameters of the format are inconsistent or if either reader or format are null.
* @throws IOException
* If there is a problem reading the header or skipping the first record
- * @throws CSVException Thrown on invalid input.
+ * @throws CSVException Thrown on invalid CSV input data.
* @since 1.5
*/
public static CSVParser parse(final InputStream inputStream, final Charset charset, final CSVFormat format)
throws IOException {
- return parse(new InputStreamReader(inputStream, charset), format);
+ return parse(new InputStreamReader(inputStream, Charsets.toCharset(charset)), format);
}
/**
@@ -353,15 +355,16 @@ public final class CSVParser implements Iterable<CSVRecord>, Closeable {
* @param path
* a CSV file. Must not be null.
* @param charset
- * The Charset to decode the given file.
+ * The Charset to decode the given file, {@code null} maps to the {@link Charset#defaultCharset() default Charset}.
* @param format
* 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.
+ * If the parameters of the format are inconsistent.
* @throws IOException
* If an I/O error occurs
- * @throws CSVException Thrown on invalid input.
+ * @throws CSVException Thrown on invalid CSV input data.
+ * @throws NullPointerException if {@code path} is {@code null}.
* @since 1.5
*/
@SuppressWarnings("resource")
@@ -387,7 +390,7 @@ public final class CSVParser implements Iterable<CSVRecord>, Closeable {
* If the parameters of the format are inconsistent or if either reader or format are null.
* @throws IOException
* If there is a problem reading the header or skipping the first record
- * @throws CSVException Thrown on invalid input.
+ * @throws CSVException Thrown on invalid CSV input data.
* @since 1.5
*/
public static CSVParser parse(final Reader reader, final CSVFormat format) throws IOException {
@@ -403,10 +406,11 @@ public final class CSVParser implements Iterable<CSVRecord>, Closeable {
* 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 string or format are null.
+ * If the parameters of the format are inconsistent.
* @throws IOException
* If an I/O error occurs
- * @throws CSVException Thrown on invalid input.
+ * @throws CSVException Thrown on invalid CSV input data.
+ * @throws NullPointerException if {@code string} is {@code null}.
*/
public static CSVParser parse(final String string, final CSVFormat format) throws IOException {
Objects.requireNonNull(string, "string");
@@ -424,15 +428,16 @@ public final class CSVParser implements Iterable<CSVRecord>, Closeable {
* @param url
* a URL. Must not be null.
* @param charset
- * the charset for the resource. Must not be null.
+ * the charset for the resource, {@code null} maps to the {@link Charset#defaultCharset() default Charset}.
* @param format
* 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 url, charset or format are null.
+ * If the parameters of the format are inconsistent.
* @throws IOException
* If an I/O error occurs
- * @throws CSVException Thrown on invalid input.
+ * @throws CSVException Thrown on invalid CSV input data.
+ * @throws NullPointerException if {@code url} is {@code null}.
*/
@SuppressWarnings("resource")
public static CSVParser parse(final URL url, final Charset charset, final CSVFormat format) throws IOException {
@@ -484,7 +489,7 @@ public final class CSVParser implements Iterable<CSVRecord>, Closeable {
* If the parameters of the format are inconsistent or if either reader or format are null.
* @throws IOException
* If there is a problem reading the header or skipping the first record
- * @throws CSVException Thrown on invalid input.
+ * @throws CSVException Thrown on invalid CSV input data.
* @deprecated Will be removed in the next major version, use {@link Builder#get()}.
*/
@Deprecated
@@ -517,10 +522,9 @@ public final class CSVParser implements Iterable<CSVRecord>, Closeable {
* @deprecated Will be private in the next major version, use {@link Builder#get()}.
*/
@Deprecated
- public CSVParser(final Reader reader, final CSVFormat format, final long characterOffset, final long recordNumber)
- throws IOException {
- this(reader, format, characterOffset, recordNumber, null, false);
- }
+ public CSVParser(final Reader reader, final CSVFormat format, final long characterOffset, final long recordNumber) throws IOException {
+ this(reader, format, characterOffset, recordNumber, null, false);
+ }
/**
* Constructs a new instance using the given {@link CSVFormat}
@@ -546,7 +550,7 @@ public final class CSVParser implements Iterable<CSVRecord>, Closeable {
* If the parameters of the format are inconsistent or if either the reader or format is null.
* @throws IOException
* If there is a problem reading the header or skipping the first record.
- * @throws CSVException Thrown on invalid input.
+ * @throws CSVException Thrown on invalid CSV input data.
*/
private CSVParser(final Reader reader, final CSVFormat format, final long characterOffset, final long recordNumber,
final Charset charset, final boolean trackBytes)
@@ -875,7 +879,7 @@ public final class CSVParser implements Iterable<CSVRecord>, Closeable {
*
* @return the record as an array of values, or {@code null} if the end of the stream has been reached.
* @throws IOException on parse error or input read-failure.
- * @throws CSVException on invalid input.
+ * @throws CSVException on invalid CSV input data.
*/
CSVRecord nextRecord() throws IOException {
CSVRecord result = null;
diff --git a/src/test/java/org/apache/commons/csv/CSVParserTest.java b/src/test/java/org/apache/commons/csv/CSVParserTest.java
index 31ede333..20ab9b65 100644
--- a/src/test/java/org/apache/commons/csv/CSVParserTest.java
+++ b/src/test/java/org/apache/commons/csv/CSVParserTest.java
@@ -1358,8 +1358,7 @@ public class CSVParserTest {
@Test
public void testParse() throws Exception {
- final ClassLoader loader = ClassLoader.getSystemClassLoader();
- final URL url = loader.getResource("org/apache/commons/csv/CSVFileParser/test.csv");
+ final URL url = ClassLoader.getSystemClassLoader().getResource("org/apache/commons/csv/CSVFileParser/test.csv");
final CSVFormat format = CSVFormat.DEFAULT.builder().setHeader("A", "B", "C", "D").get();
final Charset charset = StandardCharsets.UTF_8;
// Reader
@@ -1472,8 +1471,12 @@ public class CSVParserTest {
}
@Test
- public void testParserUrlNullCharsetFormat() {
- assertThrows(NullPointerException.class, () -> CSVParser.parse(new URL("https://commons.apache.org"), null, CSVFormat.DEFAULT));
+ public void testParserUrlNullCharsetFormat() throws IOException {
+ final URL url = ClassLoader.getSystemClassLoader().getResource("org/apache/commons/csv/CSVFileParser/test.csv");
+ try (CSVParser parser = CSVParser.parse(url, null, CSVFormat.DEFAULT)) {
+ // null maps to DEFAULT.
+ parseFully(parser);
+ }
}
@Test
@@ -1492,8 +1495,7 @@ public class CSVParserTest {
@Test
public void testParseUrlCharsetNullFormat() throws IOException {
- final ClassLoader loader = ClassLoader.getSystemClassLoader();
- final URL url = loader.getResource("org/apache/commons/csv/CSVFileParser/test.csv");
+ final URL url = ClassLoader.getSystemClassLoader().getResource("org/apache/commons/csv/CSVFileParser/test.csv");
try (CSVParser parser = CSVParser.parse(url, Charset.defaultCharset(), null)) {
// null maps to DEFAULT.
parseFully(parser);
(commons-csv) 04/07: CSVParser.parse(String, CSVFormat) with a null CSVFormat maps to CSVFormat.DEFAULT (like CSVParser.parse(Reader, CSVFormat))
Posted by gg...@apache.org.
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 931531bcd8e333372f05fc19f670dd5a9ecfd219
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Fri Mar 14 16:01:45 2025 -0400
CSVParser.parse(String, CSVFormat) with a null CSVFormat maps to
CSVFormat.DEFAULT (like CSVParser.parse(Reader, CSVFormat))
---
src/changes/changes.xml | 3 ++-
src/main/java/org/apache/commons/csv/CSVParser.java | 3 +--
src/test/java/org/apache/commons/csv/CSVParserTest.java | 13 +++++++++++--
3 files changed, 14 insertions(+), 5 deletions(-)
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 8b00e71c..bca27da3 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -44,7 +44,8 @@
<!-- FIX -->
<action type="fix" issue="CSV-317" dev="ggregory" due-to="Filipe Roque">Release history link changed from changes-report.html to changes.html #516.</action>
<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" issue="CSV-317" 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(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>
<!-- 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 df25206e..42c44558 100644
--- a/src/main/java/org/apache/commons/csv/CSVParser.java
+++ b/src/main/java/org/apache/commons/csv/CSVParser.java
@@ -403,7 +403,7 @@ public final class CSVParser implements Iterable<CSVRecord>, Closeable {
* @param string
* a CSV string. Must not be null.
* @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 string or format are null.
@@ -413,7 +413,6 @@ public final class CSVParser implements Iterable<CSVRecord>, Closeable {
*/
public static CSVParser parse(final String string, final CSVFormat format) throws IOException {
Objects.requireNonNull(string, "string");
- Objects.requireNonNull(format, "format");
return parse(new StringReader(string), format);
}
diff --git a/src/test/java/org/apache/commons/csv/CSVParserTest.java b/src/test/java/org/apache/commons/csv/CSVParserTest.java
index 6a4fe934..df9f0d8b 100644
--- a/src/test/java/org/apache/commons/csv/CSVParserTest.java
+++ b/src/test/java/org/apache/commons/csv/CSVParserTest.java
@@ -1454,8 +1454,17 @@ public class CSVParserTest {
}
@Test
- public void testParseStringNullFormat() {
- assertThrows(NullPointerException.class, () -> CSVParser.parse("csv data", (CSVFormat) null));
+ public void testParseStringNullFormat() throws IOException {
+ try (CSVParser parser = CSVParser.parse("1,2,3", null)) {
+ // null maps to DEFAULT.
+ final List<CSVRecord> records = parser.getRecords();
+ assertEquals(1, records.size());
+ final CSVRecord record = records.get(0);
+ assertEquals(3, record.size());
+ assertEquals("1", record.get(0));
+ assertEquals("2", record.get(1));
+ assertEquals("3", record.get(2));
+ }
}
@Test
(commons-csv) 05/07: CSVParser.parse(File, Charset, CSVFormat) with a null CSVFormat maps to CSVFormat.DEFAULT (like CSVParser.parse(Reader, CSVFormat))
Posted by gg...@apache.org.
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 <ga...@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());
}
}
(commons-csv) 03/07: CSVParser.parse(URL, Charset, CSVFormat) with a null CSVFormat maps to CSVFormat.DEFAULT (like CSVParser.parse(Reader, CSVFormat))
Posted by gg...@apache.org.
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 41362db16e59cf41c122d63337372657f4bde7b7
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Fri Mar 14 15:55:57 2025 -0400
CSVParser.parse(URL, Charset, CSVFormat) with a null CSVFormat maps to
CSVFormat.DEFAULT (like CSVParser.parse(Reader, CSVFormat))
---
src/changes/changes.xml | 1 +
src/main/java/org/apache/commons/csv/CSVParser.java | 5 ++---
src/test/java/org/apache/commons/csv/CSVParserTest.java | 8 ++++++--
3 files changed, 9 insertions(+), 5 deletions(-)
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index c7deab3b..8b00e71c 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -44,6 +44,7 @@
<!-- FIX -->
<action type="fix" issue="CSV-317" dev="ggregory" due-to="Filipe Roque">Release history link changed from changes-report.html to changes.html #516.</action>
<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" issue="CSV-317" 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>
<!-- 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 6163f2ed..df25206e 100644
--- a/src/main/java/org/apache/commons/csv/CSVParser.java
+++ b/src/main/java/org/apache/commons/csv/CSVParser.java
@@ -346,7 +346,6 @@ public final class CSVParser implements Iterable<CSVRecord>, Closeable {
public static CSVParser parse(final InputStream inputStream, final Charset charset, final CSVFormat format)
throws IOException {
Objects.requireNonNull(inputStream, "inputStream");
- Objects.requireNonNull(format, "format");
return parse(new InputStreamReader(inputStream, charset), format);
}
@@ -385,7 +384,7 @@ public final class CSVParser implements Iterable<CSVRecord>, Closeable {
* @param reader
* a Reader containing CSV-formatted input. Must not be null.
* @param format
- * the CSVFormat used for CSV parsing, {@code null} uses {@link CSVFormat#DEFAULT}.
+ * the CSVFormat used for CSV parsing, {@code null} maps to {@link CSVFormat#DEFAULT}.
* @return a new CSVParser configured with the given reader and format.
* @throws IllegalArgumentException
* If the parameters of the format are inconsistent or if either reader or format are null.
@@ -431,7 +430,7 @@ public final class CSVParser implements Iterable<CSVRecord>, Closeable {
* @param charset
* the charset for the resource. Must not be null.
* @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 url, charset or format are null.
diff --git a/src/test/java/org/apache/commons/csv/CSVParserTest.java b/src/test/java/org/apache/commons/csv/CSVParserTest.java
index f95c0e07..6a4fe934 100644
--- a/src/test/java/org/apache/commons/csv/CSVParserTest.java
+++ b/src/test/java/org/apache/commons/csv/CSVParserTest.java
@@ -1459,8 +1459,12 @@ public class CSVParserTest {
}
@Test
- public void testParseUrlCharsetNullFormat() {
- assertThrows(NullPointerException.class, () -> CSVParser.parse(new URL("https://commons.apache.org"), Charset.defaultCharset(), null));
+ public void testParseUrlCharsetNullFormat() throws IOException {
+ final ClassLoader loader = ClassLoader.getSystemClassLoader();
+ 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.
+ }
}
@Test
(commons-csv) 06/07: CSVParser.parse(InputStream, Charset, CSVFormat) with a null CSVFormat maps to CSVFormat.DEFAULT (like CSVParser.parse(Reader, CSVFormat))
Posted by gg...@apache.org.
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 c36d6cdeabac051bc74c1490263df129e3c0750d
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Fri Mar 14 16:13:42 2025 -0400
CSVParser.parse(InputStream, Charset, CSVFormat) with a null CSVFormat
maps to CSVFormat.DEFAULT (like CSVParser.parse(Reader, CSVFormat))
---
src/changes/changes.xml | 1 +
src/main/java/org/apache/commons/csv/CSVParser.java | 6 ++----
src/test/java/org/apache/commons/csv/CSVParserTest.java | 10 ++++++++++
3 files changed, 13 insertions(+), 4 deletions(-)
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 03dd0a98..69103432 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -48,6 +48,7 @@
<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>
+ <action type="fix" dev="ggregory" due-to="Gary Gregory">CSVParser.parse(InputStream, 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 afda2e4f..8357813b 100644
--- a/src/main/java/org/apache/commons/csv/CSVParser.java
+++ b/src/main/java/org/apache/commons/csv/CSVParser.java
@@ -329,11 +329,11 @@ public final class CSVParser implements Iterable<CSVRecord>, Closeable {
* </p>
*
* @param inputStream
- * an InputStream containing CSV-formatted input. Must not be null.
+ * an InputStream containing CSV-formatted input, {@code null} maps to {@link CSVFormat#DEFAULT}.
* @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 CSVParser configured with the given reader and format.
* @throws IllegalArgumentException
* If the parameters of the format are inconsistent or if either reader or format are null.
@@ -342,10 +342,8 @@ public final class CSVParser implements Iterable<CSVRecord>, Closeable {
* @throws CSVException Thrown on invalid input.
* @since 1.5
*/
- @SuppressWarnings("resource")
public static CSVParser parse(final InputStream inputStream, final Charset charset, final CSVFormat format)
throws IOException {
- Objects.requireNonNull(inputStream, "inputStream");
return parse(new InputStreamReader(inputStream, 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 45287d35..31ede333 100644
--- a/src/test/java/org/apache/commons/csv/CSVParserTest.java
+++ b/src/test/java/org/apache/commons/csv/CSVParserTest.java
@@ -33,6 +33,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
import java.io.File;
import java.io.IOException;
+import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PipedReader;
import java.io.PipedWriter;
@@ -1432,6 +1433,15 @@ public class CSVParserTest {
}
}
+ @Test
+ public void testParseInputStreamCharsetNullFormat() throws IOException {
+ try (InputStream in = Files.newInputStream(Paths.get("src/test/resources/org/apache/commons/csv/CSVFileParser/test.csv"));
+ CSVParser parser = CSVParser.parse(in, Charset.defaultCharset(), null)) {
+ // null maps to DEFAULT.
+ parseFully(parser);
+ }
+ }
+
@Test
public void testParseNullFileFormat() {
assertThrows(NullPointerException.class, () -> CSVParser.parse((File) null, Charset.defaultCharset(), CSVFormat.DEFAULT));