Author: sebb
Date: Wed May  7 17:27:31 2014
New Revision: 1593076

URL: http://svn.apache.org/r1593076
Log:
CSV-114 CSVFormat constructor should reject a header array with duplicate 
entries

Modified:
    commons/proper/csv/trunk/src/changes/changes.xml
    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/changes/changes.xml
URL: 
http://svn.apache.org/viewvc/commons/proper/csv/trunk/src/changes/changes.xml?rev=1593076&r1=1593075&r2=1593076&view=diff
==============================================================================
--- commons/proper/csv/trunk/src/changes/changes.xml (original)
+++ commons/proper/csv/trunk/src/changes/changes.xml Wed May  7 17:27:31 2014
@@ -40,6 +40,7 @@ The <action> type attribute can be add,u
   <body>
 
     <release version="1.0" date="TBD" description="First release">
+        <action issue="CSV-114" type="fix" dev="sebb">CSVFormat constructor 
should reject a header array with duplicate entries</action>
         <action issue="CSV-112" type="fix" dev="britter">HeaderMap is 
inconsistent when it is parsed from an input with duplicate columns 
names</action>
         <action issue="CSV-111" type="fix" dev="ggregory">CSVRecord.toMap() 
fails if row length shorter than header length</action>
         <action issue="CSV-106" type="fix" dev="ggregory">CSVFormat.format 
allways append null</action>

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=1593076&r1=1593075&r2=1593076&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 
Wed May  7 17:27:31 2014
@@ -155,7 +155,7 @@ public final class CSVFormat implements 
     private final boolean ignoreEmptyLines;
     private final String recordSeparator; // for outputs
     private final String nullString; // the string to be used for null values
-    private final String[] header;
+    private final String[] header; // array of header column names
     private final boolean skipHeaderRecord;
 
     /**
@@ -310,7 +310,17 @@ public final class CSVFormat implements 
         this.ignoreEmptyLines = ignoreEmptyLines;
         this.recordSeparator = recordSeparator;
         this.nullString = nullString;
-        this.header = header == null ? null : header.clone();
+        if (header == null) {
+               this.header = null;
+        } else {
+               Set<String> dupCheck = new HashSet<String>();
+               for(String hdr : header) {
+                       if (!dupCheck.add(hdr)) {
+                               throw new IllegalArgumentException("The header 
contains a duplicate entry: '" + hdr + "' in " + Arrays.toString(header));
+                       }
+               }
+            this.header = header.clone();              
+        }
         this.skipHeaderRecord = skipHeaderRecord;
     }
 
@@ -658,13 +668,6 @@ public final class CSVFormat implements 
             throw new IllegalStateException("No quotes mode set but no escape 
character is set");
         }
 
-        if (header != null) {
-            final 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=1593076&r1=1593075&r2=1593076&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
 Wed May  7 17:27:31 2014
@@ -61,7 +61,7 @@ public class CSVFormatTest {
         CSVFormat.DEFAULT.withDelimiter('!').withEscape('!').validate();
     }
 
-    @Test(expected = IllegalStateException.class)
+    @Test(expected = IllegalArgumentException.class)
     public void testDuplicateHeaderElements() {
         CSVFormat.DEFAULT.withHeader("A", "A").validate();
     }


Reply via email to