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 2017/03/27 19:03:55 UTC
commons-csv git commit: [CSV-203]
Repository: commons-csv
Updated Branches:
refs/heads/master a7757849d -> 977824491
[CSV-203]
withNullString value is printed without quotes when QuoteMode.ALL is
specified; add QuoteMode.ALL_NON_NULL. PR #17.
Project: http://git-wip-us.apache.org/repos/asf/commons-csv/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-csv/commit/97782449
Tree: http://git-wip-us.apache.org/repos/asf/commons-csv/tree/97782449
Diff: http://git-wip-us.apache.org/repos/asf/commons-csv/diff/97782449
Branch: refs/heads/master
Commit: 977824491d8d65b9952005573f2ebffe6ced671d
Parents: a775784
Author: Gary Gregory <gg...@apache.org>
Authored: Mon Mar 27 12:03:53 2017 -0700
Committer: Gary Gregory <gg...@apache.org>
Committed: Mon Mar 27 12:03:53 2017 -0700
----------------------------------------------------------------------
src/changes/changes.xml | 3 +-
.../java/org/apache/commons/csv/CSVFormat.java | 12 +-
.../java/org/apache/commons/csv/QuoteMode.java | 5 +
.../commons/csv/issues/JiraCsv203Test.java | 111 +++++++++++++++++++
4 files changed, 129 insertions(+), 2 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/commons-csv/blob/97782449/src/changes/changes.xml
----------------------------------------------------------------------
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index b91a71a..557e4df 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -38,7 +38,8 @@
<title>Release Notes</title>
</properties>
<body>
- <release version="1.5" date="2016-MM-DD" description="Bug fix release">
+ <release version="1.5" date="2017-MM-DD" description="Bug fix release">
+ <action issue="CSV-203" type="fix" dev="ggregory" due-to="Richard Wheeldon, Kai Paroth">withNullString value is printed without quotes when QuoteMode.ALL is specified; add QuoteMode.ALL_NON_NULL. PR #17.</action>
<action issue="CSV-194" type="fix" dev="ggregory" due-to="Marc Prud'hommeaux">Fix outdated comments about FileReader in CSVParser #13</action>
<action issue="CSV-193" type="fix" dev="ggregory" due-to="Matthias Wiehl">Fix incorrect method name 'withFirstRowAsHeader' in user guide.</action>
<action issue="CSV-171" type="fix" dev="ggregory" due-to="Gary Gregory, Michael Graessle, Adrian Bridgett">Negative numeric values in the first column are always quoted in minimal mode.</action>
http://git-wip-us.apache.org/repos/asf/commons-csv/blob/97782449/src/main/java/org/apache/commons/csv/CSVFormat.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/csv/CSVFormat.java b/src/main/java/org/apache/commons/csv/CSVFormat.java
index 79a0713..3d3e7e5 100644
--- a/src/main/java/org/apache/commons/csv/CSVFormat.java
+++ b/src/main/java/org/apache/commons/csv/CSVFormat.java
@@ -946,7 +946,16 @@ public final class CSVFormat implements Serializable {
// Only call CharSequence.toString() if you have to, helps GC-free use cases.
CharSequence charSequence;
if (value == null) {
- charSequence = nullString == null ? Constants.EMPTY : nullString;
+ // https://issues.apache.org/jira/browse/CSV-203
+ if (null == nullString) {
+ charSequence = Constants.EMPTY;
+ } else {
+ if (QuoteMode.ALL == quoteMode) {
+ charSequence = quoteCharacter + nullString + quoteCharacter;
+ } else {
+ charSequence = nullString;
+ }
+ }
} else {
charSequence = value instanceof CharSequence ? (CharSequence) value : value.toString();
}
@@ -1031,6 +1040,7 @@ public final class CSVFormat implements Serializable {
}
switch (quoteModePolicy) {
case ALL:
+ case ALL_NON_NULL:
quote = true;
break;
case NON_NUMERIC:
http://git-wip-us.apache.org/repos/asf/commons-csv/blob/97782449/src/main/java/org/apache/commons/csv/QuoteMode.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/csv/QuoteMode.java b/src/main/java/org/apache/commons/csv/QuoteMode.java
index b0a31c2..08a9b72 100644
--- a/src/main/java/org/apache/commons/csv/QuoteMode.java
+++ b/src/main/java/org/apache/commons/csv/QuoteMode.java
@@ -29,6 +29,11 @@ public enum QuoteMode {
ALL,
/**
+ * Quotes all non-null fields.
+ */
+ ALL_NON_NULL,
+
+ /**
* Quotes fields which contain special characters such as a delimiter, quotes character or any of the characters in
* line separator.
*/
http://git-wip-us.apache.org/repos/asf/commons-csv/blob/97782449/src/test/java/org/apache/commons/csv/issues/JiraCsv203Test.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/csv/issues/JiraCsv203Test.java b/src/test/java/org/apache/commons/csv/issues/JiraCsv203Test.java
new file mode 100644
index 0000000..1e3c067
--- /dev/null
+++ b/src/test/java/org/apache/commons/csv/issues/JiraCsv203Test.java
@@ -0,0 +1,111 @@
+package org.apache.commons.csv.issues;
+
+import org.apache.commons.csv.CSVFormat;
+import org.apache.commons.csv.CSVPrinter;
+import org.apache.commons.csv.QuoteMode;
+import org.junit.Assert;
+import org.junit.Test;
+
+/**
+ * JIRA: <a href="https://issues.apache.org/jira/browse/CSV-203">withNullString value is printed without quotes when QuoteMode.ALL is specified</a>
+ */
+public class JiraCsv203Test {
+
+ @Test
+ public void testQuoteModeAll() throws Exception {
+ CSVFormat format = CSVFormat.EXCEL
+ .withNullString("N/A")
+ .withIgnoreSurroundingSpaces(true)
+ .withQuoteMode(QuoteMode.ALL);
+
+ StringBuffer buffer = new StringBuffer();
+ CSVPrinter printer = new CSVPrinter(buffer, format);
+ printer.printRecord(new Object[] { null, "Hello", null, "World" });
+
+ Assert.assertEquals("\"N/A\",\"Hello\",\"N/A\",\"World\"\r\n", buffer.toString());
+ }
+
+ @Test
+ public void testQuoteModeAllNonNull() throws Exception {
+ CSVFormat format = CSVFormat.EXCEL
+ .withNullString("N/A")
+ .withIgnoreSurroundingSpaces(true)
+ .withQuoteMode(QuoteMode.ALL_NON_NULL);
+
+ StringBuffer buffer = new StringBuffer();
+ CSVPrinter printer = new CSVPrinter(buffer, format);
+ printer.printRecord(new Object[] { null, "Hello", null, "World" });
+
+ Assert.assertEquals("N/A,\"Hello\",N/A,\"World\"\r\n", buffer.toString());
+ }
+
+ @Test
+ public void testWithoutQuoteMode() throws Exception {
+ CSVFormat format = CSVFormat.EXCEL
+ .withNullString("N/A")
+ .withIgnoreSurroundingSpaces(true);
+
+ StringBuffer buffer = new StringBuffer();
+ CSVPrinter printer = new CSVPrinter(buffer, format);
+ printer.printRecord(new Object[] { null, "Hello", null, "World" });
+
+ Assert.assertEquals("N/A,Hello,N/A,World\r\n", buffer.toString());
+ }
+
+ @Test
+ public void testQuoteModeMinimal() throws Exception {
+ CSVFormat format = CSVFormat.EXCEL
+ .withNullString("N/A")
+ .withIgnoreSurroundingSpaces(true)
+ .withQuoteMode(QuoteMode.MINIMAL);
+
+ StringBuffer buffer = new StringBuffer();
+ CSVPrinter printer = new CSVPrinter(buffer, format);
+ printer.printRecord(new Object[] { null, "Hello", null, "World" });
+
+ Assert.assertEquals("N/A,Hello,N/A,World\r\n", buffer.toString());
+ }
+
+ @Test
+ public void testQuoteModeNonNumeric() throws Exception {
+ CSVFormat format = CSVFormat.EXCEL
+ .withNullString("N/A")
+ .withIgnoreSurroundingSpaces(true)
+ .withQuoteMode(QuoteMode.NON_NUMERIC);
+
+ StringBuffer buffer = new StringBuffer();
+ CSVPrinter printer = new CSVPrinter(buffer, format);
+ printer.printRecord(new Object[] { null, "Hello", null, "World" });
+
+ Assert.assertEquals("N/A,\"Hello\",N/A,\"World\"\r\n", buffer.toString());
+ }
+
+ @Test
+ public void testWithoutNullString() throws Exception {
+ CSVFormat format = CSVFormat.EXCEL
+ //.withNullString("N/A")
+ .withIgnoreSurroundingSpaces(true)
+ .withQuoteMode(QuoteMode.ALL);
+
+ StringBuffer buffer = new StringBuffer();
+ CSVPrinter printer = new CSVPrinter(buffer, format);
+ printer.printRecord(new Object[] { null, "Hello", null, "World" });
+
+ Assert.assertEquals(",\"Hello\",,\"World\"\r\n", buffer.toString());
+ }
+
+ @Test
+ public void testWithEmptyValues() throws Exception {
+ CSVFormat format = CSVFormat.EXCEL
+ .withNullString("N/A")
+ .withIgnoreSurroundingSpaces(true)
+ .withQuoteMode(QuoteMode.ALL);
+
+ StringBuffer buffer = new StringBuffer();
+ CSVPrinter printer = new CSVPrinter(buffer, format);
+ printer.printRecord(new Object[] { "", "Hello", "", "World" });
+ //printer.printRecord(new Object[] { null, "Hello", null, "World" });
+
+ Assert.assertEquals("\"\",\"Hello\",\"\",\"World\"\r\n", buffer.toString());
+ }
+}
\ No newline at end of file