This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-csv.git
The following commit(s) were added to refs/heads/master by this push:
new 46f9211 Sort members.
46f9211 is described below
commit 46f9211d604e4c47d05f370c34cd18e04b76f23d
Author: Gary Gregory <[email protected]>
AuthorDate: Sat Jul 24 11:21:30 2021 -0400
Sort members.
---
.../apache/commons/csv/ExtendedBufferedReader.java | 26 +-
.../org/apache/commons/csv/CSVFileParserTest.java | 10 +-
.../commons/csv/CSVFormatPredefinedTest.java | 20 +-
.../java/org/apache/commons/csv/CSVRecordTest.java | 18 +-
.../java/org/apache/commons/csv/LexerTest.java | 370 ++++++++++-----------
.../org/apache/commons/csv/PerformanceTest.java | 230 ++++++-------
.../java/org/apache/commons/csv/TokenMatchers.java | 24 +-
.../org/apache/commons/csv/TokenMatchersTest.java | 12 +-
src/test/java/org/apache/commons/csv/Utils.java | 6 +-
.../apache/commons/csv/issues/JiraCsv148Test.java | 30 +-
.../apache/commons/csv/issues/JiraCsv167Test.java | 10 +-
.../apache/commons/csv/issues/JiraCsv203Test.java | 26 +-
.../apache/commons/csv/issues/JiraCsv248Test.java | 8 +-
.../apache/commons/csv/perf/PerformanceTest.java | 6 +-
14 files changed, 398 insertions(+), 398 deletions(-)
diff --git a/src/main/java/org/apache/commons/csv/ExtendedBufferedReader.java
b/src/main/java/org/apache/commons/csv/ExtendedBufferedReader.java
index 1067dd2..89e63e3 100644
--- a/src/main/java/org/apache/commons/csv/ExtendedBufferedReader.java
+++ b/src/main/java/org/apache/commons/csv/ExtendedBufferedReader.java
@@ -123,19 +123,6 @@ final class ExtendedBufferedReader extends BufferedReader {
}
/**
- * Returns the next n characters in the current reader without consuming
them. The next call to {@link #read()} will still return the next value. This
- * doesn't affect line number or last character.
- *
- * @param n the number characters look ahead.
- * @return the next n characters.
- * @throws IOException If an I/O error occurs
- */
- char[] lookAhead(final int n) throws IOException {
- final char[] buf = new char[n];
- return lookAhead(buf);
- }
-
- /**
* Populates the buffer with the next {@code buf.length} characters in the
* current reader without consuming them. The next call to {@link #read()}
will
* still return the next value. This doesn't affect line number or last
@@ -154,6 +141,19 @@ final class ExtendedBufferedReader extends BufferedReader {
return buf;
}
+ /**
+ * Returns the next n characters in the current reader without consuming
them. The next call to {@link #read()} will still return the next value. This
+ * doesn't affect line number or last character.
+ *
+ * @param n the number characters look ahead.
+ * @return the next n characters.
+ * @throws IOException If an I/O error occurs
+ */
+ char[] lookAhead(final int n) throws IOException {
+ final char[] buf = new char[n];
+ return lookAhead(buf);
+ }
+
@Override
public int read() throws IOException {
final int current = super.read();
diff --git a/src/test/java/org/apache/commons/csv/CSVFileParserTest.java
b/src/test/java/org/apache/commons/csv/CSVFileParserTest.java
index 96ac874..4d9b871 100644
--- a/src/test/java/org/apache/commons/csv/CSVFileParserTest.java
+++ b/src/test/java/org/apache/commons/csv/CSVFileParserTest.java
@@ -42,6 +42,11 @@ public class CSVFileParserTest {
private static final File BASE_DIR = new
File("src/test/resources/org/apache/commons/csv/CSVFileParser");
+ public static Stream<File> generateData() {
+ final File[] files = BASE_DIR.listFiles((dir, name) ->
name.startsWith("test") && name.endsWith(".txt"));
+ return files != null ? Stream.of(files) : Stream.empty();
+ }
+
private String readTestData(final BufferedReader reader) throws
IOException {
String line;
do {
@@ -50,11 +55,6 @@ public class CSVFileParserTest {
return line;
}
- public static Stream<File> generateData() {
- final File[] files = BASE_DIR.listFiles((dir, name) ->
name.startsWith("test") && name.endsWith(".txt"));
- return files != null ? Stream.of(files) : Stream.empty();
- }
-
@ParameterizedTest
@MethodSource("generateData")
public void testCSVFile(final File testFile) throws Exception {
diff --git a/src/test/java/org/apache/commons/csv/CSVFormatPredefinedTest.java
b/src/test/java/org/apache/commons/csv/CSVFormatPredefinedTest.java
index a3e6ecd..d62f41f 100644
--- a/src/test/java/org/apache/commons/csv/CSVFormatPredefinedTest.java
+++ b/src/test/java/org/apache/commons/csv/CSVFormatPredefinedTest.java
@@ -42,28 +42,28 @@ public class CSVFormatPredefinedTest {
}
@Test
- public void testMySQL() {
- test(CSVFormat.MYSQL, "MySQL");
+ public void testMongoDbCsv() {
+ test(CSVFormat.MONGODB_CSV, "MongoDBCsv");
}
@Test
- public void testOracle() {
- test(CSVFormat.ORACLE, "Oracle");
+ public void testMongoDbTsv() {
+ test(CSVFormat.MONGODB_TSV, "MongoDBTsv");
}
@Test
- public void testPostgreSqlCsv() {
- test(CSVFormat.POSTGRESQL_CSV, "PostgreSQLCsv");
+ public void testMySQL() {
+ test(CSVFormat.MYSQL, "MySQL");
}
@Test
- public void testMongoDbCsv() {
- test(CSVFormat.MONGODB_CSV, "MongoDBCsv");
+ public void testOracle() {
+ test(CSVFormat.ORACLE, "Oracle");
}
@Test
- public void testMongoDbTsv() {
- test(CSVFormat.MONGODB_TSV, "MongoDBTsv");
+ public void testPostgreSqlCsv() {
+ test(CSVFormat.POSTGRESQL_CSV, "PostgreSQLCsv");
}
@Test
diff --git a/src/test/java/org/apache/commons/csv/CSVRecordTest.java
b/src/test/java/org/apache/commons/csv/CSVRecordTest.java
index a7f3bcd..39b02ba 100644
--- a/src/test/java/org/apache/commons/csv/CSVRecordTest.java
+++ b/src/test/java/org/apache/commons/csv/CSVRecordTest.java
@@ -265,6 +265,15 @@ public class CSVRecordTest {
}
@Test
+ public void testStream() {
+ final AtomicInteger i = new AtomicInteger();
+ record.stream().forEach(value -> {
+ assertEquals(values[i.get()], value);
+ i.incrementAndGet();
+ });
+ }
+
+ @Test
public void testToList() {
int i = 0;
for (final String value : record.toList()) {
@@ -298,15 +307,6 @@ public class CSVRecordTest {
}
@Test
- public void testStream() {
- final AtomicInteger i = new AtomicInteger();
- record.stream().forEach(value -> {
- assertEquals(values[i.get()], value);
- i.incrementAndGet();
- });
- }
-
- @Test
public void testToString() {
assertNotNull(recordWithHeader.toString());
assertTrue(recordWithHeader.toString().contains("comment="));
diff --git a/src/test/java/org/apache/commons/csv/LexerTest.java
b/src/test/java/org/apache/commons/csv/LexerTest.java
index b289773..cc8d728 100644
--- a/src/test/java/org/apache/commons/csv/LexerTest.java
+++ b/src/test/java/org/apache/commons/csv/LexerTest.java
@@ -47,60 +47,62 @@ public class LexerTest {
private CSVFormat formatWithEscaping;
- @BeforeEach
- public void setUp() {
- formatWithEscaping = CSVFormat.DEFAULT.withEscape('\\');
- }
-
@SuppressWarnings("resource")
private Lexer createLexer(final String input, final CSVFormat format) {
return new Lexer(format, new ExtendedBufferedReader(new
StringReader(input)));
}
+ @BeforeEach
+ public void setUp() {
+ formatWithEscaping = CSVFormat.DEFAULT.withEscape('\\');
+ }
+
+ // simple token with escaping enabled
@Test
- public void testSurroundingSpacesAreDeleted() throws IOException {
- final String code = "noSpaces, leadingSpaces,trailingSpaces ,
surroundingSpaces , ,,";
- try (final Lexer parser = createLexer(code,
CSVFormat.DEFAULT.withIgnoreSurroundingSpaces())) {
- assertThat(parser.nextToken(new Token()), matches(TOKEN,
"noSpaces"));
- assertThat(parser.nextToken(new Token()), matches(TOKEN,
"leadingSpaces"));
- assertThat(parser.nextToken(new Token()), matches(TOKEN,
"trailingSpaces"));
- assertThat(parser.nextToken(new Token()), matches(TOKEN,
"surroundingSpaces"));
- assertThat(parser.nextToken(new Token()), matches(TOKEN, ""));
- assertThat(parser.nextToken(new Token()), matches(TOKEN, ""));
- assertThat(parser.nextToken(new Token()), matches(EOF, ""));
+ public void testBackslashWithEscaping() throws IOException {
+ /*
+ * file: a,\,,b \,,
+ */
+ final String code = "a,\\,,b\\\\\n\\,,\\\nc,d\\\r\ne";
+ final CSVFormat format =
formatWithEscaping.withIgnoreEmptyLines(false);
+ assertTrue(format.isEscapeCharacterSet());
+ try (final Lexer parser = createLexer(code, format)) {
+ assertThat(parser.nextToken(new Token()), matches(TOKEN, "a"));
+ assertThat(parser.nextToken(new Token()), matches(TOKEN, ","));
+ assertThat(parser.nextToken(new Token()), matches(EORECORD,
"b\\"));
+ assertThat(parser.nextToken(new Token()), matches(TOKEN, ","));
+ assertThat(parser.nextToken(new Token()), matches(TOKEN, "\nc"));
+ assertThat(parser.nextToken(new Token()), matches(EORECORD,
"d\r"));
+ assertThat(parser.nextToken(new Token()), matches(EOF, "e"));
}
}
+ // simple token with escaping not enabled
@Test
- public void testSurroundingTabsAreDeleted() throws IOException {
- final String code =
"noTabs,\tleadingTab,trailingTab\t,\tsurroundingTabs\t,\t\t,,";
- try (final Lexer parser = createLexer(code,
CSVFormat.DEFAULT.withIgnoreSurroundingSpaces())) {
- assertThat(parser.nextToken(new Token()), matches(TOKEN,
"noTabs"));
- assertThat(parser.nextToken(new Token()), matches(TOKEN,
"leadingTab"));
- assertThat(parser.nextToken(new Token()), matches(TOKEN,
"trailingTab"));
- assertThat(parser.nextToken(new Token()), matches(TOKEN,
"surroundingTabs"));
+ public void testBackslashWithoutEscaping() throws IOException {
+ /*
+ * file: a,\,,b \,,
+ */
+ final String code = "a,\\,,b\\\n\\,,";
+ final CSVFormat format = CSVFormat.DEFAULT;
+ assertFalse(format.isEscapeCharacterSet());
+ try (final Lexer parser = createLexer(code, format)) {
+ assertThat(parser.nextToken(new Token()), matches(TOKEN, "a"));
+ // an unquoted single backslash is not an escape char
+ assertThat(parser.nextToken(new Token()), matches(TOKEN, "\\"));
assertThat(parser.nextToken(new Token()), matches(TOKEN, ""));
+ assertThat(parser.nextToken(new Token()), matches(EORECORD,
"b\\"));
+ // an unquoted single backslash is not an escape char
+ assertThat(parser.nextToken(new Token()), matches(TOKEN, "\\"));
assertThat(parser.nextToken(new Token()), matches(TOKEN, ""));
assertThat(parser.nextToken(new Token()), matches(EOF, ""));
}
}
@Test
- public void testIgnoreEmptyLines() throws IOException {
- final String code = "first,line,\n" + "\n" + "\n" + "second,line\n" +
"\n" + "\n" + "third line \n" + "\n" +
- "\n" + "last, line \n" + "\n" + "\n" + "\n";
- final CSVFormat format = CSVFormat.DEFAULT.withIgnoreEmptyLines();
- try (final Lexer parser = createLexer(code, format)) {
- assertThat(parser.nextToken(new Token()), matches(TOKEN, "first"));
- assertThat(parser.nextToken(new Token()), matches(TOKEN, "line"));
- assertThat(parser.nextToken(new Token()), matches(EORECORD, ""));
- assertThat(parser.nextToken(new Token()), matches(TOKEN,
"second"));
- assertThat(parser.nextToken(new Token()), matches(EORECORD,
"line"));
- assertThat(parser.nextToken(new Token()), matches(EORECORD, "third
line "));
- assertThat(parser.nextToken(new Token()), matches(TOKEN, "last"));
- assertThat(parser.nextToken(new Token()), matches(EORECORD, " line
"));
- assertThat(parser.nextToken(new Token()), matches(EOF, ""));
- assertThat(parser.nextToken(new Token()), matches(EOF, ""));
+ public void testBackspace() throws Exception {
+ try (final Lexer lexer = createLexer("character" + BACKSPACE +
"NotEscaped", formatWithEscaping)) {
+ assertThat(lexer.nextToken(new Token()), hasContent("character" +
BACKSPACE + "NotEscaped"));
}
}
@@ -173,110 +175,54 @@ public class LexerTest {
}
}
- // simple token with escaping not enabled
@Test
- public void testBackslashWithoutEscaping() throws IOException {
- /*
- * file: a,\,,b \,,
- */
- final String code = "a,\\,,b\\\n\\,,";
- final CSVFormat format = CSVFormat.DEFAULT;
- assertFalse(format.isEscapeCharacterSet());
- try (final Lexer parser = createLexer(code, format)) {
- assertThat(parser.nextToken(new Token()), matches(TOKEN, "a"));
- // an unquoted single backslash is not an escape char
- assertThat(parser.nextToken(new Token()), matches(TOKEN, "\\"));
- assertThat(parser.nextToken(new Token()), matches(TOKEN, ""));
- assertThat(parser.nextToken(new Token()), matches(EORECORD,
"b\\"));
- // an unquoted single backslash is not an escape char
- assertThat(parser.nextToken(new Token()), matches(TOKEN, "\\"));
- assertThat(parser.nextToken(new Token()), matches(TOKEN, ""));
- assertThat(parser.nextToken(new Token()), matches(EOF, ""));
+ public void testCR() throws Exception {
+ try (final Lexer lexer = createLexer("character" + CR + "NotEscaped",
formatWithEscaping)) {
+ assertThat(lexer.nextToken(new Token()), hasContent("character"));
+ assertThat(lexer.nextToken(new Token()), hasContent("NotEscaped"));
}
}
- // simple token with escaping enabled
+ // From CSV-1
@Test
- public void testBackslashWithEscaping() throws IOException {
- /*
- * file: a,\,,b \,,
- */
- final String code = "a,\\,,b\\\\\n\\,,\\\nc,d\\\r\ne";
- final CSVFormat format =
formatWithEscaping.withIgnoreEmptyLines(false);
- assertTrue(format.isEscapeCharacterSet());
- try (final Lexer parser = createLexer(code, format)) {
- assertThat(parser.nextToken(new Token()), matches(TOKEN, "a"));
- assertThat(parser.nextToken(new Token()), matches(TOKEN, ","));
- assertThat(parser.nextToken(new Token()), matches(EORECORD,
"b\\"));
- assertThat(parser.nextToken(new Token()), matches(TOKEN, ","));
- assertThat(parser.nextToken(new Token()), matches(TOKEN, "\nc"));
- assertThat(parser.nextToken(new Token()), matches(EORECORD,
"d\r"));
- assertThat(parser.nextToken(new Token()), matches(EOF, "e"));
+ public void testDelimiterIsWhitespace() throws IOException {
+ final String code = "one\ttwo\t\tfour \t five\t six";
+ try (final Lexer parser = createLexer(code, CSVFormat.TDF)) {
+ assertThat(parser.nextToken(new Token()), matches(TOKEN, "one"));
+ assertThat(parser.nextToken(new Token()), matches(TOKEN, "two"));
+ assertThat(parser.nextToken(new Token()), matches(TOKEN, ""));
+ assertThat(parser.nextToken(new Token()), matches(TOKEN, "four"));
+ assertThat(parser.nextToken(new Token()), matches(TOKEN, "five"));
+ assertThat(parser.nextToken(new Token()), matches(EOF, "six"));
}
}
- // encapsulator tokenizer (single line)
- @Test
- public void testNextToken4() throws IOException {
- /*
- * file: a,"foo",b a, " foo",b a,"foo " ,b // whitespace after closing
encapsulator a, " foo " ,b
- */
- final String code = "a,\"foo\",b\na, \" foo\",b\na,\"foo \" ,b\na,
\" foo \" ,b";
- try (final Lexer parser = createLexer(code,
CSVFormat.DEFAULT.withIgnoreSurroundingSpaces())) {
- assertThat(parser.nextToken(new Token()), matches(TOKEN, "a"));
- assertThat(parser.nextToken(new Token()), matches(TOKEN, "foo"));
- assertThat(parser.nextToken(new Token()), matches(EORECORD, "b"));
- assertThat(parser.nextToken(new Token()), matches(TOKEN, "a"));
- assertThat(parser.nextToken(new Token()), matches(TOKEN, " foo"));
- assertThat(parser.nextToken(new Token()), matches(EORECORD, "b"));
- assertThat(parser.nextToken(new Token()), matches(TOKEN, "a"));
- assertThat(parser.nextToken(new Token()), matches(TOKEN, "foo "));
- assertThat(parser.nextToken(new Token()), matches(EORECORD, "b"));
- assertThat(parser.nextToken(new Token()), matches(TOKEN, "a"));
- assertThat(parser.nextToken(new Token()), matches(TOKEN, " foo "));
- // assertTokenEquals(EORECORD, "b", parser.nextToken(new Token()));
- assertThat(parser.nextToken(new Token()), matches(EOF, "b"));
+ @Test // TODO is this correct? Do we expect <esc>BACKSPACE to be unescaped?
+ public void testEscapedBackspace() throws Exception {
+ try (final Lexer lexer = createLexer("character\\" + BACKSPACE +
"Escaped", formatWithEscaping)) {
+ assertThat(lexer.nextToken(new Token()), hasContent("character" +
BACKSPACE + "Escaped"));
}
}
- // encapsulator tokenizer (multi line, delimiter in string)
@Test
- public void testNextToken5() throws IOException {
- final String code = "a,\"foo\n\",b\n\"foo\n baar ,,,\"\n\"\n\t \n\"";
- try (final Lexer parser = createLexer(code, CSVFormat.DEFAULT)) {
- assertThat(parser.nextToken(new Token()), matches(TOKEN, "a"));
- assertThat(parser.nextToken(new Token()), matches(TOKEN, "foo\n"));
- assertThat(parser.nextToken(new Token()), matches(EORECORD, "b"));
- assertThat(parser.nextToken(new Token()), matches(EORECORD, "foo\n
baar ,,,"));
- assertThat(parser.nextToken(new Token()), matches(EOF, "\n\t \n"));
+ public void testEscapedCharacter() throws Exception {
+ try (final Lexer lexer = createLexer("character\\aEscaped",
formatWithEscaping)) {
+ assertThat(lexer.nextToken(new Token()),
hasContent("character\\aEscaped"));
}
}
- // change delimiters, comment, encapsulater
@Test
- public void testNextToken6() throws IOException {
- /*
- * file: a;'b and \' more ' !comment;;;; ;;
- */
- final String code = "a;'b and '' more\n'\n!comment;;;;\n;;";
- final CSVFormat format =
CSVFormat.DEFAULT.withQuote('\'').withCommentMarker('!').withDelimiter(';');
- try (final Lexer parser = createLexer(code, format)) {
- assertThat(parser.nextToken(new Token()), matches(TOKEN, "a"));
- assertThat(parser.nextToken(new Token()), matches(EORECORD, "b and
' more\n"));
+ public void testEscapedControlCharacter() throws Exception {
+ // we are explicitly using an escape different from \ here
+ try (final Lexer lexer = createLexer("character!rEscaped",
CSVFormat.DEFAULT.withEscape('!'))) {
+ assertThat(lexer.nextToken(new Token()), hasContent("character" +
CR + "Escaped"));
}
}
- // From CSV-1
@Test
- public void testDelimiterIsWhitespace() throws IOException {
- final String code = "one\ttwo\t\tfour \t five\t six";
- try (final Lexer parser = createLexer(code, CSVFormat.TDF)) {
- assertThat(parser.nextToken(new Token()), matches(TOKEN, "one"));
- assertThat(parser.nextToken(new Token()), matches(TOKEN, "two"));
- assertThat(parser.nextToken(new Token()), matches(TOKEN, ""));
- assertThat(parser.nextToken(new Token()), matches(TOKEN, "four"));
- assertThat(parser.nextToken(new Token()), matches(TOKEN, "five"));
- assertThat(parser.nextToken(new Token()), matches(EOF, "six"));
+ public void testEscapedControlCharacter2() throws Exception {
+ try (final Lexer lexer = createLexer("character\\rEscaped",
CSVFormat.DEFAULT.withEscape('\\'))) {
+ assertThat(lexer.nextToken(new Token()), hasContent("character" +
CR + "Escaped"));
}
}
@@ -287,11 +233,10 @@ public class LexerTest {
}
}
- @Test
- public void testCR() throws Exception {
- try (final Lexer lexer = createLexer("character" + CR + "NotEscaped",
formatWithEscaping)) {
- assertThat(lexer.nextToken(new Token()), hasContent("character"));
- assertThat(lexer.nextToken(new Token()), hasContent("NotEscaped"));
+ @Test // TODO is this correct? Do we expect <esc>FF to be unescaped?
+ public void testEscapedFF() throws Exception {
+ try (final Lexer lexer = createLexer("character\\" + FF + "Escaped",
formatWithEscaping)) {
+ assertThat(lexer.nextToken(new Token()), hasContent("character" +
FF + "Escaped"));
}
}
@@ -303,10 +248,10 @@ public class LexerTest {
}
@Test
- public void testLF() throws Exception {
- try (final Lexer lexer = createLexer("character" + LF + "NotEscaped",
formatWithEscaping)) {
- assertThat(lexer.nextToken(new Token()), hasContent("character"));
- assertThat(lexer.nextToken(new Token()), hasContent("NotEscaped"));
+ public void testEscapedMySqlNullValue() throws Exception {
+ // MySQL uses \N to symbolize null values. We have to restore this
+ try (final Lexer lexer = createLexer("character\\NEscaped",
formatWithEscaping)) {
+ assertThat(lexer.nextToken(new Token()),
hasContent("character\\NEscaped"));
}
}
@@ -319,84 +264,120 @@ public class LexerTest {
}
@Test
- public void testTab() throws Exception {
- try (final Lexer lexer = createLexer("character" + TAB + "NotEscaped",
formatWithEscaping)) {
- assertThat(lexer.nextToken(new Token()), hasContent("character" +
TAB + "NotEscaped"));
- }
- }
-
- @Test // TODO is this correct? Do we expect <esc>BACKSPACE to be unescaped?
- public void testEscapedBackspace() throws Exception {
- try (final Lexer lexer = createLexer("character\\" + BACKSPACE +
"Escaped", formatWithEscaping)) {
- assertThat(lexer.nextToken(new Token()), hasContent("character" +
BACKSPACE + "Escaped"));
+ public void testEscapingAtEOF() throws Exception {
+ final String code = "escaping at EOF is evil\\";
+ try (final Lexer lexer = createLexer(code, formatWithEscaping)) {
+ assertThrows(IOException.class, () -> lexer.nextToken(new
Token()));
}
}
@Test
- public void testBackspace() throws Exception {
- try (final Lexer lexer = createLexer("character" + BACKSPACE +
"NotEscaped", formatWithEscaping)) {
- assertThat(lexer.nextToken(new Token()), hasContent("character" +
BACKSPACE + "NotEscaped"));
+ public void testFF() throws Exception {
+ try (final Lexer lexer = createLexer("character" + FF + "NotEscaped",
formatWithEscaping)) {
+ assertThat(lexer.nextToken(new Token()), hasContent("character" +
FF + "NotEscaped"));
}
}
- @Test // TODO is this correct? Do we expect <esc>FF to be unescaped?
- public void testEscapedFF() throws Exception {
- try (final Lexer lexer = createLexer("character\\" + FF + "Escaped",
formatWithEscaping)) {
- assertThat(lexer.nextToken(new Token()), hasContent("character" +
FF + "Escaped"));
+ @Test
+ public void testIgnoreEmptyLines() throws IOException {
+ final String code = "first,line,\n" + "\n" + "\n" + "second,line\n" +
"\n" + "\n" + "third line \n" + "\n" +
+ "\n" + "last, line \n" + "\n" + "\n" + "\n";
+ final CSVFormat format = CSVFormat.DEFAULT.withIgnoreEmptyLines();
+ try (final Lexer parser = createLexer(code, format)) {
+ assertThat(parser.nextToken(new Token()), matches(TOKEN, "first"));
+ assertThat(parser.nextToken(new Token()), matches(TOKEN, "line"));
+ assertThat(parser.nextToken(new Token()), matches(EORECORD, ""));
+ assertThat(parser.nextToken(new Token()), matches(TOKEN,
"second"));
+ assertThat(parser.nextToken(new Token()), matches(EORECORD,
"line"));
+ assertThat(parser.nextToken(new Token()), matches(EORECORD, "third
line "));
+ assertThat(parser.nextToken(new Token()), matches(TOKEN, "last"));
+ assertThat(parser.nextToken(new Token()), matches(EORECORD, " line
"));
+ assertThat(parser.nextToken(new Token()), matches(EOF, ""));
+ assertThat(parser.nextToken(new Token()), matches(EOF, ""));
}
}
@Test
- public void testFF() throws Exception {
- try (final Lexer lexer = createLexer("character" + FF + "NotEscaped",
formatWithEscaping)) {
- assertThat(lexer.nextToken(new Token()), hasContent("character" +
FF + "NotEscaped"));
+ public void testIsMetaCharCommentStart() throws IOException {
+ try (final Lexer lexer = createLexer("#",
CSVFormat.DEFAULT.withCommentMarker('#'))) {
+ final int ch = lexer.readEscape();
+ assertEquals('#', ch);
}
}
@Test
- public void testEscapedMySqlNullValue() throws Exception {
- // MySQL uses \N to symbolize null values. We have to restore this
- try (final Lexer lexer = createLexer("character\\NEscaped",
formatWithEscaping)) {
- assertThat(lexer.nextToken(new Token()),
hasContent("character\\NEscaped"));
+ public void testLF() throws Exception {
+ try (final Lexer lexer = createLexer("character" + LF + "NotEscaped",
formatWithEscaping)) {
+ assertThat(lexer.nextToken(new Token()), hasContent("character"));
+ assertThat(lexer.nextToken(new Token()), hasContent("NotEscaped"));
}
}
+ // encapsulator tokenizer (single line)
@Test
- public void testEscapedCharacter() throws Exception {
- try (final Lexer lexer = createLexer("character\\aEscaped",
formatWithEscaping)) {
- assertThat(lexer.nextToken(new Token()),
hasContent("character\\aEscaped"));
+ public void testNextToken4() throws IOException {
+ /*
+ * file: a,"foo",b a, " foo",b a,"foo " ,b // whitespace after closing
encapsulator a, " foo " ,b
+ */
+ final String code = "a,\"foo\",b\na, \" foo\",b\na,\"foo \" ,b\na,
\" foo \" ,b";
+ try (final Lexer parser = createLexer(code,
CSVFormat.DEFAULT.withIgnoreSurroundingSpaces())) {
+ assertThat(parser.nextToken(new Token()), matches(TOKEN, "a"));
+ assertThat(parser.nextToken(new Token()), matches(TOKEN, "foo"));
+ assertThat(parser.nextToken(new Token()), matches(EORECORD, "b"));
+ assertThat(parser.nextToken(new Token()), matches(TOKEN, "a"));
+ assertThat(parser.nextToken(new Token()), matches(TOKEN, " foo"));
+ assertThat(parser.nextToken(new Token()), matches(EORECORD, "b"));
+ assertThat(parser.nextToken(new Token()), matches(TOKEN, "a"));
+ assertThat(parser.nextToken(new Token()), matches(TOKEN, "foo "));
+ assertThat(parser.nextToken(new Token()), matches(EORECORD, "b"));
+ assertThat(parser.nextToken(new Token()), matches(TOKEN, "a"));
+ assertThat(parser.nextToken(new Token()), matches(TOKEN, " foo "));
+ // assertTokenEquals(EORECORD, "b", parser.nextToken(new Token()));
+ assertThat(parser.nextToken(new Token()), matches(EOF, "b"));
}
}
+ // encapsulator tokenizer (multi line, delimiter in string)
@Test
- public void testEscapedControlCharacter() throws Exception {
- // we are explicitly using an escape different from \ here
- try (final Lexer lexer = createLexer("character!rEscaped",
CSVFormat.DEFAULT.withEscape('!'))) {
- assertThat(lexer.nextToken(new Token()), hasContent("character" +
CR + "Escaped"));
+ public void testNextToken5() throws IOException {
+ final String code = "a,\"foo\n\",b\n\"foo\n baar ,,,\"\n\"\n\t \n\"";
+ try (final Lexer parser = createLexer(code, CSVFormat.DEFAULT)) {
+ assertThat(parser.nextToken(new Token()), matches(TOKEN, "a"));
+ assertThat(parser.nextToken(new Token()), matches(TOKEN, "foo\n"));
+ assertThat(parser.nextToken(new Token()), matches(EORECORD, "b"));
+ assertThat(parser.nextToken(new Token()), matches(EORECORD, "foo\n
baar ,,,"));
+ assertThat(parser.nextToken(new Token()), matches(EOF, "\n\t \n"));
}
}
+ // change delimiters, comment, encapsulater
@Test
- public void testEscapedControlCharacter2() throws Exception {
- try (final Lexer lexer = createLexer("character\\rEscaped",
CSVFormat.DEFAULT.withEscape('\\'))) {
- assertThat(lexer.nextToken(new Token()), hasContent("character" +
CR + "Escaped"));
+ public void testNextToken6() throws IOException {
+ /*
+ * file: a;'b and \' more ' !comment;;;; ;;
+ */
+ final String code = "a;'b and '' more\n'\n!comment;;;;\n;;";
+ final CSVFormat format =
CSVFormat.DEFAULT.withQuote('\'').withCommentMarker('!').withDelimiter(';');
+ try (final Lexer parser = createLexer(code, format)) {
+ assertThat(parser.nextToken(new Token()), matches(TOKEN, "a"));
+ assertThat(parser.nextToken(new Token()), matches(EORECORD, "b and
' more\n"));
}
}
@Test
- public void testEscapingAtEOF() throws Exception {
- final String code = "escaping at EOF is evil\\";
- try (final Lexer lexer = createLexer(code, formatWithEscaping)) {
- assertThrows(IOException.class, () -> lexer.nextToken(new
Token()));
+ public void testReadEscapeBackspace() throws IOException {
+ try (final Lexer lexer = createLexer("b",
CSVFormat.DEFAULT.withEscape('\b'))) {
+ final int ch = lexer.readEscape();
+ assertEquals(BACKSPACE, ch);
}
}
@Test
- public void testTrimTrailingSpacesZeroLength() throws Exception {
- final StringBuilder buffer = new StringBuilder("");
- final Lexer lexer = createLexer(buffer.toString(), CSVFormat.DEFAULT);
- lexer.trimTrailingSpaces(buffer);
- assertThat(lexer.nextToken(new Token()), matches(EOF, ""));
+ public void testReadEscapeFF() throws IOException {
+ try (final Lexer lexer = createLexer("f",
CSVFormat.DEFAULT.withEscape('\f'))) {
+ final int ch = lexer.readEscape();
+ assertEquals(FF, ch);
+ }
}
@Test
@@ -409,26 +390,45 @@ public class LexerTest {
}
@Test
- public void testReadEscapeBackspace() throws IOException {
- try (final Lexer lexer = createLexer("b",
CSVFormat.DEFAULT.withEscape('\b'))) {
- final int ch = lexer.readEscape();
- assertEquals(BACKSPACE, ch);
+ public void testSurroundingSpacesAreDeleted() throws IOException {
+ final String code = "noSpaces, leadingSpaces,trailingSpaces ,
surroundingSpaces , ,,";
+ try (final Lexer parser = createLexer(code,
CSVFormat.DEFAULT.withIgnoreSurroundingSpaces())) {
+ assertThat(parser.nextToken(new Token()), matches(TOKEN,
"noSpaces"));
+ assertThat(parser.nextToken(new Token()), matches(TOKEN,
"leadingSpaces"));
+ assertThat(parser.nextToken(new Token()), matches(TOKEN,
"trailingSpaces"));
+ assertThat(parser.nextToken(new Token()), matches(TOKEN,
"surroundingSpaces"));
+ assertThat(parser.nextToken(new Token()), matches(TOKEN, ""));
+ assertThat(parser.nextToken(new Token()), matches(TOKEN, ""));
+ assertThat(parser.nextToken(new Token()), matches(EOF, ""));
}
}
@Test
- public void testReadEscapeFF() throws IOException {
- try (final Lexer lexer = createLexer("f",
CSVFormat.DEFAULT.withEscape('\f'))) {
- final int ch = lexer.readEscape();
- assertEquals(FF, ch);
+ public void testSurroundingTabsAreDeleted() throws IOException {
+ final String code =
"noTabs,\tleadingTab,trailingTab\t,\tsurroundingTabs\t,\t\t,,";
+ try (final Lexer parser = createLexer(code,
CSVFormat.DEFAULT.withIgnoreSurroundingSpaces())) {
+ assertThat(parser.nextToken(new Token()), matches(TOKEN,
"noTabs"));
+ assertThat(parser.nextToken(new Token()), matches(TOKEN,
"leadingTab"));
+ assertThat(parser.nextToken(new Token()), matches(TOKEN,
"trailingTab"));
+ assertThat(parser.nextToken(new Token()), matches(TOKEN,
"surroundingTabs"));
+ assertThat(parser.nextToken(new Token()), matches(TOKEN, ""));
+ assertThat(parser.nextToken(new Token()), matches(TOKEN, ""));
+ assertThat(parser.nextToken(new Token()), matches(EOF, ""));
}
}
@Test
- public void testIsMetaCharCommentStart() throws IOException {
- try (final Lexer lexer = createLexer("#",
CSVFormat.DEFAULT.withCommentMarker('#'))) {
- final int ch = lexer.readEscape();
- assertEquals('#', ch);
+ public void testTab() throws Exception {
+ try (final Lexer lexer = createLexer("character" + TAB + "NotEscaped",
formatWithEscaping)) {
+ assertThat(lexer.nextToken(new Token()), hasContent("character" +
TAB + "NotEscaped"));
}
}
+
+ @Test
+ public void testTrimTrailingSpacesZeroLength() throws Exception {
+ final StringBuilder buffer = new StringBuilder("");
+ final Lexer lexer = createLexer(buffer.toString(), CSVFormat.DEFAULT);
+ lexer.trimTrailingSpaces(buffer);
+ assertThat(lexer.nextToken(new Token()), matches(EOF, ""));
+ }
}
diff --git a/src/test/java/org/apache/commons/csv/PerformanceTest.java
b/src/test/java/org/apache/commons/csv/PerformanceTest.java
index 84ed083..ea6e8fd 100644
--- a/src/test/java/org/apache/commons/csv/PerformanceTest.java
+++ b/src/test/java/org/apache/commons/csv/PerformanceTest.java
@@ -41,6 +41,21 @@ import org.apache.commons.io.IOUtils;
@SuppressWarnings("boxing")
public class PerformanceTest {
+ @FunctionalInterface
+ private interface CSVParserFactory {
+ CSVParser createParser() throws IOException;
+ }
+
+ // Container for basic statistics
+ private static class Stats {
+ final int count;
+ final int fields;
+ Stats(final int c, final int f) {
+ count = c;
+ fields = f;
+ }
+ }
+
private static final String[] PROPS = {
"java.version", // Java Runtime Environment version
"java.vendor", // Java Runtime Environment vendor
@@ -58,17 +73,42 @@ public class PerformanceTest {
"os.arch", // Operating system architecture
"os.version", // Operating system version
};
-
private static int max = 11; // skip first test
private static int num; // number of elapsed times recorded
- private static final long[] ELAPSED_TIMES = new long[max];
+ private static final long[] ELAPSED_TIMES = new long[max];
private static final CSVFormat format = CSVFormat.EXCEL;
private static final String TEST_RESRC =
"org/apache/commons/csv/perf/worldcitiespop.txt.gz";
+
private static final File BIG_FILE = new
File(System.getProperty("java.io.tmpdir"), "worldcitiespop.txt");
+ private static Reader createReader() throws IOException {
+ return new InputStreamReader(new FileInputStream(BIG_FILE),
StandardCharsets.ISO_8859_1);
+ }
+
+ private static Lexer createTestCSVLexer(final String test, final
ExtendedBufferedReader input)
+ throws InstantiationException, IllegalAccessException,
InvocationTargetException, Exception {
+ return test.startsWith("CSVLexer") ?
getLexerCtor(test).newInstance(format, input) : new Lexer(format, input);
+ }
+
+ private static Constructor<Lexer> getLexerCtor(final String clazz) throws
Exception {
+ @SuppressWarnings("unchecked")
+ final Class<Lexer> lexer = (Class<Lexer>)
Class.forName("org.apache.commons.csv." + clazz);
+ return lexer.getConstructor(CSVFormat.class,
ExtendedBufferedReader.class);
+ }
+
+ private static Stats iterate(final Iterable<CSVRecord> it) {
+ int count = 0;
+ int fields = 0;
+ for (final CSVRecord record : it) {
+ count++;
+ fields += record.size();
+ }
+ return new Stats(count, fields);
+ }
+
public static void main(final String [] args) throws Exception {
if (BIG_FILE.exists()) {
System.out.printf("Found test fixture %s: %,d bytes.%n", BIG_FILE,
BIG_FILE.length());
@@ -128,26 +168,15 @@ public class PerformanceTest {
}
}
- private static Reader createReader() throws IOException {
- return new InputStreamReader(new FileInputStream(BIG_FILE),
StandardCharsets.ISO_8859_1);
- }
-
- // Container for basic statistics
- private static class Stats {
- final int count;
- final int fields;
- Stats(final int c, final int f) {
- count = c;
- fields = f;
+ private static Stats readAll(final BufferedReader in, final boolean split)
throws IOException {
+ int count = 0;
+ int fields = 0;
+ String record;
+ while ((record = in.readLine()) != null) {
+ count++;
+ fields += split ? record.split(",").length : 1;
}
- }
-
- // Display end stats; store elapsed for average
- private static void show(final String msg, final Stats s, final long
start) {
- final long elapsed = System.currentTimeMillis() - start;
- System.out.printf("%-20s: %5dms %d lines %d fields%n", msg, elapsed,
s.count, s.fields);
- ELAPSED_TIMES[num] = elapsed;
- num++;
+ return new Stats(count, fields);
}
// calculate and show average
@@ -162,30 +191,62 @@ public class PerformanceTest {
num = 0; // ready for next set
}
- private static void testReadBigFile(final boolean split) throws Exception {
+ // Display end stats; store elapsed for average
+ private static void show(final String msg, final Stats s, final long
start) {
+ final long elapsed = System.currentTimeMillis() - start;
+ System.out.printf("%-20s: %5dms %d lines %d fields%n", msg, elapsed,
s.count, s.fields);
+ ELAPSED_TIMES[num] = elapsed;
+ num++;
+ }
+
+ private static void testCSVLexer(final boolean newToken, final String
test) throws Exception {
+ Token token = new Token();
+ String dynamic = "";
for (int i = 0; i < max; i++) {
- final long startMillis;
+ final String simpleName;
final Stats stats;
- try (final BufferedReader in = new BufferedReader(createReader()))
{
+ final long startMillis;
+ try (final ExtendedBufferedReader input = new
ExtendedBufferedReader(createReader());
+ final Lexer lexer = createTestCSVLexer(test, input)) {
+ if (test.startsWith("CSVLexer")) {
+ dynamic = "!";
+ }
+ simpleName = lexer.getClass().getSimpleName();
+ int count = 0;
+ int fields = 0;
startMillis = System.currentTimeMillis();
- stats = readAll(in, split);
+ do {
+ if (newToken) {
+ token = new Token();
+ } else {
+ token.reset();
+ }
+ lexer.nextToken(token);
+ switch (token.type) {
+ case EOF:
+ break;
+ case EORECORD:
+ fields++;
+ count++;
+ break;
+ case INVALID:
+ throw new IOException("invalid parse sequence <" +
token.content.toString() + ">");
+ case TOKEN:
+ fields++;
+ break;
+ case COMMENT: // not really expecting these
+ break;
+ default:
+ throw new IllegalStateException("Unexpected Token
type: " + token.type);
+ }
+ } while (!token.type.equals(Token.Type.EOF));
+ stats = new Stats(count, fields);
}
- show(split ? "file+split" : "file", stats, startMillis);
+ show(simpleName + dynamic + " " + (newToken ? "new" : "reset"),
stats, startMillis);
}
show();
}
- private static Stats readAll(final BufferedReader in, final boolean split)
throws IOException {
- int count = 0;
- int fields = 0;
- String record;
- while ((record = in.readLine()) != null) {
- count++;
- fields += split ? record.split(",").length : 1;
- }
- return new Stats(count, fields);
- }
-
private static void testExtendedBuffer(final boolean makeString) throws
Exception {
for (int i = 0; i < max; i++) {
int fields = 0;
@@ -224,24 +285,6 @@ public class PerformanceTest {
show();
}
- private static void testParser(final String msg, final CSVParserFactory
fac) throws Exception {
- for (int i = 0; i < max; i++) {
- final long startMillis;
- final Stats stats;
- try (final CSVParser parser = fac.createParser()) {
- startMillis = System.currentTimeMillis();
- stats = iterate(parser);
- }
- show(msg, stats, startMillis);
- }
- show();
- }
-
- @FunctionalInterface
- private interface CSVParserFactory {
- CSVParser createParser() throws IOException;
- }
-
private static void testParseCommonsCSV() throws Exception {
testParser("CSV", () -> new CSVParser(createReader(), format));
}
@@ -254,77 +297,34 @@ public class PerformanceTest {
testParser("CSV-PATH-DB", () ->
CSVParser.parse(Files.newBufferedReader(Paths.get(BIG_FILE.toURI()),
StandardCharsets.ISO_8859_1), format));
}
- private static void testParseURL() throws Exception {
- testParser("CSV-URL", () -> CSVParser.parse(BIG_FILE.toURI().toURL(),
StandardCharsets.ISO_8859_1, format));
- }
-
- private static Constructor<Lexer> getLexerCtor(final String clazz) throws
Exception {
- @SuppressWarnings("unchecked")
- final Class<Lexer> lexer = (Class<Lexer>)
Class.forName("org.apache.commons.csv." + clazz);
- return lexer.getConstructor(CSVFormat.class,
ExtendedBufferedReader.class);
- }
-
- private static void testCSVLexer(final boolean newToken, final String
test) throws Exception {
- Token token = new Token();
- String dynamic = "";
+ private static void testParser(final String msg, final CSVParserFactory
fac) throws Exception {
for (int i = 0; i < max; i++) {
- final String simpleName;
- final Stats stats;
final long startMillis;
- try (final ExtendedBufferedReader input = new
ExtendedBufferedReader(createReader());
- final Lexer lexer = createTestCSVLexer(test, input)) {
- if (test.startsWith("CSVLexer")) {
- dynamic = "!";
- }
- simpleName = lexer.getClass().getSimpleName();
- int count = 0;
- int fields = 0;
+ final Stats stats;
+ try (final CSVParser parser = fac.createParser()) {
startMillis = System.currentTimeMillis();
- do {
- if (newToken) {
- token = new Token();
- } else {
- token.reset();
- }
- lexer.nextToken(token);
- switch (token.type) {
- case EOF:
- break;
- case EORECORD:
- fields++;
- count++;
- break;
- case INVALID:
- throw new IOException("invalid parse sequence <" +
token.content.toString() + ">");
- case TOKEN:
- fields++;
- break;
- case COMMENT: // not really expecting these
- break;
- default:
- throw new IllegalStateException("Unexpected Token
type: " + token.type);
- }
- } while (!token.type.equals(Token.Type.EOF));
- stats = new Stats(count, fields);
+ stats = iterate(parser);
}
- show(simpleName + dynamic + " " + (newToken ? "new" : "reset"),
stats, startMillis);
+ show(msg, stats, startMillis);
}
show();
}
- private static Lexer createTestCSVLexer(final String test, final
ExtendedBufferedReader input)
- throws InstantiationException, IllegalAccessException,
InvocationTargetException, Exception {
- return test.startsWith("CSVLexer") ?
getLexerCtor(test).newInstance(format, input) : new Lexer(format, input);
+ private static void testParseURL() throws Exception {
+ testParser("CSV-URL", () -> CSVParser.parse(BIG_FILE.toURI().toURL(),
StandardCharsets.ISO_8859_1, format));
}
- private static Stats iterate(final Iterable<CSVRecord> it) {
- int count = 0;
- int fields = 0;
- for (final CSVRecord record : it) {
- count++;
- fields += record.size();
+ private static void testReadBigFile(final boolean split) throws Exception {
+ for (int i = 0; i < max; i++) {
+ final long startMillis;
+ final Stats stats;
+ try (final BufferedReader in = new BufferedReader(createReader()))
{
+ startMillis = System.currentTimeMillis();
+ stats = readAll(in, split);
+ }
+ show(split ? "file+split" : "file", stats, startMillis);
}
- return new Stats(count, fields);
+ show();
}
}
\ No newline at end of file
diff --git a/src/test/java/org/apache/commons/csv/TokenMatchers.java
b/src/test/java/org/apache/commons/csv/TokenMatchers.java
index cfe522e..c081e7e 100644
--- a/src/test/java/org/apache/commons/csv/TokenMatchers.java
+++ b/src/test/java/org/apache/commons/csv/TokenMatchers.java
@@ -27,40 +27,40 @@ import org.hamcrest.TypeSafeDiagnosingMatcher;
*/
final class TokenMatchers {
- public static Matcher<Token> hasType(final Token.Type expectedType) {
+ public static Matcher<Token> hasContent(final String expectedContent) {
return new TypeSafeDiagnosingMatcher<Token>() {
@Override
public void describeTo(final Description description) {
- description.appendText("token has type ");
- description.appendValue(expectedType);
+ description.appendText("token has content ");
+ description.appendValue(expectedContent);
}
@Override
protected boolean matchesSafely(final Token item,
final Description mismatchDescription) {
- mismatchDescription.appendText("token type is ");
- mismatchDescription.appendValue(item.type);
- return item.type == expectedType;
+ mismatchDescription.appendText("token content is ");
+ mismatchDescription.appendValue(item.content.toString());
+ return expectedContent.equals(item.content.toString());
}
};
}
- public static Matcher<Token> hasContent(final String expectedContent) {
+ public static Matcher<Token> hasType(final Token.Type expectedType) {
return new TypeSafeDiagnosingMatcher<Token>() {
@Override
public void describeTo(final Description description) {
- description.appendText("token has content ");
- description.appendValue(expectedContent);
+ description.appendText("token has type ");
+ description.appendValue(expectedType);
}
@Override
protected boolean matchesSafely(final Token item,
final Description mismatchDescription) {
- mismatchDescription.appendText("token content is ");
- mismatchDescription.appendValue(item.content.toString());
- return expectedContent.equals(item.content.toString());
+ mismatchDescription.appendText("token type is ");
+ mismatchDescription.appendValue(item.type);
+ return item.type == expectedType;
}
};
}
diff --git a/src/test/java/org/apache/commons/csv/TokenMatchersTest.java
b/src/test/java/org/apache/commons/csv/TokenMatchersTest.java
index e8a115d..47c213d 100644
--- a/src/test/java/org/apache/commons/csv/TokenMatchersTest.java
+++ b/src/test/java/org/apache/commons/csv/TokenMatchersTest.java
@@ -40,6 +40,12 @@ public class TokenMatchersTest {
}
@Test
+ public void testHasContent() {
+ assertFalse(hasContent("This is not the token's
content").matches(token));
+ assertTrue(hasContent("content").matches(token));
+ }
+
+ @Test
public void testHasType() {
assertFalse(hasType(Token.Type.COMMENT).matches(token));
assertFalse(hasType(Token.Type.EOF).matches(token));
@@ -48,12 +54,6 @@ public class TokenMatchersTest {
}
@Test
- public void testHasContent() {
- assertFalse(hasContent("This is not the token's
content").matches(token));
- assertTrue(hasContent("content").matches(token));
- }
-
- @Test
public void testIsReady() {
assertTrue(isReady().matches(token));
token.isReady = false;
diff --git a/src/test/java/org/apache/commons/csv/Utils.java
b/src/test/java/org/apache/commons/csv/Utils.java
index 289083e..fcdbc3d 100644
--- a/src/test/java/org/apache/commons/csv/Utils.java
+++ b/src/test/java/org/apache/commons/csv/Utils.java
@@ -28,9 +28,6 @@ import java.util.List;
*/
final class Utils {
- private Utils() {
- }
-
/**
* Checks if the 2d array has the same contents as the list of records.
*
@@ -45,4 +42,7 @@ final class Utils {
assertArrayEquals(expected[i], actual.get(i).values(), message + "
(entry " + i + ")");
}
}
+
+ private Utils() {
+ }
}
diff --git a/src/test/java/org/apache/commons/csv/issues/JiraCsv148Test.java
b/src/test/java/org/apache/commons/csv/issues/JiraCsv148Test.java
index e8da064..fca6bec 100644
--- a/src/test/java/org/apache/commons/csv/issues/JiraCsv148Test.java
+++ b/src/test/java/org/apache/commons/csv/issues/JiraCsv148Test.java
@@ -24,40 +24,40 @@ import org.junit.jupiter.api.Test;
public class JiraCsv148Test {
- /**
- * 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() {
+ public void testWithIgnoreSurroundingSpacesEmpty() {
// @formatter:off
final CSVFormat format = CSVFormat.DEFAULT.builder()
.setQuoteMode(QuoteMode.ALL)
- .setTrim(true)
+ .setIgnoreSurroundingSpaces(true)
.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\"",
+ "\"\",\" \",\" 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.
+ */
@Test
- public void testWithIgnoreSurroundingSpacesEmpty() {
+ public void testWithTrimEmpty() {
// @formatter:off
final CSVFormat format = CSVFormat.DEFAULT.builder()
.setQuoteMode(QuoteMode.ALL)
- .setIgnoreSurroundingSpaces(true)
+ .setTrim(true)
.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 \"",
+ "\"\",\"\",\"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 "));
diff --git a/src/test/java/org/apache/commons/csv/issues/JiraCsv167Test.java
b/src/test/java/org/apache/commons/csv/issues/JiraCsv167Test.java
index 7f60832..c7effb6 100644
--- a/src/test/java/org/apache/commons/csv/issues/JiraCsv167Test.java
+++ b/src/test/java/org/apache/commons/csv/issues/JiraCsv167Test.java
@@ -31,6 +31,11 @@ import org.junit.jupiter.api.Test;
public class JiraCsv167Test {
+ private Reader getTestReader() {
+ return new InputStreamReader(
+
ClassLoader.getSystemClassLoader().getResourceAsStream("org/apache/commons/csv/csv-167/sample1.csv"));
+ }
+
@Test
public void parse() throws IOException {
int totcomment = 0;
@@ -81,9 +86,4 @@ public class JiraCsv167Test {
assertEquals(totcomment, comments);
assertEquals(totrecs, records); // records includes the header
}
-
- private Reader getTestReader() {
- return new InputStreamReader(
-
ClassLoader.getSystemClassLoader().getResourceAsStream("org/apache/commons/csv/csv-167/sample1.csv"));
- }
}
diff --git a/src/test/java/org/apache/commons/csv/issues/JiraCsv203Test.java
b/src/test/java/org/apache/commons/csv/issues/JiraCsv203Test.java
index ceb4132..17c6235 100644
--- a/src/test/java/org/apache/commons/csv/issues/JiraCsv203Test.java
+++ b/src/test/java/org/apache/commons/csv/issues/JiraCsv203Test.java
@@ -62,11 +62,12 @@ public class JiraCsv203Test {
}
@Test
- public void testWithoutQuoteMode() throws Exception {
+ public void testQuoteModeMinimal() throws Exception {
// @formatter:off
final CSVFormat format = CSVFormat.EXCEL.builder()
.setNullString("N/A")
.setIgnoreSurroundingSpaces(true)
+ .setQuoteMode(QuoteMode.MINIMAL)
.build();
// @formatter:on
final StringBuilder buffer = new StringBuilder();
@@ -77,35 +78,36 @@ public class JiraCsv203Test {
}
@Test
- public void testQuoteModeMinimal() throws Exception {
+ public void testQuoteModeNonNumeric() throws Exception {
// @formatter:off
final CSVFormat format = CSVFormat.EXCEL.builder()
.setNullString("N/A")
.setIgnoreSurroundingSpaces(true)
- .setQuoteMode(QuoteMode.MINIMAL)
+ .setQuoteMode(QuoteMode.NON_NUMERIC)
.build();
// @formatter:on
final StringBuilder buffer = new StringBuilder();
try (final CSVPrinter printer = new CSVPrinter(buffer, format)) {
printer.printRecord(null, "Hello", null, "World");
}
- assertEquals("N/A,Hello,N/A,World\r\n", buffer.toString());
+ assertEquals("N/A,\"Hello\",N/A,\"World\"\r\n", buffer.toString());
}
@Test
- public void testQuoteModeNonNumeric() throws Exception {
+ public void testWithEmptyValues() throws Exception {
// @formatter:off
final CSVFormat format = CSVFormat.EXCEL.builder()
.setNullString("N/A")
.setIgnoreSurroundingSpaces(true)
- .setQuoteMode(QuoteMode.NON_NUMERIC)
+ .setQuoteMode(QuoteMode.ALL)
.build();
// @formatter:on
final StringBuilder buffer = new StringBuilder();
try (final CSVPrinter printer = new CSVPrinter(buffer, format)) {
- printer.printRecord(null, "Hello", null, "World");
+ printer.printRecord("", "Hello", "", "World");
+ // printer.printRecord(new Object[] { null, "Hello", null, "World"
});
}
- assertEquals("N/A,\"Hello\",N/A,\"World\"\r\n", buffer.toString());
+ assertEquals("\"\",\"Hello\",\"\",\"World\"\r\n", buffer.toString());
}
@Test
@@ -125,19 +127,17 @@ public class JiraCsv203Test {
}
@Test
- public void testWithEmptyValues() throws Exception {
+ public void testWithoutQuoteMode() throws Exception {
// @formatter:off
final CSVFormat format = CSVFormat.EXCEL.builder()
.setNullString("N/A")
.setIgnoreSurroundingSpaces(true)
- .setQuoteMode(QuoteMode.ALL)
.build();
// @formatter:on
final StringBuilder buffer = new StringBuilder();
try (final CSVPrinter printer = new CSVPrinter(buffer, format)) {
- printer.printRecord("", "Hello", "", "World");
- // printer.printRecord(new Object[] { null, "Hello", null, "World"
});
+ printer.printRecord(null, "Hello", null, "World");
}
- assertEquals("\"\",\"Hello\",\"\",\"World\"\r\n", buffer.toString());
+ assertEquals("N/A,Hello,N/A,World\r\n", buffer.toString());
}
}
diff --git a/src/test/java/org/apache/commons/csv/issues/JiraCsv248Test.java
b/src/test/java/org/apache/commons/csv/issues/JiraCsv248Test.java
index 13d7239..84d274a 100644
--- a/src/test/java/org/apache/commons/csv/issues/JiraCsv248Test.java
+++ b/src/test/java/org/apache/commons/csv/issues/JiraCsv248Test.java
@@ -30,6 +30,10 @@ import org.apache.commons.csv.CSVRecord;
import org.junit.jupiter.api.Test;
public class JiraCsv248Test {
+ private static InputStream getTestInput() {
+ return
ClassLoader.getSystemClassLoader().getResourceAsStream("org/apache/commons/csv/CSV-248/csvRecord.bin");
+ }
+
/**
* Test deserialisation of a CSVRecord created using version 1.6.
*
@@ -74,8 +78,4 @@ public class JiraCsv248Test {
}
}
}
-
- private static InputStream getTestInput() {
- return
ClassLoader.getSystemClassLoader().getResourceAsStream("org/apache/commons/csv/CSV-248/csvRecord.bin");
- }
}
diff --git a/src/test/java/org/apache/commons/csv/perf/PerformanceTest.java
b/src/test/java/org/apache/commons/csv/perf/PerformanceTest.java
index 0579dd1..6a04988 100644
--- a/src/test/java/org/apache/commons/csv/perf/PerformanceTest.java
+++ b/src/test/java/org/apache/commons/csv/perf/PerformanceTest.java
@@ -43,11 +43,9 @@ import org.junit.jupiter.api.Test;
@SuppressWarnings("boxing") // test code
public class PerformanceTest {
- private final int max = 10;
-
private static final String TEST_RESRC =
"org/apache/commons/csv/perf/worldcitiespop.txt.gz";
- private static final File BIG_FILE = new
File(System.getProperty("java.io.tmpdir"), "worldcitiespop.txt");
+ private static final File BIG_FILE = new
File(System.getProperty("java.io.tmpdir"), "worldcitiespop.txt");
@BeforeAll
public static void setUpClass() throws FileNotFoundException, IOException {
if (BIG_FILE.exists()) {
@@ -64,6 +62,8 @@ public class PerformanceTest {
}
}
+ private final int max = 10;
+
private BufferedReader createBufferedReader() throws IOException {
return new BufferedReader(new FileReader(BIG_FILE));
}