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