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