Skip to content

Commit b25b250

Browse files
committed
CSVPrinter.printRecords(Iterable) knows how to use CSVFormat's maxRows
1 parent a557344 commit b25b250

File tree

5 files changed

+24
-17
lines changed

5 files changed

+24
-17
lines changed

src/changes/changes.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@
4848
<action type="add" dev="ggregory" due-to="Gary Gregory">Define and use Maven property commons.jmh.version.</action>
4949
<action type="add" dev="ggregory" due-to="Gary Gregory">Add CSVFormat.Builder.setMaxRows(long).</action>
5050
<action type="add" dev="ggregory" due-to="Gary Gregory">Add CSVFormat.getMaxRows().</action>
51-
<action type="add" dev="ggregory" due-to="Gary Gregory">CSVPrinter.printRecords(ResultSet) knows how to use CSVFormat's maxRows.</action>
51+
<action type="add" dev="ggregory" due-to="Gary Gregory">CSVPrinter.printRecords(ResultSet) knows how to use CSVFormat's maxRows.</action>
52+
<action type="add" dev="ggregory" due-to="Gary Gregory">CSVPrinter.printRecords(Iterable) knows how to use CSVFormat's maxRows.</action>
5253
<!-- UPDATE -->
5354
<action type="update" dev="ggregory" due-to="Gary Gregory">Bump com.opencsv:opencsv from 5.9 to 5.10.</action>
5455
<action type="update" dev="ggregory" due-to="Gary Gregory">Bump commons-codec:commons-codec from 1.17.2 to 1.18.0 #522.</action>

