Replace org.apache.commons.csv.Assertions.notNull() with

Objects.requireNonNull().
This commit is contained in:
Gary Gregory 2020-05-24 10:47:56 -04:00
parent bc2419915d
commit adc4faa784
7 changed files with 126 additions and 205 deletions

View File

@ -54,6 +54,7 @@
<action type="update" dev="ggregory" due-to="Chen">Improve CSVRecord and CSVPrinter code coverage #66.</action>
<action type="update" dev="ggregory" due-to="Chen">Improve lexer and token coverage #67.</action>
<action issue="CSV-211" type="fix" dev="ggregory" due-to="Alpesh Kulkarni, Chen">CSVFormat.format trims last delimiter if the delimiter is a white space #71.</action>
<action type="update" dev="ggregory" due-to="Gary Gregory">Replace org.apache.commons.csv.Assertions.notNull() with Objects.requireNonNull().</action>
</release>
<release version="1.8" date="2020-02-01" description="Feature and bug fix release (Java 8).

View File

@ -1,38 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.csv;
import java.util.Objects;
/**
* Utility class for input parameter validation.
*
* TODO Replace usage with {@link Objects} when we switch to Java 7.
*/
final class Assertions {
private Assertions() {
// can not be instantiated
}
static void notNull(final Object parameter, final String parameterName) {
if (parameter == null) {
throw new IllegalArgumentException("Parameter '" + parameterName + "' must not be null!");
}
}
}

View File

