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