From 83d6f8117a5634a36e56a2cf6795e4b604c2ee4c Mon Sep 17 00:00:00 2001 From: Chen <50514813+dota17@users.noreply.github.com> Date: Mon, 23 Mar 2020 23:09:09 +0800 Subject: [PATCH] improve CSVFormat test coverage (#63) * improve CSVFormat test coverage * remove print in test --- .../org/apache/commons/csv/CSVFormatTest.java | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/test/java/org/apache/commons/csv/CSVFormatTest.java b/src/test/java/org/apache/commons/csv/CSVFormatTest.java index d7173a73..3db28fcd 100644 --- a/src/test/java/org/apache/commons/csv/CSVFormatTest.java +++ b/src/test/java/org/apache/commons/csv/CSVFormatTest.java @@ -36,6 +36,9 @@ import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; +import java.io.Reader; +import java.io.IOException; +import java.io.StringReader; import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.util.Arrays; @@ -1175,4 +1178,31 @@ public class CSVFormatTest { assertEquals(System.getProperty("line.separator"), formatWithRecordSeparator.getRecordSeparator()); } + @Test + public void testPrintWithEscapesEndWithCRLF() throws IOException { + final Reader in = new StringReader("x,y,x\r\na,?b,c\r\n"); + final Appendable out = new StringBuilder(); + final CSVFormat format = CSVFormat.RFC4180.withEscape('?').withDelimiter(',').withQuote(null).withRecordSeparator(CRLF); + format.print(in,out,true); + assertEquals("x?,y?,x?r?na?,??b?,c?r?n", out.toString()); + } + + @Test + public void testPrintWithEscapesEndWithoutCRLF() throws IOException { + final Reader in = new StringReader("x,y,x"); + final Appendable out = new StringBuilder(); + final CSVFormat format = CSVFormat.RFC4180.withEscape('?').withDelimiter(',').withQuote(null).withRecordSeparator(CRLF); + format.print(in,out,true); + assertEquals("x?,y?,x", out.toString()); + } + + @Test + public void testFormatToString() throws IOException { + final CSVFormat format = CSVFormat.RFC4180.withEscape('?').withDelimiter(',') + .withQuoteMode(QuoteMode.MINIMAL).withRecordSeparator(CRLF).withQuote('"') + .withNullString("").withIgnoreHeaderCase(true) + .withHeaderComments("This is HeaderComments").withHeader("col1","col2","col3"); + assertEquals("Delimiter=<,> Escape= QuoteChar=<\"> QuoteMode= NullString=<> RecordSeparator=<" +CRLF+ + "> IgnoreHeaderCase:ignored SkipHeaderRecord:false HeaderComments:[This is HeaderComments] Header:[col1, col2, col3]", format.toString()); + } }