diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 0e128b83..e8854416 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -40,6 +40,7 @@ The type attribute can be add,update,fix,remove. + Check whether ISE/IAE are being used appropriately CSVFormat constructor should reject a header array with duplicate entries HeaderMap is inconsistent when it is parsed from an input with duplicate columns names CSVRecord.toMap() fails if row length shorter than header length diff --git a/src/main/java/org/apache/commons/csv/CSVParser.java b/src/main/java/org/apache/commons/csv/CSVParser.java index c98f05d6..fc98a4ba 100644 --- a/src/main/java/org/apache/commons/csv/CSVParser.java +++ b/src/main/java/org/apache/commons/csv/CSVParser.java @@ -236,7 +236,7 @@ public final class CSVParser implements Iterable, Closeable { * @throws IllegalArgumentException * If the parameters of the format are inconsistent or if either reader or format are null. * @throws IOException - * If an I/O error occurs + * If there is a problem reading the header or skipping the first record */ public CSVParser(final Reader reader, final CSVFormat format) throws IOException { Assertions.notNull(reader, "reader"); @@ -352,6 +352,7 @@ public final class CSVParser implements Iterable, Closeable { * Initializes the name to index mapping if the format defines a header. * * @return null if the format has no header. + * @throws IOException if there is a problem reading the header or skipping the first record */ private Map initializeHeader() throws IOException { Map hdrMap = null; @@ -377,7 +378,7 @@ public final class CSVParser implements Iterable, Closeable { if (header != null) { for (int i = 0; i < header.length; i++) { if (hdrMap.containsKey(header[i])) { - throw new IllegalStateException("The header contains duplicate names: " + + throw new IllegalArgumentException("The header contains duplicate names: " + Arrays.toString(header)); } hdrMap.put(header[i], Integer.valueOf(i)); diff --git a/src/test/java/org/apache/commons/csv/CSVParserTest.java b/src/test/java/org/apache/commons/csv/CSVParserTest.java index f68e7b94..ed7eadeb 100644 --- a/src/test/java/org/apache/commons/csv/CSVParserTest.java +++ b/src/test/java/org/apache/commons/csv/CSVParserTest.java @@ -492,7 +492,7 @@ public class CSVParserTest { parser.close(); } - @Test(expected = IllegalStateException.class) + @Test(expected = IllegalArgumentException.class) public void testDuplicateHeaderEntries() throws Exception { CSVParser.parse("a,b,a\n1,2,3\nx,y,z", CSVFormat.DEFAULT.withHeader(new String[]{})); }