Skip to content

Commit 83b2b9c

Browse files
authored
Improve record and printer Coverage (apache#66)
* Improve CSVRecord and CSVPrinter coverage * remove useless test code and test throws * add space
1 parent 6c66ca4 commit 83b2b9c

2 files changed

Lines changed: 30 additions & 0 deletions

File tree

src/test/java/org/apache/commons/csv/CSVPrinterTest.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1538,6 +1538,28 @@ public void testTrimOnTwoColumns() throws IOException {
15381538
}
15391539
}
15401540

1541+
@Test
1542+
public void testNotFlushable() throws IOException {
1543+
final Appendable out = new StringBuilder();
1544+
try (final CSVPrinter printer = new CSVPrinter(out, CSVFormat.DEFAULT)) {
1545+
printer.printRecord("a", "b", "c");
1546+
assertEquals("a,b,c" + recordSeparator, out.toString());
1547+
printer.flush();
1548+
}
1549+
}
1550+
1551+
@Test
1552+
public void testCRComment() throws IOException {
1553+
final StringWriter sw = new StringWriter();
1554+
final Object value = "abc";
1555+
try (final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withCommentMarker('#'))) {
1556+
printer.print(value);
1557+
printer.printComment("This is a comment\r\non multiple lines\rthis is next comment\r");
1558+
assertEquals("abc" + recordSeparator + "# This is a comment" + recordSeparator + "# on multiple lines"
1559+
+ recordSeparator + "# this is next comment" + recordSeparator + "# " + recordSeparator, sw.toString());
1560+
}
1561+
}
1562+
15411563
private String[] toFirstRecordValues(final String expected, final CSVFormat format) throws IOException {
15421564
return CSVParser.parse(expected, format).getRecords().get(0).values();
15431565
}

src/test/java/org/apache/commons/csv/CSVRecordTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,4 +301,12 @@ public void testGetWithEnum() {
301301
assertEquals(recordWithHeader.get("second"), recordWithHeader.get(EnumHeader.SECOND));
302302
assertThrows(IllegalArgumentException.class, () -> recordWithHeader.get(EnumFixture.UNKNOWN_COLUMN));
303303
}
304+
305+
@Test
306+
public void testCSVRecordNULLValues() throws IOException {
307+
final CSVParser parser = CSVParser.parse("A,B\r\nONE,TWO", CSVFormat.DEFAULT.withHeader());
308+
final CSVRecord csvRecord = new CSVRecord(parser, null, null, 0L, 0L);
309+
assertEquals(0, csvRecord.size());
310+
assertThrows(IllegalArgumentException.class, () -> csvRecord.get("B"));
311+
}
304312
}

0 commit comments

Comments
 (0)