Skip to content

Commit 8ad07df

Browse files
committed
Use try-with-resources
1 parent 5d494a6 commit 8ad07df

4 files changed

Lines changed: 169 additions & 142 deletions

File tree

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

Lines changed: 151 additions & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -901,22 +901,25 @@ public void testHeadersMissingOneColumnException() {
901901
@Test
902902
public void testHeadersWithNullColumnName() throws IOException {
903903
final Reader in = new StringReader("header1,null,header3\n1,2,3\n4,5,6");
904-
final Iterator<CSVRecord> records = CSVFormat.DEFAULT.withHeader().withNullString("null").withAllowMissingColumnNames().parse(in).iterator();
905-
final CSVRecord record = records.next();
906-
// Expect the null header to be missing
907-
assertEquals(Arrays.asList("header1", "header3"), record.getParser().getHeaderNames());
908-
assertEquals(2, record.getParser().getHeaderMap().size());
904+
try (final CSVParser parser = CSVFormat.DEFAULT.withHeader().withNullString("null").withAllowMissingColumnNames().parse(in)) {
905+
final Iterator<CSVRecord> records = parser.iterator();
906+
final CSVRecord record = records.next();
907+
// Expect the null header to be missing
908+
assertEquals(Arrays.asList("header1", "header3"), record.getParser().getHeaderNames());
909+
assertEquals(2, record.getParser().getHeaderMap().size());
910+
}
909911
}
910912

911913
@Test
912914
public void testIgnoreCaseHeaderMapping() throws Exception {
913915
final Reader reader = new StringReader("1,2,3");
914-
final Iterator<CSVRecord> records = CSVFormat.DEFAULT.withHeader("One", "TWO", "three").withIgnoreHeaderCase().parse(reader).iterator();
915-
final CSVRecord record = records.next();
916-
assertEquals("1", record.get("one"));
917-
assertEquals("2", record.get("two"));
918-
assertEquals("3", record.get("THREE"));
919-
}
916+
try (final CSVParser parser = CSVFormat.DEFAULT.withHeader("One", "TWO", "three").withIgnoreHeaderCase().parse(reader)) {
917+
final Iterator<CSVRecord> records = parser.iterator();
918+
final CSVRecord record = records.next();
919+
assertEquals("1", record.get("one"));
920+
assertEquals("2", record.get("two"));
921+
assertEquals("3", record.get("THREE"));
922+
}}
920923

