File tree Expand file tree Collapse file tree
main/java/org/apache/commons/csv
test/java/org/apache/commons/csv Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1717
1818package org .apache .commons .csv ;
1919
20+ import static org .apache .commons .csv .Constants .BACKSLASH ;
2021import static org .apache .commons .csv .Constants .COMMA ;
2122import static org .apache .commons .csv .Constants .CR ;
2223import static org .apache .commons .csv .Constants .CRLF ;
2324import static org .apache .commons .csv .Constants .DOUBLE_QUOTE_CHAR ;
24- import static org .apache .commons .csv .Constants .BACKSLASH ;
2525import static org .apache .commons .csv .Constants .LF ;
2626import static org .apache .commons .csv .Constants .TAB ;
2727
3030import java .io .Serializable ;
3131import java .io .StringWriter ;
3232import java .util .Arrays ;
33+ import java .util .HashSet ;
34+ import java .util .Set ;
3335
3436/**
3537 * Specifies the format of a CSV file and parses input.
@@ -530,6 +532,14 @@ void validate() throws IllegalStateException {
530532 if (escape == null && quotePolicy == Quote .NONE ) {
531533 throw new IllegalStateException ("No quotes mode set but no escape character is set" );
532534 }
535+
536+ if (header != null ) {
537+ Set <String > set = new HashSet <String >(header .length );
538+ set .addAll (Arrays .asList (header ));
539+ if (set .size () != header .length ) {
540+ throw new IllegalStateException ("The header contains duplicate names: " + Arrays .toString (header ));
541+ }
542+ }
533543 }
534544
535545 /**
Original file line number Diff line number Diff line change @@ -57,6 +57,11 @@ public void testDelimiterSameAsEscapeThrowsException() {
5757 CSVFormat .DEFAULT .withDelimiter ('!' ).withEscape ('!' ).validate ();
5858 }
5959
60+ @ Test (expected = IllegalStateException .class )
61+ public void testDuplicateHeaderElements () {
62+ CSVFormat .DEFAULT .withHeader ("A" , "A" ).validate ();
63+ }
64+
6065 @ Test
6166 public void testEquals () {
6267 final CSVFormat right = CSVFormat .DEFAULT ;
You can’t perform that action at this time.
0 commit comments