mirror of
https://github.com/TeamNewPipe/NewPipeExtractor.git
synced 2025-04-27 23:40:36 +05:30
Use Files methods in tests.
This commit is contained in:
parent
6bf0110e38
commit
4f16112d6d
@ -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());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user