Enable Checkstyle for test sources and fix issues

This commit is contained in:
Gary Gregory 2024-09-19 16:04:28 -04:00
parent fce94ea666
commit 6a11b896aa
13 changed files with 223 additions and 216 deletions

View File

@ -206,6 +206,7 @@
<configLocation>${checkstyle.config.file}</configLocation> <configLocation>${checkstyle.config.file}</configLocation>
<enableRulesSummary>false</enableRulesSummary> <enableRulesSummary>false</enableRulesSummary>
<suppressionsLocation>${checkstyle.suppress.file}</suppressionsLocation> <suppressionsLocation>${checkstyle.suppress.file}</suppressionsLocation>
<includeTestSourceDirectory>true</includeTestSourceDirectory>
</configuration> </configuration>
</plugin> </plugin>
<plugin> <plugin>

View File

@ -28,10 +28,6 @@ import java.util.Scanner;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.zip.GZIPInputStream; import java.util.zip.GZIPInputStream;
import com.generationjava.io.CsvReader;
import com.opencsv.CSVParserBuilder;
import com.opencsv.CSVReaderBuilder;
import org.apache.commons.io.IOUtils; import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.openjdk.jmh.annotations.Benchmark; import org.openjdk.jmh.annotations.Benchmark;
@ -49,6 +45,10 @@ import org.openjdk.jmh.infra.Blackhole;
import org.supercsv.io.CsvListReader; import org.supercsv.io.CsvListReader;
import org.supercsv.prefs.CsvPreference; import org.supercsv.prefs.CsvPreference;
import com.generationjava.io.CsvReader;
import com.opencsv.CSVParserBuilder;
import com.opencsv.CSVReaderBuilder;
@BenchmarkMode(Mode.AverageTime) @BenchmarkMode(Mode.AverageTime)
@Fork(value = 1, jvmArgs = {"-server", "-Xms1024M", "-Xmx1024M"}) @Fork(value = 1, jvmArgs = {"-server", "-Xms1024M", "-Xmx1024M"})
@Threads(1) @Threads(1)

View File

