diff --git a/src/test/java/org/apache/commons/csv/CSVPrinterTest.java b/src/test/java/org/apache/commons/csv/CSVPrinterTest.java index 0f9b298cae..85353dc1be 100644 --- a/src/test/java/org/apache/commons/csv/CSVPrinterTest.java +++ b/src/test/java/org/apache/commons/csv/CSVPrinterTest.java @@ -571,6 +571,33 @@ public void testExcelPrintAllArrayOfArrays() throws IOException { } } + @Test + public void testExcelPrintAllArrayOfArraysWithFirstEmptyValue2() throws IOException { + final StringWriter sw = new StringWriter(); + try (final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.EXCEL)) { + printer.printRecords((Object[]) new String[][] { { "" } }); + assertEquals("\"\"" + recordSeparator, sw.toString()); + } + } + + @Test + public void testExcelPrintAllArrayOfArraysWithFirstSpaceValue1() throws IOException { + final StringWriter sw = new StringWriter(); + try (final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.EXCEL)) { + printer.printRecords((Object[]) new String[][] { { " ", "r1c2" } }); + assertEquals("\" \",r1c2" + recordSeparator, sw.toString()); + } + } + + @Test + public void testExcelPrintAllArrayOfArraysWithFirstTabValue1() throws IOException { + final StringWriter sw = new StringWriter(); + try (final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.EXCEL)) { + printer.printRecords((Object[]) new String[][] { { "\t", "r1c2" } }); + assertEquals("\"\t\",r1c2" + recordSeparator, sw.toString()); + } + } + @Test public void testExcelPrintAllArrayOfLists() throws IOException { final StringWriter sw = new StringWriter(); @@ -581,6 +608,16 @@ public void testExcelPrintAllArrayOfLists() throws IOException { } } + @Test + public void testExcelPrintAllArrayOfListsWithFirstEmptyValue2() throws IOException { + final StringWriter sw = new StringWriter(); + try (final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.EXCEL)) { + printer.printRecords( + (Object[]) new List[] { Arrays.asList("") }); + assertEquals("\"\"" + recordSeparator, sw.toString()); + } + } + @Test public void testExcelPrintAllIterableOfArrays() throws IOException { final StringWriter sw = new StringWriter(); @@ -590,6 +627,15 @@ public void testExcelPrintAllIterableOfArrays() throws IOException { } } + @Test + public void testExcelPrintAllIterableOfArraysWithFirstEmptyValue2() throws IOException { + final StringWriter sw = new StringWriter(); + try (final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.EXCEL)) { + printer.printRecords(Arrays.asList(new String[][] { { "" } })); + assertEquals("\"\"" + recordSeparator, sw.toString()); + } + } + @Test public void testExcelPrintAllIterableOfLists() throws IOException { final StringWriter sw = new StringWriter(); @@ -689,10 +735,22 @@ public void testJdbcPrinter() throws IOException, ClassNotFoundException, SQLExc assertEquals("1,r1,\"long text 1\"" + recordSeparator + "2,r2,\"" + longText2 + "\"" + recordSeparator, sw.toString()); } + @Test + public void testJdbcPrinterWithFirstEmptyValue2() throws IOException, ClassNotFoundException, SQLException { + final StringWriter sw = new StringWriter(); + try (final Connection connection = getH2Connection()) { + try (final Statement stmt = connection.createStatement(); + final ResultSet resultSet = stmt.executeQuery("select '' AS EMPTYVALUE from DUAL"); + final CSVPrinter printer = CSVFormat.DEFAULT.withHeader(resultSet).print(sw)) { + printer.printRecords(resultSet); + } + } + assertEquals("EMPTYVALUE" + recordSeparator + "\"\"" + recordSeparator, sw.toString()); + } + @Test public void testJdbcPrinterWithResultSet() throws IOException, ClassNotFoundException, SQLException { final StringWriter sw = new StringWriter(); - Class.forName("org.h2.Driver"); try (final Connection connection = getH2Connection()) { setUpTable(connection); try (final Statement stmt = connection.createStatement(); @@ -729,7 +787,6 @@ public void testJdbcPrinterWithResultSetHeader() throws IOException, ClassNotFou @Test public void testJdbcPrinterWithResultSetMetaData() throws IOException, ClassNotFoundException, SQLException { final StringWriter sw = new StringWriter(); - Class.forName("org.h2.Driver"); try (final Connection connection = getH2Connection()) { setUpTable(connection); try (final Statement stmt = connection.createStatement(); @@ -1748,4 +1805,5 @@ private void tryFormat(final List list, final Character quote, final Cha } assertEquals(expected, out.toString()); } + }