Merge pull request #644 from TiA4f8R/utf-8-encoding-for-mocks-windows
Specify UTF-8 file encoding in RecordingDownloader and MockDownloader
This commit is contained in:
commit
db81384ae0
|
@ -7,8 +7,10 @@ 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.File;
|
||||||
import java.io.FileReader;
|
import java.io.FileInputStream;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
|
@ -24,14 +26,15 @@ class MockDownloader extends Downloader {
|
||||||
private final String path;
|
private final String path;
|
||||||
private final Map<Request, Response> mocks;
|
private final Map<Request, Response> mocks;
|
||||||
|
|
||||||
public MockDownloader(@Nonnull 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();
|
final File[] files = new File(path).listFiles();
|
||||||
if (files != null) {
|
if (files != null) {
|
||||||
for (File file : files) {
|
for (final File file : files) {
|
||||||
if (file.getName().startsWith(RecordingDownloader.FILE_NAME_PREFIX)) {
|
if (file.getName().startsWith(RecordingDownloader.FILE_NAME_PREFIX)) {
|
||||||
final FileReader reader = new FileReader(file);
|
final InputStreamReader reader = new InputStreamReader(new FileInputStream(
|
||||||
|
file), StandardCharsets.UTF_8);
|
||||||
final TestRequestResponse response = new GsonBuilder()
|
final TestRequestResponse response = new GsonBuilder()
|
||||||
.create()
|
.create()
|
||||||
.fromJson(reader, TestRequestResponse.class);
|
.fromJson(reader, TestRequestResponse.class);
|
||||||
|
@ -43,12 +46,12 @@ class MockDownloader extends Downloader {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Response execute(@Nonnull Request request) {
|
public Response execute(@Nonnull final Request request) {
|
||||||
Response result = mocks.get(request);
|
final Response result = mocks.get(request);
|
||||||
if (result == null) {
|
if (result == null) {
|
||||||
throw new NullPointerException("No mock response for request with url '" + request.url()
|
throw new NullPointerException("No mock response for request with url '" + request
|
||||||
+ "' exists in path '" + path + "'.\nPlease make sure to run the tests with " +
|
.url() + "' exists in path '" + path + "'.\nPlease make sure to run the tests "
|
||||||
"the RecordingDownloader first after changes.");
|
+ "with the RecordingDownloader first after changes.");
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,8 +8,10 @@ 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.File;
|
||||||
import java.io.FileWriter;
|
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.Path;
|
||||||
import java.nio.file.Paths;
|
import java.nio.file.Paths;
|
||||||
|
@ -18,7 +20,8 @@ import javax.annotation.Nonnull;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>
|
* <p>
|
||||||
* Relays requests to {@link DownloaderTestImpl} and saves the request/response pair into a json file.
|
* Relays requests to {@link DownloaderTestImpl} and saves the request/response pair into a json
|
||||||
|
* file.
|
||||||
* </p>
|
* </p>
|
||||||
* <p>
|
* <p>
|
||||||
* Those files are used by {@link MockDownloader}.
|
* Those files are used by {@link MockDownloader}.
|
||||||
|
@ -44,12 +47,12 @@ class RecordingDownloader extends Downloader {
|
||||||
* Deletes existing files starting with {@link RecordingDownloader#FILE_NAME_PREFIX}.
|
* Deletes existing files starting with {@link RecordingDownloader#FILE_NAME_PREFIX}.
|
||||||
* @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(String stringPath) throws IOException {
|
public RecordingDownloader(final String stringPath) throws IOException {
|
||||||
this.path = stringPath;
|
this.path = stringPath;
|
||||||
Path path = Paths.get(stringPath);
|
final Path path = Paths.get(stringPath);
|
||||||
File folder = path.toFile();
|
final File folder = path.toFile();
|
||||||
if (folder.exists()) {
|
if (folder.exists()) {
|
||||||
for (File file : folder.listFiles()) {
|
for (final File file : folder.listFiles()) {
|
||||||
if (file.getName().startsWith(RecordingDownloader.FILE_NAME_PREFIX)) {
|
if (file.getName().startsWith(RecordingDownloader.FILE_NAME_PREFIX)) {
|
||||||
file.delete();
|
file.delete();
|
||||||
}
|
}
|
||||||
|
@ -60,14 +63,17 @@ class RecordingDownloader extends Downloader {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Response execute(@Nonnull Request request) throws IOException, ReCaptchaException {
|
public Response execute(@Nonnull final Request request) throws IOException,
|
||||||
Downloader downloader = DownloaderTestImpl.getInstance();
|
ReCaptchaException {
|
||||||
Response response = downloader.execute(request);
|
final Downloader downloader = DownloaderTestImpl.getInstance();
|
||||||
|
final Response response = downloader.execute(request);
|
||||||
|
|
||||||
File outputFile = new File(path + File.separator + FILE_NAME_PREFIX + index + ".json");
|
final File outputFile = new File(path + File.separator + FILE_NAME_PREFIX + index
|
||||||
|
+ ".json");
|
||||||
index++;
|
index++;
|
||||||
outputFile.createNewFile();
|
outputFile.createNewFile();
|
||||||
FileWriter writer = new FileWriter(outputFile);
|
final OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(outputFile),
|
||||||
|
StandardCharsets.UTF_8);
|
||||||
new GsonBuilder()
|
new GsonBuilder()
|
||||||
.setPrettyPrinting()
|
.setPrettyPrinting()
|
||||||
.create()
|
.create()
|
||||||
|
|
Loading…
Reference in New Issue