@@ -287,6 +287,19 @@ public void testCloseWithFlushOn() throws IOException {
287287 }
288288 }
289289
290+ @ Test
291+ public void testCSV135 () throws IOException {
292+ List <String > l = new LinkedList <String >();
293+ l .add ("\" \" " ); // ""
294+ l .add ("\\ \\ " ); // \\
295+ l .add ("\\ \" \\ " ); // \"\
296+ tryFormat (l , null , null , "\" \" ,\\ \\ ,\\ \" \\ " ); // "",\\,\"\ (unchanged)
297+ tryFormat (l , '"' , null , "\" \" \" \" \" \" ,\\ \\ ,\" \\ \" \" \\ \" " ); // """""",\\,"\""\" (quoted, and embedded DQ doubled)
298+ tryFormat (l , null , '\\' , "\" \" ,\\ \\ \\ \\ ,\\ \\ \" \\ \\ " ); // "",\\\\,\\"\\ (escapes escaped, not quoted)
299+ tryFormat (l , '"' , '\\' , "\" \\ \" \\ \" \" ,\" \\ \\ \\ \\ \" ,\" \\ \\ \\ \" \\ \\ \" " ); // "\"\"","\\\\","\\\"\\" (quoted, and embedded DQ & escape escaped)
300+ tryFormat (l , '"' , '"' , "\" \" \" \" \" \" ,\\ \\ ,\" \\ \" \" \\ \" " ); // """""",\\,"\""\" (quoted, embedded DQ escaped)
301+ }
302+
290303 @ Test
291304 public void testDelimeterQuoted () throws IOException {
292305 final StringWriter sw = new StringWriter ();
@@ -722,29 +735,29 @@ public void testMongoDbCsvTabInValue() throws IOException {
722735 }
723736
724737 @ Test
725- public void testMongoDbTsvCommaInValue () throws IOException {
738+ public void testMongoDbTsvBasic () throws IOException {
726739 final StringWriter sw = new StringWriter ();
727740 try (final CSVPrinter printer = new CSVPrinter (sw , CSVFormat .MONGODB_TSV )) {
728- printer .printRecord ("a,b " , "c " );
729- assertEquals ("a,b \t c " + recordSeparator , sw .toString ());
741+ printer .printRecord ("a" , "b " );
742+ assertEquals ("a\t b " + recordSeparator , sw .toString ());
730743 }
731744 }
732745
733746 @ Test
734- public void testMongoDbTsvTabInValue () throws IOException {
747+ public void testMongoDbTsvCommaInValue () throws IOException {
735748 final StringWriter sw = new StringWriter ();
736749 try (final CSVPrinter printer = new CSVPrinter (sw , CSVFormat .MONGODB_TSV )) {
737- printer .printRecord ("a\t b " , "c" );
738- assertEquals ("\" a \t b \" \t c" + recordSeparator , sw .toString ());
750+ printer .printRecord ("a,b " , "c" );
751+ assertEquals ("a,b \t c" + recordSeparator , sw .toString ());
739752 }
740753 }
741754
742755 @ Test
743- public void testMongoDbTsvBasic () throws IOException {
756+ public void testMongoDbTsvTabInValue () throws IOException {
744757 final StringWriter sw = new StringWriter ();
745758 try (final CSVPrinter printer = new CSVPrinter (sw , CSVFormat .MONGODB_TSV )) {
746- printer .printRecord ("a" , "b " );
747- assertEquals ("a\t b" + recordSeparator , sw .toString ());
759+ printer .printRecord ("a\t b " , "c " );
760+ assertEquals ("\" a\t b\" \t c " + recordSeparator , sw .toString ());
748761 }
749762 }
750763
@@ -1243,6 +1256,48 @@ public void testPrintOnePositiveInteger() throws IOException {
12431256 }
12441257 }
12451258
1259+ /**
1260+ * Test to target the use of {@link IOUtils#copy(java.io.Reader, Appendable)} which directly
1261+ * buffers the value from the Reader to the Appendable.
1262+ *
1263+ * <p>Requires the format to have no quote or escape character, value to be a
1264+ * {@link java.io.Reader Reader} and the output <i>MUST NOT</i> be a
1265+ * {@link java.io.Writer Writer} but some other Appendable.</p>
1266+ *
1267+ * @throws IOException Not expected to happen
1268+ */
1269+ @ Test
1270+ public void testPrintReaderWithoutQuoteToAppendable () throws IOException {
1271+ final StringBuilder sb = new StringBuilder ();
1272+ final String content = "testValue" ;
1273+ try (final CSVPrinter printer = new CSVPrinter (sb , CSVFormat .DEFAULT .withQuote (null ))) {
1274+ final StringReader value = new StringReader (content );
1275+ printer .print (value );
1276+ }
1277+ assertEquals (content , sb .toString ());
1278+ }
1279+
1280+ /**
1281+ * Test to target the use of {@link IOUtils#copyLarge(java.io.Reader, Writer)} which directly
1282+ * buffers the value from the Reader to the Writer.
1283+ *
1284+ * <p>Requires the format to have no quote or escape character, value to be a
1285+ * {@link java.io.Reader Reader} and the output <i>MUST</i> be a
1286+ * {@link java.io.Writer Writer}.</p>
1287+ *
1288+ * @throws IOException Not expected to happen
1289+ */
1290+ @ Test
1291+ public void testPrintReaderWithoutQuoteToWriter () throws IOException {
1292+ final StringWriter sw = new StringWriter ();
1293+ final String content = "testValue" ;
1294+ try (final CSVPrinter printer = new CSVPrinter (sw , CSVFormat .DEFAULT .withQuote (null ))) {
1295+ final StringReader value = new StringReader (content );
1296+ printer .print (value );
1297+ }
1298+ assertEquals (content , sw .toString ());
1299+ }
1300+
12461301 @ Test
12471302 public void testPrintRecordsWithEmptyVector () throws IOException {
12481303 final PrintStream out = System .out ;
@@ -1386,6 +1441,7 @@ public void testRandomTdf() throws Exception {
13861441 doRandom (CSVFormat .TDF , ITERATIONS_FOR_RANDOM_TEST );
13871442 }
13881443
1444+
13891445 @ Test
13901446 public void testSingleLineComment () throws IOException {
13911447 final StringWriter sw = new StringWriter ();
@@ -1417,7 +1473,6 @@ public void testSkipHeaderRecordFalse() throws IOException {
14171473 }
14181474 }
14191475
1420-
14211476 @ Test
14221477 public void testSkipHeaderRecordTrue () throws IOException {
14231478 // functionally identical to testHeaderNotSet, used to test CSV-153
@@ -1471,48 +1526,6 @@ private String[] toFirstRecordValues(final String expected, final CSVFormat form
14711526 return CSVParser .parse (expected , format ).getRecords ().get (0 ).values ();
14721527 }
14731528
1474- /**
1475- * Test to target the use of {@link IOUtils#copyLarge(java.io.Reader, Writer)} which directly
1476- * buffers the value from the Reader to the Writer.
1477- *
1478- * <p>Requires the format to have no quote or escape character, value to be a
1479- * {@link java.io.Reader Reader} and the output <i>MUST</i> be a
1480- * {@link java.io.Writer Writer}.</p>
1481- *
1482- * @throws IOException Not expected to happen
1483- */
1484- @ Test
1485- public void testPrintReaderWithoutQuoteToWriter () throws IOException {
1486- final StringWriter sw = new StringWriter ();
1487- final String content = "testValue" ;
1488- try (final CSVPrinter printer = new CSVPrinter (sw , CSVFormat .DEFAULT .withQuote (null ))) {
1489- final StringReader value = new StringReader (content );
1490- printer .print (value );
1491- }
1492- assertEquals (content , sw .toString ());
1493- }
1494-
1495- /**
1496- * Test to target the use of {@link IOUtils#copy(java.io.Reader, Appendable)} which directly
1497- * buffers the value from the Reader to the Appendable.
1498- *
1499- * <p>Requires the format to have no quote or escape character, value to be a
1500- * {@link java.io.Reader Reader} and the output <i>MUST NOT</i> be a
1501- * {@link java.io.Writer Writer} but some other Appendable.</p>
1502- *
1503- * @throws IOException Not expected to happen
1504- */
1505- @ Test
1506- public void testPrintReaderWithoutQuoteToAppendable () throws IOException {
1507- final StringBuilder sb = new StringBuilder ();
1508- final String content = "testValue" ;
1509- try (final CSVPrinter printer = new CSVPrinter (sb , CSVFormat .DEFAULT .withQuote (null ))) {
1510- final StringReader value = new StringReader (content );
1511- printer .print (value );
1512- }
1513- assertEquals (content , sb .toString ());
1514- }
1515-
15161529 private void tryFormat (List <String > l , Character quote , Character escape , String expected ) throws IOException {
15171530 CSVFormat format = CSVFormat .DEFAULT .withQuote (quote ).withEscape (escape ).withRecordSeparator (null );
15181531 Appendable out = new StringBuilder ();
@@ -1521,17 +1534,4 @@ private void tryFormat(List<String> l, Character quote, Character escape, String
15211534 printer .close ();
15221535 assertEquals (expected , out .toString ());
15231536 }
1524-
1525- @ Test
1526- public void testCSV135 () throws IOException {
1527- List <String > l = new LinkedList <String >();
1528- l .add ("\" \" " ); // ""
1529- l .add ("\\ \\ " ); // \\
1530- l .add ("\\ \" \\ " ); // \"\
1531- tryFormat (l , null , null , "\" \" ,\\ \\ ,\\ \" \\ " ); // "",\\,\"\ (unchanged)
1532- tryFormat (l , '"' , null , "\" \" \" \" \" \" ,\\ \\ ,\" \\ \" \" \\ \" " ); // """""",\\,"\""\" (quoted, and embedded DQ doubled)
1533- tryFormat (l , null , '\\' , "\" \" ,\\ \\ \\ \\ ,\\ \\ \" \\ \\ " ); // "",\\\\,\\"\\ (escapes escaped, not quoted)
1534- tryFormat (l , '"' , '\\' , "\" \\ \" \\ \" \" ,\" \\ \\ \\ \\ \" ,\" \\ \\ \\ \" \\ \\ \" " ); // "\"\"","\\\\","\\\"\\" (quoted, and embedded DQ & escape escaped)
1535- tryFormat (l , '"' , '"' , "\" \" \" \" \" \" ,\\ \\ ,\" \\ \" \" \\ \" " ); // """""",\\,"\""\" (quoted, embedded DQ escaped)
1536- }
15371537}
0 commit comments