Add and use CSVFormat.Builder, deprecated CSVFormat#with methods, based

on #73.

Better test method names and use @SuppressWarnings("deprecation").
This commit is contained in:
Gary Gregory 2021-07-05 08:59:59 -04:00
parent 37872b84d6
commit da4d2e5c2d
1 changed files with 268 additions and 243 deletions

View File

@ -78,8 +78,9 @@ public class CSVFormatTest {
} }
} }
@SuppressWarnings("deprecation")
@Test @Test
public void testDelimiterSameAsCommentStartThrowsException0() { public void testDelimiterSameAsCommentStartThrowsException_Deprecated() {
assertThrows(IllegalArgumentException.class, () -> CSVFormat.DEFAULT.withDelimiter('!').withCommentMarker('!')); assertThrows(IllegalArgumentException.class, () -> CSVFormat.DEFAULT.withDelimiter('!').withCommentMarker('!'));
} }
@ -88,8 +89,9 @@ public class CSVFormatTest {
assertThrows(IllegalArgumentException.class, () -> CSVFormat.DEFAULT.builder().setDelimiter('!').setCommentMarker('!').build()); assertThrows(IllegalArgumentException.class, () -> CSVFormat.DEFAULT.builder().setDelimiter('!').setCommentMarker('!').build());
} }
@SuppressWarnings("deprecation")
@Test @Test
public void testDelimiterSameAsEscapeThrowsException0() { public void testDelimiterSameAsEscapeThrowsException_Deprecated() {
assertThrows(IllegalArgumentException.class, () -> CSVFormat.DEFAULT.withDelimiter('!').withEscape('!')); assertThrows(IllegalArgumentException.class, () -> CSVFormat.DEFAULT.withDelimiter('!').withEscape('!'));
} }
@ -99,11 +101,8 @@ public class CSVFormatTest {
} }
@Test @Test
public void testDuplicateHeaderElements0() { public void testDelimiterSameAsRecordSeparatorThrowsException() {
final String[] header = { "A", "A" }; assertThrows(IllegalArgumentException.class, () -> CSVFormat.newFormat(CR));
final CSVFormat format = CSVFormat.DEFAULT.withHeader(header);
assertEquals(2, format.getHeader().length);
assertArrayEquals(header, format.getHeader());
} }
@Test @Test
@ -114,11 +113,13 @@ public class CSVFormatTest {
assertArrayEquals(header, format.getHeader()); assertArrayEquals(header, format.getHeader());
} }
@SuppressWarnings("deprecation")
@Test @Test
public void testDuplicateHeaderElementsFalse0() { public void testDuplicateHeaderElements_Deprecated() {
assertThrows( final String[] header = { "A", "A" };
IllegalArgumentException.class, final CSVFormat format = CSVFormat.DEFAULT.withHeader(header);
() -> CSVFormat.DEFAULT.withAllowDuplicateHeaderNames(false).withHeader("A", "A")); assertEquals(2, format.getHeader().length);
assertArrayEquals(header, format.getHeader());
} }
@Test @Test
@ -128,14 +129,23 @@ public class CSVFormatTest {
() -> CSVFormat.DEFAULT.builder().setAllowDuplicateHeaderNames(false).setHeader("A", "A").build()); () -> CSVFormat.DEFAULT.builder().setAllowDuplicateHeaderNames(false).setHeader("A", "A").build());
} }
public void testDuplicateHeaderElementsTrue0() { @SuppressWarnings("deprecation")
CSVFormat.DEFAULT.withAllowDuplicateHeaderNames(true).withHeader("A", "A"); @Test
public void testDuplicateHeaderElementsFalse_Deprecated() {
assertThrows(
IllegalArgumentException.class,
() -> CSVFormat.DEFAULT.withAllowDuplicateHeaderNames(false).withHeader("A", "A"));
} }
public void testDuplicateHeaderElementsTrue() { public void testDuplicateHeaderElementsTrue() {
CSVFormat.DEFAULT.builder().setAllowDuplicateHeaderNames(true).setHeader("A", "A").build(); CSVFormat.DEFAULT.builder().setAllowDuplicateHeaderNames(true).setHeader("A", "A").build();
} }
@SuppressWarnings("deprecation")
public void testDuplicateHeaderElementsTrue_Deprecated() {
CSVFormat.DEFAULT.withAllowDuplicateHeaderNames(true).withHeader("A", "A");
}
@Test @Test
public void testEquals() { public void testEquals() {
final CSVFormat right = CSVFormat.DEFAULT; final CSVFormat right = CSVFormat.DEFAULT;
@ -152,18 +162,6 @@ public class CSVFormatTest {
assertEquals(right.hashCode(), left.hashCode()); 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 @Test
public void testEqualsCommentStart() { public void testEqualsCommentStart() {
final CSVFormat right = CSVFormat.newFormat('\'').builder() final CSVFormat right = CSVFormat.newFormat('\'').builder()
@ -178,23 +176,23 @@ public class CSVFormatTest {
assertNotEquals(right, left); assertNotEquals(right, left);
} }
@SuppressWarnings("deprecation")
@Test @Test
public void testEqualsDelimiter() { public void testEqualsCommentStart_Deprecated() {
final CSVFormat right = CSVFormat.newFormat('!'); final CSVFormat right = CSVFormat.newFormat('\'')
final CSVFormat left = CSVFormat.newFormat('?'); .withQuote('"')
.withCommentMarker('#')
.withQuoteMode(QuoteMode.ALL);
final CSVFormat left = right
.withCommentMarker('!');
assertNotEquals(right, left); assertNotEquals(right, left);
} }
@Test @Test
public void testEqualsEscape0() { public void testEqualsDelimiter() {
final CSVFormat right = CSVFormat.newFormat('\'') final CSVFormat right = CSVFormat.newFormat('!');
.withQuote('"') final CSVFormat left = CSVFormat.newFormat('?');
.withCommentMarker('#')
.withEscape('+')
.withQuoteMode(QuoteMode.ALL);
final CSVFormat left = right
.withEscape('!');
assertNotEquals(right, left); assertNotEquals(right, left);
} }
@ -214,6 +212,20 @@ public class CSVFormatTest {
assertNotEquals(right, left); 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 @Test
public void testEqualsHash() throws Exception { public void testEqualsHash() throws Exception {
final Method[] methods = CSVFormat.class.getDeclaredMethods(); 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 @Test
public void testEqualsHeader() { public void testEqualsHeader() {
final CSVFormat right = CSVFormat.newFormat('\'').builder() final CSVFormat right = CSVFormat.newFormat('\'').builder()
@ -298,17 +293,20 @@ public class CSVFormatTest {
assertNotEquals(right, left); assertNotEquals(right, left);
} }
@SuppressWarnings("deprecation")
@Test @Test
public void testEqualsIgnoreEmptyLines0() { public void testEqualsHeader_Deprecated() {
final CSVFormat right = CSVFormat.newFormat('\'') final CSVFormat right = CSVFormat.newFormat('\'')
.withRecordSeparator(CR)
.withCommentMarker('#') .withCommentMarker('#')
.withEscape('+') .withEscape('+')
.withHeader("One", "Two", "Three")
.withIgnoreEmptyLines() .withIgnoreEmptyLines()
.withIgnoreSurroundingSpaces() .withIgnoreSurroundingSpaces()
.withQuote('"') .withQuote('"')
.withQuoteMode(QuoteMode.ALL); .withQuoteMode(QuoteMode.ALL);
final CSVFormat left = right final CSVFormat left = right
.withIgnoreEmptyLines(false); .withHeader("Three", "Two", "One");
assertNotEquals(right, left); assertNotEquals(right, left);
} }
@ -330,16 +328,18 @@ public class CSVFormatTest {
assertNotEquals(right, left); assertNotEquals(right, left);
} }
@SuppressWarnings("deprecation")
@Test @Test
public void testEqualsIgnoreSurroundingSpaces0() { public void testEqualsIgnoreEmptyLines_Deprecated() {
final CSVFormat right = CSVFormat.newFormat('\'') final CSVFormat right = CSVFormat.newFormat('\'')
.withCommentMarker('#') .withCommentMarker('#')
.withEscape('+') .withEscape('+')
.withIgnoreEmptyLines()
.withIgnoreSurroundingSpaces() .withIgnoreSurroundingSpaces()
.withQuote('"') .withQuote('"')
.withQuoteMode(QuoteMode.ALL); .withQuoteMode(QuoteMode.ALL);
final CSVFormat left = right final CSVFormat left = right
.withIgnoreSurroundingSpaces(false); .withIgnoreEmptyLines(false);
assertNotEquals(right, left); assertNotEquals(right, left);
} }
@ -360,12 +360,19 @@ public class CSVFormatTest {
assertNotEquals(right, left); assertNotEquals(right, left);
} }
@SuppressWarnings("deprecation")
@Test @Test
public void testEqualsLeftNoQuoteRightQuote0() { public void testEqualsIgnoreSurroundingSpaces_Deprecated() {
final CSVFormat left = CSVFormat.newFormat(',').withQuote(null); final CSVFormat right = CSVFormat.newFormat('\'')
final CSVFormat right = left.withQuote('#'); .withCommentMarker('#')
.withEscape('+')
.withIgnoreSurroundingSpaces()
.withQuote('"')
.withQuoteMode(QuoteMode.ALL);
final CSVFormat left = right
.withIgnoreSurroundingSpaces(false);
assertNotEquals(left, right); assertNotEquals(right, left);
} }
@Test @Test
@ -376,12 +383,13 @@ public class CSVFormatTest {
assertNotEquals(left, right); assertNotEquals(left, right);
} }
@SuppressWarnings("deprecation")
@Test @Test
public void testEqualsNoQuotes0() { public void testEqualsLeftNoQuoteRightQuote_Deprecated() {
final CSVFormat left = CSVFormat.newFormat(',').withQuote(null); 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 @Test
@ -392,21 +400,13 @@ public class CSVFormatTest {
assertEquals(left, right); assertEquals(left, right);
} }
@SuppressWarnings("deprecation")
@Test @Test
public void testEqualsNullString0() { public void testEqualsNoQuotes_Deprecated() {
final CSVFormat right = CSVFormat.newFormat('\'') final CSVFormat left = CSVFormat.newFormat(',').withQuote(null);
.withRecordSeparator(CR) final CSVFormat right = left.withQuote(null);
.withCommentMarker('#')
.withEscape('+')
.withIgnoreEmptyLines()
.withIgnoreSurroundingSpaces()
.withQuote('"')
.withQuoteMode(QuoteMode.ALL)
.withNullString("null");
final CSVFormat left = right
.withNullString("---");
assertNotEquals(right, left); assertEquals(left, right);
} }
@Test @Test
@ -428,6 +428,24 @@ public class CSVFormatTest {
assertNotEquals(right, left); 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 @Test
public void testEqualsOne() { 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 @Test
public void testEqualsQuoteChar() { public void testEqualsQuoteChar() {
final CSVFormat right = CSVFormat.newFormat('\'').builder().setQuote('"').build(); final CSVFormat right = CSVFormat.newFormat('\'').builder().setQuote('"').build();
@ -573,13 +583,11 @@ public class CSVFormatTest {
assertNotEquals(right, left); assertNotEquals(right, left);
} }
@SuppressWarnings("deprecation")
@Test @Test
public void testEqualsQuotePolicy0() { public void testEqualsQuoteChar_Deprecated() {
final CSVFormat right = CSVFormat.newFormat('\'') final CSVFormat right = CSVFormat.newFormat('\'').withQuote('"');
.withQuote('"') final CSVFormat left = right.withQuote('!');
.withQuoteMode(QuoteMode.ALL);
final CSVFormat left = right
.withQuoteMode(QuoteMode.MINIMAL);
assertNotEquals(right, left); assertNotEquals(right, left);
} }
@ -597,18 +605,14 @@ public class CSVFormatTest {
assertNotEquals(right, left); assertNotEquals(right, left);
} }
@SuppressWarnings("deprecation")
@Test @Test
public void testEqualsRecordSeparator0() { public void testEqualsQuotePolicy_Deprecated() {
final CSVFormat right = CSVFormat.newFormat('\'') final CSVFormat right = CSVFormat.newFormat('\'')
.withRecordSeparator(CR)
.withCommentMarker('#')
.withEscape('+')
.withIgnoreEmptyLines()
.withIgnoreSurroundingSpaces()
.withQuote('"') .withQuote('"')
.withQuoteMode(QuoteMode.ALL); .withQuoteMode(QuoteMode.ALL);
final CSVFormat left = right final CSVFormat left = right
.withRecordSeparator(LF); .withQuoteMode(QuoteMode.MINIMAL);
assertNotEquals(right, left); assertNotEquals(right, left);
} }
@ -631,8 +635,9 @@ public class CSVFormatTest {
assertNotEquals(right, left); assertNotEquals(right, left);
} }
@SuppressWarnings("deprecation")
@Test @Test
public void testEqualsSkipHeaderRecord0() { public void testEqualsRecordSeparator_Deprecated() {
final CSVFormat right = CSVFormat.newFormat('\'') final CSVFormat right = CSVFormat.newFormat('\'')
.withRecordSeparator(CR) .withRecordSeparator(CR)
.withCommentMarker('#') .withCommentMarker('#')
@ -640,11 +645,9 @@ public class CSVFormatTest {
.withIgnoreEmptyLines() .withIgnoreEmptyLines()
.withIgnoreSurroundingSpaces() .withIgnoreSurroundingSpaces()
.withQuote('"') .withQuote('"')
.withQuoteMode(QuoteMode.ALL) .withQuoteMode(QuoteMode.ALL);
.withNullString("null")
.withSkipHeaderRecord();
final CSVFormat left = right final CSVFormat left = right
.withSkipHeaderRecord(false); .withRecordSeparator(LF);
assertNotEquals(right, left); assertNotEquals(right, left);
} }
@ -668,6 +671,25 @@ public class CSVFormatTest {
assertNotEquals(right, left); 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 @Test
public void testEqualsWithNull() { public void testEqualsWithNull() {
@ -731,22 +753,15 @@ public class CSVFormatTest {
} }
@Test
public void testEscapeSameAsCommentStartThrowsException0() {
assertThrows(IllegalArgumentException.class, () -> CSVFormat.DEFAULT.withEscape('!').withCommentMarker('!'));
}
@Test @Test
public void testEscapeSameAsCommentStartThrowsException() { public void testEscapeSameAsCommentStartThrowsException() {
assertThrows(IllegalArgumentException.class, () -> CSVFormat.DEFAULT.builder().setEscape('!').setCommentMarker('!').build()); assertThrows(IllegalArgumentException.class, () -> CSVFormat.DEFAULT.builder().setEscape('!').setCommentMarker('!').build());
} }
@SuppressWarnings("deprecation")
@Test @Test
public void testEscapeSameAsCommentStartThrowsExceptionForWrapperType0() { public void testEscapeSameAsCommentStartThrowsException_Deprecated() {
// Cannot assume that callers won't use different Character objects assertThrows(IllegalArgumentException.class, () -> CSVFormat.DEFAULT.withEscape('!').withCommentMarker('!'));
assertThrows(
IllegalArgumentException.class,
() -> CSVFormat.DEFAULT.withEscape(Character.valueOf('!')).withCommentMarker(Character.valueOf('!')));
} }
@Test @Test
@ -757,6 +772,15 @@ public class CSVFormatTest {
() -> CSVFormat.DEFAULT.builder().setEscape(Character.valueOf('!')).setCommentMarker(Character.valueOf('!')).build()); () -> 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 @Test
public void testFormat() { public void testFormat() {
final CSVFormat format = CSVFormat.DEFAULT; final CSVFormat format = CSVFormat.DEFAULT;
@ -775,6 +799,16 @@ public class CSVFormatTest {
assertEquals(Objects.class.getName(), e.getStackTrace()[0].getClassName()); 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=<MINIMAL> NullString=<> RecordSeparator=<" +CRLF+
"> IgnoreHeaderCase:ignored SkipHeaderRecord:false HeaderComments:[This is HeaderComments] Header:[col1, col2, col3]", format.toString());
}
@Test @Test
public void testGetHeader() { public void testGetHeader() {
final String[] header = {"one", "two", "three"}; 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 @Test
public void testJiraCsv236() { public void testJiraCsv236() {
CSVFormat.DEFAULT.builder().setAllowDuplicateHeaderNames(true).setHeader("CC","VV","VV").build(); 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 @Test
public void testNewFormat() { 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 @Test
public void testNullRecordSeparatorCsv106() { public void testNullRecordSeparatorCsv106() {
final CSVFormat format = CSVFormat.newFormat(';').builder().setSkipHeaderRecord(true).setHeader("H1", "H2").build(); final CSVFormat format = CSVFormat.newFormat(';').builder().setSkipHeaderRecord(true).setHeader("H1", "H2").build();
@ -893,9 +920,58 @@ public class CSVFormatTest {
assertFalse(formatStr.endsWith("null")); assertFalse(formatStr.endsWith("null"));
} }
@SuppressWarnings("deprecation")
@Test @Test
public void testQuoteCharSameAsCommentStartThrowsException0() { public void testNullRecordSeparatorCsv106__Deprecated() {
assertThrows(IllegalArgumentException.class, () -> CSVFormat.DEFAULT.withQuote('!').withCommentMarker('!')); 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 @Test
@ -903,12 +979,10 @@ public class CSVFormatTest {
assertThrows(IllegalArgumentException.class, () -> CSVFormat.DEFAULT.builder().setQuote('!').setCommentMarker('!').build()); assertThrows(IllegalArgumentException.class, () -> CSVFormat.DEFAULT.builder().setQuote('!').setCommentMarker('!').build());
} }
@SuppressWarnings("deprecation")
@Test @Test
public void testQuoteCharSameAsCommentStartThrowsExceptionForWrapperType0() { public void testQuoteCharSameAsCommentStartThrowsException_Deprecated() {
// Cannot assume that callers won't use different Character objects assertThrows(IllegalArgumentException.class, () -> CSVFormat.DEFAULT.withQuote('!').withCommentMarker('!'));
assertThrows(
IllegalArgumentException.class,
() -> CSVFormat.DEFAULT.builder().setQuote(Character.valueOf('!')).setCommentMarker('!').build());
} }
@Test @Test
@ -916,12 +990,16 @@ public class CSVFormatTest {
// Cannot assume that callers won't use different Character objects // Cannot assume that callers won't use different Character objects
assertThrows( assertThrows(
IllegalArgumentException.class, IllegalArgumentException.class,
() -> CSVFormat.DEFAULT.withQuote(Character.valueOf('!')).withCommentMarker('!')); () -> CSVFormat.DEFAULT.builder().setQuote(Character.valueOf('!')).setCommentMarker('!').build());
} }
@SuppressWarnings("deprecation")
@Test @Test
public void testQuoteCharSameAsDelimiterThrowsException0() { public void testQuoteCharSameAsCommentStartThrowsExceptionForWrapperType_Deprecated() {
assertThrows(IllegalArgumentException.class, () -> CSVFormat.DEFAULT.withQuote('!').withDelimiter('!')); // Cannot assume that callers won't use different Character objects
assertThrows(
IllegalArgumentException.class,
() -> CSVFormat.DEFAULT.withQuote(Character.valueOf('!')).withCommentMarker('!'));
} }
@Test @Test
@ -929,9 +1007,10 @@ public class CSVFormatTest {
assertThrows(IllegalArgumentException.class, () -> CSVFormat.DEFAULT.builder().setQuote('!').setDelimiter('!').build()); assertThrows(IllegalArgumentException.class, () -> CSVFormat.DEFAULT.builder().setQuote('!').setDelimiter('!').build());
} }
@SuppressWarnings("deprecation")
@Test @Test
public void testQuotePolicyNoneWithoutEscapeThrowsException0() { public void testQuoteCharSameAsDelimiterThrowsException_Deprecated() {
assertThrows(IllegalArgumentException.class, () -> CSVFormat.newFormat('!').withQuoteMode(QuoteMode.NONE)); assertThrows(IllegalArgumentException.class, () -> CSVFormat.DEFAULT.withQuote('!').withDelimiter('!'));
} }
@Test @Test
@ -939,6 +1018,12 @@ public class CSVFormatTest {
assertThrows(IllegalArgumentException.class, () -> CSVFormat.newFormat('!').builder().setQuoteMode(QuoteMode.NONE).build()); 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 @Test
public void testRFC4180() { public void testRFC4180() {
assertEquals(null, RFC4180.getCommentMarker()); 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 @Test
public void testWithCommentStart() { public void testWithCommentStart() {
final CSVFormat formatWithCommentStart = CSVFormat.DEFAULT.withCommentMarker('#'); final CSVFormat formatWithCommentStart = CSVFormat.DEFAULT.withCommentMarker('#');
assertEquals( Character.valueOf('#'), formatWithCommentStart.getCommentMarker()); assertEquals( Character.valueOf('#'), formatWithCommentStart.getCommentMarker());
} }
@Test @Test
public void testWithCommentStartCRThrowsException() { public void testWithCommentStartCRThrowsException() {
assertThrows(IllegalArgumentException.class, () -> CSVFormat.DEFAULT.withCommentMarker(CR)); assertThrows(IllegalArgumentException.class, () -> CSVFormat.DEFAULT.withCommentMarker(CR));
} }
@Test @Test
public void testWithDelimiter() { public void testWithDelimiter() {
final CSVFormat formatWithDelimiter = CSVFormat.DEFAULT.withDelimiter('!'); final CSVFormat formatWithDelimiter = CSVFormat.DEFAULT.withDelimiter('!');
assertEquals('!', formatWithDelimiter.getDelimiter()); assertEquals('!', formatWithDelimiter.getDelimiter());
} }
@Test @Test
public void testWithDelimiterLFThrowsException() { public void testWithDelimiterLFThrowsException() {
assertThrows(IllegalArgumentException.class, () -> CSVFormat.DEFAULT.withDelimiter(LF)); assertThrows(IllegalArgumentException.class, () -> CSVFormat.DEFAULT.withDelimiter(LF));
} }
@Test @Test
public void testWithEmptyEnum() { public void testWithEmptyEnum() {
final CSVFormat formatWithHeader = CSVFormat.DEFAULT.withHeader(EmptyEnum.class); final CSVFormat formatWithHeader = CSVFormat.DEFAULT.withHeader(EmptyEnum.class);
assertTrue(formatWithHeader.getHeader().length == 0); assertTrue(formatWithHeader.getHeader().length == 0);
} }
@Test @Test
public void testWithEscape() { public void testWithEscape() {
final CSVFormat formatWithEscape = CSVFormat.DEFAULT.withEscape('&'); final CSVFormat formatWithEscape = CSVFormat.DEFAULT.withEscape('&');
assertEquals(Character.valueOf('&'), formatWithEscape.getEscapeCharacter()); assertEquals(Character.valueOf('&'), formatWithEscape.getEscapeCharacter());
} }
@Test @Test
public void testWithEscapeCRThrowsExceptions() { public void testWithEscapeCRThrowsExceptions() {
assertThrows(IllegalArgumentException.class, () -> CSVFormat.DEFAULT.withEscape(CR)); assertThrows(IllegalArgumentException.class, () -> CSVFormat.DEFAULT.withEscape(CR));
} }
@Test @Test
public void testWithFirstRecordAsHeader() { public void testWithFirstRecordAsHeader() {
final CSVFormat formatWithFirstRecordAsHeader = CSVFormat.DEFAULT.withFirstRecordAsHeader(); final CSVFormat formatWithFirstRecordAsHeader = CSVFormat.DEFAULT.withFirstRecordAsHeader();
@ -1199,7 +1316,6 @@ public class CSVFormatTest {
assertNotSame(header, formatWithHeader.getHeader()); assertNotSame(header, formatWithHeader.getHeader());
} }
@Test @Test
public void testWithHeaderComments() { public void testWithHeaderComments() {
@ -1361,13 +1477,25 @@ public class CSVFormatTest {
} }
@Test @Test
public void testWithHeaderEnum() { public void testWithHeaderEnum() {
final CSVFormat formatWithHeader = CSVFormat.DEFAULT.withHeader(Header.class); final CSVFormat formatWithHeader = CSVFormat.DEFAULT.withHeader(Header.class);
assertArrayEquals(new String[]{ "Name", "Email", "Phone" }, formatWithHeader.getHeader()); assertArrayEquals(new String[]{ "Name", "Email", "Phone" }, formatWithHeader.getHeader());
} }
@Test
public void testWithHeaderEnumNull() {
final CSVFormat format = CSVFormat.DEFAULT;
final Class<Enum<?>> simpleName = null;
format.withHeader(simpleName);
}
@Test
public void testWithHeaderResultSetNull() throws SQLException {
final CSVFormat format = CSVFormat.DEFAULT;
final ResultSet resultSet = null;
format.withHeader(resultSet);
}
@Test @Test
public void testWithIgnoreEmptyLines() { public void testWithIgnoreEmptyLines() {
@ -1375,28 +1503,24 @@ public class CSVFormatTest {
assertTrue(CSVFormat.DEFAULT.withIgnoreEmptyLines().getIgnoreEmptyLines()); assertTrue(CSVFormat.DEFAULT.withIgnoreEmptyLines().getIgnoreEmptyLines());
} }
@Test @Test
public void testWithIgnoreSurround() { public void testWithIgnoreSurround() {
assertFalse(CSVFormat.DEFAULT.withIgnoreSurroundingSpaces(false).getIgnoreSurroundingSpaces()); assertFalse(CSVFormat.DEFAULT.withIgnoreSurroundingSpaces(false).getIgnoreSurroundingSpaces());
assertTrue(CSVFormat.DEFAULT.withIgnoreSurroundingSpaces().getIgnoreSurroundingSpaces()); assertTrue(CSVFormat.DEFAULT.withIgnoreSurroundingSpaces().getIgnoreSurroundingSpaces());
} }
@Test @Test
public void testWithNullString() { public void testWithNullString() {
final CSVFormat formatWithNullString = CSVFormat.DEFAULT.withNullString("null"); final CSVFormat formatWithNullString = CSVFormat.DEFAULT.withNullString("null");
assertEquals("null", formatWithNullString.getNullString()); assertEquals("null", formatWithNullString.getNullString());
} }
@Test @Test
public void testWithQuoteChar() { public void testWithQuoteChar() {
final CSVFormat formatWithQuoteChar = CSVFormat.DEFAULT.withQuote('"'); final CSVFormat formatWithQuoteChar = CSVFormat.DEFAULT.withQuote('"');
assertEquals(Character.valueOf('"'), formatWithQuoteChar.getQuoteCharacter()); assertEquals(Character.valueOf('"'), formatWithQuoteChar.getQuoteCharacter());
} }
@Test @Test
public void testWithQuoteLFThrowsException() { public void testWithQuoteLFThrowsException() {
assertThrows(IllegalArgumentException.class, () -> CSVFormat.DEFAULT.withQuote(LF)); assertThrows(IllegalArgumentException.class, () -> CSVFormat.DEFAULT.withQuote(LF));
@ -1431,103 +1555,4 @@ public class CSVFormatTest {
final CSVFormat formatWithRecordSeparator = CSVFormat.DEFAULT.withSystemRecordSeparator(); final CSVFormat formatWithRecordSeparator = CSVFormat.DEFAULT.withSystemRecordSeparator();
assertEquals(System.getProperty("line.separator"), formatWithRecordSeparator.getRecordSeparator()); 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=<MINIMAL> 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<Enum<?>> 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());
}
} }