Skip to content

Commit db81384

Browse files
authored
Merge pull request #644 from TiA4f8R/utf-8-encoding-for-mocks-windows
Specify UTF-8 file encoding in RecordingDownloader and MockDownloader
2 parents 7e4332e + ac31f3a commit db81384

2 files changed

Lines changed: 29 additions & 20 deletions

File tree

extractor/src/test/java/org/schabi/newpipe/downloader/MockDownloader.java

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77
import org.schabi.newpipe.extractor.downloader.Response;
88

99
import java.io.File;
10-
import java.io.FileReader;
10+
import java.io.FileInputStream;
11+
import java.io.InputStreamReader;
1112
import java.io.IOException;
13+
import java.nio.charset.StandardCharsets;
1214
import java.util.HashMap;
1315
import java.util.Map;
1416

@@ -24,14 +26,15 @@ class MockDownloader extends Downloader {
2426
private final String path;
2527
private final Map<Request, Response> mocks;
2628

27-
public MockDownloader(@Nonnull String path) throws IOException {
29+
public MockDownloader(@Nonnull final String path) throws IOException {
2830
this.path = path;
2931
this.mocks = new HashMap<>();
3032
final File[] files = new File(path).listFiles();
3133
if (files != null) {
32-
for (File file : files) {
34+
for (final File file : files) {
3335
if (file.getName().startsWith(RecordingDownloader.FILE_NAME_PREFIX)) {
34-
final FileReader reader = new FileReader(file);
36+
final InputStreamReader reader = new InputStreamReader(new FileInputStream(
37+
file), StandardCharsets.UTF_8);
3538
final TestRequestResponse response = new GsonBuilder()
3639
.create()
3740
.fromJson(reader, TestRequestResponse.class);
@@ -43,12 +46,12 @@ public MockDownloader(@Nonnull String path) throws IOException {
4346
}
4447

4548
@Override
46-
public Response execute(@Nonnull Request request) {
47-
Response result = mocks.get(request);
49+
public Response execute(@Nonnull final Request request) {
50+
final Response result = mocks.get(request);
4851
if (result == null) {
49-
throw new NullPointerException("No mock response for request with url '" + request.url()
50-
+ "' exists in path '" + path + "'.\nPlease make sure to run the tests with " +
51-
"the RecordingDownloader first after changes.");
52+
throw new NullPointerException("No mock response for request with url '" + request
53+
.url() + "' exists in path '" + path + "'.\nPlease make sure to run the tests "
54+
+ "with the RecordingDownloader first after changes.");
5255
}
5356
return result;
5457
}

extractor/src/test/java/org/schabi/newpipe/downloader/RecordingDownloader.java

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88
import org.schabi.newpipe.extractor.exceptions.ReCaptchaException;
99

1010
import java.io.File;
11-
import java.io.FileWriter;
11+
import java.io.FileOutputStream;
1212
import java.io.IOException;
13+
import java.io.OutputStreamWriter;
14+
import java.nio.charset.StandardCharsets;
1315
import java.nio.file.Files;
1416
import java.nio.file.Path;
1517
import java.nio.file.Paths;
@@ -18,7 +20,8 @@
1820

1921
/**
2022
* <p>
21-
* Relays requests to {@link DownloaderTestImpl} and saves the request/response pair into a json file.
23+
* Relays requests to {@link DownloaderTestImpl} and saves the request/response pair into a json
24+
* file.
2225
* </p>
2326
* <p>
2427
* Those files are used by {@link MockDownloader}.
@@ -44,12 +47,12 @@ class RecordingDownloader extends Downloader {
4447
* Deletes existing files starting with {@link RecordingDownloader#FILE_NAME_PREFIX}.
4548
* @param stringPath Path to the folder where the json files will be saved to.
4649
*/
47-
public RecordingDownloader(String stringPath) throws IOException {
50+
public RecordingDownloader(final String stringPath) throws IOException {
4851
this.path = stringPath;
49-
Path path = Paths.get(stringPath);
50-
File folder = path.toFile();
52+
final Path path = Paths.get(stringPath);
53+
final File folder = path.toFile();
5154
if (folder.exists()) {
52-
for (File file : folder.listFiles()) {
55+
for (final File file : folder.listFiles()) {
5356
if (file.getName().startsWith(RecordingDownloader.FILE_NAME_PREFIX)) {
5457
file.delete();
5558
}
@@ -60,14 +63,17 @@ public RecordingDownloader(String stringPath) throws IOException {
6063
}
6164

6265
@Override
63-
public Response execute(@Nonnull Request request) throws IOException, ReCaptchaException {
64-
Downloader downloader = DownloaderTestImpl.getInstance();
65-
Response response = downloader.execute(request);
66+
public Response execute(@Nonnull final Request request) throws IOException,
67+
ReCaptchaException {
68+
final Downloader downloader = DownloaderTestImpl.getInstance();
69+
final Response response = downloader.execute(request);
6670

67-
File outputFile = new File(path + File.separator + FILE_NAME_PREFIX + index + ".json");
71+
final File outputFile = new File(path + File.separator + FILE_NAME_PREFIX + index
72+
+ ".json");
6873
index++;
6974
outputFile.createNewFile();
70-
FileWriter writer = new FileWriter(outputFile);
75+
final OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(outputFile),
76+
StandardCharsets.UTF_8);
7177
new GsonBuilder()
7278
.setPrettyPrinting()
7379
.create()

0 commit comments

Comments
 (0)