You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by gg...@apache.org on 2024/08/23 12:24:04 UTC

(httpcomponents-client) branch master updated: Fix file handle leak in TestPublicSuffixMatcher

This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/httpcomponents-client.git


The following commit(s) were added to refs/heads/master by this push:
     new 044a71755 Fix file handle leak in TestPublicSuffixMatcher
044a71755 is described below

commit 044a71755c85a54dee865fb7bcb54e8da9335289
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Fri Aug 23 08:23:59 2024 -0400

    Fix file handle leak in TestPublicSuffixMatcher
    
    - Add message to assertion
    - Use tryt-with-resources
---
 .../http/impl/cookie/TestPublicSuffixListParser.java    |  7 ++-----
 .../hc/client5/http/psl/TestPublicSuffixListParser.java | 17 +++++++----------
 .../hc/client5/http/psl/TestPublicSuffixMatcher.java    | 12 +++++-------
 3 files changed, 14 insertions(+), 22 deletions(-)

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<PublicSuffixList> 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<PublicSuffixList> 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<PublicSuffixList> 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