1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.commons.csv.issues;
21
22 import static org.junit.jupiter.api.Assertions.assertThrows;
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.apache.commons.csv.DuplicateHeaderMode;
30 import org.junit.jupiter.api.Test;
31
32
33
34
35
36
37
38 class JiraCsv264Test {
39
40 private static final String CSV_STRING = "\"\",\"B\",\"\"\n" +
41 "\"1\",\"2\",\"3\"\n" +
42 "\"4\",\"5\",\"6\"";
43
44
45
46
47 private static final String CSV_STRING_GAP = "\"A\",\"B\",\"\",\"\",\"E\"\n" +
48 "\"1\",\"2\",\"\",\"\",\"5\"\n" +
49 "\"6\",\"7\",\"\",\"\",\"10\"";
50
51 @Test
52 void testJiraCsv264() {
53 final CSVFormat csvFormat = CSVFormat.DEFAULT
54 .builder()
55 .setHeader()
56 .setDuplicateHeaderMode(DuplicateHeaderMode.DISALLOW)
57 .setAllowMissingColumnNames(true)
58 .get();
59 try (StringReader reader = new StringReader(CSV_STRING)) {
60 assertThrows(IllegalArgumentException.class, () -> csvFormat.parse(reader));
61 }
62 }
63
64 @Test
65 void testJiraCsv264WithGapAllowEmpty() throws IOException {
66 final CSVFormat csvFormat = CSVFormat.DEFAULT
67 .builder()
68 .setHeader()
69 .setDuplicateHeaderMode(DuplicateHeaderMode.ALLOW_EMPTY)
70 .setAllowMissingColumnNames(true)
71 .get();
72 try (StringReader reader = new StringReader(CSV_STRING_GAP); CSVParser parser = csvFormat.parse(reader)) {
73
74 }
75 }
76
77 @Test
78 void testJiraCsv264WithGapDisallow() {
79 final CSVFormat csvFormat = CSVFormat.DEFAULT
80 .builder()
81 .setHeader()
82 .setDuplicateHeaderMode(DuplicateHeaderMode.DISALLOW)
83 .setAllowMissingColumnNames(true)
84 .get();
85 try (StringReader reader = new StringReader(CSV_STRING_GAP)) {
86 assertThrows(IllegalArgumentException.class, () -> csvFormat.parse(reader));
87 }
88 }
89 }