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 @BeforeEach
void setUp() throws Exception { void setUp() throws Exception {
final ClassLoader classLoader = getClass().getClassLoader(); final ClassLoader classLoader = getClass().getClassLoader();
final InputStream in = classLoader.getResourceAsStream(SOURCE_FILE);
Assertions.assertNotNull(in);
final PublicSuffixList suffixList; final PublicSuffixList suffixList;
try { try (InputStream in = classLoader.getResourceAsStream(SOURCE_FILE)) {
Assertions.assertNotNull(in, SOURCE_FILE);
final PublicSuffixListParser parser = PublicSuffixListParser.INSTANCE; final PublicSuffixListParser parser = PublicSuffixListParser.INSTANCE;
suffixList = parser.parse(new InputStreamReader(in, StandardCharsets.UTF_8)); suffixList = parser.parse(new InputStreamReader(in, StandardCharsets.UTF_8));
} finally {
in.close();
} }
final PublicSuffixMatcher matcher = new PublicSuffixMatcher(suffixList.getRules(), suffixList.getExceptions()); final PublicSuffixMatcher matcher = new PublicSuffixMatcher(suffixList.getRules(), suffixList.getExceptions());
this.filter = new PublicSuffixDomainFilter(BasicDomainHandler.INSTANCE, matcher); this.filter = new PublicSuffixDomainFilter(BasicDomainHandler.INSTANCE, matcher);

View File

@ -39,17 +39,17 @@ import org.junit.jupiter.api.Test;
class TestPublicSuffixListParser { class TestPublicSuffixListParser {
private static final String SUFFIXLIST_TXT = "suffixlist.txt";
private static final String SUFFIXLIST2_TXT = "suffixlist2.txt";
@Test @Test
void testParse() throws Exception { void testParse() throws Exception {
final ClassLoader classLoader = getClass().getClassLoader(); final ClassLoader classLoader = getClass().getClassLoader();
final InputStream in = classLoader.getResourceAsStream("suffixlist.txt");
Assertions.assertNotNull(in);
final PublicSuffixList suffixList; final PublicSuffixList suffixList;
try { try (InputStream in = classLoader.getResourceAsStream(SUFFIXLIST_TXT)) {
Assertions.assertNotNull(in, SUFFIXLIST_TXT);
final PublicSuffixListParser parser = PublicSuffixListParser.INSTANCE; final PublicSuffixListParser parser = PublicSuffixListParser.INSTANCE;
suffixList = parser.parse(new InputStreamReader(in, StandardCharsets.UTF_8)); suffixList = parser.parse(new InputStreamReader(in, StandardCharsets.UTF_8));
} finally {
in.close();
} }
Assertions.assertNotNull(suffixList); Assertions.assertNotNull(suffixList);
Assertions.assertEquals(Arrays.asList("xx", "jp", "ac.jp", "*.tokyo.jp", "no", "h\u00E5.no"), suffixList.getRules()); Assertions.assertEquals(Arrays.asList("xx", "jp", "ac.jp", "*.tokyo.jp", "no", "h\u00E5.no"), suffixList.getRules());
@ -59,14 +59,11 @@ class TestPublicSuffixListParser {
@Test @Test
void testParseByType() throws Exception { void testParseByType() throws Exception {
final ClassLoader classLoader = getClass().getClassLoader(); final ClassLoader classLoader = getClass().getClassLoader();
final InputStream in = classLoader.getResourceAsStream("suffixlist2.txt");
Assertions.assertNotNull(in);
final List<PublicSuffixList> suffixLists; final List<PublicSuffixList> suffixLists;
try { try (InputStream in = classLoader.getResourceAsStream(SUFFIXLIST2_TXT)) {
Assertions.assertNotNull(in, SUFFIXLIST2_TXT);
final PublicSuffixListParser parser = PublicSuffixListParser.INSTANCE; final PublicSuffixListParser parser = PublicSuffixListParser.INSTANCE;
suffixLists = parser.parseByType(new InputStreamReader(in, StandardCharsets.UTF_8)); suffixLists = parser.parseByType(new InputStreamReader(in, StandardCharsets.UTF_8));
} finally {
in.close();
} }
Assertions.assertNotNull(suffixLists); Assertions.assertNotNull(suffixLists);
Assertions.assertEquals(2, suffixLists.size()); Assertions.assertEquals(2, suffixLists.size());

View File

@ -48,14 +48,12 @@ class TestPublicSuffixMatcher {
@BeforeEach @BeforeEach
void setUp() throws Exception { void setUp() throws Exception {
final ClassLoader classLoader = getClass().getClassLoader(); final ClassLoader classLoader = getClass().getClassLoader();
// Create a matcher using a custom crafted public suffix list file // Create a matcher using a custom crafted public suffix list file
final InputStream in = classLoader.getResourceAsStream(SOURCE_FILE); try (InputStream in = classLoader.getResourceAsStream(SOURCE_FILE)) {
Assertions.assertNotNull(in); Assertions.assertNotNull(in, SOURCE_FILE);
final List<PublicSuffixList> lists = PublicSuffixListParser.INSTANCE.parseByType( final List<PublicSuffixList> lists = PublicSuffixListParser.INSTANCE.parseByType(new InputStreamReader(in, StandardCharsets.UTF_8));
new InputStreamReader(in, StandardCharsets.UTF_8)); matcher = new PublicSuffixMatcher(lists);
matcher = new PublicSuffixMatcher(lists); }
// Create a matcher using the public suffix list file provided by Mozilla // 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 // 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 // target/classes so that it is on the classpath