mirror of
https://github.com/apache/commons-csv.git
synced 2025-02-20 00:47:20 +00:00
Use try-with-resources.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/csv/trunk@1748094 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
ed6adc706e
commit
9daee9042c
@ -609,8 +609,8 @@ public final class CSVFormat implements Serializable {
|
|||||||
*/
|
*/
|
||||||
public String format(final Object... values) {
|
public String format(final Object... values) {
|
||||||
final StringWriter out = new StringWriter();
|
final StringWriter out = new StringWriter();
|
||||||
try {
|
try (final CSVPrinter csvPrinter = new CSVPrinter(out, this)) {
|
||||||
new CSVPrinter(out, this).printRecord(values);
|
csvPrinter.printRecord(values);
|
||||||
return out.toString().trim();
|
return out.toString().trim();
|
||||||
} catch (final IOException e) {
|
} catch (final IOException e) {
|
||||||
// should not happen because a StringWriter does not do IO.
|
// should not happen because a StringWriter does not do IO.
|
||||||
|
@ -116,7 +116,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.
|
||||||
final CSVParser parser = CSVParser.parse(new File(BASE, split[0]), Charset.defaultCharset(), format);
|
try (final CSVParser parser = CSVParser.parse(new File(BASE, 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());
|
||||||
if (checkComments) {
|
if (checkComments) {
|
||||||
@ -128,7 +128,7 @@ public class CSVFileParserTest {
|
|||||||
final int count = record.size();
|
final int count = record.size();
|
||||||
assertEquals(testName, readTestData(), count + ":" + parsed);
|
assertEquals(testName, readTestData(), count + ":" + parsed);
|
||||||
}
|
}
|
||||||
parser.close();
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -160,7 +160,7 @@ public class CSVFileParserTest {
|
|||||||
|
|
||||||
// Now parse the file and compare against the expected results
|
// Now parse the file and compare against the expected results
|
||||||
final URL resource = ClassLoader.getSystemResource("CSVFileParser/" + split[0]);
|
final URL resource = ClassLoader.getSystemResource("CSVFileParser/" + split[0]);
|
||||||
final CSVParser parser = CSVParser.parse(resource, Charset.forName("UTF-8"), format);
|
try (final CSVParser parser = CSVParser.parse(resource, Charset.forName("UTF-8"), format)) {
|
||||||
for (final CSVRecord record : parser) {
|
for (final CSVRecord record : parser) {
|
||||||
String parsed = Arrays.toString(record.values());
|
String parsed = Arrays.toString(record.values());
|
||||||
if (checkComments) {
|
if (checkComments) {
|
||||||
@ -172,6 +172,6 @@ public class CSVFileParserTest {
|
|||||||
final int count = record.size();
|
final int count = record.size();
|
||||||
assertEquals(testName, readTestData(), count + ":" + parsed);
|
assertEquals(testName, readTestData(), count + ":" + parsed);
|
||||||
}
|
}
|
||||||
parser.close();
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -327,10 +327,10 @@ public class CSVFormatTest {
|
|||||||
public void testSerialization() throws Exception {
|
public void testSerialization() throws Exception {
|
||||||
final ByteArrayOutputStream out = new ByteArrayOutputStream();
|
final ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||||
|
|
||||||
final ObjectOutputStream oos = new ObjectOutputStream(out);
|
try (final ObjectOutputStream oos = new ObjectOutputStream(out)) {
|
||||||
oos.writeObject(CSVFormat.DEFAULT);
|
oos.writeObject(CSVFormat.DEFAULT);
|
||||||
oos.flush();
|
oos.flush();
|
||||||
oos.close();
|
}
|
||||||
|
|
||||||
final ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(out.toByteArray()));
|
final ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(out.toByteArray()));
|
||||||
final CSVFormat format = (CSVFormat) in.readObject();
|
final CSVFormat format = (CSVFormat) in.readObject();
|
||||||
|
@ -102,12 +102,12 @@ public class CSVParserTest {
|
|||||||
final CSVFormat format = CSVFormat.newFormat(',').withQuote('\'').withRecordSeparator(CRLF).withEscape('/')
|
final CSVFormat format = CSVFormat.newFormat(',').withQuote('\'').withRecordSeparator(CRLF).withEscape('/')
|
||||||
.withIgnoreEmptyLines();
|
.withIgnoreEmptyLines();
|
||||||
|
|
||||||
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();
|
||||||
assertTrue(records.size() > 0);
|
assertTrue(records.size() > 0);
|
||||||
|
|
||||||
Utils.compare("Records do not match expected result", res, records);
|
Utils.compare("Records do not match expected result", res, records);
|
||||||
parser.close();
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -129,104 +129,98 @@ public class CSVParserTest {
|
|||||||
final CSVFormat format = CSVFormat.newFormat(',').withRecordSeparator(CRLF).withEscape('/')
|
final CSVFormat format = CSVFormat.newFormat(',').withRecordSeparator(CRLF).withEscape('/')
|
||||||
.withIgnoreEmptyLines();
|
.withIgnoreEmptyLines();
|
||||||
|
|
||||||
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();
|
||||||
assertTrue(records.size() > 0);
|
assertTrue(records.size() > 0);
|
||||||
|
|
||||||
Utils.compare("", res, records);
|
Utils.compare("", res, records);
|
||||||
parser.close();
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Ignore
|
@Ignore
|
||||||
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", "t\\,wo" }, // 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 (",")
|
{ "one", "two", "th,ree" }, { "a\\\\" }, // backslash in quotes only escapes a delimiter (",")
|
||||||
{ "a\\", "b" }, // a backslash must be returnd
|
{ "a\\", "b" }, // a backslash must be returnd
|
||||||
{ "a\\\\,b" } // backslash in quotes only escapes a delimiter (",")
|
{ "a\\\\,b" } // backslash in quotes only escapes a delimiter (",")
|
||||||
};
|
};
|
||||||
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();
|
||||||
assertEquals(res.length, records.size());
|
assertEquals(res.length, records.size());
|
||||||
assertTrue(records.size() > 0);
|
assertTrue(records.size() > 0);
|
||||||
for (int i = 0; i < res.length; i++) {
|
for (int i = 0; i < res.length; i++) {
|
||||||
assertArrayEquals(res[i], records.get(i).values());
|
assertArrayEquals(res[i], records.get(i).values());
|
||||||
}
|
}
|
||||||
parser.close();
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Ignore("CSV-107")
|
@Ignore("CSV-107")
|
||||||
public void testBOM() throws IOException {
|
public void testBOM() throws IOException {
|
||||||
final URL url = ClassLoader.getSystemClassLoader().getResource("CSVFileParser/bom.csv");
|
final URL url = ClassLoader.getSystemClassLoader().getResource("CSVFileParser/bom.csv");
|
||||||
final CSVParser parser = CSVParser.parse(url, Charset.forName("UTF-8"), CSVFormat.EXCEL.withHeader());
|
try (final CSVParser parser = CSVParser.parse(url, Charset.forName("UTF-8"), CSVFormat.EXCEL.withHeader())) {
|
||||||
try {
|
|
||||||
for (final CSVRecord record : parser) {
|
for (final CSVRecord record : parser) {
|
||||||
final String string = record.get("Date");
|
final String string = record.get("Date");
|
||||||
Assert.assertNotNull(string);
|
Assert.assertNotNull(string);
|
||||||
// System.out.println("date: " + record.get("Date"));
|
// System.out.println("date: " + record.get("Date"));
|
||||||
}
|
}
|
||||||
} finally {
|
|
||||||
parser.close();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testBOMInputStream() throws IOException {
|
public void testBOMInputStream() throws IOException {
|
||||||
final URL url = ClassLoader.getSystemClassLoader().getResource("CSVFileParser/bom.csv");
|
final URL url = ClassLoader.getSystemClassLoader().getResource("CSVFileParser/bom.csv");
|
||||||
final Reader reader = new InputStreamReader(new BOMInputStream(url.openStream()), "UTF-8");
|
try (final Reader reader = new InputStreamReader(new BOMInputStream(url.openStream()), "UTF-8");
|
||||||
final CSVParser parser = new CSVParser(reader, CSVFormat.EXCEL.withHeader());
|
final CSVParser parser = new CSVParser(reader, CSVFormat.EXCEL.withHeader())) {
|
||||||
try {
|
|
||||||
for (final CSVRecord record : parser) {
|
for (final CSVRecord record : parser) {
|
||||||
final String string = record.get("Date");
|
final String string = record.get("Date");
|
||||||
Assert.assertNotNull(string);
|
Assert.assertNotNull(string);
|
||||||
// System.out.println("date: " + record.get("Date"));
|
// System.out.println("date: " + record.get("Date"));
|
||||||
}
|
}
|
||||||
} finally {
|
|
||||||
parser.close();
|
|
||||||
reader.close();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testCarriageReturnEndings() throws IOException {
|
public void testCarriageReturnEndings() throws IOException {
|
||||||
final String code = "foo\rbaar,\rhello,world\r,kanu";
|
final String code = "foo\rbaar,\rhello,world\r,kanu";
|
||||||
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();
|
||||||
assertEquals(4, records.size());
|
assertEquals(4, records.size());
|
||||||
parser.close();
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testCarriageReturnLineFeedEndings() throws IOException {
|
public void testCarriageReturnLineFeedEndings() throws IOException {
|
||||||
final String code = "foo\r\nbaar,\r\nhello,world\r\n,kanu";
|
final String code = "foo\r\nbaar,\r\nhello,world\r\n,kanu";
|
||||||
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();
|
||||||
assertEquals(4, records.size());
|
assertEquals(4, records.size());
|
||||||
parser.close();
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected = NoSuchElementException.class)
|
@Test(expected = NoSuchElementException.class)
|
||||||
public void testClose() throws Exception {
|
public void testClose() throws Exception {
|
||||||
final Reader in = new StringReader("# comment\na,b,c\n1,2,3\nx,y,z");
|
final Reader in = new StringReader("# comment\na,b,c\n1,2,3\nx,y,z");
|
||||||
final CSVParser parser = CSVFormat.DEFAULT.withCommentMarker('#').withHeader().parse(in);
|
final Iterator<CSVRecord> records;
|
||||||
final Iterator<CSVRecord> records = parser.iterator();
|
try (final CSVParser parser = CSVFormat.DEFAULT.withCommentMarker('#').withHeader().parse(in)) {
|
||||||
|
records = parser.iterator();
|
||||||
assertTrue(records.hasNext());
|
assertTrue(records.hasNext());
|
||||||
parser.close();
|
}
|
||||||
assertFalse(records.hasNext());
|
assertFalse(records.hasNext());
|
||||||
records.next();
|
records.next();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testCSV57() throws Exception {
|
public void testCSV57() throws Exception {
|
||||||
final CSVParser parser = CSVParser.parse("", CSVFormat.DEFAULT);
|
try (final CSVParser parser = CSVParser.parse("", CSVFormat.DEFAULT)) {
|
||||||
final List<CSVRecord> list = parser.getRecords();
|
final List<CSVRecord> list = parser.getRecords();
|
||||||
assertNotNull(list);
|
assertNotNull(list);
|
||||||
assertEquals(0, list.size());
|
assertEquals(0, list.size());
|
||||||
parser.close();
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -240,22 +234,21 @@ public class CSVParserTest {
|
|||||||
|
|
||||||
CSVFormat format = CSVFormat.DEFAULT;
|
CSVFormat format = CSVFormat.DEFAULT;
|
||||||
assertFalse(format.isCommentMarkerSet());
|
assertFalse(format.isCommentMarkerSet());
|
||||||
|
final String[][] res_comments = { { "a", "b#" }, { "\n", " ", "#" }, };
|
||||||
|
|
||||||
CSVParser parser = CSVParser.parse(code, format);
|
try (final CSVParser parser = CSVParser.parse(code, format)) {
|
||||||
List<CSVRecord> records = parser.getRecords();
|
List<CSVRecord> records = parser.getRecords();
|
||||||
assertTrue(records.size() > 0);
|
assertTrue(records.size() > 0);
|
||||||
|
|
||||||
Utils.compare("Failed to parse without comments", res, records);
|
Utils.compare("Failed to parse without comments", res, records);
|
||||||
|
|
||||||
final String[][] res_comments = { { "a", "b#" }, { "\n", " ", "#" }, };
|
|
||||||
|
|
||||||
format = CSVFormat.DEFAULT.withCommentMarker('#');
|
format = CSVFormat.DEFAULT.withCommentMarker('#');
|
||||||
parser.close();
|
}
|
||||||
parser = CSVParser.parse(code, format);
|
try (final CSVParser parser = CSVParser.parse(code, format)) {
|
||||||
records = parser.getRecords();
|
List<CSVRecord> records = parser.getRecords();
|
||||||
|
|
||||||
Utils.compare("Failed to parse with comments", res_comments, records);
|
Utils.compare("Failed to parse with comments", res_comments, records);
|
||||||
parser.close();
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected = IllegalArgumentException.class)
|
@Test(expected = IllegalArgumentException.class)
|
||||||
@ -265,9 +258,9 @@ public class CSVParserTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testEmptyFile() throws Exception {
|
public void testEmptyFile() throws Exception {
|
||||||
final CSVParser parser = CSVParser.parse("", CSVFormat.DEFAULT);
|
try (final CSVParser parser = CSVParser.parse("", CSVFormat.DEFAULT)) {
|
||||||
assertNull(parser.nextRecord());
|
assertNull(parser.nextRecord());
|
||||||
parser.close();
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -276,14 +269,14 @@ public class CSVParserTest {
|
|||||||
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) {
|
||||||
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();
|
||||||
assertEquals(res.length, records.size());
|
assertEquals(res.length, records.size());
|
||||||
assertTrue(records.size() > 0);
|
assertTrue(records.size() > 0);
|
||||||
for (int i = 0; i < res.length; i++) {
|
for (int i = 0; i < res.length; i++) {
|
||||||
assertArrayEquals(res[i], records.get(i).values());
|
assertArrayEquals(res[i], records.get(i).values());
|
||||||
}
|
}
|
||||||
parser.close();
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -293,14 +286,14 @@ public class CSVParserTest {
|
|||||||
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) {
|
||||||
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());
|
||||||
assertTrue(records.size() > 0);
|
assertTrue(records.size() > 0);
|
||||||
for (int i = 0; i < res.length; i++) {
|
for (int i = 0; i < res.length; i++) {
|
||||||
assertArrayEquals(res[i], records.get(i).values());
|
assertArrayEquals(res[i], records.get(i).values());
|
||||||
}
|
}
|
||||||
parser.close();
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -312,14 +305,14 @@ public class CSVParserTest {
|
|||||||
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) {
|
||||||
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();
|
||||||
assertEquals(res.length, records.size());
|
assertEquals(res.length, records.size());
|
||||||
assertTrue(records.size() > 0);
|
assertTrue(records.size() > 0);
|
||||||
for (int i = 0; i < res.length; i++) {
|
for (int i = 0; i < res.length; i++) {
|
||||||
assertArrayEquals(res[i], records.get(i).values());
|
assertArrayEquals(res[i], records.get(i).values());
|
||||||
}
|
}
|
||||||
parser.close();
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -332,45 +325,45 @@ public class CSVParserTest {
|
|||||||
{ "world", "" } };
|
{ "world", "" } };
|
||||||
|
|
||||||
for (final String code : codes) {
|
for (final String code : codes) {
|
||||||
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());
|
||||||
assertTrue(records.size() > 0);
|
assertTrue(records.size() > 0);
|
||||||
for (int i = 0; i < res.length; i++) {
|
for (int i = 0; i < res.length; i++) {
|
||||||
assertArrayEquals(res[i], records.get(i).values());
|
assertArrayEquals(res[i], records.get(i).values());
|
||||||
}
|
}
|
||||||
parser.close();
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
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", "", "", "" }, { "" }, { "\"hello\"", " \"world\"", "abc\ndef", "" } };
|
{ " x", "", "", "" }, { "" }, { "\"hello\"", " \"world\"", "abc\ndef", "" } };
|
||||||
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());
|
||||||
assertTrue(records.size() > 0);
|
assertTrue(records.size() > 0);
|
||||||
for (int i = 0; i < res.length; i++) {
|
for (int i = 0; i < res.length; i++) {
|
||||||
assertArrayEquals(res[i], records.get(i).values());
|
assertArrayEquals(res[i], records.get(i).values());
|
||||||
}
|
}
|
||||||
parser.close();
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@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", "" } };
|
||||||
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());
|
||||||
assertTrue(records.size() > 0);
|
assertTrue(records.size() > 0);
|
||||||
for (int i = 0; i < res.length; i++) {
|
for (int i = 0; i < res.length; i++) {
|
||||||
assertArrayEquals(res[i], records.get(i).values());
|
assertArrayEquals(res[i], records.get(i).values());
|
||||||
}
|
}
|
||||||
parser.close();
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -379,37 +372,33 @@ public class CSVParserTest {
|
|||||||
@Test
|
@Test
|
||||||
public void testExcelHeaderCountLessThanData() throws Exception {
|
public void testExcelHeaderCountLessThanData() throws Exception {
|
||||||
final String code = "A,B,C,,\r\na,b,c,d,e\r\n";
|
final String code = "A,B,C,,\r\na,b,c,d,e\r\n";
|
||||||
final CSVParser parser = CSVParser.parse(code, CSVFormat.EXCEL.withHeader());
|
try (final CSVParser parser = CSVParser.parse(code, CSVFormat.EXCEL.withHeader())) {
|
||||||
try {
|
|
||||||
for (final CSVRecord record : parser.getRecords()) {
|
for (final CSVRecord record : parser.getRecords()) {
|
||||||
Assert.assertEquals("a", record.get("A"));
|
Assert.assertEquals("a", record.get("A"));
|
||||||
Assert.assertEquals("b", record.get("B"));
|
Assert.assertEquals("b", record.get("B"));
|
||||||
Assert.assertEquals("c", record.get("C"));
|
Assert.assertEquals("c", record.get("C"));
|
||||||
}
|
}
|
||||||
} finally {
|
|
||||||
parser.close();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testForEach() throws Exception {
|
public void testForEach() throws Exception {
|
||||||
final List<CSVRecord> records = new ArrayList<>();
|
final List<CSVRecord> records = new ArrayList<>();
|
||||||
|
try (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");
|
|
||||||
|
|
||||||
for (final CSVRecord record : CSVFormat.DEFAULT.parse(in)) {
|
for (final CSVRecord record : CSVFormat.DEFAULT.parse(in)) {
|
||||||
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());
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGetHeaderMap() throws Exception {
|
public void testGetHeaderMap() throws Exception {
|
||||||
final CSVParser parser = CSVParser.parse("a,b,c\n1,2,3\nx,y,z", CSVFormat.DEFAULT.withHeader("A", "B", "C"));
|
try (final CSVParser parser = CSVParser.parse("a,b,c\n1,2,3\nx,y,z",
|
||||||
|
CSVFormat.DEFAULT.withHeader("A", "B", "C"))) {
|
||||||
final Map<String, Integer> headerMap = parser.getHeaderMap();
|
final Map<String, Integer> headerMap = parser.getHeaderMap();
|
||||||
final Iterator<String> columnNames = headerMap.keySet().iterator();
|
final Iterator<String> columnNames = headerMap.keySet().iterator();
|
||||||
// Headers are iterated in column order.
|
// Headers are iterated in column order.
|
||||||
@ -428,18 +417,18 @@ public class CSVParserTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
assertFalse(records.hasNext());
|
assertFalse(records.hasNext());
|
||||||
parser.close();
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGetLine() throws IOException {
|
public void testGetLine() throws IOException {
|
||||||
final CSVParser parser = CSVParser.parse(CSV_INPUT, CSVFormat.DEFAULT.withIgnoreSurroundingSpaces());
|
try (final CSVParser parser = CSVParser.parse(CSV_INPUT, CSVFormat.DEFAULT.withIgnoreSurroundingSpaces())) {
|
||||||
for (final String[] re : RESULT) {
|
for (final String[] re : RESULT) {
|
||||||
assertArrayEquals(re, parser.nextRecord().values());
|
assertArrayEquals(re, parser.nextRecord().values());
|
||||||
}
|
}
|
||||||
|
|
||||||
assertNull(parser.nextRecord());
|
assertNull(parser.nextRecord());
|
||||||
parser.close();
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -459,10 +448,10 @@ public class CSVParserTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGetOneLine() throws IOException {
|
public void testGetOneLine() throws IOException {
|
||||||
final CSVParser parser = CSVParser.parse(CSV_INPUT_1, CSVFormat.DEFAULT);
|
try (final CSVParser parser = CSVParser.parse(CSV_INPUT_1, CSVFormat.DEFAULT)) {
|
||||||
final CSVRecord record = parser.getRecords().get(0);
|
final CSVRecord record = parser.getRecords().get(0);
|
||||||
assertArrayEquals(RESULT[0], record.values());
|
assertArrayEquals(RESULT[0], record.values());
|
||||||
parser.close();
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -472,11 +461,9 @@ public class CSVParserTest {
|
|||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void testGetOneLineOneParser() throws IOException {
|
public void testGetOneLineOneParser() throws IOException {
|
||||||
final PipedWriter writer = new PipedWriter();
|
|
||||||
final PipedReader reader = new PipedReader(writer);
|
|
||||||
final CSVFormat format = CSVFormat.DEFAULT;
|
final CSVFormat format = CSVFormat.DEFAULT;
|
||||||
final CSVParser parser = new CSVParser(reader, format);
|
try (final PipedWriter writer = new PipedWriter();
|
||||||
try {
|
final CSVParser parser = new CSVParser(new PipedReader(writer), format)) {
|
||||||
writer.append(CSV_INPUT_1);
|
writer.append(CSV_INPUT_1);
|
||||||
writer.append(format.getRecordSeparator());
|
writer.append(format.getRecordSeparator());
|
||||||
final CSVRecord record1 = parser.nextRecord();
|
final CSVRecord record1 = parser.nextRecord();
|
||||||
@ -485,8 +472,6 @@ public class CSVParserTest {
|
|||||||
writer.append(format.getRecordSeparator());
|
writer.append(format.getRecordSeparator());
|
||||||
final CSVRecord record2 = parser.nextRecord();
|
final CSVRecord record2 = parser.nextRecord();
|
||||||
assertArrayEquals(RESULT[1], record2.values());
|
assertArrayEquals(RESULT[1], record2.values());
|
||||||
} finally {
|
|
||||||
parser.close();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -517,20 +502,21 @@ public class CSVParserTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGetRecords() throws IOException {
|
public void testGetRecords() throws IOException {
|
||||||
final CSVParser parser = CSVParser.parse(CSV_INPUT, CSVFormat.DEFAULT.withIgnoreSurroundingSpaces());
|
try (final CSVParser parser = CSVParser.parse(CSV_INPUT, CSVFormat.DEFAULT.withIgnoreSurroundingSpaces())) {
|
||||||
final List<CSVRecord> records = parser.getRecords();
|
final List<CSVRecord> records = parser.getRecords();
|
||||||
assertEquals(RESULT.length, records.size());
|
assertEquals(RESULT.length, records.size());
|
||||||
assertTrue(records.size() > 0);
|
assertTrue(records.size() > 0);
|
||||||
for (int i = 0; i < RESULT.length; i++) {
|
for (int i = 0; i < RESULT.length; i++) {
|
||||||
assertArrayEquals(RESULT[i], records.get(i).values());
|
assertArrayEquals(RESULT[i], records.get(i).values());
|
||||||
}
|
}
|
||||||
parser.close();
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGetRecordWithMultiLineValues() throws Exception {
|
public void testGetRecordWithMultiLineValues() throws Exception {
|
||||||
final CSVParser parser = CSVParser.parse("\"a\r\n1\",\"a\r\n2\"" + CRLF + "\"b\r\n1\",\"b\r\n2\"" + CRLF +
|
try (final CSVParser parser = CSVParser.parse(
|
||||||
"\"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;
|
CSVRecord record;
|
||||||
assertEquals(0, parser.getRecordNumber());
|
assertEquals(0, parser.getRecordNumber());
|
||||||
assertEquals(0, parser.getCurrentLineNumber());
|
assertEquals(0, parser.getCurrentLineNumber());
|
||||||
@ -549,7 +535,7 @@ public class CSVParserTest {
|
|||||||
assertNull(record = parser.nextRecord());
|
assertNull(record = parser.nextRecord());
|
||||||
assertEquals(8, parser.getCurrentLineNumber());
|
assertEquals(8, parser.getCurrentLineNumber());
|
||||||
assertEquals(3, parser.getRecordNumber());
|
assertEquals(3, parser.getRecordNumber());
|
||||||
parser.close();
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -636,16 +622,18 @@ public class CSVParserTest {
|
|||||||
final String code = "\nfoo,baar\n\r\n,\n\n,world\r\n\n";
|
final String code = "\nfoo,baar\n\r\n,\n\n,world\r\n\n";
|
||||||
// String code = "world\r\n\n";
|
// String code = "world\r\n\n";
|
||||||
// String code = "foo;baar\r\n\r\nhello;\r\n\r\nworld;\r\n";
|
// String code = "foo;baar\r\n\r\nhello;\r\n\r\nworld;\r\n";
|
||||||
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();
|
||||||
assertEquals(3, records.size());
|
assertEquals(3, records.size());
|
||||||
parser.close();
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected = IllegalArgumentException.class)
|
@Test(expected = IllegalArgumentException.class)
|
||||||
public void testInvalidFormat() throws Exception {
|
public void testInvalidFormat() throws Exception {
|
||||||
final CSVFormat invalidFormat = CSVFormat.DEFAULT.withDelimiter(CR);
|
final CSVFormat invalidFormat = CSVFormat.DEFAULT.withDelimiter(CR);
|
||||||
new CSVParser(null, invalidFormat).close();
|
try (final CSVParser parser = new CSVParser(null, invalidFormat)) {
|
||||||
|
Assert.fail("This test should have thrown an exception.");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -680,17 +668,17 @@ public class CSVParserTest {
|
|||||||
@Test
|
@Test
|
||||||
public void testLineFeedEndings() throws IOException {
|
public void testLineFeedEndings() throws IOException {
|
||||||
final String code = "foo\nbaar,\nhello,world\n,kanu";
|
final String code = "foo\nbaar,\nhello,world\n,kanu";
|
||||||
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();
|
||||||
assertEquals(4, records.size());
|
assertEquals(4, records.size());
|
||||||
parser.close();
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testMappedButNotSetAsOutlook2007ContactExport() throws Exception {
|
public void testMappedButNotSetAsOutlook2007ContactExport() throws Exception {
|
||||||
final Reader in = new StringReader("a,b,c\n1,2\nx,y,z");
|
final Reader in = new StringReader("a,b,c\n1,2\nx,y,z");
|
||||||
final Iterator<CSVRecord> records = CSVFormat.DEFAULT.withHeader("A", "B", "C").withSkipHeaderRecord()
|
final Iterator<CSVRecord> records = CSVFormat.DEFAULT.withHeader("A", "B", "C").withSkipHeaderRecord().parse(in)
|
||||||
.parse(in).iterator();
|
.iterator();
|
||||||
CSVRecord record;
|
CSVRecord record;
|
||||||
|
|
||||||
// 1st record
|
// 1st record
|
||||||
@ -724,8 +712,7 @@ public class CSVParserTest {
|
|||||||
@Test
|
@Test
|
||||||
// TODO this may lead to strange behavior, throw an exception if iterator() has already been called?
|
// TODO this may lead to strange behavior, throw an exception if iterator() has already been called?
|
||||||
public void testMultipleIterators() throws Exception {
|
public void testMultipleIterators() throws Exception {
|
||||||
final CSVParser parser = CSVParser.parse("a,b,c" + CR + "d,e,f", CSVFormat.DEFAULT);
|
try (final CSVParser parser = CSVParser.parse("a,b,c" + CR + "d,e,f", CSVFormat.DEFAULT)) {
|
||||||
|
|
||||||
final Iterator<CSVRecord> itr1 = parser.iterator();
|
final Iterator<CSVRecord> itr1 = parser.iterator();
|
||||||
final Iterator<CSVRecord> itr2 = parser.iterator();
|
final Iterator<CSVRecord> itr2 = parser.iterator();
|
||||||
|
|
||||||
@ -738,24 +725,28 @@ public class CSVParserTest {
|
|||||||
assertEquals("d", second.get(0));
|
assertEquals("d", second.get(0));
|
||||||
assertEquals("e", second.get(1));
|
assertEquals("e", second.get(1));
|
||||||
assertEquals("f", second.get(2));
|
assertEquals("f", second.get(2));
|
||||||
parser.close();
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected = IllegalArgumentException.class)
|
@Test(expected = IllegalArgumentException.class)
|
||||||
public void testNewCSVParserNullReaderFormat() throws Exception {
|
public void testNewCSVParserNullReaderFormat() throws Exception {
|
||||||
new CSVParser(null, CSVFormat.DEFAULT).close();
|
try (final CSVParser parser = new CSVParser(null, CSVFormat.DEFAULT)) {
|
||||||
|
Assert.fail("This test should have thrown an exception.");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected = IllegalArgumentException.class)
|
@Test(expected = IllegalArgumentException.class)
|
||||||
public void testNewCSVParserReaderNullFormat() throws Exception {
|
public void testNewCSVParserReaderNullFormat() throws Exception {
|
||||||
new CSVParser(new StringReader(""), null).close();
|
try (final CSVParser parser = new CSVParser(new StringReader(""), null)) {
|
||||||
|
Assert.fail("This test should have thrown an exception.");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testNoHeaderMap() throws Exception {
|
public void testNoHeaderMap() throws Exception {
|
||||||
final CSVParser parser = CSVParser.parse("a,b,c\n1,2,3\nx,y,z", CSVFormat.DEFAULT);
|
try (final CSVParser parser = CSVParser.parse("a,b,c\n1,2,3\nx,y,z", CSVFormat.DEFAULT)) {
|
||||||
Assert.assertNull(parser.getHeaderMap());
|
Assert.assertNull(parser.getHeaderMap());
|
||||||
parser.close();
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected = IllegalArgumentException.class)
|
@Test(expected = IllegalArgumentException.class)
|
||||||
@ -780,8 +771,9 @@ public class CSVParserTest {
|
|||||||
|
|
||||||
@Test(expected = IllegalArgumentException.class)
|
@Test(expected = IllegalArgumentException.class)
|
||||||
public void testParserUrlNullCharsetFormat() throws Exception {
|
public void testParserUrlNullCharsetFormat() throws Exception {
|
||||||
final CSVParser parser = CSVParser.parse(new URL("http://commons.apache.org"), null, CSVFormat.DEFAULT);
|
try (final CSVParser parser = CSVParser.parse(new URL("http://commons.apache.org"), null, CSVFormat.DEFAULT)) {
|
||||||
parser.close();
|
Assert.fail("This test should have thrown an exception.");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected = IllegalArgumentException.class)
|
@Test(expected = IllegalArgumentException.class)
|
||||||
@ -791,8 +783,9 @@ public class CSVParserTest {
|
|||||||
|
|
||||||
@Test(expected = IllegalArgumentException.class)
|
@Test(expected = IllegalArgumentException.class)
|
||||||
public void testParseUrlCharsetNullFormat() throws Exception {
|
public void testParseUrlCharsetNullFormat() throws Exception {
|
||||||
final CSVParser parser = CSVParser.parse(new URL("http://commons.apache.org"), Charset.defaultCharset(), null);
|
try (final CSVParser parser = CSVParser.parse(new URL("http://commons.apache.org"), Charset.defaultCharset(), null)) {
|
||||||
parser.close();
|
Assert.fail("This test should have thrown an exception.");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -840,13 +833,13 @@ public class CSVParserTest {
|
|||||||
@Test
|
@Test
|
||||||
public void testRoundtrip() throws Exception {
|
public void testRoundtrip() throws Exception {
|
||||||
final StringWriter out = new StringWriter();
|
final StringWriter out = new StringWriter();
|
||||||
final CSVPrinter printer = new CSVPrinter(out, CSVFormat.DEFAULT);
|
try (final CSVPrinter printer = new CSVPrinter(out, CSVFormat.DEFAULT)) {
|
||||||
final String input = "a,b,c\r\n1,2,3\r\nx,y,z\r\n";
|
final String input = "a,b,c\r\n1,2,3\r\nx,y,z\r\n";
|
||||||
for (final CSVRecord record : CSVParser.parse(input, CSVFormat.DEFAULT)) {
|
for (final CSVRecord record : CSVParser.parse(input, CSVFormat.DEFAULT)) {
|
||||||
printer.printRecord(record);
|
printer.printRecord(record);
|
||||||
}
|
}
|
||||||
assertEquals(input, out.toString());
|
assertEquals(input, out.toString());
|
||||||
printer.close();
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -862,8 +855,8 @@ public class CSVParserTest {
|
|||||||
@Test
|
@Test
|
||||||
public void testSkipHeaderOverrideDuplicateHeaders() throws Exception {
|
public void testSkipHeaderOverrideDuplicateHeaders() throws Exception {
|
||||||
final Reader in = new StringReader("a,a,a\n1,2,3\nx,y,z");
|
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()
|
final Iterator<CSVRecord> records = CSVFormat.DEFAULT.withHeader("X", "Y", "Z").withSkipHeaderRecord().parse(in)
|
||||||
.parse(in).iterator();
|
.iterator();
|
||||||
final CSVRecord record = records.next();
|
final CSVRecord record = records.next();
|
||||||
assertEquals("1", record.get("X"));
|
assertEquals("1", record.get("X"));
|
||||||
assertEquals("2", record.get("Y"));
|
assertEquals("2", record.get("Y"));
|
||||||
@ -873,8 +866,8 @@ public class CSVParserTest {
|
|||||||
@Test
|
@Test
|
||||||
public void testSkipSetAltHeaders() throws Exception {
|
public void testSkipSetAltHeaders() 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");
|
||||||
final Iterator<CSVRecord> records = CSVFormat.DEFAULT.withHeader("X", "Y", "Z").withSkipHeaderRecord()
|
final Iterator<CSVRecord> records = CSVFormat.DEFAULT.withHeader("X", "Y", "Z").withSkipHeaderRecord().parse(in)
|
||||||
.parse(in).iterator();
|
.iterator();
|
||||||
final CSVRecord record = records.next();
|
final CSVRecord record = records.next();
|
||||||
assertEquals("1", record.get("X"));
|
assertEquals("1", record.get("X"));
|
||||||
assertEquals("2", record.get("Y"));
|
assertEquals("2", record.get("Y"));
|
||||||
@ -884,8 +877,8 @@ public class CSVParserTest {
|
|||||||
@Test
|
@Test
|
||||||
public void testSkipSetHeader() throws Exception {
|
public void testSkipSetHeader() 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");
|
||||||
final Iterator<CSVRecord> records = CSVFormat.DEFAULT.withHeader("a", "b", "c").withSkipHeaderRecord()
|
final Iterator<CSVRecord> records = CSVFormat.DEFAULT.withHeader("a", "b", "c").withSkipHeaderRecord().parse(in)
|
||||||
.parse(in).iterator();
|
.iterator();
|
||||||
final CSVRecord record = records.next();
|
final CSVRecord record = records.next();
|
||||||
assertEquals("1", record.get("a"));
|
assertEquals("1", record.get("a"));
|
||||||
assertEquals("2", record.get("b"));
|
assertEquals("2", record.get("b"));
|
||||||
@ -900,22 +893,22 @@ public class CSVParserTest {
|
|||||||
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) {
|
||||||
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());
|
||||||
assertTrue(records.size() > 0);
|
assertTrue(records.size() > 0);
|
||||||
for (int i = 0; i < res.length; i++) {
|
for (int i = 0; i < res.length; i++) {
|
||||||
assertArrayEquals(res[i], records.get(i).values());
|
assertArrayEquals(res[i], records.get(i).values());
|
||||||
}
|
}
|
||||||
parser.close();
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testTrailingDelimiter() throws Exception {
|
public void testTrailingDelimiter() throws Exception {
|
||||||
final Reader in = new StringReader("a,a,a,\n\"1\",\"2\",\"3\",\nx,y,z,");
|
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()
|
final Iterator<CSVRecord> records = CSVFormat.DEFAULT.withHeader("X", "Y", "Z").withSkipHeaderRecord()
|
||||||
.parse(in).iterator();
|
.withTrailingDelimiter().parse(in).iterator();
|
||||||
final CSVRecord record = records.next();
|
final CSVRecord record = records.next();
|
||||||
assertEquals("1", record.get("X"));
|
assertEquals("1", record.get("X"));
|
||||||
assertEquals("2", record.get("Y"));
|
assertEquals("2", record.get("Y"));
|
||||||
@ -926,8 +919,8 @@ public class CSVParserTest {
|
|||||||
@Test
|
@Test
|
||||||
public void testTrim() throws Exception {
|
public void testTrim() throws Exception {
|
||||||
final Reader in = new StringReader("a,a,a\n\" 1 \",\" 2 \",\" 3 \"\nx,y,z");
|
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()
|
final Iterator<CSVRecord> records = CSVFormat.DEFAULT.withHeader("X", "Y", "Z").withSkipHeaderRecord()
|
||||||
.parse(in).iterator();
|
.withTrim().parse(in).iterator();
|
||||||
final CSVRecord record = records.next();
|
final CSVRecord record = records.next();
|
||||||
assertEquals("1", record.get("X"));
|
assertEquals("1", record.get("X"));
|
||||||
assertEquals("2", record.get("Y"));
|
assertEquals("2", record.get("Y"));
|
||||||
@ -936,8 +929,8 @@ public class CSVParserTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void validateLineNumbers(final String lineSeparator) throws IOException {
|
private void validateLineNumbers(final String lineSeparator) throws IOException {
|
||||||
final CSVParser parser = CSVParser.parse("a" + lineSeparator + "b" + lineSeparator + "c",
|
try (final CSVParser parser = CSVParser.parse("a" + lineSeparator + "b" + lineSeparator + "c",
|
||||||
CSVFormat.DEFAULT.withRecordSeparator(lineSeparator));
|
CSVFormat.DEFAULT.withRecordSeparator(lineSeparator))) {
|
||||||
assertEquals(0, parser.getCurrentLineNumber());
|
assertEquals(0, parser.getCurrentLineNumber());
|
||||||
assertNotNull(parser.nextRecord());
|
assertNotNull(parser.nextRecord());
|
||||||
assertEquals(1, parser.getCurrentLineNumber());
|
assertEquals(1, parser.getCurrentLineNumber());
|
||||||
@ -949,12 +942,12 @@ public class CSVParserTest {
|
|||||||
assertNull(parser.nextRecord());
|
assertNull(parser.nextRecord());
|
||||||
// Still 2 because the last line is does not have EOL chars
|
// Still 2 because the last line is does not have EOL chars
|
||||||
assertEquals(2, parser.getCurrentLineNumber());
|
assertEquals(2, parser.getCurrentLineNumber());
|
||||||
parser.close();
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void validateRecordNumbers(final String lineSeparator) throws IOException {
|
private void validateRecordNumbers(final String lineSeparator) throws IOException {
|
||||||
final CSVParser parser = CSVParser.parse("a" + lineSeparator + "b" + lineSeparator + "c",
|
try (final CSVParser parser = CSVParser.parse("a" + lineSeparator + "b" + lineSeparator + "c",
|
||||||
CSVFormat.DEFAULT.withRecordSeparator(lineSeparator));
|
CSVFormat.DEFAULT.withRecordSeparator(lineSeparator))) {
|
||||||
CSVRecord record;
|
CSVRecord record;
|
||||||
assertEquals(0, parser.getRecordNumber());
|
assertEquals(0, parser.getRecordNumber());
|
||||||
assertNotNull(record = parser.nextRecord());
|
assertNotNull(record = parser.nextRecord());
|
||||||
@ -968,7 +961,7 @@ public class CSVParserTest {
|
|||||||
assertEquals(3, parser.getRecordNumber());
|
assertEquals(3, parser.getRecordNumber());
|
||||||
assertNull(record = parser.nextRecord());
|
assertNull(record = parser.nextRecord());
|
||||||
assertEquals(3, parser.getRecordNumber());
|
assertEquals(3, parser.getRecordNumber());
|
||||||
parser.close();
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void validateRecordPosition(final String lineSeparator) throws IOException {
|
private void validateRecordPosition(final String lineSeparator) throws IOException {
|
||||||
|
@ -38,6 +38,7 @@ import java.util.List;
|
|||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
|
|
||||||
import org.apache.commons.lang3.ObjectUtils;
|
import org.apache.commons.lang3.ObjectUtils;
|
||||||
|
import org.junit.Assert;
|
||||||
import org.junit.Ignore;
|
import org.junit.Ignore;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
@ -74,7 +75,7 @@ public class CSVPrinterTest {
|
|||||||
final String[][] lines = generateLines(nLines, nCol);
|
final String[][] lines = generateLines(nLines, nCol);
|
||||||
|
|
||||||
final StringWriter sw = new StringWriter();
|
final StringWriter sw = new StringWriter();
|
||||||
final CSVPrinter printer = new CSVPrinter(sw, format);
|
try (final CSVPrinter printer = new CSVPrinter(sw, format)) {
|
||||||
|
|
||||||
for (int i = 0; i < nLines; i++) {
|
for (int i = 0; i < nLines; i++) {
|
||||||
// for (int j=0; j<lines[i].length; j++) System.out.println("### VALUE=:" + printable(lines[i][j]));
|
// for (int j=0; j<lines[i].length; j++) System.out.println("### VALUE=:" + printable(lines[i][j]));
|
||||||
@ -82,11 +83,11 @@ public class CSVPrinterTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
printer.flush();
|
printer.flush();
|
||||||
printer.close();
|
}
|
||||||
final String result = sw.toString();
|
final String result = sw.toString();
|
||||||
// System.out.println("### :" + printable(result));
|
// System.out.println("### :" + printable(result));
|
||||||
|
|
||||||
final CSVParser parser = CSVParser.parse(result, format);
|
try (final CSVParser parser = CSVParser.parse(result, format)) {
|
||||||
final List<CSVRecord> parseResult = parser.getRecords();
|
final List<CSVRecord> parseResult = parser.getRecords();
|
||||||
|
|
||||||
final String[][] expected = lines.clone();
|
final String[][] expected = lines.clone();
|
||||||
@ -94,7 +95,7 @@ public class CSVPrinterTest {
|
|||||||
expected[i] = expectNulls(expected[i], format);
|
expected[i] = expectNulls(expected[i], format);
|
||||||
}
|
}
|
||||||
Utils.compare("Printer output :" + printable(result), expected, parseResult);
|
Utils.compare("Printer output :" + printable(result), expected, parseResult);
|
||||||
parser.close();
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void doRandom(final CSVFormat format, final int iter) throws Exception {
|
private void doRandom(final CSVFormat format, final int iter) throws Exception {
|
||||||
@ -197,95 +198,91 @@ public class CSVPrinterTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void setUpTable(final Connection connection) throws SQLException {
|
private void setUpTable(final Connection connection) throws SQLException {
|
||||||
final Statement statement = connection.createStatement();
|
try (final Statement statement = connection.createStatement()) {
|
||||||
try {
|
|
||||||
statement.execute("CREATE TABLE TEST(ID INT PRIMARY KEY, NAME VARCHAR(255))");
|
statement.execute("CREATE TABLE TEST(ID INT PRIMARY KEY, NAME VARCHAR(255))");
|
||||||
statement.execute("insert into TEST values(1, 'r1')");
|
statement.execute("insert into TEST values(1, 'r1')");
|
||||||
statement.execute("insert into TEST values(2, 'r2')");
|
statement.execute("insert into TEST values(2, 'r2')");
|
||||||
} finally {
|
|
||||||
statement.close();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testDelimeterQuoted() throws IOException {
|
public void testDelimeterQuoted() throws IOException {
|
||||||
final StringWriter sw = new StringWriter();
|
final StringWriter sw = new StringWriter();
|
||||||
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withQuote('\''));
|
try (final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withQuote('\''))) {
|
||||||
printer.print("a,b,c");
|
printer.print("a,b,c");
|
||||||
printer.print("xyz");
|
printer.print("xyz");
|
||||||
assertEquals("'a,b,c',xyz", sw.toString());
|
assertEquals("'a,b,c',xyz", sw.toString());
|
||||||
printer.close();
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testDelimeterQuoteNONE() throws IOException {
|
public void testDelimeterQuoteNONE() throws IOException {
|
||||||
final StringWriter sw = new StringWriter();
|
final StringWriter sw = new StringWriter();
|
||||||
final CSVFormat format = CSVFormat.DEFAULT.withEscape('!').withQuoteMode(QuoteMode.NONE);
|
final CSVFormat format = CSVFormat.DEFAULT.withEscape('!').withQuoteMode(QuoteMode.NONE);
|
||||||
final CSVPrinter printer = new CSVPrinter(sw, format);
|
try (final CSVPrinter printer = new CSVPrinter(sw, format)) {
|
||||||
printer.print("a,b,c");
|
printer.print("a,b,c");
|
||||||
printer.print("xyz");
|
printer.print("xyz");
|
||||||
assertEquals("a!,b!,c,xyz", sw.toString());
|
assertEquals("a!,b!,c,xyz", sw.toString());
|
||||||
printer.close();
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testDelimiterEscaped() throws IOException {
|
public void testDelimiterEscaped() throws IOException {
|
||||||
final StringWriter sw = new StringWriter();
|
final StringWriter sw = new StringWriter();
|
||||||
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withEscape('!').withQuote(null));
|
try (final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withEscape('!').withQuote(null))) {
|
||||||
printer.print("a,b,c");
|
printer.print("a,b,c");
|
||||||
printer.print("xyz");
|
printer.print("xyz");
|
||||||
assertEquals("a!,b!,c,xyz", sw.toString());
|
assertEquals("a!,b!,c,xyz", sw.toString());
|
||||||
printer.close();
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testDelimiterPlain() throws IOException {
|
public void testDelimiterPlain() throws IOException {
|
||||||
final StringWriter sw = new StringWriter();
|
final StringWriter sw = new StringWriter();
|
||||||
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withQuote(null));
|
try (final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withQuote(null))) {
|
||||||
printer.print("a,b,c");
|
printer.print("a,b,c");
|
||||||
printer.print("xyz");
|
printer.print("xyz");
|
||||||
assertEquals("a,b,c,xyz", sw.toString());
|
assertEquals("a,b,c,xyz", sw.toString());
|
||||||
printer.close();
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testDisabledComment() throws IOException {
|
public void testDisabledComment() throws IOException {
|
||||||
final StringWriter sw = new StringWriter();
|
final StringWriter sw = new StringWriter();
|
||||||
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT);
|
try (final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT)) {
|
||||||
printer.printComment("This is a comment");
|
printer.printComment("This is a comment");
|
||||||
|
|
||||||
assertEquals("", sw.toString());
|
assertEquals("", sw.toString());
|
||||||
printer.close();
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testEOLEscaped() throws IOException {
|
public void testEOLEscaped() throws IOException {
|
||||||
final StringWriter sw = new StringWriter();
|
final StringWriter sw = new StringWriter();
|
||||||
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withQuote(null).withEscape('!'));
|
try (final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withQuote(null).withEscape('!'))) {
|
||||||
printer.print("a\rb\nc");
|
printer.print("a\rb\nc");
|
||||||
printer.print("x\fy\bz");
|
printer.print("x\fy\bz");
|
||||||
assertEquals("a!rb!nc,x\fy\bz", sw.toString());
|
assertEquals("a!rb!nc,x\fy\bz", sw.toString());
|
||||||
printer.close();
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testEOLPlain() throws IOException {
|
public void testEOLPlain() throws IOException {
|
||||||
final StringWriter sw = new StringWriter();
|
final StringWriter sw = new StringWriter();
|
||||||
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withQuote(null));
|
try (final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withQuote(null))) {
|
||||||
printer.print("a\rb\nc");
|
printer.print("a\rb\nc");
|
||||||
printer.print("x\fy\bz");
|
printer.print("x\fy\bz");
|
||||||
assertEquals("a\rb\nc,x\fy\bz", sw.toString());
|
assertEquals("a\rb\nc,x\fy\bz", sw.toString());
|
||||||
printer.close();
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testEOLQuoted() throws IOException {
|
public void testEOLQuoted() throws IOException {
|
||||||
final StringWriter sw = new StringWriter();
|
final StringWriter sw = new StringWriter();
|
||||||
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withQuote('\''));
|
try (final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withQuote('\''))) {
|
||||||
printer.print("a\rb\nc");
|
printer.print("a\rb\nc");
|
||||||
printer.print("x\by\fz");
|
printer.print("x\by\fz");
|
||||||
assertEquals("'a\rb\nc',x\by\fz", sw.toString());
|
assertEquals("'a\rb\nc',x\by\fz", sw.toString());
|
||||||
printer.close();
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -293,33 +290,33 @@ public class CSVPrinterTest {
|
|||||||
StringWriter sw = new StringWriter();
|
StringWriter sw = new StringWriter();
|
||||||
final char quoteChar = '\'';
|
final char quoteChar = '\'';
|
||||||
final String eol = "\r\n";
|
final String eol = "\r\n";
|
||||||
CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withQuote(quoteChar));
|
try (final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withQuote(quoteChar))) {
|
||||||
printer.print("\\");
|
printer.print("\\");
|
||||||
printer.close();
|
}
|
||||||
assertEquals("'\\'", sw.toString());
|
assertEquals("'\\'", sw.toString());
|
||||||
|
|
||||||
sw = new StringWriter();
|
sw = new StringWriter();
|
||||||
printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withQuote(quoteChar));
|
try (final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withQuote(quoteChar))) {
|
||||||
printer.print("\\\r");
|
printer.print("\\\r");
|
||||||
printer.close();
|
}
|
||||||
assertEquals("'\\\r'", sw.toString());
|
assertEquals("'\\\r'", sw.toString());
|
||||||
|
|
||||||
sw = new StringWriter();
|
sw = new StringWriter();
|
||||||
printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withQuote(quoteChar));
|
try (final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withQuote(quoteChar))) {
|
||||||
printer.print("X\\\r");
|
printer.print("X\\\r");
|
||||||
printer.close();
|
}
|
||||||
assertEquals("'X\\\r'", sw.toString());
|
assertEquals("'X\\\r'", sw.toString());
|
||||||
|
|
||||||
sw = new StringWriter();
|
sw = new StringWriter();
|
||||||
printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withQuote(quoteChar));
|
try (final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withQuote(quoteChar))) {
|
||||||
printer.printRecord(new Object[] { "\\\r" });
|
printer.printRecord(new Object[] { "\\\r" });
|
||||||
printer.close();
|
}
|
||||||
assertEquals("'\\\r'" + eol, sw.toString());
|
assertEquals("'\\\r'" + eol, sw.toString());
|
||||||
|
|
||||||
sw = new StringWriter();
|
sw = new StringWriter();
|
||||||
printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withQuote(quoteChar));
|
try (final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withQuote(quoteChar))) {
|
||||||
printer.print("\\\\");
|
printer.print("\\\\");
|
||||||
printer.close();
|
}
|
||||||
assertEquals("'\\\\'", sw.toString());
|
assertEquals("'\\\\'", sw.toString());
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -327,66 +324,68 @@ public class CSVPrinterTest {
|
|||||||
@Test
|
@Test
|
||||||
public void testExcelPrintAllArrayOfArrays() throws IOException {
|
public void testExcelPrintAllArrayOfArrays() throws IOException {
|
||||||
final StringWriter sw = new StringWriter();
|
final StringWriter sw = new StringWriter();
|
||||||
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.EXCEL);
|
try (final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.EXCEL)) {
|
||||||
printer.printRecords((Object[]) new String[][] { { "r1c1", "r1c2" }, { "r2c1", "r2c2" } });
|
printer.printRecords((Object[]) new String[][] { { "r1c1", "r1c2" }, { "r2c1", "r2c2" } });
|
||||||
assertEquals("r1c1,r1c2" + recordSeparator + "r2c1,r2c2" + recordSeparator, sw.toString());
|
assertEquals("r1c1,r1c2" + recordSeparator + "r2c1,r2c2" + recordSeparator, sw.toString());
|
||||||
printer.close();
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testExcelPrintAllArrayOfLists() throws IOException {
|
public void testExcelPrintAllArrayOfLists() throws IOException {
|
||||||
final StringWriter sw = new StringWriter();
|
final StringWriter sw = new StringWriter();
|
||||||
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.EXCEL);
|
try (final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.EXCEL)) {
|
||||||
printer.printRecords((Object[]) new List[] { Arrays.asList("r1c1", "r1c2"), Arrays.asList("r2c1", "r2c2") });
|
printer.printRecords(
|
||||||
|
(Object[]) new List[] { Arrays.asList("r1c1", "r1c2"), Arrays.asList("r2c1", "r2c2") });
|
||||||
assertEquals("r1c1,r1c2" + recordSeparator + "r2c1,r2c2" + recordSeparator, sw.toString());
|
assertEquals("r1c1,r1c2" + recordSeparator + "r2c1,r2c2" + recordSeparator, sw.toString());
|
||||||
printer.close();
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testExcelPrintAllIterableOfArrays() throws IOException {
|
public void testExcelPrintAllIterableOfArrays() throws IOException {
|
||||||
final StringWriter sw = new StringWriter();
|
final StringWriter sw = new StringWriter();
|
||||||
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.EXCEL);
|
try (final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.EXCEL)) {
|
||||||
printer.printRecords(Arrays.asList(new String[][] { { "r1c1", "r1c2" }, { "r2c1", "r2c2" } }));
|
printer.printRecords(Arrays.asList(new String[][] { { "r1c1", "r1c2" }, { "r2c1", "r2c2" } }));
|
||||||
assertEquals("r1c1,r1c2" + recordSeparator + "r2c1,r2c2" + recordSeparator, sw.toString());
|
assertEquals("r1c1,r1c2" + recordSeparator + "r2c1,r2c2" + recordSeparator, sw.toString());
|
||||||
printer.close();
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testExcelPrintAllIterableOfLists() throws IOException {
|
public void testExcelPrintAllIterableOfLists() throws IOException {
|
||||||
final StringWriter sw = new StringWriter();
|
final StringWriter sw = new StringWriter();
|
||||||
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.EXCEL);
|
try (final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.EXCEL)) {
|
||||||
printer.printRecords(
|
printer.printRecords(
|
||||||
Arrays.asList(new List[] { Arrays.asList("r1c1", "r1c2"), Arrays.asList("r2c1", "r2c2") }));
|
Arrays.asList(new List[] { Arrays.asList("r1c1", "r1c2"), Arrays.asList("r2c1", "r2c2") }));
|
||||||
assertEquals("r1c1,r1c2" + recordSeparator + "r2c1,r2c2" + recordSeparator, sw.toString());
|
assertEquals("r1c1,r1c2" + recordSeparator + "r2c1,r2c2" + recordSeparator, sw.toString());
|
||||||
printer.close();
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testExcelPrinter1() throws IOException {
|
public void testExcelPrinter1() throws IOException {
|
||||||
final StringWriter sw = new StringWriter();
|
final StringWriter sw = new StringWriter();
|
||||||
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.EXCEL);
|
try (final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.EXCEL)) {
|
||||||
printer.printRecord("a", "b");
|
printer.printRecord("a", "b");
|
||||||
assertEquals("a,b" + recordSeparator, sw.toString());
|
assertEquals("a,b" + recordSeparator, sw.toString());
|
||||||
printer.close();
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testExcelPrinter2() throws IOException {
|
public void testExcelPrinter2() throws IOException {
|
||||||
final StringWriter sw = new StringWriter();
|
final StringWriter sw = new StringWriter();
|
||||||
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.EXCEL);
|
try (final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.EXCEL)) {
|
||||||
printer.printRecord("a,b", "b");
|
printer.printRecord("a,b", "b");
|
||||||
assertEquals("\"a,b\",b" + recordSeparator, sw.toString());
|
assertEquals("\"a,b\",b" + recordSeparator, sw.toString());
|
||||||
printer.close();
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testHeader() throws IOException {
|
public void testHeader() throws IOException {
|
||||||
final StringWriter sw = new StringWriter();
|
final StringWriter sw = new StringWriter();
|
||||||
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withQuote(null).withHeader("C1", "C2", "C3"));
|
try (final CSVPrinter printer = new CSVPrinter(sw,
|
||||||
|
CSVFormat.DEFAULT.withQuote(null).withHeader("C1", "C2", "C3"))) {
|
||||||
printer.printRecord("a", "b", "c");
|
printer.printRecord("a", "b", "c");
|
||||||
printer.printRecord("x", "y", "z");
|
printer.printRecord("x", "y", "z");
|
||||||
assertEquals("C1,C2,C3\r\na,b,c\r\nx,y,z\r\n", sw.toString());
|
assertEquals("C1,C2,C3\r\na,b,c\r\nx,y,z\r\n", sw.toString());
|
||||||
printer.close();
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -394,10 +393,10 @@ public class CSVPrinterTest {
|
|||||||
final StringWriter sw = new StringWriter();
|
final StringWriter sw = new StringWriter();
|
||||||
final Date now = new Date();
|
final Date now = new Date();
|
||||||
final CSVFormat format = CSVFormat.EXCEL;
|
final CSVFormat format = CSVFormat.EXCEL;
|
||||||
final CSVPrinter csvPrinter = printWithHeaderComments(sw, now, format);
|
try (final CSVPrinter csvPrinter = printWithHeaderComments(sw, now, format)) {
|
||||||
assertEquals("# Generated by Apache Commons CSV 1.1\r\n# " + now + "\r\nCol1,Col2\r\nA,B\r\nC,D\r\n",
|
assertEquals("# Generated by Apache Commons CSV 1.1\r\n# " + now + "\r\nCol1,Col2\r\nA,B\r\nC,D\r\n",
|
||||||
sw.toString());
|
sw.toString());
|
||||||
csvPrinter.close();
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -405,87 +404,71 @@ public class CSVPrinterTest {
|
|||||||
final StringWriter sw = new StringWriter();
|
final StringWriter sw = new StringWriter();
|
||||||
final Date now = new Date();
|
final Date now = new Date();
|
||||||
final CSVFormat format = CSVFormat.TDF;
|
final CSVFormat format = CSVFormat.TDF;
|
||||||
final CSVPrinter csvPrinter = printWithHeaderComments(sw, now, format);
|
try (final CSVPrinter csvPrinter = printWithHeaderComments(sw, now, format)) {
|
||||||
assertEquals("# Generated by Apache Commons CSV 1.1\r\n# " + now + "\r\nCol1\tCol2\r\nA\tB\r\nC\tD\r\n",
|
assertEquals("# Generated by Apache Commons CSV 1.1\r\n# " + now + "\r\nCol1\tCol2\r\nA\tB\r\nC\tD\r\n",
|
||||||
sw.toString());
|
sw.toString());
|
||||||
csvPrinter.close();
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testHeaderNotSet() throws IOException {
|
public void testHeaderNotSet() throws IOException {
|
||||||
final StringWriter sw = new StringWriter();
|
final StringWriter sw = new StringWriter();
|
||||||
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withQuote(null));
|
try (final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withQuote(null))) {
|
||||||
printer.printRecord("a", "b", "c");
|
printer.printRecord("a", "b", "c");
|
||||||
printer.printRecord("x", "y", "z");
|
printer.printRecord("x", "y", "z");
|
||||||
assertEquals("a,b,c\r\nx,y,z\r\n", sw.toString());
|
assertEquals("a,b,c\r\nx,y,z\r\n", sw.toString());
|
||||||
printer.close();
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected = IllegalArgumentException.class)
|
@Test(expected = IllegalArgumentException.class)
|
||||||
public void testInvalidFormat() throws Exception {
|
public void testInvalidFormat() throws Exception {
|
||||||
final CSVFormat invalidFormat = CSVFormat.DEFAULT.withDelimiter(CR);
|
final CSVFormat invalidFormat = CSVFormat.DEFAULT.withDelimiter(CR);
|
||||||
new CSVPrinter(new StringWriter(), invalidFormat).close();
|
try (final CSVPrinter printer = new CSVPrinter(new StringWriter(), invalidFormat)) {
|
||||||
|
Assert.fail("This test should have thrown an exception.");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testJdbcPrinter() throws IOException, ClassNotFoundException, SQLException {
|
public void testJdbcPrinter() throws IOException, ClassNotFoundException, SQLException {
|
||||||
final StringWriter sw = new StringWriter();
|
final StringWriter sw = new StringWriter();
|
||||||
final Connection connection = geH2Connection();
|
try (final Connection connection = geH2Connection()) {
|
||||||
try {
|
|
||||||
setUpTable(connection);
|
setUpTable(connection);
|
||||||
final Statement stmt = connection.createStatement();
|
try (final Statement stmt = connection.createStatement();
|
||||||
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT);
|
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT)) {
|
||||||
printer.printRecords(stmt.executeQuery("select ID, NAME from TEST"));
|
printer.printRecords(stmt.executeQuery("select ID, NAME from TEST"));
|
||||||
assertEquals("1,r1" + recordSeparator + "2,r2" + recordSeparator, sw.toString());
|
|
||||||
printer.close();
|
|
||||||
} finally {
|
|
||||||
connection.close();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
assertEquals("1,r1" + recordSeparator + "2,r2" + recordSeparator, sw.toString());
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testJdbcPrinterWithResultSet() throws IOException, ClassNotFoundException, SQLException {
|
public void testJdbcPrinterWithResultSet() throws IOException, ClassNotFoundException, SQLException {
|
||||||
final StringWriter sw = new StringWriter();
|
final StringWriter sw = new StringWriter();
|
||||||
Class.forName("org.h2.Driver");
|
Class.forName("org.h2.Driver");
|
||||||
final Connection connection = geH2Connection();
|
try (final Connection connection = geH2Connection();) {
|
||||||
try {
|
|
||||||
setUpTable(connection);
|
setUpTable(connection);
|
||||||
@SuppressWarnings("resource")
|
try (final Statement stmt = connection.createStatement();
|
||||||
// Closed when the connection is closed.
|
|
||||||
final Statement stmt = connection.createStatement();
|
|
||||||
@SuppressWarnings("resource")
|
|
||||||
// Closed when the connection is closed.
|
|
||||||
final ResultSet resultSet = stmt.executeQuery("select ID, NAME from TEST");
|
final ResultSet resultSet = stmt.executeQuery("select ID, NAME from TEST");
|
||||||
final CSVPrinter printer = CSVFormat.DEFAULT.withHeader(resultSet).print(sw);
|
final CSVPrinter printer = CSVFormat.DEFAULT.withHeader(resultSet).print(sw)) {
|
||||||
printer.printRecords(resultSet);
|
printer.printRecords(resultSet);
|
||||||
assertEquals("ID,NAME" + recordSeparator + "1,r1" + recordSeparator + "2,r2" + recordSeparator,
|
|
||||||
sw.toString());
|
|
||||||
printer.close();
|
|
||||||
} finally {
|
|
||||||
connection.close();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
assertEquals("ID,NAME" + recordSeparator + "1,r1" + recordSeparator + "2,r2" + recordSeparator, sw.toString());
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testJdbcPrinterWithResultSetMetaData() throws IOException, ClassNotFoundException, SQLException {
|
public void testJdbcPrinterWithResultSetMetaData() throws IOException, ClassNotFoundException, SQLException {
|
||||||
final StringWriter sw = new StringWriter();
|
final StringWriter sw = new StringWriter();
|
||||||
Class.forName("org.h2.Driver");
|
Class.forName("org.h2.Driver");
|
||||||
final Connection connection = geH2Connection();
|
try (final Connection connection = geH2Connection()) {
|
||||||
try {
|
|
||||||
setUpTable(connection);
|
setUpTable(connection);
|
||||||
@SuppressWarnings("resource")
|
try (final Statement stmt = connection.createStatement();
|
||||||
// Closed when the connection is closed.
|
|
||||||
final Statement stmt = connection.createStatement();
|
|
||||||
@SuppressWarnings("resource")
|
|
||||||
// Closed when the connection is closed.
|
|
||||||
final ResultSet resultSet = stmt.executeQuery("select ID, NAME from TEST");
|
final ResultSet resultSet = stmt.executeQuery("select ID, NAME from TEST");
|
||||||
final CSVPrinter printer = CSVFormat.DEFAULT.withHeader(resultSet.getMetaData()).print(sw);
|
final CSVPrinter printer = CSVFormat.DEFAULT.withHeader(resultSet.getMetaData()).print(sw)) {
|
||||||
printer.printRecords(resultSet);
|
printer.printRecords(resultSet);
|
||||||
assertEquals("ID,NAME" + recordSeparator + "1,r1" + recordSeparator + "2,r2" + recordSeparator,
|
assertEquals("ID,NAME" + recordSeparator + "1,r1" + recordSeparator + "2,r2" + recordSeparator,
|
||||||
sw.toString());
|
sw.toString());
|
||||||
printer.close();
|
}
|
||||||
} finally {
|
|
||||||
connection.close();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -494,11 +477,11 @@ public class CSVPrinterTest {
|
|||||||
public void testJira135_part1() throws IOException {
|
public void testJira135_part1() throws IOException {
|
||||||
final CSVFormat format = CSVFormat.DEFAULT.withRecordSeparator('\n').withQuote('"').withEscape('\\');
|
final CSVFormat format = CSVFormat.DEFAULT.withRecordSeparator('\n').withQuote('"').withEscape('\\');
|
||||||
final StringWriter sw = new StringWriter();
|
final StringWriter sw = new StringWriter();
|
||||||
final CSVPrinter printer = new CSVPrinter(sw, format);
|
|
||||||
final List<String> list = new LinkedList<>();
|
final List<String> list = new LinkedList<>();
|
||||||
|
try (final CSVPrinter printer = new CSVPrinter(sw, format)) {
|
||||||
list.add("\"");
|
list.add("\"");
|
||||||
printer.printRecord(list);
|
printer.printRecord(list);
|
||||||
printer.close();
|
}
|
||||||
final String expected = "\"\\\"\"" + format.getRecordSeparator();
|
final String expected = "\"\\\"\"" + format.getRecordSeparator();
|
||||||
assertEquals(expected, sw.toString());
|
assertEquals(expected, sw.toString());
|
||||||
final String[] record0 = toFirstRecordValues(expected, format);
|
final String[] record0 = toFirstRecordValues(expected, format);
|
||||||
@ -510,11 +493,11 @@ public class CSVPrinterTest {
|
|||||||
public void testJira135_part2() throws IOException {
|
public void testJira135_part2() throws IOException {
|
||||||
final CSVFormat format = CSVFormat.DEFAULT.withRecordSeparator('\n').withQuote('"').withEscape('\\');
|
final CSVFormat format = CSVFormat.DEFAULT.withRecordSeparator('\n').withQuote('"').withEscape('\\');
|
||||||
final StringWriter sw = new StringWriter();
|
final StringWriter sw = new StringWriter();
|
||||||
final CSVPrinter printer = new CSVPrinter(sw, format);
|
|
||||||
final List<String> list = new LinkedList<>();
|
final List<String> list = new LinkedList<>();
|
||||||
|
try (final CSVPrinter printer = new CSVPrinter(sw, format)) {
|
||||||
list.add("\n");
|
list.add("\n");
|
||||||
printer.printRecord(list);
|
printer.printRecord(list);
|
||||||
printer.close();
|
}
|
||||||
final String expected = "\"\\n\"" + format.getRecordSeparator();
|
final String expected = "\"\\n\"" + format.getRecordSeparator();
|
||||||
assertEquals(expected, sw.toString());
|
assertEquals(expected, sw.toString());
|
||||||
final String[] record0 = toFirstRecordValues(expected, format);
|
final String[] record0 = toFirstRecordValues(expected, format);
|
||||||
@ -526,11 +509,11 @@ public class CSVPrinterTest {
|
|||||||
public void testJira135_part3() throws IOException {
|
public void testJira135_part3() throws IOException {
|
||||||
final CSVFormat format = CSVFormat.DEFAULT.withRecordSeparator('\n').withQuote('"').withEscape('\\');
|
final CSVFormat format = CSVFormat.DEFAULT.withRecordSeparator('\n').withQuote('"').withEscape('\\');
|
||||||
final StringWriter sw = new StringWriter();
|
final StringWriter sw = new StringWriter();
|
||||||
final CSVPrinter printer = new CSVPrinter(sw, format);
|
|
||||||
final List<String> list = new LinkedList<>();
|
final List<String> list = new LinkedList<>();
|
||||||
|
try (final CSVPrinter printer = new CSVPrinter(sw, format)) {
|
||||||
list.add("\\");
|
list.add("\\");
|
||||||
printer.printRecord(list);
|
printer.printRecord(list);
|
||||||
printer.close();
|
}
|
||||||
final String expected = "\"\\\\\"" + format.getRecordSeparator();
|
final String expected = "\"\\\\\"" + format.getRecordSeparator();
|
||||||
assertEquals(expected, sw.toString());
|
assertEquals(expected, sw.toString());
|
||||||
final String[] record0 = toFirstRecordValues(expected, format);
|
final String[] record0 = toFirstRecordValues(expected, format);
|
||||||
@ -542,13 +525,13 @@ public class CSVPrinterTest {
|
|||||||
public void testJira135All() throws IOException {
|
public void testJira135All() throws IOException {
|
||||||
final CSVFormat format = CSVFormat.DEFAULT.withRecordSeparator('\n').withQuote('"').withEscape('\\');
|
final CSVFormat format = CSVFormat.DEFAULT.withRecordSeparator('\n').withQuote('"').withEscape('\\');
|
||||||
final StringWriter sw = new StringWriter();
|
final StringWriter sw = new StringWriter();
|
||||||
final CSVPrinter printer = new CSVPrinter(sw, format);
|
|
||||||
final List<String> list = new LinkedList<>();
|
final List<String> list = new LinkedList<>();
|
||||||
|
try (final CSVPrinter printer = new CSVPrinter(sw, format)) {
|
||||||
list.add("\"");
|
list.add("\"");
|
||||||
list.add("\n");
|
list.add("\n");
|
||||||
list.add("\\");
|
list.add("\\");
|
||||||
printer.printRecord(list);
|
printer.printRecord(list);
|
||||||
printer.close();
|
}
|
||||||
final String expected = "\"\\\"\",\"\\n\",\"\\\"" + format.getRecordSeparator();
|
final String expected = "\"\\\"\",\"\\n\",\"\\\"" + format.getRecordSeparator();
|
||||||
assertEquals(expected, sw.toString());
|
assertEquals(expected, sw.toString());
|
||||||
final String[] record0 = toFirstRecordValues(expected, format);
|
final String[] record0 = toFirstRecordValues(expected, format);
|
||||||
@ -558,11 +541,12 @@ public class CSVPrinterTest {
|
|||||||
@Test
|
@Test
|
||||||
public void testMultiLineComment() throws IOException {
|
public void testMultiLineComment() throws IOException {
|
||||||
final StringWriter sw = new StringWriter();
|
final StringWriter sw = new StringWriter();
|
||||||
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withCommentMarker('#'));
|
try (final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withCommentMarker('#'))) {
|
||||||
printer.printComment("This is a comment\non multiple lines");
|
printer.printComment("This is a comment\non multiple lines");
|
||||||
|
|
||||||
assertEquals("# This is a comment" + recordSeparator + "# on multiple lines" + recordSeparator, sw.toString());
|
assertEquals("# This is a comment" + recordSeparator + "# on multiple lines" + recordSeparator,
|
||||||
printer.close();
|
sw.toString());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -570,9 +554,9 @@ public class CSVPrinterTest {
|
|||||||
Object[] s = new String[] { "NULL", null };
|
Object[] s = new String[] { "NULL", null };
|
||||||
CSVFormat format = CSVFormat.MYSQL.withQuote('"').withNullString("NULL").withQuoteMode(QuoteMode.NON_NUMERIC);
|
CSVFormat format = CSVFormat.MYSQL.withQuote('"').withNullString("NULL").withQuoteMode(QuoteMode.NON_NUMERIC);
|
||||||
StringWriter writer = new StringWriter();
|
StringWriter writer = new StringWriter();
|
||||||
CSVPrinter printer = new CSVPrinter(writer, format);
|
try (final CSVPrinter printer = new CSVPrinter(writer, format)) {
|
||||||
printer.printRecord(s);
|
printer.printRecord(s);
|
||||||
printer.close();
|
}
|
||||||
String expected = "\"NULL\"\tNULL\n";
|
String expected = "\"NULL\"\tNULL\n";
|
||||||
assertEquals(expected, writer.toString());
|
assertEquals(expected, writer.toString());
|
||||||
String[] record0 = toFirstRecordValues(expected, format);
|
String[] record0 = toFirstRecordValues(expected, format);
|
||||||
@ -581,9 +565,9 @@ public class CSVPrinterTest {
|
|||||||
s = new String[] { "\\N", null };
|
s = new String[] { "\\N", null };
|
||||||
format = CSVFormat.MYSQL.withNullString("\\N");
|
format = CSVFormat.MYSQL.withNullString("\\N");
|
||||||
writer = new StringWriter();
|
writer = new StringWriter();
|
||||||
printer = new CSVPrinter(writer, format);
|
try (final CSVPrinter printer = new CSVPrinter(writer, format)) {
|
||||||
printer.printRecord(s);
|
printer.printRecord(s);
|
||||||
printer.close();
|
}
|
||||||
expected = "\\\\N\t\\N\n";
|
expected = "\\\\N\t\\N\n";
|
||||||
assertEquals(expected, writer.toString());
|
assertEquals(expected, writer.toString());
|
||||||
record0 = toFirstRecordValues(expected, format);
|
record0 = toFirstRecordValues(expected, format);
|
||||||
@ -592,9 +576,9 @@ public class CSVPrinterTest {
|
|||||||
s = new String[] { "\\N", "A" };
|
s = new String[] { "\\N", "A" };
|
||||||
format = CSVFormat.MYSQL.withNullString("\\N");
|
format = CSVFormat.MYSQL.withNullString("\\N");
|
||||||
writer = new StringWriter();
|
writer = new StringWriter();
|
||||||
printer = new CSVPrinter(writer, format);
|
try (final CSVPrinter printer = new CSVPrinter(writer, format)) {
|
||||||
printer.printRecord(s);
|
printer.printRecord(s);
|
||||||
printer.close();
|
}
|
||||||
expected = "\\\\N\tA\n";
|
expected = "\\\\N\tA\n";
|
||||||
assertEquals(expected, writer.toString());
|
assertEquals(expected, writer.toString());
|
||||||
record0 = toFirstRecordValues(expected, format);
|
record0 = toFirstRecordValues(expected, format);
|
||||||
@ -603,9 +587,9 @@ public class CSVPrinterTest {
|
|||||||
s = new String[] { "\n", "A" };
|
s = new String[] { "\n", "A" };
|
||||||
format = CSVFormat.MYSQL.withNullString("\\N");
|
format = CSVFormat.MYSQL.withNullString("\\N");
|
||||||
writer = new StringWriter();
|
writer = new StringWriter();
|
||||||
printer = new CSVPrinter(writer, format);
|
try (final CSVPrinter printer = new CSVPrinter(writer, format)) {
|
||||||
printer.printRecord(s);
|
printer.printRecord(s);
|
||||||
printer.close();
|
}
|
||||||
expected = "\\n\tA\n";
|
expected = "\\n\tA\n";
|
||||||
assertEquals(expected, writer.toString());
|
assertEquals(expected, writer.toString());
|
||||||
record0 = toFirstRecordValues(expected, format);
|
record0 = toFirstRecordValues(expected, format);
|
||||||
@ -614,9 +598,9 @@ public class CSVPrinterTest {
|
|||||||
s = new String[] { "", null };
|
s = new String[] { "", null };
|
||||||
format = CSVFormat.MYSQL.withNullString("NULL");
|
format = CSVFormat.MYSQL.withNullString("NULL");
|
||||||
writer = new StringWriter();
|
writer = new StringWriter();
|
||||||
printer = new CSVPrinter(writer, format);
|
try (final CSVPrinter printer = new CSVPrinter(writer, format)) {
|
||||||
printer.printRecord(s);
|
printer.printRecord(s);
|
||||||
printer.close();
|
}
|
||||||
expected = "\tNULL\n";
|
expected = "\tNULL\n";
|
||||||
assertEquals(expected, writer.toString());
|
assertEquals(expected, writer.toString());
|
||||||
record0 = toFirstRecordValues(expected, format);
|
record0 = toFirstRecordValues(expected, format);
|
||||||
@ -625,9 +609,9 @@ public class CSVPrinterTest {
|
|||||||
s = new String[] { "", null };
|
s = new String[] { "", null };
|
||||||
format = CSVFormat.MYSQL;
|
format = CSVFormat.MYSQL;
|
||||||
writer = new StringWriter();
|
writer = new StringWriter();
|
||||||
printer = new CSVPrinter(writer, format);
|
try (final CSVPrinter printer = new CSVPrinter(writer, format)) {
|
||||||
printer.printRecord(s);
|
printer.printRecord(s);
|
||||||
printer.close();
|
}
|
||||||
expected = "\t\\N\n";
|
expected = "\t\\N\n";
|
||||||
assertEquals(expected, writer.toString());
|
assertEquals(expected, writer.toString());
|
||||||
record0 = toFirstRecordValues(expected, format);
|
record0 = toFirstRecordValues(expected, format);
|
||||||
@ -636,9 +620,9 @@ public class CSVPrinterTest {
|
|||||||
s = new String[] { "\\N", "", "\u000e,\\\r" };
|
s = new String[] { "\\N", "", "\u000e,\\\r" };
|
||||||
format = CSVFormat.MYSQL;
|
format = CSVFormat.MYSQL;
|
||||||
writer = new StringWriter();
|
writer = new StringWriter();
|
||||||
printer = new CSVPrinter(writer, format);
|
try (final CSVPrinter printer = new CSVPrinter(writer, format)) {
|
||||||
printer.printRecord(s);
|
printer.printRecord(s);
|
||||||
printer.close();
|
}
|
||||||
expected = "\\\\N\t\t\u000e,\\\\\\r\n";
|
expected = "\\\\N\t\t\u000e,\\\\\\r\n";
|
||||||
assertEquals(expected, writer.toString());
|
assertEquals(expected, writer.toString());
|
||||||
record0 = toFirstRecordValues(expected, format);
|
record0 = toFirstRecordValues(expected, format);
|
||||||
@ -647,9 +631,9 @@ public class CSVPrinterTest {
|
|||||||
s = new String[] { "NULL", "\\\r" };
|
s = new String[] { "NULL", "\\\r" };
|
||||||
format = CSVFormat.MYSQL;
|
format = CSVFormat.MYSQL;
|
||||||
writer = new StringWriter();
|
writer = new StringWriter();
|
||||||
printer = new CSVPrinter(writer, format);
|
try (final CSVPrinter printer = new CSVPrinter(writer, format)) {
|
||||||
printer.printRecord(s);
|
printer.printRecord(s);
|
||||||
printer.close();
|
}
|
||||||
expected = "NULL\t\\\\\\r\n";
|
expected = "NULL\t\\\\\\r\n";
|
||||||
assertEquals(expected, writer.toString());
|
assertEquals(expected, writer.toString());
|
||||||
record0 = toFirstRecordValues(expected, format);
|
record0 = toFirstRecordValues(expected, format);
|
||||||
@ -658,9 +642,9 @@ public class CSVPrinterTest {
|
|||||||
s = new String[] { "\\\r" };
|
s = new String[] { "\\\r" };
|
||||||
format = CSVFormat.MYSQL;
|
format = CSVFormat.MYSQL;
|
||||||
writer = new StringWriter();
|
writer = new StringWriter();
|
||||||
printer = new CSVPrinter(writer, format);
|
try (final CSVPrinter printer = new CSVPrinter(writer, format)) {
|
||||||
printer.printRecord(s);
|
printer.printRecord(s);
|
||||||
printer.close();
|
}
|
||||||
expected = "\\\\\\r\n";
|
expected = "\\\\\\r\n";
|
||||||
assertEquals(expected, writer.toString());
|
assertEquals(expected, writer.toString());
|
||||||
record0 = toFirstRecordValues(expected, format);
|
record0 = toFirstRecordValues(expected, format);
|
||||||
@ -674,168 +658,172 @@ public class CSVPrinterTest {
|
|||||||
|
|
||||||
@Test(expected = IllegalArgumentException.class)
|
@Test(expected = IllegalArgumentException.class)
|
||||||
public void testNewCsvPrinterAppendableNullFormat() throws Exception {
|
public void testNewCsvPrinterAppendableNullFormat() throws Exception {
|
||||||
new CSVPrinter(new StringWriter(), null).close();
|
try (final CSVPrinter printer = new CSVPrinter(new StringWriter(), null)) {
|
||||||
|
Assert.fail("This test should have thrown an exception.");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected = IllegalArgumentException.class)
|
@Test(expected = IllegalArgumentException.class)
|
||||||
public void testNewCSVPrinterNullAppendableFormat() throws Exception {
|
public void testNewCSVPrinterNullAppendableFormat() throws Exception {
|
||||||
new CSVPrinter(null, CSVFormat.DEFAULT).close();
|
try (final CSVPrinter printer = new CSVPrinter(null, CSVFormat.DEFAULT)) {
|
||||||
|
Assert.fail("This test should have thrown an exception.");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testParseCustomNullValues() throws IOException {
|
public void testParseCustomNullValues() throws IOException {
|
||||||
final StringWriter sw = new StringWriter();
|
final StringWriter sw = new StringWriter();
|
||||||
final CSVFormat format = CSVFormat.DEFAULT.withNullString("NULL");
|
final CSVFormat format = CSVFormat.DEFAULT.withNullString("NULL");
|
||||||
final CSVPrinter printer = new CSVPrinter(sw, format);
|
try (final CSVPrinter printer = new CSVPrinter(sw, format)) {
|
||||||
printer.printRecord("a", null, "b");
|
printer.printRecord("a", null, "b");
|
||||||
printer.close();
|
}
|
||||||
final String csvString = sw.toString();
|
final String csvString = sw.toString();
|
||||||
assertEquals("a,NULL,b" + recordSeparator, csvString);
|
assertEquals("a,NULL,b" + recordSeparator, csvString);
|
||||||
final Iterable<CSVRecord> iterable = format.parse(new StringReader(csvString));
|
try (final CSVParser iterable = format.parse(new StringReader(csvString))) {
|
||||||
final Iterator<CSVRecord> iterator = iterable.iterator();
|
final Iterator<CSVRecord> iterator = iterable.iterator();
|
||||||
final CSVRecord record = iterator.next();
|
final CSVRecord record = iterator.next();
|
||||||
assertEquals("a", record.get(0));
|
assertEquals("a", record.get(0));
|
||||||
assertEquals(null, record.get(1));
|
assertEquals(null, record.get(1));
|
||||||
assertEquals("b", record.get(2));
|
assertEquals("b", record.get(2));
|
||||||
assertFalse(iterator.hasNext());
|
assertFalse(iterator.hasNext());
|
||||||
((CSVParser) iterable).close();
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testPlainEscaped() throws IOException {
|
public void testPlainEscaped() throws IOException {
|
||||||
final StringWriter sw = new StringWriter();
|
final StringWriter sw = new StringWriter();
|
||||||
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withQuote(null).withEscape('!'));
|
try (final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withQuote(null).withEscape('!'))) {
|
||||||
printer.print("abc");
|
printer.print("abc");
|
||||||
printer.print("xyz");
|
printer.print("xyz");
|
||||||
assertEquals("abc,xyz", sw.toString());
|
assertEquals("abc,xyz", sw.toString());
|
||||||
printer.close();
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testPlainPlain() throws IOException {
|
public void testPlainPlain() throws IOException {
|
||||||
final StringWriter sw = new StringWriter();
|
final StringWriter sw = new StringWriter();
|
||||||
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withQuote(null));
|
try (final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withQuote(null))) {
|
||||||
printer.print("abc");
|
printer.print("abc");
|
||||||
printer.print("xyz");
|
printer.print("xyz");
|
||||||
assertEquals("abc,xyz", sw.toString());
|
assertEquals("abc,xyz", sw.toString());
|
||||||
printer.close();
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testPlainQuoted() throws IOException {
|
public void testPlainQuoted() throws IOException {
|
||||||
final StringWriter sw = new StringWriter();
|
final StringWriter sw = new StringWriter();
|
||||||
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withQuote('\''));
|
try (final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withQuote('\''))) {
|
||||||
printer.print("abc");
|
printer.print("abc");
|
||||||
assertEquals("abc", sw.toString());
|
assertEquals("abc", sw.toString());
|
||||||
printer.close();
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testPrint() throws IOException {
|
public void testPrint() throws IOException {
|
||||||
final StringWriter sw = new StringWriter();
|
final StringWriter sw = new StringWriter();
|
||||||
final CSVPrinter printer = CSVFormat.DEFAULT.print(sw);
|
try (final CSVPrinter printer = CSVFormat.DEFAULT.print(sw)) {
|
||||||
printer.printRecord("a", "b\\c");
|
printer.printRecord("a", "b\\c");
|
||||||
assertEquals("a,b\\c" + recordSeparator, sw.toString());
|
assertEquals("a,b\\c" + recordSeparator, sw.toString());
|
||||||
printer.close();
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testPrintCustomNullValues() throws IOException {
|
public void testPrintCustomNullValues() throws IOException {
|
||||||
final StringWriter sw = new StringWriter();
|
final StringWriter sw = new StringWriter();
|
||||||
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withNullString("NULL"));
|
try (final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withNullString("NULL"))) {
|
||||||
printer.printRecord("a", null, "b");
|
printer.printRecord("a", null, "b");
|
||||||
assertEquals("a,NULL,b" + recordSeparator, sw.toString());
|
assertEquals("a,NULL,b" + recordSeparator, sw.toString());
|
||||||
printer.close();
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testPrinter1() throws IOException {
|
public void testPrinter1() throws IOException {
|
||||||
final StringWriter sw = new StringWriter();
|
final StringWriter sw = new StringWriter();
|
||||||
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT);
|
try (final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT)) {
|
||||||
printer.printRecord("a", "b");
|
printer.printRecord("a", "b");
|
||||||
assertEquals("a,b" + recordSeparator, sw.toString());
|
assertEquals("a,b" + recordSeparator, sw.toString());
|
||||||
printer.close();
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testPrinter2() throws IOException {
|
public void testPrinter2() throws IOException {
|
||||||
final StringWriter sw = new StringWriter();
|
final StringWriter sw = new StringWriter();
|
||||||
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT);
|
try (final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT)) {
|
||||||
printer.printRecord("a,b", "b");
|
printer.printRecord("a,b", "b");
|
||||||
assertEquals("\"a,b\",b" + recordSeparator, sw.toString());
|
assertEquals("\"a,b\",b" + recordSeparator, sw.toString());
|
||||||
printer.close();
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testPrinter3() throws IOException {
|
public void testPrinter3() throws IOException {
|
||||||
final StringWriter sw = new StringWriter();
|
final StringWriter sw = new StringWriter();
|
||||||
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT);
|
try (final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT)) {
|
||||||
printer.printRecord("a, b", "b ");
|
printer.printRecord("a, b", "b ");
|
||||||
assertEquals("\"a, b\",\"b \"" + recordSeparator, sw.toString());
|
assertEquals("\"a, b\",\"b \"" + recordSeparator, sw.toString());
|
||||||
printer.close();
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testPrinter4() throws IOException {
|
public void testPrinter4() throws IOException {
|
||||||
final StringWriter sw = new StringWriter();
|
final StringWriter sw = new StringWriter();
|
||||||
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT);
|
try (final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT)) {
|
||||||
printer.printRecord("a", "b\"c");
|
printer.printRecord("a", "b\"c");
|
||||||
assertEquals("a,\"b\"\"c\"" + recordSeparator, sw.toString());
|
assertEquals("a,\"b\"\"c\"" + recordSeparator, sw.toString());
|
||||||
printer.close();
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testPrinter5() throws IOException {
|
public void testPrinter5() throws IOException {
|
||||||
final StringWriter sw = new StringWriter();
|
final StringWriter sw = new StringWriter();
|
||||||
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT);
|
try (final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT)) {
|
||||||
printer.printRecord("a", "b\nc");
|
printer.printRecord("a", "b\nc");
|
||||||
assertEquals("a,\"b\nc\"" + recordSeparator, sw.toString());
|
assertEquals("a,\"b\nc\"" + recordSeparator, sw.toString());
|
||||||
printer.close();
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testPrinter6() throws IOException {
|
public void testPrinter6() throws IOException {
|
||||||
final StringWriter sw = new StringWriter();
|
final StringWriter sw = new StringWriter();
|
||||||
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT);
|
try (final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT)) {
|
||||||
printer.printRecord("a", "b\r\nc");
|
printer.printRecord("a", "b\r\nc");
|
||||||
assertEquals("a,\"b\r\nc\"" + recordSeparator, sw.toString());
|
assertEquals("a,\"b\r\nc\"" + recordSeparator, sw.toString());
|
||||||
printer.close();
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testPrinter7() throws IOException {
|
public void testPrinter7() throws IOException {
|
||||||
final StringWriter sw = new StringWriter();
|
final StringWriter sw = new StringWriter();
|
||||||
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT);
|
try (final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT)) {
|
||||||
printer.printRecord("a", "b\\c");
|
printer.printRecord("a", "b\\c");
|
||||||
assertEquals("a,b\\c" + recordSeparator, sw.toString());
|
assertEquals("a,b\\c" + recordSeparator, sw.toString());
|
||||||
printer.close();
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testPrintNullValues() throws IOException {
|
public void testPrintNullValues() throws IOException {
|
||||||
final StringWriter sw = new StringWriter();
|
final StringWriter sw = new StringWriter();
|
||||||
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT);
|
try (final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT)) {
|
||||||
printer.printRecord("a", null, "b");
|
printer.printRecord("a", null, "b");
|
||||||
assertEquals("a,,b" + recordSeparator, sw.toString());
|
assertEquals("a,,b" + recordSeparator, sw.toString());
|
||||||
printer.close();
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testQuoteAll() throws IOException {
|
public void testQuoteAll() throws IOException {
|
||||||
final StringWriter sw = new StringWriter();
|
final StringWriter sw = new StringWriter();
|
||||||
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withQuoteMode(QuoteMode.ALL));
|
try (final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withQuoteMode(QuoteMode.ALL))) {
|
||||||
printer.printRecord("a", "b\nc", "d");
|
printer.printRecord("a", "b\nc", "d");
|
||||||
assertEquals("\"a\",\"b\nc\",\"d\"" + recordSeparator, sw.toString());
|
assertEquals("\"a\",\"b\nc\",\"d\"" + recordSeparator, sw.toString());
|
||||||
printer.close();
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testQuoteNonNumeric() throws IOException {
|
public void testQuoteNonNumeric() throws IOException {
|
||||||
final StringWriter sw = new StringWriter();
|
final StringWriter sw = new StringWriter();
|
||||||
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withQuoteMode(QuoteMode.NON_NUMERIC));
|
try (final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withQuoteMode(QuoteMode.NON_NUMERIC))) {
|
||||||
printer.printRecord("a", "b\nc", Integer.valueOf(1));
|
printer.printRecord("a", "b\nc", Integer.valueOf(1));
|
||||||
assertEquals("\"a\",\"b\nc\",1" + recordSeparator, sw.toString());
|
assertEquals("\"a\",\"b\nc\",1" + recordSeparator, sw.toString());
|
||||||
printer.close();
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -866,82 +854,81 @@ public class CSVPrinterTest {
|
|||||||
@Test
|
@Test
|
||||||
public void testSingleLineComment() throws IOException {
|
public void testSingleLineComment() throws IOException {
|
||||||
final StringWriter sw = new StringWriter();
|
final StringWriter sw = new StringWriter();
|
||||||
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withCommentMarker('#'));
|
try (final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withCommentMarker('#'))) {
|
||||||
printer.printComment("This is a comment");
|
printer.printComment("This is a comment");
|
||||||
|
|
||||||
assertEquals("# This is a comment" + recordSeparator, sw.toString());
|
assertEquals("# This is a comment" + recordSeparator, sw.toString());
|
||||||
printer.close();
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testSingleQuoteQuoted() throws IOException {
|
public void testSingleQuoteQuoted() throws IOException {
|
||||||
final StringWriter sw = new StringWriter();
|
final StringWriter sw = new StringWriter();
|
||||||
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withQuote('\''));
|
try (final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withQuote('\''))) {
|
||||||
printer.print("a'b'c");
|
printer.print("a'b'c");
|
||||||
printer.print("xyz");
|
printer.print("xyz");
|
||||||
assertEquals("'a''b''c',xyz", sw.toString());
|
assertEquals("'a''b''c',xyz", sw.toString());
|
||||||
printer.close();
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testSkipHeaderRecordFalse() throws IOException {
|
public void testSkipHeaderRecordFalse() throws IOException {
|
||||||
// functionally identical to testHeader, used to test CSV-153
|
// functionally identical to testHeader, used to test CSV-153
|
||||||
final StringWriter sw = new StringWriter();
|
final StringWriter sw = new StringWriter();
|
||||||
final CSVPrinter printer = new CSVPrinter(sw,
|
try (final CSVPrinter printer = new CSVPrinter(sw,
|
||||||
CSVFormat.DEFAULT.withQuote(null).withHeader("C1", "C2", "C3").withSkipHeaderRecord(false));
|
CSVFormat.DEFAULT.withQuote(null).withHeader("C1", "C2", "C3").withSkipHeaderRecord(false))) {
|
||||||
printer.printRecord("a", "b", "c");
|
printer.printRecord("a", "b", "c");
|
||||||
printer.printRecord("x", "y", "z");
|
printer.printRecord("x", "y", "z");
|
||||||
assertEquals("C1,C2,C3\r\na,b,c\r\nx,y,z\r\n", sw.toString());
|
assertEquals("C1,C2,C3\r\na,b,c\r\nx,y,z\r\n", sw.toString());
|
||||||
printer.close();
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testSkipHeaderRecordTrue() throws IOException {
|
public void testSkipHeaderRecordTrue() throws IOException {
|
||||||
// functionally identical to testHeaderNotSet, used to test CSV-153
|
// functionally identical to testHeaderNotSet, used to test CSV-153
|
||||||
final StringWriter sw = new StringWriter();
|
final StringWriter sw = new StringWriter();
|
||||||
final CSVPrinter printer = new CSVPrinter(sw,
|
try (final CSVPrinter printer = new CSVPrinter(sw,
|
||||||
CSVFormat.DEFAULT.withQuote(null).withHeader("C1", "C2", "C3").withSkipHeaderRecord(true));
|
CSVFormat.DEFAULT.withQuote(null).withHeader("C1", "C2", "C3").withSkipHeaderRecord(true))) {
|
||||||
printer.printRecord("a", "b", "c");
|
printer.printRecord("a", "b", "c");
|
||||||
printer.printRecord("x", "y", "z");
|
printer.printRecord("x", "y", "z");
|
||||||
assertEquals("a,b,c\r\nx,y,z\r\n", sw.toString());
|
assertEquals("a,b,c\r\nx,y,z\r\n", sw.toString());
|
||||||
printer.close();
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testTrimOnOneColumn() throws IOException {
|
public void testTrimOnOneColumn() throws IOException {
|
||||||
final StringWriter sw = new StringWriter();
|
final StringWriter sw = new StringWriter();
|
||||||
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withTrim());
|
try (final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withTrim())) {
|
||||||
printer.print(" A ");
|
printer.print(" A ");
|
||||||
assertEquals("A", sw.toString());
|
assertEquals("A", sw.toString());
|
||||||
printer.close();
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testTrimOnTwoColumns() throws IOException {
|
public void testTrimOnTwoColumns() throws IOException {
|
||||||
final StringWriter sw = new StringWriter();
|
final StringWriter sw = new StringWriter();
|
||||||
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withTrim());
|
try (final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withTrim())) {
|
||||||
printer.print(" A ");
|
printer.print(" A ");
|
||||||
printer.print(" B ");
|
printer.print(" B ");
|
||||||
assertEquals("A,B", sw.toString());
|
assertEquals("A,B", sw.toString());
|
||||||
printer.close();
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testTrailingDelimiterOnTwoColumns() throws IOException {
|
public void testTrailingDelimiterOnTwoColumns() throws IOException {
|
||||||
final StringWriter sw = new StringWriter();
|
final StringWriter sw = new StringWriter();
|
||||||
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withTrailingDelimiter());
|
try (final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withTrailingDelimiter())) {
|
||||||
printer.printRecord("A", "B");
|
printer.printRecord("A", "B");
|
||||||
assertEquals("A,B,\r\n", sw.toString());
|
assertEquals("A,B,\r\n", sw.toString());
|
||||||
printer.close();
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testTrimOffOneColumn() throws IOException {
|
public void testTrimOffOneColumn() throws IOException {
|
||||||
final StringWriter sw = new StringWriter();
|
final StringWriter sw = new StringWriter();
|
||||||
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withTrim(false));
|
try (final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withTrim(false))) {
|
||||||
printer.print(" A ");
|
printer.print(" A ");
|
||||||
assertEquals("\" A \"", sw.toString());
|
assertEquals("\" A \"", sw.toString());
|
||||||
printer.close();
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private String[] toFirstRecordValues(final String expected, final CSVFormat format) throws IOException {
|
private String[] toFirstRecordValues(final String expected, final CSVFormat format) throws IOException {
|
||||||
|
@ -143,7 +143,7 @@ public class CSVRecordTest {
|
|||||||
@Test
|
@Test
|
||||||
public void testRemoveAndAddColumns() throws IOException {
|
public void testRemoveAndAddColumns() throws IOException {
|
||||||
// do:
|
// do:
|
||||||
final CSVPrinter printer = new CSVPrinter(new StringBuilder(), CSVFormat.DEFAULT);
|
try (final CSVPrinter printer = new CSVPrinter(new StringBuilder(), CSVFormat.DEFAULT)) {
|
||||||
final Map<String, String> map = recordWithHeader.toMap();
|
final Map<String, String> map = recordWithHeader.toMap();
|
||||||
map.remove("OldColumn");
|
map.remove("OldColumn");
|
||||||
map.put("ZColumn", "NewValue");
|
map.put("ZColumn", "NewValue");
|
||||||
@ -152,7 +152,7 @@ public class CSVRecordTest {
|
|||||||
Collections.sort(list);
|
Collections.sort(list);
|
||||||
printer.printRecord(list);
|
printer.printRecord(list);
|
||||||
Assert.assertEquals("A,B,C,NewValue" + CSVFormat.DEFAULT.getRecordSeparator(), printer.getOut().toString());
|
Assert.assertEquals("A,B,C,NewValue" + CSVFormat.DEFAULT.getRecordSeparator(), printer.getOut().toString());
|
||||||
printer.close();
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -163,19 +163,21 @@ public class CSVRecordTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testToMapWithShortRecord() throws Exception {
|
public void testToMapWithShortRecord() throws Exception {
|
||||||
final CSVParser parser = CSVParser.parse("a,b", CSVFormat.DEFAULT.withHeader("A", "B", "C"));
|
try (final CSVParser parser = CSVParser.parse("a,b", CSVFormat.DEFAULT.withHeader("A", "B", "C"))) {
|
||||||
final CSVRecord shortRec = parser.iterator().next();
|
final CSVRecord shortRec = parser.iterator().next();
|
||||||
shortRec.toMap();
|
shortRec.toMap();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testToMapWithNoHeader() throws Exception {
|
public void testToMapWithNoHeader() throws Exception {
|
||||||
final CSVParser parser = CSVParser.parse("a,b", CSVFormat.newFormat(','));
|
try (final CSVParser parser = CSVParser.parse("a,b", CSVFormat.newFormat(','))) {
|
||||||
final CSVRecord shortRec = parser.iterator().next();
|
final CSVRecord shortRec = parser.iterator().next();
|
||||||
final Map<String, String> map = shortRec.toMap();
|
final Map<String, String> map = shortRec.toMap();
|
||||||
assertNotNull("Map is not null.", map);
|
assertNotNull("Map is not null.", map);
|
||||||
assertTrue("Map is empty.", map.isEmpty());
|
assertTrue("Map is empty.", map.isEmpty());
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void validateMap(final Map<String, String> map, final boolean allowsNulls) {
|
private void validateMap(final Map<String, String> map, final boolean allowsNulls) {
|
||||||
assertTrue(map.containsKey("first"));
|
assertTrue(map.containsKey("first"));
|
||||||
|
@ -36,18 +36,18 @@ public class ExtendedBufferedReaderTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testEmptyInput() throws Exception {
|
public void testEmptyInput() throws Exception {
|
||||||
final ExtendedBufferedReader br = getBufferedReader("");
|
try (final ExtendedBufferedReader br = createBufferedReader("")) {
|
||||||
assertEquals(END_OF_STREAM, br.read());
|
assertEquals(END_OF_STREAM, br.read());
|
||||||
assertEquals(END_OF_STREAM, br.lookAhead());
|
assertEquals(END_OF_STREAM, br.lookAhead());
|
||||||
assertEquals(END_OF_STREAM, br.getLastChar());
|
assertEquals(END_OF_STREAM, br.getLastChar());
|
||||||
assertNull(br.readLine());
|
assertNull(br.readLine());
|
||||||
assertEquals(0, br.read(new char[10], 0, 0));
|
assertEquals(0, br.read(new char[10], 0, 0));
|
||||||
br.close();
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testReadLookahead1() throws Exception {
|
public void testReadLookahead1() throws Exception {
|
||||||
final ExtendedBufferedReader br = getBufferedReader("1\n2\r3\n");
|
try (final ExtendedBufferedReader br = createBufferedReader("1\n2\r3\n")) {
|
||||||
assertEquals(0, br.getCurrentLineNumber());
|
assertEquals(0, br.getCurrentLineNumber());
|
||||||
assertEquals('1', br.lookAhead());
|
assertEquals('1', br.lookAhead());
|
||||||
assertEquals(UNDEFINED, br.getLastChar());
|
assertEquals(UNDEFINED, br.getLastChar());
|
||||||
@ -101,7 +101,7 @@ public class ExtendedBufferedReaderTest {
|
|||||||
assertEquals(END_OF_STREAM, br.lookAhead());
|
assertEquals(END_OF_STREAM, br.lookAhead());
|
||||||
assertEquals(3, br.getCurrentLineNumber());
|
assertEquals(3, br.getCurrentLineNumber());
|
||||||
|
|
||||||
br.close();
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -109,7 +109,7 @@ public class ExtendedBufferedReaderTest {
|
|||||||
final char[] ref = new char[5];
|
final char[] ref = new char[5];
|
||||||
final char[] res = new char[5];
|
final char[] res = new char[5];
|
||||||
|
|
||||||
final ExtendedBufferedReader br = getBufferedReader("abcdefg");
|
try (final ExtendedBufferedReader br = createBufferedReader("abcdefg")) {
|
||||||
ref[0] = 'a';
|
ref[0] = 'a';
|
||||||
ref[1] = 'b';
|
ref[1] = 'b';
|
||||||
ref[2] = 'c';
|
ref[2] = 'c';
|
||||||
@ -122,21 +122,19 @@ public class ExtendedBufferedReaderTest {
|
|||||||
assertEquals(1, br.read(res, 4, 1));
|
assertEquals(1, br.read(res, 4, 1));
|
||||||
assertArrayEquals(ref, res);
|
assertArrayEquals(ref, res);
|
||||||
assertEquals('d', br.getLastChar());
|
assertEquals('d', br.getLastChar());
|
||||||
br.close();
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testReadLine() throws Exception {
|
public void testReadLine() throws Exception {
|
||||||
ExtendedBufferedReader br = getBufferedReader("");
|
try (final ExtendedBufferedReader br = createBufferedReader("")) {
|
||||||
assertNull(br.readLine());
|
assertNull(br.readLine());
|
||||||
|
}
|
||||||
br.close();
|
try (final ExtendedBufferedReader br = createBufferedReader("\n")) {
|
||||||
br = getBufferedReader("\n");
|
|
||||||
assertEquals("", br.readLine());
|
assertEquals("", br.readLine());
|
||||||
assertNull(br.readLine());
|
assertNull(br.readLine());
|
||||||
|
}
|
||||||
br.close();
|
try (final ExtendedBufferedReader br = createBufferedReader("foo\n\nhello")) {
|
||||||
br = getBufferedReader("foo\n\nhello");
|
|
||||||
assertEquals(0, br.getCurrentLineNumber());
|
assertEquals(0, br.getCurrentLineNumber());
|
||||||
assertEquals("foo", br.readLine());
|
assertEquals("foo", br.readLine());
|
||||||
assertEquals(1, br.getCurrentLineNumber());
|
assertEquals(1, br.getCurrentLineNumber());
|
||||||
@ -146,9 +144,8 @@ public class ExtendedBufferedReaderTest {
|
|||||||
assertEquals(3, br.getCurrentLineNumber());
|
assertEquals(3, br.getCurrentLineNumber());
|
||||||
assertNull(br.readLine());
|
assertNull(br.readLine());
|
||||||
assertEquals(3, br.getCurrentLineNumber());
|
assertEquals(3, br.getCurrentLineNumber());
|
||||||
|
}
|
||||||
br.close();
|
try (final ExtendedBufferedReader br = createBufferedReader("foo\n\nhello")) {
|
||||||
br = getBufferedReader("foo\n\nhello");
|
|
||||||
assertEquals('f', br.read());
|
assertEquals('f', br.read());
|
||||||
assertEquals('o', br.lookAhead());
|
assertEquals('o', br.lookAhead());
|
||||||
assertEquals("oo", br.readLine());
|
assertEquals("oo", br.readLine());
|
||||||
@ -160,17 +157,15 @@ public class ExtendedBufferedReaderTest {
|
|||||||
assertEquals("hello", br.readLine());
|
assertEquals("hello", br.readLine());
|
||||||
assertNull(br.readLine());
|
assertNull(br.readLine());
|
||||||
assertEquals(3, br.getCurrentLineNumber());
|
assertEquals(3, br.getCurrentLineNumber());
|
||||||
|
}
|
||||||
|
try (final ExtendedBufferedReader br = createBufferedReader("foo\rbaar\r\nfoo")) {
|
||||||
br.close();
|
|
||||||
br = getBufferedReader("foo\rbaar\r\nfoo");
|
|
||||||
assertEquals("foo", br.readLine());
|
assertEquals("foo", br.readLine());
|
||||||
assertEquals('b', br.lookAhead());
|
assertEquals('b', br.lookAhead());
|
||||||
assertEquals("baar", br.readLine());
|
assertEquals("baar", br.readLine());
|
||||||
assertEquals('f', br.lookAhead());
|
assertEquals('f', br.lookAhead());
|
||||||
assertEquals("foo", br.readLine());
|
assertEquals("foo", br.readLine());
|
||||||
assertNull(br.readLine());
|
assertNull(br.readLine());
|
||||||
br.close();
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -179,39 +174,39 @@ public class ExtendedBufferedReaderTest {
|
|||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void testReadChar() throws Exception {
|
public void testReadChar() throws Exception {
|
||||||
final String LF="\n"; final String CR="\r"; final String CRLF=CR+LF; final String LFCR=LF+CR;// easier to read the string below
|
final String LF = "\n";
|
||||||
|
final String CR = "\r";
|
||||||
|
final String CRLF = CR + LF;
|
||||||
|
final String LFCR = LF + CR;// easier to read the string below
|
||||||
final String test = "a" + LF + "b" + CR + "c" + LF + LF + "d" + CR + CR + "e" + LFCR + "f " + CRLF;
|
final String test = "a" + LF + "b" + CR + "c" + LF + LF + "d" + CR + CR + "e" + LFCR + "f " + CRLF;
|
||||||
// EOL eol EOL EOL eol eol EOL+CR EOL
|
// EOL eol EOL EOL eol eol EOL+CR EOL
|
||||||
final int EOLeolct = 9;
|
final int EOLeolct = 9;
|
||||||
ExtendedBufferedReader br;
|
|
||||||
|
|
||||||
br = getBufferedReader(test);
|
try (final ExtendedBufferedReader br = createBufferedReader(test)) {
|
||||||
assertEquals(0, br.getCurrentLineNumber());
|
assertEquals(0, br.getCurrentLineNumber());
|
||||||
while (br.readLine() != null) {
|
while (br.readLine() != null) {
|
||||||
// consume all
|
// consume all
|
||||||
}
|
}
|
||||||
assertEquals(EOLeolct, br.getCurrentLineNumber());
|
assertEquals(EOLeolct, br.getCurrentLineNumber());
|
||||||
|
}
|
||||||
br.close();
|
try (final ExtendedBufferedReader br = createBufferedReader(test)) {
|
||||||
br = getBufferedReader(test);
|
|
||||||
assertEquals(0, br.getCurrentLineNumber());
|
assertEquals(0, br.getCurrentLineNumber());
|
||||||
while (br.read() != -1) {
|
while (br.read() != -1) {
|
||||||
// consume all
|
// consume all
|
||||||
}
|
}
|
||||||
assertEquals(EOLeolct, br.getCurrentLineNumber());
|
assertEquals(EOLeolct, br.getCurrentLineNumber());
|
||||||
|
}
|
||||||
br.close();
|
try (final ExtendedBufferedReader br = createBufferedReader(test)) {
|
||||||
br = getBufferedReader(test);
|
|
||||||
assertEquals(0, br.getCurrentLineNumber());
|
assertEquals(0, br.getCurrentLineNumber());
|
||||||
final char[] buff = new char[10];
|
final char[] buff = new char[10];
|
||||||
while (br.read(buff, 0, 3) != -1) {
|
while (br.read(buff, 0, 3) != -1) {
|
||||||
// consume all
|
// consume all
|
||||||
}
|
}
|
||||||
assertEquals(EOLeolct, br.getCurrentLineNumber());
|
assertEquals(EOLeolct, br.getCurrentLineNumber());
|
||||||
br.close();
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private ExtendedBufferedReader getBufferedReader(final String s) {
|
private ExtendedBufferedReader createBufferedReader(final String s) {
|
||||||
return new ExtendedBufferedReader(new StringReader(s));
|
return new ExtendedBufferedReader(new StringReader(s));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -52,14 +52,14 @@ public class LexerTest {
|
|||||||
formatWithEscaping = CSVFormat.DEFAULT.withEscape('\\');
|
formatWithEscaping = CSVFormat.DEFAULT.withEscape('\\');
|
||||||
}
|
}
|
||||||
|
|
||||||
private Lexer getLexer(final String input, final CSVFormat format) {
|
private Lexer createLexer(final String input, final CSVFormat format) {
|
||||||
return new Lexer(format, new ExtendedBufferedReader(new StringReader(input)));
|
return new Lexer(format, new ExtendedBufferedReader(new StringReader(input)));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testSurroundingSpacesAreDeleted() throws IOException {
|
public void testSurroundingSpacesAreDeleted() throws IOException {
|
||||||
final String code = "noSpaces, leadingSpaces,trailingSpaces , surroundingSpaces , ,,";
|
final String code = "noSpaces, leadingSpaces,trailingSpaces , surroundingSpaces , ,,";
|
||||||
final Lexer parser = getLexer(code, CSVFormat.DEFAULT.withIgnoreSurroundingSpaces());
|
try (final Lexer parser = createLexer(code, CSVFormat.DEFAULT.withIgnoreSurroundingSpaces())) {
|
||||||
assertThat(parser.nextToken(new Token()), matches(TOKEN, "noSpaces"));
|
assertThat(parser.nextToken(new Token()), matches(TOKEN, "noSpaces"));
|
||||||
assertThat(parser.nextToken(new Token()), matches(TOKEN, "leadingSpaces"));
|
assertThat(parser.nextToken(new Token()), matches(TOKEN, "leadingSpaces"));
|
||||||
assertThat(parser.nextToken(new Token()), matches(TOKEN, "trailingSpaces"));
|
assertThat(parser.nextToken(new Token()), matches(TOKEN, "trailingSpaces"));
|
||||||
@ -68,11 +68,12 @@ public class LexerTest {
|
|||||||
assertThat(parser.nextToken(new Token()), matches(TOKEN, ""));
|
assertThat(parser.nextToken(new Token()), matches(TOKEN, ""));
|
||||||
assertThat(parser.nextToken(new Token()), matches(EOF, ""));
|
assertThat(parser.nextToken(new Token()), matches(EOF, ""));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testSurroundingTabsAreDeleted() throws IOException {
|
public void testSurroundingTabsAreDeleted() throws IOException {
|
||||||
final String code = "noTabs,\tleadingTab,trailingTab\t,\tsurroundingTabs\t,\t\t,,";
|
final String code = "noTabs,\tleadingTab,trailingTab\t,\tsurroundingTabs\t,\t\t,,";
|
||||||
final Lexer parser = getLexer(code, CSVFormat.DEFAULT.withIgnoreSurroundingSpaces());
|
try (final Lexer parser = createLexer(code, CSVFormat.DEFAULT.withIgnoreSurroundingSpaces())) {
|
||||||
assertThat(parser.nextToken(new Token()), matches(TOKEN, "noTabs"));
|
assertThat(parser.nextToken(new Token()), matches(TOKEN, "noTabs"));
|
||||||
assertThat(parser.nextToken(new Token()), matches(TOKEN, "leadingTab"));
|
assertThat(parser.nextToken(new Token()), matches(TOKEN, "leadingTab"));
|
||||||
assertThat(parser.nextToken(new Token()), matches(TOKEN, "trailingTab"));
|
assertThat(parser.nextToken(new Token()), matches(TOKEN, "trailingTab"));
|
||||||
@ -81,26 +82,14 @@ public class LexerTest {
|
|||||||
assertThat(parser.nextToken(new Token()), matches(TOKEN, ""));
|
assertThat(parser.nextToken(new Token()), matches(TOKEN, ""));
|
||||||
assertThat(parser.nextToken(new Token()), matches(EOF, ""));
|
assertThat(parser.nextToken(new Token()), matches(EOF, ""));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testIgnoreEmptyLines() throws IOException {
|
public void testIgnoreEmptyLines() throws IOException {
|
||||||
final String code =
|
final String code = "first,line,\n" + "\n" + "\n" + "second,line\n" + "\n" + "\n" + "third line \n" + "\n" +
|
||||||
"first,line,\n"+
|
"\n" + "last, line \n" + "\n" + "\n" + "\n";
|
||||||
"\n"+
|
|
||||||
"\n"+
|
|
||||||
"second,line\n"+
|
|
||||||
"\n"+
|
|
||||||
"\n"+
|
|
||||||
"third line \n"+
|
|
||||||
"\n"+
|
|
||||||
"\n"+
|
|
||||||
"last, line \n"+
|
|
||||||
"\n"+
|
|
||||||
"\n"+
|
|
||||||
"\n";
|
|
||||||
final CSVFormat format = CSVFormat.DEFAULT.withIgnoreEmptyLines();
|
final CSVFormat format = CSVFormat.DEFAULT.withIgnoreEmptyLines();
|
||||||
final Lexer parser = getLexer(code, format);
|
try (final Lexer parser = createLexer(code, format)) {
|
||||||
|
|
||||||
assertThat(parser.nextToken(new Token()), matches(TOKEN, "first"));
|
assertThat(parser.nextToken(new Token()), matches(TOKEN, "first"));
|
||||||
assertThat(parser.nextToken(new Token()), matches(TOKEN, "line"));
|
assertThat(parser.nextToken(new Token()), matches(TOKEN, "line"));
|
||||||
assertThat(parser.nextToken(new Token()), matches(EORECORD, ""));
|
assertThat(parser.nextToken(new Token()), matches(EORECORD, ""));
|
||||||
@ -112,19 +101,14 @@ public class LexerTest {
|
|||||||
assertThat(parser.nextToken(new Token()), matches(EOF, ""));
|
assertThat(parser.nextToken(new Token()), matches(EOF, ""));
|
||||||
assertThat(parser.nextToken(new Token()), matches(EOF, ""));
|
assertThat(parser.nextToken(new Token()), matches(EOF, ""));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testComments() throws IOException {
|
public void testComments() throws IOException {
|
||||||
final String code =
|
final String code = "first,line,\n" + "second,line,tokenWith#no-comment\n" + "# comment line \n" +
|
||||||
"first,line,\n"+
|
"third,line,#no-comment\n" + "# penultimate comment\n" + "# Final comment\n";
|
||||||
"second,line,tokenWith#no-comment\n"+
|
|
||||||
"# comment line \n"+
|
|
||||||
"third,line,#no-comment\n"+
|
|
||||||
"# penultimate comment\n"+
|
|
||||||
"# Final comment\n";
|
|
||||||
final CSVFormat format = CSVFormat.DEFAULT.withCommentMarker('#');
|
final CSVFormat format = CSVFormat.DEFAULT.withCommentMarker('#');
|
||||||
final Lexer parser = getLexer(code, format);
|
try (final Lexer parser = createLexer(code, format)) {
|
||||||
|
|
||||||
assertThat(parser.nextToken(new Token()), matches(TOKEN, "first"));
|
assertThat(parser.nextToken(new Token()), matches(TOKEN, "first"));
|
||||||
assertThat(parser.nextToken(new Token()), matches(TOKEN, "line"));
|
assertThat(parser.nextToken(new Token()), matches(TOKEN, "line"));
|
||||||
assertThat(parser.nextToken(new Token()), matches(EORECORD, ""));
|
assertThat(parser.nextToken(new Token()), matches(EORECORD, ""));
|
||||||
@ -140,11 +124,11 @@ public class LexerTest {
|
|||||||
assertThat(parser.nextToken(new Token()), matches(EOF, ""));
|
assertThat(parser.nextToken(new Token()), matches(EOF, ""));
|
||||||
assertThat(parser.nextToken(new Token()), matches(EOF, ""));
|
assertThat(parser.nextToken(new Token()), matches(EOF, ""));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testCommentsAndEmptyLines() throws IOException {
|
public void testCommentsAndEmptyLines() throws IOException {
|
||||||
final String code =
|
final String code = "1,2,3,\n" + // 1
|
||||||
"1,2,3,\n"+ // 1
|
|
||||||
"\n" + // 1b
|
"\n" + // 1b
|
||||||
"\n" + // 1c
|
"\n" + // 1c
|
||||||
"a,b x,c#no-comment\n" + // 2
|
"a,b x,c#no-comment\n" + // 2
|
||||||
@ -161,9 +145,7 @@ public class LexerTest {
|
|||||||
final CSVFormat format = CSVFormat.DEFAULT.withCommentMarker('#').withIgnoreEmptyLines(false);
|
final CSVFormat format = CSVFormat.DEFAULT.withCommentMarker('#').withIgnoreEmptyLines(false);
|
||||||
assertFalse("Should not ignore empty lines", format.getIgnoreEmptyLines());
|
assertFalse("Should not ignore empty lines", format.getIgnoreEmptyLines());
|
||||||
|
|
||||||
final Lexer parser = getLexer(code, format);
|
try (final Lexer parser = createLexer(code, format)) {
|
||||||
|
|
||||||
|
|
||||||
assertThat(parser.nextToken(new Token()), matches(TOKEN, "1"));
|
assertThat(parser.nextToken(new Token()), matches(TOKEN, "1"));
|
||||||
assertThat(parser.nextToken(new Token()), matches(TOKEN, "2"));
|
assertThat(parser.nextToken(new Token()), matches(TOKEN, "2"));
|
||||||
assertThat(parser.nextToken(new Token()), matches(TOKEN, "3"));
|
assertThat(parser.nextToken(new Token()), matches(TOKEN, "3"));
|
||||||
@ -187,20 +169,19 @@ public class LexerTest {
|
|||||||
assertThat(parser.nextToken(new Token()), matches(COMMENT, "Final comment")); // 7
|
assertThat(parser.nextToken(new Token()), matches(COMMENT, "Final comment")); // 7
|
||||||
assertThat(parser.nextToken(new Token()), matches(EOF, ""));
|
assertThat(parser.nextToken(new Token()), matches(EOF, ""));
|
||||||
assertThat(parser.nextToken(new Token()), matches(EOF, ""));
|
assertThat(parser.nextToken(new Token()), matches(EOF, ""));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// simple token with escaping not enabled
|
// simple token with escaping not enabled
|
||||||
@Test
|
@Test
|
||||||
public void testBackslashWithoutEscaping() throws IOException {
|
public void testBackslashWithoutEscaping() throws IOException {
|
||||||
/* file: a,\,,b
|
/*
|
||||||
* \,,
|
* file: a,\,,b \,,
|
||||||
*/
|
*/
|
||||||
final String code = "a,\\,,b\\\n\\,,";
|
final String code = "a,\\,,b\\\n\\,,";
|
||||||
final CSVFormat format = CSVFormat.DEFAULT;
|
final CSVFormat format = CSVFormat.DEFAULT;
|
||||||
assertFalse(format.isEscapeCharacterSet());
|
assertFalse(format.isEscapeCharacterSet());
|
||||||
final Lexer parser = getLexer(code, format);
|
try (final Lexer parser = createLexer(code, format)) {
|
||||||
|
|
||||||
assertThat(parser.nextToken(new Token()), matches(TOKEN, "a"));
|
assertThat(parser.nextToken(new Token()), matches(TOKEN, "a"));
|
||||||
// an unquoted single backslash is not an escape char
|
// an unquoted single backslash is not an escape char
|
||||||
assertThat(parser.nextToken(new Token()), matches(TOKEN, "\\"));
|
assertThat(parser.nextToken(new Token()), matches(TOKEN, "\\"));
|
||||||
@ -211,18 +192,18 @@ public class LexerTest {
|
|||||||
assertThat(parser.nextToken(new Token()), matches(TOKEN, ""));
|
assertThat(parser.nextToken(new Token()), matches(TOKEN, ""));
|
||||||
assertThat(parser.nextToken(new Token()), matches(EOF, ""));
|
assertThat(parser.nextToken(new Token()), matches(EOF, ""));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// simple token with escaping enabled
|
// simple token with escaping enabled
|
||||||
@Test
|
@Test
|
||||||
public void testBackslashWithEscaping() throws IOException {
|
public void testBackslashWithEscaping() throws IOException {
|
||||||
/* file: a,\,,b
|
/*
|
||||||
* \,,
|
* file: a,\,,b \,,
|
||||||
*/
|
*/
|
||||||
final String code = "a,\\,,b\\\\\n\\,,\\\nc,d\\\r\ne";
|
final String code = "a,\\,,b\\\\\n\\,,\\\nc,d\\\r\ne";
|
||||||
final CSVFormat format = formatWithEscaping.withIgnoreEmptyLines(false);
|
final CSVFormat format = formatWithEscaping.withIgnoreEmptyLines(false);
|
||||||
assertTrue(format.isEscapeCharacterSet());
|
assertTrue(format.isEscapeCharacterSet());
|
||||||
final Lexer parser = getLexer(code, format);
|
try (final Lexer parser = createLexer(code, format)) {
|
||||||
|
|
||||||
assertThat(parser.nextToken(new Token()), matches(TOKEN, "a"));
|
assertThat(parser.nextToken(new Token()), matches(TOKEN, "a"));
|
||||||
assertThat(parser.nextToken(new Token()), matches(TOKEN, ","));
|
assertThat(parser.nextToken(new Token()), matches(TOKEN, ","));
|
||||||
assertThat(parser.nextToken(new Token()), matches(EORECORD, "b\\"));
|
assertThat(parser.nextToken(new Token()), matches(EORECORD, "b\\"));
|
||||||
@ -231,17 +212,16 @@ public class LexerTest {
|
|||||||
assertThat(parser.nextToken(new Token()), matches(EORECORD, "d\r"));
|
assertThat(parser.nextToken(new Token()), matches(EORECORD, "d\r"));
|
||||||
assertThat(parser.nextToken(new Token()), matches(EOF, "e"));
|
assertThat(parser.nextToken(new Token()), matches(EOF, "e"));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// encapsulator tokenizer (single line)
|
// encapsulator tokenizer (single line)
|
||||||
@Test
|
@Test
|
||||||
public void testNextToken4() throws IOException {
|
public void testNextToken4() throws IOException {
|
||||||
/* file: a,"foo",b
|
/*
|
||||||
* a, " foo",b
|
* file: a,"foo",b a, " foo",b a,"foo " ,b // whitespace after closing encapsulator a, " foo " ,b
|
||||||
* a,"foo " ,b // whitespace after closing encapsulator
|
|
||||||
* a, " foo " ,b
|
|
||||||
*/
|
*/
|
||||||
final String code = "a,\"foo\",b\na, \" foo\",b\na,\"foo \" ,b\na, \" foo \" ,b";
|
final String code = "a,\"foo\",b\na, \" foo\",b\na,\"foo \" ,b\na, \" foo \" ,b";
|
||||||
final Lexer parser = getLexer(code, CSVFormat.DEFAULT.withIgnoreSurroundingSpaces());
|
try (final Lexer parser = createLexer(code, CSVFormat.DEFAULT.withIgnoreSurroundingSpaces())) {
|
||||||
assertThat(parser.nextToken(new Token()), matches(TOKEN, "a"));
|
assertThat(parser.nextToken(new Token()), matches(TOKEN, "a"));
|
||||||
assertThat(parser.nextToken(new Token()), matches(TOKEN, "foo"));
|
assertThat(parser.nextToken(new Token()), matches(TOKEN, "foo"));
|
||||||
assertThat(parser.nextToken(new Token()), matches(EORECORD, "b"));
|
assertThat(parser.nextToken(new Token()), matches(EORECORD, "b"));
|
||||||
@ -256,40 +236,40 @@ public class LexerTest {
|
|||||||
// assertTokenEquals(EORECORD, "b", parser.nextToken(new Token()));
|
// assertTokenEquals(EORECORD, "b", parser.nextToken(new Token()));
|
||||||
assertThat(parser.nextToken(new Token()), matches(EOF, "b"));
|
assertThat(parser.nextToken(new Token()), matches(EOF, "b"));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// encapsulator tokenizer (multi line, delimiter in string)
|
// encapsulator tokenizer (multi line, delimiter in string)
|
||||||
@Test
|
@Test
|
||||||
public void testNextToken5() throws IOException {
|
public void testNextToken5() throws IOException {
|
||||||
final String code = "a,\"foo\n\",b\n\"foo\n baar ,,,\"\n\"\n\t \n\"";
|
final String code = "a,\"foo\n\",b\n\"foo\n baar ,,,\"\n\"\n\t \n\"";
|
||||||
final Lexer parser = getLexer(code, CSVFormat.DEFAULT);
|
try (final Lexer parser = createLexer(code, CSVFormat.DEFAULT)) {
|
||||||
assertThat(parser.nextToken(new Token()), matches(TOKEN, "a"));
|
assertThat(parser.nextToken(new Token()), matches(TOKEN, "a"));
|
||||||
assertThat(parser.nextToken(new Token()), matches(TOKEN, "foo\n"));
|
assertThat(parser.nextToken(new Token()), matches(TOKEN, "foo\n"));
|
||||||
assertThat(parser.nextToken(new Token()), matches(EORECORD, "b"));
|
assertThat(parser.nextToken(new Token()), matches(EORECORD, "b"));
|
||||||
assertThat(parser.nextToken(new Token()), matches(EORECORD, "foo\n baar ,,,"));
|
assertThat(parser.nextToken(new Token()), matches(EORECORD, "foo\n baar ,,,"));
|
||||||
assertThat(parser.nextToken(new Token()), matches(EOF, "\n\t \n"));
|
assertThat(parser.nextToken(new Token()), matches(EOF, "\n\t \n"));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// change delimiters, comment, encapsulater
|
// change delimiters, comment, encapsulater
|
||||||
@Test
|
@Test
|
||||||
public void testNextToken6() throws IOException {
|
public void testNextToken6() throws IOException {
|
||||||
/* file: a;'b and \' more
|
/*
|
||||||
* '
|
* file: a;'b and \' more ' !comment;;;; ;;
|
||||||
* !comment;;;;
|
|
||||||
* ;;
|
|
||||||
*/
|
*/
|
||||||
final String code = "a;'b and '' more\n'\n!comment;;;;\n;;";
|
final String code = "a;'b and '' more\n'\n!comment;;;;\n;;";
|
||||||
final CSVFormat format = CSVFormat.DEFAULT.withQuote('\'').withCommentMarker('!').withDelimiter(';');
|
final CSVFormat format = CSVFormat.DEFAULT.withQuote('\'').withCommentMarker('!').withDelimiter(';');
|
||||||
final Lexer parser = getLexer(code, format);
|
try (final Lexer parser = createLexer(code, format)) {
|
||||||
assertThat(parser.nextToken(new Token()), matches(TOKEN, "a"));
|
assertThat(parser.nextToken(new Token()), matches(TOKEN, "a"));
|
||||||
assertThat(parser.nextToken(new Token()), matches(EORECORD, "b and ' more\n"));
|
assertThat(parser.nextToken(new Token()), matches(EORECORD, "b and ' more\n"));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// From CSV-1
|
// From CSV-1
|
||||||
@Test
|
@Test
|
||||||
public void testDelimiterIsWhitespace() throws IOException {
|
public void testDelimiterIsWhitespace() throws IOException {
|
||||||
final String code = "one\ttwo\t\tfour \t five\t six";
|
final String code = "one\ttwo\t\tfour \t five\t six";
|
||||||
final Lexer parser = getLexer(code, CSVFormat.TDF);
|
try (final Lexer parser = createLexer(code, CSVFormat.TDF)) {
|
||||||
assertThat(parser.nextToken(new Token()), matches(TOKEN, "one"));
|
assertThat(parser.nextToken(new Token()), matches(TOKEN, "one"));
|
||||||
assertThat(parser.nextToken(new Token()), matches(TOKEN, "two"));
|
assertThat(parser.nextToken(new Token()), matches(TOKEN, "two"));
|
||||||
assertThat(parser.nextToken(new Token()), matches(TOKEN, ""));
|
assertThat(parser.nextToken(new Token()), matches(TOKEN, ""));
|
||||||
@ -297,100 +277,116 @@ public class LexerTest {
|
|||||||
assertThat(parser.nextToken(new Token()), matches(TOKEN, "five"));
|
assertThat(parser.nextToken(new Token()), matches(TOKEN, "five"));
|
||||||
assertThat(parser.nextToken(new Token()), matches(EOF, "six"));
|
assertThat(parser.nextToken(new Token()), matches(EOF, "six"));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testEscapedCR() throws Exception {
|
public void testEscapedCR() throws Exception {
|
||||||
final Lexer lexer = getLexer("character\\" + CR + "Escaped", formatWithEscaping);
|
try (final Lexer lexer = createLexer("character\\" + CR + "Escaped", formatWithEscaping)) {
|
||||||
assertThat(lexer.nextToken(new Token()), hasContent("character" + CR + "Escaped"));
|
assertThat(lexer.nextToken(new Token()), hasContent("character" + CR + "Escaped"));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testCR() throws Exception {
|
public void testCR() throws Exception {
|
||||||
final Lexer lexer = getLexer("character" + CR + "NotEscaped", formatWithEscaping);
|
try (final Lexer lexer = createLexer("character" + CR + "NotEscaped", formatWithEscaping)) {
|
||||||
assertThat(lexer.nextToken(new Token()), hasContent("character"));
|
assertThat(lexer.nextToken(new Token()), hasContent("character"));
|
||||||
assertThat(lexer.nextToken(new Token()), hasContent("NotEscaped"));
|
assertThat(lexer.nextToken(new Token()), hasContent("NotEscaped"));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testEscapedLF() throws Exception {
|
public void testEscapedLF() throws Exception {
|
||||||
final Lexer lexer = getLexer("character\\" + LF + "Escaped", formatWithEscaping);
|
try (final Lexer lexer = createLexer("character\\" + LF + "Escaped", formatWithEscaping)) {
|
||||||
assertThat(lexer.nextToken(new Token()), hasContent("character" + LF + "Escaped"));
|
assertThat(lexer.nextToken(new Token()), hasContent("character" + LF + "Escaped"));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testLF() throws Exception {
|
public void testLF() throws Exception {
|
||||||
final Lexer lexer = getLexer("character" + LF + "NotEscaped", formatWithEscaping);
|
try (final Lexer lexer = createLexer("character" + LF + "NotEscaped", formatWithEscaping)) {
|
||||||
assertThat(lexer.nextToken(new Token()), hasContent("character"));
|
assertThat(lexer.nextToken(new Token()), hasContent("character"));
|
||||||
assertThat(lexer.nextToken(new Token()), hasContent("NotEscaped"));
|
assertThat(lexer.nextToken(new Token()), hasContent("NotEscaped"));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Test // TODO is this correct? Do we expect <esc>TAB to be unescaped?
|
@Test // TODO is this correct? Do we expect <esc>TAB to be unescaped?
|
||||||
public void testEscapedTab() throws Exception {
|
public void testEscapedTab() throws Exception {
|
||||||
final Lexer lexer = getLexer("character\\" + TAB + "Escaped", formatWithEscaping);
|
try (final Lexer lexer = createLexer("character\\" + TAB + "Escaped", formatWithEscaping)) {
|
||||||
assertThat(lexer.nextToken(new Token()), hasContent("character" + TAB + "Escaped"));
|
assertThat(lexer.nextToken(new Token()), hasContent("character" + TAB + "Escaped"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testTab() throws Exception {
|
public void testTab() throws Exception {
|
||||||
final Lexer lexer = getLexer("character" + TAB + "NotEscaped", formatWithEscaping);
|
try (final Lexer lexer = createLexer("character" + TAB + "NotEscaped", formatWithEscaping)) {
|
||||||
assertThat(lexer.nextToken(new Token()), hasContent("character" + TAB + "NotEscaped"));
|
assertThat(lexer.nextToken(new Token()), hasContent("character" + TAB + "NotEscaped"));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Test // TODO is this correct? Do we expect <esc>BACKSPACE to be unescaped?
|
@Test // TODO is this correct? Do we expect <esc>BACKSPACE to be unescaped?
|
||||||
public void testEscapedBackspace() throws Exception {
|
public void testEscapedBackspace() throws Exception {
|
||||||
final Lexer lexer = getLexer("character\\" + BACKSPACE + "Escaped", formatWithEscaping);
|
try (final Lexer lexer = createLexer("character\\" + BACKSPACE + "Escaped", formatWithEscaping)) {
|
||||||
assertThat(lexer.nextToken(new Token()), hasContent("character" + BACKSPACE + "Escaped"));
|
assertThat(lexer.nextToken(new Token()), hasContent("character" + BACKSPACE + "Escaped"));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testBackspace() throws Exception {
|
public void testBackspace() throws Exception {
|
||||||
final Lexer lexer = getLexer("character" + BACKSPACE + "NotEscaped", formatWithEscaping);
|
try (final Lexer lexer = createLexer("character" + BACKSPACE + "NotEscaped", formatWithEscaping)) {
|
||||||
assertThat(lexer.nextToken(new Token()), hasContent("character" + BACKSPACE + "NotEscaped"));
|
assertThat(lexer.nextToken(new Token()), hasContent("character" + BACKSPACE + "NotEscaped"));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Test // TODO is this correct? Do we expect <esc>FF to be unescaped?
|
@Test // TODO is this correct? Do we expect <esc>FF to be unescaped?
|
||||||
public void testEscapedFF() throws Exception {
|
public void testEscapedFF() throws Exception {
|
||||||
final Lexer lexer = getLexer("character\\" + FF + "Escaped", formatWithEscaping);
|
try (final Lexer lexer = createLexer("character\\" + FF + "Escaped", formatWithEscaping)) {
|
||||||
assertThat(lexer.nextToken(new Token()), hasContent("character" + FF + "Escaped"));
|
assertThat(lexer.nextToken(new Token()), hasContent("character" + FF + "Escaped"));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testFF() throws Exception {
|
public void testFF() throws Exception {
|
||||||
final Lexer lexer = getLexer("character" + FF + "NotEscaped", formatWithEscaping);
|
try (final Lexer lexer = createLexer("character" + FF + "NotEscaped", formatWithEscaping)) {
|
||||||
assertThat(lexer.nextToken(new Token()), hasContent("character" + FF + "NotEscaped"));
|
assertThat(lexer.nextToken(new Token()), hasContent("character" + FF + "NotEscaped"));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testEscapedMySqlNullValue() throws Exception {
|
public void testEscapedMySqlNullValue() throws Exception {
|
||||||
// MySQL uses \N to symbolize null values. We have to restore this
|
// MySQL uses \N to symbolize null values. We have to restore this
|
||||||
final Lexer lexer = getLexer("character\\NEscaped", formatWithEscaping);
|
try (final Lexer lexer = createLexer("character\\NEscaped", formatWithEscaping)) {
|
||||||
assertThat(lexer.nextToken(new Token()), hasContent("character\\NEscaped"));
|
assertThat(lexer.nextToken(new Token()), hasContent("character\\NEscaped"));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testEscapedCharacter() throws Exception {
|
public void testEscapedCharacter() throws Exception {
|
||||||
final Lexer lexer = getLexer("character\\aEscaped", formatWithEscaping);
|
try (final Lexer lexer = createLexer("character\\aEscaped", formatWithEscaping)) {
|
||||||
assertThat(lexer.nextToken(new Token()), hasContent("character\\aEscaped"));
|
assertThat(lexer.nextToken(new Token()), hasContent("character\\aEscaped"));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testEscapedControlCharacter() throws Exception {
|
public void testEscapedControlCharacter() throws Exception {
|
||||||
// we are explicitly using an escape different from \ here
|
// we are explicitly using an escape different from \ here
|
||||||
final Lexer lexer = getLexer("character!rEscaped", CSVFormat.DEFAULT.withEscape('!'));
|
try (final Lexer lexer = createLexer("character!rEscaped", CSVFormat.DEFAULT.withEscape('!'))) {
|
||||||
assertThat(lexer.nextToken(new Token()), hasContent("character" + CR + "Escaped"));
|
assertThat(lexer.nextToken(new Token()), hasContent("character" + CR + "Escaped"));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testEscapedControlCharacter2() throws Exception {
|
public void testEscapedControlCharacter2() throws Exception {
|
||||||
final Lexer lexer = getLexer("character\\rEscaped", CSVFormat.DEFAULT.withEscape('\\'));
|
try (final Lexer lexer = createLexer("character\\rEscaped", CSVFormat.DEFAULT.withEscape('\\'))) {
|
||||||
assertThat(lexer.nextToken(new Token()), hasContent("character" + CR + "Escaped"));
|
assertThat(lexer.nextToken(new Token()), hasContent("character" + CR + "Escaped"));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Test(expected = IOException.class)
|
@Test(expected = IOException.class)
|
||||||
public void testEscapingAtEOF() throws Exception {
|
public void testEscapingAtEOF() throws Exception {
|
||||||
final String code = "escaping at EOF is evil\\";
|
final String code = "escaping at EOF is evil\\";
|
||||||
final Lexer lexer = getLexer(code, formatWithEscaping);
|
try (final Lexer lexer = createLexer(code, formatWithEscaping)) {
|
||||||
|
|
||||||
lexer.nextToken(new Token());
|
lexer.nextToken(new Token());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
@ -26,6 +26,7 @@ import java.io.IOException;
|
|||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
import java.lang.reflect.Constructor;
|
import java.lang.reflect.Constructor;
|
||||||
|
import java.lang.reflect.InvocationTargetException;
|
||||||
import java.util.zip.GZIPInputStream;
|
import java.util.zip.GZIPInputStream;
|
||||||
|
|
||||||
import org.apache.commons.io.IOUtils;
|
import org.apache.commons.io.IOUtils;
|
||||||
@ -73,11 +74,11 @@ public class PerformanceTest {
|
|||||||
System.out.println(String.format("Found test fixture %s: %,d bytes.", BIG_FILE, BIG_FILE.length()));
|
System.out.println(String.format("Found test fixture %s: %,d bytes.", BIG_FILE, BIG_FILE.length()));
|
||||||
} else {
|
} else {
|
||||||
System.out.println("Decompressing test fixture " + BIG_FILE + "...");
|
System.out.println("Decompressing test fixture " + BIG_FILE + "...");
|
||||||
final InputStream input = new GZIPInputStream(new FileInputStream("src/test/resources/perf/worldcitiespop.txt.gz"));
|
try (final InputStream input = new GZIPInputStream(
|
||||||
final OutputStream output = new FileOutputStream(BIG_FILE);
|
new FileInputStream("src/test/resources/perf/worldcitiespop.txt.gz"));
|
||||||
|
final OutputStream output = new FileOutputStream(BIG_FILE)) {
|
||||||
IOUtils.copy(input, output);
|
IOUtils.copy(input, output);
|
||||||
input.close();
|
}
|
||||||
output.close();
|
|
||||||
System.out.println(String.format("Decompressed test fixture %s: %,d bytes.", BIG_FILE, BIG_FILE.length()));
|
System.out.println(String.format("Decompressed test fixture %s: %,d bytes.", BIG_FILE, BIG_FILE.length()));
|
||||||
}
|
}
|
||||||
final int argc = args.length;
|
final int argc = args.length;
|
||||||
@ -121,7 +122,7 @@ public class PerformanceTest {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static BufferedReader getReader() throws IOException {
|
private static BufferedReader createReader() throws IOException {
|
||||||
return new BufferedReader(new FileReader(BIG_FILE));
|
return new BufferedReader(new FileReader(BIG_FILE));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -156,11 +157,13 @@ public class PerformanceTest {
|
|||||||
|
|
||||||
private static void testReadBigFile(final boolean split) throws Exception {
|
private static void testReadBigFile(final boolean split) throws Exception {
|
||||||
for (int i = 0; i < max; i++) {
|
for (int i = 0; i < max; i++) {
|
||||||
final BufferedReader in = getReader();
|
final long startMillis;
|
||||||
final long t0 = System.currentTimeMillis();
|
final Stats stats;
|
||||||
final Stats s = readAll(in, split);
|
try (final BufferedReader in = createReader()) {
|
||||||
in.close();
|
startMillis = System.currentTimeMillis();
|
||||||
show(split?"file+split":"file", s, t0);
|
stats = readAll(in, split);
|
||||||
|
}
|
||||||
|
show(split ? "file+split" : "file", stats, startMillis);
|
||||||
}
|
}
|
||||||
show();
|
show();
|
||||||
}
|
}
|
||||||
@ -178,11 +181,12 @@ public class PerformanceTest {
|
|||||||
|
|
||||||
private static void testExtendedBuffer(final boolean makeString) throws Exception {
|
private static void testExtendedBuffer(final boolean makeString) throws Exception {
|
||||||
for (int i = 0; i < max; i++) {
|
for (int i = 0; i < max; i++) {
|
||||||
final ExtendedBufferedReader in = new ExtendedBufferedReader(getReader());
|
|
||||||
final long t0 = System.currentTimeMillis();
|
|
||||||
int read;
|
|
||||||
int fields = 0;
|
int fields = 0;
|
||||||
int lines = 0;
|
int lines = 0;
|
||||||
|
final long startMillis;
|
||||||
|
try (final ExtendedBufferedReader in = new ExtendedBufferedReader(createReader())) {
|
||||||
|
startMillis = System.currentTimeMillis();
|
||||||
|
int read;
|
||||||
if (makeString) {
|
if (makeString) {
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
while ((read = in.read()) != -1) {
|
while ((read = in.read()) != -1) {
|
||||||
@ -207,21 +211,23 @@ public class PerformanceTest {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
fields += lines; // EOL is a delimiter too
|
fields += lines; // EOL is a delimiter too
|
||||||
in.close();
|
}
|
||||||
show("Extended"+(makeString?" toString":""), new Stats(lines, fields), t0);
|
show("Extended" + (makeString ? " toString" : ""), new Stats(lines, fields), startMillis);
|
||||||
}
|
}
|
||||||
show();
|
show();
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void testParseCommonsCSV() throws Exception {
|
private static void testParseCommonsCSV() throws Exception {
|
||||||
for (int i = 0; i < max; i++) {
|
for (int i = 0; i < max; i++) {
|
||||||
final BufferedReader reader = getReader();
|
final long startMillis;
|
||||||
final CSVParser parser = new CSVParser(reader, format);
|
final Stats stats;
|
||||||
final long t0 = System.currentTimeMillis();
|
try (final BufferedReader reader = createReader()) {
|
||||||
final Stats s = iterate(parser);
|
try (final CSVParser parser = new CSVParser(reader, format)) {
|
||||||
reader.close();
|
startMillis = System.currentTimeMillis();
|
||||||
show("CSV", s, t0);
|
stats = iterate(parser);
|
||||||
parser.close();
|
}
|
||||||
|
show("CSV", stats, startMillis);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
show();
|
show();
|
||||||
}
|
}
|
||||||
@ -237,17 +243,18 @@ public class PerformanceTest {
|
|||||||
Token token = new Token();
|
Token token = new Token();
|
||||||
String dynamic = "";
|
String dynamic = "";
|
||||||
for (int i = 0; i < max; i++) {
|
for (int i = 0; i < max; i++) {
|
||||||
final ExtendedBufferedReader input = new ExtendedBufferedReader(getReader());
|
final String simpleName;
|
||||||
Lexer lexer = null;
|
final Stats stats;
|
||||||
|
final long startMillis;
|
||||||
|
try (final ExtendedBufferedReader input = new ExtendedBufferedReader(createReader());
|
||||||
|
Lexer lexer = createTestCSVLexer(test, input)) {
|
||||||
if (test.startsWith("CSVLexer")) {
|
if (test.startsWith("CSVLexer")) {
|
||||||
dynamic = "!";
|
dynamic = "!";
|
||||||
lexer = getLexerCtor(test).newInstance(new Object[]{format, input});
|
|
||||||
} else {
|
|
||||||
lexer = new Lexer(format, input);
|
|
||||||
}
|
}
|
||||||
|
simpleName = lexer.getClass().getSimpleName();
|
||||||
int count = 0;
|
int count = 0;
|
||||||
int fields = 0;
|
int fields = 0;
|
||||||
final long t0 = System.currentTimeMillis();
|
startMillis = System.currentTimeMillis();
|
||||||
do {
|
do {
|
||||||
if (newToken) {
|
if (newToken) {
|
||||||
token = new Token();
|
token = new Token();
|
||||||
@ -272,15 +279,20 @@ public class PerformanceTest {
|
|||||||
default:
|
default:
|
||||||
throw new IllegalStateException("Unexpected Token type: " + token.type);
|
throw new IllegalStateException("Unexpected Token type: " + token.type);
|
||||||
}
|
}
|
||||||
|
|
||||||
} while (!token.type.equals(Token.Type.EOF));
|
} while (!token.type.equals(Token.Type.EOF));
|
||||||
final Stats s = new Stats(count, fields);
|
stats = new Stats(count, fields);
|
||||||
input.close();
|
}
|
||||||
show(lexer.getClass().getSimpleName()+dynamic+" "+(newToken ? "new" : "reset"), s, t0);
|
show(simpleName + dynamic + " " + (newToken ? "new" : "reset"), stats, startMillis);
|
||||||
}
|
}
|
||||||
show();
|
show();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static Lexer createTestCSVLexer(final String test, final ExtendedBufferedReader input)
|
||||||
|
throws InstantiationException, IllegalAccessException, InvocationTargetException, Exception {
|
||||||
|
return test.startsWith("CSVLexer") ? getLexerCtor(test)
|
||||||
|
.newInstance(new Object[] { format, input }) : new Lexer(format, input);
|
||||||
|
}
|
||||||
|
|
||||||
private static Stats iterate(final Iterable<CSVRecord> it) {
|
private static Stats iterate(final Iterable<CSVRecord> it) {
|
||||||
int count = 0;
|
int count = 0;
|
||||||
int fields = 0;
|
int fields = 0;
|
||||||
|
@ -29,12 +29,13 @@ public class JiraCsv164Test {
|
|||||||
@Test
|
@Test
|
||||||
public void testJiraCsv154_withCommentMarker() throws IOException {
|
public void testJiraCsv154_withCommentMarker() throws IOException {
|
||||||
final String comment = "This is a header comment";
|
final String comment = "This is a header comment";
|
||||||
final CSVFormat format = CSVFormat.EXCEL.withHeader("H1", "H2").withCommentMarker('#').withHeaderComments(comment);
|
final CSVFormat format = CSVFormat.EXCEL.withHeader("H1", "H2").withCommentMarker('#')
|
||||||
|
.withHeaderComments(comment);
|
||||||
final StringBuilder out = new StringBuilder();
|
final StringBuilder out = new StringBuilder();
|
||||||
final CSVPrinter printer = format.print(out);
|
try (final CSVPrinter printer = format.print(out)) {
|
||||||
printer.print("A");
|
printer.print("A");
|
||||||
printer.print("B");
|
printer.print("B");
|
||||||
printer.close();
|
}
|
||||||
final String s = out.toString();
|
final String s = out.toString();
|
||||||
assertTrue(s, s.contains(comment));
|
assertTrue(s, s.contains(comment));
|
||||||
}
|
}
|
||||||
@ -42,12 +43,13 @@ public class JiraCsv164Test {
|
|||||||
@Test
|
@Test
|
||||||
public void testJiraCsv154_withHeaderComments() throws IOException {
|
public void testJiraCsv154_withHeaderComments() throws IOException {
|
||||||
final String comment = "This is a header comment";
|
final String comment = "This is a header comment";
|
||||||
final CSVFormat format = CSVFormat.EXCEL.withHeader("H1", "H2").withHeaderComments(comment).withCommentMarker('#');
|
final CSVFormat format = CSVFormat.EXCEL.withHeader("H1", "H2").withHeaderComments(comment)
|
||||||
|
.withCommentMarker('#');
|
||||||
final StringBuilder out = new StringBuilder();
|
final StringBuilder out = new StringBuilder();
|
||||||
final CSVPrinter printer = format.print(out);
|
try (final CSVPrinter printer = format.print(out)) {
|
||||||
printer.print("A");
|
printer.print("A");
|
||||||
printer.print("B");
|
printer.print("B");
|
||||||
printer.close();
|
}
|
||||||
final String s = out.toString();
|
final String s = out.toString();
|
||||||
assertTrue(s, s.contains(comment));
|
assertTrue(s, s.contains(comment));
|
||||||
}
|
}
|
||||||
|
@ -33,10 +33,10 @@ public class JiraCsv167Test {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void parse() throws IOException {
|
public void parse() throws IOException {
|
||||||
final BufferedReader br = new BufferedReader(getTestInput());
|
|
||||||
String s = null;
|
|
||||||
int totcomment = 0;
|
int totcomment = 0;
|
||||||
int totrecs = 0;
|
int totrecs = 0;
|
||||||
|
try (final BufferedReader br = new BufferedReader(getTestInput())) {
|
||||||
|
String s = null;
|
||||||
boolean lastWasComment = false;
|
boolean lastWasComment = false;
|
||||||
while ((s = br.readLine()) != null) {
|
while ((s = br.readLine()) != null) {
|
||||||
if (s.startsWith("#")) {
|
if (s.startsWith("#")) {
|
||||||
@ -49,7 +49,7 @@ public class JiraCsv167Test {
|
|||||||
lastWasComment = false;
|
lastWasComment = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
br.close();
|
}
|
||||||
CSVFormat format = CSVFormat.DEFAULT;
|
CSVFormat format = CSVFormat.DEFAULT;
|
||||||
//
|
//
|
||||||
format = format.withAllowMissingColumnNames(false);
|
format = format.withAllowMissingColumnNames(false);
|
||||||
@ -66,15 +66,16 @@ public class JiraCsv167Test {
|
|||||||
format = format.withRecordSeparator('\n');
|
format = format.withRecordSeparator('\n');
|
||||||
format = format.withSkipHeaderRecord(false);
|
format = format.withSkipHeaderRecord(false);
|
||||||
//
|
//
|
||||||
final CSVParser parser = format.parse(getTestInput());
|
|
||||||
int comments = 0;
|
int comments = 0;
|
||||||
int records = 0;
|
int records = 0;
|
||||||
|
try (final CSVParser parser = format.parse(getTestInput())) {
|
||||||
for (final CSVRecord csvRecord : parser) {
|
for (final CSVRecord csvRecord : parser) {
|
||||||
records++;
|
records++;
|
||||||
if (csvRecord.hasComment()) {
|
if (csvRecord.hasComment()) {
|
||||||
comments++;
|
comments++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
// Comment lines are concatenated, in this example 4 lines become 2 comments.
|
// Comment lines are concatenated, in this example 4 lines become 2 comments.
|
||||||
Assert.assertEquals(totcomment, comments);
|
Assert.assertEquals(totcomment, comments);
|
||||||
Assert.assertEquals(totrecs, records); // records includes the header
|
Assert.assertEquals(totrecs, records); // records includes the header
|
||||||
|
@ -56,15 +56,15 @@ public class PerformanceTest {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
System.out.println("Decompressing test fixture " + BIG_FILE + "...");
|
System.out.println("Decompressing test fixture " + BIG_FILE + "...");
|
||||||
final InputStream input = new GZIPInputStream(new FileInputStream("src/test/resources/perf/worldcitiespop.txt.gz"));
|
try (final InputStream input = new GZIPInputStream(
|
||||||
final OutputStream output = new FileOutputStream(BIG_FILE);
|
new FileInputStream("src/test/resources/perf/worldcitiespop.txt.gz"));
|
||||||
|
final OutputStream output = new FileOutputStream(BIG_FILE)) {
|
||||||
IOUtils.copy(input, output);
|
IOUtils.copy(input, output);
|
||||||
System.out.println(String.format("Decompressed test fixture %s: %,d bytes.", BIG_FILE, BIG_FILE.length()));
|
System.out.println(String.format("Decompressed test fixture %s: %,d bytes.", BIG_FILE, BIG_FILE.length()));
|
||||||
input.close();
|
}
|
||||||
output.close();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private BufferedReader getBufferedReader() throws IOException {
|
private BufferedReader createBufferedReader() throws IOException {
|
||||||
return new BufferedReader(new FileReader(BIG_FILE));
|
return new BufferedReader(new FileReader(BIG_FILE));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -96,7 +96,7 @@ public class PerformanceTest {
|
|||||||
|
|
||||||
public long testParseBigFile(final boolean traverseColumns) throws Exception {
|
public long testParseBigFile(final boolean traverseColumns) throws Exception {
|
||||||
final long startMillis = System.currentTimeMillis();
|
final long startMillis = System.currentTimeMillis();
|
||||||
final long count = this.parse(this.getBufferedReader(), traverseColumns);
|
final long count = this.parse(this.createBufferedReader(), traverseColumns);
|
||||||
final long totalMillis = System.currentTimeMillis() - startMillis;
|
final long totalMillis = System.currentTimeMillis() - startMillis;
|
||||||
this.println(String.format("File parsed in %,d milliseconds with Commons CSV: %,d lines.", totalMillis, count));
|
this.println(String.format("File parsed in %,d milliseconds with Commons CSV: %,d lines.", totalMillis, count));
|
||||||
return totalMillis;
|
return totalMillis;
|
||||||
@ -115,13 +115,12 @@ public class PerformanceTest {
|
|||||||
public void testReadBigFile() throws Exception {
|
public void testReadBigFile() throws Exception {
|
||||||
long bestTime = Long.MAX_VALUE;
|
long bestTime = Long.MAX_VALUE;
|
||||||
for (int i = 0; i < this.max; i++) {
|
for (int i = 0; i < this.max; i++) {
|
||||||
final BufferedReader in = this.getBufferedReader();
|
final long startMillis;
|
||||||
final long startMillis = System.currentTimeMillis();
|
long count;
|
||||||
long count = 0;
|
try (final BufferedReader in = this.createBufferedReader()) {
|
||||||
try {
|
startMillis = System.currentTimeMillis();
|
||||||
|
count = 0;
|
||||||
count = this.readAll(in);
|
count = this.readAll(in);
|
||||||
} finally {
|
|
||||||
in.close();
|
|
||||||
}
|
}
|
||||||
final long totalMillis = System.currentTimeMillis() - startMillis;
|
final long totalMillis = System.currentTimeMillis() - startMillis;
|
||||||
bestTime = Math.min(totalMillis, bestTime);
|
bestTime = Math.min(totalMillis, bestTime);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user