mirror of
https://github.com/apache/commons-csv.git
synced 2025-02-16 23:16:01 +00:00
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();
|
||||||
|
@ -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 {
|
||||||
|
|
||||||
@ -238,8 +160,8 @@ 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 (",")
|
||||||
@ -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());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -446,6 +369,13 @@ 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",
|
||||||
@ -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());
|
||||||
@ -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");
|
||||||
|
0
src/test/resources/org/apache/commons/csv/empty.txt
Normal file
0
src/test/resources/org/apache/commons/csv/empty.txt
Normal file
Loading…
x
Reference in New Issue
Block a user