diff --git a/src/test/java/org/apache/commons/csv/CSVParserTest.java b/src/test/java/org/apache/commons/csv/CSVParserTest.java index d15a7ad8..32be45de 100644 --- a/src/test/java/org/apache/commons/csv/CSVParserTest.java +++ b/src/test/java/org/apache/commons/csv/CSVParserTest.java @@ -258,6 +258,11 @@ public class CSVParserTest { parser.close(); } + @Test(expected = IllegalArgumentException.class) + public void testDuplicateHeaders() throws Exception { + CSVParser.parse("a,b,a\n1,2,3\nx,y,z", CSVFormat.DEFAULT.withHeader(new String[] {})); + } + @Test public void testEmptyFile() throws Exception { final CSVParser parser = CSVParser.parse("", CSVFormat.DEFAULT); @@ -299,25 +304,6 @@ public class CSVParserTest { } } - @Test - @Ignore - public void testStartWithEmptyLinesThenHeaders() throws Exception { - final String[] codes = {"\r\n\r\n\r\nhello,\r\n\r\n\r\n", "hello,\n\n\n", "hello,\"\"\r\n\r\n\r\n", - "hello,\"\"\n\n\n"}; - final String[][] res = {{"hello", ""}, {""}, // Excel format does not ignore empty lines - {""}}; - for (final String code : codes) { - final CSVParser parser = CSVParser.parse(code, CSVFormat.EXCEL); - final List 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 testEndOfFileBehaviorCSV() throws Exception { final String[] codes = { "hello,\r\n\r\nworld,\r\n", "hello,\r\n\r\nworld,", "hello,\r\n\r\nworld,\"\"\r\n", @@ -445,11 +431,6 @@ public class CSVParserTest { parser.close(); } - @Test(expected = IllegalArgumentException.class) - public void testDuplicateHeaders() throws Exception { - CSVParser.parse("a,b,a\n1,2,3\nx,y,z", CSVFormat.DEFAULT.withHeader(new String[] {})); - } - @Test public void testGetLine() throws IOException { final CSVParser parser = CSVParser.parse(CSV_INPUT, CSVFormat.DEFAULT.withIgnoreSurroundingSpaces()); @@ -476,16 +457,6 @@ public class CSVParserTest { this.validateLineNumbers(String.valueOf(LF)); } - @Test - public void testGetRecordPositionWithCRLF() throws Exception { - this.validateRecordPosition(CRLF); - } - - @Test - public void testGetRecordPositionWithLF() throws Exception { - this.validateRecordPosition(String.valueOf(LF)); - } - @Test public void testGetOneLine() throws IOException { final CSVParser parser = CSVParser.parse(CSV_INPUT_1, CSVFormat.DEFAULT); @@ -534,6 +505,16 @@ public class CSVParserTest { this.validateRecordNumbers(String.valueOf(LF)); } + @Test + public void testGetRecordPositionWithCRLF() throws Exception { + this.validateRecordPosition(CRLF); + } + + @Test + public void testGetRecordPositionWithLF() throws Exception { + this.validateRecordPosition(String.valueOf(LF)); + } + @Test public void testGetRecords() throws IOException { final CSVParser parser = CSVParser.parse(CSV_INPUT, CSVFormat.DEFAULT.withIgnoreSurroundingSpaces()); @@ -588,6 +569,23 @@ public class CSVParserTest { assertFalse(records.hasNext()); } + @Test + public void testHeaderComment() throws Exception { + final Reader in = new StringReader("# comment\na,b,c\n1,2,3\nx,y,z"); + + final Iterator records = CSVFormat.DEFAULT.withCommentMarker('#').withHeader().parse(in).iterator(); + + for (int i = 0; i < 2; 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 testHeaderMissing() throws Exception { final Reader in = new StringReader("a,,c\n1,2,3\nx,y,z"); @@ -604,10 +602,10 @@ public class CSVParserTest { assertFalse(records.hasNext()); } - @Test(expected = IllegalArgumentException.class) - public void testHeadersMissingException() throws Exception { + @Test + public void testHeaderMissingWithNull() throws Exception { final Reader in = new StringReader("a,,c,,d\n1,2,3,4\nx,y,z,zz"); - CSVFormat.DEFAULT.withHeader().parse(in).iterator(); + CSVFormat.DEFAULT.withHeader().withNullString("").withAllowMissingColumnNames().parse(in).iterator(); } @Test @@ -616,27 +614,21 @@ public class CSVParserTest { CSVFormat.DEFAULT.withHeader().withAllowMissingColumnNames().parse(in).iterator(); } - @Test - public void testHeaderMissingWithNull() throws Exception { + @Test(expected = IllegalArgumentException.class) + public void testHeadersMissingException() throws Exception { final Reader in = new StringReader("a,,c,,d\n1,2,3,4\nx,y,z,zz"); - CSVFormat.DEFAULT.withHeader().withNullString("").withAllowMissingColumnNames().parse(in).iterator(); + CSVFormat.DEFAULT.withHeader().parse(in).iterator(); } @Test - public void testHeaderComment() throws Exception { - final Reader in = new StringReader("# comment\na,b,c\n1,2,3\nx,y,z"); - - final Iterator records = CSVFormat.DEFAULT.withCommentMarker('#').withHeader().parse(in).iterator(); - - for (int i = 0; i < 2; 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()); + public void testIgnoreCaseHeaderMapping() throws Exception { + final Reader in = new StringReader("1,2,3"); + final Iterator records = CSVFormat.DEFAULT.withHeader("One", "TWO", "three").withIgnoreHeaderCase() + .parse(in).iterator(); + final CSVRecord record = records.next(); + assertEquals("1", record.get("one")); + assertEquals("2", record.get("two")); + assertEquals("3", record.get("THREE")); } @Test @@ -868,14 +860,14 @@ 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 records = CSVFormat.DEFAULT.withHeader("a", "b", "c").withSkipHeaderRecord() + public void testSkipHeaderOverrideDuplicateHeaders() throws Exception { + final Reader in = new StringReader("a,a,a\n1,2,3\nx,y,z"); + final Iterator records = CSVFormat.DEFAULT.withHeader("X", "Y", "Z").withSkipHeaderRecord() .parse(in).iterator(); final CSVRecord record = records.next(); - assertEquals("1", record.get("a")); - assertEquals("2", record.get("b")); - assertEquals("3", record.get("c")); + assertEquals("1", record.get("X")); + assertEquals("2", record.get("Y")); + assertEquals("3", record.get("Z")); } @Test @@ -890,14 +882,33 @@ 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 records = CSVFormat.DEFAULT.withHeader("X", "Y", "Z").withSkipHeaderRecord() + public void testSkipSetHeader() throws Exception { + final Reader in = new StringReader("a,b,c\n1,2,3\nx,y,z"); + final Iterator records = CSVFormat.DEFAULT.withHeader("a", "b", "c").withSkipHeaderRecord() .parse(in).iterator(); final CSVRecord record = records.next(); - assertEquals("1", record.get("X")); - assertEquals("2", record.get("Y")); - assertEquals("3", record.get("Z")); + assertEquals("1", record.get("a")); + assertEquals("2", record.get("b")); + assertEquals("3", record.get("c")); + } + + @Test + @Ignore + public void testStartWithEmptyLinesThenHeaders() throws Exception { + final String[] codes = {"\r\n\r\n\r\nhello,\r\n\r\n\r\n", "hello,\n\n\n", "hello,\"\"\r\n\r\n\r\n", + "hello,\"\"\n\n\n"}; + final String[][] res = {{"hello", ""}, {""}, // Excel format does not ignore empty lines + {""}}; + for (final String code : codes) { + final CSVParser parser = CSVParser.parse(code, CSVFormat.EXCEL); + final List 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(); + } } private void validateLineNumbers(final String lineSeparator) throws IOException { @@ -995,15 +1006,4 @@ public class CSVParserTest { parser.close(); } - - @Test - public void testIgnoreCaseHeaderMapping() throws Exception { - final Reader in = new StringReader("1,2,3"); - final Iterator records = CSVFormat.DEFAULT.withHeader("One", "TWO", "three").withIgnoreHeaderCase() - .parse(in).iterator(); - final CSVRecord record = records.next(); - assertEquals("1", record.get("one")); - assertEquals("2", record.get("two")); - assertEquals("3", record.get("THREE")); - } }