Skip to content

Commit 3cd5067

Browse files
committed
CSV-117 Validate format parameters in constructor
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/csv/trunk@1603967 13f79535-47bb-0310-9956-ffa450edef68
1 parent f4cc9a9 commit 3cd5067

5 files changed

Lines changed: 32 additions & 32 deletions

File tree

src/changes/changes.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
<body>
4141

4242
<release version="1.0" date="TBD" description="First release">
43+
<action issue="CSV-117" type="update" dev="sebb">Validate format parameters in constructor</action>
4344
<action issue="CSV-121" type="add" dev="ggregory" due-to="Sebastian Hardt">IllegalArgumentException thrown when the header contains duplicate names when the column names are empty.</action>
4445
<action issue="CSV-120" type="add" dev="ggregory" due-to="Sergei Lebedev">CSVFormat#withHeader doesn't work with CSVPrinter</action>
4546
<action issue="CSV-119" type="add" dev="ggregory" due-to="Sergei Lebedev">CSVFormat is missing a print(...) method</action>

src/main/java/org/apache/commons/csv/CSVFormat.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,7 @@ private CSVFormat(final char delimiter, final Character quoteChar,
327327
this.header = header.clone();
328328
}
329329
this.skipHeaderRecord = skipHeaderRecord;
330+
validate();
330331
}
331332

332333
@Override
@@ -666,38 +667,38 @@ public String toString() {
666667
}
667668

