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.QuoteMode;
30  import org.junit.jupiter.api.Test;
31  
32  /**
33   * Tests [CSV-263] Print from Reader with embedded quotes generates incorrect output.
34   */
35  public class JiraCsv263Test {
36  
37      @Test
38      public void testPrintFromReaderWithQuotes() throws IOException {
39          // @formatter:off
40          final CSVFormat format = CSVFormat.RFC4180.builder()
41              .setDelimiter(',')
42              .setQuote('"')
43              .setEscape('?')
44              .setQuoteMode(QuoteMode.NON_NUMERIC)
45              .get();
46          // @formatter:on
47          final StringBuilder out = new StringBuilder();
48  
49          final Reader atStartOnly = new StringReader("\"a,b,c\r\nx,y,z");
50          format.print(atStartOnly, out, true);
51          assertEquals("\"\"\"a,b,c\r\nx,y,z\"", out.toString());
52  
53          final Reader atEndOnly = new StringReader("a,b,c\r\nx,y,z\"");
54          out.setLength(0);
55          format.print(atEndOnly, out, true);
56          assertEquals("\"a,b,c\r\nx,y,z\"\"\"", out.toString());
57  
58          final Reader atBeginEnd = new StringReader("\"a,b,c\r\nx,y,z\"");
59          out.setLength(0);
60          format.print(atBeginEnd, out, true);
61          assertEquals("\"\"\"a,b,c\r\nx,y,z\"\"\"", out.toString());
62  
63          final Reader embeddedBeginMiddle = new StringReader("\"a\",b,c\r\nx,\"y\",z");
64          out.setLength(0);
65          format.print(embeddedBeginMiddle, out, true);
66          assertEquals("\"\"\"a\"\",b,c\r\nx,\"\"y\"\",z\"", out.toString());
67  
68          final Reader embeddedMiddleEnd = new StringReader("a,\"b\",c\r\nx,y,\"z\"");
69          out.setLength(0);
70          format.print(embeddedMiddleEnd, out, true);
71          assertEquals("\"a,\"\"b\"\",c\r\nx,y,\"\"z\"\"\"", out.toString());
72  
73          final Reader nested = new StringReader("a,\"b \"and\" c\",d");
74          out.setLength(0);
75          format.print(nested, out, true);
76          assertEquals("\"a,\"\"b \"\"and\"\" c\"\",d\"", out.toString());
77      }
78  
79  }