Skip to content

Commit d6e494b

Browse files
author
Gary Gregory
committed
[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 apache#43.
1 parent 7d100bf commit d6e494b

4 files changed

Lines changed: 18 additions & 3 deletions

File tree

src/changes/changes.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@
3838
<title>Release Notes</title>
3939
</properties>
4040
<body>
41+
<release version="1.8" date="2019-MM-DD" description="Feature and bug fix release (Java 8)">
42+
<action issue="CSV-241" type="fix" dev="ggregory" due-to="LuckyIlam, Gary Gregory">CSVFormat#valiadte() does not account for allowDuplicateHeaderNames #43.</action>
43+
</release>
4144
<release version="1.7" date="2019-06-01" description="Feature and bug fix release (Java 8)">
4245
<action issue="CSV-233" type="add" dev="ggregory" due-to="Gary Gregory">Add predefined CSVFormats for printing MongoDB CSV and TSV.</action>
4346
<action issue="CSV-208" type="fix" dev="ggregory" due-to="Jurrie Overgoor">Fix escape character for POSTGRESQL_TEXT and POSTGRESQL_CSV formats.</action>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1668,7 +1668,7 @@ private void validate() throws IllegalArgumentException {
16681668
}
16691669

16701670
// validate header
1671-
if (header != null) {
1671+
if (header != null && !allowDuplicateHeaderNames) {
16721672
final Set<String> dupCheck = new HashSet<>();
16731673
for (final String hdr : header) {
16741674
if (!dupCheck.add(hdr)) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ public Map<String, String> toMap() {
281281
*/
282282
@Override
283283
public String toString() {
284-
return "CSVRecord [comment='" + comment + "', recordNumber=" + recordNumber + ", values=" +
284+
return "CSVRecord [comment='" + comment + "', recordNumber=" + recordNumber + ", values=" +
285285
Arrays.toString(values) + "]";
286286
}
287287

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,20 @@ public void testDelimiterSameAsEscapeThrowsException() {
7272
}
7373

7474
@Test(expected = IllegalArgumentException.class)
75+
public void testDuplicateHeaderElementsFalse() {
76+
CSVFormat.DEFAULT.withAllowDuplicateHeaderNames(false).withHeader("A", "A");
77+
}
78+
79+
public void testDuplicateHeaderElementsTrue() {
80+
CSVFormat.DEFAULT.withAllowDuplicateHeaderNames(true).withHeader("A", "A");
81+
}
82+
83+
@Test
7584
public void testDuplicateHeaderElements() {
76-
CSVFormat.DEFAULT.withHeader("A", "A");
85+
final String[] header = { "A", "A" };
86+
final CSVFormat format = CSVFormat.DEFAULT.withHeader(header);
87+
assertEquals(2, format.getHeader().length);
88+
assertArrayEquals(header, format.getHeader());
7789
}
7890

7991
@Test

0 commit comments

Comments
 (0)