668669
/**
669-
* Verifies the consistency of the parameters and throws an IllegalStateException if necessary.
670+
* Verifies the consistency of the parameters and throws an IllegalArgumentException if necessary.
670671
*
671-
* @throws IllegalStateException
672+
* @throws IllegalArgumentException
672673
*/
673-
void validate() throws IllegalStateException {
674+
private void validate() throws IllegalArgumentException {
674675
if (quoteChar != null && delimiter == quoteChar.charValue()) {
675-
throw new IllegalStateException(
676+
throw new IllegalArgumentException(
676677
"The quoteChar character and the delimiter cannot be the same ('" + quoteChar + "')");
677678
}
678679

679680
if (escape != null && delimiter == escape.charValue()) {
680-
throw new IllegalStateException(
681+
throw new IllegalArgumentException(
681682
"The escape character and the delimiter cannot be the same ('" + escape + "')");
682683
}
683684

684685
if (commentStart != null && delimiter == commentStart.charValue()) {
685-
throw new IllegalStateException(
686+
throw new IllegalArgumentException(
686687
"The comment start character and the delimiter cannot be the same ('" + commentStart + "')");
687688
}
688689

689690
if (quoteChar != null && quoteChar.equals(commentStart)) {
690-
throw new IllegalStateException(
691+
throw new IllegalArgumentException(
691692
"The comment start character and the quoteChar cannot be the same ('" + commentStart + "')");
692693
}
693694

694695
if (escape != null && escape.equals(commentStart)) {
695-
throw new IllegalStateException(
696+
throw new IllegalArgumentException(
696697
"The comment start and the escape character cannot be the same ('" + commentStart + "')");
697698
}
698699

699700
if (escape == null && quotePolicy == Quote.NONE) {
700-
throw new IllegalStateException("No quotes mode set but no escape character is set");
701+
throw new IllegalArgumentException("No quotes mode set but no escape character is set");
701702
}
702703

703704
}

src/main/java/org/apache/commons/csv/CSVParser.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,6 @@ public CSVParser(final Reader reader, final CSVFormat format) throws IOException
245245
Assertions.notNull(reader, "reader");
246246
Assertions.notNull(format, "format");
247247

248-
format.validate();
249248
this.format = format;
250249
this.lexer = new Lexer(format, new ExtendedBufferedReader(reader));
251250
this.headerMap = this.initializeHeader();

src/main/java/org/apache/commons/csv/CSVPrinter.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ public CSVPrinter(final Appendable out, final CSVFormat format) throws IOExcepti
6464

6565
this.out = out;
6666
this.format = format;
67-
this.format.validate();
6867
// TODO: Is it a good idea to do this here instead of on the first call to a print method?
6968
// It seems a pain to have to track whether the header has already been printed or not.
7069
if (format.getHeader() != null) {

src/test/java/org/apache/commons/csv/CSVFormatTest.java

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -51,19 +51,19 @@ private static CSVFormat copy(final CSVFormat format) {
5151
return format.withDelimiter(format.getDelimiter());
5252
}
5353

54-
@Test(expected = IllegalStateException.class)
54+
@Test(expected = IllegalArgumentException.class)
5555
public void testDelimiterSameAsCommentStartThrowsException() {
56-
CSVFormat.DEFAULT.withDelimiter('!').withCommentStart('!').validate();
56+
CSVFormat.DEFAULT.withDelimiter('!').withCommentStart('!');
5757
}
5858

59-
@Test(expected = IllegalStateException.class)
59+
@Test(expected = IllegalArgumentException.class)
6060
public void testDelimiterSameAsEscapeThrowsException() {
61-
CSVFormat.DEFAULT.withDelimiter('!').withEscape('!').validate();
61+
CSVFormat.DEFAULT.withDelimiter('!').withEscape('!');
6262
}
6363

6464
@Test(expected = IllegalArgumentException.class)
6565
public void testDuplicateHeaderElements() {
66-
CSVFormat.DEFAULT.withHeader("A", "A").validate();
66+
CSVFormat.DEFAULT.withHeader("A", "A");
6767
}
6868

6969
@Test
@@ -231,15 +231,15 @@ public void testEqualsSkipHeaderRecord() {
231231
assertNotEquals(right, left);
232232
}
233233

234-
@Test(expected = IllegalStateException.class)
234+
@Test(expected = IllegalArgumentException.class)
235235
public void testEscapeSameAsCommentStartThrowsException() {
236-
CSVFormat.DEFAULT.withEscape('!').withCommentStart('!').validate();
236+
CSVFormat.DEFAULT.withEscape('!').withCommentStart('!');
237237
}
238238

239-
@Test(expected = IllegalStateException.class)
239+
@Test(expected = IllegalArgumentException.class)
240240
public void testEscapeSameAsCommentStartThrowsExceptionForWrapperType() {
241241
// Cannot assume that callers won't use different Character objects
242-
CSVFormat.DEFAULT.withEscape(new Character('!')).withCommentStart(new Character('!')).validate();
242+
CSVFormat.DEFAULT.withEscape(new Character('!')).withCommentStart(new Character('!'));
243243
}
244244

245245
@Test
@@ -272,25 +272,25 @@ public void testNullRecordSeparatorCsv106() {
272272
assertFalse(formatStr.endsWith("null"));
273273
}
274274

275-
@Test(expected = IllegalStateException.class)
275+
@Test(expected = IllegalArgumentException.class)
276276
public void testQuoteCharSameAsCommentStartThrowsException() {
277-
CSVFormat.DEFAULT.withQuoteChar('!').withCommentStart('!').validate();
277+
CSVFormat.DEFAULT.withQuoteChar('!').withCommentStart('!');
278278
}
279279

280-
@Test(expected = IllegalStateException.class)
280+
@Test(expected = IllegalArgumentException.class)
281281
public void testQuoteCharSameAsCommentStartThrowsExceptionForWrapperType() {
282282
// Cannot assume that callers won't use different Character objects
283-
CSVFormat.DEFAULT.withQuoteChar(new Character('!')).withCommentStart('!').validate();
283+
CSVFormat.DEFAULT.withQuoteChar(new Character('!')).withCommentStart('!');
284284
}
285285

286-
@Test(expected = IllegalStateException.class)
286+
@Test//(expected = IllegalArgumentException.class)
287287
public void testQuoteCharSameAsDelimiterThrowsException() {
288-
CSVFormat.DEFAULT.withQuoteChar('!').withDelimiter('!').validate();
288+
CSVFormat.DEFAULT.withQuoteChar('!').withDelimiter('!');
289289
}
290290

291-
@Test(expected = IllegalStateException.class)
291+
@Test(expected = IllegalArgumentException.class)
292292
public void testQuotePolicyNoneWithoutEscapeThrowsException() {
293-
CSVFormat.newFormat('!').withQuotePolicy(Quote.NONE).validate();
293+
CSVFormat.newFormat('!').withQuotePolicy(Quote.NONE);
294294
}
295295

296296
@Test
@@ -335,7 +335,7 @@ public void testWithCommentStart() throws Exception {
335335

336336
@Test(expected = IllegalArgumentException.class)
337337
public void testWithCommentStartCRThrowsException() {
338-
CSVFormat.DEFAULT.withCommentStart(CR).validate();
338+
CSVFormat.DEFAULT.withCommentStart(CR);
339339
}
340340

341341
@Test
@@ -346,7 +346,7 @@ public void testWithDelimiter() throws Exception {
346346

347347
@Test(expected = IllegalArgumentException.class)
348348
public void testWithDelimiterLFThrowsException() {
349-
CSVFormat.DEFAULT.withDelimiter(LF).validate();
349+
CSVFormat.DEFAULT.withDelimiter(LF);
350350
}
351351

352352
@Test
@@ -357,7 +357,7 @@ public void testWithEscape() throws Exception {
357357

358358
@Test(expected = IllegalArgumentException.class)
359359
public void testWithEscapeCRThrowsExceptions() {
360-
CSVFormat.DEFAULT.withEscape(CR).validate();
360+
CSVFormat.DEFAULT.withEscape(CR);
361361
}
362362

363363
@Test
@@ -399,7 +399,7 @@ public void testWithQuoteChar() throws Exception {
399399

400400
@Test(expected = IllegalArgumentException.class)
401401
public void testWithQuoteLFThrowsException() {
402-
CSVFormat.DEFAULT.withQuoteChar(LF).validate();
402+
CSVFormat.DEFAULT.withQuoteChar(LF);
403403
}
404404

405405
@Test

0 commit comments

Comments
 (0)