src/main/java/org/apache/commons/csv/CSVPrinter.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,11 @@ private void printRecordObject(final Object value) throws IOException {
383383
*/
384384
@SuppressWarnings("resource")
385385
public void printRecords(final Iterable<?> values) throws IOException {
386-
IOStream.of(values).forEachOrdered(this::printRecordObject);
386+
IOStream<?> stream = IOStream.of(values);
387+
if (format.getMaxRows() > 0) {
388+
stream = stream.limit(format.getMaxRows());
389+
}
390+
stream.forEachOrdered(this::printRecordObject);
387391
}
388392

389393
/**

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ public void testBackslashEscaping() throws IOException {
169169
try (CSVParser parser = CSVParser.parse(code, format)) {
170170
final List<CSVRecord> records = parser.getRecords();
171171
assertFalse(records.isEmpty());
172-
Utils.compare("Records do not match expected result", res, records);
172+
Utils.compare("Records do not match expected result", res, records, -1);
173173
}
174174
}
175175

@@ -192,7 +192,7 @@ public void testBackslashEscaping2() throws IOException {
192192
try (CSVParser parser = CSVParser.parse(code, format)) {
193193
final List<CSVRecord> records = parser.getRecords();
194194
assertFalse(records.isEmpty());
195-
Utils.compare("", res, records);
195+
Utils.compare("", res, records, -1);
196196
}
197197
}
198198

@@ -428,12 +428,12 @@ public void testDefaultFormat() throws IOException {
428428
try (CSVParser parser = CSVParser.parse(code, format)) {
429429
final List<CSVRecord> records = parser.getRecords();
430430
assertFalse(records.isEmpty());
431-
Utils.compare("Failed to parse without comments", res, records);
431+
Utils.compare("Failed to parse without comments", res, records, -1);
432432
format = CSVFormat.DEFAULT.withCommentMarker('#');
433433
}
434434
try (CSVParser parser = CSVParser.parse(code, format)) {
435435
final List<CSVRecord> records = parser.getRecords();
436-
Utils.compare("Failed to parse with comments", resComments, records);
436+
Utils.compare("Failed to parse with comments", resComments, records, -1);
437437
}
438438
}
439439

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

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ private void doOneRandom(final CSVFormat format) throws Exception {
146146
for (int i = 0; i < expected.length; i++) {
147147
expected[i] = expectNulls(expected[i], format);
148148
}
149-
Utils.compare("Printer output :" + printable(result), expected, parseResult);
149+
Utils.compare("Printer output :" + printable(result), expected, parseResult, -1);
150150
}
151151
}
152152

@@ -1470,7 +1470,7 @@ public void testPrintCSVParser() throws IOException {
14701470
try (CSVParser parser = CSVParser.parse(sw.toString(), format)) {
14711471
final List<CSVRecord> records = parser.getRecords();
14721472
assertFalse(records.isEmpty());
1473-
Utils.compare("Fail", res, records);
1473+
Utils.compare("Fail", res, records, -1);
14741474
}
14751475
}
14761476

@@ -1498,20 +1498,21 @@ public void testPrintCSVRecord() throws IOException {
14981498
try (CSVParser parser = CSVParser.parse(sw.toString(), format)) {
14991499
final List<CSVRecord> records = parser.getRecords();
15001500
assertFalse(records.isEmpty());
1501-
Utils.compare("Fail", res, records);
1501+
Utils.compare("Fail", res, records, -1);
15021502
}
15031503
}
15041504

1505-
@Test
1506-
public void testPrintCSVRecords() throws IOException {
1505+
@ParameterizedTest
1506+
@ValueSource(ints = { -1, 0, 3, 4, Integer.MAX_VALUE })
1507+
public void testPrintCSVRecords(final int maxRows) throws IOException {
15071508
// @formatter:off
15081509
final String code = "a1,b1\n" + // 1)
15091510
"a2,b2\n" + // 2)
15101511
"a3,b3\n" + // 3)
15111512
"a4,b4\n"; // 4)
15121513
// @formatter:on
1513-
final String[][] res = { { "a1", "b1" }, { "a2", "b2" }, { "a3", "b3" }, { "a4", "b4" } };
1514-
final CSVFormat format = CSVFormat.DEFAULT;
1514+
final String[][] expected = { { "a1", "b1" }, { "a2", "b2" }, { "a3", "b3" }, { "a4", "b4" } };
1515+
final CSVFormat format = CSVFormat.DEFAULT.builder().setMaxRows(maxRows).get();
15151516
final StringWriter sw = new StringWriter();
15161517
try (CSVPrinter printer = format.print(sw);
15171518
CSVParser parser = CSVParser.parse(code, format)) {
@@ -1521,7 +1522,7 @@ public void testPrintCSVRecords() throws IOException {
15211522
try (CSVParser parser = CSVParser.parse(sw.toString(), format)) {
15221523
final List<CSVRecord> records = parser.getRecords();
15231524
assertFalse(records.isEmpty());
1524-
Utils.compare("Fail", res, records);
1525+
Utils.compare("Fail", expected, records, maxRows);
15251526
}
15261527
}
15271528

@@ -1690,7 +1691,7 @@ public void testPrintRecordStream() throws IOException {
16901691
try (CSVParser parser = CSVParser.parse(sw.toString(), format)) {
16911692
final List<CSVRecord> records = parser.getRecords();
16921693
assertFalse(records.isEmpty());
1693-
Utils.compare("Fail", res, records);
1694+
Utils.compare("Fail", res, records, -1);
16941695
}
16951696
}
16961697

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,10 @@ final class Utils {
3737
* @param message the message to be displayed
3838
* @param expected the 2d array of expected results
3939
* @param actual the List of {@link CSVRecord} entries, each containing an array of values
40+
* @param maxRows the maximum number of rows expected, less than or equal to zero means no limit.
4041
*/
41-
public static void compare(final String message, final String[][] expected, final List<CSVRecord> actual) {
42-
final int expectedLength = expected.length;
42+
public static void compare(final String message, final String[][] expected, final List<CSVRecord> actual, final int maxRows) {
43+
final int expectedLength = maxRows > 0 ? Math.min(maxRows, expected.length) : expected.length;
4344
assertEquals(expectedLength, actual.size(), message + " - outer array size");
4445
for (int i = 0; i < expectedLength; i++) {
4546
assertArrayEquals(expected[i], actual.get(i).values(), message + " (entry " + i + ")");

0 commit comments

Comments
 (0)