Use Files methods in tests.
This commit is contained in:
parent
7c7ceaceab
commit
6a3036f1a4
|
@ -6,11 +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.nio.charset.StandardCharsets;
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Paths;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
|
@ -29,16 +27,14 @@ class MockDownloader extends Downloader {
|
||||||
public MockDownloader(@Nonnull final String path) throws IOException {
|
public MockDownloader(@Nonnull final String path) throws IOException {
|
||||||
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 InputStreamReader reader = new InputStreamReader(new FileInputStream(
|
try (final var reader = Files.newBufferedReader(entry)) {
|
||||||
file), StandardCharsets.UTF_8);
|
final var response = new GsonBuilder()
|
||||||
final TestRequestResponse response = new GsonBuilder()
|
|
||||||
.create()
|
.create()
|
||||||
.fromJson(reader, TestRequestResponse.class);
|
.fromJson(reader, TestRequestResponse.class);
|
||||||
reader.close();
|
|
||||||
mocks.put(response.getRequest(), response.getResponse());
|
mocks.put(response.getRequest(), response.getResponse());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,13 +7,8 @@ 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.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;
|
||||||
|
@ -53,12 +48,13 @@ class RecordingDownloader extends Downloader {
|
||||||
*/
|
*/
|
||||||
public RecordingDownloader(final String stringPath) throws IOException {
|
public RecordingDownloader(final String stringPath) throws IOException {
|
||||||
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 {
|
||||||
|
@ -80,18 +76,14 @@ class RecordingDownloader extends Downloader {
|
||||||
response.latestUrl()
|
response.latestUrl()
|
||||||
);
|
);
|
||||||
|
|
||||||
final File outputFile = new File(path + File.separator + FILE_NAME_PREFIX + index
|
final var outputPath = Paths.get(path).resolve(FILE_NAME_PREFIX + index + ".json");
|
||||||
+ ".json");
|
|
||||||
index++;
|
index++;
|
||||||
outputFile.createNewFile();
|
try (final var writer = Files.newBufferedWriter(outputPath)) {
|
||||||
final OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(outputFile),
|
new GsonBuilder()
|
||||||
StandardCharsets.UTF_8);
|
.setPrettyPrinting()
|
||||||
new GsonBuilder()
|
.create()
|
||||||
.setPrettyPrinting()
|
.toJson(new TestRequestResponse(request, response), writer);
|
||||||
.create()
|
}
|
||||||
.toJson(new TestRequestResponse(request, response), writer);
|
|
||||||
writer.flush();
|
|
||||||
writer.close();
|
|
||||||
|
|
||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue