Skip to content

Commit 0047fb0

Browse files
committed
Refactor code duplication in tests
1 parent 6293899 commit 0047fb0

1 file changed

Lines changed: 19 additions & 36 deletions

File tree

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

Lines changed: 19 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import static org.junit.jupiter.api.Assertions.assertEquals;
2121

22+
import java.io.IOException;
2223
import java.io.Reader;
2324
import java.io.StringReader;
2425

@@ -30,6 +31,12 @@
3031

3132
public class JiraCsv288Test {
3233

34+
private void print(final CSVRecord csvRecord, CSVPrinter csvPrinter) throws IOException {
35+
for (String value : csvRecord) {
36+
csvPrinter.print(value);
37+
}
38+
}
39+
3340
@Test
3441
// Before fix:
3542
// expected: <a,b,c,d,,f> but was: <a,b,c,d,|f>
@@ -39,9 +46,7 @@ public void testParseWithABADelimiter() throws Exception {
3946
try (CSVPrinter csvPrinter = new CSVPrinter(stringBuilder, CSVFormat.EXCEL);
4047
CSVParser parser = CSVParser.parse(in, CSVFormat.Builder.create().setDelimiter("|~|").build())) {
4148
for (final CSVRecord csvRecord : parser) {
42-
for (int i = 0; i < csvRecord.size(); i++) {
43-
csvPrinter.print(csvRecord.get(i));
44-
}
49+
print(csvRecord, csvPrinter);
4550
assertEquals("a,b,c,d,,f", stringBuilder.toString());
4651
}
4752
}
@@ -56,9 +61,7 @@ public void testParseWithDoublePipeDelimiter() throws Exception {
5661
try (CSVPrinter csvPrinter = new CSVPrinter(stringBuilder, CSVFormat.EXCEL);
5762
CSVParser csvParser = CSVParser.parse(in, CSVFormat.Builder.create().setDelimiter("||").build())) {
5863
for (final CSVRecord csvRecord : csvParser) {
59-
for (int i = 0; i < csvRecord.size(); i++) {
60-
csvPrinter.print(csvRecord.get(i));
61-
}
64+
print(csvRecord, csvPrinter);
6265
assertEquals("a,b,c,d,,f", stringBuilder.toString());
6366
}
6467
}
@@ -73,9 +76,7 @@ public void testParseWithDoublePipeDelimiterDoubleCharValue() throws Exception {
7376
try (CSVPrinter csvPrinter = new CSVPrinter(stringBuilder, CSVFormat.EXCEL);
7477
CSVParser csvParser = CSVParser.parse(in, CSVFormat.Builder.create().setDelimiter("||").build())) {
7578
for (final CSVRecord csvRecord : csvParser) {
76-
for (int i = 0; i < csvRecord.size(); i++) {
77-
csvPrinter.print(csvRecord.get(i));
78-
}
79+
print(csvRecord, csvPrinter);
7980
assertEquals("a,bb,cc,dd,f", stringBuilder.toString());
8081
}
8182
}
@@ -90,9 +91,7 @@ public void testParseWithDoublePipeDelimiterEndsWithDelimiter() throws Exception
9091
try (CSVPrinter csvPrinter = new CSVPrinter(stringBuilder, CSVFormat.EXCEL);
9192
CSVParser csvParser = CSVParser.parse(in, CSVFormat.Builder.create().setDelimiter("||").build())) {
9293
for (final CSVRecord csvRecord : csvParser) {
93-
for (int i = 0; i < csvRecord.size(); i++) {
94-
csvPrinter.print(csvRecord.get(i));
95-
}
94+
print(csvRecord, csvPrinter);
9695
assertEquals("a,b,c,d,,f,", stringBuilder.toString());
9796
}
9897
}
@@ -107,9 +106,7 @@ public void testParseWithDoublePipeDelimiterQuoted() throws Exception {
107106
try (CSVPrinter csvPrinter = new CSVPrinter(stringBuilder, CSVFormat.EXCEL);
108107
CSVParser csvParser = CSVParser.parse(in, CSVFormat.Builder.create().setDelimiter("||").build())) {
109108
for (final CSVRecord csvRecord : csvParser) {
110-
for (int i = 0; i < csvRecord.size(); i++) {
111-
csvPrinter.print(csvRecord.get(i));
112-
}
109+
print(csvRecord, csvPrinter);
113110
assertEquals("a,b||c,d,,f", stringBuilder.toString());
114111
}
115112
}
@@ -123,9 +120,7 @@ public void testParseWithSinglePipeDelimiterEndsWithDelimiter() throws Exception
123120
try (CSVPrinter csvPrinter = new CSVPrinter(stringBuilder, CSVFormat.EXCEL);
124121
CSVParser csvParser = CSVParser.parse(in, CSVFormat.Builder.create().setDelimiter("|").build())) {
125122
for (final CSVRecord csvRecord : csvParser) {
126-
for (int i = 0; i < csvRecord.size(); i++) {
127-
csvPrinter.print(csvRecord.get(i));
128-
}
123+
print(csvRecord, csvPrinter);
129124
assertEquals("a,b,c,d,,f,", stringBuilder.toString());
130125
}
131126
}
@@ -140,9 +135,7 @@ public void testParseWithTriplePipeDelimiter() throws Exception {
140135
try (CSVPrinter csvPrinter = new CSVPrinter(stringBuilder, CSVFormat.EXCEL);
141136
CSVParser csvParser = CSVParser.parse(in, CSVFormat.Builder.create().setDelimiter("|||").build())) {
142137
for (final CSVRecord csvRecord : csvParser) {
143-
for (int i = 0; i < csvRecord.size(); i++) {
144-
csvPrinter.print(csvRecord.get(i));
145-
}
138+
print(csvRecord, csvPrinter);
146139
assertEquals("a,b,c,d,,f", stringBuilder.toString());
147140
}
148141
}
@@ -156,9 +149,7 @@ public void testParseWithTwoCharDelimiter1() throws Exception {
156149
try (CSVPrinter csvPrinter = new CSVPrinter(stringBuilder, CSVFormat.EXCEL);
157150
CSVParser csvParser = CSVParser.parse(in, CSVFormat.Builder.create().setDelimiter("~|").build())) {
158151
for (final CSVRecord csvRecord : csvParser) {
159-
for (int i = 0; i < csvRecord.size(); i++) {
160-
csvPrinter.print(csvRecord.get(i));
161-
}
152+
print(csvRecord, csvPrinter);
162153
assertEquals("a,b,c,d,,f", stringBuilder.toString());
163154
}
164155
}
@@ -172,9 +163,7 @@ public void testParseWithTwoCharDelimiter2() throws Exception {
172163
try (CSVPrinter csvPrinter = new CSVPrinter(stringBuilder, CSVFormat.EXCEL);
173164
CSVParser csvParser = CSVParser.parse(in, CSVFormat.Builder.create().setDelimiter("~|").build())) {
174165
for (final CSVRecord csvRecord : csvParser) {
175-
for (int i = 0; i < csvRecord.size(); i++) {
176-
csvPrinter.print(csvRecord.get(i));
177-
}
166+
print(csvRecord, csvPrinter);
178167
assertEquals("a,b,c,d,,f~", stringBuilder.toString());
179168
}
180169
}
@@ -188,9 +177,7 @@ public void testParseWithTwoCharDelimiter3() throws Exception {
188177
try (CSVPrinter csvPrinter = new CSVPrinter(stringBuilder, CSVFormat.EXCEL);
189178
CSVParser csvParser = CSVParser.parse(in, CSVFormat.Builder.create().setDelimiter("~|").build())) {
190179
for (final CSVRecord csvRecord : csvParser) {
191-
for (int i = 0; i < csvRecord.size(); i++) {
192-
csvPrinter.print(csvRecord.get(i));
193-
}
180+
print(csvRecord, csvPrinter);
194181
assertEquals("a,b,c,d,,f|", stringBuilder.toString());
195182
}
196183
}
@@ -204,9 +191,7 @@ public void testParseWithTwoCharDelimiter4() throws Exception {
204191
try (CSVPrinter csvPrinter = new CSVPrinter(stringBuilder, CSVFormat.EXCEL);
205192
CSVParser csvParser = CSVParser.parse(in, CSVFormat.Builder.create().setDelimiter("~|").build())) {
206193
for (final CSVRecord csvRecord : csvParser) {
207-
for (int i = 0; i < csvRecord.size(); i++) {
208-
csvPrinter.print(csvRecord.get(i));
209-
}
194+
print(csvRecord, csvPrinter);
210195
assertEquals("a,b,c,d,,f~,|g", stringBuilder.toString());
211196
}
212197
}
@@ -221,9 +206,7 @@ public void testParseWithTwoCharDelimiterEndsWithDelimiter() throws Exception {
221206
try (CSVPrinter csvPrinter = new CSVPrinter(stringBuilder, CSVFormat.EXCEL);
222207
CSVParser csvParser = CSVParser.parse(in, CSVFormat.Builder.create().setDelimiter("~|").build())) {
223208
for (final CSVRecord csvRecord : csvParser) {
224-
for (int i = 0; i < csvRecord.size(); i++) {
225-
csvPrinter.print(csvRecord.get(i));
226-
}
209+
print(csvRecord, csvPrinter);
227210
assertEquals("a,b,c,d,,f,", stringBuilder.toString());
228211
}
229212
}

0 commit comments

Comments
 (0)