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