Update test case for cases that are only supported for parsing

This commit is contained in:
Gary Gregory 2022-10-22 15:04:12 -04:00
parent 4ddeddc2a1
commit b6c63e47d9
2 changed files with 44 additions and 23 deletions

View File

@ -286,7 +286,7 @@ public final class CSVFormat implements Serializable {
} }
/** /**
* Sets the missing column names behavior, {@code true} to allow missing column names in the header line, {@code false} to cause an * Sets the missing column names parser behavior, {@code true} to allow missing column names in the header line, {@code false} to cause an
* {@link IllegalArgumentException} to be thrown. * {@link IllegalArgumentException} to be thrown.
* *
* @param allowMissingColumnNames the missing column names behavior, {@code true} to allow missing column names in the header line, {@code false} to * @param allowMissingColumnNames the missing column names behavior, {@code true} to allow missing column names in the header line, {@code false} to

View File

@ -31,6 +31,7 @@ import org.junit.jupiter.params.provider.MethodSource;
* The test verifies that headers are consistently handled by CSVFormat and CSVParser. * The test verifies that headers are consistently handled by CSVFormat and CSVParser.
*/ */
public class CSVDuplicateHeaderTest { public class CSVDuplicateHeaderTest {
/** /**
* Return test cases for duplicate header data. Uses the order: * Return test cases for duplicate header data. Uses the order:
* <pre> * <pre>
@ -39,13 +40,16 @@ public class CSVDuplicateHeaderTest {
* String[] headers * String[] headers
* boolean valid * boolean valid
* </pre> * </pre>
* * <p>
* <p>TODO: Reinstate cases failed by CSVFormat. * TODO: Reinstate cases failed by CSVFormat.
* </p>
* *
* @return the stream of arguments * @return the stream of arguments
*/ */
static Stream<Arguments> duplicateHeaderData() { static Stream<Arguments> duplicateHeaderData() {
return Stream.of( return Stream.of(
// Commented out data here are for cases that are only supported for parsing.
// Any combination with a valid header // Any combination with a valid header
Arguments.of(DuplicateHeaderMode.DISALLOW, false, new String[] {"A", "B"}, true), Arguments.of(DuplicateHeaderMode.DISALLOW, false, new String[] {"A", "B"}, true),
Arguments.of(DuplicateHeaderMode.ALLOW_EMPTY, false, new String[] {"A", "B"}, true), Arguments.of(DuplicateHeaderMode.ALLOW_EMPTY, false, new String[] {"A", "B"}, true),
@ -64,8 +68,8 @@ public class CSVDuplicateHeaderTest {
// Duplicate empty names // Duplicate empty names
Arguments.of(DuplicateHeaderMode.DISALLOW, false, new String[] {"", ""}, false), Arguments.of(DuplicateHeaderMode.DISALLOW, false, new String[] {"", ""}, false),
//Arguments.of(DuplicateHeaderMode.ALLOW_EMPTY, false, new String[] {"", ""}, false), // Arguments.of(DuplicateHeaderMode.ALLOW_EMPTY, false, new String[] {"", ""}, false),
//Arguments.of(DuplicateHeaderMode.ALLOW_ALL, false, new String[] {"", ""}, false), // Arguments.of(DuplicateHeaderMode.ALLOW_ALL, false, new String[] {"", ""}, false),
Arguments.of(DuplicateHeaderMode.DISALLOW, true, new String[] {"", ""}, false), Arguments.of(DuplicateHeaderMode.DISALLOW, true, new String[] {"", ""}, false),
Arguments.of(DuplicateHeaderMode.ALLOW_EMPTY, true, new String[] {"", ""}, true), Arguments.of(DuplicateHeaderMode.ALLOW_EMPTY, true, new String[] {"", ""}, true),
Arguments.of(DuplicateHeaderMode.ALLOW_ALL, true, new String[] {"", ""}, true), Arguments.of(DuplicateHeaderMode.ALLOW_ALL, true, new String[] {"", ""}, true),
@ -73,15 +77,15 @@ public class CSVDuplicateHeaderTest {
// Duplicate blank names // Duplicate blank names
Arguments.of(DuplicateHeaderMode.DISALLOW, false, new String[] {" ", " "}, false), Arguments.of(DuplicateHeaderMode.DISALLOW, false, new String[] {" ", " "}, false),
Arguments.of(DuplicateHeaderMode.ALLOW_EMPTY, false, new String[] {" ", " "}, false), Arguments.of(DuplicateHeaderMode.ALLOW_EMPTY, false, new String[] {" ", " "}, false),
//Arguments.of(DuplicateHeaderMode.ALLOW_ALL, false, new String[] {" ", " "}, false), // Arguments.of(DuplicateHeaderMode.ALLOW_ALL, false, new String[] {" ", " "}, false),
Arguments.of(DuplicateHeaderMode.DISALLOW, true, new String[] {" ", " "}, false), Arguments.of(DuplicateHeaderMode.DISALLOW, true, new String[] {" ", " "}, false),
//Arguments.of(DuplicateHeaderMode.ALLOW_EMPTY, true, new String[] {" ", " "}, true), // Arguments.of(DuplicateHeaderMode.ALLOW_EMPTY, true, new String[] {" ", " "}, true),
Arguments.of(DuplicateHeaderMode.ALLOW_ALL, true, new String[] {" ", " "}, true), Arguments.of(DuplicateHeaderMode.ALLOW_ALL, true, new String[] {" ", " "}, true),
// Duplicate non-empty and empty names // Duplicate non-empty and empty names
Arguments.of(DuplicateHeaderMode.DISALLOW, false, new String[] {"A", "A", "", ""}, false), Arguments.of(DuplicateHeaderMode.DISALLOW, false, new String[] {"A", "A", "", ""}, false),
Arguments.of(DuplicateHeaderMode.ALLOW_EMPTY, false, new String[] {"A", "A", "", ""}, false), Arguments.of(DuplicateHeaderMode.ALLOW_EMPTY, false, new String[] {"A", "A", "", ""}, false),
//Arguments.of(DuplicateHeaderMode.ALLOW_ALL, false, new String[] {"A", "A", "", ""}, false), // Arguments.of(DuplicateHeaderMode.ALLOW_ALL, false, new String[] {"A", "A", "", ""}, false),
Arguments.of(DuplicateHeaderMode.DISALLOW, true, new String[] {"A", "A", "", ""}, false), Arguments.of(DuplicateHeaderMode.DISALLOW, true, new String[] {"A", "A", "", ""}, false),
Arguments.of(DuplicateHeaderMode.ALLOW_EMPTY, true, new String[] {"A", "A", "", ""}, false), Arguments.of(DuplicateHeaderMode.ALLOW_EMPTY, true, new String[] {"A", "A", "", ""}, false),
Arguments.of(DuplicateHeaderMode.ALLOW_ALL, true, new String[] {"A", "A", "", ""}, true), Arguments.of(DuplicateHeaderMode.ALLOW_ALL, true, new String[] {"A", "A", "", ""}, true),
@ -89,13 +93,30 @@ public class CSVDuplicateHeaderTest {
// Duplicate non-empty and blank names // Duplicate non-empty and blank names
Arguments.of(DuplicateHeaderMode.DISALLOW, false, new String[] {"A", "A", " ", " "}, false), Arguments.of(DuplicateHeaderMode.DISALLOW, false, new String[] {"A", "A", " ", " "}, false),
Arguments.of(DuplicateHeaderMode.ALLOW_EMPTY, false, new String[] {"A", "A", " ", " "}, false), Arguments.of(DuplicateHeaderMode.ALLOW_EMPTY, false, new String[] {"A", "A", " ", " "}, false),
//Arguments.of(DuplicateHeaderMode.ALLOW_ALL, false, new String[] {"A", "A", " ", " "}, false), // Arguments.of(DuplicateHeaderMode.ALLOW_ALL, false, new String[] {"A", "A", " ", " "}, false),
Arguments.of(DuplicateHeaderMode.DISALLOW, true, new String[] {"A", "A", " ", " "}, false), Arguments.of(DuplicateHeaderMode.DISALLOW, true, new String[] {"A", "A", " ", " "}, false),
Arguments.of(DuplicateHeaderMode.ALLOW_EMPTY, true, new String[] {"A", "A", " ", " "}, false), Arguments.of(DuplicateHeaderMode.ALLOW_EMPTY, true, new String[] {"A", "A", " ", " "}, false),
Arguments.of(DuplicateHeaderMode.ALLOW_ALL, true, new String[] {"A", "A", " ", " "}, true) Arguments.of(DuplicateHeaderMode.ALLOW_ALL, true, new String[] {"A", "A", " ", " "}, true)
); );
} }
static Stream<Arguments> duplicateHeaderParseOnlyData() {
return Stream.of(
// Duplicate empty names
Arguments.of(DuplicateHeaderMode.ALLOW_EMPTY, false, new String[] { "", "" }, false),
Arguments.of(DuplicateHeaderMode.ALLOW_ALL, false, new String[] { "", "" }, false),
// Duplicate blank names
Arguments.of(DuplicateHeaderMode.ALLOW_ALL, false, new String[] { " ", " " }, false),
Arguments.of(DuplicateHeaderMode.ALLOW_EMPTY, true, new String[] { " ", " " }, true),
// Duplicate non-empty and empty names
Arguments.of(DuplicateHeaderMode.ALLOW_ALL, false, new String[] { "A", "A", "", "" }, false),
// Duplicate non-empty and blank names
Arguments.of(DuplicateHeaderMode.ALLOW_ALL, false, new String[] { "A", "A", " ", " " }, false));
}
/** /**
* Test duplicate headers with the CSVFormat. * Test duplicate headers with the CSVFormat.
* *
@ -106,21 +127,21 @@ public class CSVDuplicateHeaderTest {
*/ */
@ParameterizedTest @ParameterizedTest
@MethodSource(value = {"duplicateHeaderData"}) @MethodSource(value = {"duplicateHeaderData"})
public void testCSVFormat(DuplicateHeaderMode duplicateHeaderMode, public void testCSVFormat(final DuplicateHeaderMode duplicateHeaderMode,
boolean allowMissingColumnNames, final boolean allowMissingColumnNames,
String[] headers, final String[] headers,
boolean valid) { final boolean valid) {
CSVFormat.Builder builder = CSVFormat.DEFAULT.builder() final CSVFormat.Builder builder = CSVFormat.DEFAULT.builder()
.setDuplicateHeaderMode(duplicateHeaderMode) .setDuplicateHeaderMode(duplicateHeaderMode)
.setAllowMissingColumnNames(allowMissingColumnNames) .setAllowMissingColumnNames(allowMissingColumnNames)
.setHeader(headers); .setHeader(headers);
if (valid) { if (valid) {
CSVFormat format = builder.build(); final CSVFormat format = builder.build();
Assertions.assertEquals(duplicateHeaderMode, format.getDuplicateHeaderMode(), "DuplicateHeaderMode"); Assertions.assertEquals(duplicateHeaderMode, format.getDuplicateHeaderMode(), "DuplicateHeaderMode");
Assertions.assertEquals(allowMissingColumnNames, format.getAllowMissingColumnNames(), "AllowMissingColumnNames"); Assertions.assertEquals(allowMissingColumnNames, format.getAllowMissingColumnNames(), "AllowMissingColumnNames");
Assertions.assertArrayEquals(headers, format.getHeader(), "Header"); Assertions.assertArrayEquals(headers, format.getHeader(), "Header");
} else { } else {
Assertions.assertThrows(IllegalArgumentException.class, () -> builder.build()); Assertions.assertThrows(IllegalArgumentException.class, builder::build);
} }
} }
@ -134,17 +155,17 @@ public class CSVDuplicateHeaderTest {
* @throws IOException Signals that an I/O exception has occurred. * @throws IOException Signals that an I/O exception has occurred.
*/ */
@ParameterizedTest @ParameterizedTest
@MethodSource(value = {"duplicateHeaderData"}) @MethodSource(value = {"duplicateHeaderData", "duplicateHeaderParseOnlyData"})
public void testCSVParser(DuplicateHeaderMode duplicateHeaderMode, public void testCSVParser(final DuplicateHeaderMode duplicateHeaderMode,
boolean allowMissingColumnNames, final boolean allowMissingColumnNames,
String[] headers, final String[] headers,
boolean valid) throws IOException { final boolean valid) throws IOException {
CSVFormat format = CSVFormat.DEFAULT.builder() final CSVFormat format = CSVFormat.DEFAULT.builder()
.setDuplicateHeaderMode(duplicateHeaderMode) .setDuplicateHeaderMode(duplicateHeaderMode)
.setAllowMissingColumnNames(allowMissingColumnNames) .setAllowMissingColumnNames(allowMissingColumnNames)
.setHeader() .setHeader()
.build(); .build();
String input = Arrays.stream(headers).collect(Collectors.joining(format.getDelimiterString())); final String input = Arrays.stream(headers).collect(Collectors.joining(format.getDelimiterString()));
if (valid) { if (valid) {
try(CSVParser parser = CSVParser.parse(input, format)) { try(CSVParser parser = CSVParser.parse(input, format)) {
Assertions.assertEquals(Arrays.asList(headers), parser.getHeaderNames()); Assertions.assertEquals(Arrays.asList(headers), parser.getHeaderNames());