1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.commons.csv.issues;
20
21 import static org.junit.jupiter.api.Assertions.assertEquals;
22
23 import org.apache.commons.csv.CSVFormat;
24 import org.apache.commons.csv.CSVPrinter;
25 import org.apache.commons.csv.QuoteMode;
26 import org.junit.jupiter.api.Test;
27
28
29
30
31
32 public class JiraCsv203Test {
33
34 @Test
35 public void testQuoteModeAll() throws Exception {
36
37 final CSVFormat format = CSVFormat.EXCEL.builder()
38 .setNullString("N/A")
39 .setIgnoreSurroundingSpaces(true)
40 .setQuoteMode(QuoteMode.ALL)
41 .get();
42
43 final StringBuilder buffer = new StringBuilder();
44 try (CSVPrinter printer = new CSVPrinter(buffer, format)) {
45 printer.printRecord(null, "Hello", null, "World");
46 }
47 assertEquals("\"N/A\",\"Hello\",\"N/A\",\"World\"\r\n", buffer.toString());
48 }
49
50 @Test
51 public void testQuoteModeAllNonNull() throws Exception {
52
53 final CSVFormat format = CSVFormat.EXCEL.builder()
54 .setNullString("N/A")
55 .setIgnoreSurroundingSpaces(true)
56 .setQuoteMode(QuoteMode.ALL_NON_NULL)
57 .get();
58
59 final StringBuilder buffer = new StringBuilder();
60 try (CSVPrinter printer = new CSVPrinter(buffer, format)) {
61 printer.printRecord(null, "Hello", null, "World");
62 }
63 assertEquals("N/A,\"Hello\",N/A,\"World\"\r\n", buffer.toString());
64 }
65
66 @Test
67 public void testQuoteModeMinimal() throws Exception {
68
69 final CSVFormat format = CSVFormat.EXCEL.builder()
70 .setNullString("N/A")
71 .setIgnoreSurroundingSpaces(true)
72 .setQuoteMode(QuoteMode.MINIMAL)
73 .get();
74
75 final StringBuilder buffer = new StringBuilder();
76 try (CSVPrinter printer = new CSVPrinter(buffer, format)) {
77 printer.printRecord(null, "Hello", null, "World");
78 }
79 assertEquals("N/A,Hello,N/A,World\r\n", buffer.toString());
80 }
81
82 @Test
83 public void testQuoteModeNonNumeric() throws Exception {
84
85 final CSVFormat format = CSVFormat.EXCEL.builder()
86 .setNullString("N/A")
87 .setIgnoreSurroundingSpaces(true)
88 .setQuoteMode(QuoteMode.NON_NUMERIC)
89 .get();
90
91 final StringBuilder buffer = new StringBuilder();
92 try (CSVPrinter printer = new CSVPrinter(buffer, format)) {
93 printer.printRecord(null, "Hello", null, "World");
94 }
95 assertEquals("N/A,\"Hello\",N/A,\"World\"\r\n", buffer.toString());
96 }
97
98 @Test
99 public void testWithEmptyValues() throws Exception {
100
101 final CSVFormat format = CSVFormat.EXCEL.builder()
102 .setNullString("N/A")
103 .setIgnoreSurroundingSpaces(true)
104 .setQuoteMode(QuoteMode.ALL)
105 .get();
106
107 final StringBuilder buffer = new StringBuilder();
108 try (CSVPrinter printer = new CSVPrinter(buffer, format)) {
109 printer.printRecord("", "Hello", "", "World");
110
111 }
112 assertEquals("\"\",\"Hello\",\"\",\"World\"\r\n", buffer.toString());
113 }
114
115 @Test
116 public void testWithoutNullString() throws Exception {
117
118 final CSVFormat format = CSVFormat.EXCEL.builder()
119
120 .setIgnoreSurroundingSpaces(true)
121 .setQuoteMode(QuoteMode.ALL)
122 .get();
123
124 final StringBuilder buffer = new StringBuilder();
125 try (CSVPrinter printer = new CSVPrinter(buffer, format)) {
126 printer.printRecord(null, "Hello", null, "World");
127 }
128 assertEquals(",\"Hello\",,\"World\"\r\n", buffer.toString());
129 }
130
131 @Test
132 public void testWithoutQuoteMode() throws Exception {
133
134 final CSVFormat format = CSVFormat.EXCEL.builder()
135 .setNullString("N/A")
136 .setIgnoreSurroundingSpaces(true)
137 .get();
138
139 final StringBuilder buffer = new StringBuilder();
140 try (CSVPrinter printer = new CSVPrinter(buffer, format)) {
141 printer.printRecord(null, "Hello", null, "World");
142 }
143 assertEquals("N/A,Hello,N/A,World\r\n", buffer.toString());
144 }
145 }