Use Files methods in tests.

This commit is contained in:
Isira Seneviratne 2023-05-21 06:07:13 +05:30 committed by litetex
parent 6bf0110e38
commit 4f16112d6d
No known key found for this signature in database
GPG Key ID: 525B43E6039B3689
2 changed files with 22 additions and 41 deletions

View File

@ -6,12 +6,9 @@ import org.schabi.newpipe.extractor.downloader.Downloader;
import org.schabi.newpipe.extractor.downloader.Request; import org.schabi.newpipe.extractor.downloader.Request;
import org.schabi.newpipe.extractor.downloader.Response; 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.io.IOException;
import java.io.UncheckedIOException; import java.nio.file.Files;
import java.nio.charset.StandardCharsets; import java.nio.file.Paths;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -30,19 +27,14 @@ class MockDownloader extends Downloader {
public MockDownloader(@Nonnull final String path) { public MockDownloader(@Nonnull final String path) {
this.path = path; this.path = path;
this.mocks = new HashMap<>(); this.mocks = new HashMap<>();
final File[] files = new File(path).listFiles(); try (final var directoryStream = Files.newDirectoryStream(Paths.get(path),
if (files != null) { entry -> entry.getFileName().toString()
for (final File file : files) { .startsWith(RecordingDownloader.FILE_NAME_PREFIX))) {
if (file.getName().startsWith(RecordingDownloader.FILE_NAME_PREFIX)) { for (final var entry : directoryStream) {
final TestRequestResponse response; try (final var reader = Files.newBufferedReader(entry)) {
try(final InputStreamReader reader = new InputStreamReader( final var response = new GsonBuilder()
new FileInputStream(file), StandardCharsets.UTF_8)) { .create()
response = new GsonBuilder() .fromJson(reader, TestRequestResponse.class);
.create()
.fromJson(reader, TestRequestResponse.class);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
mocks.put(response.getRequest(), response.getResponse()); mocks.put(response.getRequest(), response.getResponse());
} }
} }

View File

@ -7,14 +7,9 @@ import org.schabi.newpipe.extractor.downloader.Request;
import org.schabi.newpipe.extractor.downloader.Response; import org.schabi.newpipe.extractor.downloader.Response;
import org.schabi.newpipe.extractor.exceptions.ReCaptchaException; import org.schabi.newpipe.extractor.exceptions.ReCaptchaException;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UncheckedIOException; import java.io.UncheckedIOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
@ -54,12 +49,13 @@ class RecordingDownloader extends Downloader {
*/ */
public RecordingDownloader(final String stringPath) { public RecordingDownloader(final String stringPath) {
this.path = stringPath; this.path = stringPath;
final Path path = Paths.get(stringPath); final var path = Paths.get(stringPath);
final File folder = path.toFile(); if (Files.exists(path)) {
if (folder.exists()) { try (final var directoryStream = Files.newDirectoryStream(path,
for (final File file : folder.listFiles()) { entry -> entry.getFileName().toString()
if (file.getName().startsWith(RecordingDownloader.FILE_NAME_PREFIX)) { .startsWith(RecordingDownloader.FILE_NAME_PREFIX))) {
file.delete(); for (final var entry : directoryStream) {
Files.delete(entry);
} }
} }
} else { } else {
@ -84,20 +80,13 @@ class RecordingDownloader extends Downloader {
response.latestUrl() response.latestUrl()
); );
final File outputFile = new File( final var outputPath = Paths.get(path).resolve(FILE_NAME_PREFIX + index + ".json");
path + File.separator + FILE_NAME_PREFIX + index + ".json");
index++; index++;
outputFile.createNewFile(); try (final var writer = Files.newBufferedWriter(outputPath)) {
try (final OutputStreamWriter writer = new OutputStreamWriter(
new FileOutputStream(outputFile), StandardCharsets.UTF_8)) {
new GsonBuilder() new GsonBuilder()
.setPrettyPrinting() .setPrettyPrinting()
.create() .create()
.toJson(new TestRequestResponse(request, response), writer); .toJson(new TestRequestResponse(request, response), writer);
writer.flush();
} }
return response; return response;