Skip to content

Commit 6bbb815

Browse files
committed
Test refactoring
1 parent 701a64f commit 6bbb815

File tree

3 files changed

+65
-42
lines changed

3 files changed

+65
-42
lines changed

src/test/java/org/apache/commons/csv/CSVParserTest.java

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import static org.apache.commons.csv.Constants.CR;
2323
import static org.apache.commons.csv.Constants.CRLF;
2424
import static org.apache.commons.csv.Constants.LF;
25-
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
25+
import static org.apache.commons.csv.CsvAssertions.assertValuesEquals;
2626
import static org.junit.jupiter.api.Assertions.assertEquals;
2727
import static org.junit.jupiter.api.Assertions.assertFalse;
2828
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
@@ -217,7 +217,7 @@ void testBackslashEscapingOld() throws IOException {
217217
assertEquals(res.length, records.size());
218218
assertFalse(records.isEmpty());
219219
for (int i = 0; i < res.length; i++) {
220-
assertArrayEquals(res[i], records.get(i).values());
220+
assertValuesEquals(res[i], records.get(i));
221221
}
222222
}
223223
}
@@ -479,7 +479,7 @@ void testEmptyLineBehaviorCSV() throws Exception {
479479
assertEquals(res.length, records.size());
480480
assertFalse(records.isEmpty());
481481
for (int i = 0; i < res.length; i++) {
482-
assertArrayEquals(res[i], records.get(i).values());
482+
assertValuesEquals(res[i], records.get(i));
483483
}
484484
}
485485
}
@@ -496,7 +496,7 @@ void testEmptyLineBehaviorExcel() throws Exception {
496496
assertEquals(res.length, records.size());
497497
assertFalse(records.isEmpty());
498498
for (int i = 0; i < res.length; i++) {
499-
assertArrayEquals(res[i], records.get(i).values());
499+
assertValuesEquals(res[i], records.get(i));
500500
}
501501
}
502502
}
@@ -521,7 +521,7 @@ void testEndOfFileBehaviorCSV() throws Exception {
521521
assertEquals(res.length, records.size());
522522
assertFalse(records.isEmpty());
523523
for (int i = 0; i < res.length; i++) {
524-
assertArrayEquals(res[i], records.get(i).values());
524+
assertValuesEquals(res[i], records.get(i));
525525
}
526526
}
527527
}
@@ -540,7 +540,7 @@ void testEndOfFileBehaviorExcel() throws Exception {
540540
assertEquals(res.length, records.size());
541541
assertFalse(records.isEmpty());
542542
for (int i = 0; i < res.length; i++) {
543-
assertArrayEquals(res[i], records.get(i).values());
543+
assertValuesEquals(res[i], records.get(i));
544544
}
545545
}
546546
}
@@ -556,7 +556,7 @@ void testExcelFormat1() throws IOException {
556556
assertEquals(res.length, records.size());
557557
assertFalse(records.isEmpty());
558558
for (int i = 0; i < res.length; i++) {
559-
assertArrayEquals(res[i], records.get(i).values());
559+
assertValuesEquals(res[i], records.get(i));
560560
}
561561
}
562562
}
@@ -570,7 +570,7 @@ void testExcelFormat2() throws Exception {
570570
assertEquals(res.length, records.size());
571571
assertFalse(records.isEmpty());
572572
for (int i = 0; i < res.length; i++) {
573-
assertArrayEquals(res[i], records.get(i).values());
573+
assertValuesEquals(res[i], records.get(i));
574574
}
575575
}
576576
}
@@ -629,9 +629,9 @@ void testForEach() throws Exception {
629629
records.add(record);
630630
}
631631
assertEquals(3, records.size());
632-
assertArrayEquals(new String[] { "a", "b", "c" }, records.get(0).values());
633-
assertArrayEquals(new String[] { "1", "2", "3" }, records.get(1).values());
634-
assertArrayEquals(new String[] { "x", "y", "z" }, records.get(2).values());
632+
assertValuesEquals(new String[] { "a", "b", "c" }, records.get(0));
633+
assertValuesEquals(new String[] { "1", "2", "3" }, records.get(1));
634+
assertValuesEquals(new String[] { "x", "y", "z" }, records.get(2));
635635
}
636636
}
637637

