From d6e494b44d85540923b39eb4956a704ecee295b1 Mon Sep 17 00:00:00 2001 From: Gary Gregory Date: Wed, 5 Jun 2019 18:03:25 -0400 Subject: [PATCH] [CSV-241] CSVFormat#valiadte() does not account for llowDuplicateHeaderNames. Applying a different version of the GitHub patch with adjustments to the tests. Also remove trailing whitespace from CSVRecord. Closes #43. --- src/changes/changes.xml | 3 +++ .../java/org/apache/commons/csv/CSVFormat.java | 2 +- .../java/org/apache/commons/csv/CSVRecord.java | 2 +- .../java/org/apache/commons/csv/CSVFormatTest.java | 14 +++++++++++++- 4 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 0a7aa45a..44892736 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -38,6 +38,9 @@ Release Notes + + CSVFormat#valiadte() does not account for allowDuplicateHeaderNames #43. + Add predefined CSVFormats for printing MongoDB CSV and TSV. Fix escape character for POSTGRESQL_TEXT and POSTGRESQL_CSV formats. diff --git a/src/main/java/org/apache/commons/csv/CSVFormat.java b/src/main/java/org/apache/commons/csv/CSVFormat.java index 264f2f8e..1f607851 100644 --- a/src/main/java/org/apache/commons/csv/CSVFormat.java +++ b/src/main/java/org/apache/commons/csv/CSVFormat.java @@ -1668,7 +1668,7 @@ public final class CSVFormat implements Serializable { } // validate header - if (header != null) { + if (header != null && !allowDuplicateHeaderNames) { final Set dupCheck = new HashSet<>(); for (final String hdr : header) { if (!dupCheck.add(hdr)) { diff --git a/src/main/java/org/apache/commons/csv/CSVRecord.java b/src/main/java/org/apache/commons/csv/CSVRecord.java index 942a0905..b1b3f995 100644 --- a/src/main/java/org/apache/commons/csv/CSVRecord.java +++ b/src/main/java/org/apache/commons/csv/CSVRecord.java @@ -281,7 +281,7 @@ public final class CSVRecord implements Serializable, Iterable { */ @Override public String toString() { - return "CSVRecord [comment='" + comment + "', recordNumber=" + recordNumber + ", values=" + + return "CSVRecord [comment='" + comment + "', recordNumber=" + recordNumber + ", values=" + Arrays.toString(values) + "]"; } diff --git a/src/test/java/org/apache/commons/csv/CSVFormatTest.java b/src/test/java/org/apache/commons/csv/CSVFormatTest.java index 6d30e371..2a74b6f5 100644 --- a/src/test/java/org/apache/commons/csv/CSVFormatTest.java +++ b/src/test/java/org/apache/commons/csv/CSVFormatTest.java @@ -72,8 +72,20 @@ public class CSVFormatTest { } @Test(expected = IllegalArgumentException.class) + public void testDuplicateHeaderElementsFalse() { + CSVFormat.DEFAULT.withAllowDuplicateHeaderNames(false).withHeader("A", "A"); + } + + public void testDuplicateHeaderElementsTrue() { + CSVFormat.DEFAULT.withAllowDuplicateHeaderNames(true).withHeader("A", "A"); + } + + @Test public void testDuplicateHeaderElements() { - CSVFormat.DEFAULT.withHeader("A", "A"); + final String[] header = { "A", "A" }; + final CSVFormat format = CSVFormat.DEFAULT.withHeader(header); + assertEquals(2, format.getHeader().length); + assertArrayEquals(header, format.getHeader()); } @Test