From 2915f885e2e163300c28e14462140cd371e47b85 Mon Sep 17 00:00:00 2001 From: Isira Seneviratne Date: Tue, 23 Jul 2024 08:50:09 +0530 Subject: [PATCH] Use Files.list() in fuzz tests --- .../org/jsoup/integration/FuzzFixesIT.java | 27 +++++++++---------- .../org/jsoup/integration/FuzzFixesTest.java | 23 +++++++--------- 2 files changed, 22 insertions(+), 28 deletions(-) diff --git a/src/test/java/org/jsoup/integration/FuzzFixesIT.java b/src/test/java/org/jsoup/integration/FuzzFixesIT.java index d528eda793..e7bd20fcdf 100644 --- a/src/test/java/org/jsoup/integration/FuzzFixesIT.java +++ b/src/test/java/org/jsoup/integration/FuzzFixesIT.java @@ -8,8 +8,9 @@ import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.MethodSource; -import java.io.File; import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.stream.Stream; import static org.junit.jupiter.api.Assertions.assertNotNull; @@ -22,40 +23,36 @@ public class FuzzFixesIT { static int numIters = 50; static int timeout = 30; // external fuzzer is set to 60 for 100 runs - static File testDir = ParseTest.getFile("/fuzztests/"); + static Path testDir = ParseTest.getPath("/fuzztests/"); - private static Stream testFiles() { - File[] files = testDir.listFiles(); - assertNotNull(files); - assertTrue(files.length > 10); - - return Stream.of(files); + private static Stream testPaths() throws IOException { + return Files.list(testDir); } @Disabled // disabled, as these soak up build time and the outcome oughtn't change unless we are refactoring the tree builders. manually execute as desired. @ParameterizedTest - @MethodSource("testFiles") - void testHtmlParse(File file) throws IOException { + @MethodSource("testPaths") + void testHtmlParse(Path path) throws IOException { long startTime = System.currentTimeMillis(); long completeBy = startTime + timeout * 1000L; for (int i = 0; i < numIters; i++) { - Document doc = Jsoup.parse(file, "UTF-8", "https://example.com/"); + Document doc = Jsoup.parse(path, "UTF-8", "https://example.com/"); assertNotNull(doc); if (System.currentTimeMillis() > completeBy) - Assertions.fail(String.format("Timeout: only completed %d iters of [%s] in %d seconds", i, file.getName(), timeout)); + Assertions.fail(String.format("Timeout: only completed %d iters of [%s] in %d seconds", i, path.getFileName(), timeout)); } } @Disabled // disabled, as these soak up build time and the outcome oughtn't change unless we are refactoring the tree builders. manually execute as desired. @ParameterizedTest - @MethodSource("testFiles") - void testXmlParse(File file) throws IOException { + @MethodSource("testPaths") + void testXmlParse(Path path) throws IOException { long startTime = System.currentTimeMillis(); long completeBy = startTime + timeout * 1000L; for (int i = 0; i < numIters; i++) { - Document doc = Jsoup.parse(file, "UTF-8", "https://example.com/", Parser.xmlParser()); + Document doc = Jsoup.parse(path, "UTF-8", "https://example.com/", Parser.xmlParser()); assertNotNull(doc); if (System.currentTimeMillis() > completeBy) Assertions.fail(String.format("Timeout: only completed %d iters of [%s] in %d seconds", i, file.getName(), timeout)); diff --git a/src/test/java/org/jsoup/integration/FuzzFixesTest.java b/src/test/java/org/jsoup/integration/FuzzFixesTest.java index 0fd668f256..e4f52ad2a2 100644 --- a/src/test/java/org/jsoup/integration/FuzzFixesTest.java +++ b/src/test/java/org/jsoup/integration/FuzzFixesTest.java @@ -7,8 +7,9 @@ import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.MethodSource; -import java.io.File; import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.stream.Stream; import static org.junit.jupiter.api.Assertions.assertNotNull; @@ -20,12 +21,8 @@ */ public class FuzzFixesTest { - private static Stream testFiles() { - File[] files = FuzzFixesIT.testDir.listFiles(); - assertNotNull(files); - assertTrue(files.length > 10); - - return Stream.of(files); + private static Stream testPaths() throws IOException { + return Files.list(FuzzFixesIT.testDir); } @Test @@ -48,16 +45,16 @@ public void bookmark() { } @ParameterizedTest - @MethodSource("testFiles") - void testHtmlParse(File file) throws IOException { - Document doc = Jsoup.parse(file, "UTF-8", "https://example.com/"); + @MethodSource("testPaths") + void testHtmlParse(Path path) throws IOException { + Document doc = Jsoup.parse(path, "UTF-8", "https://example.com/"); assertNotNull(doc); } @ParameterizedTest - @MethodSource("testFiles") - void testXmlParse(File file) throws IOException { - Document doc = Jsoup.parse(file, "UTF-8", "https://example.com/", Parser.xmlParser()); + @MethodSource("testPaths") + void testXmlParse(Path path) throws IOException { + Document doc = Jsoup.parse(path, "UTF-8", "https://example.com/", Parser.xmlParser()); assertNotNull(doc); } }