@@ -756,7 +756,7 @@ void testGetHeaderNamesReadOnly() throws IOException {
756756
void testGetLine() throws IOException {
757757
try (CSVParser parser = CSVParser.parse(CSV_INPUT, CSVFormat.DEFAULT.withIgnoreSurroundingSpaces())) {
758758
for (final String[] re : RESULT) {
759-
assertArrayEquals(re, parser.nextRecord().values());
759+
assertValuesEquals(re, parser.nextRecord());
760760
}
761761

762762
assertNull(parser.nextRecord());
@@ -782,7 +782,7 @@ void testGetLineNumberWithLF() throws Exception {
782782
void testGetOneLine() throws IOException {
783783
try (CSVParser parser = CSVParser.parse(CSV_INPUT_1, CSVFormat.DEFAULT)) {
784784
final CSVRecord record = parser.getRecords().get(0);
785-
assertArrayEquals(RESULT[0], record.values());
785+
assertValuesEquals(RESULT[0], record);
786786
}
787787
}
788788

@@ -803,11 +803,11 @@ void testGetOneLineOneParser() throws IOException {
803803
writer.append(CSV_INPUT_1);
804804
writer.append(format.getRecordSeparator());
805805
final CSVRecord record1 = parser.nextRecord();
806-
assertArrayEquals(RESULT[0], record1.values());
806+
assertValuesEquals(RESULT[0], record1);
807807
writer.append(CSV_INPUT_2);
808808
writer.append(format.getRecordSeparator());
809809
final CSVRecord record2 = parser.nextRecord();
810-
assertArrayEquals(RESULT[1], record2.values());
810+
assertValuesEquals(RESULT[1], record2);
811811
}
812812
}
813813

@@ -877,7 +877,7 @@ void testGetRecords() throws IOException {
877877
assertEquals(RESULT.length, records.size());
878878
assertFalse(records.isEmpty());
879879
for (int i = 0; i < RESULT.length; i++) {
880-
assertArrayEquals(RESULT[i], records.get(i).values());
880+
assertValuesEquals(RESULT[i], records.get(i));
881881
}
882882
}
883883
}
@@ -899,7 +899,7 @@ void testGetRecordsMaxRows(final long maxRows) throws IOException {
899899
assertEquals(expectedLength, records.size());
900900
assertFalse(records.isEmpty());
901901
for (int i = 0; i < expectedLength; i++) {
902-
assertArrayEquals(RESULT[i], records.get(i).values());
902+
assertValuesEquals(RESULT[i], records.get(i));
903903
}
904904
}
905905
}
@@ -1155,12 +1155,12 @@ void testIterator() throws Exception {
11551155
final Iterator<CSVRecord> iterator = parser.iterator();
11561156
assertTrue(iterator.hasNext());
11571157
assertThrows(UnsupportedOperationException.class, iterator::remove);
1158-
assertArrayEquals(new String[] { "a", "b", "c" }, iterator.next().values());
1159-
assertArrayEquals(new String[] { "1", "2", "3" }, iterator.next().values());
1158+
assertValuesEquals(new String[] { "a", "b", "c" }, iterator.next());
1159+
assertValuesEquals(new String[] { "1", "2", "3" }, iterator.next());
11601160
assertTrue(iterator.hasNext());
11611161
assertTrue(iterator.hasNext());
11621162
assertTrue(iterator.hasNext());
1163-
assertArrayEquals(new String[] { "x", "y", "z" }, iterator.next().values());
1163+
assertValuesEquals(new String[] { "x", "y", "z" }, iterator.next());
11641164
assertFalse(iterator.hasNext());
11651165
assertThrows(NoSuchElementException.class, iterator::next);
11661166
}
@@ -1174,20 +1174,20 @@ void testIteratorMaxRows(final long maxRows) throws Exception {
11741174
final Iterator<CSVRecord> iterator = parser.iterator();
11751175
assertTrue(iterator.hasNext());
11761176
assertThrows(UnsupportedOperationException.class, iterator::remove);
1177-
assertArrayEquals(new String[] { "a", "b", "c" }, iterator.next().values());
1177+
assertValuesEquals(new String[] { "a", "b", "c" }, iterator.next());
11781178
final boolean noLimit = maxRows <= 0;
11791179
final int fixtureLen = 3;
11801180
final long expectedLen = noLimit ? fixtureLen : Math.min(fixtureLen, maxRows);
11811181
if (expectedLen > 1) {
11821182
assertTrue(iterator.hasNext());
1183-
assertArrayEquals(new String[] { "1", "2", "3" }, iterator.next().values());
1183+
assertValuesEquals(new String[] { "1", "2", "3" }, iterator.next());
11841184
}
11851185
assertEquals(expectedLen > 2, iterator.hasNext());
11861186
// again
11871187
assertEquals(expectedLen > 2, iterator.hasNext());
11881188
if (expectedLen == fixtureLen) {
11891189
assertTrue(iterator.hasNext());
1190-
assertArrayEquals(new String[] { "x", "y", "z" }, iterator.next().values());
1190+
assertValuesEquals(new String[] { "x", "y", "z" }, iterator.next());
11911191
}
11921192
assertFalse(iterator.hasNext());
11931193
assertThrows(NoSuchElementException.class, iterator::next);
@@ -1584,7 +1584,7 @@ void testParsingPrintedEmptyFirstColumn(final CSVFormat.Predefined format) throw
15841584
.setFormat(format.getFormat())
15851585
.get()) {
15861586
for (final String[] line : lines) {
1587-
assertArrayEquals(line, csvRecords.nextRecord().values());
1587+
assertValuesEquals(line, csvRecords.nextRecord());
15881588
}
15891589
assertNull(csvRecords.nextRecord());
15901590
}
@@ -1715,7 +1715,7 @@ void testStartWithEmptyLinesThenHeaders() throws Exception {
17151715
assertEquals(res.length, records.size());
17161716
assertFalse(records.isEmpty());
17171717
for (int i = 0; i < res.length; i++) {
1718-
assertArrayEquals(res[i], records.get(i).values());
1718+
assertValuesEquals(res[i], records.get(i));
17191719
}
17201720
}
17211721
}
@@ -1727,9 +1727,9 @@ void testStream() throws Exception {
17271727
try (CSVParser parser = CSVFormat.DEFAULT.parse(in)) {
17281728
final List<CSVRecord> list = parser.stream().collect(Collectors.toList());
17291729
assertFalse(list.isEmpty());
1730-
assertArrayEquals(new String[] { "a", "b", "c" }, list.get(0).values());
1731-
assertArrayEquals(new String[] { "1", "2", "3" }, list.get(1).values());
1732-
assertArrayEquals(new String[] { "x", "y", "z" }, list.get(2).values());
1730+
assertValuesEquals(new String[] { "a", "b", "c" }, list.get(0));
1731+
assertValuesEquals(new String[] { "1", "2", "3" }, list.get(1));
1732+
assertValuesEquals(new String[] { "x", "y", "z" }, list.get(2));
17331733
}
17341734
}
17351735

