Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 38 additions & 23 deletions src/test/java/org/apache/commons/csv/CSVPrinterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,18 @@ public void testCloseWithFlushOn() throws IOException {
}
}

@Test
public void testCRComment() throws IOException {
final StringWriter sw = new StringWriter();
final Object value = "abc";
try (final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withCommentMarker('#'))) {
printer.print(value);
printer.printComment("This is a comment\r\non multiple lines\rthis is next comment\r");
assertEquals("abc" + recordSeparator + "# This is a comment" + recordSeparator + "# on multiple lines"
+ recordSeparator + "# this is next comment" + recordSeparator + "# " + recordSeparator, sw.toString());
}
}

@Test
public void testCSV135() throws IOException {
final List<String> list = new LinkedList<>();
Expand Down Expand Up @@ -909,6 +921,16 @@ public void testNewCsvPrinterNullAppendableFormat() {
assertThrows(NullPointerException.class, () -> new CSVPrinter(null, CSVFormat.DEFAULT));
}

@Test
public void testNotFlushable() throws IOException {
final Appendable out = new StringBuilder();
try (final CSVPrinter printer = new CSVPrinter(out, CSVFormat.DEFAULT)) {
printer.printRecord("a", "b", "c");
assertEquals("a,b,c" + recordSeparator, out.toString());
printer.flush();
}
}

@Test
public void testParseCustomNullValues() throws IOException {
final StringWriter sw = new StringWriter();
Expand Down Expand Up @@ -1314,6 +1336,21 @@ public void testPrintReaderWithoutQuoteToWriter() throws IOException {
assertEquals(content, sw.toString());
}

@Test
public void testPrintRecordsWithCSVRecord() throws IOException {
final String[] values = new String[] {"A", "B", "C"};
final String rowData = StringUtils.join(values, ',');
final CharArrayWriter charArrayWriter = new CharArrayWriter(0);
try (final CSVParser parser = CSVFormat.DEFAULT.parse(new StringReader(rowData));
CSVPrinter csvPrinter = CSVFormat.INFORMIX_UNLOAD.print(charArrayWriter)) {
for (CSVRecord record : parser) {
csvPrinter.printRecord(record);
}
}
assertEquals(6, charArrayWriter.size());
assertEquals("A|B|C" + CSVFormat.INFORMIX_UNLOAD.getRecordSeparator(), charArrayWriter.toString());
}

@Test
public void testPrintRecordsWithEmptyVector() throws IOException {
final PrintStream out = System.out;
Expand Down Expand Up @@ -1447,6 +1484,7 @@ public void testRandomPostgreSqlText() throws Exception {
doRandom(CSVFormat.POSTGRESQL_TEXT, ITERATIONS_FOR_RANDOM_TEST);
}


@Test
public void testRandomRfc4180() throws Exception {
doRandom(CSVFormat.RFC4180, ITERATIONS_FOR_RANDOM_TEST);
Expand All @@ -1457,7 +1495,6 @@ public void testRandomTdf() throws Exception {
doRandom(CSVFormat.TDF, ITERATIONS_FOR_RANDOM_TEST);
}


@Test
public void testSingleLineComment() throws IOException {
final StringWriter sw = new StringWriter();
Expand Down Expand Up @@ -1538,28 +1575,6 @@ public void testTrimOnTwoColumns() throws IOException {
}
}

@Test
public void testNotFlushable() throws IOException {
final Appendable out = new StringBuilder();
try (final CSVPrinter printer = new CSVPrinter(out, CSVFormat.DEFAULT)) {
printer.printRecord("a", "b", "c");
assertEquals("a,b,c" + recordSeparator, out.toString());
printer.flush();
}
}

@Test
public void testCRComment() throws IOException {
final StringWriter sw = new StringWriter();
final Object value = "abc";
try (final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withCommentMarker('#'))) {
printer.print(value);
printer.printComment("This is a comment\r\non multiple lines\rthis is next comment\r");
assertEquals("abc" + recordSeparator + "# This is a comment" + recordSeparator + "# on multiple lines"
+ recordSeparator + "# this is next comment" + recordSeparator + "# " + recordSeparator, sw.toString());
}
}

private String[] toFirstRecordValues(final String expected, final CSVFormat format) throws IOException {
return CSVParser.parse(expected, format).getRecords().get(0).values();
}
Expand Down