Test parsing an empty file.
- Sort members. - Better constant name. - Move RAT check to plugin management section to de-duplicate the exclusion list.
This commit is contained in:
parent
d608f55f0d
commit
f26a22a9a6
84
pom.xml
84
pom.xml
|
@ -228,6 +228,38 @@
|
||||||
</rulesets>
|
</rulesets>
|
||||||
</configuration>
|
</configuration>
|
||||||
</plugin>
|
</plugin>
|
||||||
|
<!-- We need to add our test data files to rat exclusions -->
|
||||||
|
<!-- Needed for command-line access, e.g mvn apache-rat:rat and mvn apache-rat:check -->
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.rat</groupId>
|
||||||
|
<artifactId>apache-rat-plugin</artifactId>
|
||||||
|
<!-- Should agree with config in reporting section -->
|
||||||
|
<configuration>
|
||||||
|
<excludes>
|
||||||
|
<!-- These files are used as test data and test result specifications. -->
|
||||||
|
<exclude>src/test/resources/org/apache/commons/csv/empty.txt</exclude>
|
||||||
|
<exclude>src/test/resources/org/apache/commons/csv/csv-167/sample1.csv</exclude>
|
||||||
|
<exclude>src/test/resources/org/apache/commons/csv/CSV-198/optd_por_public.csv</exclude>
|
||||||
|
<exclude>src/test/resources/org/apache/commons/csv/CSV-213/999751170.patch.csv</exclude>
|
||||||
|
<exclude>src/test/resources/org/apache/commons/csv/CSVFileParser/bom.csv</exclude>
|
||||||
|
<exclude>src/test/resources/org/apache/commons/csv/CSVFileParser/test.csv</exclude>
|
||||||
|
<exclude>src/test/resources/org/apache/commons/csv/CSVFileParser/test_default.txt</exclude>
|
||||||
|
<exclude>src/test/resources/org/apache/commons/csv/CSVFileParser/test_default_comment.txt</exclude>
|
||||||
|
<exclude>src/test/resources/org/apache/commons/csv/CSVFileParser/test_rfc4180.txt</exclude>
|
||||||
|
<exclude>src/test/resources/org/apache/commons/csv/CSVFileParser/test_rfc4180_trim.txt</exclude>
|
||||||
|
<exclude>src/test/resources/org/apache/commons/csv/CSVFileParser/testCSV85.csv</exclude>
|
||||||
|
<exclude>src/test/resources/org/apache/commons/csv/CSVFileParser/testCSV85_default.txt</exclude>
|
||||||
|
<exclude>src/test/resources/org/apache/commons/csv/CSVFileParser/testCSV85_ignoreEmpty.txt</exclude>
|
||||||
|
<!-- The ferc.gov files are included discussion in https://issues.apache.org/jira/browse/LEGAL-175. -->
|
||||||
|
<exclude>src/test/resources/org/apache/commons/csv/ferc.gov/contract.txt</exclude>
|
||||||
|
<exclude>src/test/resources/org/apache/commons/csv/ferc.gov/transaction.txt</exclude>
|
||||||
|
<exclude>src/test/resources/**/*.bin</exclude>
|
||||||
|
<exclude>src/test/resources/org/apache/commons/csv/CSV-259/sample.txt</exclude>
|
||||||
|
<exclude>src/test/resources/org/apache/commons/csv/CSVFileParser/testCSV246.csv</exclude>
|
||||||
|
<exclude>src/test/resources/org/apache/commons/csv/CSVFileParser/testCSV246_checkWithNoComment.txt</exclude>
|
||||||
|
</excludes>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
</plugins>
|
</plugins>
|
||||||
</pluginManagement>
|
</pluginManagement>
|
||||||
<plugins>
|
<plugins>
|
||||||
|
@ -277,36 +309,9 @@
|
||||||
<artifactId>maven-pmd-plugin</artifactId>
|
<artifactId>maven-pmd-plugin</artifactId>
|
||||||
</plugin>
|
</plugin>
|
||||||
|
|
||||||
<!-- We need to add our test data files to rat exclusions -->
|
|
||||||
<!-- Needed for command-line access, e.g mvn apache-rat:rat and mvn apache-rat:check -->
|
|
||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.apache.rat</groupId>
|
<groupId>org.apache.rat</groupId>
|
||||||
<artifactId>apache-rat-plugin</artifactId>
|
<artifactId>apache-rat-plugin</artifactId>
|
||||||
<!-- Should agree with config in reporting section -->
|
|
||||||
<configuration>
|
|
||||||
<excludes>
|
|
||||||
<!-- These files are used as test data and test result specifications. -->
|
|
||||||
<exclude>src/test/resources/org/apache/commons/csv/csv-167/sample1.csv</exclude>
|
|
||||||
<exclude>src/test/resources/org/apache/commons/csv/CSV-198/optd_por_public.csv</exclude>
|
|
||||||
<exclude>src/test/resources/org/apache/commons/csv/CSV-213/999751170.patch.csv</exclude>
|
|
||||||
<exclude>src/test/resources/org/apache/commons/csv/CSVFileParser/bom.csv</exclude>
|
|
||||||
<exclude>src/test/resources/org/apache/commons/csv/CSVFileParser/test.csv</exclude>
|
|
||||||
<exclude>src/test/resources/org/apache/commons/csv/CSVFileParser/test_default.txt</exclude>
|
|
||||||
<exclude>src/test/resources/org/apache/commons/csv/CSVFileParser/test_default_comment.txt</exclude>
|
|
||||||
<exclude>src/test/resources/org/apache/commons/csv/CSVFileParser/test_rfc4180.txt</exclude>
|
|
||||||
<exclude>src/test/resources/org/apache/commons/csv/CSVFileParser/test_rfc4180_trim.txt</exclude>
|
|
||||||
<exclude>src/test/resources/org/apache/commons/csv/CSVFileParser/testCSV85.csv</exclude>
|
|
||||||
<exclude>src/test/resources/org/apache/commons/csv/CSVFileParser/testCSV85_default.txt</exclude>
|
|
||||||
<exclude>src/test/resources/org/apache/commons/csv/CSVFileParser/testCSV85_ignoreEmpty.txt</exclude>
|
|
||||||
<!-- The ferc.gov files are included discussion in https://issues.apache.org/jira/browse/LEGAL-175. -->
|
|
||||||
<exclude>src/test/resources/org/apache/commons/csv/ferc.gov/contract.txt</exclude>
|
|
||||||
<exclude>src/test/resources/org/apache/commons/csv/ferc.gov/transaction.txt</exclude>
|
|
||||||
<exclude>src/test/resources/**/*.bin</exclude>
|
|
||||||
<exclude>src/test/resources/org/apache/commons/csv/CSV-259/sample.txt</exclude>
|
|
||||||
<exclude>src/test/resources/org/apache/commons/csv/CSVFileParser/testCSV246.csv</exclude>
|
|
||||||
<exclude>src/test/resources/org/apache/commons/csv/CSVFileParser/testCSV246_checkWithNoComment.txt</exclude>
|
|
||||||
</excludes>
|
|
||||||
</configuration>
|
|
||||||
</plugin>
|
</plugin>
|
||||||
|
|
||||||
</plugins>
|
</plugins>
|
||||||
|
@ -384,34 +389,9 @@
|
||||||
</tagListOptions>
|
</tagListOptions>
|
||||||
</configuration>
|
</configuration>
|
||||||
</plugin>
|
</plugin>
|
||||||
<!-- We need to add our test data files to rat exclusions -->
|
|
||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.apache.rat</groupId>
|
<groupId>org.apache.rat</groupId>
|
||||||
<artifactId>apache-rat-plugin</artifactId>
|
<artifactId>apache-rat-plugin</artifactId>
|
||||||
<version>${commons.rat.version}</version>
|
|
||||||
<!-- Should agree with config in build section -->
|
|
||||||
<configuration>
|
|
||||||
<excludes>
|
|
||||||
<exclude>src/test/resources/csv-167/sample1.csv</exclude>
|
|
||||||
<exclude>src/test/resources/CSV-198/optd_por_public.csv</exclude>
|
|
||||||
<exclude>src/test/resources/CSV-213/999751170.patch.csv</exclude>
|
|
||||||
<exclude>src/test/resources/CSVFileParser/bom.csv</exclude>
|
|
||||||
<exclude>src/test/resources/CSVFileParser/test.csv</exclude>
|
|
||||||
<exclude>src/test/resources/CSVFileParser/test_default.txt</exclude>
|
|
||||||
<exclude>src/test/resources/CSVFileParser/test_default_comment.txt</exclude>
|
|
||||||
<exclude>src/test/resources/CSVFileParser/test_rfc4180.txt</exclude>
|
|
||||||
<exclude>src/test/resources/CSVFileParser/test_rfc4180_trim.txt</exclude>
|
|
||||||
<exclude>src/test/resources/CSVFileParser/testCSV85.csv</exclude>
|
|
||||||
<exclude>src/test/resources/CSVFileParser/testCSV85_default.txt</exclude>
|
|
||||||
<exclude>src/test/resources/CSVFileParser/testCSV85_ignoreEmpty.txt</exclude>
|
|
||||||
<exclude>src/test/resources/ferc.gov/contract.txt</exclude>
|
|
||||||
<exclude>src/test/resources/ferc.gov/transaction.txt</exclude>
|
|
||||||
<exclude>src/test/resources/**/*.bin</exclude>
|
|
||||||
<exclude>src/test/resources/CSV-259/sample.txt</exclude>
|
|
||||||
<exclude>src/test/resources/CSVFileParser/testCSV246.csv</exclude>
|
|
||||||
<exclude>src/test/resources/CSVFileParser/testCSV246_checkWithNoComment.txt</exclude>
|
|
||||||
</excludes>
|
|
||||||
</configuration>
|
|
||||||
</plugin>
|
</plugin>
|
||||||
</plugins>
|
</plugins>
|
||||||
</reporting>
|
</reporting>
|
||||||
|
|
|
@ -25,11 +25,11 @@ import static org.junit.jupiter.api.Assertions.fail;
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileReader;
|
import java.io.FileReader;
|
||||||
import java.io.FilenameFilter;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.nio.charset.Charset;
|
import java.nio.charset.Charset;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.nio.file.Files;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
|
@ -41,7 +41,7 @@ import org.junit.jupiter.params.provider.MethodSource;
|
||||||
*/
|
*/
|
||||||
public class CSVFileParserTest {
|
public class CSVFileParserTest {
|
||||||
|
|
||||||
private static final File BASE = new File("src/test/resources/org/apache/commons/csv/CSVFileParser");
|
private static final File BASE_DIR = new File("src/test/resources/org/apache/commons/csv/CSVFileParser");
|
||||||
|
|
||||||
private String readTestData(final BufferedReader reader) throws IOException {
|
private String readTestData(final BufferedReader reader) throws IOException {
|
||||||
String line;
|
String line;
|
||||||
|
@ -52,8 +52,7 @@ public class CSVFileParserTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Stream<File> generateData() {
|
public static Stream<File> generateData() {
|
||||||
final FilenameFilter fileNameFilter = (dir, name) -> name.startsWith("test") && name.endsWith(".txt");
|
final File[] files = BASE_DIR.listFiles((dir, name) -> name.startsWith("test") && name.endsWith(".txt"));
|
||||||
final File[] files = BASE.listFiles(fileNameFilter);
|
|
||||||
return files != null ? Stream.of(files) : Stream.empty();
|
return files != null ? Stream.of(files) : Stream.empty();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -88,7 +87,7 @@ public class CSVFileParserTest {
|
||||||
|
|
||||||
// Now parse the file and compare against the expected results
|
// Now parse the file and compare against the expected results
|
||||||
// We use a buffered reader internally so no need to create one here.
|
// We use a buffered reader internally so no need to create one here.
|
||||||
try (final CSVParser parser = CSVParser.parse(new File(BASE, split[0]), Charset.defaultCharset(), format)) {
|
try (final CSVParser parser = CSVParser.parse(new File(BASE_DIR, split[0]), Charset.defaultCharset(), format)) {
|
||||||
for (final CSVRecord record : parser) {
|
for (final CSVRecord record : parser) {
|
||||||
String parsed = Arrays.toString(record.values());
|
String parsed = Arrays.toString(record.values());
|
||||||
final String comment = record.getComment();
|
final String comment = record.getComment();
|
||||||
|
|
|
@ -75,8 +75,8 @@ public class CSVParserTest {
|
||||||
|
|
||||||
private static final String CSV_INPUT_2 = "a,b,1 2";
|
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", "" },
|
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 {
|
private BOMInputStream createBOMInputStream(final String resource) throws IOException {
|
||||||
final URL url = ClassLoader.getSystemClassLoader().getResource(resource);
|
final URL url = ClassLoader.getSystemClassLoader().getResource(resource);
|
||||||
|
@ -89,84 +89,6 @@ public class CSVParserTest {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testParseWithDelimiterWithQuote() throws IOException {
|
|
||||||
final String source = "'a,b,c',xyz";
|
|
||||||
final CSVFormat csvFormat = CSVFormat.DEFAULT.withQuote('\'');
|
|
||||||
try (CSVParser csvParser = csvFormat.parse(new StringReader(source))) {
|
|
||||||
final CSVRecord csvRecord = csvParser.nextRecord();
|
|
||||||
assertEquals("a,b,c", csvRecord.get(0));
|
|
||||||
assertEquals("xyz", csvRecord.get(1));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testParseWithDelimiterStringWithQuote() throws IOException {
|
|
||||||
final String source = "'a[|]b[|]c'[|]xyz\r\nabc[abc][|]xyz";
|
|
||||||
final CSVFormat csvFormat = CSVFormat.DEFAULT.builder().setDelimiter("[|]").setQuote('\'').build();
|
|
||||||
try (CSVParser csvParser = csvFormat.parse(new StringReader(source))) {
|
|
||||||
CSVRecord csvRecord = csvParser.nextRecord();
|
|
||||||
assertEquals("a[|]b[|]c", csvRecord.get(0));
|
|
||||||
assertEquals("xyz", csvRecord.get(1));
|
|
||||||
csvRecord = csvParser.nextRecord();
|
|
||||||
assertEquals("abc[abc]", csvRecord.get(0));
|
|
||||||
assertEquals("xyz", csvRecord.get(1));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testParseWithDelimiterWithEscape() throws IOException {
|
|
||||||
final String source = "a!,b!,c,xyz";
|
|
||||||
final CSVFormat csvFormat = CSVFormat.DEFAULT.withEscape('!');
|
|
||||||
try (CSVParser csvParser = csvFormat.parse(new StringReader(source))) {
|
|
||||||
final CSVRecord csvRecord = csvParser.nextRecord();
|
|
||||||
assertEquals("a,b,c", csvRecord.get(0));
|
|
||||||
assertEquals("xyz", csvRecord.get(1));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testParseWithDelimiterStringWithEscape() throws IOException {
|
|
||||||
final String source = "a![!|!]b![|]c[|]xyz\r\nabc[abc][|]xyz";
|
|
||||||
final CSVFormat csvFormat = CSVFormat.DEFAULT.builder().setDelimiter("[|]").setEscape('!').build();
|
|
||||||
try (CSVParser csvParser = csvFormat.parse(new StringReader(source))) {
|
|
||||||
CSVRecord csvRecord = csvParser.nextRecord();
|
|
||||||
assertEquals("a[|]b![|]c", csvRecord.get(0));
|
|
||||||
assertEquals("xyz", csvRecord.get(1));
|
|
||||||
csvRecord = csvParser.nextRecord();
|
|
||||||
assertEquals("abc[abc]", csvRecord.get(0));
|
|
||||||
assertEquals("xyz", csvRecord.get(1));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testParseWithQuoteWithEscape() throws IOException {
|
|
||||||
final String source = "'a?,b?,c?d',xyz";
|
|
||||||
final CSVFormat csvFormat = CSVFormat.DEFAULT.withQuote('\'').withEscape('?');
|
|
||||||
try (CSVParser csvParser = csvFormat.parse(new StringReader(source))) {
|
|
||||||
final CSVRecord csvRecord = csvParser.nextRecord();
|
|
||||||
assertEquals("a,b,c?d", csvRecord.get(0));
|
|
||||||
assertEquals("xyz", csvRecord.get(1));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testParseWithQuoteThrowsException() {
|
|
||||||
final CSVFormat csvFormat = CSVFormat.DEFAULT.withQuote('\'');
|
|
||||||
assertThrows(IOException.class, () -> csvFormat.parse(new StringReader("'a,b,c','")).nextRecord());
|
|
||||||
assertThrows(IOException.class, () -> csvFormat.parse(new StringReader("'a,b,c'abc,xyz")).nextRecord());
|
|
||||||
assertThrows(IOException.class, () -> csvFormat.parse(new StringReader("'abc'a,b,c',xyz")).nextRecord());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testNotValueCSV() throws IOException {
|
|
||||||
final String source = "#";
|
|
||||||
final CSVFormat csvFormat = CSVFormat.DEFAULT.withCommentMarker('#');
|
|
||||||
final CSVParser csvParser = csvFormat.parse(new StringReader(source));
|
|
||||||
final CSVRecord csvRecord = csvParser.nextRecord();
|
|
||||||
assertNull(csvRecord);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testBackslashEscaping() throws IOException {
|
public void testBackslashEscaping() throws IOException {
|
||||||
|
|
||||||
|
@ -185,15 +107,15 @@ public class CSVParserTest {
|
||||||
+ " 8 , \"quoted \"\" /\" // string\" \n" // don't eat spaces
|
+ " 8 , \"quoted \"\" /\" // string\" \n" // don't eat spaces
|
||||||
+ "9, /\n \n" // escaped newline
|
+ "9, /\n \n" // escaped newline
|
||||||
+ "";
|
+ "";
|
||||||
final String[][] res = { { "one", "two", "three" }, // 0
|
final String[][] res = {{"one", "two", "three"}, // 0
|
||||||
{ "", "" }, // 1
|
{"", ""}, // 1
|
||||||
{ "'", "'" }, // 2
|
{"'", "'"}, // 2
|
||||||
{ "'", "'" }, // 3
|
{"'", "'"}, // 3
|
||||||
{ "'", "'" }, // 4
|
{"'", "'"}, // 4
|
||||||
{ ",", "," }, // 5
|
{",", ","}, // 5
|
||||||
{ "/", "/" }, // 6
|
{"/", "/"}, // 6
|
||||||
{ "/", "/" }, // 7
|
{"/", "/"}, // 7
|
||||||
{ " 8 ", " \"quoted \"\" /\" / string\" " }, { "9", " \n " }, };
|
{" 8 ", " \"quoted \"\" /\" / string\" "}, {"9", " \n "},};
|
||||||
|
|
||||||
final CSVFormat format = CSVFormat.newFormat(',').withQuote('\'').withRecordSeparator(CRLF).withEscape('/')
|
final CSVFormat format = CSVFormat.newFormat(',').withQuote('\'').withRecordSeparator(CRLF).withEscape('/')
|
||||||
.withIgnoreEmptyLines();
|
.withIgnoreEmptyLines();
|
||||||
|
@ -217,9 +139,9 @@ public class CSVParserTest {
|
||||||
+ " \t , , \n" // 2)
|
+ " \t , , \n" // 2)
|
||||||
+ " // , /, , /,\n" // 3)
|
+ " // , /, , /,\n" // 3)
|
||||||
+ "";
|
+ "";
|
||||||
final String[][] res = { { " ", " ", " " }, // 1
|
final String[][] res = {{" ", " ", " "}, // 1
|
||||||
{ " \t ", " ", " " }, // 2
|
{" \t ", " ", " "}, // 2
|
||||||
{ " / ", " , ", " ," }, // 3
|
{" / ", " , ", " ,"}, // 3
|
||||||
};
|
};
|
||||||
|
|
||||||
final CSVFormat format = CSVFormat.newFormat(',').withRecordSeparator(CRLF).withEscape('/')
|
final CSVFormat format = CSVFormat.newFormat(',').withRecordSeparator(CRLF).withEscape('/')
|
||||||
|
@ -238,11 +160,11 @@ public class CSVParserTest {
|
||||||
public void testBackslashEscapingOld() throws IOException {
|
public void testBackslashEscapingOld() throws IOException {
|
||||||
final String code = "one,two,three\n" + "on\\\"e,two\n" + "on\"e,two\n" + "one,\"tw\\\"o\"\n"
|
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\"";
|
+ "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" },
|
final String[][] res = {{"one", "two", "three"}, {"on\\\"e", "two"}, {"on\"e", "two"}, {"one", "tw\"o"},
|
||||||
{ "one", "tw\"o" }, { "one", "t\\,wo" }, // backslash in quotes only escapes a delimiter (",")
|
{"one", "t\\,wo"}, // backslash in quotes only escapes a delimiter (",")
|
||||||
{ "one", "two", "th,ree" }, { "a\\\\" }, // 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"}, // a backslash must be returned
|
||||||
{ "a\\\\,b" } // backslash in quotes only escapes a delimiter (",")
|
{"a\\\\,b"} // backslash in quotes only escapes a delimiter (",")
|
||||||
};
|
};
|
||||||
try (final CSVParser parser = CSVParser.parse(code, CSVFormat.DEFAULT)) {
|
try (final CSVParser parser = CSVParser.parse(code, CSVFormat.DEFAULT)) {
|
||||||
final List<CSVRecord> records = parser.getRecords();
|
final List<CSVRecord> records = parser.getRecords();
|
||||||
|
@ -365,11 +287,11 @@ public class CSVParserTest {
|
||||||
+ "#,\"\"\n" // 3)
|
+ "#,\"\"\n" // 3)
|
||||||
+ "# Final comment\n"// 4)
|
+ "# Final comment\n"// 4)
|
||||||
;
|
;
|
||||||
final String[][] res = { { "a", "b#" }, { "\n", " ", "#" }, { "#", "" }, { "# Final comment" } };
|
final String[][] res = {{"a", "b#"}, {"\n", " ", "#"}, {"#", ""}, {"# Final comment"}};
|
||||||
|
|
||||||
CSVFormat format = CSVFormat.DEFAULT;
|
CSVFormat format = CSVFormat.DEFAULT;
|
||||||
assertFalse(format.isCommentMarkerSet());
|
assertFalse(format.isCommentMarkerSet());
|
||||||
final String[][] res_comments = { { "a", "b#" }, { "\n", " ", "#" }, };
|
final String[][] res_comments = {{"a", "b#"}, {"\n", " ", "#"},};
|
||||||
|
|
||||||
try (final CSVParser parser = CSVParser.parse(code, format)) {
|
try (final CSVParser parser = CSVParser.parse(code, format)) {
|
||||||
final List<CSVRecord> records = parser.getRecords();
|
final List<CSVRecord> records = parser.getRecords();
|
||||||
|
@ -399,7 +321,8 @@ public class CSVParserTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testEmptyFile() throws Exception {
|
public void testEmptyFile() throws Exception {
|
||||||
try (final CSVParser parser = CSVParser.parse("", CSVFormat.DEFAULT)) {
|
try (final CSVParser parser = CSVParser.parse(Paths.get("src/test/resources/org/apache/commons/csv/empty.txt"),
|
||||||
|
StandardCharsets.UTF_8, CSVFormat.DEFAULT)) {
|
||||||
assertNull(parser.nextRecord());
|
assertNull(parser.nextRecord());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -414,8 +337,8 @@ public class CSVParserTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testEmptyLineBehaviorCSV() throws Exception {
|
public void testEmptyLineBehaviorCSV() 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[] 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", "" } // CSV format ignores empty lines
|
final String[][] res = {{"hello", ""} // CSV format ignores empty lines
|
||||||
};
|
};
|
||||||
for (final String code : codes) {
|
for (final String code : codes) {
|
||||||
try (final CSVParser parser = CSVParser.parse(code, CSVFormat.DEFAULT)) {
|
try (final CSVParser parser = CSVParser.parse(code, CSVFormat.DEFAULT)) {
|
||||||
|
@ -431,9 +354,9 @@ public class CSVParserTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testEmptyLineBehaviorExcel() throws Exception {
|
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[] 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
|
final String[][] res = {{"hello", ""}, {""}, // Excel format does not ignore empty lines
|
||||||
{ "" } };
|
{""}};
|
||||||
for (final String code : codes) {
|
for (final String code : codes) {
|
||||||
try (final CSVParser parser = CSVParser.parse(code, CSVFormat.EXCEL)) {
|
try (final CSVParser parser = CSVParser.parse(code, CSVFormat.EXCEL)) {
|
||||||
final List<CSVRecord> records = parser.getRecords();
|
final List<CSVRecord> records = parser.getRecords();
|
||||||
|
@ -446,13 +369,20 @@ public class CSVParserTest {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testEmptyString() throws Exception {
|
||||||
|
try (final CSVParser parser = CSVParser.parse("", CSVFormat.DEFAULT)) {
|
||||||
|
assertNull(parser.nextRecord());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testEndOfFileBehaviorCSV() throws Exception {
|
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",
|
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,\n", "hello,\r\n\r\nworld,", "hello,\r\n\r\nworld,\"\"\n",
|
||||||
"hello,\r\n\r\nworld,\"\"" };
|
"hello,\r\n\r\nworld,\"\""};
|
||||||
final String[][] res = { { "hello", "" }, // CSV format ignores empty lines
|
final String[][] res = {{"hello", ""}, // CSV format ignores empty lines
|
||||||
{ "world", "" } };
|
{"world", ""}};
|
||||||
for (final String code : codes) {
|
for (final String code : codes) {
|
||||||
try (final CSVParser parser = CSVParser.parse(code, CSVFormat.DEFAULT)) {
|
try (final CSVParser parser = CSVParser.parse(code, CSVFormat.DEFAULT)) {
|
||||||
final List<CSVRecord> records = parser.getRecords();
|
final List<CSVRecord> records = parser.getRecords();
|
||||||
|
@ -467,11 +397,11 @@ public class CSVParserTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testEndOfFileBehaviorExcel() throws Exception {
|
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",
|
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,\n", "hello,\r\n\r\nworld,", "hello,\r\n\r\nworld,\"\"\n",
|
||||||
"hello,\r\n\r\nworld,\"\"" };
|
"hello,\r\n\r\nworld,\"\""};
|
||||||
final String[][] res = { { "hello", "" }, { "" }, // Excel format does not ignore empty lines
|
final String[][] res = {{"hello", ""}, {""}, // Excel format does not ignore empty lines
|
||||||
{ "world", "" } };
|
{"world", ""}};
|
||||||
|
|
||||||
for (final String code : codes) {
|
for (final String code : codes) {
|
||||||
try (final CSVParser parser = CSVParser.parse(code, CSVFormat.EXCEL)) {
|
try (final CSVParser parser = CSVParser.parse(code, CSVFormat.EXCEL)) {
|
||||||
|
@ -489,8 +419,8 @@ public class CSVParserTest {
|
||||||
public void testExcelFormat1() throws IOException {
|
public void testExcelFormat1() throws IOException {
|
||||||
final String code = "value1,value2,value3,value4\r\na,b,c,d\r\n x,,,"
|
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";
|
+ "\r\n\r\n\"\"\"hello\"\"\",\" \"\"world\"\"\",\"abc\ndef\",\r\n";
|
||||||
final String[][] res = { { "value1", "value2", "value3", "value4" }, { "a", "b", "c", "d" },
|
final String[][] res = {{"value1", "value2", "value3", "value4"}, {"a", "b", "c", "d"}, {" x", "", "", ""},
|
||||||
{ " x", "", "", "" }, { "" }, { "\"hello\"", " \"world\"", "abc\ndef", "" } };
|
{""}, {"\"hello\"", " \"world\"", "abc\ndef", ""}};
|
||||||
try (final CSVParser parser = CSVParser.parse(code, CSVFormat.EXCEL)) {
|
try (final CSVParser parser = CSVParser.parse(code, CSVFormat.EXCEL)) {
|
||||||
final List<CSVRecord> records = parser.getRecords();
|
final List<CSVRecord> records = parser.getRecords();
|
||||||
assertEquals(res.length, records.size());
|
assertEquals(res.length, records.size());
|
||||||
|
@ -504,7 +434,7 @@ public class CSVParserTest {
|
||||||
@Test
|
@Test
|
||||||
public void testExcelFormat2() throws Exception {
|
public void testExcelFormat2() throws Exception {
|
||||||
final String code = "foo,baar\r\n\r\nhello,\r\n\r\nworld,\r\n";
|
final String code = "foo,baar\r\n\r\nhello,\r\n\r\nworld,\r\n";
|
||||||
final String[][] res = { { "foo", "baar" }, { "" }, { "hello", "" }, { "" }, { "world", "" } };
|
final String[][] res = {{"foo", "baar"}, {""}, {"hello", ""}, {""}, {"world", ""}};
|
||||||
try (final CSVParser parser = CSVParser.parse(code, CSVFormat.EXCEL)) {
|
try (final CSVParser parser = CSVParser.parse(code, CSVFormat.EXCEL)) {
|
||||||
final List<CSVRecord> records = parser.getRecords();
|
final List<CSVRecord> records = parser.getRecords();
|
||||||
assertEquals(res.length, records.size());
|
assertEquals(res.length, records.size());
|
||||||
|
@ -570,9 +500,9 @@ public class CSVParserTest {
|
||||||
records.add(record);
|
records.add(record);
|
||||||
}
|
}
|
||||||
assertEquals(3, records.size());
|
assertEquals(3, records.size());
|
||||||
assertArrayEquals(new String[] { "a", "b", "c" }, records.get(0).values());
|
assertArrayEquals(new String[] {"a", "b", "c"}, records.get(0).values());
|
||||||
assertArrayEquals(new String[] { "1", "2", "3" }, records.get(1).values());
|
assertArrayEquals(new String[] {"1", "2", "3"}, records.get(1).values());
|
||||||
assertArrayEquals(new String[] { "x", "y", "z" }, records.get(2).values());
|
assertArrayEquals(new String[] {"x", "y", "z"}, records.get(2).values());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -865,12 +795,12 @@ public class CSVParserTest {
|
||||||
|
|
||||||
assertTrue(iterator.hasNext());
|
assertTrue(iterator.hasNext());
|
||||||
assertThrows(UnsupportedOperationException.class, iterator::remove);
|
assertThrows(UnsupportedOperationException.class, iterator::remove);
|
||||||
assertArrayEquals(new String[] { "a", "b", "c" }, iterator.next().values());
|
assertArrayEquals(new String[] {"a", "b", "c"}, iterator.next().values());
|
||||||
assertArrayEquals(new String[] { "1", "2", "3" }, iterator.next().values());
|
assertArrayEquals(new String[] {"1", "2", "3"}, iterator.next().values());
|
||||||
assertTrue(iterator.hasNext());
|
assertTrue(iterator.hasNext());
|
||||||
assertTrue(iterator.hasNext());
|
assertTrue(iterator.hasNext());
|
||||||
assertTrue(iterator.hasNext());
|
assertTrue(iterator.hasNext());
|
||||||
assertArrayEquals(new String[] { "x", "y", "z" }, iterator.next().values());
|
assertArrayEquals(new String[] {"x", "y", "z"}, iterator.next().values());
|
||||||
assertFalse(iterator.hasNext());
|
assertFalse(iterator.hasNext());
|
||||||
|
|
||||||
assertThrows(NoSuchElementException.class, iterator::next);
|
assertThrows(NoSuchElementException.class, iterator::next);
|
||||||
|
@ -1033,6 +963,15 @@ public class CSVParserTest {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testNotValueCSV() throws IOException {
|
||||||
|
final String source = "#";
|
||||||
|
final CSVFormat csvFormat = CSVFormat.DEFAULT.withCommentMarker('#');
|
||||||
|
final CSVParser csvParser = csvFormat.parse(new StringReader(source));
|
||||||
|
final CSVRecord csvRecord = csvParser.nextRecord();
|
||||||
|
assertNull(csvRecord);
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testParse() throws Exception {
|
public void testParse() throws Exception {
|
||||||
final ClassLoader loader = ClassLoader.getSystemClassLoader();
|
final ClassLoader loader = ClassLoader.getSystemClassLoader();
|
||||||
|
@ -1114,6 +1053,75 @@ public class CSVParserTest {
|
||||||
() -> CSVParser.parse(new URL("https://commons.apache.org"), Charset.defaultCharset(), null));
|
() -> CSVParser.parse(new URL("https://commons.apache.org"), Charset.defaultCharset(), null));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testParseWithDelimiterStringWithEscape() throws IOException {
|
||||||
|
final String source = "a![!|!]b![|]c[|]xyz\r\nabc[abc][|]xyz";
|
||||||
|
final CSVFormat csvFormat = CSVFormat.DEFAULT.builder().setDelimiter("[|]").setEscape('!').build();
|
||||||
|
try (CSVParser csvParser = csvFormat.parse(new StringReader(source))) {
|
||||||
|
CSVRecord csvRecord = csvParser.nextRecord();
|
||||||
|
assertEquals("a[|]b![|]c", csvRecord.get(0));
|
||||||
|
assertEquals("xyz", csvRecord.get(1));
|
||||||
|
csvRecord = csvParser.nextRecord();
|
||||||
|
assertEquals("abc[abc]", csvRecord.get(0));
|
||||||
|
assertEquals("xyz", csvRecord.get(1));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testParseWithDelimiterStringWithQuote() throws IOException {
|
||||||
|
final String source = "'a[|]b[|]c'[|]xyz\r\nabc[abc][|]xyz";
|
||||||
|
final CSVFormat csvFormat = CSVFormat.DEFAULT.builder().setDelimiter("[|]").setQuote('\'').build();
|
||||||
|
try (CSVParser csvParser = csvFormat.parse(new StringReader(source))) {
|
||||||
|
CSVRecord csvRecord = csvParser.nextRecord();
|
||||||
|
assertEquals("a[|]b[|]c", csvRecord.get(0));
|
||||||
|
assertEquals("xyz", csvRecord.get(1));
|
||||||
|
csvRecord = csvParser.nextRecord();
|
||||||
|
assertEquals("abc[abc]", csvRecord.get(0));
|
||||||
|
assertEquals("xyz", csvRecord.get(1));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testParseWithDelimiterWithEscape() throws IOException {
|
||||||
|
final String source = "a!,b!,c,xyz";
|
||||||
|
final CSVFormat csvFormat = CSVFormat.DEFAULT.withEscape('!');
|
||||||
|
try (CSVParser csvParser = csvFormat.parse(new StringReader(source))) {
|
||||||
|
final CSVRecord csvRecord = csvParser.nextRecord();
|
||||||
|
assertEquals("a,b,c", csvRecord.get(0));
|
||||||
|
assertEquals("xyz", csvRecord.get(1));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testParseWithDelimiterWithQuote() throws IOException {
|
||||||
|
final String source = "'a,b,c',xyz";
|
||||||
|
final CSVFormat csvFormat = CSVFormat.DEFAULT.withQuote('\'');
|
||||||
|
try (CSVParser csvParser = csvFormat.parse(new StringReader(source))) {
|
||||||
|
final CSVRecord csvRecord = csvParser.nextRecord();
|
||||||
|
assertEquals("a,b,c", csvRecord.get(0));
|
||||||
|
assertEquals("xyz", csvRecord.get(1));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testParseWithQuoteThrowsException() {
|
||||||
|
final CSVFormat csvFormat = CSVFormat.DEFAULT.withQuote('\'');
|
||||||
|
assertThrows(IOException.class, () -> csvFormat.parse(new StringReader("'a,b,c','")).nextRecord());
|
||||||
|
assertThrows(IOException.class, () -> csvFormat.parse(new StringReader("'a,b,c'abc,xyz")).nextRecord());
|
||||||
|
assertThrows(IOException.class, () -> csvFormat.parse(new StringReader("'abc'a,b,c',xyz")).nextRecord());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testParseWithQuoteWithEscape() throws IOException {
|
||||||
|
final String source = "'a?,b?,c?d',xyz";
|
||||||
|
final CSVFormat csvFormat = CSVFormat.DEFAULT.withQuote('\'').withEscape('?');
|
||||||
|
try (CSVParser csvParser = csvFormat.parse(new StringReader(source))) {
|
||||||
|
final CSVRecord csvRecord = csvParser.nextRecord();
|
||||||
|
assertEquals("a,b,c?d", csvRecord.get(0));
|
||||||
|
assertEquals("xyz", csvRecord.get(1));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testProvidedHeader() throws Exception {
|
public void testProvidedHeader() throws Exception {
|
||||||
final Reader in = new StringReader("a,b,c\n1,2,3\nx,y,z");
|
final Reader in = new StringReader("a,b,c\n1,2,3\nx,y,z");
|
||||||
|
@ -1222,10 +1230,10 @@ public class CSVParserTest {
|
||||||
@Test
|
@Test
|
||||||
@Disabled
|
@Disabled
|
||||||
public void testStartWithEmptyLinesThenHeaders() throws Exception {
|
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",
|
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
|
final String[][] res = {{"hello", ""}, {""}, // Excel format does not ignore empty lines
|
||||||
{ "" } };
|
{""}};
|
||||||
for (final String code : codes) {
|
for (final String code : codes) {
|
||||||
try (final CSVParser parser = CSVParser.parse(code, CSVFormat.EXCEL)) {
|
try (final CSVParser parser = CSVParser.parse(code, CSVFormat.EXCEL)) {
|
||||||
final List<CSVRecord> records = parser.getRecords();
|
final List<CSVRecord> records = parser.getRecords();
|
||||||
|
@ -1243,9 +1251,9 @@ public class CSVParserTest {
|
||||||
final Reader in = new StringReader("a,b,c\n1,2,3\nx,y,z");
|
final Reader in = new StringReader("a,b,c\n1,2,3\nx,y,z");
|
||||||
final List<CSVRecord> list = CSVFormat.DEFAULT.parse(in).stream().collect(Collectors.toList());
|
final List<CSVRecord> list = CSVFormat.DEFAULT.parse(in).stream().collect(Collectors.toList());
|
||||||
assertFalse(list.isEmpty());
|
assertFalse(list.isEmpty());
|
||||||
assertArrayEquals(new String[] { "a", "b", "c" }, list.get(0).values());
|
assertArrayEquals(new String[] {"a", "b", "c"}, list.get(0).values());
|
||||||
assertArrayEquals(new String[] { "1", "2", "3" }, list.get(1).values());
|
assertArrayEquals(new String[] {"1", "2", "3"}, list.get(1).values());
|
||||||
assertArrayEquals(new String[] { "x", "y", "z" }, list.get(2).values());
|
assertArrayEquals(new String[] {"x", "y", "z"}, list.get(2).values());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
Loading…
Reference in New Issue