@@ -1740,12 +1740,12 @@ void testStreamMaxRows(final long maxRows) throws Exception {
17401740
try (CSVParser parser = CSVFormat.DEFAULT.builder().setMaxRows(maxRows).get().parse(in)) {
17411741
final List<CSVRecord> list = parser.stream().collect(Collectors.toList());
17421742
assertFalse(list.isEmpty());
1743-
assertArrayEquals(new String[] { "a", "b", "c" }, list.get(0).values());
1743+
assertValuesEquals(new String[] { "a", "b", "c" }, list.get(0));
17441744
if (maxRows <= 0 || maxRows > 1) {
1745-
assertArrayEquals(new String[] { "1", "2", "3" }, list.get(1).values());
1745+
assertValuesEquals(new String[] { "1", "2", "3" }, list.get(1));
17461746
}
17471747
if (maxRows <= 0 || maxRows > 2) {
1748-
assertArrayEquals(new String[] { "x", "y", "z" }, list.get(2).values());
1748+
assertValuesEquals(new String[] { "x", "y", "z" }, list.get(2));
17491749
}
17501750
}
17511751
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* https://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.apache.commons.csv;
21+
22+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
23+
24+
public class CsvAssertions {
25+
26+
public static void assertValuesEquals(final String[] expected, final CSVRecord actual) {
27+
assertArrayEquals(expected, actual.values());
28+
}
29+
}

src/test/java/org/apache/commons/csv/issues/JiraCsv253Test.java

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919
package org.apache.commons.csv.issues;
2020

21-
import static org.junit.jupiter.api.Assertions.assertEquals;
21+
import static org.apache.commons.csv.CsvAssertions.assertValuesEquals;
2222

2323
import java.io.IOException;
2424
import java.io.StringReader;
@@ -35,22 +35,16 @@
3535
*/
3636
class JiraCsv253Test {
3737

38-
private void assertArrayEqual(final String[] expected, final CSVRecord actual) {
39-
for (int i = 0; i < expected.length; i++) {
40-
assertEquals(expected[i], actual.get(i));
41-
}
42-
}
43-
4438
@Test
4539
void testHandleAbsentValues() throws IOException {
4640
final String source = "\"John\",,\"Doe\"\n" + ",\"AA\",123\n" + "\"John\",90,\n" + "\"\",,90";
4741
final CSVFormat csvFormat = CSVFormat.DEFAULT.builder().setQuoteMode(QuoteMode.NON_NUMERIC).get();
4842
try (CSVParser parser = csvFormat.parse(new StringReader(source))) {
4943
final Iterator<CSVRecord> csvRecords = parser.iterator();
50-
assertArrayEqual(new String[] {"John", null, "Doe"}, csvRecords.next());
51-
assertArrayEqual(new String[] {null, "AA", "123"}, csvRecords.next());
52-
assertArrayEqual(new String[] {"John", "90", null}, csvRecords.next());
53-
assertArrayEqual(new String[] {"", null, "90"}, csvRecords.next());
44+
assertValuesEquals(new String[] {"John", null, "Doe"}, csvRecords.next());
45+
assertValuesEquals(new String[] {null, "AA", "123"}, csvRecords.next());
46+
assertValuesEquals(new String[] {"John", "90", null}, csvRecords.next());
47+
assertValuesEquals(new String[] {"", null, "90"}, csvRecords.next());
5448
}
5549
}
5650
}

0 commit comments

Comments
 (0)