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.StringReader;
26  import java.io.StringWriter;
27  import java.util.List;
28  
29  import org.apache.commons.csv.CSVFormat;
30  import org.apache.commons.csv.CSVParser;
31  import org.apache.commons.csv.CSVPrinter;
32  import org.apache.commons.csv.CSVRecord;
33  import org.junit.jupiter.api.Test;
34  
35  class JiraCsv249Test {
36  
37      @Test
38      void testJiraCsv249() throws IOException {
39          final CSVFormat format = CSVFormat.DEFAULT.builder().setEscape('\\').get();
40          final StringWriter stringWriter = new StringWriter();
41          try (CSVPrinter printer = new CSVPrinter(stringWriter, format)) {
42              printer.printRecord("foo \\", "bar");
43          }
44          final StringReader reader = new StringReader(stringWriter.toString());
45          final List<CSVRecord> records;
46          try (CSVParser parser = CSVParser.builder().setReader(reader).setFormat(format).get()) {
47              records = parser.getRecords();
48          }
49          records.forEach(record -> {
50              assertEquals("foo \\", record.get(0));
51              assertEquals("bar", record.get(1));
52          });
53  
54      }
55  }