From 7aa3b46719521ce26d9cca6dea7a96e93e2aa66e Mon Sep 17 00:00:00 2001 From: Gary Gregory Date: Wed, 11 Nov 2020 14:01:17 -0500 Subject: [PATCH 1/2] Sort members. --- .../apache/commons/csv/CSVPrinterTest.java | 46 +++++++++---------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/src/test/java/org/apache/commons/csv/CSVPrinterTest.java b/src/test/java/org/apache/commons/csv/CSVPrinterTest.java index c7e305eeeb..0c203b8950 100644 --- a/src/test/java/org/apache/commons/csv/CSVPrinterTest.java +++ b/src/test/java/org/apache/commons/csv/CSVPrinterTest.java @@ -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 list = new LinkedList<>(); @@ -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(); @@ -1447,6 +1469,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); @@ -1457,7 +1480,6 @@ public void testRandomTdf() throws Exception { doRandom(CSVFormat.TDF, ITERATIONS_FOR_RANDOM_TEST); } - @Test public void testSingleLineComment() throws IOException { final StringWriter sw = new StringWriter(); @@ -1538,28 +1560,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(); } From 979134603b23af24a945d48461a6b6c6d6d79618 Mon Sep 17 00:00:00 2001 From: Gary Gregory Date: Wed, 11 Nov 2020 14:08:30 -0500 Subject: [PATCH 2/2] Add org.apache.commons.csv.CSVPrinterTest.testPrintRecordsWithCSVRecord(). --- .../org/apache/commons/csv/CSVPrinterTest.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/test/java/org/apache/commons/csv/CSVPrinterTest.java b/src/test/java/org/apache/commons/csv/CSVPrinterTest.java index 0c203b8950..7126e9d417 100644 --- a/src/test/java/org/apache/commons/csv/CSVPrinterTest.java +++ b/src/test/java/org/apache/commons/csv/CSVPrinterTest.java @@ -1336,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;