@ -39,6 +39,7 @@ import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.TreeMap;
/**
@ -200,8 +201,8 @@ public final class CSVParser implements Iterable<CSVRecord>, Closeable {
*/
@SuppressWarnings("resource")
public static CSVParser parse(final File file, final Charset charset, final CSVFormat format) throws IOException {
Assertions.notNull(file, "file");
Assertions.notNull(format, "format");
Objects.requireNonNull(file, "file");
Objects.requireNonNull(format, "format");
return new CSVParser(new InputStreamReader(new FileInputStream(file), charset), format);
}
@ -229,8 +230,8 @@ public final class CSVParser implements Iterable<CSVRecord>, Closeable {
@SuppressWarnings("resource")
public static CSVParser parse(final InputStream inputStream, final Charset charset, final CSVFormat format)
throws IOException {
Assertions.notNull(inputStream, "inputStream");
Assertions.notNull(format, "format");
Objects.requireNonNull(inputStream, "inputStream");
Objects.requireNonNull(format, "format");
return parse(new InputStreamReader(inputStream, charset), format);
}
@ -251,8 +252,8 @@ public final class CSVParser implements Iterable<CSVRecord>, Closeable {
* @since 1.5
*/
public static CSVParser parse(final Path path, final Charset charset, final CSVFormat format) throws IOException {
Assertions.notNull(path, "path");
Assertions.notNull(format, "format");
Objects.requireNonNull(path, "path");
Objects.requireNonNull(format, "format");
return parse(Files.newInputStream(path), charset, format);
}
@ -293,8 +294,8 @@ public final class CSVParser implements Iterable<CSVRecord>, Closeable {
* If an I/O error occurs
*/
public static CSVParser parse(final String string, final CSVFormat format) throws IOException {
Assertions.notNull(string, "string");
Assertions.notNull(format, "format");
Objects.requireNonNull(string, "string");
Objects.requireNonNull(format, "format");
return new CSVParser(new StringReader(string), format);
}
@ -322,9 +323,9 @@ public final class CSVParser implements Iterable<CSVRecord>, Closeable {
* If an I/O error occurs
*/
public static CSVParser parse(final URL url, final Charset charset, final CSVFormat format) throws IOException {
Assertions.notNull(url, "url");
Assertions.notNull(charset, "charset");
Assertions.notNull(format, "format");
Objects.requireNonNull(url, "url");
Objects.requireNonNull(charset, "charset");
Objects.requireNonNull(format, "format");
return new CSVParser(new InputStreamReader(url.openStream(), charset), format);
}
@ -403,8 +404,8 @@ public final class CSVParser implements Iterable<CSVRecord>, Closeable {
@SuppressWarnings("resource")
public CSVParser(final Reader reader, final CSVFormat format, final long characterOffset, final long recordNumber)
throws IOException {
Assertions.notNull(reader, "reader");
Assertions.notNull(format, "format");
Objects.requireNonNull(reader, "reader");
Objects.requireNonNull(format, "format");
this.format = format;
this.lexer = new Lexer(format, new ExtendedBufferedReader(reader));

View File

@ -28,6 +28,7 @@ import java.sql.Clob;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.Objects;
/**
* Prints values in a {@link CSVFormat CSV format}.
@ -92,8 +93,8 @@ public final class CSVPrinter implements Flushable, Closeable {
* thrown if the parameters of the format are inconsistent or if either out or format are null.
*/
public CSVPrinter(final Appendable out, final CSVFormat format) throws IOException {
Assertions.notNull(out, "out");
Assertions.notNull(format, "format");
Objects.requireNonNull(out, "out");
Objects.requireNonNull(format, "format");
this.out = out;
this.format = format;

View File

@ -1,37 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.csv;
import static org.junit.jupiter.api.Assertions.assertThrows;
import org.junit.jupiter.api.Test;
/**
*/
public class AssertionsTest {
@Test
public void testNotNull() {
Assertions.notNull(new Object(), "object");
}
@Test
public void testNotNullNull() {
assertThrows(IllegalArgumentException.class, () -> Assertions.notNull(null, "object"));
}
}

View File

@ -157,8 +157,8 @@ public class CSVParserTest {
@Test
@Disabled
public void testBackslashEscapingOld() throws IOException {
final String code = "one,two,three\n" + "on\\\"e,two\n" + "on\"e,two\n" + "one,\"tw\\\"o\"\n" +
"one,\"t\\,wo\"\n" + "one,two,\"th,ree\"\n" + "\"a\\\\\"\n" + "a\\,b\n" + "\"a\\\\,b\"";
final String code = "one,two,three\n" + "on\\\"e,two\n" + "on\"e,two\n" + "one,\"tw\\\"o\"\n"
+ "one,\"t\\,wo\"\n" + "one,two,\"th,ree\"\n" + "\"a\\\\\"\n" + "a\\,b\n" + "\"a\\\\,b\"";
final String[][] res = { { "one", "two", "three" }, { "on\\\"e", "two" }, { "on\"e", "two" },
{ "one", "tw\"o" }, { "one", "t\\,wo" }, // backslash in quotes only escapes a delimiter (",")
{ "one", "two", "th,ree" }, { "a\\\\" }, // backslash in quotes only escapes a delimiter (",")
@ -293,9 +293,7 @@ public class CSVParserTest {
@Test
public void testDuplicateHeadersNotAllowed() {
assertThrows(
IllegalArgumentException.class,
() -> CSVParser.parse("a,b,a\n1,2,3\nx,y,z",
assertThrows(IllegalArgumentException.class, () -> CSVParser.parse("a,b,a\n1,2,3\nx,y,z",
CSVFormat.DEFAULT.withHeader().withAllowDuplicateHeaderNames(false)));
}
@ -356,8 +354,8 @@ public class CSVParserTest {
@Test
public void testEndOfFileBehaviorCSV() throws Exception {
final String[] codes = { "hello,\r\n\r\nworld,\r\n", "hello,\r\n\r\nworld,", "hello,\r\n\r\nworld,\"\"\r\n",
"hello,\r\n\r\nworld,\"\"", "hello,\r\n\r\nworld,\n", "hello,\r\n\r\nworld,",
"hello,\r\n\r\nworld,\"\"\n", "hello,\r\n\r\nworld,\"\"" };
"hello,\r\n\r\nworld,\"\"", "hello,\r\n\r\nworld,\n", "hello,\r\n\r\nworld,", "hello,\r\n\r\nworld,\"\"\n",
"hello,\r\n\r\nworld,\"\"" };
final String[][] res = { { "hello", "" }, // CSV format ignores empty lines
{ "world", "" } };
for (final String code : codes) {
@ -375,8 +373,8 @@ public class CSVParserTest {
@Test
public void testEndOfFileBehaviorExcel() throws Exception {
final String[] codes = { "hello,\r\n\r\nworld,\r\n", "hello,\r\n\r\nworld,", "hello,\r\n\r\nworld,\"\"\r\n",
"hello,\r\n\r\nworld,\"\"", "hello,\r\n\r\nworld,\n", "hello,\r\n\r\nworld,",
"hello,\r\n\r\nworld,\"\"\n", "hello,\r\n\r\nworld,\"\"" };
"hello,\r\n\r\nworld,\"\"", "hello,\r\n\r\nworld,\n", "hello,\r\n\r\nworld,", "hello,\r\n\r\nworld,\"\"\n",
"hello,\r\n\r\nworld,\"\"" };
final String[][] res = { { "hello", "" }, { "" }, // Excel format does not ignore empty lines
{ "world", "" } };
@ -394,8 +392,8 @@ public class CSVParserTest {
@Test
public void testExcelFormat1() throws IOException {
final String code = "value1,value2,value3,value4\r\na,b,c,d\r\n x,,," +
"\r\n\r\n\"\"\"hello\"\"\",\" \"\"world\"\"\",\"abc\ndef\",\r\n";
final String code = "value1,value2,value3,value4\r\na,b,c,d\r\n x,,,"
+ "\r\n\r\n\"\"\"hello\"\"\",\" \"\"world\"\"\",\"abc\ndef\",\r\n";
final String[][] res = { { "value1", "value2", "value3", "value4" }, { "a", "b", "c", "d" },
{ " x", "", "", "" }, { "" }, { "\"hello\"", " \"world\"", "abc\ndef", "" } };
try (final CSVParser parser = CSVParser.parse(code, CSVFormat.EXCEL)) {
@ -424,6 +422,7 @@ public class CSVParserTest {
/**
* Tests an exported Excel worksheet with a header row and rows that have more columns than the headers
*
* @throws Exception
*/
@Test
@ -688,7 +687,8 @@ public class CSVParserTest {
public void testHeaderMissing() throws Exception {
final Reader in = new StringReader("a,,c\n1,2,3\nx,y,z");
final Iterator<CSVRecord> records = CSVFormat.DEFAULT.withHeader().withAllowMissingColumnNames().parse(in).iterator();
final Iterator<CSVRecord> records = CSVFormat.DEFAULT.withHeader().withAllowMissingColumnNames().parse(in)
.iterator();
for (int i = 0; i < 2; i++) {
assertTrue(records.hasNext());
@ -727,11 +727,8 @@ public class CSVParserTest {
@Test
public void testHeadersWithNullColumnName() throws IOException {
final Reader in = new StringReader("header1,null,header3\n1,2,3\n4,5,6");
final Iterator<CSVRecord> records = CSVFormat.DEFAULT
.withHeader()
.withNullString("null")
.withAllowMissingColumnNames()
.parse(in).iterator();
final Iterator<CSVRecord> records = CSVFormat.DEFAULT.withHeader().withNullString("null")
.withAllowMissingColumnNames().parse(in).iterator();
final CSVRecord record = records.next();
// Expect the null header to be missing
assertEquals(Arrays.asList("header1", "header3"), record.getParser().getHeaderNames());
@ -926,12 +923,12 @@ public class CSVParserTest {
@Test
public void testNewCSVParserNullReaderFormat() {
assertThrows(IllegalArgumentException.class, () -> new CSVParser(null, CSVFormat.DEFAULT));
assertThrows(NullPointerException.class, () -> new CSVParser(null, CSVFormat.DEFAULT));
}
@Test
public void testNewCSVParserReaderNullFormat() {
assertThrows(IllegalArgumentException.class, () -> new CSVParser(new StringReader(""), null));
assertThrows(NullPointerException.class, () -> new CSVParser(new StringReader(""), null));
}
@Test
@ -948,81 +945,77 @@ public class CSVParserTest {
final CSVFormat format = CSVFormat.DEFAULT.withHeader("A", "B", "C", "D");
final Charset charset = StandardCharsets.UTF_8;
try(final CSVParser parser = CSVParser.parse(new InputStreamReader(url.openStream(), charset), format)) {
try (final CSVParser parser = CSVParser.parse(new InputStreamReader(url.openStream(), charset), format)) {
parseFully(parser);
}
try(final CSVParser parser = CSVParser.parse(new String(Files.readAllBytes(Paths.get(url.toURI())), charset), format)) {
try (final CSVParser parser = CSVParser.parse(new String(Files.readAllBytes(Paths.get(url.toURI())), charset),
format)) {
parseFully(parser);
}
try(final CSVParser parser = CSVParser.parse(new File(url.toURI()), charset, format)) {
try (final CSVParser parser = CSVParser.parse(new File(url.toURI()), charset, format)) {
parseFully(parser);
}
try(final CSVParser parser = CSVParser.parse(url.openStream(), charset, format)) {
try (final CSVParser parser = CSVParser.parse(url.openStream(), charset, format)) {
parseFully(parser);
}
try(final CSVParser parser = CSVParser.parse(Paths.get(url.toURI()), charset, format)) {
try (final CSVParser parser = CSVParser.parse(Paths.get(url.toURI()), charset, format)) {
parseFully(parser);
}
try(final CSVParser parser = CSVParser.parse(url, charset, format)) {
try (final CSVParser parser = CSVParser.parse(url, charset, format)) {
parseFully(parser);
}
try(final CSVParser parser = new CSVParser(new InputStreamReader(url.openStream(), charset), format)) {
try (final CSVParser parser = new CSVParser(new InputStreamReader(url.openStream(), charset), format)) {
parseFully(parser);
}
try(final CSVParser parser = new CSVParser(new InputStreamReader(url.openStream(), charset), format, /*characterOffset=*/0, /*recordNumber=*/1)) {
try (final CSVParser parser = new CSVParser(new InputStreamReader(url.openStream(), charset), format,
/* characterOffset= */0, /* recordNumber= */1)) {
parseFully(parser);
}
}
@Test
public void testParseFileNullFormat() {
assertThrows(
IllegalArgumentException.class,
assertThrows(NullPointerException.class,
() -> CSVParser.parse(new File("CSVFileParser/test.csv"), Charset.defaultCharset(), null));
}
@Test
public void testParseNullFileFormat() {
assertThrows(
IllegalArgumentException.class,
assertThrows(NullPointerException.class,
() -> CSVParser.parse((File) null, Charset.defaultCharset(), CSVFormat.DEFAULT));
}
@Test
public void testParseNullPathFormat() {
assertThrows(
IllegalArgumentException.class,
assertThrows(NullPointerException.class,
() -> CSVParser.parse((Path) null, Charset.defaultCharset(), CSVFormat.DEFAULT));
}
@Test
public void testParseNullStringFormat() {
assertThrows(IllegalArgumentException.class, () -> CSVParser.parse((String) null, CSVFormat.DEFAULT));
assertThrows(NullPointerException.class, () -> CSVParser.parse((String) null, CSVFormat.DEFAULT));
}
@Test
public void testParseNullUrlCharsetFormat() {
assertThrows(
IllegalArgumentException.class,
assertThrows(NullPointerException.class,
() -> CSVParser.parse((URL) null, Charset.defaultCharset(), CSVFormat.DEFAULT));
}
@Test
public void testParserUrlNullCharsetFormat() {
assertThrows(
IllegalArgumentException.class,
assertThrows(NullPointerException.class,
() -> CSVParser.parse(new URL("https://commons.apache.org"), null, CSVFormat.DEFAULT));
}
@Test
public void testParseStringNullFormat() {
assertThrows(IllegalArgumentException.class, () -> CSVParser.parse("csv data", (CSVFormat) null));
assertThrows(NullPointerException.class, () -> CSVParser.parse("csv data", (CSVFormat) null));
}
@Test
public void testParseUrlCharsetNullFormat() {
assertThrows(
IllegalArgumentException.class,
assertThrows(NullPointerException.class,
() -> CSVParser.parse(new URL("https://commons.apache.org"), Charset.defaultCharset(), null));
}

View File

@ -901,12 +901,12 @@ public class CSVPrinterTest {
@Test
public void testNewCsvPrinterAppendableNullFormat() {
assertThrows(IllegalArgumentException.class, () -> new CSVPrinter(new StringWriter(), null));
assertThrows(NullPointerException.class, () -> new CSVPrinter(new StringWriter(), null));
}
@Test
public void testNewCsvPrinterNullAppendableFormat() {
assertThrows(IllegalArgumentException.class, () -> new CSVPrinter(null, CSVFormat.DEFAULT));
assertThrows(NullPointerException.class, () -> new CSVPrinter(null, CSVFormat.DEFAULT));
}
@Test