Skip to content

Commit aebe0d0

Browse files
committed
New QuoteMode:ALL_NON_NULL, JUnit: testQuoteModeAllNonNull
1 parent 82b5210 commit aebe0d0

3 files changed

Lines changed: 20 additions & 0 deletions

File tree

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1040,6 +1040,7 @@ private void printAndQuote(final Object object, final CharSequence value, final
10401040
}
10411041
switch (quoteModePolicy) {
10421042
case ALL:
1043+
case ALL_NON_NULL:
10431044
quote = true;
10441045
break;
10451046
case NON_NUMERIC:

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ public enum QuoteMode {
2828
*/
2929
ALL,
3030

31+
/**
32+
* Quotes all non-null fields.
33+
*/
34+
ALL_NON_NULL,
35+
3136
/**
3237
* Quotes fields which contain special characters such as a delimiter, quotes character or any of the characters in
3338
* line separator.

src/test/java/org/apache/commons/csv/issues/JiraCsv203Test.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,20 @@ public void testQuoteModeAll() throws Exception {
2525
Assert.assertEquals("\"N/A\",\"Hello\",\"N/A\",\"World\"\r\n", buffer.toString());
2626
}
2727

28+
@Test
29+
public void testQuoteModeAllNonNull() throws Exception {
30+
CSVFormat format = CSVFormat.EXCEL
31+
.withNullString("N/A")
32+
.withIgnoreSurroundingSpaces(true)
33+
.withQuoteMode(QuoteMode.ALL_NON_NULL);
34+
35+
StringBuffer buffer = new StringBuffer();
36+
CSVPrinter printer = new CSVPrinter(buffer, format);
37+
printer.printRecord(new Object[] { null, "Hello", null, "World" });
38+
39+
Assert.assertEquals("N/A,\"Hello\",N/A,\"World\"\r\n", buffer.toString());
40+
}
41+
2842
@Test
2943
public void testWithoutQuoteMode() throws Exception {
3044
CSVFormat format = CSVFormat.EXCEL

0 commit comments

Comments
 (0)