From 236b0f4f84f1629df4bd68540cc9597fb8583aa4 Mon Sep 17 00:00:00 2001 From: Gary Gregory Date: Wed, 26 Oct 2022 10:12:58 -0400 Subject: [PATCH] Add @SuppressWarnings("resource") in test as narrowly as possible --- .../org/apache/commons/csv/CSVParserTest.java | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/test/java/org/apache/commons/csv/CSVParserTest.java b/src/test/java/org/apache/commons/csv/CSVParserTest.java index ae4769f0..dfae3094 100644 --- a/src/test/java/org/apache/commons/csv/CSVParserTest.java +++ b/src/test/java/org/apache/commons/csv/CSVParserTest.java @@ -905,8 +905,10 @@ public class CSVParserTest { final Iterator records = parser.iterator(); final CSVRecord record = records.next(); // Expect the null header to be missing - assertEquals(Arrays.asList("header1", "header3"), record.getParser().getHeaderNames()); - assertEquals(2, record.getParser().getHeaderMap().size()); + @SuppressWarnings("resource") + final CSVParser recordParser = record.getParser(); + assertEquals(Arrays.asList("header1", "header3"), recordParser.getHeaderNames()); + assertEquals(2, recordParser.getHeaderMap().size()); } } @@ -1132,7 +1134,8 @@ public class CSVParserTest { final CSVFormat format = CSVFormat.DEFAULT.withHeader("A", "B", "C", "D"); final Charset charset = StandardCharsets.UTF_8; - try (final CSVParser parser = CSVParser.parse(new InputStreamReader(url.openStream(), charset), format)) { + try (@SuppressWarnings("resource") // CSVParser closes the input resource + final CSVParser parser = CSVParser.parse(new InputStreamReader(url.openStream(), charset), format)) { parseFully(parser); } try (final CSVParser parser = CSVParser.parse(new String(Files.readAllBytes(Paths.get(url.toURI())), charset), format)) { @@ -1141,7 +1144,8 @@ public class CSVParserTest { try (final CSVParser parser = CSVParser.parse(new File(url.toURI()), charset, format)) { parseFully(parser); } - try (final CSVParser parser = CSVParser.parse(url.openStream(), charset, format)) { + try (@SuppressWarnings("resource") // CSVParser closes the input resource + final CSVParser parser = CSVParser.parse(url.openStream(), charset, format)) { parseFully(parser); } try (final CSVParser parser = CSVParser.parse(Paths.get(url.toURI()), charset, format)) { @@ -1313,7 +1317,9 @@ public class CSVParserTest { try (final CSVParser parser = CSVFormat.DEFAULT.withFirstRecordAsHeader().withTrim().parse(in)) { final Iterator records = parser.iterator(); final CSVRecord record = records.next(); - assertEquals(Arrays.asList("header1", "header2", "header1"), record.getParser().getHeaderNames()); + @SuppressWarnings("resource") + final CSVParser recordParser = record.getParser(); + assertEquals(Arrays.asList("header1", "header2", "header1"), recordParser.getHeaderNames()); }} @Test