Skip to content

Commit 3b34b82

Browse files
committed
Downloader: Don't force IOException
Use ``UncheckedIOException`` instead This no longer forces one to always write ``throws IOException`` in tests
1 parent 1772624 commit 3b34b82

10 files changed

Lines changed: 42 additions & 33 deletions

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public static DownloaderType getDownloaderType() {
3636
* @param path The path to the folder where mocks are saved/retrieved.
3737
* Preferably starting with {@link DownloaderFactory#RESOURCE_PATH}
3838
*/
39-
public static Downloader getDownloader(final String path) throws IOException {
39+
public static Downloader getDownloader(final String path) {
4040
final DownloaderType type = getDownloaderType();
4141
switch (type) {
4242
case REAL:

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

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import java.io.FileInputStream;
1111
import java.io.InputStreamReader;
1212
import java.io.IOException;
13+
import java.io.UncheckedIOException;
1314
import java.nio.charset.StandardCharsets;
1415
import java.util.HashMap;
1516
import java.util.Map;
@@ -26,19 +27,22 @@ class MockDownloader extends Downloader {
2627
private final String path;
2728
private final Map<Request, Response> mocks;
2829

29-
public MockDownloader(@Nonnull final String path) throws IOException {
30+
public MockDownloader(@Nonnull final String path) {
3031
this.path = path;
3132
this.mocks = new HashMap<>();
3233
final File[] files = new File(path).listFiles();
3334
if (files != null) {
3435
for (final File file : files) {
3536
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()
39-
.create()
40-
.fromJson(reader, TestRequestResponse.class);
41-
reader.close();
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+
}
4246
mocks.put(response.getRequest(), response.getResponse());
4347
}
4448
}

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import java.io.FileOutputStream;
1212
import java.io.IOException;
1313
import java.io.OutputStreamWriter;
14+
import java.io.UncheckedIOException;
1415
import java.nio.charset.StandardCharsets;
1516
import java.nio.file.Files;
1617
import java.nio.file.Path;
@@ -51,7 +52,7 @@ class RecordingDownloader extends Downloader {
5152
* Deletes existing files starting with {@link RecordingDownloader#FILE_NAME_PREFIX}.
5253
* @param stringPath Path to the folder where the json files will be saved to.
5354
*/
54-
public RecordingDownloader(final String stringPath) throws IOException {
55+
public RecordingDownloader(final String stringPath) {
5556
this.path = stringPath;
5657
final Path path = Paths.get(stringPath);
5758
final File folder = path.toFile();
@@ -62,7 +63,11 @@ public RecordingDownloader(final String stringPath) throws IOException {
6263
}
6364
}
6465
} else {
65-
Files.createDirectories(path);
66+
try {
67+
Files.createDirectories(path);
68+
} catch (IOException e) {
69+
throw new UncheckedIOException(e);
70+
}
6671
}
6772
}
6873

extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeChannelExtractorTest.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,29 +41,29 @@ public class YoutubeChannelExtractorTest {
4141

4242
public static class NotAvailable {
4343
@BeforeAll
44-
public static void setUp() throws IOException {
44+
public static void setUp() {
4545
YoutubeTestsUtils.ensureStateless();
4646
NewPipe.init(DownloaderFactory.getDownloader(RESOURCE_PATH + "notAvailable"));
4747
}
4848

4949
@Test
50-
public void deletedFetch() throws Exception {
50+
void deletedFetch() throws Exception {
5151
final ChannelExtractor extractor =
5252
YouTube.getChannelExtractor("https://www.youtube.com/channel/UCAUc4iz6edWerIjlnL8OSSw");
5353

5454
assertThrows(ContentNotAvailableException.class, extractor::fetchPage);
5555
}
5656

5757
@Test
58-
public void nonExistentFetch() throws Exception {
58+
void nonExistentFetch() throws Exception {
5959
final ChannelExtractor extractor =
6060
YouTube.getChannelExtractor("https://www.youtube.com/channel/DOESNT-EXIST");
6161

6262
assertThrows(ContentNotAvailableException.class, extractor::fetchPage);
6363
}
6464

6565
@Test
66-
public void accountTerminatedTOSFetch() throws Exception {
66+
void accountTerminatedTOSFetch() throws Exception {
6767
// "This account has been terminated for a violation of YouTube's Terms of Service."
6868
final ChannelExtractor extractor =
6969
YouTube.getChannelExtractor("https://www.youtube.com/channel/UCTGjY2I-ZUGnwVoWAGRd7XQ");
@@ -74,7 +74,7 @@ public void accountTerminatedTOSFetch() throws Exception {
7474
}
7575

7676
@Test
77-
public void accountTerminatedCommunityFetch() throws Exception {
77+
void accountTerminatedCommunityFetch() throws Exception {
7878
// "This account has been terminated for violating YouTube's Community Guidelines."
7979
final ChannelExtractor extractor =
8080
YouTube.getChannelExtractor("https://www.youtube.com/channel/UC0AuOxCr9TZ0TtEgL1zpIgA");
@@ -85,7 +85,7 @@ public void accountTerminatedCommunityFetch() throws Exception {
8585
}
8686

8787
@Test
88-
public void accountTerminatedHateFetch() throws Exception {
88+
void accountTerminatedHateFetch() throws Exception {
8989
// "This account has been terminated due to multiple or severe violations
9090
// of YouTube's policy prohibiting hate speech."
9191
final ChannelExtractor extractor =
@@ -97,7 +97,7 @@ public void accountTerminatedHateFetch() throws Exception {
9797
}
9898

9999
@Test
100-
public void accountTerminatedBullyFetch() throws Exception {
100+
void accountTerminatedBullyFetch() throws Exception {
101101
// "This account has been terminated due to multiple or severe violations
102102
// of YouTube's policy prohibiting content designed to harass, bully or threaten."
103103
final ChannelExtractor extractor =
@@ -109,7 +109,7 @@ public void accountTerminatedBullyFetch() throws Exception {
109109
}
110110

111111
@Test
112-
public void accountTerminatedSpamFetch() throws Exception {
112+
void accountTerminatedSpamFetch() throws Exception {
113113
// "This account has been terminated due to multiple or severe violations
114114
// of YouTube's policy against spam, deceptive practices and misleading content
115115
// or other Terms of Service violations."
@@ -122,7 +122,7 @@ public void accountTerminatedSpamFetch() throws Exception {
122122
}
123123

124124
@Test
125-
public void accountTerminatedCopyrightFetch() throws Exception {
125+
void accountTerminatedCopyrightFetch() throws Exception {
126126
// "This account has been terminated because we received multiple third-party claims
127127
// of copyright infringement regarding material that the user posted."
128128
final ChannelExtractor extractor =
@@ -137,7 +137,7 @@ public void accountTerminatedCopyrightFetch() throws Exception {
137137

138138
static class SystemTopic {
139139
@BeforeAll
140-
static void setUp() throws IOException {
140+
static void setUp() {
141141
YoutubeTestsUtils.ensureStateless();
142142
NewPipe.init(DownloaderFactory.getDownloader(RESOURCE_PATH + "systemTopic"));
143143
}

extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeFeedExtractorTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ public void testMoreRelatedItems() throws Exception {
8181
public static class NotAvailable {
8282

8383
@BeforeAll
84-
public static void setUp() throws IOException {
84+
public static void setUp() {
85+
YoutubeTestsUtils.ensureStateless();
8586
NewPipe.init(DownloaderFactory.getDownloader(RESOURCE_PATH + "notAvailable/"));
8687
}
8788

extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeMixPlaylistExtractorTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ public static class Invalid {
310310
private static final String VIDEO_ID = "QMVCAPd5cwBcg";
311311

312312
@BeforeAll
313-
public static void setUp() throws IOException {
313+
public static void setUp() {
314314
YoutubeTestsUtils.ensureStateless();
315315
YoutubeParsingHelper.setConsentAccepted(true);
316316
NewPipe.init(DownloaderFactory.getDownloader(RESOURCE_PATH + "invalid"));

extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeParsingHelperTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public class YoutubeParsingHelperTest {
1919
private static final String RESOURCE_PATH = DownloaderFactory.RESOURCE_PATH + "services/youtube/";
2020

2121
@BeforeAll
22-
public static void setUp() throws IOException {
22+
public static void setUp() {
2323
YoutubeTestsUtils.ensureStateless();
2424
NewPipe.init(DownloaderFactory.getDownloader(RESOURCE_PATH + "youtubeParsingHelper"));
2525
}

extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubePlaylistExtractorTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public class YoutubePlaylistExtractorTest {
4040

4141
public static class NotAvailable {
4242
@BeforeAll
43-
public static void setUp() throws IOException {
43+
public static void setUp() {
4444
YoutubeTestsUtils.ensureStateless();
4545
NewPipe.init(DownloaderFactory.getDownloader(RESOURCE_PATH + "notAvailable"));
4646
}
@@ -549,7 +549,7 @@ void testDescription() throws ParsingException {
549549
public static class ContinuationsTests {
550550

551551
@BeforeAll
552-
public static void setUp() throws IOException {
552+
public static void setUp() {
553553
YoutubeTestsUtils.ensureStateless();
554554
NewPipe.init(DownloaderFactory.getDownloader(RESOURCE_PATH + "continuations"));
555555
}

extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeSignaturesTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class YoutubeSignaturesTest {
1919
DownloaderFactory.RESOURCE_PATH + "services/youtube/extractor/signatures";
2020

2121
@BeforeEach
22-
void setUp() throws IOException {
22+
void setUp() {
2323
YoutubeTestsUtils.ensureStateless();
2424
NewPipe.init(DownloaderFactory.getDownloader(RESOURCE_PATH));
2525
}

extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeTrendingKioskInfoTest.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,13 @@
2828
import org.junit.jupiter.api.Test;
2929
import org.schabi.newpipe.downloader.DownloaderFactory;
3030
import org.schabi.newpipe.extractor.NewPipe;
31-
import org.schabi.newpipe.extractor.StreamingService;
3231
import org.schabi.newpipe.extractor.kiosk.KioskInfo;
3332
import org.schabi.newpipe.extractor.linkhandler.LinkHandlerFactory;
3433

3534
/**
3635
* Test for {@link KioskInfo}
3736
*/
38-
public class YoutubeTrendingKioskInfoTest {
37+
class YoutubeTrendingKioskInfoTest {
3938

4039
private static final String RESOURCE_PATH = DownloaderFactory.RESOURCE_PATH + "kiosk";
4140

@@ -46,24 +45,24 @@ public static void setUp()
4645
throws Exception {
4746
YoutubeTestsUtils.ensureStateless();
4847
NewPipe.init(DownloaderFactory.getDownloader(RESOURCE_PATH));
49-
LinkHandlerFactory LinkHandlerFactory = ((StreamingService) YouTube).getKioskList().getListLinkHandlerFactoryByType("Trending");
48+
LinkHandlerFactory linkHandlerFactory = YouTube.getKioskList().getListLinkHandlerFactoryByType("Trending");
5049

51-
kioskInfo = KioskInfo.getInfo(YouTube, LinkHandlerFactory.fromId("Trending").getUrl());
50+
kioskInfo = KioskInfo.getInfo(YouTube, linkHandlerFactory.fromId("Trending").getUrl());
5251
}
5352

5453
@Test
55-
public void getStreams() {
54+
void getStreams() {
5655
assertFalse(kioskInfo.getRelatedItems().isEmpty());
5756
}
5857

5958
@Test
60-
public void getId() {
59+
void getId() {
6160
assertTrue(kioskInfo.getId().equals("Trending")
6261
|| kioskInfo.getId().equals("Trends"));
6362
}
6463

6564
@Test
66-
public void getName() {
65+
void getName() {
6766
assertFalse(kioskInfo.getName().isEmpty());
6867
}
6968
}

0 commit comments

Comments
 (0)