From 6a3036f1a49a9ee9348b23265fc9ca55f516217f Mon Sep 17 00:00:00 2001 From: Isira Seneviratne Date: Sun, 21 May 2023 06:07:13 +0530 Subject: [PATCH] Use Files methods in tests. --- .../newpipe/downloader/MockDownloader.java | 20 +++++------ .../downloader/RecordingDownloader.java | 36 ++++++++----------- 2 files changed, 22 insertions(+), 34 deletions(-) diff --git a/extractor/src/test/java/org/schabi/newpipe/downloader/MockDownloader.java b/extractor/src/test/java/org/schabi/newpipe/downloader/MockDownloader.java index bcf72b213..4400de314 100644 --- a/extractor/src/test/java/org/schabi/newpipe/downloader/MockDownloader.java +++ b/extractor/src/test/java/org/schabi/newpipe/downloader/MockDownloader.java @@ -6,11 +6,9 @@ import org.schabi.newpipe.extractor.downloader.Downloader; import org.schabi.newpipe.extractor.downloader.Request; import org.schabi.newpipe.extractor.downloader.Response; -import java.io.File; -import java.io.FileInputStream; -import java.io.InputStreamReader; import java.io.IOException; -import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Paths; import java.util.HashMap; import java.util.Map; @@ -29,16 +27,14 @@ class MockDownloader extends Downloader { public MockDownloader(@Nonnull final String path) throws IOException { this.path = path; this.mocks = new HashMap<>(); - final File[] files = new File(path).listFiles(); - if (files != null) { - for (final File file : files) { - if (file.getName().startsWith(RecordingDownloader.FILE_NAME_PREFIX)) { - final InputStreamReader reader = new InputStreamReader(new FileInputStream( - file), StandardCharsets.UTF_8); - final TestRequestResponse response = new GsonBuilder() + try (final var directoryStream = Files.newDirectoryStream(Paths.get(path), + entry -> entry.getFileName().toString() + .startsWith(RecordingDownloader.FILE_NAME_PREFIX))) { + for (final var entry : directoryStream) { + try (final var reader = Files.newBufferedReader(entry)) { + final var response = new GsonBuilder() .create() .fromJson(reader, TestRequestResponse.class); - reader.close(); mocks.put(response.getRequest(), response.getResponse()); } } diff --git a/extractor/src/test/java/org/schabi/newpipe/downloader/RecordingDownloader.java b/extractor/src/test/java/org/schabi/newpipe/downloader/RecordingDownloader.java index 7d649732b..2e0e29d32 100644 --- a/extractor/src/test/java/org/schabi/newpipe/downloader/RecordingDownloader.java +++ b/extractor/src/test/java/org/schabi/newpipe/downloader/RecordingDownloader.java @@ -7,13 +7,8 @@ import org.schabi.newpipe.extractor.downloader.Request; import org.schabi.newpipe.extractor.downloader.Response; import org.schabi.newpipe.extractor.exceptions.ReCaptchaException; -import java.io.File; -import java.io.FileOutputStream; import java.io.IOException; -import java.io.OutputStreamWriter; -import java.nio.charset.StandardCharsets; import java.nio.file.Files; -import java.nio.file.Path; import java.nio.file.Paths; import javax.annotation.Nonnull; @@ -53,12 +48,13 @@ class RecordingDownloader extends Downloader { */ public RecordingDownloader(final String stringPath) throws IOException { this.path = stringPath; - final Path path = Paths.get(stringPath); - final File folder = path.toFile(); - if (folder.exists()) { - for (final File file : folder.listFiles()) { - if (file.getName().startsWith(RecordingDownloader.FILE_NAME_PREFIX)) { - file.delete(); + final var path = Paths.get(stringPath); + if (Files.exists(path)) { + try (final var directoryStream = Files.newDirectoryStream(path, + entry -> entry.getFileName().toString() + .startsWith(RecordingDownloader.FILE_NAME_PREFIX))) { + for (final var entry : directoryStream) { + Files.delete(entry); } } } else { @@ -80,18 +76,14 @@ class RecordingDownloader extends Downloader { response.latestUrl() ); - final File outputFile = new File(path + File.separator + FILE_NAME_PREFIX + index - + ".json"); + final var outputPath = Paths.get(path).resolve(FILE_NAME_PREFIX + index + ".json"); index++; - outputFile.createNewFile(); - final OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(outputFile), - StandardCharsets.UTF_8); - new GsonBuilder() - .setPrettyPrinting() - .create() - .toJson(new TestRequestResponse(request, response), writer); - writer.flush(); - writer.close(); + try (final var writer = Files.newBufferedWriter(outputPath)) { + new GsonBuilder() + .setPrettyPrinting() + .create() + .toJson(new TestRequestResponse(request, response), writer); + } return response; }