Author: ggregory
Date: Tue Jul 30 20:44:15 2013
New Revision: 1508618
URL: http://svn.apache.org/r1508618
Log:
Validate that headers do not contain duplicates.
Modified:
commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVFormat.java
commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVFormatTest.java
Modified:
commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVFormat.java
URL:
http://svn.apache.org/viewvc/commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVFormat.java?rev=1508618&r1=1508617&r2=1508618&view=diff
==============================================================================
---
commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVFormat.java
(original)
+++
commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVFormat.java
Tue Jul 30 20:44:15 2013
@@ -17,11 +17,11 @@
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.CR;
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.BACKSLASH;
import static org.apache.commons.csv.Constants.LF;
import static org.apache.commons.csv.Constants.TAB;
@@ -30,6 +30,8 @@ import java.io.Reader;
import java.io.Serializable;
import java.io.StringWriter;
import java.util.Arrays;
+import java.util.HashSet;
+import java.util.Set;
/**
* Specifies the format of a CSV file and parses input.
@@ -530,6 +532,14 @@ public class CSVFormat implements Serial
if (escape == null && quotePolicy == Quote.NONE) {
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));
+ }
+ }
}
/**
Modified:
commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVFormatTest.java
URL:
http://svn.apache.org/viewvc/commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVFormatTest.java?rev=1508618&r1=1508617&r2=1508618&view=diff
==============================================================================
---
commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVFormatTest.java
(original)
+++
commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVFormatTest.java
Tue Jul 30 20:44:15 2013
@@ -57,6 +57,11 @@ public class CSVFormatTest {
CSVFormat.DEFAULT.withDelimiter('!').withEscape('!').validate();
}
+ @Test(expected = IllegalStateException.class)
+ public void testDuplicateHeaderElements() {
+ CSVFormat.DEFAULT.withHeader("A", "A").validate();
+ }
+
@Test
public void testEquals() {
final CSVFormat right = CSVFormat.DEFAULT;