mirror of
https://github.com/TeamNewPipe/NewPipeExtractor.git
synced 2025-04-27 23:40:36 +05:30
Merge pull request #1065 from Isira-Seneviratne/Java_Files
Use Files methods in tests
This commit is contained in:
commit
d57a206344
@ -6,12 +6,11 @@ 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.io.UncheckedIOException;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.nio.file.Paths;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
@ -30,22 +29,20 @@ 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();
|
|
||||||
if (files != null) {
|
try (final var directoryStream = Files.newDirectoryStream(Paths.get(path),
|
||||||
for (final File file : files) {
|
entry -> entry.getFileName().toString()
|
||||||
if (file.getName().startsWith(RecordingDownloader.FILE_NAME_PREFIX)) {
|
.startsWith(RecordingDownloader.FILE_NAME_PREFIX))) {
|
||||||
final TestRequestResponse response;
|
for (final Path entry : directoryStream) {
|
||||||
try(final InputStreamReader reader = new InputStreamReader(
|
try (final var reader = Files.newBufferedReader(entry)) {
|
||||||
new FileInputStream(file), StandardCharsets.UTF_8)) {
|
final var response = new GsonBuilder()
|
||||||
response = new GsonBuilder()
|
.create()
|
||||||
.create()
|
.fromJson(reader, TestRequestResponse.class);
|
||||||
.fromJson(reader, TestRequestResponse.class);
|
|
||||||
} catch (IOException e) {
|
|
||||||
throw new UncheckedIOException(e);
|
|
||||||
}
|
|
||||||
mocks.put(response.getRequest(), response.getResponse());
|
mocks.put(response.getRequest(), response.getResponse());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} catch (final IOException ioe) {
|
||||||
|
throw new UncheckedIOException(ioe);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -7,12 +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.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.Path;
|
||||||
import java.nio.file.Paths;
|
import java.nio.file.Paths;
|
||||||
@ -45,7 +41,7 @@ class RecordingDownloader extends Downloader {
|
|||||||
"(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)";
|
"(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)";
|
||||||
|
|
||||||
private int index = 0;
|
private int index = 0;
|
||||||
private final String path;
|
private final Path path;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates the folder described by {@code stringPath} if it does not exist.
|
* Creates the folder described by {@code stringPath} if it does not exist.
|
||||||
@ -53,19 +49,22 @@ class RecordingDownloader extends Downloader {
|
|||||||
* @param stringPath Path to the folder where the json files will be saved to.
|
* @param stringPath Path to the folder where the json files will be saved to.
|
||||||
*/
|
*/
|
||||||
public RecordingDownloader(final String stringPath) {
|
public RecordingDownloader(final String stringPath) {
|
||||||
this.path = stringPath;
|
this.path = Paths.get(stringPath);
|
||||||
final Path 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 Path entry : directoryStream) {
|
||||||
|
Files.delete(entry);
|
||||||
}
|
}
|
||||||
|
} catch (final IOException ioe) {
|
||||||
|
throw new UncheckedIOException(ioe);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
try {
|
try {
|
||||||
Files.createDirectories(path);
|
Files.createDirectories(path);
|
||||||
} catch (IOException e) {
|
} catch (final IOException e) {
|
||||||
throw new UncheckedIOException(e);
|
throw new UncheckedIOException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -84,20 +83,13 @@ class RecordingDownloader extends Downloader {
|
|||||||
response.latestUrl()
|
response.latestUrl()
|
||||||
);
|
);
|
||||||
|
|
||||||
final File outputFile = new File(
|
final Path outputPath = 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