[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.
This commit is contained in:
Gary Gregory 2019-06-05 18:03:25 -04:00
parent 7d100bf05b
commit d6e494b44d
4 changed files with 18 additions and 3 deletions

View File

@ -38,6 +38,9 @@
<title>Release Notes</title>
</properties>
<body>
<release version="1.8" date="2019-MM-DD" description="Feature and bug fix release (Java 8)">
<action issue="CSV-241" type="fix" dev="ggregory" due-to="LuckyIlam, Gary Gregory">CSVFormat#valiadte() does not account for allowDuplicateHeaderNames #43.</action>
</release>
<release version="1.7" date="2019-06-01" description="Feature and bug fix release (Java 8)">
<action issue="CSV-233" type="add" dev="ggregory" due-to="Gary Gregory">Add predefined CSVFormats for printing MongoDB CSV and TSV.</action>
<action issue="CSV-208" type="fix" dev="ggregory" due-to="Jurrie Overgoor">Fix escape character for POSTGRESQL_TEXT and POSTGRESQL_CSV formats.</action>

View File

@ -1668,7 +1668,7 @@ public final class CSVFormat implements Serializable {
}
// validate header
if (header != null) {
if (header != null && !allowDuplicateHeaderNames) {
final Set<String> dupCheck = new HashSet<>();
for (final String hdr : header) {
if (!dupCheck.add(hdr)) {

View File

@ -281,7 +281,7 @@ public final class CSVRecord implements Serializable, Iterable<String> {
*/
@Override
public String toString() {
return "CSVRecord [comment='" + comment + "', recordNumber=" + recordNumber + ", values=" +
return "CSVRecord [comment='" + comment + "', recordNumber=" + recordNumber + ", values=" +
Arrays.toString(values) + "]";
}

View File

@ -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