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