You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2023/11/03 13:57:44 UTC
(commons-csv) 02/02: Sort test members
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
commit d2ed0ba46bff49f95501fb4bb44309baf704d1f8
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Fri Nov 3 09:57:37 2023 -0400
Sort test members
---
.../java/org/apache/commons/csv/CSVBenchmark.java | 116 ++++++++++-----------
.../java/org/apache/commons/csv/CSVFormatTest.java | 26 ++---
.../java/org/apache/commons/csv/CSVRecordTest.java | 62 +++++------
3 files changed, 102 insertions(+), 102 deletions(-)
diff --git a/src/test/java/org/apache/commons/csv/CSVBenchmark.java b/src/test/java/org/apache/commons/csv/CSVBenchmark.java
index 86a345d1..4a146a0a 100644
--- a/src/test/java/org/apache/commons/csv/CSVBenchmark.java
+++ b/src/test/java/org/apache/commons/csv/CSVBenchmark.java
@@ -58,8 +58,21 @@ import org.supercsv.prefs.CsvPreference;
@State(Scope.Benchmark)
public class CSVBenchmark {
+ private static final class CountingReaderCallback implements org.skife.csv.ReaderCallback {
+ public int count;
+
+ @Override
+ public void onRow(final String[] fields) {
+ count++;
+ }
+ }
+
private String data;
+ private Reader getReader() {
+ return new StringReader(data);
+ }
+
/**
* Load the data in memory before running the benchmarks, this takes out IO from the results.
*/
@@ -72,55 +85,6 @@ public class CSVBenchmark {
}
}
- private Reader getReader() {
- return new StringReader(data);
- }
-
- @Benchmark
- public int read(final Blackhole bh) throws Exception {
- int count = 0;
-
- try (BufferedReader reader = new BufferedReader(getReader())) {
- while (reader.readLine() != null) {
- count++;
- }
- }
-
- bh.consume(count);
- return count;
- }
-
- @Benchmark
- public int scan(final Blackhole bh) throws Exception {
- int count = 0;
-
- try (Scanner scanner = new Scanner(getReader())) {
- while (scanner.hasNextLine()) {
- scanner.nextLine();
- count++;
- }
- }
-
- bh.consume(count);
- return count;
- }
-
- @Benchmark
- public int split(final Blackhole bh) throws Exception {
- int count = 0;
-
- try (BufferedReader reader = new BufferedReader(getReader())) {
- String line;
- while ((line = reader.readLine()) != null) {
- final String[] values = StringUtils.split(line, ',');
- count += values.length;
- }
- }
-
- bh.consume(count);
- return count;
- }
-
@Benchmark
public int parseCommonsCSV(final Blackhole bh) throws Exception {
int count = 0;
@@ -202,15 +166,6 @@ public class CSVBenchmark {
return callback.count;
}
- private static final class CountingReaderCallback implements org.skife.csv.ReaderCallback {
- public int count;
-
- @Override
- public void onRow(final String[] fields) {
- count++;
- }
- }
-
@Benchmark
public int parseSuperCSV(final Blackhole bh) throws Exception {
int count = 0;
@@ -224,4 +179,49 @@ public class CSVBenchmark {
bh.consume(count);
return count;
}
+
+ @Benchmark
+ public int read(final Blackhole bh) throws Exception {
+ int count = 0;
+
+ try (BufferedReader reader = new BufferedReader(getReader())) {
+ while (reader.readLine() != null) {
+ count++;
+ }
+ }
+
+ bh.consume(count);
+ return count;
+ }
+
+ @Benchmark
+ public int scan(final Blackhole bh) throws Exception {
+ int count = 0;
+
+ try (Scanner scanner = new Scanner(getReader())) {
+ while (scanner.hasNextLine()) {
+ scanner.nextLine();
+ count++;
+ }
+ }
+
+ bh.consume(count);
+ return count;
+ }
+
+ @Benchmark
+ public int split(final Blackhole bh) throws Exception {
+ int count = 0;
+
+ try (BufferedReader reader = new BufferedReader(getReader())) {
+ String line;
+ while ((line = reader.readLine()) != null) {
+ final String[] values = StringUtils.split(line, ',');
+ count += values.length;
+ }
+ }
+
+ bh.consume(count);
+ return count;
+ }
}
diff --git a/src/test/java/org/apache/commons/csv/CSVFormatTest.java b/src/test/java/org/apache/commons/csv/CSVFormatTest.java
index f683e62d..e3a284f7 100644
--- a/src/test/java/org/apache/commons/csv/CSVFormatTest.java
+++ b/src/test/java/org/apache/commons/csv/CSVFormatTest.java
@@ -935,6 +935,19 @@ public class CSVFormatTest {
assertThrows(IllegalArgumentException.class, () -> CSVFormat.DEFAULT.withQuote('!').withDelimiter('!'));
}
+ @Test
+ public void testQuoteModeNoneShouldReturnMeaningfulExceptionMessage() {
+ Exception exception = assertThrows(IllegalArgumentException.class, () -> {
+ CSVFormat.DEFAULT.builder()
+ .setHeader("Col1", "Col2", "Col3", "Col4")
+ .setQuoteMode(QuoteMode.NONE)
+ .build();
+ });
+ String actualMessage = exception.getMessage();
+ String expectedMessage = "Quote mode set to NONE but no escape character is set";
+ assertEquals(expectedMessage, actualMessage);
+ }
+
@Test
public void testQuotePolicyNoneWithoutEscapeThrowsException() {
assertThrows(IllegalArgumentException.class, () -> CSVFormat.newFormat('!').builder().setQuoteMode(QuoteMode.NONE).build());
@@ -1477,17 +1490,4 @@ public class CSVFormatTest {
final CSVFormat formatWithRecordSeparator = CSVFormat.DEFAULT.withSystemRecordSeparator();
assertEquals(System.lineSeparator(), formatWithRecordSeparator.getRecordSeparator());
}
-
- @Test
- public void testQuoteModeNoneShouldReturnMeaningfulExceptionMessage() {
- Exception exception = assertThrows(IllegalArgumentException.class, () -> {
- CSVFormat.DEFAULT.builder()
- .setHeader("Col1", "Col2", "Col3", "Col4")
- .setQuoteMode(QuoteMode.NONE)
- .build();
- });
- String actualMessage = exception.getMessage();
- String expectedMessage = "Quote mode set to NONE but no escape character is set";
- assertEquals(expectedMessage, actualMessage);
- }
}
diff --git a/src/test/java/org/apache/commons/csv/CSVRecordTest.java b/src/test/java/org/apache/commons/csv/CSVRecordTest.java
index 975d45a5..9be416cf 100644
--- a/src/test/java/org/apache/commons/csv/CSVRecordTest.java
+++ b/src/test/java/org/apache/commons/csv/CSVRecordTest.java
@@ -89,6 +89,37 @@ public class CSVRecordTest {
assertThrows(IllegalArgumentException.class, () -> csvRecord.get("B"));
}
+ @Test
+ public void testDuplicateHeaderGet() throws IOException {
+ final String csv = "A,A,B,B\n1,2,5,6\n";
+ final CSVFormat format = CSVFormat.DEFAULT.builder().setHeader().build();
+
+ try (final CSVParser parser = CSVParser.parse(csv, format)) {
+ final CSVRecord record = parser.nextRecord();
+
+ assertAll("Test that it gets the last instance of a column when there are duplicate headings",
+ () -> assertEquals("2", record.get("A")),
+ () -> assertEquals("6", record.get("B"))
+ );
+ }
+ }
+
+ @Test
+ public void testDuplicateHeaderToMap() throws IOException {
+ final String csv = "A,A,B,B\n1,2,5,6\n";
+ final CSVFormat format = CSVFormat.DEFAULT.builder().setHeader().build();
+
+ try (final CSVParser parser = CSVParser.parse(csv, format)) {
+ final CSVRecord record = parser.nextRecord();
+ final Map<String, String> map = record.toMap();
+
+ assertAll("Test that it gets the last instance of a column when there are duplicate headings",
+ () -> assertEquals("2", map.get("A")),
+ () -> assertEquals("6", map.get("B"))
+ );
+ }
+ }
+
@Test
public void testGetInt() {
assertEquals(values[0], record.get(0));
@@ -337,37 +368,6 @@ public class CSVRecordTest {
assertTrue(recordWithHeader.toString().contains("values="));
}
- @Test
- public void testDuplicateHeaderGet() throws IOException {
- final String csv = "A,A,B,B\n1,2,5,6\n";
- final CSVFormat format = CSVFormat.DEFAULT.builder().setHeader().build();
-
- try (final CSVParser parser = CSVParser.parse(csv, format)) {
- final CSVRecord record = parser.nextRecord();
-
- assertAll("Test that it gets the last instance of a column when there are duplicate headings",
- () -> assertEquals("2", record.get("A")),
- () -> assertEquals("6", record.get("B"))
- );
- }
- }
-
- @Test
- public void testDuplicateHeaderToMap() throws IOException {
- final String csv = "A,A,B,B\n1,2,5,6\n";
- final CSVFormat format = CSVFormat.DEFAULT.builder().setHeader().build();
-
- try (final CSVParser parser = CSVParser.parse(csv, format)) {
- final CSVRecord record = parser.nextRecord();
- final Map<String, String> map = record.toMap();
-
- assertAll("Test that it gets the last instance of a column when there are duplicate headings",
- () -> assertEquals("2", map.get("A")),
- () -> assertEquals("6", map.get("B"))
- );
- }
- }
-
private void validateMap(final Map<String, String> map, final boolean allowsNulls) {
assertTrue(map.containsKey(EnumHeader.FIRST.name()));
assertTrue(map.containsKey(EnumHeader.SECOND.name()));