@ -308,24 +308,23 @@ public class CSVDuplicateHeaderTest {
final boolean ignoreHeaderCase, final boolean ignoreHeaderCase,
final String[] headers, final String[] headers,
final boolean valid) throws IOException { final boolean valid) throws IOException {
final CSVFormat format = // @formatter:off
CSVFormat.DEFAULT.builder() final CSVFormat format = CSVFormat.DEFAULT.builder()
.setDuplicateHeaderMode(duplicateHeaderMode) .setDuplicateHeaderMode(duplicateHeaderMode)
.setAllowMissingColumnNames(allowMissingColumnNames) .setAllowMissingColumnNames(allowMissingColumnNames)
.setIgnoreHeaderCase(ignoreHeaderCase) .setIgnoreHeaderCase(ignoreHeaderCase)
.setNullString("NULL") .setNullString("NULL")
.setHeader() .setHeader()
.build(); .build();
// @formatter:on
final String input = Arrays.stream(headers) final String input = Arrays.stream(headers)
.map(s -> s == null ? format.getNullString() : s) .map(s -> s == null ? format.getNullString() : s)
.collect(Collectors.joining(format.getDelimiterString())); .collect(Collectors.joining(format.getDelimiterString()));
// @formatter:off
if (valid) { if (valid) {
try (CSVParser parser = CSVParser.parse(input, format)) { try (CSVParser parser = CSVParser.parse(input, format)) {
// Parser ignores null headers // Parser ignores null headers
final List<String> expected = final List<String> expected = Arrays.stream(headers).filter(s -> s != null).collect(Collectors.toList());
Arrays.stream(headers)
.filter(s -> s != null)
.collect(Collectors.toList());
Assertions.assertEquals(expected, parser.getHeaderNames(), "HeaderNames"); Assertions.assertEquals(expected, parser.getHeaderNames(), "HeaderNames");
} }
} else { } else {

View File

@ -715,11 +715,21 @@ public class CSVFormatTest {
@Test @Test
public void testFormatToString() { public void testFormatToString() {
final CSVFormat format = CSVFormat.RFC4180.withEscape('?').withDelimiter(',').withQuoteMode(QuoteMode.MINIMAL).withRecordSeparator(CRLF).withQuote('"') // @formatter:off
.withNullString("").withIgnoreHeaderCase(true).withHeaderComments("This is HeaderComments").withHeader("col1", "col2", "col3"); final CSVFormat format = CSVFormat.RFC4180
.withEscape('?')
.withDelimiter(',')
.withQuoteMode(QuoteMode.MINIMAL)
.withRecordSeparator(CRLF)
.withQuote('"')
.withNullString("")
.withIgnoreHeaderCase(true)
.withHeaderComments("This is HeaderComments")
.withHeader("col1", "col2", "col3");
// @formatter:on
assertEquals( assertEquals(
"Delimiter=<,> Escape=<?> QuoteChar=<\"> QuoteMode=<MINIMAL> NullString=<> RecordSeparator=<" + CRLF "Delimiter=<,> Escape=<?> QuoteChar=<\"> QuoteMode=<MINIMAL> NullString=<> RecordSeparator=<" + CRLF +
+ "> IgnoreHeaderCase:ignored SkipHeaderRecord:false HeaderComments:[This is HeaderComments] Header:[col1, col2, col3]", "> IgnoreHeaderCase:ignored SkipHeaderRecord:false HeaderComments:[This is HeaderComments] Header:[col1, col2, col3]",
format.toString()); format.toString());
} }
@ -960,12 +970,14 @@ public class CSVFormatTest {
@Test @Test
public void testQuoteModeNoneShouldReturnMeaningfulExceptionMessage() { public void testQuoteModeNoneShouldReturnMeaningfulExceptionMessage() {
final Exception exception = assertThrows(IllegalArgumentException.class, () -> { final Exception exception = assertThrows(IllegalArgumentException.class, () ->
// @formatter:off
CSVFormat.DEFAULT.builder() CSVFormat.DEFAULT.builder()
.setHeader("Col1", "Col2", "Col3", "Col4") .setHeader("Col1", "Col2", "Col3", "Col4")
.setQuoteMode(QuoteMode.NONE) .setQuoteMode(QuoteMode.NONE)
.build(); .build()
}); // @formatter:on
);
final String actualMessage = exception.getMessage(); final String actualMessage = exception.getMessage();
final String expectedMessage = "Quote mode set to NONE but no escape character is set"; final String expectedMessage = "Quote mode set to NONE but no escape character is set";
assertEquals(expectedMessage, actualMessage); assertEquals(expectedMessage, actualMessage);

View File

@ -64,9 +64,8 @@ import org.junit.jupiter.params.provider.EnumSource;
/** /**
* CSVParserTest * CSVParserTest
* *
* The test are organized in three different sections: The 'setter/getter' section, the lexer section and finally the * The test are organized in three different sections: The 'setter/getter' section, the lexer section and finally the parser section. In case a test fails, you
* parser section. In case a test fails, you should follow a top-down approach for fixing a potential bug (its likely * should follow a top-down approach for fixing a potential bug (its likely that the parser itself fails if the lexer has problems...).
* that the parser itself fails if the lexer has problems...).
*/ */
public class CSVParserTest { public class CSVParserTest {
@ -74,9 +73,9 @@ public class CSVParserTest {
private static final String UTF_8_NAME = UTF_8.name(); private static final String UTF_8_NAME = UTF_8.name();
private static final String CSV_INPUT = "a,b,c,d\n" + " a , b , 1 2 \n" + "\"foo baar\", b,\n" private static final String CSV_INPUT = "a,b,c,d\n" + " a , b , 1 2 \n" + "\"foo baar\", b,\n" +
// + " \"foo\n,,\n\"\",,\n\\\"\",d,e\n"; // + " \"foo\n,,\n\"\",,\n\\\"\",d,e\n";
+ " \"foo\n,,\n\"\",,\n\"\"\",d,e\n"; // changed to use standard CSV escaping " \"foo\n,,\n\"\",,\n\"\"\",d,e\n"; // changed to use standard CSV escaping
private static final String CSV_INPUT_1 = "a,b,c,d"; private static final String CSV_INPUT_1 = "a,b,c,d";
@ -94,23 +93,28 @@ public class CSVParserTest {
static private final String CSV_INPUT_HEADER_TRAILER_COMMENT = "# header comment" + CRLF + "A,B" + CRLF + "1,2" + CRLF + "# comment"; static private final String CSV_INPUT_HEADER_TRAILER_COMMENT = "# header comment" + CRLF + "A,B" + CRLF + "1,2" + CRLF + "# comment";
// CSV with a multi-line header and trailer comment // CSV with a multi-line header and trailer comment
static private final String CSV_INPUT_MULTILINE_HEADER_TRAILER_COMMENT = "# multi-line" + CRLF + "# header comment" + CRLF + "A,B" + CRLF + "1,2" + CRLF + "# multi-line" + CRLF + "# comment"; static private final String CSV_INPUT_MULTILINE_HEADER_TRAILER_COMMENT = "# multi-line" + CRLF + "# header comment" + CRLF + "A,B" + CRLF + "1,2" + CRLF +
"# multi-line" + CRLF + "# comment";
// Format with auto-detected header // Format with auto-detected header
static private final CSVFormat FORMAT_AUTO_HEADER = CSVFormat.Builder.create(CSVFormat.DEFAULT).setCommentMarker('#').setHeader().build(); static private final CSVFormat FORMAT_AUTO_HEADER = CSVFormat.Builder.create(CSVFormat.DEFAULT).setCommentMarker('#').setHeader().build();
// Format with explicit header // Format with explicit header
// @formatter:off
static private final CSVFormat FORMAT_EXPLICIT_HEADER = CSVFormat.Builder.create(CSVFormat.DEFAULT) static private final CSVFormat FORMAT_EXPLICIT_HEADER = CSVFormat.Builder.create(CSVFormat.DEFAULT)
.setSkipHeaderRecord(true) .setSkipHeaderRecord(true)
.setCommentMarker('#') .setCommentMarker('#')
.setHeader("A", "B") .setHeader("A", "B")
.build(); .build();
// @formatter:on
// Format with explicit header that does not skip the header line // Format with explicit header that does not skip the header line
// @formatter:off
CSVFormat FORMAT_EXPLICIT_HEADER_NOSKIP = CSVFormat.Builder.create(CSVFormat.DEFAULT) CSVFormat FORMAT_EXPLICIT_HEADER_NOSKIP = CSVFormat.Builder.create(CSVFormat.DEFAULT)
.setCommentMarker('#') .setCommentMarker('#')
.setHeader("A", "B") .setHeader("A", "B")
.build(); .build();
// @formatter:on
@SuppressWarnings("resource") // caller releases @SuppressWarnings("resource") // caller releases
private BOMInputStream createBOMInputStream(final String resource) throws IOException { private BOMInputStream createBOMInputStream(final String resource) throws IOException {
@ -131,22 +135,22 @@ public class CSVParserTest {
@Test @Test
public void testBackslashEscaping() throws IOException { public void testBackslashEscaping() throws IOException {
// To avoid confusion over the need for escaping chars in java code, // To avoid confusion over the need for escaping chars in java code,
// We will test with a forward slash as the escape char, and a single // We will test with a forward slash as the escape char, and a single
// quote as the encapsulator. // quote as the encapsulator.
final String code = "one,two,three\n" // 0 // @formatter:off
+ "'',''\n" // 1) empty encapsulators final String code = "one,two,three\n" + // 0
+ "/',/'\n" // 2) single encapsulators "'',''\n" + // 1) empty encapsulators
+ "'/'','/''\n" // 3) single encapsulators encapsulated via escape "/',/'\n" + // 2) single encapsulators
+ "'''',''''\n" // 4) single encapsulators encapsulated via doubling "'/'','/''\n" + // 3) single encapsulators encapsulated via escape
+ "/,,/,\n" // 5) separator escaped "'''',''''\n" + // 4) single encapsulators encapsulated via doubling
+ "//,//\n" // 6) escape escaped "/,,/,\n" + // 5) separator escaped
+ "'//','//'\n" // 7) escape escaped in encapsulation "//,//\n" + // 6) escape escaped
+ " 8 , \"quoted \"\" /\" // string\" \n" // don't eat spaces "'//','//'\n" + // 7) escape escaped in encapsulation
+ "9, /\n \n" // escaped newline " 8 , \"quoted \"\" /\" // string\" \n" + // don't eat spaces
+ ""; "9, /\n \n" + // escaped newline
"";
final String[][] res = {{"one", "two", "three"}, // 0 final String[][] res = {{"one", "two", "three"}, // 0
{"", ""}, // 1 {"", ""}, // 1
{"'", "'"}, // 2 {"'", "'"}, // 2
@ -155,40 +159,35 @@ public class CSVParserTest {
{",", ","}, // 5 {",", ","}, // 5
{"/", "/"}, // 6 {"/", "/"}, // 6
{"/", "/"}, // 7 {"/", "/"}, // 7
{" 8 ", " \"quoted \"\" /\" / string\" "}, {"9", " \n "},}; {" 8 ", " \"quoted \"\" /\" / string\" "}, {"9", " \n "} };
// @formatter:on
final CSVFormat format = CSVFormat.newFormat(',').withQuote('\'').withRecordSeparator(CRLF).withEscape('/').withIgnoreEmptyLines(); final CSVFormat format = CSVFormat.newFormat(',').withQuote('\'').withRecordSeparator(CRLF).withEscape('/').withIgnoreEmptyLines();
try (final CSVParser parser = CSVParser.parse(code, format)) { try (final CSVParser parser = CSVParser.parse(code, format)) {
final List<CSVRecord> records = parser.getRecords(); final List<CSVRecord> records = parser.getRecords();
assertFalse(records.isEmpty()); assertFalse(records.isEmpty());
Utils.compare("Records do not match expected result", res, records); Utils.compare("Records do not match expected result", res, records);
} }
} }
@Test @Test
public void testBackslashEscaping2() throws IOException { public void testBackslashEscaping2() throws IOException {
// To avoid confusion over the need for escaping chars in java code, // To avoid confusion over the need for escaping chars in java code,
// We will test with a forward slash as the escape char, and a single // We will test with a forward slash as the escape char, and a single
// quote as the encapsulator. // quote as the encapsulator.
// @formatter:off
final String code = "" + " , , \n" // 1) final String code = "" + " , , \n" + // 1)
+ " \t , , \n" // 2) " \t , , \n" + // 2)
+ " // , /, , /,\n" // 3) " // , /, , /,\n" + // 3)
+ ""; "";
final String[][] res = {{" ", " ", " "}, // 1 final String[][] res = {{" ", " ", " "}, // 1
{" \t ", " ", " "}, // 2 {" \t ", " ", " "}, // 2
{" / ", " , ", " ,"}, // 3 {" / ", " , ", " ,"}, // 3
}; };
// @formatter:on
final CSVFormat format = CSVFormat.newFormat(',').withRecordSeparator(CRLF).withEscape('/').withIgnoreEmptyLines(); final CSVFormat format = CSVFormat.newFormat(',').withRecordSeparator(CRLF).withEscape('/').withIgnoreEmptyLines();
try (final CSVParser parser = CSVParser.parse(code, format)) { try (final CSVParser parser = CSVParser.parse(code, format)) {
final List<CSVRecord> records = parser.getRecords(); final List<CSVRecord> records = parser.getRecords();
assertFalse(records.isEmpty()); assertFalse(records.isEmpty());
Utils.compare("", res, records); Utils.compare("", res, records);
} }
} }
@ -196,10 +195,13 @@ public class CSVParserTest {
@Test @Test
@Disabled @Disabled
public void testBackslashEscapingOld() throws IOException { public void testBackslashEscapingOld() throws IOException {
final String code = "one,two,three\n" + "on\\\"e,two\n" + "on\"e,two\n" + "one,\"tw\\\"o\"\n" + "one,\"t\\,wo\"\n" + "one,two,\"th,ree\"\n" final String code = "one,two,three\n" + "on\\\"e,two\n" + "on\"e,two\n" + "one,\"tw\\\"o\"\n" + "one,\"t\\,wo\"\n" + "one,two,\"th,ree\"\n" +
+ "\"a\\\\\"\n" + "a\\,b\n" + "\"a\\\\,b\""; "\"a\\\\\"\n" + "a\\,b\n" + "\"a\\\\,b\"";
final String[][] res = {{"one", "two", "three"}, {"on\\\"e", "two"}, {"on\"e", "two"}, {"one", "tw\"o"}, {"one", "t\\,wo"}, // backslash in quotes only final String[][] res = { { "one", "two", "three" }, { "on\\\"e", "two" }, { "on\"e", "two" }, { "one", "tw\"o" }, { "one", "t\\,wo" }, // backslash in
// escapes a delimiter (",") // quotes only
// escapes a
// delimiter
// (",")
{ "one", "two", "th,ree" }, { "a\\\\" }, // backslash in quotes only escapes a delimiter (",") { "one", "two", "th,ree" }, { "a\\\\" }, // backslash in quotes only escapes a delimiter (",")
{ "a\\", "b" }, // a backslash must be returned { "a\\", "b" }, // a backslash must be returned
{ "a\\\\,b" } // backslash in quotes only escapes a delimiter (",") { "a\\\\,b" } // backslash in quotes only escapes a delimiter (",")
@ -402,28 +404,25 @@ public class CSVParserTest {
@Test @Test
public void testDefaultFormat() throws IOException { public void testDefaultFormat() throws IOException {
final String code = "" + "a,b#\n" // 1) // @formatter:off
+ "\"\n\",\" \",#\n" // 2) final String code = "" + "a,b#\n" + // 1)
+ "#,\"\"\n" // 3) "\"\n\",\" \",#\n" + // 2)
+ "# Final comment\n"// 4) "#,\"\"\n" + // 3)
"# Final comment\n" // 4)
; ;
// @formatter:on
final String[][] res = { { "a", "b#" }, { "\n", " ", "#" }, { "#", "" }, { "# Final comment" } }; final String[][] res = { { "a", "b#" }, { "\n", " ", "#" }, { "#", "" }, { "# Final comment" } };
CSVFormat format = CSVFormat.DEFAULT; CSVFormat format = CSVFormat.DEFAULT;
assertFalse(format.isCommentMarkerSet()); assertFalse(format.isCommentMarkerSet());
final String[][] res_comments = {{"a", "b#"}, {"\n", " ", "#"},}; final String[][] res_comments = { { "a", "b#" }, { "\n", " ", "#" } };
try (final CSVParser parser = CSVParser.parse(code, format)) { try (final CSVParser parser = CSVParser.parse(code, format)) {
final List<CSVRecord> records = parser.getRecords(); final List<CSVRecord> records = parser.getRecords();
assertFalse(records.isEmpty()); assertFalse(records.isEmpty());
Utils.compare("Failed to parse without comments", res, records); Utils.compare("Failed to parse without comments", res, records);
format = CSVFormat.DEFAULT.withCommentMarker('#'); format = CSVFormat.DEFAULT.withCommentMarker('#');
} }
try (final CSVParser parser = CSVParser.parse(code, format)) { try (final CSVParser parser = CSVParser.parse(code, format)) {
final List<CSVRecord> records = parser.getRecords(); final List<CSVRecord> records = parser.getRecords();
Utils.compare("Failed to parse with comments", res_comments, records); Utils.compare("Failed to parse with comments", res_comments, records);
} }
} }
@ -611,7 +610,8 @@ public class CSVParserTest {
@Test @Test
public void testForEach() throws Exception { public void testForEach() throws Exception {
try (final Reader in = new StringReader("a,b,c\n1,2,3\nx,y,z"); final CSVParser parser = CSVFormat.DEFAULT.parse(in)) { try (final Reader in = new StringReader("a,b,c\n1,2,3\nx,y,z");
final CSVParser parser = CSVFormat.DEFAULT.parse(in)) {
final List<CSVRecord> records = new ArrayList<>(); final List<CSVRecord> records = new ArrayList<>();
for (final CSVRecord record : parser) { for (final CSVRecord record : parser) {
records.add(record); records.add(record);
@ -782,7 +782,8 @@ public class CSVParserTest {
@Test @Test
public void testGetOneLineOneParser() throws IOException { public void testGetOneLineOneParser() throws IOException {
final CSVFormat format = CSVFormat.DEFAULT; final CSVFormat format = CSVFormat.DEFAULT;
try (final PipedWriter writer = new PipedWriter(); final CSVParser parser = new CSVParser(new PipedReader(writer), format)) { try (final PipedWriter writer = new PipedWriter();
final CSVParser parser = new CSVParser(new PipedReader(writer), format)) {
writer.append(CSV_INPUT_1); writer.append(CSV_INPUT_1);
writer.append(format.getRecordSeparator()); writer.append(format.getRecordSeparator());
final CSVRecord record1 = parser.nextRecord(); final CSVRecord record1 = parser.nextRecord();
@ -949,10 +950,8 @@ public class CSVParserTest {
@Test @Test
public void testHeaderComment() throws Exception { public void testHeaderComment() throws Exception {
final Reader in = new StringReader("# comment\na,b,c\n1,2,3\nx,y,z"); final Reader in = new StringReader("# comment\na,b,c\n1,2,3\nx,y,z");
try (final CSVParser parser = CSVFormat.DEFAULT.withCommentMarker('#').withHeader().parse(in)) { try (final CSVParser parser = CSVFormat.DEFAULT.withCommentMarker('#').withHeader().parse(in)) {
final Iterator<CSVRecord> records = parser.iterator(); final Iterator<CSVRecord> records = parser.iterator();
for (int i = 0; i < 2; i++) { for (int i = 0; i < 2; i++) {
assertTrue(records.hasNext()); assertTrue(records.hasNext());
final CSVRecord record = records.next(); final CSVRecord record = records.next();
@ -960,7 +959,6 @@ public class CSVParserTest {
assertEquals(record.get(1), record.get("b")); assertEquals(record.get(1), record.get("b"));
assertEquals(record.get(2), record.get("c")); assertEquals(record.get(2), record.get("c"));
} }
assertFalse(records.hasNext()); assertFalse(records.hasNext());
} }
} }
@ -968,17 +966,14 @@ public class CSVParserTest {
@Test @Test
public void testHeaderMissing() throws Exception { public void testHeaderMissing() throws Exception {
final Reader in = new StringReader("a,,c\n1,2,3\nx,y,z"); final Reader in = new StringReader("a,,c\n1,2,3\nx,y,z");
try (final CSVParser parser = CSVFormat.DEFAULT.withHeader().withAllowMissingColumnNames().parse(in)) { try (final CSVParser parser = CSVFormat.DEFAULT.withHeader().withAllowMissingColumnNames().parse(in)) {
final Iterator<CSVRecord> records = parser.iterator(); final Iterator<CSVRecord> records = parser.iterator();
for (int i = 0; i < 2; i++) { for (int i = 0; i < 2; i++) {
assertTrue(records.hasNext()); assertTrue(records.hasNext());
final CSVRecord record = records.next(); final CSVRecord record = records.next();
assertEquals(record.get(0), record.get("a")); assertEquals(record.get(0), record.get("a"));
assertEquals(record.get(2), record.get("c")); assertEquals(record.get(2), record.get("c"));
} }
assertFalse(records.hasNext()); assertFalse(records.hasNext());
} }
} }
@ -1034,7 +1029,8 @@ public class CSVParserTest {
assertEquals("1", record.get("one")); assertEquals("1", record.get("one"));
assertEquals("2", record.get("two")); assertEquals("2", record.get("two"));
assertEquals("3", record.get("THREE")); assertEquals("3", record.get("THREE"));
}} }
}
@Test @Test
public void testIgnoreEmptyLines() throws IOException { public void testIgnoreEmptyLines() throws IOException {
@ -1055,10 +1051,8 @@ public class CSVParserTest {
@Test @Test
public void testIterator() throws Exception { public void testIterator() throws Exception {
final Reader in = new StringReader("a,b,c\n1,2,3\nx,y,z"); final Reader in = new StringReader("a,b,c\n1,2,3\nx,y,z");
try (final CSVParser parser = CSVFormat.DEFAULT.parse(in)) { try (final CSVParser parser = CSVFormat.DEFAULT.parse(in)) {
final Iterator<CSVRecord> iterator = parser.iterator(); final Iterator<CSVRecord> iterator = parser.iterator();
assertTrue(iterator.hasNext()); assertTrue(iterator.hasNext());
assertThrows(UnsupportedOperationException.class, iterator::remove); assertThrows(UnsupportedOperationException.class, iterator::remove);
assertArrayEquals(new String[] { "a", "b", "c" }, iterator.next().values()); assertArrayEquals(new String[] { "a", "b", "c" }, iterator.next().values());
@ -1068,17 +1062,15 @@ public class CSVParserTest {
assertTrue(iterator.hasNext()); assertTrue(iterator.hasNext());
assertArrayEquals(new String[] { "x", "y", "z" }, iterator.next().values()); assertArrayEquals(new String[] { "x", "y", "z" }, iterator.next().values());
assertFalse(iterator.hasNext()); assertFalse(iterator.hasNext());
assertThrows(NoSuchElementException.class, iterator::next); assertThrows(NoSuchElementException.class, iterator::next);
}} }
}
@Test @Test
public void testIteratorSequenceBreaking() throws IOException { public void testIteratorSequenceBreaking() throws IOException {
final String fiveRows = "1\n2\n3\n4\n5\n"; final String fiveRows = "1\n2\n3\n4\n5\n";
// Iterator hasNext() shouldn't break sequence // Iterator hasNext() shouldn't break sequence
try (CSVParser parser = CSVFormat.DEFAULT.parse(new StringReader(fiveRows))) { try (CSVParser parser = CSVFormat.DEFAULT.parse(new StringReader(fiveRows))) {
final Iterator<CSVRecord> iter = parser.iterator(); final Iterator<CSVRecord> iter = parser.iterator();
int recordNumber = 0; int recordNumber = 0;
while (iter.hasNext()) { while (iter.hasNext()) {
@ -1096,7 +1088,6 @@ public class CSVParserTest {
assertEquals(String.valueOf(recordNumber), record.get(0)); assertEquals(String.valueOf(recordNumber), record.get(0));
} }
} }
// Consecutive enhanced for loops shouldn't break sequence // Consecutive enhanced for loops shouldn't break sequence
try (CSVParser parser = CSVFormat.DEFAULT.parse(new StringReader(fiveRows))) { try (CSVParser parser = CSVFormat.DEFAULT.parse(new StringReader(fiveRows))) {
int recordNumber = 0; int recordNumber = 0;
@ -1112,7 +1103,6 @@ public class CSVParserTest {
assertEquals(String.valueOf(recordNumber), record.get(0)); assertEquals(String.valueOf(recordNumber), record.get(0));
} }
} }
// Consecutive enhanced for loops with hasNext() peeking shouldn't break sequence // Consecutive enhanced for loops with hasNext() peeking shouldn't break sequence
try (CSVParser parser = CSVFormat.DEFAULT.parse(new StringReader(fiveRows))) { try (CSVParser parser = CSVFormat.DEFAULT.parse(new StringReader(fiveRows))) {
int recordNumber = 0; int recordNumber = 0;
@ -1146,7 +1136,6 @@ public class CSVParserTest {
try (final CSVParser parser = CSVFormat.DEFAULT.withHeader("A", "B", "C").withSkipHeaderRecord().parse(in)) { try (final CSVParser parser = CSVFormat.DEFAULT.withHeader("A", "B", "C").withSkipHeaderRecord().parse(in)) {
final Iterator<CSVRecord> records = parser.iterator(); final Iterator<CSVRecord> records = parser.iterator();
CSVRecord record; CSVRecord record;
// 1st record // 1st record
record = records.next(); record = records.next();
assertTrue(record.isMapped("A")); assertTrue(record.isMapped("A"));
@ -1158,7 +1147,6 @@ public class CSVParserTest {
assertEquals("1", record.get("A")); assertEquals("1", record.get("A"));
assertEquals("2", record.get("B")); assertEquals("2", record.get("B"));
assertFalse(record.isConsistent()); assertFalse(record.isConsistent());
// 2nd record // 2nd record
record = records.next(); record = records.next();
assertTrue(record.isMapped("A")); assertTrue(record.isMapped("A"));
@ -1171,7 +1159,7 @@ public class CSVParserTest {
assertEquals("y", record.get("B")); assertEquals("y", record.get("B"));
assertEquals("z", record.get("C")); assertEquals("z", record.get("C"));
assertTrue(record.isConsistent()); assertTrue(record.isConsistent());
// end
assertFalse(records.hasNext()); assertFalse(records.hasNext());
} }
} }
@ -1455,7 +1443,8 @@ public class CSVParserTest {
@SuppressWarnings("resource") @SuppressWarnings("resource")
final CSVParser recordParser = record.getParser(); final CSVParser recordParser = record.getParser();
assertEquals(Arrays.asList("header1", "header2", "header1"), recordParser.getHeaderNames()); assertEquals(Arrays.asList("header1", "header2", "header1"), recordParser.getHeaderNames());
}} }
}
@Test @Test
public void testRoundtrip() throws Exception { public void testRoundtrip() throws Exception {
@ -1491,7 +1480,8 @@ public class CSVParserTest {
assertEquals("1", record.get("X")); assertEquals("1", record.get("X"));
assertEquals("2", record.get("Y")); assertEquals("2", record.get("Y"));
assertEquals("3", record.get("Z")); assertEquals("3", record.get("Z"));
}} }
}
@Test @Test
public void testSkipSetAltHeaders() throws Exception { public void testSkipSetAltHeaders() throws Exception {
@ -1544,7 +1534,8 @@ public class CSVParserTest {
assertArrayEquals(new String[] { "a", "b", "c" }, list.get(0).values()); assertArrayEquals(new String[] { "a", "b", "c" }, list.get(0).values());
assertArrayEquals(new String[] { "1", "2", "3" }, list.get(1).values()); assertArrayEquals(new String[] { "1", "2", "3" }, list.get(1).values());
assertArrayEquals(new String[] { "x", "y", "z" }, list.get(2).values()); assertArrayEquals(new String[] { "x", "y", "z" }, list.get(2).values());
}} }
}
@Test @Test
public void testThrowExceptionWithLineAndPosition() throws IOException { public void testThrowExceptionWithLineAndPosition() throws IOException {
@ -1587,7 +1578,8 @@ public class CSVParserTest {
assertEquals("2", record.get("Y")); assertEquals("2", record.get("Y"));
assertEquals("3", record.get("Z")); assertEquals("3", record.get("Z"));
assertEquals(3, record.size()); assertEquals(3, record.size());
}} }
}
private void validateLineNumbers(final String lineSeparator) throws IOException { private void validateLineNumbers(final String lineSeparator) throws IOException {
try (final CSVParser parser = CSVParser.parse("a" + lineSeparator + "b" + lineSeparator + "c", CSVFormat.DEFAULT.withRecordSeparator(lineSeparator))) { try (final CSVParser parser = CSVParser.parse("a" + lineSeparator + "b" + lineSeparator + "c", CSVFormat.DEFAULT.withRecordSeparator(lineSeparator))) {

View File

@ -314,8 +314,8 @@ public class CSVPrinterTest {
try (final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withCommentMarker('#'))) { try (final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withCommentMarker('#'))) {
printer.print(value); printer.print(value);
printer.printComment("This is a comment\r\non multiple lines\rthis is next comment\r"); printer.printComment("This is a comment\r\non multiple lines\rthis is next comment\r");
assertEquals("abc" + recordSeparator + "# This is a comment" + recordSeparator + "# on multiple lines" + recordSeparator + "# this is next comment" assertEquals("abc" + recordSeparator + "# This is a comment" + recordSeparator + "# on multiple lines" + recordSeparator +
+ recordSeparator + "# " + recordSeparator, sw.toString()); "# this is next comment" + recordSeparator + "# " + recordSeparator, sw.toString());
} }
} }
@ -729,8 +729,8 @@ public class CSVPrinterTest {
} }
} }
final String csv = sw.toString(); final String csv = sw.toString();
assertEquals("1,r1,\"long text 1\",\"YmluYXJ5IGRhdGEgMQ==\r\n\"" + recordSeparator + "2,r2,\"" + longText2 + "\",\"YmluYXJ5IGRhdGEgMg==\r\n\"" assertEquals("1,r1,\"long text 1\",\"YmluYXJ5IGRhdGEgMQ==\r\n\"" + recordSeparator + "2,r2,\"" + longText2 + "\",\"YmluYXJ5IGRhdGEgMg==\r\n\"" +
+ recordSeparator, csv); recordSeparator, csv);
// Round trip the data // Round trip the data
try (StringReader reader = new StringReader(csv); try (StringReader reader = new StringReader(csv);
final CSVParser csvParser = csvFormat.parse(reader)) { final CSVParser csvParser = csvFormat.parse(reader)) {
@ -1349,11 +1349,12 @@ public class CSVPrinterTest {
@Test @Test
public void testPrintCSVParser() throws IOException { public void testPrintCSVParser() throws IOException {
final String code = "a1,b1\n" // 1) // @formatter:off
+ "a2,b2\n" // 2) final String code = "a1,b1\n" + // 1)
+ "a3,b3\n" // 3) "a2,b2\n" + // 2)
+ "a4,b4\n"// 4) "a3,b3\n" + // 3)
; "a4,b4\n"; // 4)
// @formatter:on
final String[][] res = { { "a1", "b1" }, { "a2", "b2" }, { "a3", "b3" }, { "a4", "b4" } }; final String[][] res = { { "a1", "b1" }, { "a2", "b2" }, { "a3", "b3" }, { "a4", "b4" } };
final CSVFormat format = CSVFormat.DEFAULT; final CSVFormat format = CSVFormat.DEFAULT;
final StringWriter sw = new StringWriter(); final StringWriter sw = new StringWriter();
@ -1370,11 +1371,12 @@ public class CSVPrinterTest {
@Test @Test
public void testPrintCSVRecord() throws IOException { public void testPrintCSVRecord() throws IOException {
final String code = "a1,b1\n" // 1) // @formatter:off
+ "a2,b2\n" // 2) final String code = "a1,b1\n" + // 1)
+ "a3,b3\n" // 3) "a2,b2\n" + // 2)
+ "a4,b4\n"// 4) "a3,b3\n" + // 3)
; "a4,b4\n"; // 4)
// @formatter:on
final String[][] res = { { "a1", "b1" }, { "a2", "b2" }, { "a3", "b3" }, { "a4", "b4" } }; final String[][] res = { { "a1", "b1" }, { "a2", "b2" }, { "a3", "b3" }, { "a4", "b4" } };
final CSVFormat format = CSVFormat.DEFAULT; final CSVFormat format = CSVFormat.DEFAULT;
final StringWriter sw = new StringWriter(); final StringWriter sw = new StringWriter();
@ -1393,11 +1395,12 @@ public class CSVPrinterTest {
@Test @Test
public void testPrintCSVRecords() throws IOException { public void testPrintCSVRecords() throws IOException {
final String code = "a1,b1\n" // 1) // @formatter:off
+ "a2,b2\n" // 2) final String code = "a1,b1\n" + // 1)
+ "a3,b3\n" // 3) "a2,b2\n" + // 2)
+ "a4,b4\n"// 4) "a3,b3\n" + // 3)
; "a4,b4\n"; // 4)
// @formatter:on
final String[][] res = { { "a1", "b1" }, { "a2", "b2" }, { "a3", "b3" }, { "a4", "b4" } }; final String[][] res = { { "a1", "b1" }, { "a2", "b2" }, { "a3", "b3" }, { "a4", "b4" } };
final CSVFormat format = CSVFormat.DEFAULT; final CSVFormat format = CSVFormat.DEFAULT;
final StringWriter sw = new StringWriter(); final StringWriter sw = new StringWriter();
@ -1506,8 +1509,8 @@ public class CSVPrinterTest {
* Test to target the use of {@link IOUtils#copy(java.io.Reader, Appendable)} which directly buffers the value from the Reader to the Appendable. * Test to target the use of {@link IOUtils#copy(java.io.Reader, Appendable)} which directly buffers the value from the Reader to the Appendable.
* *
* <p> * <p>
* Requires the format to have no quote or escape character, value to be a {@link Reader Reader} and the output <em>MUST NOT</em> be a * Requires the format to have no quote or escape character, value to be a {@link Reader Reader} and the output <em>MUST NOT</em> be a {@link Writer Writer}
* {@link Writer Writer} but some other Appendable. * but some other Appendable.
* </p> * </p>
* *
* @throws IOException Not expected to happen * @throws IOException Not expected to happen
@ -1527,8 +1530,7 @@ public class CSVPrinterTest {
* Test to target the use of {@link IOUtils#copyLarge(java.io.Reader, Writer)} which directly buffers the value from the Reader to the Writer. * Test to target the use of {@link IOUtils#copyLarge(java.io.Reader, Writer)} which directly buffers the value from the Reader to the Writer.
* *
* <p> * <p>
* Requires the format to have no quote or escape character, value to be a {@link Reader Reader} and the output <em>MUST</em> be a * Requires the format to have no quote or escape character, value to be a {@link Reader Reader} and the output <em>MUST</em> be a {@link Writer Writer}.
* {@link Writer Writer}.
* </p> * </p>
* *
* @throws IOException Not expected to happen * @throws IOException Not expected to happen
@ -1546,11 +1548,12 @@ public class CSVPrinterTest {
@Test @Test
public void testPrintRecordStream() throws IOException { public void testPrintRecordStream() throws IOException {
final String code = "a1,b1\n" // 1) // @formatter:off
+ "a2,b2\n" // 2) final String code = "a1,b1\n" + // 1)
+ "a3,b3\n" // 3) "a2,b2\n" + // 2)
+ "a4,b4\n"// 4) "a3,b3\n" + // 3)
; "a4,b4\n"; // 4)
// @formatter:on
final String[][] res = { { "a1", "b1" }, { "a2", "b2" }, { "a3", "b3" }, { "a4", "b4" } }; final String[][] res = { { "a1", "b1" }, { "a2", "b2" }, { "a3", "b3" }, { "a4", "b4" } };
final CSVFormat format = CSVFormat.DEFAULT; final CSVFormat format = CSVFormat.DEFAULT;
final StringWriter sw = new StringWriter(); final StringWriter sw = new StringWriter();

View File

@ -53,14 +53,14 @@ public class PerformanceTest {
private static final class Stats { private static final class Stats {
final int count; final int count;
final int fields; final int fields;
Stats(final int c, final int f) { Stats(final int c, final int f) {
count = c; count = c;
fields = f; fields = f;
} }
} }
private static final String[] PROPERTY_NAMES = { private static final String[] PROPERTY_NAMES = { "java.version", // Java Runtime Environment version
"java.version", // Java Runtime Environment version
"java.vendor", // Java Runtime Environment vendor "java.vendor", // Java Runtime Environment vendor
// "java.vm.specification.version", // Java Virtual Machine specification version // "java.vm.specification.version", // Java Virtual Machine specification version
// "java.vm.specification.vendor", // Java Virtual Machine specification vendor // "java.vm.specification.vendor", // Java Virtual Machine specification vendor
@ -117,9 +117,7 @@ public class PerformanceTest {
System.out.printf("Found test fixture %s: %,d bytes.%n", BIG_FILE, BIG_FILE.length()); System.out.printf("Found test fixture %s: %,d bytes.%n", BIG_FILE, BIG_FILE.length());
} else { } else {
System.out.println("Decompressing test fixture to: " + BIG_FILE + "..."); System.out.println("Decompressing test fixture to: " + BIG_FILE + "...");
try ( try (final InputStream input = new GZIPInputStream(PerformanceTest.class.getClassLoader().getResourceAsStream(TEST_RESRC));
final InputStream input = new GZIPInputStream(
PerformanceTest.class.getClassLoader().getResourceAsStream(TEST_RESRC));
final OutputStream output = new FileOutputStream(BIG_FILE)) { final OutputStream output = new FileOutputStream(BIG_FILE)) {
IOUtils.copy(input, output); IOUtils.copy(input, output);
System.out.println(String.format("Decompressed test fixture %s: %,d bytes.", BIG_FILE, BIG_FILE.length())); System.out.println(String.format("Decompressed test fixture %s: %,d bytes.", BIG_FILE, BIG_FILE.length()));
@ -341,5 +339,5 @@ public class PerformanceTest {
} }
show(); show();
} }
} }

View File

@ -33,18 +33,16 @@ public class JiraCsv148Test {
.build(); .build();
// @formatter:on // @formatter:on
assertEquals( assertEquals(
"\"\",\" \",\" Single space on the left\",\"Single space on the right \"," "\"\",\" \",\" Single space on the left\",\"Single space on the right \"," +
+ "\" Single spaces on both sides \",\" Multiple spaces on the left\"," "\" Single spaces on both sides \",\" Multiple spaces on the left\"," +
+ "\"Multiple spaces on the right \",\" Multiple spaces on both sides \"", "\"Multiple spaces on the right \",\" Multiple spaces on both sides \"",
format.format("", " ", " Single space on the left", "Single space on the right ", format.format("", " ", " Single space on the left", "Single space on the right ", " Single spaces on both sides ",
" Single spaces on both sides ", " Multiple spaces on the left", "Multiple spaces on the right ", " Multiple spaces on the left", "Multiple spaces on the right ", " Multiple spaces on both sides "));
" Multiple spaces on both sides "));
} }
/** /**
* The difference between withTrim()and withIgnoreSurroundingSpace() difference: withTrim() can remove the leading * The difference between withTrim()and withIgnoreSurroundingSpace() difference: withTrim() can remove the leading and trailing spaces and newlines in
* and trailing spaces and newlines in quotation marks, while withIgnoreSurroundingSpace() cannot The same point: * quotation marks, while withIgnoreSurroundingSpace() cannot The same point: you can remove the leading and trailing spaces, tabs and other symbols.
* you can remove the leading and trailing spaces, tabs and other symbols.
*/ */
@Test @Test
public void testWithTrimEmpty() { public void testWithTrimEmpty() {
@ -55,11 +53,9 @@ public class JiraCsv148Test {
.build(); .build();
// @formatter:on // @formatter:on
assertEquals( assertEquals(
"\"\",\"\",\"Single space on the left\",\"Single space on the right\"," "\"\",\"\",\"Single space on the left\",\"Single space on the right\"," + "\"Single spaces on both sides\",\"Multiple spaces on the left\"," +
+ "\"Single spaces on both sides\",\"Multiple spaces on the left\"," "\"Multiple spaces on the right\",\"Multiple spaces on both sides\"",
+ "\"Multiple spaces on the right\",\"Multiple spaces on both sides\"", format.format("", " ", " Single space on the left", "Single space on the right ", " Single spaces on both sides ",
format.format("", " ", " Single space on the left", "Single space on the right ", " Multiple spaces on the left", "Multiple spaces on the right ", " Multiple spaces on both sides "));
" Single spaces on both sides ", " Multiple spaces on the left", "Multiple spaces on the right ",
" Multiple spaces on both sides "));
} }
} }

View File

@ -49,8 +49,12 @@ public class JiraCsv206Test {
assertEquals("123 Main St.", record.get(2)); assertEquals("123 Main St.", record.get(2));
} }
// Write with multiple character delimiter // Write with multiple character delimiter
final String outString = "# Change delimiter to [I]\r\n" + "first name[I]last name[I]address\r\n" // @formatter:off
+ "John[I]Smith[I]123 Main St."; final String outString =
"# Change delimiter to [I]\r\n" +
"first name[I]last name[I]address\r\n" +
"John[I]Smith[I]123 Main St.";
// @formatter:on
final String comment = "Change delimiter to [I]"; final String comment = "Change delimiter to [I]";
// @formatter:off // @formatter:off
final CSVFormat format = CSVFormat.EXCEL.builder() final CSVFormat format = CSVFormat.EXCEL.builder()

View File

@ -36,13 +36,14 @@ public class JiraCsv265Test {
@Test @Test
public void testCharacterPositionWithComments() throws IOException { public void testCharacterPositionWithComments() throws IOException {
// @formatter:off // @formatter:off
final String csv = "# Comment1\n" final String csv =
+ "Header1,Header2\n" "# Comment1\n" +
+ "# Comment2\n" "Header1,Header2\n" +
+ "Value1,Value2\n" "# Comment2\n" +
+ "# Comment3\n" "Value1,Value2\n" +
+ "Value3,Value4\n" "# Comment3\n" +
+ "# Comment4\n"; "Value3,Value4\n" +
"# Comment4\n";
final CSVFormat csvFormat = CSVFormat.DEFAULT.builder() final CSVFormat csvFormat = CSVFormat.DEFAULT.builder()
.setCommentMarker('#') .setCommentMarker('#')
.setHeader() .setHeader()
@ -61,15 +62,16 @@ public class JiraCsv265Test {
@Test @Test
public void testCharacterPositionWithCommentsSpanningMultipleLines() throws IOException { public void testCharacterPositionWithCommentsSpanningMultipleLines() throws IOException {
// @formatter:off // @formatter:off
final String csv = "# Comment1\n" final String csv =
+ "# Comment2\n" "# Comment1\n" +
+ "Header1,Header2\n" "# Comment2\n" +
+ "# Comment3\n" "Header1,Header2\n" +
+ "# Comment4\n" "# Comment3\n" +
+ "Value1,Value2\n" "# Comment4\n" +
+ "# Comment5\n" "Value1,Value2\n" +
+ "# Comment6\n" "# Comment5\n" +
+ "Value3,Value4"; "# Comment6\n" +
"Value3,Value4";
final CSVFormat csvFormat = CSVFormat.DEFAULT.builder() final CSVFormat csvFormat = CSVFormat.DEFAULT.builder()
.setCommentMarker('#') .setCommentMarker('#')
.setHeader() .setHeader()