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