Skip to content

Commit b6c63e4

Browse files
committed
Update test case for cases that are only supported for parsing
1 parent 4ddeddc commit b6c63e4

2 files changed

Lines changed: 44 additions & 23 deletions

File tree

src/main/java/org/apache/commons/csv/CSVFormat.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ public Builder setAllowDuplicateHeaderNames(final boolean allowDuplicateHeaderNa
286286
}
287287

288288
/**
289-
* Sets the missing column names behavior, {@code true} to allow missing column names in the header line, {@code false} to cause an
289+
* Sets the missing column names parser behavior, {@code true} to allow missing column names in the header line, {@code false} to cause an
290290
* {@link IllegalArgumentException} to be thrown.
291291
*
292292
* @param allowMissingColumnNames the missing column names behavior, {@code true} to allow missing column names in the header line, {@code false} to

src/test/java/org/apache/commons/csv/CSVDuplicateHeaderTest.java

Lines changed: 43 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
* The test verifies that headers are consistently handled by CSVFormat and CSVParser.
3232
*/
3333
public class CSVDuplicateHeaderTest {
34+
3435
/**
3536
* Return test cases for duplicate header data. Uses the order:
3637
* <pre>
@@ -39,13 +40,16 @@ public class CSVDuplicateHeaderTest {
3940
* String[] headers
4041
* boolean valid
4142
* </pre>
42-
*
43-
* <p>TODO: Reinstate cases failed by CSVFormat.
43+
* <p>
44+
* TODO: Reinstate cases failed by CSVFormat.
45+
* </p>
4446
*
4547
* @return the stream of arguments
4648
*/
4749
static Stream<Arguments> duplicateHeaderData() {
4850
return Stream.of(
51+
// Commented out data here are for cases that are only supported for parsing.
52+
4953
// Any combination with a valid header
5054
Arguments.of(DuplicateHeaderMode.DISALLOW, false, new String[] {"A", "B"}, true),
5155
Arguments.of(DuplicateHeaderMode.ALLOW_EMPTY, false, new String[] {"A", "B"}, true),
@@ -64,38 +68,55 @@ static Stream<Arguments> duplicateHeaderData() {
6468

6569
// Duplicate empty names
6670
Arguments.of(DuplicateHeaderMode.DISALLOW, false, new String[] {"", ""}, false),
67-
//Arguments.of(DuplicateHeaderMode.ALLOW_EMPTY, false, new String[] {"", ""}, false),
68-
//Arguments.of(DuplicateHeaderMode.ALLOW_ALL, false, new String[] {"", ""}, false),
71+
// Arguments.of(DuplicateHeaderMode.ALLOW_EMPTY, false, new String[] {"", ""}, false),
72+
// Arguments.of(DuplicateHeaderMode.ALLOW_ALL, false, new String[] {"", ""}, false),
6973
Arguments.of(DuplicateHeaderMode.DISALLOW, true, new String[] {"", ""}, false),
7074
Arguments.of(DuplicateHeaderMode.ALLOW_EMPTY, true, new String[] {"", ""}, true),
7175
Arguments.of(DuplicateHeaderMode.ALLOW_ALL, true, new String[] {"", ""}, true),
7276

7377
// Duplicate blank names
7478
Arguments.of(DuplicateHeaderMode.DISALLOW, false, new String[] {" ", " "}, false),
7579
Arguments.of(DuplicateHeaderMode.ALLOW_EMPTY, false, new String[] {" ", " "}, false),
76-
//Arguments.of(DuplicateHeaderMode.ALLOW_ALL, false, new String[] {" ", " "}, false),
80+
// Arguments.of(DuplicateHeaderMode.ALLOW_ALL, false, new String[] {" ", " "}, false),
7781
Arguments.of(DuplicateHeaderMode.DISALLOW, true, new String[] {" ", " "}, false),
78-
//Arguments.of(DuplicateHeaderMode.ALLOW_EMPTY, true, new String[] {" ", " "}, true),
82+
// Arguments.of(DuplicateHeaderMode.ALLOW_EMPTY, true, new String[] {" ", " "}, true),
7983
Arguments.of(DuplicateHeaderMode.ALLOW_ALL, true, new String[] {" ", " "}, true),
8084

8185
// Duplicate non-empty and empty names
8286
Arguments.of(DuplicateHeaderMode.DISALLOW, false, new String[] {"A", "A", "", ""}, false),
8387
Arguments.of(DuplicateHeaderMode.ALLOW_EMPTY, false, new String[] {"A", "A", "", ""}, false),
84-
//Arguments.of(DuplicateHeaderMode.ALLOW_ALL, false, new String[] {"A", "A", "", ""}, false),
88+
// Arguments.of(DuplicateHeaderMode.ALLOW_ALL, false, new String[] {"A", "A", "", ""}, false),
8589
Arguments.of(DuplicateHeaderMode.DISALLOW, true, new String[] {"A", "A", "", ""}, false),
8690
Arguments.of(DuplicateHeaderMode.ALLOW_EMPTY, true, new String[] {"A", "A", "", ""}, false),
8791
Arguments.of(DuplicateHeaderMode.ALLOW_ALL, true, new String[] {"A", "A", "", ""}, true),
8892

8993
// Duplicate non-empty and blank names
9094
Arguments.of(DuplicateHeaderMode.DISALLOW, false, new String[] {"A", "A", " ", " "}, false),
9195
Arguments.of(DuplicateHeaderMode.ALLOW_EMPTY, false, new String[] {"A", "A", " ", " "}, false),
92-
//Arguments.of(DuplicateHeaderMode.ALLOW_ALL, false, new String[] {"A", "A", " ", " "}, false),
96+
// Arguments.of(DuplicateHeaderMode.ALLOW_ALL, false, new String[] {"A", "A", " ", " "}, false),
9397
Arguments.of(DuplicateHeaderMode.DISALLOW, true, new String[] {"A", "A", " ", " "}, false),
9498
Arguments.of(DuplicateHeaderMode.ALLOW_EMPTY, true, new String[] {"A", "A", " ", " "}, false),
9599
Arguments.of(DuplicateHeaderMode.ALLOW_ALL, true, new String[] {"A", "A", " ", " "}, true)
96100
);
97101
}
98102

103+
static Stream<Arguments> duplicateHeaderParseOnlyData() {
104+
return Stream.of(
105+
// Duplicate empty names
106+
Arguments.of(DuplicateHeaderMode.ALLOW_EMPTY, false, new String[] { "", "" }, false),
107+
Arguments.of(DuplicateHeaderMode.ALLOW_ALL, false, new String[] { "", "" }, false),
108+
109+
// Duplicate blank names
110+
Arguments.of(DuplicateHeaderMode.ALLOW_ALL, false, new String[] { " ", " " }, false),
111+
Arguments.of(DuplicateHeaderMode.ALLOW_EMPTY, true, new String[] { " ", " " }, true),
112+
113+
// Duplicate non-empty and empty names
114+
Arguments.of(DuplicateHeaderMode.ALLOW_ALL, false, new String[] { "A", "A", "", "" }, false),
115+
116+
// Duplicate non-empty and blank names
117+
Arguments.of(DuplicateHeaderMode.ALLOW_ALL, false, new String[] { "A", "A", " ", " " }, false));
118+
}
119+
99120
/**
100121
* Test duplicate headers with the CSVFormat.
101122
*
@@ -106,21 +127,21 @@ static Stream<Arguments> duplicateHeaderData() {
106127
*/
107128
@ParameterizedTest
108129
@MethodSource(value = {"duplicateHeaderData"})
109-
public void testCSVFormat(DuplicateHeaderMode duplicateHeaderMode,
110-
boolean allowMissingColumnNames,
111-
String[] headers,
112-
boolean valid) {
113-
CSVFormat.Builder builder = CSVFormat.DEFAULT.builder()
130+
public void testCSVFormat(final DuplicateHeaderMode duplicateHeaderMode,
131+
final boolean allowMissingColumnNames,
132+
final String[] headers,
133+
final boolean valid) {
134+
final CSVFormat.Builder builder = CSVFormat.DEFAULT.builder()
114135
.setDuplicateHeaderMode(duplicateHeaderMode)
115136
.setAllowMissingColumnNames(allowMissingColumnNames)
116137
.setHeader(headers);
117138
if (valid) {
118-
CSVFormat format = builder.build();
139+
final CSVFormat format = builder.build();
119140
Assertions.assertEquals(duplicateHeaderMode, format.getDuplicateHeaderMode(), "DuplicateHeaderMode");
120141
Assertions.assertEquals(allowMissingColumnNames, format.getAllowMissingColumnNames(), "AllowMissingColumnNames");
121142
Assertions.assertArrayEquals(headers, format.getHeader(), "Header");
122143
} else {
123-
Assertions.assertThrows(IllegalArgumentException.class, () -> builder.build());
144+
Assertions.assertThrows(IllegalArgumentException.class, builder::build);
124145
}
125146
}
126147

@@ -134,17 +155,17 @@ public void testCSVFormat(DuplicateHeaderMode duplicateHeaderMode,
134155
* @throws IOException Signals that an I/O exception has occurred.
135156
*/
136157
@ParameterizedTest
137-
@MethodSource(value = {"duplicateHeaderData"})
138-
public void testCSVParser(DuplicateHeaderMode duplicateHeaderMode,
139-
boolean allowMissingColumnNames,
140-
String[] headers,
141-
boolean valid) throws IOException {
142-
CSVFormat format = CSVFormat.DEFAULT.builder()
158+
@MethodSource(value = {"duplicateHeaderData", "duplicateHeaderParseOnlyData"})
159+
public void testCSVParser(final DuplicateHeaderMode duplicateHeaderMode,
160+
final boolean allowMissingColumnNames,
161+
final String[] headers,
162+
final boolean valid) throws IOException {
163+
final CSVFormat format = CSVFormat.DEFAULT.builder()
143164
.setDuplicateHeaderMode(duplicateHeaderMode)
144165
.setAllowMissingColumnNames(allowMissingColumnNames)
145166
.setHeader()
146167
.build();
147-
String input = Arrays.stream(headers).collect(Collectors.joining(format.getDelimiterString()));
168+
final String input = Arrays.stream(headers).collect(Collectors.joining(format.getDelimiterString()));
148169
if (valid) {
149170
try(CSVParser parser = CSVParser.parse(input, format)) {
150171
Assertions.assertEquals(Arrays.asList(headers), parser.getHeaderNames());

0 commit comments

Comments
 (0)