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  package org.apache.commons.csv.issues;
18  
19  import static org.junit.jupiter.api.Assertions.assertEquals;
20  
21  import java.io.IOException;
22  import java.io.StringReader;
23  
24  import org.apache.commons.csv.CSVFormat;
25  import org.apache.commons.csv.CSVParser;
26  import org.apache.commons.csv.CSVRecord;
27  import org.junit.jupiter.api.Test;
28  
29  public class JiraCsv211Test {
30  
31      @Test
32      public void testJiraCsv211Format() throws IOException {
33          final String[] values = {"1", "Jane Doe", "USA", ""};
34  
35          // @formatter:off
36          final CSVFormat printFormat = CSVFormat.DEFAULT.builder()
37              .setDelimiter('\t')
38              .setHeader("ID", "Name", "Country", "Age")
39              .build();
40          // @formatter:on
41          final String formatted = printFormat.format(values);
42          assertEquals("ID\tName\tCountry\tAge\r\n1\tJane Doe\tUSA\t", formatted);
43  
44          final CSVFormat parseFormat = CSVFormat.DEFAULT.builder().setDelimiter('\t').setHeader()
45              .setSkipHeaderRecord(true).build();
46          try (final CSVParser parser = parseFormat.parse(new StringReader(formatted))) {
47              for (final CSVRecord record : parser) {
48                  assertEquals("1", record.get(0));
49                  assertEquals("Jane Doe", record.get(1));
50                  assertEquals("USA", record.get(2));
51                  assertEquals("", record.get(3));
52              }
53          }
54      }
55  }