1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.commons.csv.issues;
21
22 import static org.junit.jupiter.api.Assertions.assertEquals;
23
24 import java.io.IOException;
25 import java.io.Reader;
26 import java.io.StringReader;
27
28 import org.apache.commons.csv.CSVFormat;
29 import org.apache.commons.csv.CSVParser;
30 import org.apache.commons.csv.CSVPrinter;
31 import org.apache.commons.csv.CSVRecord;
32 import org.junit.jupiter.api.Test;
33
34 public class JiraCsv288Test {
35
36 private void print(final CSVRecord csvRecord, final CSVPrinter csvPrinter) throws IOException {
37 for (final String value : csvRecord) {
38 csvPrinter.print(value);
39 }
40 }
41
42 @Test
43
44
45 public void testParseWithABADelimiter() throws Exception {
46 final Reader in = new StringReader("a|~|b|~|c|~|d|~||~|f");
47 final StringBuilder stringBuilder = new StringBuilder();
48 try (CSVPrinter csvPrinter = new CSVPrinter(stringBuilder, CSVFormat.EXCEL);
49 CSVParser parser = CSVParser.parse(in, CSVFormat.Builder.create().setDelimiter("|~|").get())) {
50 for (final CSVRecord csvRecord : parser) {
51 print(csvRecord, csvPrinter);
52 assertEquals("a,b,c,d,,f", stringBuilder.toString());
53 }
54 }
55 }
56
57 @Test
58
59
60 public void testParseWithDoublePipeDelimiter() throws Exception {
61 final Reader in = new StringReader("a||b||c||d||||f");
62 final StringBuilder stringBuilder = new StringBuilder();
63 try (CSVPrinter csvPrinter = new CSVPrinter(stringBuilder, CSVFormat.EXCEL);
64 CSVParser csvParser = CSVParser.parse(in, CSVFormat.Builder.create().setDelimiter("||").get())) {
65 for (final CSVRecord csvRecord : csvParser) {
66 print(csvRecord, csvPrinter);
67 assertEquals("a,b,c,d,,f", stringBuilder.toString());
68 }
69 }
70 }
71
72 @Test
73
74
75 public void testParseWithDoublePipeDelimiterDoubleCharValue() throws Exception {
76 final Reader in = new StringReader("a||bb||cc||dd||f");
77 final StringBuilder stringBuilder = new StringBuilder();
78 try (CSVPrinter csvPrinter = new CSVPrinter(stringBuilder, CSVFormat.EXCEL);
79 CSVParser csvParser = CSVParser.parse(in, CSVFormat.Builder.create().setDelimiter("||").get())) {
80 for (final CSVRecord csvRecord : csvParser) {
81 print(csvRecord, csvPrinter);
82 assertEquals("a,bb,cc,dd,f", stringBuilder.toString());
83 }
84 }
85 }
86
87 @Test
88
89
90 public void testParseWithDoublePipeDelimiterEndsWithDelimiter() throws Exception {
91 final Reader in = new StringReader("a||b||c||d||||f||");
92 final StringBuilder stringBuilder = new StringBuilder();
93 try (CSVPrinter csvPrinter = new CSVPrinter(stringBuilder, CSVFormat.EXCEL);
94 CSVParser csvParser = CSVParser.parse(in, CSVFormat.Builder.create().setDelimiter("||").get())) {
95 for (final CSVRecord csvRecord : csvParser) {
96 print(csvRecord, csvPrinter);
97 assertEquals("a,b,c,d,,f,", stringBuilder.toString());
98 }
99 }
100 }
101
102 @Test
103
104
105 public void testParseWithDoublePipeDelimiterQuoted() throws Exception {
106 final Reader in = new StringReader("a||\"b||c\"||d||||f");
107 final StringBuilder stringBuilder = new StringBuilder();
108 try (CSVPrinter csvPrinter = new CSVPrinter(stringBuilder, CSVFormat.EXCEL);
109 CSVParser csvParser = CSVParser.parse(in, CSVFormat.Builder.create().setDelimiter("||").get())) {
110 for (final CSVRecord csvRecord : csvParser) {
111 print(csvRecord, csvPrinter);
112 assertEquals("a,b||c,d,,f", stringBuilder.toString());
113 }
114 }
115 }
116
117 @Test
118
119 public void testParseWithSinglePipeDelimiterEndsWithDelimiter() throws Exception {
120 final Reader in = new StringReader("a|b|c|d||f|");
121 final StringBuilder stringBuilder = new StringBuilder();
122 try (CSVPrinter csvPrinter = new CSVPrinter(stringBuilder, CSVFormat.EXCEL);
123 CSVParser csvParser = CSVParser.parse(in, CSVFormat.Builder.create().setDelimiter("|").get())) {
124 for (final CSVRecord csvRecord : csvParser) {
125 print(csvRecord, csvPrinter);
126 assertEquals("a,b,c,d,,f,", stringBuilder.toString());
127 }
128 }
129 }
130
131 @Test
132
133
134 public void testParseWithTriplePipeDelimiter() throws Exception {
135 final Reader in = new StringReader("a|||b|||c|||d||||||f");
136 final StringBuilder stringBuilder = new StringBuilder();
137 try (CSVPrinter csvPrinter = new CSVPrinter(stringBuilder, CSVFormat.EXCEL);
138 CSVParser csvParser = CSVParser.parse(in, CSVFormat.Builder.create().setDelimiter("|||").get())) {
139 for (final CSVRecord csvRecord : csvParser) {
140 print(csvRecord, csvPrinter);
141 assertEquals("a,b,c,d,,f", stringBuilder.toString());
142 }
143 }
144 }
145
146 @Test
147
148 public void testParseWithTwoCharDelimiter1() throws Exception {
149 final Reader in = new StringReader("a~|b~|c~|d~|~|f");
150 final StringBuilder stringBuilder = new StringBuilder();
151 try (CSVPrinter csvPrinter = new CSVPrinter(stringBuilder, CSVFormat.EXCEL);
152 CSVParser csvParser = CSVParser.parse(in, CSVFormat.Builder.create().setDelimiter("~|").get())) {
153 for (final CSVRecord csvRecord : csvParser) {
154 print(csvRecord, csvPrinter);
155 assertEquals("a,b,c,d,,f", stringBuilder.toString());
156 }
157 }
158 }
159
160 @Test
161
162 public void testParseWithTwoCharDelimiter2() throws Exception {
163 final Reader in = new StringReader("a~|b~|c~|d~|~|f~");
164 final StringBuilder stringBuilder = new StringBuilder();
165 try (CSVPrinter csvPrinter = new CSVPrinter(stringBuilder, CSVFormat.EXCEL);
166 CSVParser csvParser = CSVParser.parse(in, CSVFormat.Builder.create().setDelimiter("~|").get())) {
167 for (final CSVRecord csvRecord : csvParser) {
168 print(csvRecord, csvPrinter);
169 assertEquals("a,b,c,d,,f~", stringBuilder.toString());
170 }
171 }
172 }
173
174 @Test
175
176 public void testParseWithTwoCharDelimiter3() throws Exception {
177 final Reader in = new StringReader("a~|b~|c~|d~|~|f|");
178 final StringBuilder stringBuilder = new StringBuilder();
179 try (CSVPrinter csvPrinter = new CSVPrinter(stringBuilder, CSVFormat.EXCEL);
180 CSVParser csvParser = CSVParser.parse(in, CSVFormat.Builder.create().setDelimiter("~|").get())) {
181 for (final CSVRecord csvRecord : csvParser) {
182 print(csvRecord, csvPrinter);
183 assertEquals("a,b,c,d,,f|", stringBuilder.toString());
184 }
185 }
186 }
187
188 @Test
189
190 public void testParseWithTwoCharDelimiter4() throws Exception {
191 final Reader in = new StringReader("a~|b~|c~|d~|~|f~~||g");
192 final StringBuilder stringBuilder = new StringBuilder();
193 try (CSVPrinter csvPrinter = new CSVPrinter(stringBuilder, CSVFormat.EXCEL);
194 CSVParser csvParser = CSVParser.parse(in, CSVFormat.Builder.create().setDelimiter("~|").get())) {
195 for (final CSVRecord csvRecord : csvParser) {
196 print(csvRecord, csvPrinter);
197 assertEquals("a,b,c,d,,f~,|g", stringBuilder.toString());
198 }
199 }
200 }
201
202 @Test
203
204
205 public void testParseWithTwoCharDelimiterEndsWithDelimiter() throws Exception {
206 final Reader in = new StringReader("a~|b~|c~|d~|~|f~|");
207 final StringBuilder stringBuilder = new StringBuilder();
208 try (CSVPrinter csvPrinter = new CSVPrinter(stringBuilder, CSVFormat.EXCEL);
209 CSVParser csvParser = CSVParser.parse(in, CSVFormat.Builder.create().setDelimiter("~|").get())) {
210 for (final CSVRecord csvRecord : csvParser) {
211 print(csvRecord, csvPrinter);
212 assertEquals("a,b,c,d,,f,", stringBuilder.toString());
213 }
214 }
215 }
216 }