Skip to content

Commit 84d471d

Browse files
Use Files methods in tests.
1 parent 533121f commit 84d471d

2 files changed

Lines changed: 22 additions & 34 deletions

File tree

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

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

9-
import java.io.File;
10-
import java.io.FileInputStream;
11-
import java.io.InputStreamReader;
129
import java.io.IOException;
13-
import java.nio.charset.StandardCharsets;
10+
import java.nio.file.Files;
11+
import java.nio.file.Paths;
1412
import java.util.HashMap;
1513
import java.util.Map;
1614

@@ -29,16 +27,14 @@ class MockDownloader extends Downloader {
2927
public MockDownloader(@Nonnull final String path) throws IOException {
3028
this.path = path;
3129
this.mocks = new HashMap<>();
32-
final File[] files = new File(path).listFiles();
33-
if (files != null) {
34-
for (final File file : files) {
35-
if (file.getName().startsWith(RecordingDownloader.FILE_NAME_PREFIX)) {
36-
final InputStreamReader reader = new InputStreamReader(new FileInputStream(
37-
file), StandardCharsets.UTF_8);
38-
final TestRequestResponse response = new GsonBuilder()
30+
try (final var directoryStream = Files.newDirectoryStream(Paths.get(path),
31+
entry -> entry.getFileName().toString()
32+
.startsWith(RecordingDownloader.FILE_NAME_PREFIX))) {
33+
for (final var entry : directoryStream) {
34+
try (final var reader = Files.newBufferedReader(entry)) {
35+
final var response = new GsonBuilder()
3936
.create()
4037
.fromJson(reader, TestRequestResponse.class);
41-
reader.close();
4238
mocks.put(response.getRequest(), response.getResponse());
4339
}
4440
}

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

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,8 @@
77
import org.schabi.newpipe.extractor.downloader.Response;
88
import org.schabi.newpipe.extractor.exceptions.ReCaptchaException;
99

10-
import java.io.File;
11-
import java.io.FileOutputStream;
1210
import java.io.IOException;
13-
import java.io.OutputStreamWriter;
14-
import java.nio.charset.StandardCharsets;
1511
import java.nio.file.Files;
16-
import java.nio.file.Path;
1712
import java.nio.file.Paths;
1813

1914
import javax.annotation.Nonnull;
@@ -53,12 +48,13 @@ class RecordingDownloader extends Downloader {
5348
*/
5449
public RecordingDownloader(final String stringPath) throws IOException {
5550
this.path = stringPath;
56-
final Path path = Paths.get(stringPath);
57-
final File folder = path.toFile();
58-
if (folder.exists()) {
59-
for (final File file : folder.listFiles()) {
60-
if (file.getName().startsWith(RecordingDownloader.FILE_NAME_PREFIX)) {
61-
file.delete();
51+
final var path = Paths.get(stringPath);
52+
if (Files.exists(path)) {
53+
try (final var directoryStream = Files.newDirectoryStream(path,
54+
entry -> entry.getFileName().toString()
55+
.startsWith(RecordingDownloader.FILE_NAME_PREFIX))) {
56+
for (final var entry : directoryStream) {
57+
Files.delete(entry);
6258
}
6359
}
6460
} else {
@@ -80,18 +76,14 @@ public Response execute(@Nonnull final Request request) throws IOException,
8076
response.latestUrl()
8177
);
8278

83-
final File outputFile = new File(path + File.separator + FILE_NAME_PREFIX + index
84-
+ ".json");
79+
final var outputPath = Paths.get(path).resolve(FILE_NAME_PREFIX + index + ".json");
8580
index++;
86-
outputFile.createNewFile();
87-
final OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(outputFile),
88-
StandardCharsets.UTF_8);
89-
new GsonBuilder()
90-
.setPrettyPrinting()
91-
.create()
92-
.toJson(new TestRequestResponse(request, response), writer);
93-
writer.flush();
94-
writer.close();
81+
try (final var writer = Files.newBufferedWriter(outputPath)) {
82+
new GsonBuilder()
83+
.setPrettyPrinting()
84+
.create()
85+
.toJson(new TestRequestResponse(request, response), writer);
86+
}
9587

9688
return response;
9789
}

0 commit comments

Comments
 (0)