1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
33 final CSVFormat printFormat = CSVFormat.DEFAULT.builder()
34 .setDelimiter('\t')
35 .setHeader("ID", "Name", "Country", "Age")
36 .build();
37
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 }