921924
@Test
922925
public void testIgnoreEmptyLines() throws IOException {
@@ -938,20 +941,21 @@ public void testInvalidFormat() {
938941
public void testIterator() throws Exception {
939942
final Reader in = new StringReader("a,b,c\n1,2,3\nx,y,z");
940943

941-
final Iterator<CSVRecord> iterator = CSVFormat.DEFAULT.parse(in).iterator();
944+
try (final CSVParser parser = CSVFormat.DEFAULT.parse(in)) {
945+
final Iterator<CSVRecord> iterator = parser.iterator();
942946

943-
assertTrue(iterator.hasNext());
944-
assertThrows(UnsupportedOperationException.class, iterator::remove);
945-
assertArrayEquals(new String[] {"a", "b", "c"}, iterator.next().values());
946-
assertArrayEquals(new String[] {"1", "2", "3"}, iterator.next().values());
947-
assertTrue(iterator.hasNext());
948-
assertTrue(iterator.hasNext());
949-
assertTrue(iterator.hasNext());
950-
assertArrayEquals(new String[] {"x", "y", "z"}, iterator.next().values());
951-
assertFalse(iterator.hasNext());
947+
assertTrue(iterator.hasNext());
948+
assertThrows(UnsupportedOperationException.class, iterator::remove);
949+
assertArrayEquals(new String[] { "a", "b", "c" }, iterator.next().values());
950+
assertArrayEquals(new String[] { "1", "2", "3" }, iterator.next().values());
951+
assertTrue(iterator.hasNext());
952+
assertTrue(iterator.hasNext());
953+
assertTrue(iterator.hasNext());
954+
assertArrayEquals(new String[] { "x", "y", "z" }, iterator.next().values());
955+
assertFalse(iterator.hasNext());
952956

953-
assertThrows(NoSuchElementException.class, iterator::next);
954-
}
957+
assertThrows(NoSuchElementException.class, iterator::next);
958+
}}
955959

956960
@Test
957961
public void testIteratorSequenceBreaking() throws IOException {
@@ -1024,35 +1028,37 @@ public void testLineFeedEndings() throws IOException {
10241028
@Test
10251029
public void testMappedButNotSetAsOutlook2007ContactExport() throws Exception {
10261030
final Reader in = new StringReader("a,b,c\n1,2\nx,y,z");
1027-
final Iterator<CSVRecord> records = CSVFormat.DEFAULT.withHeader("A", "B", "C").withSkipHeaderRecord().parse(in).iterator();
1028-
CSVRecord record;
1031+
try (final CSVParser parser = CSVFormat.DEFAULT.withHeader("A", "B", "C").withSkipHeaderRecord().parse(in)) {
1032+
final Iterator<CSVRecord> records = parser.iterator();
1033+
CSVRecord record;
10291034

1030-
// 1st record
1031-
record = records.next();
1032-
assertTrue(record.isMapped("A"));
1033-
assertTrue(record.isMapped("B"));
1034-
assertTrue(record.isMapped("C"));
1035-
assertTrue(record.isSet("A"));
1036-
assertTrue(record.isSet("B"));
1037-
assertFalse(record.isSet("C"));
1038-
assertEquals("1", record.get("A"));
1039-
assertEquals("2", record.get("B"));
1040-
assertFalse(record.isConsistent());
1041-
1042-
// 2nd record
1043-
record = records.next();
1044-
assertTrue(record.isMapped("A"));
1045-
assertTrue(record.isMapped("B"));
1046-
assertTrue(record.isMapped("C"));
1047-
assertTrue(record.isSet("A"));
1048-
assertTrue(record.isSet("B"));
1049-
assertTrue(record.isSet("C"));
1050-
assertEquals("x", record.get("A"));
1051-
assertEquals("y", record.get("B"));
1052-
assertEquals("z", record.get("C"));
1053-
assertTrue(record.isConsistent());
1035+
// 1st record
1036+
record = records.next();
1037+
assertTrue(record.isMapped("A"));
1038+
assertTrue(record.isMapped("B"));
1039+
assertTrue(record.isMapped("C"));
1040+
assertTrue(record.isSet("A"));
1041+
assertTrue(record.isSet("B"));
1042+
assertFalse(record.isSet("C"));
1043+
assertEquals("1", record.get("A"));
1044+
assertEquals("2", record.get("B"));
1045+
assertFalse(record.isConsistent());
1046+
1047+
// 2nd record
1048+
record = records.next();
1049+
assertTrue(record.isMapped("A"));
1050+
assertTrue(record.isMapped("B"));
1051+
assertTrue(record.isMapped("C"));
1052+
assertTrue(record.isSet("A"));
1053+
assertTrue(record.isSet("B"));
1054+
assertTrue(record.isSet("C"));
1055+
assertEquals("x", record.get("A"));
1056+
assertEquals("y", record.get("B"));
1057+
assertEquals("z", record.get("C"));
1058+
assertTrue(record.isConsistent());
10541059

1055-
assertFalse(records.hasNext());
1060+
assertFalse(records.hasNext());
1061+
}
10561062
}
10571063

10581064
@Test
@@ -1113,9 +1119,10 @@ public void testNoHeaderMap() throws Exception {
11131119
public void testNotValueCSV() throws IOException {
11141120
final String source = "#";
11151121
final CSVFormat csvFormat = CSVFormat.DEFAULT.withCommentMarker('#');
1116-
final CSVParser csvParser = csvFormat.parse(new StringReader(source));
1117-
final CSVRecord csvRecord = csvParser.nextRecord();
1118-
assertNull(csvRecord);
1122+
try (final CSVParser csvParser = csvFormat.parse(new StringReader(source))) {
1123+
final CSVRecord csvRecord = csvParser.nextRecord();
1124+
assertNull(csvRecord);
1125+
}
11191126
}
11201127

11211128
@Test
@@ -1258,102 +1265,115 @@ public void testParseWithQuoteWithEscape() throws IOException {
12581265
public void testProvidedHeader() throws Exception {
12591266
final Reader in = new StringReader("a,b,c\n1,2,3\nx,y,z");
12601267

1261-
final Iterator<CSVRecord> records = CSVFormat.DEFAULT.withHeader("A", "B", "C").parse(in).iterator();
1268+
try (final CSVParser parser = CSVFormat.DEFAULT.withHeader("A", "B", "C").parse(in)) {
1269+
final Iterator<CSVRecord> records = parser.iterator();
12621270

1263-
for (int i = 0; i < 3; i++) {
1264-
assertTrue(records.hasNext());
1265-
final CSVRecord record = records.next();
1266-
assertTrue(record.isMapped("A"));
1267-
assertTrue(record.isMapped("B"));
1268-
assertTrue(record.isMapped("C"));
1269-
assertFalse(record.isMapped("NOT MAPPED"));
1270-
assertEquals(record.get(0), record.get("A"));
1271-
assertEquals(record.get(1), record.get("B"));
1272-
assertEquals(record.get(2), record.get("C"));
1273-
}
1271+
for (int i = 0; i < 3; i++) {
1272+
assertTrue(records.hasNext());
1273+
final CSVRecord record = records.next();
1274+
assertTrue(record.isMapped("A"));
1275+
assertTrue(record.isMapped("B"));
1276+
assertTrue(record.isMapped("C"));
1277+
assertFalse(record.isMapped("NOT MAPPED"));
1278+
assertEquals(record.get(0), record.get("A"));
1279+
assertEquals(record.get(1), record.get("B"));
1280+
assertEquals(record.get(2), record.get("C"));
1281+
}
12741282

1275-
assertFalse(records.hasNext());
1283+
assertFalse(records.hasNext());
1284+
}
12761285
}
12771286

12781287
@Test
12791288
public void testProvidedHeaderAuto() throws Exception {
12801289
final Reader in = new StringReader("a,b,c\n1,2,3\nx,y,z");
12811290

1282-
final Iterator<CSVRecord> records = CSVFormat.DEFAULT.withHeader().parse(in).iterator();
1291+
try (final CSVParser parser = CSVFormat.DEFAULT.withHeader().parse(in)) {
1292+
final Iterator<CSVRecord> records = parser.iterator();
1293+
1294+
for (int i = 0; i < 2; i++) {
1295+
assertTrue(records.hasNext());
1296+
final CSVRecord record = records.next();
1297+
assertTrue(record.isMapped("a"));
1298+
assertTrue(record.isMapped("b"));
1299+
assertTrue(record.isMapped("c"));
1300+
assertFalse(record.isMapped("NOT MAPPED"));
1301+
assertEquals(record.get(0), record.get("a"));
1302+
assertEquals(record.get(1), record.get("b"));
1303+
assertEquals(record.get(2), record.get("c"));
1304+
}
12831305

1284-
for (int i = 0; i < 2; i++) {
1285-
assertTrue(records.hasNext());
1286-
final CSVRecord record = records.next();
1287-
assertTrue(record.isMapped("a"));
1288-
assertTrue(record.isMapped("b"));
1289-
assertTrue(record.isMapped("c"));
1290-
assertFalse(record.isMapped("NOT MAPPED"));
1291-
assertEquals(record.get(0), record.get("a"));
1292-
assertEquals(record.get(1), record.get("b"));
1293-
assertEquals(record.get(2), record.get("c"));
1306+
assertFalse(records.hasNext());
12941307
}
1295-
1296-
assertFalse(records.hasNext());
12971308
}
12981309

12991310
@Test
13001311
public void testRepeatedHeadersAreReturnedInCSVRecordHeaderNames() throws IOException {
13011312
final Reader in = new StringReader("header1,header2,header1\n1,2,3\n4,5,6");
1302-
final Iterator<CSVRecord> records = CSVFormat.DEFAULT.withFirstRecordAsHeader().withTrim().parse(in).iterator();
1303-
final CSVRecord record = records.next();
1304-
assertEquals(Arrays.asList("header1", "header2", "header1"), record.getParser().getHeaderNames());
1305-
}
1313+
try (final CSVParser parser = CSVFormat.DEFAULT.withFirstRecordAsHeader().withTrim().parse(in)) {
1314+
final Iterator<CSVRecord> records = parser.iterator();
1315+
final CSVRecord record = records.next();
1316+
assertEquals(Arrays.asList("header1", "header2", "header1"), record.getParser().getHeaderNames());
1317+
}}
13061318

13071319
@Test
13081320
public void testRoundtrip() throws Exception {
13091321
final StringWriter out = new StringWriter();
1310-
try (final CSVPrinter printer = new CSVPrinter(out, CSVFormat.DEFAULT)) {
1311-
final String input = "a,b,c\r\n1,2,3\r\nx,y,z\r\n";
1312-
for (final CSVRecord record : CSVParser.parse(input, CSVFormat.DEFAULT)) {
1322+
final String data = "a,b,c\r\n1,2,3\r\nx,y,z\r\n";
1323+
try (final CSVPrinter printer = new CSVPrinter(out, CSVFormat.DEFAULT);
1324+
final CSVParser parse = CSVParser.parse(data, CSVFormat.DEFAULT)) {
1325+
for (final CSVRecord record : parse) {
13131326
printer.printRecord(record);
13141327
}
1315-
assertEquals(input, out.toString());
1328+
assertEquals(data, out.toString());
13161329
}
13171330
}
13181331

13191332
@Test
13201333
public void testSkipAutoHeader() throws Exception {
13211334
final Reader in = new StringReader("a,b,c\n1,2,3\nx,y,z");
1322-
final Iterator<CSVRecord> records = CSVFormat.DEFAULT.withHeader().parse(in).iterator();
1323-
final CSVRecord record = records.next();
1324-
assertEquals("1", record.get("a"));
1325-
assertEquals("2", record.get("b"));
1326-
assertEquals("3", record.get("c"));
1335+
try (final CSVParser parser = CSVFormat.DEFAULT.withHeader().parse(in)) {
1336+
final Iterator<CSVRecord> records = parser.iterator();
1337+
final CSVRecord record = records.next();
1338+
assertEquals("1", record.get("a"));
1339+
assertEquals("2", record.get("b"));
1340+
assertEquals("3", record.get("c"));
1341+
}
13271342
}
13281343

13291344
@Test
13301345
public void testSkipHeaderOverrideDuplicateHeaders() throws Exception {
13311346
final Reader in = new StringReader("a,a,a\n1,2,3\nx,y,z");
1332-
final Iterator<CSVRecord> records = CSVFormat.DEFAULT.withHeader("X", "Y", "Z").withSkipHeaderRecord().parse(in).iterator();
1333-
final CSVRecord record = records.next();
1334-
assertEquals("1", record.get("X"));
1335-
assertEquals("2", record.get("Y"));
1336-
assertEquals("3", record.get("Z"));
1337-
}
1347+
try (final CSVParser parser = CSVFormat.DEFAULT.withHeader("X", "Y", "Z").withSkipHeaderRecord().parse(in)) {
1348+
final Iterator<CSVRecord> records = parser.iterator();
1349+
final CSVRecord record = records.next();
1350+
assertEquals("1", record.get("X"));
1351+
assertEquals("2", record.get("Y"));
1352+
assertEquals("3", record.get("Z"));
1353+
}}
13381354

13391355
@Test
13401356
public void testSkipSetAltHeaders() throws Exception {
13411357
final Reader in = new StringReader("a,b,c\n1,2,3\nx,y,z");
1342-
final Iterator<CSVRecord> records = CSVFormat.DEFAULT.withHeader("X", "Y", "Z").withSkipHeaderRecord().parse(in).iterator();
1343-
final CSVRecord record = records.next();
1344-
assertEquals("1", record.get("X"));
1345-
assertEquals("2", record.get("Y"));
1346-
assertEquals("3", record.get("Z"));
1358+
try (final CSVParser parser = CSVFormat.DEFAULT.withHeader("X", "Y", "Z").withSkipHeaderRecord().parse(in)) {
1359+
final Iterator<CSVRecord> records = parser.iterator();
1360+
final CSVRecord record = records.next();
1361+
assertEquals("1", record.get("X"));
1362+
assertEquals("2", record.get("Y"));
1363+
assertEquals("3", record.get("Z"));
1364+
}
13471365
}
13481366

13491367
@Test
13501368
public void testSkipSetHeader() throws Exception {
13511369
final Reader in = new StringReader("a,b,c\n1,2,3\nx,y,z");
1352-
final Iterator<CSVRecord> records = CSVFormat.DEFAULT.withHeader("a", "b", "c").withSkipHeaderRecord().parse(in).iterator();
1353-
final CSVRecord record = records.next();
1354-
assertEquals("1", record.get("a"));
1355-
assertEquals("2", record.get("b"));
1356-
assertEquals("3", record.get("c"));
1370+
try (final CSVParser parser = CSVFormat.DEFAULT.withHeader("a", "b", "c").withSkipHeaderRecord().parse(in)) {
1371+
final Iterator<CSVRecord> records = parser.iterator();
1372+
final CSVRecord record = records.next();
1373+
assertEquals("1", record.get("a"));
1374+
assertEquals("2", record.get("b"));
1375+
assertEquals("3", record.get("c"));
1376+
}
13571377
}
13581378

13591379
@Test
@@ -1377,34 +1397,38 @@ public void testStartWithEmptyLinesThenHeaders() throws Exception {
13771397
@Test
13781398
public void testStream() throws Exception {
13791399
final Reader in = new StringReader("a,b,c\n1,2,3\nx,y,z");
1380-
final List<CSVRecord> list = CSVFormat.DEFAULT.parse(in).stream().collect(Collectors.toList());
1381-
assertFalse(list.isEmpty());
1382-
assertArrayEquals(new String[] {"a", "b", "c"}, list.get(0).values());
1383-
assertArrayEquals(new String[] {"1", "2", "3"}, list.get(1).values());
1384-
assertArrayEquals(new String[] {"x", "y", "z"}, list.get(2).values());
1385-
}
1400+
try (final CSVParser parser = CSVFormat.DEFAULT.parse(in)) {
1401+
final List<CSVRecord> list = parser.stream().collect(Collectors.toList());
1402+
assertFalse(list.isEmpty());
1403+
assertArrayEquals(new String[] { "a", "b", "c" }, list.get(0).values());
1404+
assertArrayEquals(new String[] { "1", "2", "3" }, list.get(1).values());
1405+
assertArrayEquals(new String[] { "x", "y", "z" }, list.get(2).values());
1406+
}}
13861407

13871408
@Test
13881409
public void testTrailingDelimiter() throws Exception {
13891410
final Reader in = new StringReader("a,a,a,\n\"1\",\"2\",\"3\",\nx,y,z,");
1390-
final Iterator<CSVRecord> records = CSVFormat.DEFAULT.withHeader("X", "Y", "Z").withSkipHeaderRecord().withTrailingDelimiter().parse(in).iterator();
1391-
final CSVRecord record = records.next();
1392-
assertEquals("1", record.get("X"));
1393-
assertEquals("2", record.get("Y"));
1394-
assertEquals("3", record.get("Z"));
1395-
assertEquals(3, record.size());
1411+
try (final CSVParser parser = CSVFormat.DEFAULT.withHeader("X", "Y", "Z").withSkipHeaderRecord().withTrailingDelimiter().parse(in)) {
1412+
final Iterator<CSVRecord> records = parser.iterator();
1413+
final CSVRecord record = records.next();
1414+
assertEquals("1", record.get("X"));
1415+
assertEquals("2", record.get("Y"));
1416+
assertEquals("3", record.get("Z"));
1417+
assertEquals(3, record.size());
1418+
}
13961419
}
13971420

13981421
@Test
13991422
public void testTrim() throws Exception {
14001423
final Reader in = new StringReader("a,a,a\n\" 1 \",\" 2 \",\" 3 \"\nx,y,z");
1401-
final Iterator<CSVRecord> records = CSVFormat.DEFAULT.withHeader("X", "Y", "Z").withSkipHeaderRecord().withTrim().parse(in).iterator();
1402-
final CSVRecord record = records.next();
1403-
assertEquals("1", record.get("X"));
1404-
assertEquals("2", record.get("Y"));
1405-
assertEquals("3", record.get("Z"));
1406-
assertEquals(3, record.size());
1407-
}
1424+
try (final CSVParser parser = CSVFormat.DEFAULT.withHeader("X", "Y", "Z").withSkipHeaderRecord().withTrim().parse(in)) {
1425+
final Iterator<CSVRecord> records = parser.iterator();
1426+
final CSVRecord record = records.next();
1427+
assertEquals("1", record.get("X"));
1428+
assertEquals("2", record.get("Y"));
1429+
assertEquals("3", record.get("Z"));
1430+
assertEquals(3, record.size());
1431+
}}
14081432

14091433
private void validateLineNumbers(final String lineSeparator) throws IOException {
14101434
try (final CSVParser parser = CSVParser.parse("a" + lineSeparator + "b" + lineSeparator + "c", CSVFormat.DEFAULT.withRecordSeparator(lineSeparator))) {

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1725,7 +1725,9 @@ public void testTrimOnTwoColumns() throws IOException {
17251725
}
17261726

17271727
private String[] toFirstRecordValues(final String expected, final CSVFormat format) throws IOException {
1728-
return CSVParser.parse(expected, format).getRecords().get(0).values();
1728+
try (final CSVParser parser = CSVParser.parse(expected, format)) {
1729+
return parser.getRecords().get(0).values();
1730+
}
17291731
}
17301732

17311733
private void tryFormat(final List<String> list, final Character quote, final Character escape, final String expected) throws IOException {

0 commit comments

Comments
 (0)