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.assertArrayEquals;
21  import static org.junit.jupiter.api.Assertions.assertEquals;
22  
23  import java.io.InputStreamReader;
24  import java.io.StringReader;
25  import java.io.StringWriter;
26  import java.util.ArrayList;
27  import java.util.Iterator;
28  import java.util.List;
29  import java.util.stream.Collectors;
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  // psql (14.5 (Homebrew))
38  //
39  // create table COMMONS_CSV_PSQL_TEST (ID INTEGER, COL1 VARCHAR, COL2 VARCHAR, COL3 VARCHAR, COL4 VARCHAR);
40  // insert into COMMONS_CSV_PSQL_TEST select 1, 'abc', 'test line 1' || chr(10) || 'test line 2', null, '';
41  // insert into COMMONS_CSV_PSQL_TEST select 2, 'xyz', '\b:' || chr(8) || ' \t:' || chr(9) || ' \n:' || chr(10) || ' \r:' || chr(13), 'a', 'b';
42  // insert into COMMONS_CSV_PSQL_TEST values (3, 'a', 'b,c,d', '"quoted"', 'e');
43  // copy COMMONS_CSV_PSQL_TEST TO '/tmp/psql.csv' WITH (FORMAT CSV);
44  // copy COMMONS_CSV_PSQL_TEST TO '/tmp/psql.tsv';
45  //
46  // cat /tmp/psql.csv
47  // 1,abc,"test line 1
48  // test line 2",,""
49  // 2,xyz,"\b:^H \t:         \n:
50  //  \r:^M",a,b
51  // 3,a,"b,c,d","""quoted""",e
52  //
53  // cat /tmp/psql.tsv
54  // 1    abc    test line 1\ntest line 2                  \N
55  // 2    xyz    \\b:\b \\t:\t \\n:\n \\r:\r    a          b
56  // 3    a      b,c,d                         "quoted"    e
57  //
58  public class JiraCsv290Test {
59  
60      private void testHelper(final String filename, final CSVFormat format) throws Exception {
61          List<List<String>> content = new ArrayList<>();
62          try (CSVParser csvParser = CSVParser.parse(new InputStreamReader(this.getClass().getResourceAsStream("/org/apache/commons/csv/CSV-290/" + filename)),
63                  format)) {
64              content = csvParser.stream().collect(Collectors.mapping(CSVRecord::toList, Collectors.toList()));
65          }
66  
67          assertEquals(3, content.size());
68  
69          assertEquals("1", content.get(0).get(0));
70          assertEquals("abc", content.get(0).get(1));
71          assertEquals("test line 1\ntest line 2", content.get(0).get(2)); // new line
72          assertEquals(null, content.get(0).get(3)); // null
73          assertEquals("", content.get(0).get(4));
74  
75          assertEquals("2", content.get(1).get(0));
76          assertEquals("\\b:\b \\t:\t \\n:\n \\r:\r", content.get(1).get(2)); // \b, \t, \n, \r
77  
78          assertEquals("3", content.get(2).get(0));
79          assertEquals("b,c,d", content.get(2).get(2)); // value has comma
80          assertEquals("\"quoted\"", content.get(2).get(3)); // quoted
81      }
82  
83      @Test
84      public void testPostgresqlCsv() throws Exception {
85          testHelper("psql.csv", CSVFormat.POSTGRESQL_CSV);
86      }
87  
88      @Test
89      public void testPostgresqlText() throws Exception {
90          testHelper("psql.tsv", CSVFormat.POSTGRESQL_TEXT);
91      }
92  
93      @Test
94      public void testWriteThenRead() throws Exception {
95          final StringWriter sw = new StringWriter();
96  
97          try (CSVPrinter printer = new CSVPrinter(sw, CSVFormat.POSTGRESQL_CSV.builder().setHeader().setSkipHeaderRecord(true).build())) {
98  
99              printer.printRecord("column1", "column2");
100             printer.printRecord("v11", "v12");
101             printer.printRecord("v21", "v22");
102             printer.close();
103 
104             final CSVParser parser = new CSVParser(new StringReader(sw.toString()),
105                     CSVFormat.POSTGRESQL_CSV.builder().setHeader().setSkipHeaderRecord(true).build());
106 
107             assertArrayEquals(new Object[] { "column1", "column2" }, parser.getHeaderNames().toArray());
108 
109             final Iterator<CSVRecord> i = parser.iterator();
110             assertArrayEquals(new String[] { "v11", "v12" }, i.next().toList().toArray());
111             assertArrayEquals(new String[] { "v21", "v22" }, i.next().toList().toArray());
112         }
113     }
114 }