From da4d2e5c2d38e7384a0dbff4739027d034384ab6 Mon Sep 17 00:00:00 2001 From: Gary Gregory Date: Mon, 5 Jul 2021 08:59:59 -0400 Subject: [PATCH] Add and use CSVFormat.Builder, deprecated CSVFormat#with methods, based on #73. Better test method names and use @SuppressWarnings("deprecation"). --- .../org/apache/commons/csv/CSVFormatTest.java | 511 +++++++++--------- 1 file changed, 268 insertions(+), 243 deletions(-) diff --git a/src/test/java/org/apache/commons/csv/CSVFormatTest.java b/src/test/java/org/apache/commons/csv/CSVFormatTest.java index 3e5d7342..400cc072 100644 --- a/src/test/java/org/apache/commons/csv/CSVFormatTest.java +++ b/src/test/java/org/apache/commons/csv/CSVFormatTest.java @@ -78,8 +78,9 @@ public class CSVFormatTest { } } + @SuppressWarnings("deprecation") @Test - public void testDelimiterSameAsCommentStartThrowsException0() { + public void testDelimiterSameAsCommentStartThrowsException_Deprecated() { assertThrows(IllegalArgumentException.class, () -> CSVFormat.DEFAULT.withDelimiter('!').withCommentMarker('!')); } @@ -88,8 +89,9 @@ public class CSVFormatTest { assertThrows(IllegalArgumentException.class, () -> CSVFormat.DEFAULT.builder().setDelimiter('!').setCommentMarker('!').build()); } + @SuppressWarnings("deprecation") @Test - public void testDelimiterSameAsEscapeThrowsException0() { + public void testDelimiterSameAsEscapeThrowsException_Deprecated() { assertThrows(IllegalArgumentException.class, () -> CSVFormat.DEFAULT.withDelimiter('!').withEscape('!')); } @@ -99,11 +101,8 @@ public class CSVFormatTest { } @Test - public void testDuplicateHeaderElements0() { - final String[] header = { "A", "A" }; - final CSVFormat format = CSVFormat.DEFAULT.withHeader(header); - assertEquals(2, format.getHeader().length); - assertArrayEquals(header, format.getHeader()); + public void testDelimiterSameAsRecordSeparatorThrowsException() { + assertThrows(IllegalArgumentException.class, () -> CSVFormat.newFormat(CR)); } @Test @@ -114,11 +113,13 @@ public class CSVFormatTest { assertArrayEquals(header, format.getHeader()); } + @SuppressWarnings("deprecation") @Test - public void testDuplicateHeaderElementsFalse0() { - assertThrows( - IllegalArgumentException.class, - () -> CSVFormat.DEFAULT.withAllowDuplicateHeaderNames(false).withHeader("A", "A")); + public void testDuplicateHeaderElements_Deprecated() { + final String[] header = { "A", "A" }; + final CSVFormat format = CSVFormat.DEFAULT.withHeader(header); + assertEquals(2, format.getHeader().length); + assertArrayEquals(header, format.getHeader()); } @Test @@ -128,14 +129,23 @@ public class CSVFormatTest { () -> CSVFormat.DEFAULT.builder().setAllowDuplicateHeaderNames(false).setHeader("A", "A").build()); } - public void testDuplicateHeaderElementsTrue0() { - CSVFormat.DEFAULT.withAllowDuplicateHeaderNames(true).withHeader("A", "A"); + @SuppressWarnings("deprecation") + @Test + public void testDuplicateHeaderElementsFalse_Deprecated() { + assertThrows( + IllegalArgumentException.class, + () -> CSVFormat.DEFAULT.withAllowDuplicateHeaderNames(false).withHeader("A", "A")); } public void testDuplicateHeaderElementsTrue() { CSVFormat.DEFAULT.builder().setAllowDuplicateHeaderNames(true).setHeader("A", "A").build(); } + @SuppressWarnings("deprecation") + public void testDuplicateHeaderElementsTrue_Deprecated() { + CSVFormat.DEFAULT.withAllowDuplicateHeaderNames(true).withHeader("A", "A"); + } + @Test public void testEquals() { final CSVFormat right = CSVFormat.DEFAULT; @@ -152,18 +162,6 @@ public class CSVFormatTest { assertEquals(right.hashCode(), left.hashCode()); } - @Test - public void testEqualsCommentStart0() { - final CSVFormat right = CSVFormat.newFormat('\'') - .withQuote('"') - .withCommentMarker('#') - .withQuoteMode(QuoteMode.ALL); - final CSVFormat left = right - .withCommentMarker('!'); - - assertNotEquals(right, left); - } - @Test public void testEqualsCommentStart() { final CSVFormat right = CSVFormat.newFormat('\'').builder() @@ -178,23 +176,23 @@ public class CSVFormatTest { assertNotEquals(right, left); } + @SuppressWarnings("deprecation") @Test - public void testEqualsDelimiter() { - final CSVFormat right = CSVFormat.newFormat('!'); - final CSVFormat left = CSVFormat.newFormat('?'); + public void testEqualsCommentStart_Deprecated() { + final CSVFormat right = CSVFormat.newFormat('\'') + .withQuote('"') + .withCommentMarker('#') + .withQuoteMode(QuoteMode.ALL); + final CSVFormat left = right + .withCommentMarker('!'); assertNotEquals(right, left); } @Test - public void testEqualsEscape0() { - final CSVFormat right = CSVFormat.newFormat('\'') - .withQuote('"') - .withCommentMarker('#') - .withEscape('+') - .withQuoteMode(QuoteMode.ALL); - final CSVFormat left = right - .withEscape('!'); + public void testEqualsDelimiter() { + final CSVFormat right = CSVFormat.newFormat('!'); + final CSVFormat left = CSVFormat.newFormat('?'); assertNotEquals(right, left); } @@ -214,6 +212,20 @@ public class CSVFormatTest { assertNotEquals(right, left); } + @SuppressWarnings("deprecation") + @Test + public void testEqualsEscape_Deprecated() { + final CSVFormat right = CSVFormat.newFormat('\'') + .withQuote('"') + .withCommentMarker('#') + .withEscape('+') + .withQuoteMode(QuoteMode.ALL); + final CSVFormat left = right + .withEscape('!'); + + assertNotEquals(right, left); + } + @Test public void testEqualsHash() throws Exception { final Method[] methods = CSVFormat.class.getDeclaredMethods(); @@ -262,23 +274,6 @@ public class CSVFormatTest { } } - @Test - public void testEqualsHeader0() { - final CSVFormat right = CSVFormat.newFormat('\'') - .withRecordSeparator(CR) - .withCommentMarker('#') - .withEscape('+') - .withHeader("One", "Two", "Three") - .withIgnoreEmptyLines() - .withIgnoreSurroundingSpaces() - .withQuote('"') - .withQuoteMode(QuoteMode.ALL); - final CSVFormat left = right - .withHeader("Three", "Two", "One"); - - assertNotEquals(right, left); - } - @Test public void testEqualsHeader() { final CSVFormat right = CSVFormat.newFormat('\'').builder() @@ -298,17 +293,20 @@ public class CSVFormatTest { assertNotEquals(right, left); } + @SuppressWarnings("deprecation") @Test - public void testEqualsIgnoreEmptyLines0() { + public void testEqualsHeader_Deprecated() { final CSVFormat right = CSVFormat.newFormat('\'') + .withRecordSeparator(CR) .withCommentMarker('#') .withEscape('+') + .withHeader("One", "Two", "Three") .withIgnoreEmptyLines() .withIgnoreSurroundingSpaces() .withQuote('"') .withQuoteMode(QuoteMode.ALL); final CSVFormat left = right - .withIgnoreEmptyLines(false); + .withHeader("Three", "Two", "One"); assertNotEquals(right, left); } @@ -330,16 +328,18 @@ public class CSVFormatTest { assertNotEquals(right, left); } + @SuppressWarnings("deprecation") @Test - public void testEqualsIgnoreSurroundingSpaces0() { + public void testEqualsIgnoreEmptyLines_Deprecated() { final CSVFormat right = CSVFormat.newFormat('\'') .withCommentMarker('#') .withEscape('+') + .withIgnoreEmptyLines() .withIgnoreSurroundingSpaces() .withQuote('"') .withQuoteMode(QuoteMode.ALL); final CSVFormat left = right - .withIgnoreSurroundingSpaces(false); + .withIgnoreEmptyLines(false); assertNotEquals(right, left); } @@ -360,12 +360,19 @@ public class CSVFormatTest { assertNotEquals(right, left); } + @SuppressWarnings("deprecation") @Test - public void testEqualsLeftNoQuoteRightQuote0() { - final CSVFormat left = CSVFormat.newFormat(',').withQuote(null); - final CSVFormat right = left.withQuote('#'); + public void testEqualsIgnoreSurroundingSpaces_Deprecated() { + final CSVFormat right = CSVFormat.newFormat('\'') + .withCommentMarker('#') + .withEscape('+') + .withIgnoreSurroundingSpaces() + .withQuote('"') + .withQuoteMode(QuoteMode.ALL); + final CSVFormat left = right + .withIgnoreSurroundingSpaces(false); - assertNotEquals(left, right); + assertNotEquals(right, left); } @Test @@ -376,12 +383,13 @@ public class CSVFormatTest { assertNotEquals(left, right); } + @SuppressWarnings("deprecation") @Test - public void testEqualsNoQuotes0() { + public void testEqualsLeftNoQuoteRightQuote_Deprecated() { final CSVFormat left = CSVFormat.newFormat(',').withQuote(null); - final CSVFormat right = left.withQuote(null); + final CSVFormat right = left.withQuote('#'); - assertEquals(left, right); + assertNotEquals(left, right); } @Test @@ -392,21 +400,13 @@ public class CSVFormatTest { assertEquals(left, right); } + @SuppressWarnings("deprecation") @Test - public void testEqualsNullString0() { - final CSVFormat right = CSVFormat.newFormat('\'') - .withRecordSeparator(CR) - .withCommentMarker('#') - .withEscape('+') - .withIgnoreEmptyLines() - .withIgnoreSurroundingSpaces() - .withQuote('"') - .withQuoteMode(QuoteMode.ALL) - .withNullString("null"); - final CSVFormat left = right - .withNullString("---"); + public void testEqualsNoQuotes_Deprecated() { + final CSVFormat left = CSVFormat.newFormat(',').withQuote(null); + final CSVFormat right = left.withQuote(null); - assertNotEquals(right, left); + assertEquals(left, right); } @Test @@ -428,6 +428,24 @@ public class CSVFormatTest { assertNotEquals(right, left); } + @SuppressWarnings("deprecation") + @Test + public void testEqualsNullString_Deprecated() { + final CSVFormat right = CSVFormat.newFormat('\'') + .withRecordSeparator(CR) + .withCommentMarker('#') + .withEscape('+') + .withIgnoreEmptyLines() + .withIgnoreSurroundingSpaces() + .withQuote('"') + .withQuoteMode(QuoteMode.ALL) + .withNullString("null"); + final CSVFormat left = right + .withNullString("---"); + + assertNotEquals(right, left); + } + @Test public void testEqualsOne() { @@ -557,14 +575,6 @@ public class CSVFormatTest { } - @Test - public void testEqualsQuoteChar0() { - final CSVFormat right = CSVFormat.newFormat('\'').withQuote('"'); - final CSVFormat left = right.withQuote('!'); - - assertNotEquals(right, left); - } - @Test public void testEqualsQuoteChar() { final CSVFormat right = CSVFormat.newFormat('\'').builder().setQuote('"').build(); @@ -573,13 +583,11 @@ public class CSVFormatTest { assertNotEquals(right, left); } + @SuppressWarnings("deprecation") @Test - public void testEqualsQuotePolicy0() { - final CSVFormat right = CSVFormat.newFormat('\'') - .withQuote('"') - .withQuoteMode(QuoteMode.ALL); - final CSVFormat left = right - .withQuoteMode(QuoteMode.MINIMAL); + public void testEqualsQuoteChar_Deprecated() { + final CSVFormat right = CSVFormat.newFormat('\'').withQuote('"'); + final CSVFormat left = right.withQuote('!'); assertNotEquals(right, left); } @@ -597,18 +605,14 @@ public class CSVFormatTest { assertNotEquals(right, left); } + @SuppressWarnings("deprecation") @Test - public void testEqualsRecordSeparator0() { + public void testEqualsQuotePolicy_Deprecated() { final CSVFormat right = CSVFormat.newFormat('\'') - .withRecordSeparator(CR) - .withCommentMarker('#') - .withEscape('+') - .withIgnoreEmptyLines() - .withIgnoreSurroundingSpaces() .withQuote('"') .withQuoteMode(QuoteMode.ALL); final CSVFormat left = right - .withRecordSeparator(LF); + .withQuoteMode(QuoteMode.MINIMAL); assertNotEquals(right, left); } @@ -631,8 +635,9 @@ public class CSVFormatTest { assertNotEquals(right, left); } + @SuppressWarnings("deprecation") @Test - public void testEqualsSkipHeaderRecord0() { + public void testEqualsRecordSeparator_Deprecated() { final CSVFormat right = CSVFormat.newFormat('\'') .withRecordSeparator(CR) .withCommentMarker('#') @@ -640,11 +645,9 @@ public class CSVFormatTest { .withIgnoreEmptyLines() .withIgnoreSurroundingSpaces() .withQuote('"') - .withQuoteMode(QuoteMode.ALL) - .withNullString("null") - .withSkipHeaderRecord(); + .withQuoteMode(QuoteMode.ALL); final CSVFormat left = right - .withSkipHeaderRecord(false); + .withRecordSeparator(LF); assertNotEquals(right, left); } @@ -668,6 +671,25 @@ public class CSVFormatTest { assertNotEquals(right, left); } + @SuppressWarnings("deprecation") + @Test + public void testEqualsSkipHeaderRecord_Deprecated() { + final CSVFormat right = CSVFormat.newFormat('\'') + .withRecordSeparator(CR) + .withCommentMarker('#') + .withEscape('+') + .withIgnoreEmptyLines() + .withIgnoreSurroundingSpaces() + .withQuote('"') + .withQuoteMode(QuoteMode.ALL) + .withNullString("null") + .withSkipHeaderRecord(); + final CSVFormat left = right + .withSkipHeaderRecord(false); + + assertNotEquals(right, left); + } + @Test public void testEqualsWithNull() { @@ -731,22 +753,15 @@ public class CSVFormatTest { } - @Test - public void testEscapeSameAsCommentStartThrowsException0() { - assertThrows(IllegalArgumentException.class, () -> CSVFormat.DEFAULT.withEscape('!').withCommentMarker('!')); - } - @Test public void testEscapeSameAsCommentStartThrowsException() { assertThrows(IllegalArgumentException.class, () -> CSVFormat.DEFAULT.builder().setEscape('!').setCommentMarker('!').build()); } + @SuppressWarnings("deprecation") @Test - public void testEscapeSameAsCommentStartThrowsExceptionForWrapperType0() { - // Cannot assume that callers won't use different Character objects - assertThrows( - IllegalArgumentException.class, - () -> CSVFormat.DEFAULT.withEscape(Character.valueOf('!')).withCommentMarker(Character.valueOf('!'))); + public void testEscapeSameAsCommentStartThrowsException_Deprecated() { + assertThrows(IllegalArgumentException.class, () -> CSVFormat.DEFAULT.withEscape('!').withCommentMarker('!')); } @Test @@ -757,6 +772,15 @@ public class CSVFormatTest { () -> CSVFormat.DEFAULT.builder().setEscape(Character.valueOf('!')).setCommentMarker(Character.valueOf('!')).build()); } + @SuppressWarnings("deprecation") + @Test + public void testEscapeSameAsCommentStartThrowsExceptionForWrapperType_Deprecated() { + // Cannot assume that callers won't use different Character objects + assertThrows( + IllegalArgumentException.class, + () -> CSVFormat.DEFAULT.withEscape(Character.valueOf('!')).withCommentMarker(Character.valueOf('!'))); + } + @Test public void testFormat() { final CSVFormat format = CSVFormat.DEFAULT; @@ -775,6 +799,16 @@ public class CSVFormatTest { assertEquals(Objects.class.getName(), e.getStackTrace()[0].getClassName()); } + @Test + public void testFormatToString() { + 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()); + } + @Test public void testGetHeader() { final String[] header = {"one", "two", "three"}; @@ -806,16 +840,17 @@ public class CSVFormatTest { } - @Test - public void testJiraCsv236_0() { - CSVFormat.DEFAULT.withAllowDuplicateHeaderNames().withHeader("CC","VV","VV"); - } - @Test public void testJiraCsv236() { CSVFormat.DEFAULT.builder().setAllowDuplicateHeaderNames(true).setHeader("CC","VV","VV").build(); } + @SuppressWarnings("deprecation") + @Test + public void testJiraCsv236__Deprecated() { + CSVFormat.DEFAULT.withAllowDuplicateHeaderNames().withHeader("CC","VV","VV"); + } + @Test public void testNewFormat() { @@ -877,14 +912,6 @@ public class CSVFormatTest { } - @Test - public void testNullRecordSeparatorCsv106_0() { - final CSVFormat format = CSVFormat.newFormat(';').withSkipHeaderRecord().withHeader("H1", "H2"); - final String formatStr = format.format("A", "B"); - assertNotNull(formatStr); - assertFalse(formatStr.endsWith("null")); - } - @Test public void testNullRecordSeparatorCsv106() { final CSVFormat format = CSVFormat.newFormat(';').builder().setSkipHeaderRecord(true).setHeader("H1", "H2").build(); @@ -893,9 +920,58 @@ public class CSVFormatTest { assertFalse(formatStr.endsWith("null")); } + @SuppressWarnings("deprecation") @Test - public void testQuoteCharSameAsCommentStartThrowsException0() { - assertThrows(IllegalArgumentException.class, () -> CSVFormat.DEFAULT.withQuote('!').withCommentMarker('!')); + public void testNullRecordSeparatorCsv106__Deprecated() { + final CSVFormat format = CSVFormat.newFormat(';').withSkipHeaderRecord().withHeader("H1", "H2"); + final String formatStr = format.format("A", "B"); + assertNotNull(formatStr); + assertFalse(formatStr.endsWith("null")); + } + + @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 testPrintWithoutQuotes() throws IOException { + final Reader in = new StringReader(""); + final Appendable out = new StringBuilder(); + final CSVFormat format = CSVFormat.RFC4180.withDelimiter(',').withQuote('"').withEscape('?').withQuoteMode(QuoteMode.NON_NUMERIC); + format.print(in, out, true); + assertEquals("\"\"", out.toString()); + } + + @Test + public void testPrintWithQuoteModeIsNONE() throws IOException { + final Reader in = new StringReader("a,b,c"); + final Appendable out = new StringBuilder(); + final CSVFormat format = CSVFormat.RFC4180.withDelimiter(',').withQuote('"').withEscape('?').withQuoteMode(QuoteMode.NONE); + format.print(in, out, true); + assertEquals("a?,b?,c", out.toString()); + } + + @Test + public void testPrintWithQuotes() throws IOException { + final Reader in = new StringReader("\"a,b,c\r\nx,y,z"); + final Appendable out = new StringBuilder(); + final CSVFormat format = CSVFormat.RFC4180.withDelimiter(',').withQuote('"').withEscape('?').withQuoteMode(QuoteMode.NON_NUMERIC); + format.print(in, out, true); + assertEquals("\"\"\"\"a,b,c\r\nx,y,z\"", out.toString()); } @Test @@ -903,12 +979,10 @@ public class CSVFormatTest { assertThrows(IllegalArgumentException.class, () -> CSVFormat.DEFAULT.builder().setQuote('!').setCommentMarker('!').build()); } + @SuppressWarnings("deprecation") @Test - public void testQuoteCharSameAsCommentStartThrowsExceptionForWrapperType0() { - // Cannot assume that callers won't use different Character objects - assertThrows( - IllegalArgumentException.class, - () -> CSVFormat.DEFAULT.builder().setQuote(Character.valueOf('!')).setCommentMarker('!').build()); + public void testQuoteCharSameAsCommentStartThrowsException_Deprecated() { + assertThrows(IllegalArgumentException.class, () -> CSVFormat.DEFAULT.withQuote('!').withCommentMarker('!')); } @Test @@ -916,12 +990,16 @@ public class CSVFormatTest { // Cannot assume that callers won't use different Character objects assertThrows( IllegalArgumentException.class, - () -> CSVFormat.DEFAULT.withQuote(Character.valueOf('!')).withCommentMarker('!')); + () -> CSVFormat.DEFAULT.builder().setQuote(Character.valueOf('!')).setCommentMarker('!').build()); } + @SuppressWarnings("deprecation") @Test - public void testQuoteCharSameAsDelimiterThrowsException0() { - assertThrows(IllegalArgumentException.class, () -> CSVFormat.DEFAULT.withQuote('!').withDelimiter('!')); + public void testQuoteCharSameAsCommentStartThrowsExceptionForWrapperType_Deprecated() { + // Cannot assume that callers won't use different Character objects + assertThrows( + IllegalArgumentException.class, + () -> CSVFormat.DEFAULT.withQuote(Character.valueOf('!')).withCommentMarker('!')); } @Test @@ -929,9 +1007,10 @@ public class CSVFormatTest { assertThrows(IllegalArgumentException.class, () -> CSVFormat.DEFAULT.builder().setQuote('!').setDelimiter('!').build()); } + @SuppressWarnings("deprecation") @Test - public void testQuotePolicyNoneWithoutEscapeThrowsException0() { - assertThrows(IllegalArgumentException.class, () -> CSVFormat.newFormat('!').withQuoteMode(QuoteMode.NONE)); + public void testQuoteCharSameAsDelimiterThrowsException_Deprecated() { + assertThrows(IllegalArgumentException.class, () -> CSVFormat.DEFAULT.withQuote('!').withDelimiter('!')); } @Test @@ -939,6 +1018,12 @@ public class CSVFormatTest { assertThrows(IllegalArgumentException.class, () -> CSVFormat.newFormat('!').builder().setQuoteMode(QuoteMode.NONE).build()); } + @SuppressWarnings("deprecation") + @Test + public void testQuotePolicyNoneWithoutEscapeThrowsException_Deprecated() { + assertThrows(IllegalArgumentException.class, () -> CSVFormat.newFormat('!').withQuoteMode(QuoteMode.NONE)); + } + @Test public void testRFC4180() { assertEquals(null, RFC4180.getCommentMarker()); @@ -1144,45 +1229,77 @@ public class CSVFormatTest { } + @Test + public void testTrim() throws IOException { + final CSVFormat formatWithTrim = CSVFormat.DEFAULT.withDelimiter(',').withTrim().withQuote(null).withRecordSeparator(CRLF); + + CharSequence in = "a,b,c"; + final StringBuilder out = new StringBuilder(); + formatWithTrim.print(in, out, true); + assertEquals("a,b,c", out.toString()); + + in = new StringBuilder(" x,y,z"); + out.setLength(0); + formatWithTrim.print(in, out, true); + assertEquals("x,y,z", out.toString()); + + in = new StringBuilder(""); + out.setLength(0); + formatWithTrim.print(in, out, true); + assertEquals("", out.toString()); + + in = new StringBuilder("header\r\n"); + out.setLength(0); + formatWithTrim.print(in, out, true); + assertEquals("header", out.toString()); + } + @Test public void testWithCommentStart() { final CSVFormat formatWithCommentStart = CSVFormat.DEFAULT.withCommentMarker('#'); assertEquals( Character.valueOf('#'), formatWithCommentStart.getCommentMarker()); } + @Test public void testWithCommentStartCRThrowsException() { assertThrows(IllegalArgumentException.class, () -> CSVFormat.DEFAULT.withCommentMarker(CR)); } + @Test public void testWithDelimiter() { final CSVFormat formatWithDelimiter = CSVFormat.DEFAULT.withDelimiter('!'); assertEquals('!', formatWithDelimiter.getDelimiter()); } + @Test public void testWithDelimiterLFThrowsException() { assertThrows(IllegalArgumentException.class, () -> CSVFormat.DEFAULT.withDelimiter(LF)); } + @Test public void testWithEmptyEnum() { final CSVFormat formatWithHeader = CSVFormat.DEFAULT.withHeader(EmptyEnum.class); assertTrue(formatWithHeader.getHeader().length == 0); } + @Test public void testWithEscape() { final CSVFormat formatWithEscape = CSVFormat.DEFAULT.withEscape('&'); assertEquals(Character.valueOf('&'), formatWithEscape.getEscapeCharacter()); } + @Test public void testWithEscapeCRThrowsExceptions() { assertThrows(IllegalArgumentException.class, () -> CSVFormat.DEFAULT.withEscape(CR)); } + @Test public void testWithFirstRecordAsHeader() { final CSVFormat formatWithFirstRecordAsHeader = CSVFormat.DEFAULT.withFirstRecordAsHeader(); @@ -1199,7 +1316,6 @@ public class CSVFormatTest { assertNotSame(header, formatWithHeader.getHeader()); } - @Test public void testWithHeaderComments() { @@ -1361,13 +1477,25 @@ public class CSVFormatTest { } - @Test public void testWithHeaderEnum() { final CSVFormat formatWithHeader = CSVFormat.DEFAULT.withHeader(Header.class); assertArrayEquals(new String[]{ "Name", "Email", "Phone" }, formatWithHeader.getHeader()); } + @Test + public void testWithHeaderEnumNull() { + final CSVFormat format = CSVFormat.DEFAULT; + final Class> simpleName = null; + format.withHeader(simpleName); + } + + @Test + public void testWithHeaderResultSetNull() throws SQLException { + final CSVFormat format = CSVFormat.DEFAULT; + final ResultSet resultSet = null; + format.withHeader(resultSet); + } @Test public void testWithIgnoreEmptyLines() { @@ -1375,28 +1503,24 @@ public class CSVFormatTest { assertTrue(CSVFormat.DEFAULT.withIgnoreEmptyLines().getIgnoreEmptyLines()); } - @Test public void testWithIgnoreSurround() { assertFalse(CSVFormat.DEFAULT.withIgnoreSurroundingSpaces(false).getIgnoreSurroundingSpaces()); assertTrue(CSVFormat.DEFAULT.withIgnoreSurroundingSpaces().getIgnoreSurroundingSpaces()); } - @Test public void testWithNullString() { final CSVFormat formatWithNullString = CSVFormat.DEFAULT.withNullString("null"); assertEquals("null", formatWithNullString.getNullString()); } - @Test public void testWithQuoteChar() { final CSVFormat formatWithQuoteChar = CSVFormat.DEFAULT.withQuote('"'); assertEquals(Character.valueOf('"'), formatWithQuoteChar.getQuoteCharacter()); } - @Test public void testWithQuoteLFThrowsException() { assertThrows(IllegalArgumentException.class, () -> CSVFormat.DEFAULT.withQuote(LF)); @@ -1431,103 +1555,4 @@ public class CSVFormatTest { final CSVFormat formatWithRecordSeparator = CSVFormat.DEFAULT.withSystemRecordSeparator(); 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() { - 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()); - } - - @Test - public void testDelimiterSameAsRecordSeparatorThrowsException() { - assertThrows(IllegalArgumentException.class, () -> CSVFormat.newFormat(CR)); - } - - @Test - public void testWithHeaderEnumNull() { - final CSVFormat format = CSVFormat.DEFAULT; - final Class> simpleName = null; - format.withHeader(simpleName); - } - - @Test - public void testWithHeaderResultSetNull() throws SQLException { - final CSVFormat format = CSVFormat.DEFAULT; - final ResultSet resultSet = null; - format.withHeader(resultSet); - } - - @Test - public void testPrintWithQuoteModeIsNONE() throws IOException { - final Reader in = new StringReader("a,b,c"); - final Appendable out = new StringBuilder(); - final CSVFormat format = CSVFormat.RFC4180.withDelimiter(',').withQuote('"').withEscape('?').withQuoteMode(QuoteMode.NONE); - format.print(in, out, true); - assertEquals("a?,b?,c", out.toString()); - } - - @Test - public void testPrintWithQuotes() throws IOException { - final Reader in = new StringReader("\"a,b,c\r\nx,y,z"); - final Appendable out = new StringBuilder(); - final CSVFormat format = CSVFormat.RFC4180.withDelimiter(',').withQuote('"').withEscape('?').withQuoteMode(QuoteMode.NON_NUMERIC); - format.print(in, out, true); - assertEquals("\"\"\"\"a,b,c\r\nx,y,z\"", out.toString()); - } - - @Test - public void testPrintWithoutQuotes() throws IOException { - final Reader in = new StringReader(""); - final Appendable out = new StringBuilder(); - final CSVFormat format = CSVFormat.RFC4180.withDelimiter(',').withQuote('"').withEscape('?').withQuoteMode(QuoteMode.NON_NUMERIC); - format.print(in, out, true); - assertEquals("\"\"", out.toString()); - } - - @Test - public void testTrim() throws IOException { - final CSVFormat formatWithTrim = CSVFormat.DEFAULT.withDelimiter(',').withTrim().withQuote(null).withRecordSeparator(CRLF); - - CharSequence in = "a,b,c"; - final StringBuilder out = new StringBuilder(); - formatWithTrim.print(in, out, true); - assertEquals("a,b,c", out.toString()); - - in = new StringBuilder(" x,y,z"); - out.setLength(0); - formatWithTrim.print(in, out, true); - assertEquals("x,y,z", out.toString()); - - in = new StringBuilder(""); - out.setLength(0); - formatWithTrim.print(in, out, true); - assertEquals("", out.toString()); - - in = new StringBuilder("header\r\n"); - out.setLength(0); - formatWithTrim.print(in, out, true); - assertEquals("header", out.toString()); - } }