This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-csv.git
The following commit(s) were added to refs/heads/master by this push:
new b6442ce5 Add JiraCsv294Test
b6442ce5 is described below
commit b6442ce59998f636cf99eb66ef9143d139374e9a
Author: Gary Gregory <[email protected]>
AuthorDate: Sat Sep 14 10:34:52 2024 -0400
Add JiraCsv294Test
---
.../apache/commons/csv/issues/JiraCsv294Test.java | 78 ++++++++++++++++++++++
1 file changed, 78 insertions(+)
diff --git a/src/test/java/org/apache/commons/csv/issues/JiraCsv294Test.java
b/src/test/java/org/apache/commons/csv/issues/JiraCsv294Test.java
new file mode 100644
index 00000000..f01948fa
--- /dev/null
+++ b/src/test/java/org/apache/commons/csv/issues/JiraCsv294Test.java
@@ -0,0 +1,78 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.csv.issues;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.OutputStreamWriter;
+import java.nio.charset.StandardCharsets;
+import java.util.List;
+
+import org.apache.commons.csv.CSVFormat;
+import org.apache.commons.csv.CSVParser;
+import org.apache.commons.csv.CSVPrinter;
+import org.apache.commons.csv.CSVRecord;
+import org.junit.jupiter.api.Test;
+
+public class JiraCsv294Test {
+
+ private static void testInternal(final CSVFormat csvFormat, final String
expectedSubstring) throws IOException {
+ final ByteArrayOutputStream bos = new ByteArrayOutputStream();
+ try (CSVPrinter printer = new CSVPrinter(new OutputStreamWriter(bos,
StandardCharsets.UTF_8), csvFormat)) {
+ printer.printRecord("a", "b \"\"", "c");
+ }
+ final byte[] written = bos.toByteArray();
+ final String writtenString = new String(written,
StandardCharsets.UTF_8);
+ assertTrue(writtenString.contains(expectedSubstring));
+ try (CSVParser parser = new CSVParser(new InputStreamReader(new
ByteArrayInputStream(written), StandardCharsets.UTF_8), csvFormat)) {
+ final List<CSVRecord> records = parser.getRecords();
+ assertEquals(1, records.size());
+ final CSVRecord record = records.get(0);
+ assertEquals("a", record.get(0));
+ assertEquals("b \"\"", record.get(1));
+ assertEquals("c", record.get(2));
+ }
+ }
+
+ @Test
+ public void testDefaultCsvFormatWithBackslashEscapeWorks() throws
IOException {
+ testInternal(CSVFormat.Builder.create().setEscape('\\').build(), ",\"b
\\\"\\\"\",");
+ }
+
+ @Test
+ public void testDefaultCsvFormatWithNullEscapeWorks() throws IOException {
+ testInternal(CSVFormat.Builder.create().setEscape(null).build(), ",\"b
\"\"\"\"\",");
+ }
+
+ @Test
+ public void testDefaultCsvFormatWithQuoteEscapeWorks() throws IOException {
+ // this one doesn't actually work but should behave like
setEscape(null)
+ // Printer is writing the expected content but Parser is unable to
consume it
+ testInternal(CSVFormat.Builder.create().setEscape('"').build(), ",\"b
\"\"\"\"\",");
+ }
+
+ @Test
+ public void testDefaultCsvFormatWorks() throws IOException {
+ testInternal(CSVFormat.Builder.create().build(), ",\"b \"\"\"\"\",");
+ }
+}