Skip to content

Commit dc5d034

Browse files
committed
Use try-with-resources.
1 parent 5d16917 commit dc5d034

5 files changed

Lines changed: 29 additions & 29 deletions

File tree

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

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -282,9 +282,8 @@ public void testCloseWithFlushOff() throws IOException {
282282
@Test
283283
public void testCloseWithFlushOn() throws IOException {
284284
try (final Writer writer = mock(Writer.class)) {
285-
final CSVFormat csvFormat = CSVFormat.DEFAULT;
286285
@SuppressWarnings("resource")
287-
final CSVPrinter csvPrinter = new CSVPrinter(writer, csvFormat);
286+
final CSVPrinter csvPrinter = new CSVPrinter(writer, CSVFormat.DEFAULT);
288287
csvPrinter.close(true);
289288
verify(writer, times(1)).flush();
290289
}
@@ -328,8 +327,8 @@ public void testCSV135() throws IOException {
328327
@Test
329328
public void testCSV259() throws IOException {
330329
final StringWriter sw = new StringWriter();
331-
final Reader reader = new FileReader("src/test/resources/org/apache/commons/csv/CSV-259/sample.txt");
332-
try (final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withEscape('!').withQuote(null))) {
330+
try (final Reader reader = new FileReader("src/test/resources/org/apache/commons/csv/CSV-259/sample.txt");
331+
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withEscape('!').withQuote(null))) {
333332
printer.print(reader);
334333
assertEquals("x!,y!,z", sw.toString());
335334
}
@@ -624,12 +623,12 @@ public void testJdbcPrinter() throws IOException, ClassNotFoundException, SQLExc
624623
try (final Connection connection = getH2Connection()) {
625624
setUpTable(connection);
626625
try (final Statement stmt = connection.createStatement();
627-
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT)) {
628-
printer.printRecords(stmt.executeQuery("select ID, NAME, TEXT from TEST"));
626+
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT);
627+
final ResultSet resultSet = stmt.executeQuery("select ID, NAME, TEXT from TEST");) {
628+
printer.printRecords(resultSet);
629629
}
630630
}
631-
assertEquals("1,r1,\"long text 1\"" + recordSeparator + "2,r2,\"" + longText2 + "\"" + recordSeparator,
632-
sw.toString());
631+
assertEquals("1,r1,\"long text 1\"" + recordSeparator + "2,r2,\"" + longText2 + "\"" + recordSeparator, sw.toString());
633632
}
634633

635634
@Test

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
import java.io.BufferedReader;
2222
import java.io.IOException;
23-
import java.io.InputStream;
2423
import java.io.InputStreamReader;
2524
import java.io.Reader;
2625

@@ -83,7 +82,6 @@ public void parse() throws IOException {
8382
}
8483

8584
private Reader getTestInput() {
86-
final InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("org/apache/commons/csv/csv-167/sample1.csv");
87-
return new InputStreamReader(is);
85+
return new InputStreamReader(ClassLoader.getSystemClassLoader().getResourceAsStream("org/apache/commons/csv/csv-167/sample1.csv"));
8886
}
8987
}

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,12 @@ public void testJiraCsv211Format() throws IOException {
3737
assertEquals("ID\tName\tCountry\tAge\r\n1\tJane Doe\tUSA\t", formatted);
3838

3939
final CSVFormat parseFormat = CSVFormat.DEFAULT.withDelimiter('\t').withFirstRecordAsHeader();
40-
final CSVParser parser = parseFormat.parse(new StringReader(formatted));
41-
for (final CSVRecord record : parser) {
42-
assertEquals("1", record.get(0));
43-
assertEquals("Jane Doe", record.get(1));
44-
assertEquals("USA", record.get(2));
45-
assertEquals("", record.get(3));
46-
}
47-
}
40+
try (final CSVParser parser = parseFormat.parse(new StringReader(formatted))) {
41+
for (final CSVRecord record : parser) {
42+
assertEquals("1", record.get(0));
43+
assertEquals("Jane Doe", record.get(1));
44+
assertEquals("USA", record.get(2));
45+
assertEquals("", record.get(3));
46+
}
47+
}}
4848
}

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,17 +65,18 @@ record = iterator.next();
6565
public void testHeadersMissingThrowsWhenNotAllowingMissingColumnNames() throws Exception {
6666
final CSVFormat format = CSVFormat.DEFAULT.withHeader();
6767

68-
assertFalse(format.getAllowMissingColumnNames(),
69-
"By default we should not allow missing column names");
68+
assertFalse(format.getAllowMissingColumnNames(), "By default we should not allow missing column names");
7069

7170
assertThrows(IllegalArgumentException.class, () -> {
72-
final Reader in = new StringReader("a,,c,d,e\n1,2,3,4,5\nv,w,x,y,z");
73-
format.parse(in);
71+
try (final Reader in = new StringReader("a,,c,d,e\n1,2,3,4,5\nv,w,x,y,z")) {
72+
format.parse(in);
73+
}
7474
}, "1 missing column header is not allowed");
7575

7676
assertThrows(IllegalArgumentException.class, () -> {
77-
final Reader in = new StringReader("a,,c,d,\n1,2,3,4,5\nv,w,x,y,z");
78-
format.parse(in);
77+
try (final Reader in = new StringReader("a,,c,d,\n1,2,3,4,5\nv,w,x,y,z")) {
78+
format.parse(in);
79+
}
7980
}, "2+ missing column headers is not allowed!");
8081
}
8182
}

src/test/java/org/apache/commons/csv/perf/PerformanceTest.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,12 @@ private long readAll(final BufferedReader in) throws IOException {
9494

9595
public long testParseBigFile(final boolean traverseColumns) throws Exception {
9696
final long startMillis = System.currentTimeMillis();
97-
final long count = this.parse(this.createBufferedReader(), traverseColumns);
98-
final long totalMillis = System.currentTimeMillis() - startMillis;
99-
this.println(String.format("File parsed in %,d milliseconds with Commons CSV: %,d lines.", totalMillis, count));
100-
return totalMillis;
97+
try (final BufferedReader reader = this.createBufferedReader()) {
98+
final long count = this.parse(reader, traverseColumns);
99+
final long totalMillis = System.currentTimeMillis() - startMillis;
100+
this.println(String.format("File parsed in %,d milliseconds with Commons CSV: %,d lines.", totalMillis, count));
101+
return totalMillis;
102+
}
101103
}
102104

103105
@Test

0 commit comments

Comments
 (0)