Skip to content

Commit e295ac3

Browse files
author
kai
committed
CSV-203: withNullString value is printed without quotes when QuoteMode.ALL is specified
1 parent a775784 commit e295ac3

2 files changed

Lines changed: 78 additions & 1 deletion

File tree

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -946,7 +946,16 @@ public void print(final Object value, final Appendable out, final boolean newRec
946946
// Only call CharSequence.toString() if you have to, helps GC-free use cases.
947947
CharSequence charSequence;
948948
if (value == null) {
949-
charSequence = nullString == null ? Constants.EMPTY : nullString;
949+
// https://issues.apache.org/jira/browse/CSV-203
950+
if (null == nullString) {
951+
charSequence = Constants.EMPTY;
952+
} else {
953+
if (QuoteMode.ALL == quoteMode) {
954+
charSequence = quoteCharacter + nullString + quoteCharacter;
955+
} else {
956+
charSequence = nullString;
957+
}
958+
}
950959
} else {
951960
charSequence = value instanceof CharSequence ? (CharSequence) value : value.toString();
952961
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package org.apache.commons.csv.issues;
2+
3+
import org.apache.commons.csv.CSVFormat;
4+
import org.apache.commons.csv.CSVPrinter;
5+
import org.apache.commons.csv.QuoteMode;
6+
import org.junit.Assert;
7+
import org.junit.Test;
8+
9+
/**
10+
* JIRA: <a href="https://issues.apache.org/jira/browse/CSV-203">withNullString value is printed without quotes when QuoteMode.ALL is specified</a>
11+
*/
12+
public class JiraCsv203Test {
13+
14+
@Test
15+
public void testQuoteModeAll() throws Exception {
16+
CSVFormat format = CSVFormat.EXCEL
17+
.withNullString("N/A")
18+
.withIgnoreSurroundingSpaces(true)
19+
.withQuoteMode(QuoteMode.ALL);
20+
21+
StringBuffer buffer = new StringBuffer();
22+
CSVPrinter printer = new CSVPrinter(buffer, format);
23+
printer.printRecord(new Object[] { null, "Hello", null, "World" });
24+
25+
Assert.assertEquals("\"N/A\",\"Hello\",\"N/A\",\"World\"\r\n", buffer.toString());
26+
}
27+
28+
@Test
29+
public void testWithoutQuoteMode() throws Exception {
30+
CSVFormat format = CSVFormat.EXCEL
31+
.withNullString("N/A")
32+
.withIgnoreSurroundingSpaces(true);
33+
34+
StringBuffer buffer = new StringBuffer();
35+
CSVPrinter printer = new CSVPrinter(buffer, format);
36+
printer.printRecord(new Object[] { null, "Hello", null, "World" });
37+
38+
Assert.assertEquals("N/A,Hello,N/A,World\r\n", buffer.toString());
39+
}
40+
41+
@Test
42+
public void testQuoteModeMinimal() throws Exception {
43+
CSVFormat format = CSVFormat.EXCEL
44+
.withNullString("N/A")
45+
.withIgnoreSurroundingSpaces(true)
46+
.withQuoteMode(QuoteMode.MINIMAL);
47+
48+
StringBuffer buffer = new StringBuffer();
49+
CSVPrinter printer = new CSVPrinter(buffer, format);
50+
printer.printRecord(new Object[] { null, "Hello", null, "World" });
51+
52+
Assert.assertEquals("N/A,Hello,N/A,World\r\n", buffer.toString());
53+
}
54+
55+
@Test
56+
public void testQuoteModeNoneNumeric() throws Exception {
57+
CSVFormat format = CSVFormat.EXCEL
58+
.withNullString("N/A")
59+
.withIgnoreSurroundingSpaces(true)
60+
.withQuoteMode(QuoteMode.NON_NUMERIC);
61+
62+
StringBuffer buffer = new StringBuffer();
63+
CSVPrinter printer = new CSVPrinter(buffer, format);
64+
printer.printRecord(new Object[] { null, "Hello", null, "World" });
65+
66+
Assert.assertEquals("N/A,\"Hello\",N/A,\"World\"\r\n", buffer.toString());
67+
}
68+
}

0 commit comments

Comments
 (0)