mirror of
https://github.com/apache/commons-csv.git
synced 2025-02-17 07:26:32 +00:00
Replace org.apache.commons.csv.Assertions.notNull() with
Objects.requireNonNull().
This commit is contained in:
parent
bc2419915d
commit
adc4faa784
@ -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).
|
||||
|
||||
|
@ -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!");
|
||||
}
|
||||
}
|
||||
}
|
@ -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));
|
||||
|
@ -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;
|
||||
|
@ -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"));
|
||||
}
|
||||
}
|
@ -67,15 +67,15 @@ public class CSVParserTest {
|
||||
private static final String UTF_8_NAME = UTF_8.name();
|
||||
|
||||
private static final String CSV_INPUT = "a,b,c,d\n" + " a , b , 1 2 \n" + "\"foo baar\", b,\n"
|
||||
// + " \"foo\n,,\n\"\",,\n\\\"\",d,e\n";
|
||||
+ " \"foo\n,,\n\"\",,\n\"\"\",d,e\n"; // changed to use standard CSV escaping
|
||||
// + " \"foo\n,,\n\"\",,\n\\\"\",d,e\n";
|
||||
+ " \"foo\n,,\n\"\",,\n\"\"\",d,e\n"; // changed to use standard CSV escaping
|
||||
|
||||
private static final String CSV_INPUT_1 = "a,b,c,d";
|
||||
|
||||
private static final String CSV_INPUT_2 = "a,b,1 2";
|
||||
|
||||
private static final String[][] RESULT = { { "a", "b", "c", "d" }, { "a", "b", "1 2" }, { "foo baar", "b", "" },
|
||||
{ "foo\n,,\n\",,\n\"", "d", "e" } };
|
||||
{ "foo\n,,\n\",,\n\"", "d", "e" } };
|
||||
|
||||
private BOMInputStream createBOMInputStream(final String resource) throws IOException {
|
||||
final URL url = ClassLoader.getSystemClassLoader().getResource(resource);
|
||||
@ -96,28 +96,28 @@ public class CSVParserTest {
|
||||
// quote as the encapsulator.
|
||||
|
||||
final String code = "one,two,three\n" // 0
|
||||
+ "'',''\n" // 1) empty encapsulators
|
||||
+ "/',/'\n" // 2) single encapsulators
|
||||
+ "'/'','/''\n" // 3) single encapsulators encapsulated via escape
|
||||
+ "'''',''''\n" // 4) single encapsulators encapsulated via doubling
|
||||
+ "/,,/,\n" // 5) separator escaped
|
||||
+ "//,//\n" // 6) escape escaped
|
||||
+ "'//','//'\n" // 7) escape escaped in encapsulation
|
||||
+ " 8 , \"quoted \"\" /\" // string\" \n" // don't eat spaces
|
||||
+ "9, /\n \n" // escaped newline
|
||||
+ "";
|
||||
+ "'',''\n" // 1) empty encapsulators
|
||||
+ "/',/'\n" // 2) single encapsulators
|
||||
+ "'/'','/''\n" // 3) single encapsulators encapsulated via escape
|
||||
+ "'''',''''\n" // 4) single encapsulators encapsulated via doubling
|
||||
+ "/,,/,\n" // 5) separator escaped
|
||||
+ "//,//\n" // 6) escape escaped
|
||||
+ "'//','//'\n" // 7) escape escaped in encapsulation
|
||||
+ " 8 , \"quoted \"\" /\" // string\" \n" // don't eat spaces
|
||||
+ "9, /\n \n" // escaped newline
|
||||
+ "";
|
||||
final String[][] res = { { "one", "two", "three" }, // 0
|
||||
{ "", "" }, // 1
|
||||
{ "'", "'" }, // 2
|
||||
{ "'", "'" }, // 3
|
||||
{ "'", "'" }, // 4
|
||||
{ ",", "," }, // 5
|
||||
{ "/", "/" }, // 6
|
||||
{ "/", "/" }, // 7
|
||||
{ " 8 ", " \"quoted \"\" /\" / string\" " }, { "9", " \n " }, };
|
||||
{ "", "" }, // 1
|
||||
{ "'", "'" }, // 2
|
||||
{ "'", "'" }, // 3
|
||||
{ "'", "'" }, // 4
|
||||
{ ",", "," }, // 5
|
||||
{ "/", "/" }, // 6
|
||||
{ "/", "/" }, // 7
|
||||
{ " 8 ", " \"quoted \"\" /\" / string\" " }, { "9", " \n " }, };
|
||||
|
||||
final CSVFormat format = CSVFormat.newFormat(',').withQuote('\'').withRecordSeparator(CRLF).withEscape('/')
|
||||
.withIgnoreEmptyLines();
|
||||
.withIgnoreEmptyLines();
|
||||
|
||||
try (final CSVParser parser = CSVParser.parse(code, format)) {
|
||||
final List<CSVRecord> records = parser.getRecords();
|
||||
@ -135,16 +135,16 @@ public class CSVParserTest {
|
||||
// quote as the encapsulator.
|
||||
|
||||
final String code = "" + " , , \n" // 1)
|
||||
+ " \t , , \n" // 2)
|
||||
+ " // , /, , /,\n" // 3)
|
||||
+ "";
|
||||
+ " \t , , \n" // 2)
|
||||
+ " // , /, , /,\n" // 3)
|
||||
+ "";
|
||||
final String[][] res = { { " ", " ", " " }, // 1
|
||||
{ " \t ", " ", " " }, // 2
|
||||
{ " / ", " , ", " ," }, // 3
|
||||
{ " \t ", " ", " " }, // 2
|
||||
{ " / ", " , ", " ," }, // 3
|
||||
};
|
||||
|
||||
final CSVFormat format = CSVFormat.newFormat(',').withRecordSeparator(CRLF).withEscape('/')
|
||||
.withIgnoreEmptyLines();
|
||||
.withIgnoreEmptyLines();
|
||||
|
||||
try (final CSVParser parser = CSVParser.parse(code, format)) {
|
||||
final List<CSVRecord> records = parser.getRecords();
|
||||
@ -157,13 +157,13 @@ 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 (",")
|
||||
{ "a\\", "b" }, // a backslash must be returned
|
||||
{ "a\\\\,b" } // backslash in quotes only escapes a delimiter (",")
|
||||
{ "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 (",")
|
||||
{ "a\\", "b" }, // a backslash must be returned
|
||||
{ "a\\\\,b" } // backslash in quotes only escapes a delimiter (",")
|
||||
};
|
||||
try (final CSVParser parser = CSVParser.parse(code, CSVFormat.DEFAULT)) {
|
||||
final List<CSVRecord> records = parser.getRecords();
|
||||
@ -191,7 +191,7 @@ public class CSVParserTest {
|
||||
@Test
|
||||
public void testBOMInputStream_ParserWithInputStream() throws IOException {
|
||||
try (final BOMInputStream inputStream = createBOMInputStream("CSVFileParser/bom.csv");
|
||||
final CSVParser parser = CSVParser.parse(inputStream, UTF_8, CSVFormat.EXCEL.withHeader())) {
|
||||
final CSVParser parser = CSVParser.parse(inputStream, UTF_8, CSVFormat.EXCEL.withHeader())) {
|
||||
for (final CSVRecord record : parser) {
|
||||
final String string = record.get("Date");
|
||||
assertNotNull(string);
|
||||
@ -203,7 +203,7 @@ public class CSVParserTest {
|
||||
@Test
|
||||
public void testBOMInputStream_ParserWithReader() throws IOException {
|
||||
try (final Reader reader = new InputStreamReader(createBOMInputStream("CSVFileParser/bom.csv"), UTF_8_NAME);
|
||||
final CSVParser parser = new CSVParser(reader, CSVFormat.EXCEL.withHeader())) {
|
||||
final CSVParser parser = new CSVParser(reader, CSVFormat.EXCEL.withHeader())) {
|
||||
for (final CSVRecord record : parser) {
|
||||
final String string = record.get("Date");
|
||||
assertNotNull(string);
|
||||
@ -215,7 +215,7 @@ public class CSVParserTest {
|
||||
@Test
|
||||
public void testBOMInputStream_parseWithReader() throws IOException {
|
||||
try (final Reader reader = new InputStreamReader(createBOMInputStream("CSVFileParser/bom.csv"), UTF_8_NAME);
|
||||
final CSVParser parser = CSVParser.parse(reader, CSVFormat.EXCEL.withHeader())) {
|
||||
final CSVParser parser = CSVParser.parse(reader, CSVFormat.EXCEL.withHeader())) {
|
||||
for (final CSVRecord record : parser) {
|
||||
final String string = record.get("Date");
|
||||
assertNotNull(string);
|
||||
@ -266,10 +266,10 @@ public class CSVParserTest {
|
||||
@Test
|
||||
public void testDefaultFormat() throws IOException {
|
||||
final String code = "" + "a,b#\n" // 1)
|
||||
+ "\"\n\",\" \",#\n" // 2)
|
||||
+ "#,\"\"\n" // 3)
|
||||
+ "# Final comment\n"// 4)
|
||||
;
|
||||
+ "\"\n\",\" \",#\n" // 2)
|
||||
+ "#,\"\"\n" // 3)
|
||||
+ "# Final comment\n"// 4)
|
||||
;
|
||||
final String[][] res = { { "a", "b#" }, { "\n", " ", "#" }, { "#", "" }, { "# Final comment" } };
|
||||
|
||||
CSVFormat format = CSVFormat.DEFAULT;
|
||||
@ -293,10 +293,8 @@ public class CSVParserTest {
|
||||
|
||||
@Test
|
||||
public void testDuplicateHeadersNotAllowed() {
|
||||
assertThrows(
|
||||
IllegalArgumentException.class,
|
||||
() -> CSVParser.parse("a,b,a\n1,2,3\nx,y,z",
|
||||
CSVFormat.DEFAULT.withHeader().withAllowDuplicateHeaderNames(false)));
|
||||
assertThrows(IllegalArgumentException.class, () -> CSVParser.parse("a,b,a\n1,2,3\nx,y,z",
|
||||
CSVFormat.DEFAULT.withHeader().withAllowDuplicateHeaderNames(false)));
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -340,7 +338,7 @@ public class CSVParserTest {
|
||||
public void testEmptyLineBehaviorExcel() throws Exception {
|
||||
final String[] codes = { "hello,\r\n\r\n\r\n", "hello,\n\n\n", "hello,\"\"\r\n\r\n\r\n", "hello,\"\"\n\n\n" };
|
||||
final String[][] res = { { "hello", "" }, { "" }, // Excel format does not ignore empty lines
|
||||
{ "" } };
|
||||
{ "" } };
|
||||
for (final String code : codes) {
|
||||
try (final CSVParser parser = CSVParser.parse(code, CSVFormat.EXCEL)) {
|
||||
final List<CSVRecord> records = parser.getRecords();
|
||||
@ -356,10 +354,10 @@ 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", "" } };
|
||||
{ "world", "" } };
|
||||
for (final String code : codes) {
|
||||
try (final CSVParser parser = CSVParser.parse(code, CSVFormat.DEFAULT)) {
|
||||
final List<CSVRecord> records = parser.getRecords();
|
||||
@ -375,10 +373,10 @@ 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", "" } };
|
||||
{ "world", "" } };
|
||||
|
||||
for (final String code : codes) {
|
||||
try (final CSVParser parser = CSVParser.parse(code, CSVFormat.EXCEL)) {
|
||||
@ -394,10 +392,10 @@ 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", "" } };
|
||||
{ " x", "", "", "" }, { "" }, { "\"hello\"", " \"world\"", "abc\ndef", "" } };
|
||||
try (final CSVParser parser = CSVParser.parse(code, CSVFormat.EXCEL)) {
|
||||
final List<CSVRecord> records = parser.getRecords();
|
||||
assertEquals(res.length, records.size());
|
||||
@ -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
|
||||
@ -485,7 +484,7 @@ public class CSVParserTest {
|
||||
@Test
|
||||
public void testGetHeaderMap() throws Exception {
|
||||
try (final CSVParser parser = CSVParser.parse("a,b,c\n1,2,3\nx,y,z",
|
||||
CSVFormat.DEFAULT.withHeader("A", "B", "C"))) {
|
||||
CSVFormat.DEFAULT.withHeader("A", "B", "C"))) {
|
||||
final Map<String, Integer> headerMap = parser.getHeaderMap();
|
||||
final Iterator<String> columnNames = headerMap.keySet().iterator();
|
||||
// Headers are iterated in column order.
|
||||
@ -575,7 +574,7 @@ public class CSVParserTest {
|
||||
public void testGetOneLineOneParser() throws IOException {
|
||||
final CSVFormat format = CSVFormat.DEFAULT;
|
||||
try (final PipedWriter writer = new PipedWriter();
|
||||
final CSVParser parser = new CSVParser(new PipedReader(writer), format)) {
|
||||
final CSVParser parser = new CSVParser(new PipedReader(writer), format)) {
|
||||
writer.append(CSV_INPUT_1);
|
||||
writer.append(format.getRecordSeparator());
|
||||
final CSVRecord record1 = parser.nextRecord();
|
||||
@ -627,8 +626,8 @@ public class CSVParserTest {
|
||||
@Test
|
||||
public void testGetRecordWithMultiLineValues() throws Exception {
|
||||
try (final CSVParser parser = CSVParser.parse(
|
||||
"\"a\r\n1\",\"a\r\n2\"" + CRLF + "\"b\r\n1\",\"b\r\n2\"" + CRLF + "\"c\r\n1\",\"c\r\n2\"",
|
||||
CSVFormat.DEFAULT.withRecordSeparator(CRLF))) {
|
||||
"\"a\r\n1\",\"a\r\n2\"" + CRLF + "\"b\r\n1\",\"b\r\n2\"" + CRLF + "\"c\r\n1\",\"c\r\n2\"",
|
||||
CSVFormat.DEFAULT.withRecordSeparator(CRLF))) {
|
||||
CSVRecord record;
|
||||
assertEquals(0, parser.getRecordNumber());
|
||||
assertEquals(0, parser.getCurrentLineNumber());
|
||||
@ -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());
|
||||
@ -720,18 +720,15 @@ public class CSVParserTest {
|
||||
|
||||
@Test
|
||||
public void testHeadersMissingOneColumnException() throws Exception {
|
||||
final Reader in = new StringReader("a,,c,d,e\n1,2,3,4,5\nv,w,x,y,z");
|
||||
assertThrows(IllegalArgumentException.class, () -> CSVFormat.DEFAULT.withHeader().parse(in).iterator());
|
||||
final Reader in = new StringReader("a,,c,d,e\n1,2,3,4,5\nv,w,x,y,z");
|
||||
assertThrows(IllegalArgumentException.class, () -> CSVFormat.DEFAULT.withHeader().parse(in).iterator());
|
||||
}
|
||||
|
||||
@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());
|
||||
@ -742,7 +739,7 @@ public class CSVParserTest {
|
||||
public void testIgnoreCaseHeaderMapping() throws Exception {
|
||||
final Reader reader = new StringReader("1,2,3");
|
||||
final Iterator<CSVRecord> records = CSVFormat.DEFAULT.withHeader("One", "TWO", "three").withIgnoreHeaderCase()
|
||||
.parse(reader).iterator();
|
||||
.parse(reader).iterator();
|
||||
final CSVRecord record = records.next();
|
||||
assertEquals("1", record.get("one"));
|
||||
assertEquals("2", record.get("two"));
|
||||
@ -856,7 +853,7 @@ public class CSVParserTest {
|
||||
public void testMappedButNotSetAsOutlook2007ContactExport() throws Exception {
|
||||
final Reader in = new StringReader("a,b,c\n1,2\nx,y,z");
|
||||
final Iterator<CSVRecord> records = CSVFormat.DEFAULT.withHeader("A", "B", "C").withSkipHeaderRecord().parse(in)
|
||||
.iterator();
|
||||
.iterator();
|
||||
CSVRecord record;
|
||||
|
||||
// 1st record
|
||||
@ -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,82 +945,78 @@ 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,
|
||||
() -> CSVParser.parse(new File("CSVFileParser/test.csv"), Charset.defaultCharset(), null));
|
||||
assertThrows(NullPointerException.class,
|
||||
() -> CSVParser.parse(new File("CSVFileParser/test.csv"), Charset.defaultCharset(), null));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParseNullFileFormat() {
|
||||
assertThrows(
|
||||
IllegalArgumentException.class,
|
||||
() -> CSVParser.parse((File) null, Charset.defaultCharset(), CSVFormat.DEFAULT));
|
||||
assertThrows(NullPointerException.class,
|
||||
() -> CSVParser.parse((File) null, Charset.defaultCharset(), CSVFormat.DEFAULT));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParseNullPathFormat() {
|
||||
assertThrows(
|
||||
IllegalArgumentException.class,
|
||||
() -> CSVParser.parse((Path) null, Charset.defaultCharset(), CSVFormat.DEFAULT));
|
||||
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,
|
||||
() -> CSVParser.parse((URL) null, Charset.defaultCharset(), CSVFormat.DEFAULT));
|
||||
assertThrows(NullPointerException.class,
|
||||
() -> CSVParser.parse((URL) null, Charset.defaultCharset(), CSVFormat.DEFAULT));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParserUrlNullCharsetFormat() {
|
||||
assertThrows(
|
||||
IllegalArgumentException.class,
|
||||
() -> CSVParser.parse(new URL("https://commons.apache.org"), null, CSVFormat.DEFAULT));
|
||||
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,
|
||||
() -> CSVParser.parse(new URL("https://commons.apache.org"), Charset.defaultCharset(), null));
|
||||
assertThrows(NullPointerException.class,
|
||||
() -> CSVParser.parse(new URL("https://commons.apache.org"), Charset.defaultCharset(), null));
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -1094,7 +1087,7 @@ public class CSVParserTest {
|
||||
public void testSkipHeaderOverrideDuplicateHeaders() throws Exception {
|
||||
final Reader in = new StringReader("a,a,a\n1,2,3\nx,y,z");
|
||||
final Iterator<CSVRecord> records = CSVFormat.DEFAULT.withHeader("X", "Y", "Z").withSkipHeaderRecord().parse(in)
|
||||
.iterator();
|
||||
.iterator();
|
||||
final CSVRecord record = records.next();
|
||||
assertEquals("1", record.get("X"));
|
||||
assertEquals("2", record.get("Y"));
|
||||
@ -1105,7 +1098,7 @@ public class CSVParserTest {
|
||||
public void testSkipSetAltHeaders() throws Exception {
|
||||
final Reader in = new StringReader("a,b,c\n1,2,3\nx,y,z");
|
||||
final Iterator<CSVRecord> records = CSVFormat.DEFAULT.withHeader("X", "Y", "Z").withSkipHeaderRecord().parse(in)
|
||||
.iterator();
|
||||
.iterator();
|
||||
final CSVRecord record = records.next();
|
||||
assertEquals("1", record.get("X"));
|
||||
assertEquals("2", record.get("Y"));
|
||||
@ -1116,7 +1109,7 @@ public class CSVParserTest {
|
||||
public void testSkipSetHeader() throws Exception {
|
||||
final Reader in = new StringReader("a,b,c\n1,2,3\nx,y,z");
|
||||
final Iterator<CSVRecord> records = CSVFormat.DEFAULT.withHeader("a", "b", "c").withSkipHeaderRecord().parse(in)
|
||||
.iterator();
|
||||
.iterator();
|
||||
final CSVRecord record = records.next();
|
||||
assertEquals("1", record.get("a"));
|
||||
assertEquals("2", record.get("b"));
|
||||
@ -1127,9 +1120,9 @@ public class CSVParserTest {
|
||||
@Disabled
|
||||
public void testStartWithEmptyLinesThenHeaders() throws Exception {
|
||||
final String[] codes = { "\r\n\r\n\r\nhello,\r\n\r\n\r\n", "hello,\n\n\n", "hello,\"\"\r\n\r\n\r\n",
|
||||
"hello,\"\"\n\n\n" };
|
||||
"hello,\"\"\n\n\n" };
|
||||
final String[][] res = { { "hello", "" }, { "" }, // Excel format does not ignore empty lines
|
||||
{ "" } };
|
||||
{ "" } };
|
||||
for (final String code : codes) {
|
||||
try (final CSVParser parser = CSVParser.parse(code, CSVFormat.EXCEL)) {
|
||||
final List<CSVRecord> records = parser.getRecords();
|
||||
@ -1146,7 +1139,7 @@ public class CSVParserTest {
|
||||
public void testTrailingDelimiter() throws Exception {
|
||||
final Reader in = new StringReader("a,a,a,\n\"1\",\"2\",\"3\",\nx,y,z,");
|
||||
final Iterator<CSVRecord> records = CSVFormat.DEFAULT.withHeader("X", "Y", "Z").withSkipHeaderRecord()
|
||||
.withTrailingDelimiter().parse(in).iterator();
|
||||
.withTrailingDelimiter().parse(in).iterator();
|
||||
final CSVRecord record = records.next();
|
||||
assertEquals("1", record.get("X"));
|
||||
assertEquals("2", record.get("Y"));
|
||||
@ -1158,7 +1151,7 @@ public class CSVParserTest {
|
||||
public void testTrim() throws Exception {
|
||||
final Reader in = new StringReader("a,a,a\n\" 1 \",\" 2 \",\" 3 \"\nx,y,z");
|
||||
final Iterator<CSVRecord> records = CSVFormat.DEFAULT.withHeader("X", "Y", "Z").withSkipHeaderRecord()
|
||||
.withTrim().parse(in).iterator();
|
||||
.withTrim().parse(in).iterator();
|
||||
final CSVRecord record = records.next();
|
||||
assertEquals("1", record.get("X"));
|
||||
assertEquals("2", record.get("Y"));
|
||||
@ -1188,7 +1181,7 @@ public class CSVParserTest {
|
||||
|
||||
private void validateLineNumbers(final String lineSeparator) throws IOException {
|
||||
try (final CSVParser parser = CSVParser.parse("a" + lineSeparator + "b" + lineSeparator + "c",
|
||||
CSVFormat.DEFAULT.withRecordSeparator(lineSeparator))) {
|
||||
CSVFormat.DEFAULT.withRecordSeparator(lineSeparator))) {
|
||||
assertEquals(0, parser.getCurrentLineNumber());
|
||||
assertNotNull(parser.nextRecord());
|
||||
assertEquals(1, parser.getCurrentLineNumber());
|
||||
@ -1205,7 +1198,7 @@ public class CSVParserTest {
|
||||
|
||||
private void validateRecordNumbers(final String lineSeparator) throws IOException {
|
||||
try (final CSVParser parser = CSVParser.parse("a" + lineSeparator + "b" + lineSeparator + "c",
|
||||
CSVFormat.DEFAULT.withRecordSeparator(lineSeparator))) {
|
||||
CSVFormat.DEFAULT.withRecordSeparator(lineSeparator))) {
|
||||
CSVRecord record;
|
||||
assertEquals(0, parser.getRecordNumber());
|
||||
assertNotNull(record = parser.nextRecord());
|
||||
@ -1226,11 +1219,11 @@ public class CSVParserTest {
|
||||
final String nl = lineSeparator; // used as linebreak in values for better distinction
|
||||
|
||||
final String code = "a,b,c" + lineSeparator + "1,2,3" + lineSeparator +
|
||||
// to see if recordPosition correctly points to the enclosing quote
|
||||
"'A" + nl + "A','B" + nl + "B',CC" + lineSeparator +
|
||||
// unicode test... not very relevant while operating on strings instead of bytes, but for
|
||||
// completeness...
|
||||
"\u00c4,\u00d6,\u00dc" + lineSeparator + "EOF,EOF,EOF";
|
||||
// to see if recordPosition correctly points to the enclosing quote
|
||||
"'A" + nl + "A','B" + nl + "B',CC" + lineSeparator +
|
||||
// unicode test... not very relevant while operating on strings instead of bytes, but for
|
||||
// completeness...
|
||||
"\u00c4,\u00d6,\u00dc" + lineSeparator + "EOF,EOF,EOF";
|
||||
|
||||
final CSVFormat format = CSVFormat.newFormat(',').withQuote('\'').withRecordSeparator(lineSeparator);
|
||||
CSVParser parser = CSVParser.parse(code, format);
|
||||
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user