Validate that headers do not contain duplicates.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/csv/trunk@1508618 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
3320c53e27
commit
f881372d92
|
@ -17,11 +17,11 @@
|
||||||
|
|
||||||
package org.apache.commons.csv;
|
package org.apache.commons.csv;
|
||||||
|
|
||||||
|
import static org.apache.commons.csv.Constants.BACKSLASH;
|
||||||
import static org.apache.commons.csv.Constants.COMMA;
|
import static org.apache.commons.csv.Constants.COMMA;
|
||||||
import static org.apache.commons.csv.Constants.CR;
|
import static org.apache.commons.csv.Constants.CR;
|
||||||
import static org.apache.commons.csv.Constants.CRLF;
|
import static org.apache.commons.csv.Constants.CRLF;
|
||||||
import static org.apache.commons.csv.Constants.DOUBLE_QUOTE_CHAR;
|
import static org.apache.commons.csv.Constants.DOUBLE_QUOTE_CHAR;
|
||||||
import static org.apache.commons.csv.Constants.BACKSLASH;
|
|
||||||
import static org.apache.commons.csv.Constants.LF;
|
import static org.apache.commons.csv.Constants.LF;
|
||||||
import static org.apache.commons.csv.Constants.TAB;
|
import static org.apache.commons.csv.Constants.TAB;
|
||||||
|
|
||||||
|
@ -30,6 +30,8 @@ import java.io.Reader;
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
import java.io.StringWriter;
|
import java.io.StringWriter;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Specifies the format of a CSV file and parses input.
|
* Specifies the format of a CSV file and parses input.
|
||||||
|
@ -530,6 +532,14 @@ public class CSVFormat implements Serializable {
|
||||||
if (escape == null && quotePolicy == Quote.NONE) {
|
if (escape == null && quotePolicy == Quote.NONE) {
|
||||||
throw new IllegalStateException("No quotes mode set but no escape character is set");
|
throw new IllegalStateException("No quotes mode set but no escape character is set");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (header != null) {
|
||||||
|
Set<String> set = new HashSet<String>(header.length);
|
||||||
|
set.addAll(Arrays.asList(header));
|
||||||
|
if (set.size() != header.length) {
|
||||||
|
throw new IllegalStateException("The header contains duplicate names: " + Arrays.toString(header));
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -57,6 +57,11 @@ public class CSVFormatTest {
|
||||||
CSVFormat.DEFAULT.withDelimiter('!').withEscape('!').validate();
|
CSVFormat.DEFAULT.withDelimiter('!').withEscape('!').validate();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test(expected = IllegalStateException.class)
|
||||||
|
public void testDuplicateHeaderElements() {
|
||||||
|
CSVFormat.DEFAULT.withHeader("A", "A").validate();
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testEquals() {
|
public void testEquals() {
|
||||||
final CSVFormat right = CSVFormat.DEFAULT;
|
final CSVFormat right = CSVFormat.DEFAULT;
|
||||||
|
|
Loading…
Reference in New Issue