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