From dc5d034a47aa1f69612f20ec646b7a2f0050279e Mon Sep 17 00:00:00 2001 From: Gary Gregory Date: Sat, 3 Jul 2021 16:50:13 -0400 Subject: [PATCH] Use try-with-resources. --- .../org/apache/commons/csv/CSVPrinterTest.java | 15 +++++++-------- .../commons/csv/issues/JiraCsv167Test.java | 4 +--- .../commons/csv/issues/JiraCsv211Test.java | 16 ++++++++-------- .../commons/csv/issues/JiraCsv247Test.java | 13 +++++++------ .../apache/commons/csv/perf/PerformanceTest.java | 10 ++++++---- 5 files changed, 29 insertions(+), 29 deletions(-) diff --git a/src/test/java/org/apache/commons/csv/CSVPrinterTest.java b/src/test/java/org/apache/commons/csv/CSVPrinterTest.java index c5680b7f..357c4911 100644 --- a/src/test/java/org/apache/commons/csv/CSVPrinterTest.java +++ b/src/test/java/org/apache/commons/csv/CSVPrinterTest.java @@ -282,9 +282,8 @@ public class CSVPrinterTest { @Test public void testCloseWithFlushOn() throws IOException { try (final Writer writer = mock(Writer.class)) { - final CSVFormat csvFormat = CSVFormat.DEFAULT; @SuppressWarnings("resource") - final CSVPrinter csvPrinter = new CSVPrinter(writer, csvFormat); + final CSVPrinter csvPrinter = new CSVPrinter(writer, CSVFormat.DEFAULT); csvPrinter.close(true); verify(writer, times(1)).flush(); } @@ -328,8 +327,8 @@ public class CSVPrinterTest { @Test public void testCSV259() throws IOException { final StringWriter sw = new StringWriter(); - final Reader reader = new FileReader("src/test/resources/org/apache/commons/csv/CSV-259/sample.txt"); - try (final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withEscape('!').withQuote(null))) { + try (final Reader reader = new FileReader("src/test/resources/org/apache/commons/csv/CSV-259/sample.txt"); + final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withEscape('!').withQuote(null))) { printer.print(reader); assertEquals("x!,y!,z", sw.toString()); } @@ -624,12 +623,12 @@ public class CSVPrinterTest { try (final Connection connection = getH2Connection()) { setUpTable(connection); try (final Statement stmt = connection.createStatement(); - final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT)) { - printer.printRecords(stmt.executeQuery("select ID, NAME, TEXT from TEST")); + final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT); + final ResultSet resultSet = stmt.executeQuery("select ID, NAME, TEXT from TEST");) { + printer.printRecords(resultSet); } } - assertEquals("1,r1,\"long text 1\"" + recordSeparator + "2,r2,\"" + longText2 + "\"" + recordSeparator, - sw.toString()); + assertEquals("1,r1,\"long text 1\"" + recordSeparator + "2,r2,\"" + longText2 + "\"" + recordSeparator, sw.toString()); } @Test diff --git a/src/test/java/org/apache/commons/csv/issues/JiraCsv167Test.java b/src/test/java/org/apache/commons/csv/issues/JiraCsv167Test.java index 5b8a20e5..7194d7b9 100644 --- a/src/test/java/org/apache/commons/csv/issues/JiraCsv167Test.java +++ b/src/test/java/org/apache/commons/csv/issues/JiraCsv167Test.java @@ -20,7 +20,6 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import java.io.BufferedReader; import java.io.IOException; -import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; @@ -83,7 +82,6 @@ public class JiraCsv167Test { } private Reader getTestInput() { - final InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("org/apache/commons/csv/csv-167/sample1.csv"); - return new InputStreamReader(is); + return new InputStreamReader(ClassLoader.getSystemClassLoader().getResourceAsStream("org/apache/commons/csv/csv-167/sample1.csv")); } } diff --git a/src/test/java/org/apache/commons/csv/issues/JiraCsv211Test.java b/src/test/java/org/apache/commons/csv/issues/JiraCsv211Test.java index fc44261e..254d6132 100644 --- a/src/test/java/org/apache/commons/csv/issues/JiraCsv211Test.java +++ b/src/test/java/org/apache/commons/csv/issues/JiraCsv211Test.java @@ -37,12 +37,12 @@ public class JiraCsv211Test { assertEquals("ID\tName\tCountry\tAge\r\n1\tJane Doe\tUSA\t", formatted); final CSVFormat parseFormat = CSVFormat.DEFAULT.withDelimiter('\t').withFirstRecordAsHeader(); - final CSVParser parser = parseFormat.parse(new StringReader(formatted)); - for (final CSVRecord record : parser) { - assertEquals("1", record.get(0)); - assertEquals("Jane Doe", record.get(1)); - assertEquals("USA", record.get(2)); - assertEquals("", record.get(3)); - } - } + try (final CSVParser parser = parseFormat.parse(new StringReader(formatted))) { + for (final CSVRecord record : parser) { + assertEquals("1", record.get(0)); + assertEquals("Jane Doe", record.get(1)); + assertEquals("USA", record.get(2)); + assertEquals("", record.get(3)); + } + }} } diff --git a/src/test/java/org/apache/commons/csv/issues/JiraCsv247Test.java b/src/test/java/org/apache/commons/csv/issues/JiraCsv247Test.java index daa66be5..a6d17f39 100644 --- a/src/test/java/org/apache/commons/csv/issues/JiraCsv247Test.java +++ b/src/test/java/org/apache/commons/csv/issues/JiraCsv247Test.java @@ -65,17 +65,18 @@ public class JiraCsv247Test { public void testHeadersMissingThrowsWhenNotAllowingMissingColumnNames() throws Exception { final CSVFormat format = CSVFormat.DEFAULT.withHeader(); - assertFalse(format.getAllowMissingColumnNames(), - "By default we should not allow missing column names"); + assertFalse(format.getAllowMissingColumnNames(), "By default we should not allow missing column names"); assertThrows(IllegalArgumentException.class, () -> { - final Reader in = new StringReader("a,,c,d,e\n1,2,3,4,5\nv,w,x,y,z"); - format.parse(in); + try (final Reader in = new StringReader("a,,c,d,e\n1,2,3,4,5\nv,w,x,y,z")) { + format.parse(in); + } }, "1 missing column header is not allowed"); assertThrows(IllegalArgumentException.class, () -> { - final Reader in = new StringReader("a,,c,d,\n1,2,3,4,5\nv,w,x,y,z"); - format.parse(in); + try (final Reader in = new StringReader("a,,c,d,\n1,2,3,4,5\nv,w,x,y,z")) { + format.parse(in); + } }, "2+ missing column headers is not allowed!"); } } \ No newline at end of file diff --git a/src/test/java/org/apache/commons/csv/perf/PerformanceTest.java b/src/test/java/org/apache/commons/csv/perf/PerformanceTest.java index 37613253..33ffa950 100644 --- a/src/test/java/org/apache/commons/csv/perf/PerformanceTest.java +++ b/src/test/java/org/apache/commons/csv/perf/PerformanceTest.java @@ -94,10 +94,12 @@ public class PerformanceTest { public long testParseBigFile(final boolean traverseColumns) throws Exception { final long startMillis = System.currentTimeMillis(); - final long count = this.parse(this.createBufferedReader(), traverseColumns); - final long totalMillis = System.currentTimeMillis() - startMillis; - this.println(String.format("File parsed in %,d milliseconds with Commons CSV: %,d lines.", totalMillis, count)); - return totalMillis; + try (final BufferedReader reader = this.createBufferedReader()) { + final long count = this.parse(reader, traverseColumns); + final long totalMillis = System.currentTimeMillis() - startMillis; + this.println(String.format("File parsed in %,d milliseconds with Commons CSV: %,d lines.", totalMillis, count)); + return totalMillis; + } } @Test