diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index b81f389d..0e74adda 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -40,6 +40,7 @@
+ Validate format parameters in constructor
IllegalArgumentException thrown when the header contains duplicate names when the column names are empty.
CSVFormat#withHeader doesn't work with CSVPrinter
CSVFormat is missing a print(...) method
diff --git a/src/main/java/org/apache/commons/csv/CSVFormat.java b/src/main/java/org/apache/commons/csv/CSVFormat.java
index aa271297..0b916219 100644
--- a/src/main/java/org/apache/commons/csv/CSVFormat.java
+++ b/src/main/java/org/apache/commons/csv/CSVFormat.java
@@ -327,6 +327,7 @@ public final class CSVFormat implements Serializable {
this.header = header.clone();
}
this.skipHeaderRecord = skipHeaderRecord;
+ validate();
}
@Override
@@ -666,38 +667,38 @@ public final class CSVFormat implements Serializable {
}
/**
- * Verifies the consistency of the parameters and throws an IllegalStateException if necessary.
+ * Verifies the consistency of the parameters and throws an IllegalArgumentException if necessary.
*
- * @throws IllegalStateException
+ * @throws IllegalArgumentException
*/
- void validate() throws IllegalStateException {
+ private void validate() throws IllegalArgumentException {
if (quoteChar != null && delimiter == quoteChar.charValue()) {
- throw new IllegalStateException(
+ throw new IllegalArgumentException(
"The quoteChar character and the delimiter cannot be the same ('" + quoteChar + "')");
}
if (escape != null && delimiter == escape.charValue()) {
- throw new IllegalStateException(
+ throw new IllegalArgumentException(
"The escape character and the delimiter cannot be the same ('" + escape + "')");
}
if (commentStart != null && delimiter == commentStart.charValue()) {
- throw new IllegalStateException(
+ throw new IllegalArgumentException(
"The comment start character and the delimiter cannot be the same ('" + commentStart + "')");
}
if (quoteChar != null && quoteChar.equals(commentStart)) {
- throw new IllegalStateException(
+ throw new IllegalArgumentException(
"The comment start character and the quoteChar cannot be the same ('" + commentStart + "')");
}
if (escape != null && escape.equals(commentStart)) {
- throw new IllegalStateException(
+ throw new IllegalArgumentException(
"The comment start and the escape character cannot be the same ('" + commentStart + "')");
}
if (escape == null && quotePolicy == Quote.NONE) {
- throw new IllegalStateException("No quotes mode set but no escape character is set");
+ throw new IllegalArgumentException("No quotes mode set but no escape character is set");
}
}
diff --git a/src/main/java/org/apache/commons/csv/CSVParser.java b/src/main/java/org/apache/commons/csv/CSVParser.java
index e9e94575..b6867a40 100644
--- a/src/main/java/org/apache/commons/csv/CSVParser.java
+++ b/src/main/java/org/apache/commons/csv/CSVParser.java
@@ -245,7 +245,6 @@ public final class CSVParser implements Iterable, Closeable {
Assertions.notNull(reader, "reader");
Assertions.notNull(format, "format");
- format.validate();
this.format = format;
this.lexer = new Lexer(format, new ExtendedBufferedReader(reader));
this.headerMap = this.initializeHeader();
diff --git a/src/main/java/org/apache/commons/csv/CSVPrinter.java b/src/main/java/org/apache/commons/csv/CSVPrinter.java
index bcb35cec..22693544 100644
--- a/src/main/java/org/apache/commons/csv/CSVPrinter.java
+++ b/src/main/java/org/apache/commons/csv/CSVPrinter.java
@@ -64,7 +64,6 @@ public final class CSVPrinter implements Flushable, Closeable {
this.out = out;
this.format = format;
- this.format.validate();
// TODO: Is it a good idea to do this here instead of on the first call to a print method?
// It seems a pain to have to track whether the header has already been printed or not.
if (format.getHeader() != null) {
diff --git a/src/test/java/org/apache/commons/csv/CSVFormatTest.java b/src/test/java/org/apache/commons/csv/CSVFormatTest.java
index 240ae6ea..2af770a1 100644
--- a/src/test/java/org/apache/commons/csv/CSVFormatTest.java
+++ b/src/test/java/org/apache/commons/csv/CSVFormatTest.java
@@ -51,19 +51,19 @@ public class CSVFormatTest {
return format.withDelimiter(format.getDelimiter());
}
- @Test(expected = IllegalStateException.class)
+ @Test(expected = IllegalArgumentException.class)
public void testDelimiterSameAsCommentStartThrowsException() {
- CSVFormat.DEFAULT.withDelimiter('!').withCommentStart('!').validate();
+ CSVFormat.DEFAULT.withDelimiter('!').withCommentStart('!');
}
- @Test(expected = IllegalStateException.class)
+ @Test(expected = IllegalArgumentException.class)
public void testDelimiterSameAsEscapeThrowsException() {
- CSVFormat.DEFAULT.withDelimiter('!').withEscape('!').validate();
+ CSVFormat.DEFAULT.withDelimiter('!').withEscape('!');
}
@Test(expected = IllegalArgumentException.class)
public void testDuplicateHeaderElements() {
- CSVFormat.DEFAULT.withHeader("A", "A").validate();
+ CSVFormat.DEFAULT.withHeader("A", "A");
}
@Test
@@ -231,15 +231,15 @@ public class CSVFormatTest {
assertNotEquals(right, left);
}
- @Test(expected = IllegalStateException.class)
+ @Test(expected = IllegalArgumentException.class)
public void testEscapeSameAsCommentStartThrowsException() {
- CSVFormat.DEFAULT.withEscape('!').withCommentStart('!').validate();
+ CSVFormat.DEFAULT.withEscape('!').withCommentStart('!');
}
- @Test(expected = IllegalStateException.class)
+ @Test(expected = IllegalArgumentException.class)
public void testEscapeSameAsCommentStartThrowsExceptionForWrapperType() {
// Cannot assume that callers won't use different Character objects
- CSVFormat.DEFAULT.withEscape(new Character('!')).withCommentStart(new Character('!')).validate();
+ CSVFormat.DEFAULT.withEscape(new Character('!')).withCommentStart(new Character('!'));
}
@Test
@@ -272,25 +272,25 @@ public class CSVFormatTest {
assertFalse(formatStr.endsWith("null"));
}
- @Test(expected = IllegalStateException.class)
+ @Test(expected = IllegalArgumentException.class)
public void testQuoteCharSameAsCommentStartThrowsException() {
- CSVFormat.DEFAULT.withQuoteChar('!').withCommentStart('!').validate();
+ CSVFormat.DEFAULT.withQuoteChar('!').withCommentStart('!');
}
- @Test(expected = IllegalStateException.class)
+ @Test(expected = IllegalArgumentException.class)
public void testQuoteCharSameAsCommentStartThrowsExceptionForWrapperType() {
// Cannot assume that callers won't use different Character objects
- CSVFormat.DEFAULT.withQuoteChar(new Character('!')).withCommentStart('!').validate();
+ CSVFormat.DEFAULT.withQuoteChar(new Character('!')).withCommentStart('!');
}
- @Test(expected = IllegalStateException.class)
+ @Test//(expected = IllegalArgumentException.class)
public void testQuoteCharSameAsDelimiterThrowsException() {
- CSVFormat.DEFAULT.withQuoteChar('!').withDelimiter('!').validate();
+ CSVFormat.DEFAULT.withQuoteChar('!').withDelimiter('!');
}
- @Test(expected = IllegalStateException.class)
+ @Test(expected = IllegalArgumentException.class)
public void testQuotePolicyNoneWithoutEscapeThrowsException() {
- CSVFormat.newFormat('!').withQuotePolicy(Quote.NONE).validate();
+ CSVFormat.newFormat('!').withQuotePolicy(Quote.NONE);
}
@Test
@@ -335,7 +335,7 @@ public class CSVFormatTest {
@Test(expected = IllegalArgumentException.class)
public void testWithCommentStartCRThrowsException() {
- CSVFormat.DEFAULT.withCommentStart(CR).validate();
+ CSVFormat.DEFAULT.withCommentStart(CR);
}
@Test
@@ -346,7 +346,7 @@ public class CSVFormatTest {
@Test(expected = IllegalArgumentException.class)
public void testWithDelimiterLFThrowsException() {
- CSVFormat.DEFAULT.withDelimiter(LF).validate();
+ CSVFormat.DEFAULT.withDelimiter(LF);
}
@Test
@@ -357,7 +357,7 @@ public class CSVFormatTest {
@Test(expected = IllegalArgumentException.class)
public void testWithEscapeCRThrowsExceptions() {
- CSVFormat.DEFAULT.withEscape(CR).validate();
+ CSVFormat.DEFAULT.withEscape(CR);
}
@Test
@@ -399,7 +399,7 @@ public class CSVFormatTest {
@Test(expected = IllegalArgumentException.class)
public void testWithQuoteLFThrowsException() {
- CSVFormat.DEFAULT.withQuoteChar(LF).validate();
+ CSVFormat.DEFAULT.withQuoteChar(LF);
}
@Test