Fix file handle leak in TestPublicSuffixMatcher

- Add message to assertion
- Use tryt-with-resources
This commit is contained in:
Gary Gregory 2024-08-23 08:23:59 -04:00
parent 883dbe3ce7
commit 044a71755c
3 changed files with 14 additions and 22 deletions

View File

@ -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);

View File

@ -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());

View File

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