Skip to content

Commit 044a717

Browse files
committed
Fix file handle leak in TestPublicSuffixMatcher
- Add message to assertion - Use tryt-with-resources
1 parent 883dbe3 commit 044a717

File tree

3 files changed

+14
-22
lines changed

3 files changed

+14
-22
lines changed

httpclient5/src/test/java/org/apache/hc/client5/http/impl/cookie/TestPublicSuffixListParser.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,11 @@ class TestPublicSuffixListParser {
4949
@BeforeEach
5050
void setUp() throws Exception {
5151
final ClassLoader classLoader = getClass().getClassLoader();
52-
final InputStream in = classLoader.getResourceAsStream(SOURCE_FILE);
53-
Assertions.assertNotNull(in);
5452
final PublicSuffixList suffixList;
55-
try {
53+
try (InputStream in = classLoader.getResourceAsStream(SOURCE_FILE)) {
54+
Assertions.assertNotNull(in, SOURCE_FILE);
5655
final PublicSuffixListParser parser = PublicSuffixListParser.INSTANCE;
5756
suffixList = parser.parse(new InputStreamReader(in, StandardCharsets.UTF_8));
58-
} finally {
59-
in.close();
6057
}
6158
final PublicSuffixMatcher matcher = new PublicSuffixMatcher(suffixList.getRules(), suffixList.getExceptions());
6259
this.filter = new PublicSuffixDomainFilter(BasicDomainHandler.INSTANCE, matcher);

httpclient5/src/test/java/org/apache/hc/client5/http/psl/TestPublicSuffixListParser.java

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,17 @@
3939

4040
class TestPublicSuffixListParser {
4141

42+
private static final String SUFFIXLIST_TXT = "suffixlist.txt";
43+
private static final String SUFFIXLIST2_TXT = "suffixlist2.txt";
44+
4245
@Test
4346
void testParse() throws Exception {
4447
final ClassLoader classLoader = getClass().getClassLoader();
45-
final InputStream in = classLoader.getResourceAsStream("suffixlist.txt");
46-
Assertions.assertNotNull(in);
4748
final PublicSuffixList suffixList;
48-
try {
49+
try (InputStream in = classLoader.getResourceAsStream(SUFFIXLIST_TXT)) {
50+
Assertions.assertNotNull(in, SUFFIXLIST_TXT);
4951
final PublicSuffixListParser parser = PublicSuffixListParser.INSTANCE;
5052
suffixList = parser.parse(new InputStreamReader(in, StandardCharsets.UTF_8));
51-
} finally {
52-
in.close();
5353
}
5454
Assertions.assertNotNull(suffixList);
5555
Assertions.assertEquals(Arrays.asList("xx", "jp", "ac.jp", "*.tokyo.jp", "no", "h\u00E5.no"), suffixList.getRules());
@@ -59,14 +59,11 @@ void testParse() throws Exception {
5959
@Test
6060
void testParseByType() throws Exception {
6161
final ClassLoader classLoader = getClass().getClassLoader();
62-
final InputStream in = classLoader.getResourceAsStream("suffixlist2.txt");
63-
Assertions.assertNotNull(in);
6462
final List<PublicSuffixList> suffixLists;
65-
try {
63+
try (InputStream in = classLoader.getResourceAsStream(SUFFIXLIST2_TXT)) {
64+
Assertions.assertNotNull(in, SUFFIXLIST2_TXT);
6665
final PublicSuffixListParser parser = PublicSuffixListParser.INSTANCE;
6766
suffixLists = parser.parseByType(new InputStreamReader(in, StandardCharsets.UTF_8));
68-
} finally {
69-
in.close();
7067
}
7168
Assertions.assertNotNull(suffixLists);
7269
Assertions.assertEquals(2, suffixLists.size());

httpclient5/src/test/java/org/apache/hc/client5/http/psl/TestPublicSuffixMatcher.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,12 @@ class TestPublicSuffixMatcher {
4848
@BeforeEach
4949
void setUp() throws Exception {
5050
final ClassLoader classLoader = getClass().getClassLoader();
51-
5251
// Create a matcher using a custom crafted public suffix list file
53-
final InputStream in = classLoader.getResourceAsStream(SOURCE_FILE);
54-
Assertions.assertNotNull(in);
55-
final List<PublicSuffixList> lists = PublicSuffixListParser.INSTANCE.parseByType(
56-
new InputStreamReader(in, StandardCharsets.UTF_8));
57-
matcher = new PublicSuffixMatcher(lists);
58-
52+
try (InputStream in = classLoader.getResourceAsStream(SOURCE_FILE)) {
53+
Assertions.assertNotNull(in, SOURCE_FILE);
54+
final List<PublicSuffixList> lists = PublicSuffixListParser.INSTANCE.parseByType(new InputStreamReader(in, StandardCharsets.UTF_8));
55+
matcher = new PublicSuffixMatcher(lists);
56+
}
5957
// Create a matcher using the public suffix list file provided by Mozilla
6058
// Note: the test requires `mvn generate-resources` to have been called to fetch the Mozilla file into
6159
// target/classes so that it is on the classpath

0 commit comments

Comments
 (0)