[CSV-203]

withNullString value is printed without quotes when QuoteMode.ALL is
specified; add QuoteMode.ALL_NON_NULL. PR #17.
This commit is contained in:
Gary Gregory 2017-03-27 12:03:53 -07:00
parent a7757849d3
commit 977824491d
4 changed files with 129 additions and 2 deletions

View File

@ -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>

View File

@ -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:

View File

@ -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.

View File

@ -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());
}
}