View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   https://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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      // Before fix:
44      // expected: <a,b,c,d,,f> but was: <a,b,c,d,|f>
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      // Before fix:
59      // expected: <a,b,c,d,,f> but was: <a,b|c,d,|f>
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      // Regression, already passed before fix
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      // Before fix:
89      // expected: <a,b,c,d,,f,> but was: <a,b|c,d,|f>
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     // Before fix:
104     // expected: <a,b||c,d,,f> but was: <a,b||c,d,|f>
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     // Regression, already passed before fix
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     // Before fix:
133     // expected: <a,b,c,d,,f> but was: <a,b|c,d,|f>
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     // Regression, already passed before fix
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     // Regression, already passed before fix
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     // Regression, already passed before fix
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     // Regression, already passed before fix
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     // Before fix:
204     // expected: <a,b,c,d,,f,> but was: <a,b,c,d,,f>
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 }