Downloader: Don't force IOException

Use ``UncheckedIOException`` instead

This no longer forces one to always write ``throws IOException`` in tests
This commit is contained in:
litetex 2025-02-12 21:46:45 +01:00
parent 177262485a
commit 3b34b82e0f
No known key found for this signature in database
GPG Key ID: 525B43E6039B3689
10 changed files with 42 additions and 33 deletions

View File

@ -36,7 +36,7 @@ public class DownloaderFactory {
* @param path The path to the folder where mocks are saved/retrieved. * @param path The path to the folder where mocks are saved/retrieved.
* Preferably starting with {@link DownloaderFactory#RESOURCE_PATH} * Preferably starting with {@link DownloaderFactory#RESOURCE_PATH}
*/ */
public static Downloader getDownloader(final String path) throws IOException { public static Downloader getDownloader(final String path) {
final DownloaderType type = getDownloaderType(); final DownloaderType type = getDownloaderType();
switch (type) { switch (type) {
case REAL: case REAL:

View File

@ -10,6 +10,7 @@ import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.io.IOException; import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -26,19 +27,22 @@ 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 final String path) throws IOException { public MockDownloader(@Nonnull final String path) {
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 (final 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 InputStreamReader reader = new InputStreamReader(new FileInputStream( final TestRequestResponse response;
file), StandardCharsets.UTF_8); try(final InputStreamReader reader = new InputStreamReader(
final TestRequestResponse response = new GsonBuilder() new FileInputStream(file), StandardCharsets.UTF_8)) {
.create() response = new GsonBuilder()
.fromJson(reader, TestRequestResponse.class); .create()
reader.close(); .fromJson(reader, TestRequestResponse.class);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
mocks.put(response.getRequest(), response.getResponse()); mocks.put(response.getRequest(), response.getResponse());
} }
} }

View File

@ -11,6 +11,7 @@ import java.io.File;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStreamWriter; import java.io.OutputStreamWriter;
import java.io.UncheckedIOException;
import java.nio.charset.StandardCharsets; 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;
@ -51,7 +52,7 @@ 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(final String stringPath) throws IOException { public RecordingDownloader(final String stringPath) {
this.path = stringPath; this.path = stringPath;
final Path path = Paths.get(stringPath); final Path path = Paths.get(stringPath);
final File folder = path.toFile(); final File folder = path.toFile();
@ -62,7 +63,11 @@ class RecordingDownloader extends Downloader {
} }
} }
} else { } else {
Files.createDirectories(path); try {
Files.createDirectories(path);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
} }
} }

View File

@ -41,13 +41,13 @@ public class YoutubeChannelExtractorTest {
public static class NotAvailable { public static class NotAvailable {
@BeforeAll @BeforeAll
public static void setUp() throws IOException { public static void setUp() {
YoutubeTestsUtils.ensureStateless(); YoutubeTestsUtils.ensureStateless();
NewPipe.init(DownloaderFactory.getDownloader(RESOURCE_PATH + "notAvailable")); NewPipe.init(DownloaderFactory.getDownloader(RESOURCE_PATH + "notAvailable"));
} }
@Test @Test
public void deletedFetch() throws Exception { void deletedFetch() throws Exception {
final ChannelExtractor extractor = final ChannelExtractor extractor =
YouTube.getChannelExtractor("https://www.youtube.com/channel/UCAUc4iz6edWerIjlnL8OSSw"); YouTube.getChannelExtractor("https://www.youtube.com/channel/UCAUc4iz6edWerIjlnL8OSSw");
@ -55,7 +55,7 @@ public class YoutubeChannelExtractorTest {
} }
@Test @Test
public void nonExistentFetch() throws Exception { void nonExistentFetch() throws Exception {
final ChannelExtractor extractor = final ChannelExtractor extractor =
YouTube.getChannelExtractor("https://www.youtube.com/channel/DOESNT-EXIST"); YouTube.getChannelExtractor("https://www.youtube.com/channel/DOESNT-EXIST");
@ -63,7 +63,7 @@ public class YoutubeChannelExtractorTest {
} }
@Test @Test
public void accountTerminatedTOSFetch() throws Exception { void accountTerminatedTOSFetch() throws Exception {
// "This account has been terminated for a violation of YouTube's Terms of Service." // "This account has been terminated for a violation of YouTube's Terms of Service."
final ChannelExtractor extractor = final ChannelExtractor extractor =
YouTube.getChannelExtractor("https://www.youtube.com/channel/UCTGjY2I-ZUGnwVoWAGRd7XQ"); YouTube.getChannelExtractor("https://www.youtube.com/channel/UCTGjY2I-ZUGnwVoWAGRd7XQ");
@ -74,7 +74,7 @@ public class YoutubeChannelExtractorTest {
} }
@Test @Test
public void accountTerminatedCommunityFetch() throws Exception { void accountTerminatedCommunityFetch() throws Exception {
// "This account has been terminated for violating YouTube's Community Guidelines." // "This account has been terminated for violating YouTube's Community Guidelines."
final ChannelExtractor extractor = final ChannelExtractor extractor =
YouTube.getChannelExtractor("https://www.youtube.com/channel/UC0AuOxCr9TZ0TtEgL1zpIgA"); YouTube.getChannelExtractor("https://www.youtube.com/channel/UC0AuOxCr9TZ0TtEgL1zpIgA");
@ -85,7 +85,7 @@ public class YoutubeChannelExtractorTest {
} }
@Test @Test
public void accountTerminatedHateFetch() throws Exception { void accountTerminatedHateFetch() throws Exception {
// "This account has been terminated due to multiple or severe violations // "This account has been terminated due to multiple or severe violations
// of YouTube's policy prohibiting hate speech." // of YouTube's policy prohibiting hate speech."
final ChannelExtractor extractor = final ChannelExtractor extractor =
@ -97,7 +97,7 @@ public class YoutubeChannelExtractorTest {
} }
@Test @Test
public void accountTerminatedBullyFetch() throws Exception { void accountTerminatedBullyFetch() throws Exception {
// "This account has been terminated due to multiple or severe violations // "This account has been terminated due to multiple or severe violations
// of YouTube's policy prohibiting content designed to harass, bully or threaten." // of YouTube's policy prohibiting content designed to harass, bully or threaten."
final ChannelExtractor extractor = final ChannelExtractor extractor =
@ -109,7 +109,7 @@ public class YoutubeChannelExtractorTest {
} }
@Test @Test
public void accountTerminatedSpamFetch() throws Exception { void accountTerminatedSpamFetch() throws Exception {
// "This account has been terminated due to multiple or severe violations // "This account has been terminated due to multiple or severe violations
// of YouTube's policy against spam, deceptive practices and misleading content // of YouTube's policy against spam, deceptive practices and misleading content
// or other Terms of Service violations." // or other Terms of Service violations."
@ -122,7 +122,7 @@ public class YoutubeChannelExtractorTest {
} }
@Test @Test
public void accountTerminatedCopyrightFetch() throws Exception { void accountTerminatedCopyrightFetch() throws Exception {
// "This account has been terminated because we received multiple third-party claims // "This account has been terminated because we received multiple third-party claims
// of copyright infringement regarding material that the user posted." // of copyright infringement regarding material that the user posted."
final ChannelExtractor extractor = final ChannelExtractor extractor =
@ -137,7 +137,7 @@ public class YoutubeChannelExtractorTest {
static class SystemTopic { static class SystemTopic {
@BeforeAll @BeforeAll
static void setUp() throws IOException { static void setUp() {
YoutubeTestsUtils.ensureStateless(); YoutubeTestsUtils.ensureStateless();
NewPipe.init(DownloaderFactory.getDownloader(RESOURCE_PATH + "systemTopic")); NewPipe.init(DownloaderFactory.getDownloader(RESOURCE_PATH + "systemTopic"));
} }

View File

@ -81,7 +81,8 @@ public class YoutubeFeedExtractorTest {
public static class NotAvailable { public static class NotAvailable {
@BeforeAll @BeforeAll
public static void setUp() throws IOException { public static void setUp() {
YoutubeTestsUtils.ensureStateless();
NewPipe.init(DownloaderFactory.getDownloader(RESOURCE_PATH + "notAvailable/")); NewPipe.init(DownloaderFactory.getDownloader(RESOURCE_PATH + "notAvailable/"));
} }

View File

@ -310,7 +310,7 @@ public class YoutubeMixPlaylistExtractorTest {
private static final String VIDEO_ID = "QMVCAPd5cwBcg"; private static final String VIDEO_ID = "QMVCAPd5cwBcg";
@BeforeAll @BeforeAll
public static void setUp() throws IOException { public static void setUp() {
YoutubeTestsUtils.ensureStateless(); YoutubeTestsUtils.ensureStateless();
YoutubeParsingHelper.setConsentAccepted(true); YoutubeParsingHelper.setConsentAccepted(true);
NewPipe.init(DownloaderFactory.getDownloader(RESOURCE_PATH + "invalid")); NewPipe.init(DownloaderFactory.getDownloader(RESOURCE_PATH + "invalid"));

View File

@ -19,7 +19,7 @@ public class YoutubeParsingHelperTest {
private static final String RESOURCE_PATH = DownloaderFactory.RESOURCE_PATH + "services/youtube/"; private static final String RESOURCE_PATH = DownloaderFactory.RESOURCE_PATH + "services/youtube/";
@BeforeAll @BeforeAll
public static void setUp() throws IOException { public static void setUp() {
YoutubeTestsUtils.ensureStateless(); YoutubeTestsUtils.ensureStateless();
NewPipe.init(DownloaderFactory.getDownloader(RESOURCE_PATH + "youtubeParsingHelper")); NewPipe.init(DownloaderFactory.getDownloader(RESOURCE_PATH + "youtubeParsingHelper"));
} }

View File

@ -40,7 +40,7 @@ public class YoutubePlaylistExtractorTest {
public static class NotAvailable { public static class NotAvailable {
@BeforeAll @BeforeAll
public static void setUp() throws IOException { public static void setUp() {
YoutubeTestsUtils.ensureStateless(); YoutubeTestsUtils.ensureStateless();
NewPipe.init(DownloaderFactory.getDownloader(RESOURCE_PATH + "notAvailable")); NewPipe.init(DownloaderFactory.getDownloader(RESOURCE_PATH + "notAvailable"));
} }
@ -549,7 +549,7 @@ public class YoutubePlaylistExtractorTest {
public static class ContinuationsTests { public static class ContinuationsTests {
@BeforeAll @BeforeAll
public static void setUp() throws IOException { public static void setUp() {
YoutubeTestsUtils.ensureStateless(); YoutubeTestsUtils.ensureStateless();
NewPipe.init(DownloaderFactory.getDownloader(RESOURCE_PATH + "continuations")); NewPipe.init(DownloaderFactory.getDownloader(RESOURCE_PATH + "continuations"));
} }

View File

@ -19,7 +19,7 @@ class YoutubeSignaturesTest {
DownloaderFactory.RESOURCE_PATH + "services/youtube/extractor/signatures"; DownloaderFactory.RESOURCE_PATH + "services/youtube/extractor/signatures";
@BeforeEach @BeforeEach
void setUp() throws IOException { void setUp() {
YoutubeTestsUtils.ensureStateless(); YoutubeTestsUtils.ensureStateless();
NewPipe.init(DownloaderFactory.getDownloader(RESOURCE_PATH)); NewPipe.init(DownloaderFactory.getDownloader(RESOURCE_PATH));
} }

View File

@ -28,14 +28,13 @@ import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.schabi.newpipe.downloader.DownloaderFactory; import org.schabi.newpipe.downloader.DownloaderFactory;
import org.schabi.newpipe.extractor.NewPipe; import org.schabi.newpipe.extractor.NewPipe;
import org.schabi.newpipe.extractor.StreamingService;
import org.schabi.newpipe.extractor.kiosk.KioskInfo; import org.schabi.newpipe.extractor.kiosk.KioskInfo;
import org.schabi.newpipe.extractor.linkhandler.LinkHandlerFactory; import org.schabi.newpipe.extractor.linkhandler.LinkHandlerFactory;
/** /**
* Test for {@link KioskInfo} * Test for {@link KioskInfo}
*/ */
public class YoutubeTrendingKioskInfoTest { class YoutubeTrendingKioskInfoTest {
private static final String RESOURCE_PATH = DownloaderFactory.RESOURCE_PATH + "kiosk"; private static final String RESOURCE_PATH = DownloaderFactory.RESOURCE_PATH + "kiosk";
@ -46,24 +45,24 @@ public class YoutubeTrendingKioskInfoTest {
throws Exception { throws Exception {
YoutubeTestsUtils.ensureStateless(); YoutubeTestsUtils.ensureStateless();
NewPipe.init(DownloaderFactory.getDownloader(RESOURCE_PATH)); NewPipe.init(DownloaderFactory.getDownloader(RESOURCE_PATH));
LinkHandlerFactory LinkHandlerFactory = ((StreamingService) YouTube).getKioskList().getListLinkHandlerFactoryByType("Trending"); LinkHandlerFactory linkHandlerFactory = YouTube.getKioskList().getListLinkHandlerFactoryByType("Trending");
kioskInfo = KioskInfo.getInfo(YouTube, LinkHandlerFactory.fromId("Trending").getUrl()); kioskInfo = KioskInfo.getInfo(YouTube, linkHandlerFactory.fromId("Trending").getUrl());
} }
@Test @Test
public void getStreams() { void getStreams() {
assertFalse(kioskInfo.getRelatedItems().isEmpty()); assertFalse(kioskInfo.getRelatedItems().isEmpty());
} }
@Test @Test
public void getId() { void getId() {
assertTrue(kioskInfo.getId().equals("Trending") assertTrue(kioskInfo.getId().equals("Trending")
|| kioskInfo.getId().equals("Trends")); || kioskInfo.getId().equals("Trends"));
} }
@Test @Test
public void getName() { void getName() {
assertFalse(kioskInfo.getName().isEmpty()); assertFalse(kioskInfo.getName().isEmpty());
} }
} }