diff --git a/httpclient5/src/test/java/org/apache/hc/client5/http/impl/cookie/TestPublicSuffixListParser.java b/httpclient5/src/test/java/org/apache/hc/client5/http/impl/cookie/TestPublicSuffixListParser.java index 60cf10556..1ca8e1bb9 100644 --- a/httpclient5/src/test/java/org/apache/hc/client5/http/impl/cookie/TestPublicSuffixListParser.java +++ b/httpclient5/src/test/java/org/apache/hc/client5/http/impl/cookie/TestPublicSuffixListParser.java @@ -49,14 +49,11 @@ class TestPublicSuffixListParser { @BeforeEach void setUp() throws Exception { final ClassLoader classLoader = getClass().getClassLoader(); - final InputStream in = classLoader.getResourceAsStream(SOURCE_FILE); - Assertions.assertNotNull(in); final PublicSuffixList suffixList; - try { + try (InputStream in = classLoader.getResourceAsStream(SOURCE_FILE)) { + Assertions.assertNotNull(in, SOURCE_FILE); final PublicSuffixListParser parser = PublicSuffixListParser.INSTANCE; suffixList = parser.parse(new InputStreamReader(in, StandardCharsets.UTF_8)); - } finally { - in.close(); } final PublicSuffixMatcher matcher = new PublicSuffixMatcher(suffixList.getRules(), suffixList.getExceptions()); this.filter = new PublicSuffixDomainFilter(BasicDomainHandler.INSTANCE, matcher); diff --git a/httpclient5/src/test/java/org/apache/hc/client5/http/psl/TestPublicSuffixListParser.java b/httpclient5/src/test/java/org/apache/hc/client5/http/psl/TestPublicSuffixListParser.java index 3549751cc..395fc6c0d 100644 --- a/httpclient5/src/test/java/org/apache/hc/client5/http/psl/TestPublicSuffixListParser.java +++ b/httpclient5/src/test/java/org/apache/hc/client5/http/psl/TestPublicSuffixListParser.java @@ -39,17 +39,17 @@ import org.junit.jupiter.api.Test; class TestPublicSuffixListParser { + private static final String SUFFIXLIST_TXT = "suffixlist.txt"; + private static final String SUFFIXLIST2_TXT = "suffixlist2.txt"; + @Test void testParse() throws Exception { final ClassLoader classLoader = getClass().getClassLoader(); - final InputStream in = classLoader.getResourceAsStream("suffixlist.txt"); - Assertions.assertNotNull(in); final PublicSuffixList suffixList; - try { + try (InputStream in = classLoader.getResourceAsStream(SUFFIXLIST_TXT)) { + Assertions.assertNotNull(in, SUFFIXLIST_TXT); final PublicSuffixListParser parser = PublicSuffixListParser.INSTANCE; suffixList = parser.parse(new InputStreamReader(in, StandardCharsets.UTF_8)); - } finally { - in.close(); } Assertions.assertNotNull(suffixList); Assertions.assertEquals(Arrays.asList("xx", "jp", "ac.jp", "*.tokyo.jp", "no", "h\u00E5.no"), suffixList.getRules()); @@ -59,14 +59,11 @@ class TestPublicSuffixListParser { @Test void testParseByType() throws Exception { final ClassLoader classLoader = getClass().getClassLoader(); - final InputStream in = classLoader.getResourceAsStream("suffixlist2.txt"); - Assertions.assertNotNull(in); final List suffixLists; - try { + try (InputStream in = classLoader.getResourceAsStream(SUFFIXLIST2_TXT)) { + Assertions.assertNotNull(in, SUFFIXLIST2_TXT); final PublicSuffixListParser parser = PublicSuffixListParser.INSTANCE; suffixLists = parser.parseByType(new InputStreamReader(in, StandardCharsets.UTF_8)); - } finally { - in.close(); } Assertions.assertNotNull(suffixLists); Assertions.assertEquals(2, suffixLists.size()); diff --git a/httpclient5/src/test/java/org/apache/hc/client5/http/psl/TestPublicSuffixMatcher.java b/httpclient5/src/test/java/org/apache/hc/client5/http/psl/TestPublicSuffixMatcher.java index 1ecf6a483..3a8e4f34b 100644 --- a/httpclient5/src/test/java/org/apache/hc/client5/http/psl/TestPublicSuffixMatcher.java +++ b/httpclient5/src/test/java/org/apache/hc/client5/http/psl/TestPublicSuffixMatcher.java @@ -48,14 +48,12 @@ class TestPublicSuffixMatcher { @BeforeEach void setUp() throws Exception { final ClassLoader classLoader = getClass().getClassLoader(); - // Create a matcher using a custom crafted public suffix list file - final InputStream in = classLoader.getResourceAsStream(SOURCE_FILE); - Assertions.assertNotNull(in); - final List lists = PublicSuffixListParser.INSTANCE.parseByType( - new InputStreamReader(in, StandardCharsets.UTF_8)); - matcher = new PublicSuffixMatcher(lists); - + try (InputStream in = classLoader.getResourceAsStream(SOURCE_FILE)) { + Assertions.assertNotNull(in, SOURCE_FILE); + final List lists = PublicSuffixListParser.INSTANCE.parseByType(new InputStreamReader(in, StandardCharsets.UTF_8)); + matcher = new PublicSuffixMatcher(lists); + } // Create a matcher using the public suffix list file provided by Mozilla // Note: the test requires `mvn generate-resources` to have been called to fetch the Mozilla file into // target/classes so that it is on the classpath