From e295ac3391bd50d1056997e172e1ce39e3ef818e Mon Sep 17 00:00:00 2001 From: kai Date: Sun, 19 Mar 2017 15:29:50 +0100 Subject: [PATCH 1/3] CSV-203: withNullString value is printed without quotes when QuoteMode.ALL is specified --- .../org/apache/commons/csv/CSVFormat.java | 11 ++- .../commons/csv/issues/JiraCsv203Test.java | 68 +++++++++++++++++++ 2 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 src/test/java/org/apache/commons/csv/issues/JiraCsv203Test.java diff --git a/src/main/java/org/apache/commons/csv/CSVFormat.java b/src/main/java/org/apache/commons/csv/CSVFormat.java index 79a0713e61..79f9297f68 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 void print(final Object value, final Appendable out, final boolean newRec // 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(); } 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 0000000000..c093c821be --- /dev/null +++ b/src/test/java/org/apache/commons/csv/issues/JiraCsv203Test.java @@ -0,0 +1,68 @@ +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: withNullString value is printed without quotes when QuoteMode.ALL is specified + */ +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 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 testQuoteModeNoneNumeric() 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()); + } +} From 82b52107fdd41da6e96ca4ed141823cf158fd902 Mon Sep 17 00:00:00 2001 From: kparoth Date: Wed, 22 Mar 2017 11:23:35 +0100 Subject: [PATCH 2/3] JUnit: testWithoutNullString, testWithEmptyValues --- .../commons/csv/issues/JiraCsv203Test.java | 31 ++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/src/test/java/org/apache/commons/csv/issues/JiraCsv203Test.java b/src/test/java/org/apache/commons/csv/issues/JiraCsv203Test.java index c093c821be..79b1966b08 100644 --- a/src/test/java/org/apache/commons/csv/issues/JiraCsv203Test.java +++ b/src/test/java/org/apache/commons/csv/issues/JiraCsv203Test.java @@ -53,7 +53,7 @@ public void testQuoteModeMinimal() throws Exception { } @Test - public void testQuoteModeNoneNumeric() throws Exception { + public void testQuoteModeNonNumeric() throws Exception { CSVFormat format = CSVFormat.EXCEL .withNullString("N/A") .withIgnoreSurroundingSpaces(true) @@ -65,4 +65,33 @@ public void testQuoteModeNoneNumeric() throws Exception { 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()); + } } From aebe0d023329a453e38739149c255693123b3cc8 Mon Sep 17 00:00:00 2001 From: kparoth Date: Mon, 27 Mar 2017 11:06:47 +0200 Subject: [PATCH 3/3] New QuoteMode:ALL_NON_NULL, JUnit: testQuoteModeAllNonNull --- .../java/org/apache/commons/csv/CSVFormat.java | 1 + .../java/org/apache/commons/csv/QuoteMode.java | 5 +++++ .../apache/commons/csv/issues/JiraCsv203Test.java | 14 ++++++++++++++ 3 files changed, 20 insertions(+) diff --git a/src/main/java/org/apache/commons/csv/CSVFormat.java b/src/main/java/org/apache/commons/csv/CSVFormat.java index 79f9297f68..3d3e7e5eb0 100644 --- a/src/main/java/org/apache/commons/csv/CSVFormat.java +++ b/src/main/java/org/apache/commons/csv/CSVFormat.java @@ -1040,6 +1040,7 @@ private void printAndQuote(final Object object, final CharSequence value, final } switch (quoteModePolicy) { case ALL: + case ALL_NON_NULL: quote = true; break; case NON_NUMERIC: diff --git a/src/main/java/org/apache/commons/csv/QuoteMode.java b/src/main/java/org/apache/commons/csv/QuoteMode.java index b0a31c2386..08a9b7251f 100644 --- a/src/main/java/org/apache/commons/csv/QuoteMode.java +++ b/src/main/java/org/apache/commons/csv/QuoteMode.java @@ -28,6 +28,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. diff --git a/src/test/java/org/apache/commons/csv/issues/JiraCsv203Test.java b/src/test/java/org/apache/commons/csv/issues/JiraCsv203Test.java index 79b1966b08..063ba4b317 100644 --- a/src/test/java/org/apache/commons/csv/issues/JiraCsv203Test.java +++ b/src/test/java/org/apache/commons/csv/issues/JiraCsv203Test.java @@ -25,6 +25,20 @@ public void testQuoteModeAll() throws Exception { 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