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.
* 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();
switch (type) {
case REAL:

View File

@ -10,6 +10,7 @@ import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
@ -26,19 +27,22 @@ class MockDownloader extends Downloader {
private final String path;
private final Map<Request, Response> mocks;
public MockDownloader(@Nonnull final String path) throws IOException {
public MockDownloader(@Nonnull final String path) {
this.path = path;
this.mocks = new HashMap<>();
final File[] files = new File(path).listFiles();
if (files != null) {
for (final File file : files) {
if (file.getName().startsWith(RecordingDownloader.FILE_NAME_PREFIX)) {
final InputStreamReader reader = new InputStreamReader(new FileInputStream(
file), StandardCharsets.UTF_8);
final TestRequestResponse response = new GsonBuilder()
.create()
.fromJson(reader, TestRequestResponse.class);
reader.close();
final TestRequestResponse response;
try(final InputStreamReader reader = new InputStreamReader(
new FileInputStream(file), StandardCharsets.UTF_8)) {
response = new GsonBuilder()
.create()
.fromJson(reader, TestRequestResponse.class);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
mocks.put(response.getRequest(), response.getResponse());
}
}

View File

@ -11,6 +11,7 @@ import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UncheckedIOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
@ -51,7 +52,7 @@ class RecordingDownloader extends Downloader {
* Deletes existing files starting with {@link RecordingDownloader#FILE_NAME_PREFIX}.
* @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;
final Path path = Paths.get(stringPath);
final File folder = path.toFile();
@ -62,7 +63,11 @@ class RecordingDownloader extends Downloader {
}
}
} 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 {
@BeforeAll
public static void setUp() throws IOException {
public static void setUp() {
YoutubeTestsUtils.ensureStateless();
NewPipe.init(DownloaderFactory.getDownloader(RESOURCE_PATH + "notAvailable"));
}
@Test
public void deletedFetch() throws Exception {
void deletedFetch() throws Exception {
final ChannelExtractor extractor =
YouTube.getChannelExtractor("https://www.youtube.com/channel/UCAUc4iz6edWerIjlnL8OSSw");
@ -55,7 +55,7 @@ public class YoutubeChannelExtractorTest {
}
@Test
public void nonExistentFetch() throws Exception {
void nonExistentFetch() throws Exception {
final ChannelExtractor extractor =
YouTube.getChannelExtractor("https://www.youtube.com/channel/DOESNT-EXIST");
@ -63,7 +63,7 @@ public class YoutubeChannelExtractorTest {
}
@Test
public void accountTerminatedTOSFetch() throws Exception {
void accountTerminatedTOSFetch() throws Exception {
// "This account has been terminated for a violation of YouTube's Terms of Service."
final ChannelExtractor extractor =
YouTube.getChannelExtractor("https://www.youtube.com/channel/UCTGjY2I-ZUGnwVoWAGRd7XQ");
@ -74,7 +74,7 @@ public class YoutubeChannelExtractorTest {
}
@Test
public void accountTerminatedCommunityFetch() throws Exception {
void accountTerminatedCommunityFetch() throws Exception {
// "This account has been terminated for violating YouTube's Community Guidelines."
final ChannelExtractor extractor =
YouTube.getChannelExtractor("https://www.youtube.com/channel/UC0AuOxCr9TZ0TtEgL1zpIgA");
@ -85,7 +85,7 @@ public class YoutubeChannelExtractorTest {
}
@Test
public void accountTerminatedHateFetch() throws Exception {
void accountTerminatedHateFetch() throws Exception {
// "This account has been terminated due to multiple or severe violations
// of YouTube's policy prohibiting hate speech."
final ChannelExtractor extractor =
@ -97,7 +97,7 @@ public class YoutubeChannelExtractorTest {
}
@Test
public void accountTerminatedBullyFetch() throws Exception {
void accountTerminatedBullyFetch() throws Exception {
// "This account has been terminated due to multiple or severe violations
// of YouTube's policy prohibiting content designed to harass, bully or threaten."
final ChannelExtractor extractor =
@ -109,7 +109,7 @@ public class YoutubeChannelExtractorTest {
}
@Test
public void accountTerminatedSpamFetch() throws Exception {
void accountTerminatedSpamFetch() throws Exception {
// "This account has been terminated due to multiple or severe violations
// of YouTube's policy against spam, deceptive practices and misleading content
// or other Terms of Service violations."
@ -122,7 +122,7 @@ public class YoutubeChannelExtractorTest {
}
@Test
public void accountTerminatedCopyrightFetch() throws Exception {
void accountTerminatedCopyrightFetch() throws Exception {
// "This account has been terminated because we received multiple third-party claims
// of copyright infringement regarding material that the user posted."
final ChannelExtractor extractor =
@ -137,7 +137,7 @@ public class YoutubeChannelExtractorTest {
static class SystemTopic {
@BeforeAll
static void setUp() throws IOException {
static void setUp() {
YoutubeTestsUtils.ensureStateless();
NewPipe.init(DownloaderFactory.getDownloader(RESOURCE_PATH + "systemTopic"));
}

View File

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

View File

@ -310,7 +310,7 @@ public class YoutubeMixPlaylistExtractorTest {
private static final String VIDEO_ID = "QMVCAPd5cwBcg";
@BeforeAll
public static void setUp() throws IOException {
public static void setUp() {
YoutubeTestsUtils.ensureStateless();
YoutubeParsingHelper.setConsentAccepted(true);
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/";
@BeforeAll
public static void setUp() throws IOException {
public static void setUp() {
YoutubeTestsUtils.ensureStateless();
NewPipe.init(DownloaderFactory.getDownloader(RESOURCE_PATH + "youtubeParsingHelper"));
}

View File

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

View File

@ -19,7 +19,7 @@ class YoutubeSignaturesTest {
DownloaderFactory.RESOURCE_PATH + "services/youtube/extractor/signatures";
@BeforeEach
void setUp() throws IOException {
void setUp() {
YoutubeTestsUtils.ensureStateless();
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.schabi.newpipe.downloader.DownloaderFactory;
import org.schabi.newpipe.extractor.NewPipe;
import org.schabi.newpipe.extractor.StreamingService;
import org.schabi.newpipe.extractor.kiosk.KioskInfo;
import org.schabi.newpipe.extractor.linkhandler.LinkHandlerFactory;
/**
* Test for {@link KioskInfo}
*/
public class YoutubeTrendingKioskInfoTest {
class YoutubeTrendingKioskInfoTest {
private static final String RESOURCE_PATH = DownloaderFactory.RESOURCE_PATH + "kiosk";
@ -46,24 +45,24 @@ public class YoutubeTrendingKioskInfoTest {
throws Exception {
YoutubeTestsUtils.ensureStateless();
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
public void getStreams() {
void getStreams() {
assertFalse(kioskInfo.getRelatedItems().isEmpty());
}
@Test
public void getId() {
void getId() {
assertTrue(kioskInfo.getId().equals("Trending")
|| kioskInfo.getId().equals("Trends"));
}
@Test
public void getName() {
void getName() {
assertFalse(kioskInfo.getName().isEmpty());
}
}