Skip to content

Commit 4f16112

Browse files
Isira-Seneviratnelitetex
authored andcommitted
Use Files methods in tests.
1 parent 6bf0110 commit 4f16112

2 files changed

Lines changed: 22 additions & 41 deletions

File tree

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

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +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.io.UncheckedIOException;
14-
import java.nio.charset.StandardCharsets;
10+
import java.nio.file.Files;
11+
import java.nio.file.Paths;
1512
import java.util.HashMap;
1613
import java.util.Map;
1714

@@ -30,19 +27,14 @@ class MockDownloader extends Downloader {
3027
public MockDownloader(@Nonnull final String path) {
3128
this.path = path;
3229
this.mocks = new HashMap<>();
33-
final File[] files = new File(path).listFiles();
34-
if (files != null) {
35-
for (final File file : files) {
36-
if (file.getName().startsWith(RecordingDownloader.FILE_NAME_PREFIX)) {
37-
final TestRequestResponse response;
38-
try(final InputStreamReader reader = new InputStreamReader(
39-
new FileInputStream(file), StandardCharsets.UTF_8)) {
40-
response = new GsonBuilder()
41-
.create()
42-
.fromJson(reader, TestRequestResponse.class);
43-
} catch (IOException e) {
44-
throw new UncheckedIOException(e);
45-
}
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()
36+
.create()
37+
.fromJson(reader, TestRequestResponse.class);
4638
mocks.put(response.getRequest(), response.getResponse());
4739
}
4840
}

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

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,9 @@
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;
1411
import java.io.UncheckedIOException;
15-
import java.nio.charset.StandardCharsets;
1612
import java.nio.file.Files;
17-
import java.nio.file.Path;
1813
import java.nio.file.Paths;
1914

2015
import javax.annotation.Nonnull;
@@ -54,12 +49,13 @@ class RecordingDownloader extends Downloader {
5449
*/
5550
public RecordingDownloader(final String stringPath) {
5651
this.path = stringPath;
57-
final Path path = Paths.get(stringPath);
58-
final File folder = path.toFile();
59-
if (folder.exists()) {
60-
for (final File file : folder.listFiles()) {
61-
if (file.getName().startsWith(RecordingDownloader.FILE_NAME_PREFIX)) {
62-
file.delete();
52+
final var path = Paths.get(stringPath);
53+
if (Files.exists(path)) {
54+
try (final var directoryStream = Files.newDirectoryStream(path,
55+
entry -> entry.getFileName().toString()
56+
.startsWith(RecordingDownloader.FILE_NAME_PREFIX))) {
57+
for (final var entry : directoryStream) {
58+
Files.delete(entry);
6359
}
6460
}
6561
} else {
@@ -84,20 +80,13 @@ public Response execute(@Nonnull final Request request) throws IOException,
8480
response.latestUrl()
8581
);
8682

87-
final File outputFile = new File(
88-
path + File.separator + FILE_NAME_PREFIX + index + ".json");
83+
final var outputPath = Paths.get(path).resolve(FILE_NAME_PREFIX + index + ".json");
8984
index++;
90-
outputFile.createNewFile();
91-
92-
try (final OutputStreamWriter writer = new OutputStreamWriter(
93-
new FileOutputStream(outputFile), StandardCharsets.UTF_8)) {
94-
85+
try (final var writer = Files.newBufferedWriter(outputPath)) {
9586
new GsonBuilder()
96-
.setPrettyPrinting()
97-
.create()
98-
.toJson(new TestRequestResponse(request, response), writer);
99-
100-
writer.flush();
87+
.setPrettyPrinting()
88+
.create()
89+
.toJson(new TestRequestResponse(request, response), writer);
10190
}
10291

10392
return response;

0 commit comments

Comments
 (0)