Enable Checkstyle for test sources and fix issues
This commit is contained in:
parent
fce94ea666
commit
6a11b896aa
1
pom.xml
1
pom.xml
|
@ -206,6 +206,7 @@
|
|||
<configLocation>${checkstyle.config.file}</configLocation>
|
||||
<enableRulesSummary>false</enableRulesSummary>
|
||||
<suppressionsLocation>${checkstyle.suppress.file}</suppressionsLocation>
|
||||
<includeTestSourceDirectory>true</includeTestSourceDirectory>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
|
|
|
@ -28,10 +28,6 @@ import java.util.Scanner;
|
|||
import java.util.concurrent.TimeUnit;
|
||||
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.lang3.StringUtils;
|
||||
import org.openjdk.jmh.annotations.Benchmark;
|
||||
|
@ -49,6 +45,10 @@ import org.openjdk.jmh.infra.Blackhole;
|
|||
import org.supercsv.io.CsvListReader;
|
||||
import org.supercsv.prefs.CsvPreference;
|
||||
|
||||
import com.generationjava.io.CsvReader;
|
||||
import com.opencsv.CSVParserBuilder;
|
||||
import com.opencsv.CSVReaderBuilder;
|
||||
|
||||
@BenchmarkMode(Mode.AverageTime)
|
||||
@Fork(value = 1, jvmArgs = {"-server", "-Xms1024M", "-Xmx1024M"})
|
||||
@Threads(1)
|
||||
|
|
|
@ -308,24 +308,23 @@ public class CSVDuplicateHeaderTest {
|
|||
final boolean ignoreHeaderCase,
|
||||
final String[] headers,
|
||||
final boolean valid) throws IOException {
|
||||
final CSVFormat format =
|
||||
CSVFormat.DEFAULT.builder()
|
||||
// @formatter:off
|
||||
final CSVFormat format = CSVFormat.DEFAULT.builder()
|
||||
.setDuplicateHeaderMode(duplicateHeaderMode)
|
||||
.setAllowMissingColumnNames(allowMissingColumnNames)
|
||||
.setIgnoreHeaderCase(ignoreHeaderCase)
|
||||
.setNullString("NULL")
|
||||
.setHeader()
|
||||
.build();
|
||||
// @formatter:on
|
||||
final String input = Arrays.stream(headers)
|
||||
.map(s -> s == null ? format.getNullString() : s)
|
||||
.collect(Collectors.joining(format.getDelimiterString()));
|
||||
// @formatter:off
|
||||
if (valid) {
|
||||
try (CSVParser parser = CSVParser.parse(input, format)) {
|
||||
// Parser ignores null headers
|
||||
final List<String> expected =
|
||||
Arrays.stream(headers)
|
||||
.filter(s -> s != null)
|
||||
.collect(Collectors.toList());
|
||||
final List<String> expected = Arrays.stream(headers).filter(s -> s != null).collect(Collectors.toList());
|
||||
Assertions.assertEquals(expected, parser.getHeaderNames(), "HeaderNames");
|
||||
}
|
||||
} else {
|
||||
|
|
|
@ -715,11 +715,21 @@ public class CSVFormatTest {
|
|||
|
||||
@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");
|
||||
// @formatter:off
|
||||
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(
|
||||
"Delimiter=<,> Escape=<?> QuoteChar=<\"> QuoteMode=<MINIMAL> NullString=<> RecordSeparator=<" + CRLF
|
||||
+ "> IgnoreHeaderCase:ignored SkipHeaderRecord:false HeaderComments:[This is HeaderComments] Header:[col1, col2, col3]",
|
||||
"Delimiter=<,> Escape=<?> QuoteChar=<\"> QuoteMode=<MINIMAL> NullString=<> RecordSeparator=<" + CRLF +
|
||||
"> IgnoreHeaderCase:ignored SkipHeaderRecord:false HeaderComments:[This is HeaderComments] Header:[col1, col2, col3]",
|
||||
format.toString());
|
||||
}
|
||||
|
||||
|
@ -960,12 +970,14 @@ public class CSVFormatTest {
|
|||
|
||||
@Test
|
||||
public void testQuoteModeNoneShouldReturnMeaningfulExceptionMessage() {
|
||||
final Exception exception = assertThrows(IllegalArgumentException.class, () -> {
|
||||
final Exception exception = assertThrows(IllegalArgumentException.class, () ->
|
||||
// @formatter:off
|
||||
CSVFormat.DEFAULT.builder()
|
||||
.setHeader("Col1", "Col2", "Col3", "Col4")
|
||||
.setQuoteMode(QuoteMode.NONE)
|
||||
.build();
|
||||
});
|
||||
.build()
|
||||
// @formatter:on
|
||||
);
|
||||
final String actualMessage = exception.getMessage();
|
||||
final String expectedMessage = "Quote mode set to NONE but no escape character is set";
|
||||
assertEquals(expectedMessage, actualMessage);
|
||||
|
|
|
@ -64,9 +64,8 @@ import org.junit.jupiter.params.provider.EnumSource;
|
|||
/**
|
||||
* CSVParserTest
|
||||
*
|
||||
* 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 should follow a top-down approach for fixing a potential bug (its likely
|
||||
* that the parser itself fails if the lexer has problems...).
|
||||
* 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
|
||||
* should follow a top-down approach for fixing a potential bug (its likely that the parser itself fails if the lexer has problems...).
|
||||
*/
|
||||
public class CSVParserTest {
|
||||
|
||||
|
@ -74,9 +73,9 @@ public class CSVParserTest {
|
|||
|
||||
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"; // 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";
|
||||
|
||||
|
@ -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";
|
||||
|
||||
// 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
|
||||
static private final CSVFormat FORMAT_AUTO_HEADER = CSVFormat.Builder.create(CSVFormat.DEFAULT).setCommentMarker('#').setHeader().build();
|
||||
|
||||
// Format with explicit header
|
||||
// @formatter:off
|
||||
static private final CSVFormat FORMAT_EXPLICIT_HEADER = CSVFormat.Builder.create(CSVFormat.DEFAULT)
|
||||
.setSkipHeaderRecord(true)
|
||||
.setCommentMarker('#')
|
||||
.setHeader("A", "B")
|
||||
.build();
|
||||
// @formatter:on
|
||||
|
||||
// Format with explicit header that does not skip the header line
|
||||
// @formatter:off
|
||||
CSVFormat FORMAT_EXPLICIT_HEADER_NOSKIP = CSVFormat.Builder.create(CSVFormat.DEFAULT)
|
||||
.setCommentMarker('#')
|
||||
.setHeader("A", "B")
|
||||
.build();
|
||||
// @formatter:on
|
||||
|
||||
@SuppressWarnings("resource") // caller releases
|
||||
private BOMInputStream createBOMInputStream(final String resource) throws IOException {
|
||||
|
@ -131,22 +135,22 @@ public class CSVParserTest {
|
|||
|
||||
@Test
|
||||
public void testBackslashEscaping() throws IOException {
|
||||
|
||||
// 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
|
||||
// quote as the encapsulator.
|
||||
|
||||
final String code = "one,two,three\n" // 0
|
||||
+ "'',''\n" // 1) empty encapsulators
|
||||
+ "/',/'\n" // 2) single encapsulators
|
||||
+ "'/'','/''\n" // 3) single encapsulators encapsulated via escape
|
||||
+ "'''',''''\n" // 4) single encapsulators encapsulated via doubling
|
||||
+ "/,,/,\n" // 5) separator escaped
|
||||
+ "//,//\n" // 6) escape escaped
|
||||
+ "'//','//'\n" // 7) escape escaped in encapsulation
|
||||
+ " 8 , \"quoted \"\" /\" // string\" \n" // don't eat spaces
|
||||
+ "9, /\n \n" // escaped newline
|
||||
+ "";
|
||||
// @formatter:off
|
||||
final String code = "one,two,three\n" + // 0
|
||||
"'',''\n" + // 1) empty encapsulators
|
||||
"/',/'\n" + // 2) single encapsulators
|
||||
"'/'','/''\n" + // 3) single encapsulators encapsulated via escape
|
||||
"'''',''''\n" + // 4) single encapsulators encapsulated via doubling
|
||||
"/,,/,\n" + // 5) separator escaped
|
||||
"//,//\n" + // 6) escape escaped
|
||||
"'//','//'\n" + // 7) escape escaped in encapsulation
|
||||
" 8 , \"quoted \"\" /\" // string\" \n" + // don't eat spaces
|
||||
"9, /\n \n" + // escaped newline
|
||||
"";
|
||||
final String[][] res = {{"one", "two", "three"}, // 0
|
||||
{"", ""}, // 1
|
||||
{"'", "'"}, // 2
|
||||
|
@ -155,40 +159,35 @@ public class CSVParserTest {
|
|||
{",", ","}, // 5
|
||||
{"/", "/"}, // 6
|
||||
{"/", "/"}, // 7
|
||||
{" 8 ", " \"quoted \"\" /\" / string\" "}, {"9", " \n "},};
|
||||
|
||||
{" 8 ", " \"quoted \"\" /\" / string\" "}, {"9", " \n "} };
|
||||
// @formatter:on
|
||||
final CSVFormat format = CSVFormat.newFormat(',').withQuote('\'').withRecordSeparator(CRLF).withEscape('/').withIgnoreEmptyLines();
|
||||
|
||||
try (final CSVParser parser = CSVParser.parse(code, format)) {
|
||||
final List<CSVRecord> records = parser.getRecords();
|
||||
assertFalse(records.isEmpty());
|
||||
|
||||
Utils.compare("Records do not match expected result", res, records);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBackslashEscaping2() throws IOException {
|
||||
|
||||
// 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
|
||||
// quote as the encapsulator.
|
||||
|
||||
final String code = "" + " , , \n" // 1)
|
||||
+ " \t , , \n" // 2)
|
||||
+ " // , /, , /,\n" // 3)
|
||||
+ "";
|
||||
// @formatter:off
|
||||
final String code = "" + " , , \n" + // 1)
|
||||
" \t , , \n" + // 2)
|
||||
" // , /, , /,\n" + // 3)
|
||||
"";
|
||||
final String[][] res = {{" ", " ", " "}, // 1
|
||||
{" \t ", " ", " "}, // 2
|
||||
{" / ", " , ", " ,"}, // 3
|
||||
};
|
||||
|
||||
// @formatter:on
|
||||
final CSVFormat format = CSVFormat.newFormat(',').withRecordSeparator(CRLF).withEscape('/').withIgnoreEmptyLines();
|
||||
|
||||
try (final CSVParser parser = CSVParser.parse(code, format)) {
|
||||
final List<CSVRecord> records = parser.getRecords();
|
||||
assertFalse(records.isEmpty());
|
||||
|
||||
Utils.compare("", res, records);
|
||||
}
|
||||
}
|
||||
|
@ -196,10 +195,13 @@ public class CSVParserTest {
|
|||
@Test
|
||||
@Disabled
|
||||
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"
|
||||
+ "\"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
|
||||
// escapes a delimiter (",")
|
||||
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\"";
|
||||
final String[][] res = { { "one", "two", "three" }, { "on\\\"e", "two" }, { "on\"e", "two" }, { "one", "tw\"o" }, { "one", "t\\,wo" }, // 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" } // backslash in quotes only escapes a delimiter (",")
|
||||
|
@ -402,28 +404,25 @@ public class CSVParserTest {
|
|||
|
||||
@Test
|
||||
public void testDefaultFormat() throws IOException {
|
||||
final String code = "" + "a,b#\n" // 1)
|
||||
+ "\"\n\",\" \",#\n" // 2)
|
||||
+ "#,\"\"\n" // 3)
|
||||
+ "# Final comment\n"// 4)
|
||||
// @formatter:off
|
||||
final String code = "" + "a,b#\n" + // 1)
|
||||
"\"\n\",\" \",#\n" + // 2)
|
||||
"#,\"\"\n" + // 3)
|
||||
"# Final comment\n" // 4)
|
||||
;
|
||||
// @formatter:on
|
||||
final String[][] res = { { "a", "b#" }, { "\n", " ", "#" }, { "#", "" }, { "# Final comment" } };
|
||||
|
||||
CSVFormat format = CSVFormat.DEFAULT;
|
||||
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)) {
|
||||
final List<CSVRecord> records = parser.getRecords();
|
||||
assertFalse(records.isEmpty());
|
||||
|
||||
Utils.compare("Failed to parse without comments", res, records);
|
||||
|
||||
format = CSVFormat.DEFAULT.withCommentMarker('#');
|
||||
}
|
||||
try (final CSVParser parser = CSVParser.parse(code, format)) {
|
||||
final List<CSVRecord> records = parser.getRecords();
|
||||
|
||||
Utils.compare("Failed to parse with comments", res_comments, records);
|
||||
}
|
||||
}
|
||||
|
@ -611,7 +610,8 @@ public class CSVParserTest {
|
|||
|
||||
@Test
|
||||
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<>();
|
||||
for (final CSVRecord record : parser) {
|
||||
records.add(record);
|
||||
|
@ -782,7 +782,8 @@ public class CSVParserTest {
|
|||
@Test
|
||||
public void testGetOneLineOneParser() throws IOException {
|
||||
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(format.getRecordSeparator());
|
||||
final CSVRecord record1 = parser.nextRecord();
|
||||
|
@ -949,10 +950,8 @@ public class CSVParserTest {
|
|||
@Test
|
||||
public void testHeaderComment() throws Exception {
|
||||
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)) {
|
||||
final Iterator<CSVRecord> records = parser.iterator();
|
||||
|
||||
for (int i = 0; i < 2; i++) {
|
||||
assertTrue(records.hasNext());
|
||||
final CSVRecord record = records.next();
|
||||
|
@ -960,7 +959,6 @@ public class CSVParserTest {
|
|||
assertEquals(record.get(1), record.get("b"));
|
||||
assertEquals(record.get(2), record.get("c"));
|
||||
}
|
||||
|
||||
assertFalse(records.hasNext());
|
||||
}
|
||||
}
|
||||
|
@ -968,17 +966,14 @@ public class CSVParserTest {
|
|||
@Test
|
||||
public void testHeaderMissing() throws Exception {
|
||||
final Reader in = new StringReader("a,,c\n1,2,3\nx,y,z");
|
||||
|
||||
try (final CSVParser parser = CSVFormat.DEFAULT.withHeader().withAllowMissingColumnNames().parse(in)) {
|
||||
final Iterator<CSVRecord> records = parser.iterator();
|
||||
|
||||
for (int i = 0; i < 2; i++) {
|
||||
assertTrue(records.hasNext());
|
||||
final CSVRecord record = records.next();
|
||||
assertEquals(record.get(0), record.get("a"));
|
||||
assertEquals(record.get(2), record.get("c"));
|
||||
}
|
||||
|
||||
assertFalse(records.hasNext());
|
||||
}
|
||||
}
|
||||
|
@ -1034,7 +1029,8 @@ public class CSVParserTest {
|
|||
assertEquals("1", record.get("one"));
|
||||
assertEquals("2", record.get("two"));
|
||||
assertEquals("3", record.get("THREE"));
|
||||
}}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIgnoreEmptyLines() throws IOException {
|
||||
|
@ -1055,10 +1051,8 @@ public class CSVParserTest {
|
|||
@Test
|
||||
public void testIterator() throws Exception {
|
||||
final Reader in = new StringReader("a,b,c\n1,2,3\nx,y,z");
|
||||
|
||||
try (final CSVParser parser = CSVFormat.DEFAULT.parse(in)) {
|
||||
final Iterator<CSVRecord> iterator = parser.iterator();
|
||||
|
||||
assertTrue(iterator.hasNext());
|
||||
assertThrows(UnsupportedOperationException.class, iterator::remove);
|
||||
assertArrayEquals(new String[] { "a", "b", "c" }, iterator.next().values());
|
||||
|
@ -1068,17 +1062,15 @@ public class CSVParserTest {
|
|||
assertTrue(iterator.hasNext());
|
||||
assertArrayEquals(new String[] { "x", "y", "z" }, iterator.next().values());
|
||||
assertFalse(iterator.hasNext());
|
||||
|
||||
assertThrows(NoSuchElementException.class, iterator::next);
|
||||
}}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIteratorSequenceBreaking() throws IOException {
|
||||
final String fiveRows = "1\n2\n3\n4\n5\n";
|
||||
|
||||
// Iterator hasNext() shouldn't break sequence
|
||||
try (CSVParser parser = CSVFormat.DEFAULT.parse(new StringReader(fiveRows))) {
|
||||
|
||||
final Iterator<CSVRecord> iter = parser.iterator();
|
||||
int recordNumber = 0;
|
||||
while (iter.hasNext()) {
|
||||
|
@ -1096,7 +1088,6 @@ public class CSVParserTest {
|
|||
assertEquals(String.valueOf(recordNumber), record.get(0));
|
||||
}
|
||||
}
|
||||
|
||||
// Consecutive enhanced for loops shouldn't break sequence
|
||||
try (CSVParser parser = CSVFormat.DEFAULT.parse(new StringReader(fiveRows))) {
|
||||
int recordNumber = 0;
|
||||
|
@ -1112,7 +1103,6 @@ public class CSVParserTest {
|
|||
assertEquals(String.valueOf(recordNumber), record.get(0));
|
||||
}
|
||||
}
|
||||
|
||||
// Consecutive enhanced for loops with hasNext() peeking shouldn't break sequence
|
||||
try (CSVParser parser = CSVFormat.DEFAULT.parse(new StringReader(fiveRows))) {
|
||||
int recordNumber = 0;
|
||||
|
@ -1146,7 +1136,6 @@ public class CSVParserTest {
|
|||
try (final CSVParser parser = CSVFormat.DEFAULT.withHeader("A", "B", "C").withSkipHeaderRecord().parse(in)) {
|
||||
final Iterator<CSVRecord> records = parser.iterator();
|
||||
CSVRecord record;
|
||||
|
||||
// 1st record
|
||||
record = records.next();
|
||||
assertTrue(record.isMapped("A"));
|
||||
|
@ -1158,7 +1147,6 @@ public class CSVParserTest {
|
|||
assertEquals("1", record.get("A"));
|
||||
assertEquals("2", record.get("B"));
|
||||
assertFalse(record.isConsistent());
|
||||
|
||||
// 2nd record
|
||||
record = records.next();
|
||||
assertTrue(record.isMapped("A"));
|
||||
|
@ -1171,7 +1159,7 @@ public class CSVParserTest {
|
|||
assertEquals("y", record.get("B"));
|
||||
assertEquals("z", record.get("C"));
|
||||
assertTrue(record.isConsistent());
|
||||
|
||||
// end
|
||||
assertFalse(records.hasNext());
|
||||
}
|
||||
}
|
||||
|
@ -1455,7 +1443,8 @@ public class CSVParserTest {
|
|||
@SuppressWarnings("resource")
|
||||
final CSVParser recordParser = record.getParser();
|
||||
assertEquals(Arrays.asList("header1", "header2", "header1"), recordParser.getHeaderNames());
|
||||
}}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRoundtrip() throws Exception {
|
||||
|
@ -1491,7 +1480,8 @@ public class CSVParserTest {
|
|||
assertEquals("1", record.get("X"));
|
||||
assertEquals("2", record.get("Y"));
|
||||
assertEquals("3", record.get("Z"));
|
||||
}}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
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[] { "1", "2", "3" }, list.get(1).values());
|
||||
assertArrayEquals(new String[] { "x", "y", "z" }, list.get(2).values());
|
||||
}}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testThrowExceptionWithLineAndPosition() throws IOException {
|
||||
|
@ -1587,7 +1578,8 @@ public class CSVParserTest {
|
|||
assertEquals("2", record.get("Y"));
|
||||
assertEquals("3", record.get("Z"));
|
||||
assertEquals(3, record.size());
|
||||
}}
|
||||
}
|
||||
}
|
||||
|
||||
private void validateLineNumbers(final String lineSeparator) throws IOException {
|
||||
try (final CSVParser parser = CSVParser.parse("a" + lineSeparator + "b" + lineSeparator + "c", CSVFormat.DEFAULT.withRecordSeparator(lineSeparator))) {
|
||||
|
|
|
@ -314,8 +314,8 @@ public class CSVPrinterTest {
|
|||
try (final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withCommentMarker('#'))) {
|
||||
printer.print(value);
|
||||
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"
|
||||
+ recordSeparator + "# " + recordSeparator, sw.toString());
|
||||
assertEquals("abc" + recordSeparator + "# This is a comment" + recordSeparator + "# on multiple lines" + recordSeparator +
|
||||
"# this is next comment" + recordSeparator + "# " + recordSeparator, sw.toString());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -729,8 +729,8 @@ public class CSVPrinterTest {
|
|||
}
|
||||
}
|
||||
final String csv = sw.toString();
|
||||
assertEquals("1,r1,\"long text 1\",\"YmluYXJ5IGRhdGEgMQ==\r\n\"" + recordSeparator + "2,r2,\"" + longText2 + "\",\"YmluYXJ5IGRhdGEgMg==\r\n\""
|
||||
+ recordSeparator, csv);
|
||||
assertEquals("1,r1,\"long text 1\",\"YmluYXJ5IGRhdGEgMQ==\r\n\"" + recordSeparator + "2,r2,\"" + longText2 + "\",\"YmluYXJ5IGRhdGEgMg==\r\n\"" +
|
||||
recordSeparator, csv);
|
||||
// Round trip the data
|
||||
try (StringReader reader = new StringReader(csv);
|
||||
final CSVParser csvParser = csvFormat.parse(reader)) {
|
||||
|
@ -1349,11 +1349,12 @@ public class CSVPrinterTest {
|
|||
|
||||
@Test
|
||||
public void testPrintCSVParser() throws IOException {
|
||||
final String code = "a1,b1\n" // 1)
|
||||
+ "a2,b2\n" // 2)
|
||||
+ "a3,b3\n" // 3)
|
||||
+ "a4,b4\n"// 4)
|
||||
;
|
||||
// @formatter:off
|
||||
final String code = "a1,b1\n" + // 1)
|
||||
"a2,b2\n" + // 2)
|
||||
"a3,b3\n" + // 3)
|
||||
"a4,b4\n"; // 4)
|
||||
// @formatter:on
|
||||
final String[][] res = { { "a1", "b1" }, { "a2", "b2" }, { "a3", "b3" }, { "a4", "b4" } };
|
||||
final CSVFormat format = CSVFormat.DEFAULT;
|
||||
final StringWriter sw = new StringWriter();
|
||||
|
@ -1370,11 +1371,12 @@ public class CSVPrinterTest {
|
|||
|
||||
@Test
|
||||
public void testPrintCSVRecord() throws IOException {
|
||||
final String code = "a1,b1\n" // 1)
|
||||
+ "a2,b2\n" // 2)
|
||||
+ "a3,b3\n" // 3)
|
||||
+ "a4,b4\n"// 4)
|
||||
;
|
||||
// @formatter:off
|
||||
final String code = "a1,b1\n" + // 1)
|
||||
"a2,b2\n" + // 2)
|
||||
"a3,b3\n" + // 3)
|
||||
"a4,b4\n"; // 4)
|
||||
// @formatter:on
|
||||
final String[][] res = { { "a1", "b1" }, { "a2", "b2" }, { "a3", "b3" }, { "a4", "b4" } };
|
||||
final CSVFormat format = CSVFormat.DEFAULT;
|
||||
final StringWriter sw = new StringWriter();
|
||||
|
@ -1393,11 +1395,12 @@ public class CSVPrinterTest {
|
|||
|
||||
@Test
|
||||
public void testPrintCSVRecords() throws IOException {
|
||||
final String code = "a1,b1\n" // 1)
|
||||
+ "a2,b2\n" // 2)
|
||||
+ "a3,b3\n" // 3)
|
||||
+ "a4,b4\n"// 4)
|
||||
;
|
||||
// @formatter:off
|
||||
final String code = "a1,b1\n" + // 1)
|
||||
"a2,b2\n" + // 2)
|
||||
"a3,b3\n" + // 3)
|
||||
"a4,b4\n"; // 4)
|
||||
// @formatter:on
|
||||
final String[][] res = { { "a1", "b1" }, { "a2", "b2" }, { "a3", "b3" }, { "a4", "b4" } };
|
||||
final CSVFormat format = CSVFormat.DEFAULT;
|
||||
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.
|
||||
*
|
||||
* <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
|
||||
* {@link Writer Writer} but some other Appendable.
|
||||
* 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}
|
||||
* but some other Appendable.
|
||||
* </p>
|
||||
*
|
||||
* @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.
|
||||
*
|
||||
* <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
|
||||
* {@link Writer Writer}.
|
||||
* 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}.
|
||||
* </p>
|
||||
*
|
||||
* @throws IOException Not expected to happen
|
||||
|
@ -1546,11 +1548,12 @@ public class CSVPrinterTest {
|
|||
|
||||
@Test
|
||||
public void testPrintRecordStream() throws IOException {
|
||||
final String code = "a1,b1\n" // 1)
|
||||
+ "a2,b2\n" // 2)
|
||||
+ "a3,b3\n" // 3)
|
||||
+ "a4,b4\n"// 4)
|
||||
;
|
||||
// @formatter:off
|
||||
final String code = "a1,b1\n" + // 1)
|
||||
"a2,b2\n" + // 2)
|
||||
"a3,b3\n" + // 3)
|
||||
"a4,b4\n"; // 4)
|
||||
// @formatter:on
|
||||
final String[][] res = { { "a1", "b1" }, { "a2", "b2" }, { "a3", "b3" }, { "a4", "b4" } };
|
||||
final CSVFormat format = CSVFormat.DEFAULT;
|
||||
final StringWriter sw = new StringWriter();
|
||||
|
|
|
@ -53,14 +53,14 @@ public class PerformanceTest {
|
|||
private static final class Stats {
|
||||
final int count;
|
||||
final int fields;
|
||||
|
||||
Stats(final int c, final int f) {
|
||||
count = c;
|
||||
fields = f;
|
||||
}
|
||||
}
|
||||
|
||||
private static final String[] PROPERTY_NAMES = {
|
||||
"java.version", // Java Runtime Environment version
|
||||
private static final String[] PROPERTY_NAMES = { "java.version", // Java Runtime Environment version
|
||||
"java.vendor", // Java Runtime Environment vendor
|
||||
// "java.vm.specification.version", // Java Virtual Machine specification version
|
||||
// "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());
|
||||
} else {
|
||||
System.out.println("Decompressing test fixture to: " + BIG_FILE + "...");
|
||||
try (
|
||||
final InputStream input = new GZIPInputStream(
|
||||
PerformanceTest.class.getClassLoader().getResourceAsStream(TEST_RESRC));
|
||||
try (final InputStream input = new GZIPInputStream(PerformanceTest.class.getClassLoader().getResourceAsStream(TEST_RESRC));
|
||||
final OutputStream output = new FileOutputStream(BIG_FILE)) {
|
||||
IOUtils.copy(input, output);
|
||||
System.out.println(String.format("Decompressed test fixture %s: %,d bytes.", BIG_FILE, BIG_FILE.length()));
|
||||
|
@ -341,5 +339,5 @@ public class PerformanceTest {
|
|||
}
|
||||
show();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -33,18 +33,16 @@ public class JiraCsv148Test {
|
|||
.build();
|
||||
// @formatter:on
|
||||
assertEquals(
|
||||
"\"\",\" \",\" Single space on the left\",\"Single space on the right \","
|
||||
+ "\" Single spaces on both sides \",\" Multiple spaces on the left\","
|
||||
+ "\"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 ", " Multiple spaces on the left", "Multiple spaces on the right ",
|
||||
" Multiple spaces on both sides "));
|
||||
"\"\",\" \",\" Single space on the left\",\"Single space on the right \"," +
|
||||
"\" Single spaces on both sides \",\" Multiple spaces on the left\"," +
|
||||
"\"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 ",
|
||||
" Multiple spaces on the left", "Multiple spaces on the right ", " Multiple spaces on both sides "));
|
||||
}
|
||||
|
||||
/**
|
||||
* The difference between withTrim()and withIgnoreSurroundingSpace(): difference: withTrim() can remove the leading
|
||||
* and trailing spaces and newlines in quotation marks, while withIgnoreSurroundingSpace() cannot The same point:
|
||||
* you can remove the leading and trailing spaces, tabs and other symbols.
|
||||
* The difference between withTrim()and withIgnoreSurroundingSpace(): difference: withTrim() can remove the leading and trailing spaces and newlines in
|
||||
* quotation marks, while withIgnoreSurroundingSpace() cannot The same point: you can remove the leading and trailing spaces, tabs and other symbols.
|
||||
*/
|
||||
@Test
|
||||
public void testWithTrimEmpty() {
|
||||
|
@ -55,11 +53,9 @@ public class JiraCsv148Test {
|
|||
.build();
|
||||
// @formatter:on
|
||||
assertEquals(
|
||||
"\"\",\"\",\"Single space on the left\",\"Single space on the right\","
|
||||
+ "\"Single spaces on both sides\",\"Multiple spaces on the left\","
|
||||
+ "\"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 ", " Multiple spaces on the left", "Multiple spaces on the right ",
|
||||
" Multiple spaces on both sides "));
|
||||
"\"\",\"\",\"Single space on the left\",\"Single space on the right\"," + "\"Single spaces on both sides\",\"Multiple spaces on the left\"," +
|
||||
"\"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 ",
|
||||
" Multiple spaces on the left", "Multiple spaces on the right ", " Multiple spaces on both sides "));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -49,8 +49,12 @@ public class JiraCsv206Test {
|
|||
assertEquals("123 Main St.", record.get(2));
|
||||
}
|
||||
// Write with multiple character delimiter
|
||||
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:off
|
||||
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]";
|
||||
// @formatter:off
|
||||
final CSVFormat format = CSVFormat.EXCEL.builder()
|
||||
|
|
|
@ -36,13 +36,14 @@ public class JiraCsv265Test {
|
|||
@Test
|
||||
public void testCharacterPositionWithComments() throws IOException {
|
||||
// @formatter:off
|
||||
final String csv = "# Comment1\n"
|
||||
+ "Header1,Header2\n"
|
||||
+ "# Comment2\n"
|
||||
+ "Value1,Value2\n"
|
||||
+ "# Comment3\n"
|
||||
+ "Value3,Value4\n"
|
||||
+ "# Comment4\n";
|
||||
final String csv =
|
||||
"# Comment1\n" +
|
||||
"Header1,Header2\n" +
|
||||
"# Comment2\n" +
|
||||
"Value1,Value2\n" +
|
||||
"# Comment3\n" +
|
||||
"Value3,Value4\n" +
|
||||
"# Comment4\n";
|
||||
final CSVFormat csvFormat = CSVFormat.DEFAULT.builder()
|
||||
.setCommentMarker('#')
|
||||
.setHeader()
|
||||
|
@ -61,15 +62,16 @@ public class JiraCsv265Test {
|
|||
@Test
|
||||
public void testCharacterPositionWithCommentsSpanningMultipleLines() throws IOException {
|
||||
// @formatter:off
|
||||
final String csv = "# Comment1\n"
|
||||
+ "# Comment2\n"
|
||||
+ "Header1,Header2\n"
|
||||
+ "# Comment3\n"
|
||||
+ "# Comment4\n"
|
||||
+ "Value1,Value2\n"
|
||||
+ "# Comment5\n"
|
||||
+ "# Comment6\n"
|
||||
+ "Value3,Value4";
|
||||
final String csv =
|
||||
"# Comment1\n" +
|
||||
"# Comment2\n" +
|
||||
"Header1,Header2\n" +
|
||||
"# Comment3\n" +
|
||||
"# Comment4\n" +
|
||||
"Value1,Value2\n" +
|
||||
"# Comment5\n" +
|
||||
"# Comment6\n" +
|
||||
"Value3,Value4";
|
||||
final CSVFormat csvFormat = CSVFormat.DEFAULT.builder()
|
||||
.setCommentMarker('#')
|
||||
.setHeader()
|
||||
|
|
Loading…
Reference in New Issue