Skip to content

Commit b6442ce

Browse files
committed
Add JiraCsv294Test
1 parent fcb8374 commit b6442ce

1 file changed

Lines changed: 78 additions & 0 deletions

File tree

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.commons.csv.issues;
19+
20+
import static org.junit.jupiter.api.Assertions.assertEquals;
21+
import static org.junit.jupiter.api.Assertions.assertTrue;
22+
23+
import java.io.ByteArrayInputStream;
24+
import java.io.ByteArrayOutputStream;
25+
import java.io.IOException;
26+
import java.io.InputStreamReader;
27+
import java.io.OutputStreamWriter;
28+
import java.nio.charset.StandardCharsets;
29+
import java.util.List;
30+
31+
import org.apache.commons.csv.CSVFormat;
32+
import org.apache.commons.csv.CSVParser;
33+
import org.apache.commons.csv.CSVPrinter;
34+
import org.apache.commons.csv.CSVRecord;
35+
import org.junit.jupiter.api.Test;
36+
37+
public class JiraCsv294Test {
38+
39+
private static void testInternal(final CSVFormat csvFormat, final String expectedSubstring) throws IOException {
40+
final ByteArrayOutputStream bos = new ByteArrayOutputStream();
41+
try (CSVPrinter printer = new CSVPrinter(new OutputStreamWriter(bos, StandardCharsets.UTF_8), csvFormat)) {
42+
printer.printRecord("a", "b \"\"", "c");
43+
}
44+
final byte[] written = bos.toByteArray();
45+
final String writtenString = new String(written, StandardCharsets.UTF_8);
46+
assertTrue(writtenString.contains(expectedSubstring));
47+
try (CSVParser parser = new CSVParser(new InputStreamReader(new ByteArrayInputStream(written), StandardCharsets.UTF_8), csvFormat)) {
48+
final List<CSVRecord> records = parser.getRecords();
49+
assertEquals(1, records.size());
50+
final CSVRecord record = records.get(0);
51+
assertEquals("a", record.get(0));
52+
assertEquals("b \"\"", record.get(1));
53+
assertEquals("c", record.get(2));
54+
}
55+
}
56+
57+
@Test
58+
public void testDefaultCsvFormatWithBackslashEscapeWorks() throws IOException {
59+
testInternal(CSVFormat.Builder.create().setEscape('\\').build(), ",\"b \\\"\\\"\",");
60+
}
61+
62+
@Test
63+
public void testDefaultCsvFormatWithNullEscapeWorks() throws IOException {
64+
testInternal(CSVFormat.Builder.create().setEscape(null).build(), ",\"b \"\"\"\"\",");
65+
}
66+
67+
@Test
68+
public void testDefaultCsvFormatWithQuoteEscapeWorks() throws IOException {
69+
// this one doesn't actually work but should behave like setEscape(null)
70+
// Printer is writing the expected content but Parser is unable to consume it
71+
testInternal(CSVFormat.Builder.create().setEscape('"').build(), ",\"b \"\"\"\"\",");
72+
}
73+
74+
@Test
75+
public void testDefaultCsvFormatWorks() throws IOException {
76+
testInternal(CSVFormat.Builder.create().build(), ",\"b \"\"\"\"\",");
77+
}
78+
}

0 commit comments

Comments
 (0)