Skip to content

Commit a2fef00

Browse files
committed
Make methods that create parsers or printers fail early and provide an expressive error messages. Document new behavior in JavaDoc
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/csv/trunk@1518802 13f79535-47bb-0310-9956-ffa450edef68
1 parent 33cf289 commit a2fef00

6 files changed

Lines changed: 126 additions & 14 deletions

File tree

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package org.apache.commons.csv;
2+
3+
/**
4+
* Utility class for input parameter validation
5+
*
6+
* @version $Id$
7+
*/
8+
final class Assertions {
9+
10+
private Assertions() {
11+
// can not be instantiated
12+
}
13+
14+
public static <T> void notNull(T parameter, String parameterName) {
15+
if (parameter == null) {
16+
throw new IllegalArgumentException("Parameter '" + parameterName + "' must not be null!");
17+
}
18+
}
19+
}

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

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -96,29 +96,39 @@ public final class CSVParser implements Iterable<CSVRecord>, Closeable {
9696
* Creates a parser for the given {@link File}.
9797
*
9898
* @param file
99-
* a CSV file
99+
* a CSV file. Must not be null.
100100
* @param format
101-
* the CSVFormat used for CSV parsing
101+
* the CSVFormat used for CSV parsing. Must not be null.
102102
* @return a new parser
103+
* @throws IllegalArgumentException
104+
* If the parameters of the format are inconsistent or if either file or format are null.
103105
* @throws IOException
104106
* If an I/O error occurs
105107
*/
106108
public static CSVParser parse(File file, final CSVFormat format) throws IOException {
109+
Assertions.notNull(file, "file");
110+
Assertions.notNull(format, "format");
111+
107112
return new CSVParser(new FileReader(file), format);
108113
}
109114

110115
/**
111116
* Creates a parser for the given {@link String}.
112117
*
113118
* @param string
114-
* a CSV string
119+
* a CSV string. Must not be null.
115120
* @param format
116-
* the CSVFormat used for CSV parsing
121+
* the CSVFormat used for CSV parsing. Must not be null.
117122
* @return a new parser
123+
* @throws IllegalArgumentException
124+
* If the parameters of the format are inconsistent or if either string or format are null.
118125
* @throws IOException
119126
* If an I/O error occurs
120127
*/
121128
public static CSVParser parse(String string, final CSVFormat format) throws IOException {
129+
Assertions.notNull(string, "string");
130+
Assertions.notNull(format, "format");
131+
122132
return new CSVParser(new StringReader(string), format);
123133
}
124134

@@ -131,17 +141,22 @@ public static CSVParser parse(String string, final CSVFormat format) throws IOEx
131141
* </p>
132142
*
133143
* @param url
134-
* a URL
144+
* a URL. Must not be null.
135145
* @param charset
136146
* the charset for the resource, if {@code null}, uses {@code UTF-8}. UTF-8 is one of the encodings
137147
* required by the Java specification.
138148
* @param format
139-
* the CSVFormat used for CSV parsing
149+
* the CSVFormat used for CSV parsing. Must not be null.
140150
* @return a new parser
151+
* @throws IllegalArgumentException
152+
* If the parameters of the format are inconsistent or if either url or format are null.
141153
* @throws IOException
142154
* If an I/O error occurs
143155
*/
144156
public static CSVParser parse(URL url, Charset charset, final CSVFormat format) throws IOException {
157+
Assertions.notNull(url, "url");
158+
Assertions.notNull(format, "format");
159+
145160
return new CSVParser(new InputStreamReader(url.openStream(),
146161
charset == null ? Charset.forName("UTF-8") : charset), format);
147162
}
@@ -169,15 +184,18 @@ public static CSVParser parse(URL url, Charset charset, final CSVFormat format)
169184
* </p>
170185
*
171186
* @param reader
172-
* a Reader containing CSV-formatted input
187+
* a Reader containing CSV-formatted input. Must not be null.
173188
* @param format
174-
* the CSVFormat used for CSV parsing
189+
* the CSVFormat used for CSV parsing. Must not be null.
175190
* @throws IllegalArgumentException
176-
* thrown if the parameters of the format are inconsistent
191+
* If the parameters of the format are inconsistent or if either reader or format are null.
177192
* @throws IOException
178193
* If an I/O error occurs
179194
*/
180195
public CSVParser(final Reader reader, final CSVFormat format) throws IOException {
196+
Assertions.notNull(reader, "reader");
197+
Assertions.notNull(format, "format");
198+
181199
format.validate();
182200
this.format = format;
183201
this.lexer = new Lexer(format, new ExtendedBufferedReader(reader));

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,18 @@ public final class CSVPrinter implements Flushable, Closeable {
4949
* (encapsulation and escaping with a different character) are not supported.
5050
*
5151
* @param out
52-
* stream to which to print.
52+
* stream to which to print. Must not be null.
5353
* @param format
54-
* the CSV format. If null the default format is used ({@link CSVFormat#DEFAULT})
54+
* the CSV format. Must not be null.
5555
* @throws IllegalArgumentException
56-
* thrown if the parameters of the format are inconsistent
56+
* thrown if the parameters of the format are inconsistent or if either out or format are null.
5757
*/
5858
public CSVPrinter(final Appendable out, final CSVFormat format) {
59+
Assertions.notNull(out, "out");
60+
Assertions.notNull(format, "format");
61+
5962
this.out = out;
60-
this.format = format == null ? CSVFormat.DEFAULT : format;
63+
this.format = format;
6164
this.format.validate();
6265
}
6366

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package org.apache.commons.csv;
2+
3+
import org.junit.Test;
4+
5+
/**
6+
* @version $Id$
7+
*/
8+
public class AssertionsTest {
9+
10+
@Test
11+
public void testNotNull() throws Exception {
12+
Assertions.notNull(new Object(), "object");
13+
}
14+
15+
@Test(expected = IllegalArgumentException.class)
16+
public void testNotNullNull() throws Exception {
17+
Assertions.notNull(null, "object");
18+
}
19+
}

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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,13 @@
2828
import static org.junit.Assert.assertTrue;
2929
import static org.junit.Assert.fail;
3030

31+
import java.io.File;
3132
import java.io.IOException;
3233
import java.io.Reader;
3334
import java.io.StringReader;
3435
import java.io.StringWriter;
36+
import java.net.URL;
37+
import java.nio.charset.Charset;
3538
import java.util.ArrayList;
3639
import java.util.Iterator;
3740
import java.util.List;
@@ -721,6 +724,46 @@ public void testInvalidFormat() throws Exception {
721724
new CSVParser(null, invalidFormat).close();
722725
}
723726

727+
@Test(expected = IllegalArgumentException.class)
728+
public void testParseNullFileFormat() throws Exception {
729+
CSVParser.parse((File) null, CSVFormat.DEFAULT);
730+
}
731+
732+
@Test(expected = IllegalArgumentException.class)
733+
public void testParseFileNullFormat() throws Exception {
734+
CSVParser.parse(new File(""), null);
735+
}
736+
737+
@Test(expected = IllegalArgumentException.class)
738+
public void testParseNullStringFormat() throws Exception {
739+
CSVParser.parse((String) null, CSVFormat.DEFAULT);
740+
}
741+
742+
@Test(expected = IllegalArgumentException.class)
743+
public void testParseStringNullFormat() throws Exception {
744+
CSVParser.parse("csv data", null);
745+
}
746+
747+
@Test(expected = IllegalArgumentException.class)
748+
public void testParseNullUrlCharsetFormat() throws Exception {
749+
CSVParser.parse(null, Charset.defaultCharset(), CSVFormat.DEFAULT);
750+
}
751+
752+
@Test(expected = IllegalArgumentException.class)
753+
public void testParseUrlCharsetNullFormat() throws Exception {
754+
CSVParser.parse(new URL("http://commons.apache.org"), Charset.defaultCharset(), null);
755+
}
756+
757+
@Test(expected = IllegalArgumentException.class)
758+
public void testNewCSVParserNullReaderFormat() throws Exception {
759+
new CSVParser(null, CSVFormat.DEFAULT);
760+
}
761+
762+
@Test(expected = IllegalArgumentException.class)
763+
public void testNewCSVParserReaderNullFormat() throws Exception {
764+
new CSVParser(new StringReader(""), null);
765+
}
766+
724767
private void validateRecordNumbers(final String lineSeparator) throws IOException {
725768
final CSVParser parser = CSVParser.parse("a" + lineSeparator + "b" + lineSeparator + "c", CSVFormat.DEFAULT.withRecordSeparator(lineSeparator));
726769
CSVRecord record;

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,16 @@ public void testEOLPlain() throws IOException {
487487
@Test(expected = IllegalArgumentException.class)
488488
public void testInvalidFormat() throws Exception {
489489
final CSVFormat invalidFormat = CSVFormat.DEFAULT.withDelimiter(CR);
490-
new CSVPrinter(null, invalidFormat).close();
490+
new CSVPrinter(new StringWriter(), invalidFormat).close();
491+
}
492+
493+
@Test(expected = IllegalArgumentException.class)
494+
public void testNewCSVPrinterNullAppendableFormat() throws Exception {
495+
new CSVPrinter(null, CSVFormat.DEFAULT);
496+
}
497+
498+
@Test(expected = IllegalArgumentException.class)
499+
public void testNewCsvPrinterAppendableNullFormat() throws Exception {
500+
new CSVPrinter(new StringWriter(), null);
491501
}
492502
}

0 commit comments

Comments
 (0)