You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2014/03/17 01:33:09 UTC
svn commit: r1578187 -
/commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVParserTest.java
Author: ggregory
Date: Mon Mar 17 00:33:09 2014
New Revision: 1578187
URL: http://svn.apache.org/r1578187
Log:
Sort methods.
Modified:
commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVParserTest.java
Modified: commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVParserTest.java
URL: http://svn.apache.org/viewvc/commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVParserTest.java?rev=1578187&r1=1578186&r2=1578187&view=diff
==============================================================================
--- commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVParserTest.java (original)
+++ commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVParserTest.java Mon Mar 17 00:33:09 2014
@@ -72,39 +72,102 @@ public class CSVParserTest {
};
@Test
- public void testGetLine() throws IOException {
- final CSVParser parser = CSVParser.parse(CSVINPUT, CSVFormat.DEFAULT.withIgnoreSurroundingSpaces(true));
- for (final String[] re : RESULT) {
- assertArrayEquals(re, parser.nextRecord().values());
- }
+ public void testBackslashEscaping() throws IOException {
- assertNull(parser.nextRecord());
+ // To avoid confusion over the need for escaping chars in java code,
+ // We will test with a forward slash as the escape char, and a single
+ // quote as the encapsulator.
+
+ final String code =
+ "one,two,three\n" // 0
+ + "'',''\n" // 1) empty encapsulators
+ + "/',/'\n" // 2) single encapsulators
+ + "'/'','/''\n" // 3) single encapsulators encapsulated via escape
+ + "'''',''''\n" // 4) single encapsulators encapsulated via doubling
+ + "/,,/,\n" // 5) separator escaped
+ + "//,//\n" // 6) escape escaped
+ + "'//','//'\n" // 7) escape escaped in encapsulation
+ + " 8 , \"quoted \"\" /\" // string\" \n" // don't eat spaces
+ + "9, /\n \n" // escaped newline
+ + "";
+ final String[][] res = {
+ {"one", "two", "three"}, // 0
+ {"", ""}, // 1
+ {"'", "'"}, // 2
+ {"'", "'"}, // 3
+ {"'", "'"}, // 4
+ {",", ","}, // 5
+ {"/", "/"}, // 6
+ {"/", "/"}, // 7
+ {" 8 ", " \"quoted \"\" /\" / string\" "},
+ {"9", " \n "},
+ };
+
+
+ final CSVFormat format = CSVFormat.newFormat(',').withQuoteChar('\'')
+ .withRecordSeparator(CRLF).withEscape('/').withIgnoreEmptyLines(true);
+
+ 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);
}
@Test
- public void testGetRecords() throws IOException {
- final CSVParser parser = CSVParser.parse(CSVINPUT, CSVFormat.DEFAULT.withIgnoreSurroundingSpaces(true));
+ public void testBackslashEscaping2() throws IOException {
+
+ // To avoid confusion over the need for escaping chars in java code,
+ // We will test with a forward slash as the escape char, and a single
+ // quote as the encapsulator.
+
+ final String code = ""
+ + " , , \n" // 1)
+ + " \t , , \n" // 2)
+ + " // , /, , /,\n" // 3)
+ + "";
+ final String[][] res = {
+ {" ", " ", " "}, // 1
+ {" \t ", " ", " "}, // 2
+ {" / ", " , ", " ,"}, // 3
+ };
+
+
+ final CSVFormat format = CSVFormat.newFormat(',')
+ .withRecordSeparator(CRLF).withEscape('/').withIgnoreEmptyLines(true);
+
+ final CSVParser parser = CSVParser.parse(code, format);
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());
- }
+
+ Utils.compare("", res, records);
}
@Test
- public void testExcelFormat1() throws IOException {
+ @Ignore
+ public void testBackslashEscapingOld() 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";
+ "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 = {
- {"value1", "value2", "value3", "value4"},
- {"a", "b", "c", "d"},
- {" x", "", "", ""},
- {""},
- {"\"hello\"", " \"world\"", "abc\ndef", ""}
+ {"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.EXCEL);
+ final CSVParser parser = CSVParser.parse(code, CSVFormat.DEFAULT);
final List<CSVRecord> records = parser.getRecords();
assertEquals(res.length, records.size());
assertTrue(records.size() > 0);
@@ -114,42 +177,117 @@ public class CSVParserTest {
}
@Test
- public void testExcelFormat2() throws Exception {
- final String code = "foo,baar\r\n\r\nhello,\r\n\r\nworld,\r\n";
+ public void testCarriageReturnEndings() throws IOException {
+ final String code = "foo\rbaar,\rhello,world\r,kanu";
+ final CSVParser parser = CSVParser.parse(code, CSVFormat.DEFAULT);
+ final List<CSVRecord> records = parser.getRecords();
+ assertEquals(4, records.size());
+ }
+
+ @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);
+ final List<CSVRecord> records = parser.getRecords();
+ assertEquals(4, records.size());
+ }
+
+ @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.withCommentStart('#').withHeader().parse(in);
+ final Iterator<CSVRecord> 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);
+ final List<CSVRecord> list = parser.getRecords();
+ assertNotNull(list);
+ assertEquals(0, list.size());
+ }
+
+ @Test
+ public void testDefaultFormat() throws IOException {
+ final String code = ""
+ + "a,b#\n" // 1)
+ + "\"\n\",\" \",#\n" // 2)
+ + "#,\"\"\n" // 3)
+ + "# Final comment\n"// 4)
+ ;
final String[][] res = {
- {"foo", "baar"},
- {""},
- {"hello", ""},
- {""},
- {"world", ""}
+ {"a", "b#"},
+ {"\n", " ", "#"},
+ {"#", ""},
+ {"# Final comment"}
};
- final CSVParser parser = CSVParser.parse(code, CSVFormat.EXCEL);
- final List<CSVRecord> records = parser.getRecords();
- assertEquals(res.length, records.size());
+
+ CSVFormat format = CSVFormat.DEFAULT;
+ assertFalse(format.isCommentingEnabled());
+
+ CSVParser parser = CSVParser.parse(code, format);
+ List<CSVRecord> records = parser.getRecords();
assertTrue(records.size() > 0);
- for (int i = 0; i < res.length; i++) {
- assertArrayEquals(res[i], records.get(i).values());
+
+ Utils.compare("Failed to parse without comments", res, records);
+
+ final String[][] res_comments = {
+ {"a", "b#"},
+ {"\n", " ", "#"},
+ };
+
+ format = CSVFormat.DEFAULT.withCommentStart('#');
+ parser = CSVParser.parse(code, format);
+ records = parser.getRecords();
+
+ Utils.compare("Failed to parse with comments", res_comments, records);
+ }
+
+ @Test
+ public void testEmptyFile() throws Exception {
+ final CSVParser parser = CSVParser.parse("", CSVFormat.DEFAULT);
+ assertNull(parser.nextRecord());
+ }
+
+ @Test
+ public void testEmptyLineBehaviourCSV() throws Exception {
+ final String[] codes = {
+ "hello,\r\n\r\n\r\n",
+ "hello,\n\n\n",
+ "hello,\"\"\r\n\r\n\r\n",
+ "hello,\"\"\n\n\n"
+ };
+ final String[][] res = {
+ {"hello", ""} // CSV format ignores empty lines
+ };
+ for (final String code : codes) {
+ 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());
+ }
}
}
@Test
- public void testEndOfFileBehaviourExcel() throws Exception {
+ public void testEmptyLineBehaviourExcel() throws Exception {
final String[] codes = {
- "hello,\r\n\r\nworld,\r\n",
- "hello,\r\n\r\nworld,",
- "hello,\r\n\r\nworld,\"\"\r\n",
- "hello,\r\n\r\nworld,\"\"",
- "hello,\r\n\r\nworld,\n",
- "hello,\r\n\r\nworld,",
- "hello,\r\n\r\nworld,\"\"\n",
- "hello,\r\n\r\nworld,\"\""
+ "hello,\r\n\r\n\r\n",
+ "hello,\n\n\n",
+ "hello,\"\"\r\n\r\n\r\n",
+ "hello,\"\"\n\n\n"
};
final String[][] res = {
{"hello", ""},
{""}, // Excel format does not ignore empty lines
- {"world", ""}
+ {""}
};
-
for (final String code : codes) {
final CSVParser parser = CSVParser.parse(code, CSVFormat.EXCEL);
final List<CSVRecord> records = parser.getRecords();
@@ -189,18 +327,23 @@ public class CSVParserTest {
}
@Test
- public void testEmptyLineBehaviourExcel() throws Exception {
+ public void testEndOfFileBehaviourExcel() throws Exception {
final String[] codes = {
- "hello,\r\n\r\n\r\n",
- "hello,\n\n\n",
- "hello,\"\"\r\n\r\n\r\n",
- "hello,\"\"\n\n\n"
+ "hello,\r\n\r\nworld,\r\n",
+ "hello,\r\n\r\nworld,",
+ "hello,\r\n\r\nworld,\"\"\r\n",
+ "hello,\r\n\r\nworld,\"\"",
+ "hello,\r\n\r\nworld,\n",
+ "hello,\r\n\r\nworld,",
+ "hello,\r\n\r\nworld,\"\"\n",
+ "hello,\r\n\r\nworld,\"\""
};
final String[][] res = {
{"hello", ""},
{""}, // Excel format does not ignore empty lines
- {""}
+ {"world", ""}
};
+
for (final String code : codes) {
final CSVParser parser = CSVParser.parse(code, CSVFormat.EXCEL);
final List<CSVRecord> records = parser.getRecords();
@@ -213,66 +356,18 @@ public class CSVParserTest {
}
@Test
- public void testEmptyLineBehaviourCSV() throws Exception {
- final String[] codes = {
- "hello,\r\n\r\n\r\n",
- "hello,\n\n\n",
- "hello,\"\"\r\n\r\n\r\n",
- "hello,\"\"\n\n\n"
- };
+ 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[][] res = {
- {"hello", ""} // CSV format ignores empty lines
- };
- for (final String code : codes) {
- 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());
- }
- }
- }
-
- @Test
- public void testEmptyFile() throws Exception {
- final CSVParser parser = CSVParser.parse("", CSVFormat.DEFAULT);
- assertNull(parser.nextRecord());
- }
-
- @Test
- public void testCSV57() throws Exception {
- final CSVParser parser = CSVParser.parse("", CSVFormat.DEFAULT);
- final List<CSVRecord> list = parser.getRecords();
- assertNotNull(list);
- assertEquals(0, list.size());
- }
-
- @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[][] 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 (",")
+ {"value1", "value2", "value3", "value4"},
+ {"a", "b", "c", "d"},
+ {" x", "", "", ""},
+ {""},
+ {"\"hello\"", " \"world\"", "abc\ndef", ""}
};
- final CSVParser parser = CSVParser.parse(code, CSVFormat.DEFAULT);
+ final CSVParser parser = CSVParser.parse(code, CSVFormat.EXCEL);
final List<CSVRecord> records = parser.getRecords();
assertEquals(res.length, records.size());
assertTrue(records.size() > 0);
@@ -282,231 +377,136 @@ public class CSVParserTest {
}
@Test
- public void testBackslashEscaping() throws IOException {
-
- // To avoid confusion over the need for escaping chars in java code,
- // We will test with a forward slash as the escape char, and a single
- // quote as the encapsulator.
-
- final String code =
- "one,two,three\n" // 0
- + "'',''\n" // 1) empty encapsulators
- + "/',/'\n" // 2) single encapsulators
- + "'/'','/''\n" // 3) single encapsulators encapsulated via escape
- + "'''',''''\n" // 4) single encapsulators encapsulated via doubling
- + "/,,/,\n" // 5) separator escaped
- + "//,//\n" // 6) escape escaped
- + "'//','//'\n" // 7) escape escaped in encapsulation
- + " 8 , \"quoted \"\" /\" // string\" \n" // don't eat spaces
- + "9, /\n \n" // escaped newline
- + "";
+ public void testExcelFormat2() throws Exception {
+ final String code = "foo,baar\r\n\r\nhello,\r\n\r\nworld,\r\n";
final String[][] res = {
- {"one", "two", "three"}, // 0
- {"", ""}, // 1
- {"'", "'"}, // 2
- {"'", "'"}, // 3
- {"'", "'"}, // 4
- {",", ","}, // 5
- {"/", "/"}, // 6
- {"/", "/"}, // 7
- {" 8 ", " \"quoted \"\" /\" / string\" "},
- {"9", " \n "},
+ {"foo", "baar"},
+ {""},
+ {"hello", ""},
+ {""},
+ {"world", ""}
};
-
-
- final CSVFormat format = CSVFormat.newFormat(',').withQuoteChar('\'')
- .withRecordSeparator(CRLF).withEscape('/').withIgnoreEmptyLines(true);
-
- final CSVParser parser = CSVParser.parse(code, format);
+ final CSVParser parser = CSVParser.parse(code, CSVFormat.EXCEL);
final List<CSVRecord> records = parser.getRecords();
+ assertEquals(res.length, records.size());
assertTrue(records.size() > 0);
-
- Utils.compare("Records do not match expected result", res, records);
+ for (int i = 0; i < res.length; i++) {
+ assertArrayEquals(res[i], records.get(i).values());
+ }
}
@Test
- public void testBackslashEscaping2() throws IOException {
-
- // To avoid confusion over the need for escaping chars in java code,
- // We will test with a forward slash as the escape char, and a single
- // quote as the encapsulator.
-
- final String code = ""
- + " , , \n" // 1)
- + " \t , , \n" // 2)
- + " // , /, , /,\n" // 3)
- + "";
- final String[][] res = {
- {" ", " ", " "}, // 1
- {" \t ", " ", " "}, // 2
- {" / ", " , ", " ,"}, // 3
- };
-
+ public void testForEach() throws Exception {
+ final List<CSVRecord> records = new ArrayList<CSVRecord>();
- final CSVFormat format = CSVFormat.newFormat(',')
- .withRecordSeparator(CRLF).withEscape('/').withIgnoreEmptyLines(true);
+ final Reader in = new StringReader("a,b,c\n1,2,3\nx,y,z");
- final CSVParser parser = CSVParser.parse(code, format);
- final List<CSVRecord> records = parser.getRecords();
- assertTrue(records.size() > 0);
+ for (final CSVRecord record : CSVFormat.DEFAULT.parse(in)) {
+ records.add(record);
+ }
- Utils.compare("", res, records);
+ 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 testDefaultFormat() throws IOException {
- final String code = ""
- + "a,b#\n" // 1)
- + "\"\n\",\" \",#\n" // 2)
- + "#,\"\"\n" // 3)
- + "# Final comment\n"// 4)
- ;
- final String[][] res = {
- {"a", "b#"},
- {"\n", " ", "#"},
- {"#", ""},
- {"# Final comment"}
- };
-
- CSVFormat format = CSVFormat.DEFAULT;
- assertFalse(format.isCommentingEnabled());
-
- 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", " ", "#"},
- };
+ 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"));
+ final Map<String, Integer> headerMap = parser.getHeaderMap();
+ final Iterator<String> columnNames = headerMap.keySet().iterator();
+ // Headers are iterated in column order.
+ Assert.assertEquals("A", columnNames.next());
+ Assert.assertEquals("B", columnNames.next());
+ Assert.assertEquals("C", columnNames.next());
+ final Iterator<CSVRecord> records = parser.iterator();
- format = CSVFormat.DEFAULT.withCommentStart('#');
- parser = CSVParser.parse(code, format);
- records = parser.getRecords();
+ // Parse to make sure getHeaderMap did not have a side-effect.
+ for (int i = 0; i < 3; i++) {
+ assertTrue(records.hasNext());
+ final CSVRecord record = records.next();
+ assertEquals(record.get(0), record.get("A"));
+ assertEquals(record.get(1), record.get("B"));
+ assertEquals(record.get(2), record.get("C"));
+ }
- Utils.compare("Failed to parse with comments", res_comments, records);
+ assertFalse(records.hasNext());
}
@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);
- final List<CSVRecord> records = parser.getRecords();
- assertEquals(4, records.size());
- }
+ public void testGetLine() throws IOException {
+ final CSVParser parser = CSVParser.parse(CSVINPUT, CSVFormat.DEFAULT.withIgnoreSurroundingSpaces(true));
+ for (final String[] re : RESULT) {
+ assertArrayEquals(re, parser.nextRecord().values());
+ }
- @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.withCommentStart('#').withHeader().parse(in);
- final Iterator<CSVRecord> records = parser.iterator();
- assertTrue(records.hasNext());
- parser.close();
- assertFalse(records.hasNext());
- records.next();
+ assertNull(parser.nextRecord());
}
@Test
- public void testCarriageReturnEndings() throws IOException {
- final String code = "foo\rbaar,\rhello,world\r,kanu";
- final CSVParser parser = CSVParser.parse(code, CSVFormat.DEFAULT);
- final List<CSVRecord> records = parser.getRecords();
- assertEquals(4, records.size());
+ public void testGetLineNumberWithCR() throws Exception {
+ this.validateLineNumbers(String.valueOf(CR));
}
@Test
- public void testLineFeedEndings() throws IOException {
- final String code = "foo\nbaar,\nhello,world\n,kanu";
- final CSVParser parser = CSVParser.parse(code, CSVFormat.DEFAULT);
- final List<CSVRecord> records = parser.getRecords();
- assertEquals(4, records.size());
+ public void testGetLineNumberWithCRLF() throws Exception {
+ this.validateLineNumbers(CRLF);
}
@Test
- public void testIgnoreEmptyLines() throws IOException {
- 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);
- final List<CSVRecord> records = parser.getRecords();
- assertEquals(3, records.size());
+ public void testGetLineNumberWithLF() throws Exception {
+ this.validateLineNumbers(String.valueOf(LF));
}
@Test
- public void testForEach() throws Exception {
- final List<CSVRecord> records = new ArrayList<CSVRecord>();
-
- 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());
+ public void testGetRecordNumberWithCR() throws Exception {
+ this.validateRecordNumbers(String.valueOf(CR));
}
@Test
- public void testRoundtrip() throws Exception {
- final StringWriter out = new StringWriter();
- 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();
+ public void testGetRecordNumberWithCRLF() throws Exception {
+ this.validateRecordNumbers(CRLF);
}
@Test
- public void testIterator() throws Exception {
- final Reader in = new StringReader("a,b,c\n1,2,3\nx,y,z");
-
- final Iterator<CSVRecord> iterator = CSVFormat.DEFAULT.parse(in).iterator();
-
- assertTrue(iterator.hasNext());
- try {
- iterator.remove();
- fail("expected UnsupportedOperationException");
- } catch (final UnsupportedOperationException expected) {
- // expected
- }
- assertArrayEquals(new String[]{"a", "b", "c"}, iterator.next().values());
- assertArrayEquals(new String[]{"1", "2", "3"}, iterator.next().values());
- assertTrue(iterator.hasNext());
- assertTrue(iterator.hasNext());
- assertTrue(iterator.hasNext());
- assertArrayEquals(new String[]{"x", "y", "z"}, iterator.next().values());
- assertFalse(iterator.hasNext());
+ public void testGetRecordNumberWithLF() throws Exception {
+ this.validateRecordNumbers(String.valueOf(LF));
+ }
- try {
- iterator.next();
- fail("NoSuchElementException expected");
- } catch (final NoSuchElementException e) {
- // expected
+ @Test
+ public void testGetRecords() throws IOException {
+ final CSVParser parser = CSVParser.parse(CSVINPUT, CSVFormat.DEFAULT.withIgnoreSurroundingSpaces(true));
+ 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());
}
}
- @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);
-
- final Iterator<CSVRecord> itr1 = parser.iterator();
- final Iterator<CSVRecord> itr2 = parser.iterator();
-
- final CSVRecord first = itr1.next();
- assertEquals("a", first.get(0));
- assertEquals("b", first.get(1));
- assertEquals("c", first.get(2));
-
- final CSVRecord second = itr2.next();
- assertEquals("d", second.get(0));
- assertEquals("e", second.get(1));
- assertEquals("f", second.get(2));
+ @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));
+ CSVRecord record;
+ assertEquals(0, parser.getRecordNumber());
+ assertEquals(0, parser.getCurrentLineNumber());
+ assertNotNull(record = parser.nextRecord());
+ assertEquals(3, parser.getCurrentLineNumber());
+ assertEquals(1, record.getRecordNumber());
+ assertEquals(1, parser.getRecordNumber());
+ assertNotNull(record = parser.nextRecord());
+ assertEquals(6, parser.getCurrentLineNumber());
+ assertEquals(2, record.getRecordNumber());
+ assertEquals(2, parser.getRecordNumber());
+ assertNotNull(record = parser.nextRecord());
+ assertEquals(8, parser.getCurrentLineNumber());
+ assertEquals(3, record.getRecordNumber());
+ assertEquals(3, parser.getRecordNumber());
+ assertNull(record = parser.nextRecord());
+ assertEquals(8, parser.getCurrentLineNumber());
+ assertEquals(3, parser.getRecordNumber());
}
@Test
@@ -527,27 +527,6 @@ 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(true)
- .parse(in).iterator();
- final CSVRecord record = records.next();
- assertEquals("1", record.get("a"));
- assertEquals("2", record.get("b"));
- assertEquals("3", record.get("c"));
- }
-
- @Test
- public void testSkipAutoHeader() throws Exception {
- final Reader in = new StringReader("a,b,c\n1,2,3\nx,y,z");
- final Iterator<CSVRecord> records = CSVFormat.DEFAULT.withHeader().parse(in).iterator();
- final CSVRecord record = records.next();
- assertEquals("1", record.get("a"));
- assertEquals("2", record.get("b"));
- assertEquals("3", record.get("c"));
- }
-
- @Test
public void testHeaderComment() throws Exception {
final Reader in = new StringReader("# comment\na,b,c\n1,2,3\nx,y,z");
@@ -565,45 +544,56 @@ public class CSVParserTest {
}
@Test
- public void testProvidedHeader() throws Exception {
+ public void testIgnoreEmptyLines() throws IOException {
+ 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);
+ final List<CSVRecord> records = parser.getRecords();
+ assertEquals(3, records.size());
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testInvalidFormat() throws Exception {
+ final CSVFormat invalidFormat = CSVFormat.DEFAULT.withDelimiter(CR);
+ new CSVParser(null, invalidFormat).close();
+ }
+
+ @Test
+ public void testIterator() 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").parse(in).iterator();
+ final Iterator<CSVRecord> iterator = CSVFormat.DEFAULT.parse(in).iterator();
- for (int i = 0; i < 3; i++) {
- assertTrue(records.hasNext());
- final CSVRecord record = records.next();
- assertTrue(record.isMapped("A"));
- assertTrue(record.isMapped("B"));
- assertTrue(record.isMapped("C"));
- assertFalse(record.isMapped("NOT MAPPED"));
- assertEquals(record.get(0), record.get("A"));
- assertEquals(record.get(1), record.get("B"));
- assertEquals(record.get(2), record.get("C"));
+ assertTrue(iterator.hasNext());
+ try {
+ iterator.remove();
+ fail("expected UnsupportedOperationException");
+ } catch (final UnsupportedOperationException expected) {
+ // expected
}
+ assertArrayEquals(new String[]{"a", "b", "c"}, iterator.next().values());
+ assertArrayEquals(new String[]{"1", "2", "3"}, iterator.next().values());
+ assertTrue(iterator.hasNext());
+ assertTrue(iterator.hasNext());
+ assertTrue(iterator.hasNext());
+ assertArrayEquals(new String[]{"x", "y", "z"}, iterator.next().values());
+ assertFalse(iterator.hasNext());
- assertFalse(records.hasNext());
+ try {
+ iterator.next();
+ fail("NoSuchElementException expected");
+ } catch (final NoSuchElementException e) {
+ // expected
+ }
}
@Test
- public void testProvidedHeaderAuto() throws Exception {
- final Reader in = new StringReader("a,b,c\n1,2,3\nx,y,z");
-
- final Iterator<CSVRecord> records = CSVFormat.DEFAULT.withHeader().parse(in).iterator();
-
- for (int i = 0; i < 2; i++) {
- assertTrue(records.hasNext());
- final CSVRecord record = records.next();
- assertTrue(record.isMapped("a"));
- assertTrue(record.isMapped("b"));
- assertTrue(record.isMapped("c"));
- assertFalse(record.isMapped("NOT MAPPED"));
- assertEquals(record.get(0), record.get("a"));
- assertEquals(record.get(1), record.get("b"));
- assertEquals(record.get(2), record.get("c"));
- }
-
- assertFalse(records.hasNext());
+ public void testLineFeedEndings() throws IOException {
+ final String code = "foo\nbaar,\nhello,world\n,kanu";
+ final CSVParser parser = CSVParser.parse(code, CSVFormat.DEFAULT);
+ final List<CSVRecord> records = parser.getRecords();
+ assertEquals(4, records.size());
}
@Test
@@ -641,93 +631,43 @@ public class CSVParserTest {
assertFalse(records.hasNext());
}
- @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"));
- final Map<String, Integer> headerMap = parser.getHeaderMap();
- final Iterator<String> columnNames = headerMap.keySet().iterator();
- // Headers are iterated in column order.
- Assert.assertEquals("A", columnNames.next());
- Assert.assertEquals("B", columnNames.next());
- Assert.assertEquals("C", columnNames.next());
- final Iterator<CSVRecord> records = parser.iterator();
-
- // Parse to make sure getHeaderMap did not have a side-effect.
- for (int i = 0; i < 3; i++) {
- assertTrue(records.hasNext());
- final CSVRecord record = records.next();
- assertEquals(record.get(0), record.get("A"));
- assertEquals(record.get(1), record.get("B"));
- assertEquals(record.get(2), record.get("C"));
- }
-
- assertFalse(records.hasNext());
- }
-
- @Test
- public void testNoHeaderMap() throws Exception {
- final CSVParser parser = CSVParser.parse("a,b,c\n1,2,3\nx,y,z", CSVFormat.DEFAULT);
- Assert.assertNull(parser.getHeaderMap());
- }
-
- @Test
- public void testGetLineNumberWithLF() throws Exception {
- this.validateLineNumbers(String.valueOf(LF));
- }
+ @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);
- @Test
- public void testGetLineNumberWithCRLF() throws Exception {
- this.validateLineNumbers(CRLF);
- }
+ final Iterator<CSVRecord> itr1 = parser.iterator();
+ final Iterator<CSVRecord> itr2 = parser.iterator();
- @Test
- public void testGetLineNumberWithCR() throws Exception {
- this.validateLineNumbers(String.valueOf(CR));
- }
+ final CSVRecord first = itr1.next();
+ assertEquals("a", first.get(0));
+ assertEquals("b", first.get(1));
+ assertEquals("c", first.get(2));
- @Test
- public void testGetRecordNumberWithLF() throws Exception {
- this.validateRecordNumbers(String.valueOf(LF));
+ final CSVRecord second = itr2.next();
+ assertEquals("d", second.get(0));
+ assertEquals("e", second.get(1));
+ assertEquals("f", second.get(2));
}
- @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));
- CSVRecord record;
- assertEquals(0, parser.getRecordNumber());
- assertEquals(0, parser.getCurrentLineNumber());
- assertNotNull(record = parser.nextRecord());
- assertEquals(3, parser.getCurrentLineNumber());
- assertEquals(1, record.getRecordNumber());
- assertEquals(1, parser.getRecordNumber());
- assertNotNull(record = parser.nextRecord());
- assertEquals(6, parser.getCurrentLineNumber());
- assertEquals(2, record.getRecordNumber());
- assertEquals(2, parser.getRecordNumber());
- assertNotNull(record = parser.nextRecord());
- assertEquals(8, parser.getCurrentLineNumber());
- assertEquals(3, record.getRecordNumber());
- assertEquals(3, parser.getRecordNumber());
- assertNull(record = parser.nextRecord());
- assertEquals(8, parser.getCurrentLineNumber());
- assertEquals(3, parser.getRecordNumber());
+ @Test(expected = IllegalArgumentException.class)
+ public void testNewCSVParserNullReaderFormat() throws Exception {
+ new CSVParser(null, CSVFormat.DEFAULT);
}
- @Test
- public void testGetRecordNumberWithCRLF() throws Exception {
- this.validateRecordNumbers(CRLF);
+ @Test(expected = IllegalArgumentException.class)
+ public void testNewCSVParserReaderNullFormat() throws Exception {
+ new CSVParser(new StringReader(""), null);
}
@Test
- public void testGetRecordNumberWithCR() throws Exception {
- this.validateRecordNumbers(String.valueOf(CR));
+ public void testNoHeaderMap() throws Exception {
+ final CSVParser parser = CSVParser.parse("a,b,c\n1,2,3\nx,y,z", CSVFormat.DEFAULT);
+ Assert.assertNull(parser.getHeaderMap());
}
@Test(expected = IllegalArgumentException.class)
- public void testInvalidFormat() throws Exception {
- final CSVFormat invalidFormat = CSVFormat.DEFAULT.withDelimiter(CR);
- new CSVParser(null, invalidFormat).close();
+ public void testParseFileNullFormat() throws Exception {
+ CSVParser.parse(new File(""), null);
}
@Test(expected = IllegalArgumentException.class)
@@ -736,21 +676,11 @@ public class CSVParserTest {
}
@Test(expected = IllegalArgumentException.class)
- public void testParseFileNullFormat() throws Exception {
- CSVParser.parse(new File(""), null);
- }
-
- @Test(expected = IllegalArgumentException.class)
public void testParseNullStringFormat() throws Exception {
CSVParser.parse((String) null, CSVFormat.DEFAULT);
}
@Test(expected = IllegalArgumentException.class)
- public void testParseStringNullFormat() throws Exception {
- CSVParser.parse("csv data", null);
- }
-
- @Test(expected = IllegalArgumentException.class)
public void testParseNullUrlCharsetFormat() throws Exception {
CSVParser.parse(null, Charset.defaultCharset(), CSVFormat.DEFAULT);
}
@@ -761,35 +691,88 @@ public class CSVParserTest {
}
@Test(expected = IllegalArgumentException.class)
+ public void testParseStringNullFormat() throws Exception {
+ CSVParser.parse("csv data", null);
+ }
+
+ @Test(expected = IllegalArgumentException.class)
public void testParseUrlCharsetNullFormat() throws Exception {
CSVParser.parse(new URL("http://commons.apache.org"), Charset.defaultCharset(), null);
}
- @Test(expected = IllegalArgumentException.class)
- public void testNewCSVParserNullReaderFormat() throws Exception {
- new CSVParser(null, CSVFormat.DEFAULT);
+ @Test
+ public void testProvidedHeader() 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").parse(in).iterator();
+
+ for (int i = 0; i < 3; i++) {
+ assertTrue(records.hasNext());
+ final CSVRecord record = records.next();
+ assertTrue(record.isMapped("A"));
+ assertTrue(record.isMapped("B"));
+ assertTrue(record.isMapped("C"));
+ assertFalse(record.isMapped("NOT MAPPED"));
+ assertEquals(record.get(0), record.get("A"));
+ assertEquals(record.get(1), record.get("B"));
+ assertEquals(record.get(2), record.get("C"));
+ }
+
+ assertFalse(records.hasNext());
}
- @Test(expected = IllegalArgumentException.class)
- public void testNewCSVParserReaderNullFormat() throws Exception {
- new CSVParser(new StringReader(""), null);
+ @Test
+ public void testProvidedHeaderAuto() throws Exception {
+ final Reader in = new StringReader("a,b,c\n1,2,3\nx,y,z");
+
+ final Iterator<CSVRecord> records = CSVFormat.DEFAULT.withHeader().parse(in).iterator();
+
+ for (int i = 0; i < 2; i++) {
+ assertTrue(records.hasNext());
+ final CSVRecord record = records.next();
+ assertTrue(record.isMapped("a"));
+ assertTrue(record.isMapped("b"));
+ assertTrue(record.isMapped("c"));
+ assertFalse(record.isMapped("NOT MAPPED"));
+ assertEquals(record.get(0), record.get("a"));
+ assertEquals(record.get(1), record.get("b"));
+ assertEquals(record.get(2), record.get("c"));
+ }
+
+ assertFalse(records.hasNext());
}
- private void validateRecordNumbers(final String lineSeparator) throws IOException {
- final CSVParser parser = CSVParser.parse("a" + lineSeparator + "b" + lineSeparator + "c", CSVFormat.DEFAULT.withRecordSeparator(lineSeparator));
- CSVRecord record;
- assertEquals(0, parser.getRecordNumber());
- assertNotNull(record = parser.nextRecord());
- assertEquals(1, record.getRecordNumber());
- assertEquals(1, parser.getRecordNumber());
- assertNotNull(record = parser.nextRecord());
- assertEquals(2, record.getRecordNumber());
- assertEquals(2, parser.getRecordNumber());
- assertNotNull(record = parser.nextRecord());
- assertEquals(3, record.getRecordNumber());
- assertEquals(3, parser.getRecordNumber());
- assertNull(record = parser.nextRecord());
- assertEquals(3, parser.getRecordNumber());
+ @Test
+ public void testRoundtrip() throws Exception {
+ final StringWriter out = new StringWriter();
+ 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
+ public void testSkipAutoHeader() throws Exception {
+ final Reader in = new StringReader("a,b,c\n1,2,3\nx,y,z");
+ final Iterator<CSVRecord> records = CSVFormat.DEFAULT.withHeader().parse(in).iterator();
+ final CSVRecord record = records.next();
+ assertEquals("1", record.get("a"));
+ assertEquals("2", record.get("b"));
+ assertEquals("3", record.get("c"));
+ }
+
+ @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(true)
+ .parse(in).iterator();
+ final CSVRecord record = records.next();
+ assertEquals("1", record.get("a"));
+ assertEquals("2", record.get("b"));
+ assertEquals("3", record.get("c"));
}
private void validateLineNumbers(final String lineSeparator) throws IOException {
@@ -807,4 +790,21 @@ public class CSVParserTest {
assertEquals(2, parser.getCurrentLineNumber());
}
+ private void validateRecordNumbers(final String lineSeparator) throws IOException {
+ final CSVParser parser = CSVParser.parse("a" + lineSeparator + "b" + lineSeparator + "c", CSVFormat.DEFAULT.withRecordSeparator(lineSeparator));
+ CSVRecord record;
+ assertEquals(0, parser.getRecordNumber());
+ assertNotNull(record = parser.nextRecord());
+ assertEquals(1, record.getRecordNumber());
+ assertEquals(1, parser.getRecordNumber());
+ assertNotNull(record = parser.nextRecord());
+ assertEquals(2, record.getRecordNumber());
+ assertEquals(2, parser.getRecordNumber());
+ assertNotNull(record = parser.nextRecord());
+ assertEquals(3, record.getRecordNumber());
+ assertEquals(3, parser.getRecordNumber());
+ assertNull(record = parser.nextRecord());
+ assertEquals(3, parser.getRecordNumber());
+ }
+
}