From 0c37c759812c18c11e7c84ff132ef621e2c2cecd Mon Sep 17 00:00:00 2001 From: Stypox Date: Wed, 16 Mar 2022 17:22:42 +0100 Subject: [PATCH 1/3] Make getDownloader static & extract getDownloaderType --- .../newpipe/downloader/DownloaderFactory.java | 20 +++++++++------- .../youtube/YoutubeChannelExtractorTest.java | 14 +++++------ .../YoutubeChannelLocalizationTest.java | 2 +- .../youtube/YoutubeCommentsExtractorTest.java | 14 +++++------ .../youtube/YoutubeFeedExtractorTest.java | 4 ++-- .../youtube/YoutubeKioskExtractorTest.java | 2 +- .../YoutubeMixPlaylistExtractorTest.java | 10 ++++---- .../youtube/YoutubeParsingHelperTest.java | 2 +- .../youtube/YoutubePlaylistExtractorTest.java | 10 ++++---- .../YoutubeSuggestionExtractorTest.java | 2 +- .../youtube/YoutubeTrendingKioskInfoTest.java | 2 +- .../search/YoutubeSearchExtractorTest.java | 24 +++++++++---------- ...utubeStreamExtractorAgeRestrictedTest.java | 2 +- ...utubeStreamExtractorControversialTest.java | 2 +- .../YoutubeStreamExtractorDefaultTest.java | 14 +++++------ .../YoutubeStreamExtractorLivestreamTest.java | 2 +- .../YoutubeStreamExtractorUnlistedTest.java | 2 +- 17 files changed, 65 insertions(+), 63 deletions(-) diff --git a/extractor/src/test/java/org/schabi/newpipe/downloader/DownloaderFactory.java b/extractor/src/test/java/org/schabi/newpipe/downloader/DownloaderFactory.java index 6799a9150..db805e6bb 100644 --- a/extractor/src/test/java/org/schabi/newpipe/downloader/DownloaderFactory.java +++ b/extractor/src/test/java/org/schabi/newpipe/downloader/DownloaderFactory.java @@ -10,6 +10,14 @@ public class DownloaderFactory { private final static DownloaderType DEFAULT_DOWNLOADER = DownloaderType.REAL; + public static DownloaderType getDownloaderType() { + try { + return DownloaderType.valueOf(System.getProperty("downloader")); + } catch (final Exception e) { + return DEFAULT_DOWNLOADER; + } + } + /** *

* Returns a implementation of a {@link Downloader}. @@ -28,14 +36,8 @@ public class DownloaderFactory { * @param path The path to the folder where mocks are saved/retrieved. * Preferably starting with {@link DownloaderFactory#RESOURCE_PATH} */ - public Downloader getDownloader(String path) throws IOException { - DownloaderType type; - try { - type = DownloaderType.valueOf(System.getProperty("downloader")); - } catch (Exception e) { - type = DEFAULT_DOWNLOADER; - } - + public static Downloader getDownloader(final String path) throws IOException { + final DownloaderType type = getDownloaderType(); switch (type) { case REAL: return DownloaderTestImpl.getInstance(); @@ -44,7 +46,7 @@ public class DownloaderFactory { case RECORDING: return new RecordingDownloader(path); default: - throw new UnsupportedOperationException("Unknown downloader type: " + type.toString()); + throw new UnsupportedOperationException("Unknown downloader type: " + type); } } } diff --git a/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeChannelExtractorTest.java b/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeChannelExtractorTest.java index 98283068f..94966913b 100644 --- a/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeChannelExtractorTest.java +++ b/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeChannelExtractorTest.java @@ -35,7 +35,7 @@ public class YoutubeChannelExtractorTest { public static void setUp() throws IOException { YoutubeParsingHelper.resetClientVersionAndKey(); YoutubeParsingHelper.setNumberGenerator(new Random(1)); - NewPipe.init(new DownloaderFactory().getDownloader(RESOURCE_PATH + "notAvailable")); + NewPipe.init(DownloaderFactory.getDownloader(RESOURCE_PATH + "notAvailable")); } @Test @@ -132,7 +132,7 @@ public class YoutubeChannelExtractorTest { public static void setUp() throws IOException { YoutubeParsingHelper.resetClientVersionAndKey(); YoutubeParsingHelper.setNumberGenerator(new Random(1)); - NewPipe.init(new DownloaderFactory().getDownloader(RESOURCE_PATH + "notSupported")); + NewPipe.init(DownloaderFactory.getDownloader(RESOURCE_PATH + "notSupported")); } @Test @@ -151,7 +151,7 @@ public class YoutubeChannelExtractorTest { public static void setUp() throws Exception { YoutubeParsingHelper.resetClientVersionAndKey(); YoutubeParsingHelper.setNumberGenerator(new Random(1)); - NewPipe.init(new DownloaderFactory().getDownloader(RESOURCE_PATH + "gronkh")); + NewPipe.init(DownloaderFactory.getDownloader(RESOURCE_PATH + "gronkh")); extractor = (YoutubeChannelExtractor) YouTube .getChannelExtractor("http://www.youtube.com/user/Gronkh"); extractor.fetchPage(); @@ -248,7 +248,7 @@ public class YoutubeChannelExtractorTest { public static void setUp() throws Exception { YoutubeParsingHelper.resetClientVersionAndKey(); YoutubeParsingHelper.setNumberGenerator(new Random(1)); - NewPipe.init(new DownloaderFactory().getDownloader(RESOURCE_PATH + "VSauce")); + NewPipe.init(DownloaderFactory.getDownloader(RESOURCE_PATH + "VSauce")); extractor = (YoutubeChannelExtractor) YouTube .getChannelExtractor("https://www.youtube.com/user/Vsauce"); extractor.fetchPage(); @@ -344,7 +344,7 @@ public class YoutubeChannelExtractorTest { public static void setUp() throws Exception { YoutubeParsingHelper.resetClientVersionAndKey(); YoutubeParsingHelper.setNumberGenerator(new Random(1)); - NewPipe.init(new DownloaderFactory().getDownloader(RESOURCE_PATH + "kurzgesagt")); + NewPipe.init(DownloaderFactory.getDownloader(RESOURCE_PATH + "kurzgesagt")); extractor = (YoutubeChannelExtractor) YouTube .getChannelExtractor("https://www.youtube.com/channel/UCsXVk37bltHxD1rDPwtNM8Q"); extractor.fetchPage(); @@ -461,7 +461,7 @@ public class YoutubeChannelExtractorTest { public static void setUp() throws Exception { YoutubeParsingHelper.resetClientVersionAndKey(); YoutubeParsingHelper.setNumberGenerator(new Random(1)); - NewPipe.init(new DownloaderFactory().getDownloader(RESOURCE_PATH + "captainDisillusion")); + NewPipe.init(DownloaderFactory.getDownloader(RESOURCE_PATH + "captainDisillusion")); extractor = (YoutubeChannelExtractor) YouTube .getChannelExtractor("https://www.youtube.com/user/CaptainDisillusion/videos"); extractor.fetchPage(); @@ -556,7 +556,7 @@ public class YoutubeChannelExtractorTest { public static void setUp() throws Exception { YoutubeParsingHelper.resetClientVersionAndKey(); YoutubeParsingHelper.setNumberGenerator(new Random(1)); - NewPipe.init(new DownloaderFactory().getDownloader(RESOURCE_PATH + "random")); + NewPipe.init(DownloaderFactory.getDownloader(RESOURCE_PATH + "random")); extractor = (YoutubeChannelExtractor) YouTube .getChannelExtractor("https://www.youtube.com/channel/UCUaQMQS9lY5lit3vurpXQ6w"); extractor.fetchPage(); diff --git a/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeChannelLocalizationTest.java b/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeChannelLocalizationTest.java index 0c64b0b77..5cf41a769 100644 --- a/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeChannelLocalizationTest.java +++ b/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeChannelLocalizationTest.java @@ -32,7 +32,7 @@ public class YoutubeChannelLocalizationTest { public void testAllSupportedLocalizations() throws Exception { YoutubeParsingHelper.resetClientVersionAndKey(); YoutubeParsingHelper.setNumberGenerator(new Random(1)); - NewPipe.init(new DownloaderFactory().getDownloader(RESOURCE_PATH + "localization")); + NewPipe.init(DownloaderFactory.getDownloader(RESOURCE_PATH + "localization")); testLocalizationsFor("https://www.youtube.com/user/NBCNews"); testLocalizationsFor("https://www.youtube.com/channel/UCcmpeVbSSQlZRvHfdC-CRwg/videos"); diff --git a/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeCommentsExtractorTest.java b/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeCommentsExtractorTest.java index 6ea11a4d4..2c5119d5f 100644 --- a/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeCommentsExtractorTest.java +++ b/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeCommentsExtractorTest.java @@ -40,7 +40,7 @@ public class YoutubeCommentsExtractorTest { public static void setUp() throws Exception { YoutubeParsingHelper.resetClientVersionAndKey(); YoutubeParsingHelper.setNumberGenerator(new Random(1)); - NewPipe.init(new DownloaderFactory().getDownloader(RESOURCE_PATH + "thomas")); + NewPipe.init(DownloaderFactory.getDownloader(RESOURCE_PATH + "thomas")); extractor = (YoutubeCommentsExtractor) YouTube .getCommentsExtractor(url); extractor.fetchPage(); @@ -129,7 +129,7 @@ public class YoutubeCommentsExtractorTest { public static void setUp() throws Exception { YoutubeParsingHelper.resetClientVersionAndKey(); YoutubeParsingHelper.setNumberGenerator(new Random(1)); - NewPipe.init(new DownloaderFactory().getDownloader(RESOURCE_PATH + "empty")); + NewPipe.init(DownloaderFactory.getDownloader(RESOURCE_PATH + "empty")); extractor = (YoutubeCommentsExtractor) YouTube .getCommentsExtractor(url); extractor.fetchPage(); @@ -169,7 +169,7 @@ public class YoutubeCommentsExtractorTest { public static void setUp() throws Exception { YoutubeParsingHelper.resetClientVersionAndKey(); YoutubeParsingHelper.setNumberGenerator(new Random(1)); - NewPipe.init(new DownloaderFactory().getDownloader(RESOURCE_PATH + "hearted")); + NewPipe.init(DownloaderFactory.getDownloader(RESOURCE_PATH + "hearted")); extractor = (YoutubeCommentsExtractor) YouTube .getCommentsExtractor(url); extractor.fetchPage(); @@ -212,7 +212,7 @@ public class YoutubeCommentsExtractorTest { public static void setUp() throws Exception { YoutubeParsingHelper.resetClientVersionAndKey(); YoutubeParsingHelper.setNumberGenerator(new Random(1)); - NewPipe.init(new DownloaderFactory().getDownloader(RESOURCE_PATH + "pinned")); + NewPipe.init(DownloaderFactory.getDownloader(RESOURCE_PATH + "pinned")); extractor = (YoutubeCommentsExtractor) YouTube .getCommentsExtractor(url); extractor.fetchPage(); @@ -254,7 +254,7 @@ public class YoutubeCommentsExtractorTest { public static void setUp() throws Exception { YoutubeParsingHelper.resetClientVersionAndKey(); YoutubeParsingHelper.setNumberGenerator(new Random(1)); - NewPipe.init(new DownloaderFactory().getDownloader(RESOURCE_PATH + "likes")); + NewPipe.init(DownloaderFactory.getDownloader(RESOURCE_PATH + "likes")); extractor = (YoutubeCommentsExtractor) YouTube .getCommentsExtractor(url); extractor.fetchPage(); @@ -286,7 +286,7 @@ public class YoutubeCommentsExtractorTest { public static void setUp() throws Exception { YoutubeParsingHelper.resetClientVersionAndKey(); YoutubeParsingHelper.setNumberGenerator(new Random(1)); - NewPipe.init(new DownloaderFactory().getDownloader(RESOURCE_PATH + "localized_vote_count")); + NewPipe.init(DownloaderFactory.getDownloader(RESOURCE_PATH + "localized_vote_count")); extractor = (YoutubeCommentsExtractor) YouTube .getCommentsExtractor(url); // Force non english local here @@ -315,7 +315,7 @@ public class YoutubeCommentsExtractorTest { public static void setUp() throws Exception { YoutubeParsingHelper.resetClientVersionAndKey(); YoutubeParsingHelper.setNumberGenerator(new Random(1)); - NewPipe.init(new DownloaderFactory().getDownloader(RESOURCE_PATH + "replies")); + NewPipe.init(DownloaderFactory.getDownloader(RESOURCE_PATH + "replies")); extractor = (YoutubeCommentsExtractor) YouTube .getCommentsExtractor(url); extractor.fetchPage(); diff --git a/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeFeedExtractorTest.java b/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeFeedExtractorTest.java index 2ee116b21..ef1605641 100644 --- a/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeFeedExtractorTest.java +++ b/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeFeedExtractorTest.java @@ -30,7 +30,7 @@ public class YoutubeFeedExtractorTest { public static void setUp() throws Exception { YoutubeParsingHelper.resetClientVersionAndKey(); YoutubeParsingHelper.setNumberGenerator(new Random(1)); - NewPipe.init(new DownloaderFactory().getDownloader(RESOURCE_PATH)); + NewPipe.init(DownloaderFactory.getDownloader(RESOURCE_PATH)); extractor = (YoutubeFeedExtractor) YouTube .getFeedExtractor("https://www.youtube.com/user/Kurzgesagt"); extractor.fetchPage(); @@ -84,7 +84,7 @@ public class YoutubeFeedExtractorTest { @BeforeAll public static void setUp() throws IOException { - NewPipe.init(new DownloaderFactory().getDownloader(RESOURCE_PATH + "notAvailable/")); + NewPipe.init(DownloaderFactory.getDownloader(RESOURCE_PATH + "notAvailable/")); } @Test diff --git a/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeKioskExtractorTest.java b/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeKioskExtractorTest.java index f247566e2..23db8a580 100644 --- a/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeKioskExtractorTest.java +++ b/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeKioskExtractorTest.java @@ -26,7 +26,7 @@ public class YoutubeKioskExtractorTest { public static void setUp() throws Exception { YoutubeParsingHelper.resetClientVersionAndKey(); YoutubeParsingHelper.setNumberGenerator(new Random(1)); - NewPipe.init(new DownloaderFactory().getDownloader(RESOURCE_PATH + "trending")); + NewPipe.init(DownloaderFactory.getDownloader(RESOURCE_PATH + "trending")); extractor = (YoutubeTrendingExtractor) YouTube.getKioskList().getDefaultKioskExtractor(); extractor.fetchPage(); } diff --git a/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeMixPlaylistExtractorTest.java b/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeMixPlaylistExtractorTest.java index ff415b920..ceb5852c9 100644 --- a/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeMixPlaylistExtractorTest.java +++ b/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeMixPlaylistExtractorTest.java @@ -41,7 +41,7 @@ public class YoutubeMixPlaylistExtractorTest { public static void setUp() throws Exception { YoutubeParsingHelper.resetClientVersionAndKey(); YoutubeParsingHelper.setNumberGenerator(new Random(1)); - NewPipe.init(new DownloaderFactory().getDownloader(RESOURCE_PATH + "mix")); + NewPipe.init(DownloaderFactory.getDownloader(RESOURCE_PATH + "mix")); dummyCookie.put(YoutubeMixPlaylistExtractor.COOKIE_NAME, "whatever"); extractor = (YoutubeMixPlaylistExtractor) YouTube .getPlaylistExtractor("https://www.youtube.com/watch?v=" + VIDEO_ID @@ -131,7 +131,7 @@ public class YoutubeMixPlaylistExtractorTest { public static void setUp() throws Exception { YoutubeParsingHelper.resetClientVersionAndKey(); YoutubeParsingHelper.setNumberGenerator(new Random(1)); - NewPipe.init(new DownloaderFactory().getDownloader(RESOURCE_PATH + "mixWithIndex")); + NewPipe.init(DownloaderFactory.getDownloader(RESOURCE_PATH + "mixWithIndex")); dummyCookie.put(YoutubeMixPlaylistExtractor.COOKIE_NAME, "whatever"); extractor = (YoutubeMixPlaylistExtractor) YouTube .getPlaylistExtractor("https://www.youtube.com/watch?v=" + VIDEO_ID_NUMBER_4 @@ -212,7 +212,7 @@ public class YoutubeMixPlaylistExtractorTest { public static void setUp() throws Exception { YoutubeParsingHelper.resetClientVersionAndKey(); YoutubeParsingHelper.setNumberGenerator(new Random(1)); - NewPipe.init(new DownloaderFactory().getDownloader(RESOURCE_PATH + "myMix")); + NewPipe.init(DownloaderFactory.getDownloader(RESOURCE_PATH + "myMix")); dummyCookie.put(YoutubeMixPlaylistExtractor.COOKIE_NAME, "whatever"); extractor = (YoutubeMixPlaylistExtractor) YouTube .getPlaylistExtractor("https://www.youtube.com/watch?v=" + VIDEO_ID @@ -297,7 +297,7 @@ public class YoutubeMixPlaylistExtractorTest { public static void setUp() throws IOException { YoutubeParsingHelper.resetClientVersionAndKey(); YoutubeParsingHelper.setNumberGenerator(new Random(1)); - NewPipe.init(new DownloaderFactory().getDownloader(RESOURCE_PATH + "invalid")); + NewPipe.init(DownloaderFactory.getDownloader(RESOURCE_PATH + "invalid")); dummyCookie.put(YoutubeMixPlaylistExtractor.COOKIE_NAME, "whatever"); } @@ -332,7 +332,7 @@ public class YoutubeMixPlaylistExtractorTest { public static void setUp() throws Exception { YoutubeParsingHelper.resetClientVersionAndKey(); YoutubeParsingHelper.setNumberGenerator(new Random(1)); - NewPipe.init(new DownloaderFactory().getDownloader(RESOURCE_PATH + "channelMix")); + NewPipe.init(DownloaderFactory.getDownloader(RESOURCE_PATH + "channelMix")); dummyCookie.put(YoutubeMixPlaylistExtractor.COOKIE_NAME, "whatever"); extractor = (YoutubeMixPlaylistExtractor) YouTube .getPlaylistExtractor("https://www.youtube.com/watch?v=" + VIDEO_ID_OF_CHANNEL diff --git a/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeParsingHelperTest.java b/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeParsingHelperTest.java index 7ae7d4d15..45643ec03 100644 --- a/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeParsingHelperTest.java +++ b/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeParsingHelperTest.java @@ -21,7 +21,7 @@ public class YoutubeParsingHelperTest { public static void setUp() throws IOException { YoutubeParsingHelper.resetClientVersionAndKey(); YoutubeParsingHelper.setNumberGenerator(new Random(1)); - NewPipe.init(new DownloaderFactory().getDownloader(RESOURCE_PATH + "youtubeParsingHelper")); + NewPipe.init(DownloaderFactory.getDownloader(RESOURCE_PATH + "youtubeParsingHelper")); } @Test diff --git a/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubePlaylistExtractorTest.java b/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubePlaylistExtractorTest.java index fdd919387..e0b9a205b 100644 --- a/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubePlaylistExtractorTest.java +++ b/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubePlaylistExtractorTest.java @@ -42,7 +42,7 @@ public class YoutubePlaylistExtractorTest { public static void setUp() throws IOException { YoutubeParsingHelper.resetClientVersionAndKey(); YoutubeParsingHelper.setNumberGenerator(new Random(1)); - NewPipe.init(new DownloaderFactory().getDownloader(RESOURCE_PATH + "notAvailable")); + NewPipe.init(DownloaderFactory.getDownloader(RESOURCE_PATH + "notAvailable")); } @Test @@ -67,7 +67,7 @@ public class YoutubePlaylistExtractorTest { public static void setUp() throws Exception { YoutubeParsingHelper.resetClientVersionAndKey(); YoutubeParsingHelper.setNumberGenerator(new Random(1)); - NewPipe.init(new DownloaderFactory().getDownloader(RESOURCE_PATH + "TimelessPopHits")); + NewPipe.init(DownloaderFactory.getDownloader(RESOURCE_PATH + "TimelessPopHits")); extractor = (YoutubePlaylistExtractor) YouTube .getPlaylistExtractor("http://www.youtube.com/watch?v=lp-EO5I60KA&list=PLMC9KNkIncKtPzgY-5rmhvj7fax8fdxoj"); extractor.fetchPage(); @@ -170,7 +170,7 @@ public class YoutubePlaylistExtractorTest { public static void setUp() throws Exception { YoutubeParsingHelper.resetClientVersionAndKey(); YoutubeParsingHelper.setNumberGenerator(new Random(1)); - NewPipe.init(new DownloaderFactory().getDownloader(RESOURCE_PATH + "huge")); + NewPipe.init(DownloaderFactory.getDownloader(RESOURCE_PATH + "huge")); extractor = (YoutubePlaylistExtractor) YouTube .getPlaylistExtractor("https://www.youtube.com/watch?v=8SbUC-UaAxE&list=PLWwAypAcFRgKAIIFqBr9oy-ZYZnixa_Fj"); extractor.fetchPage(); @@ -289,7 +289,7 @@ public class YoutubePlaylistExtractorTest { public static void setUp() throws Exception { YoutubeParsingHelper.resetClientVersionAndKey(); YoutubeParsingHelper.setNumberGenerator(new Random(1)); - NewPipe.init(new DownloaderFactory().getDownloader(RESOURCE_PATH + "learning")); + NewPipe.init(DownloaderFactory.getDownloader(RESOURCE_PATH + "learning")); extractor = (YoutubePlaylistExtractor) YouTube .getPlaylistExtractor("https://www.youtube.com/playlist?list=PL8dPuuaLjXtOAKed_MxxWBNaPno5h3Zs8"); extractor.fetchPage(); @@ -392,7 +392,7 @@ public class YoutubePlaylistExtractorTest { public static void setUp() throws IOException { YoutubeParsingHelper.resetClientVersionAndKey(); YoutubeParsingHelper.setNumberGenerator(new Random(1)); - NewPipe.init(new DownloaderFactory().getDownloader(RESOURCE_PATH + "continuations")); + NewPipe.init(DownloaderFactory.getDownloader(RESOURCE_PATH + "continuations")); } @Test diff --git a/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeSuggestionExtractorTest.java b/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeSuggestionExtractorTest.java index dd9d5b176..1855f8a03 100644 --- a/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeSuggestionExtractorTest.java +++ b/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeSuggestionExtractorTest.java @@ -47,7 +47,7 @@ public class YoutubeSuggestionExtractorTest { public static void setUp() throws Exception { YoutubeParsingHelper.resetClientVersionAndKey(); YoutubeParsingHelper.setNumberGenerator(new Random(1)); - NewPipe.init(new DownloaderFactory().getDownloader(RESOURCE_PATH + ""), new Localization("de", "DE")); + NewPipe.init(DownloaderFactory.getDownloader(RESOURCE_PATH + ""), new Localization("de", "DE")); suggestionExtractor = YouTube.getSuggestionExtractor(); } diff --git a/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeTrendingKioskInfoTest.java b/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeTrendingKioskInfoTest.java index b11a2f068..164ffc8f5 100644 --- a/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeTrendingKioskInfoTest.java +++ b/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeTrendingKioskInfoTest.java @@ -48,7 +48,7 @@ public class YoutubeTrendingKioskInfoTest { throws Exception { YoutubeParsingHelper.resetClientVersionAndKey(); YoutubeParsingHelper.setNumberGenerator(new Random(1)); - NewPipe.init(new DownloaderFactory().getDownloader(RESOURCE_PATH)); + NewPipe.init(DownloaderFactory.getDownloader(RESOURCE_PATH)); LinkHandlerFactory LinkHandlerFactory = ((StreamingService) YouTube).getKioskList().getListLinkHandlerFactoryByType("Trending"); kioskInfo = KioskInfo.getInfo(YouTube, LinkHandlerFactory.fromId("Trending").getUrl()); diff --git a/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/search/YoutubeSearchExtractorTest.java b/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/search/YoutubeSearchExtractorTest.java index 3a1de7ebe..8b5aaf655 100644 --- a/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/search/YoutubeSearchExtractorTest.java +++ b/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/search/YoutubeSearchExtractorTest.java @@ -41,7 +41,7 @@ public class YoutubeSearchExtractorTest { public static void setUp() throws Exception { YoutubeParsingHelper.resetClientVersionAndKey(); YoutubeParsingHelper.setNumberGenerator(new Random(1)); - NewPipe.init(new DownloaderFactory().getDownloader(RESOURCE_PATH + "all")); + NewPipe.init(DownloaderFactory.getDownloader(RESOURCE_PATH + "all")); extractor = YouTube.getSearchExtractor(QUERY); extractor.fetchPage(); } @@ -64,7 +64,7 @@ public class YoutubeSearchExtractorTest { public static void setUp() throws Exception { YoutubeParsingHelper.resetClientVersionAndKey(); YoutubeParsingHelper.setNumberGenerator(new Random(1)); - NewPipe.init(new DownloaderFactory().getDownloader(RESOURCE_PATH + "channel")); + NewPipe.init(DownloaderFactory.getDownloader(RESOURCE_PATH + "channel")); extractor = YouTube.getSearchExtractor(QUERY, singletonList(CHANNELS), ""); extractor.fetchPage(); } @@ -89,7 +89,7 @@ public class YoutubeSearchExtractorTest { public static void setUp() throws Exception { YoutubeParsingHelper.resetClientVersionAndKey(); YoutubeParsingHelper.setNumberGenerator(new Random(1)); - NewPipe.init(new DownloaderFactory().getDownloader(RESOURCE_PATH + "playlist")); + NewPipe.init(DownloaderFactory.getDownloader(RESOURCE_PATH + "playlist")); extractor = YouTube.getSearchExtractor(QUERY, singletonList(PLAYLISTS), ""); extractor.fetchPage(); } @@ -114,7 +114,7 @@ public class YoutubeSearchExtractorTest { public static void setUp() throws Exception { YoutubeParsingHelper.resetClientVersionAndKey(); YoutubeParsingHelper.setNumberGenerator(new Random(1)); - NewPipe.init(new DownloaderFactory().getDownloader(RESOURCE_PATH + "videos")); + NewPipe.init(DownloaderFactory.getDownloader(RESOURCE_PATH + "videos")); extractor = YouTube.getSearchExtractor(QUERY, singletonList(VIDEOS), ""); extractor.fetchPage(); } @@ -140,7 +140,7 @@ public class YoutubeSearchExtractorTest { public static void setUp() throws Exception { YoutubeParsingHelper.resetClientVersionAndKey(); YoutubeParsingHelper.setNumberGenerator(new Random(1)); - NewPipe.init(new DownloaderFactory().getDownloader(RESOURCE_PATH + "suggestions")); + NewPipe.init(DownloaderFactory.getDownloader(RESOURCE_PATH + "suggestions")); extractor = YouTube.getSearchExtractor(QUERY, singletonList(VIDEOS), ""); extractor.fetchPage(); } @@ -165,7 +165,7 @@ public class YoutubeSearchExtractorTest { public static void setUp() throws Exception { YoutubeParsingHelper.resetClientVersionAndKey(); YoutubeParsingHelper.setNumberGenerator(new Random(1)); - NewPipe.init(new DownloaderFactory().getDownloader(RESOURCE_PATH + "corrected")); + NewPipe.init(DownloaderFactory.getDownloader(RESOURCE_PATH + "corrected")); extractor = YouTube.getSearchExtractor(QUERY, singletonList(VIDEOS), ""); extractor.fetchPage(); } @@ -190,7 +190,7 @@ public class YoutubeSearchExtractorTest { public static void setUp() throws Exception { YoutubeParsingHelper.resetClientVersionAndKey(); YoutubeParsingHelper.setNumberGenerator(new Random(1)); - NewPipe.init(new DownloaderFactory().getDownloader(RESOURCE_PATH + "random")); + NewPipe.init(DownloaderFactory.getDownloader(RESOURCE_PATH + "random")); extractor = YouTube.getSearchExtractor(QUERY); extractor.fetchPage(); } @@ -226,7 +226,7 @@ public class YoutubeSearchExtractorTest { public void duplicatedItemsCheck() throws Exception { YoutubeParsingHelper.resetClientVersionAndKey(); YoutubeParsingHelper.setNumberGenerator(new Random(1)); - NewPipe.init(new DownloaderFactory().getDownloader(RESOURCE_PATH + "paging")); + NewPipe.init(DownloaderFactory.getDownloader(RESOURCE_PATH + "paging")); final SearchExtractor extractor = YouTube.getSearchExtractor("cirque du soleil", singletonList(VIDEOS), ""); extractor.fetchPage(); @@ -245,7 +245,7 @@ public class YoutubeSearchExtractorTest { public static void setUp() throws Exception { YoutubeParsingHelper.resetClientVersionAndKey(); YoutubeParsingHelper.setNumberGenerator(new Random(1)); - NewPipe.init(new DownloaderFactory().getDownloader(RESOURCE_PATH + "metaInfo")); + NewPipe.init(DownloaderFactory.getDownloader(RESOURCE_PATH + "metaInfo")); extractor = YouTube.getSearchExtractor(QUERY, singletonList(VIDEOS), ""); extractor.fetchPage(); } @@ -282,7 +282,7 @@ public class YoutubeSearchExtractorTest { public static void setUp() throws Exception { YoutubeParsingHelper.resetClientVersionAndKey(); YoutubeParsingHelper.setNumberGenerator(new Random(1)); - NewPipe.init(new DownloaderFactory().getDownloader(RESOURCE_PATH + "verified")); + NewPipe.init(DownloaderFactory.getDownloader(RESOURCE_PATH + "verified")); extractor = YouTube.getSearchExtractor(QUERY, singletonList(CHANNELS), ""); extractor.fetchPage(); } @@ -320,7 +320,7 @@ public class YoutubeSearchExtractorTest { public static void setUp() throws Exception { YoutubeParsingHelper.resetClientVersionAndKey(); YoutubeParsingHelper.setNumberGenerator(new Random(1)); - NewPipe.init(new DownloaderFactory().getDownloader(RESOURCE_PATH + "video_uploader_avatar")); + NewPipe.init(DownloaderFactory.getDownloader(RESOURCE_PATH + "video_uploader_avatar")); extractor = YouTube.getSearchExtractor(QUERY, singletonList(VIDEOS), ""); extractor.fetchPage(); } @@ -352,7 +352,7 @@ public class YoutubeSearchExtractorTest { public static void setUp() throws Exception { YoutubeParsingHelper.resetClientVersionAndKey(); YoutubeParsingHelper.setNumberGenerator(new Random(1)); - NewPipe.init(new DownloaderFactory().getDownloader(RESOURCE_PATH + "video_description")); + NewPipe.init(DownloaderFactory.getDownloader(RESOURCE_PATH + "video_description")); extractor = YouTube.getSearchExtractor(QUERY, singletonList(VIDEOS), ""); extractor.fetchPage(); } diff --git a/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/stream/YoutubeStreamExtractorAgeRestrictedTest.java b/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/stream/YoutubeStreamExtractorAgeRestrictedTest.java index 1ac7b5c1e..b907bcec6 100644 --- a/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/stream/YoutubeStreamExtractorAgeRestrictedTest.java +++ b/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/stream/YoutubeStreamExtractorAgeRestrictedTest.java @@ -30,7 +30,7 @@ public class YoutubeStreamExtractorAgeRestrictedTest extends DefaultStreamExtrac YoutubeParsingHelper.resetClientVersionAndKey(); YoutubeParsingHelper.setNumberGenerator(new Random(1)); YoutubeStreamExtractor.resetDeobfuscationCode(); - NewPipe.init(new DownloaderFactory().getDownloader(RESOURCE_PATH + "ageRestricted")); + NewPipe.init(DownloaderFactory.getDownloader(RESOURCE_PATH + "ageRestricted")); extractor = YouTube.getStreamExtractor(URL); extractor.fetchPage(); } diff --git a/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/stream/YoutubeStreamExtractorControversialTest.java b/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/stream/YoutubeStreamExtractorControversialTest.java index 73d7648aa..51bbbecda 100644 --- a/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/stream/YoutubeStreamExtractorControversialTest.java +++ b/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/stream/YoutubeStreamExtractorControversialTest.java @@ -34,7 +34,7 @@ public class YoutubeStreamExtractorControversialTest extends DefaultStreamExtrac YoutubeParsingHelper.resetClientVersionAndKey(); YoutubeParsingHelper.setNumberGenerator(new Random(1)); YoutubeStreamExtractor.resetDeobfuscationCode(); - NewPipe.init(new DownloaderFactory().getDownloader(RESOURCE_PATH + "controversial")); + NewPipe.init(DownloaderFactory.getDownloader(RESOURCE_PATH + "controversial")); extractor = YouTube.getStreamExtractor(URL); extractor.fetchPage(); } diff --git a/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/stream/YoutubeStreamExtractorDefaultTest.java b/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/stream/YoutubeStreamExtractorDefaultTest.java index 67e80c297..e8681e5b4 100644 --- a/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/stream/YoutubeStreamExtractorDefaultTest.java +++ b/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/stream/YoutubeStreamExtractorDefaultTest.java @@ -65,7 +65,7 @@ public class YoutubeStreamExtractorDefaultTest { YoutubeParsingHelper.resetClientVersionAndKey(); YoutubeParsingHelper.setNumberGenerator(new Random(1)); YoutubeStreamExtractor.resetDeobfuscationCode(); - NewPipe.init(new DownloaderFactory().getDownloader(RESOURCE_PATH + "notAvailable")); + NewPipe.init(DownloaderFactory.getDownloader(RESOURCE_PATH + "notAvailable")); } @Test @@ -122,7 +122,7 @@ public class YoutubeStreamExtractorDefaultTest { YoutubeParsingHelper.resetClientVersionAndKey(); YoutubeParsingHelper.setNumberGenerator(new Random(1)); YoutubeStreamExtractor.resetDeobfuscationCode(); - NewPipe.init(new DownloaderFactory().getDownloader(RESOURCE_PATH + "pewdiwpie")); + NewPipe.init(DownloaderFactory.getDownloader(RESOURCE_PATH + "pewdiwpie")); extractor = YouTube.getStreamExtractor(URL); extractor.fetchPage(); } @@ -167,7 +167,7 @@ public class YoutubeStreamExtractorDefaultTest { YoutubeParsingHelper.resetClientVersionAndKey(); YoutubeParsingHelper.setNumberGenerator(new Random(1)); YoutubeStreamExtractor.resetDeobfuscationCode(); - NewPipe.init(new DownloaderFactory().getDownloader(RESOURCE_PATH + "unboxing")); + NewPipe.init(DownloaderFactory.getDownloader(RESOURCE_PATH + "unboxing")); extractor = YouTube.getStreamExtractor(URL); extractor.fetchPage(); } @@ -222,7 +222,7 @@ public class YoutubeStreamExtractorDefaultTest { YoutubeParsingHelper.resetClientVersionAndKey(); YoutubeParsingHelper.setNumberGenerator(new Random(1)); YoutubeStreamExtractor.resetDeobfuscationCode(); - NewPipe.init(new DownloaderFactory().getDownloader(RESOURCE_PATH + "ratingsDisabled")); + NewPipe.init(DownloaderFactory.getDownloader(RESOURCE_PATH + "ratingsDisabled")); extractor = YouTube.getStreamExtractor(URL); extractor.fetchPage(); } @@ -260,7 +260,7 @@ public class YoutubeStreamExtractorDefaultTest { YoutubeParsingHelper.resetClientVersionAndKey(); YoutubeParsingHelper.setNumberGenerator(new Random(1)); YoutubeStreamExtractor.resetDeobfuscationCode(); - NewPipe.init(new DownloaderFactory().getDownloader(RESOURCE_PATH + "streamSegmentsTagesschau")); + NewPipe.init(DownloaderFactory.getDownloader(RESOURCE_PATH + "streamSegmentsTagesschau")); extractor = YouTube.getStreamExtractor(URL); extractor.fetchPage(); } @@ -323,7 +323,7 @@ public class YoutubeStreamExtractorDefaultTest { YoutubeParsingHelper.resetClientVersionAndKey(); YoutubeParsingHelper.setNumberGenerator(new Random(1)); YoutubeStreamExtractor.resetDeobfuscationCode(); - NewPipe.init(new DownloaderFactory().getDownloader(RESOURCE_PATH + "streamSegmentsMaiLab")); + NewPipe.init(DownloaderFactory.getDownloader(RESOURCE_PATH + "streamSegmentsMaiLab")); extractor = YouTube.getStreamExtractor(URL); extractor.fetchPage(); } @@ -392,7 +392,7 @@ public class YoutubeStreamExtractorDefaultTest { public static void setUp() throws Exception { YoutubeParsingHelper.resetClientVersionAndKey(); YoutubeParsingHelper.setNumberGenerator(new Random(1)); - NewPipe.init(new DownloaderFactory().getDownloader(RESOURCE_PATH + "publicBroadcast")); + NewPipe.init(DownloaderFactory.getDownloader(RESOURCE_PATH + "publicBroadcast")); extractor = YouTube.getStreamExtractor(URL); YoutubeStreamExtractor.resetDeobfuscationCode(); extractor.fetchPage(); diff --git a/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/stream/YoutubeStreamExtractorLivestreamTest.java b/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/stream/YoutubeStreamExtractorLivestreamTest.java index 3a3e7f0bb..2ec365822 100644 --- a/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/stream/YoutubeStreamExtractorLivestreamTest.java +++ b/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/stream/YoutubeStreamExtractorLivestreamTest.java @@ -31,7 +31,7 @@ public class YoutubeStreamExtractorLivestreamTest extends DefaultStreamExtractor YoutubeParsingHelper.resetClientVersionAndKey(); YoutubeParsingHelper.setNumberGenerator(new Random(1)); YoutubeStreamExtractor.resetDeobfuscationCode(); - NewPipe.init(new DownloaderFactory().getDownloader(RESOURCE_PATH + "live")); + NewPipe.init(DownloaderFactory.getDownloader(RESOURCE_PATH + "live")); extractor = YouTube.getStreamExtractor(URL); extractor.fetchPage(); } diff --git a/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/stream/YoutubeStreamExtractorUnlistedTest.java b/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/stream/YoutubeStreamExtractorUnlistedTest.java index a3f2b0b21..634391d07 100644 --- a/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/stream/YoutubeStreamExtractorUnlistedTest.java +++ b/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/stream/YoutubeStreamExtractorUnlistedTest.java @@ -30,7 +30,7 @@ public class YoutubeStreamExtractorUnlistedTest extends DefaultStreamExtractorTe YoutubeParsingHelper.resetClientVersionAndKey(); YoutubeParsingHelper.setNumberGenerator(new Random(1)); YoutubeStreamExtractor.resetDeobfuscationCode(); - NewPipe.init(new DownloaderFactory().getDownloader(RESOURCE_PATH + "unlisted")); + NewPipe.init(DownloaderFactory.getDownloader(RESOURCE_PATH + "unlisted")); extractor = YouTube.getStreamExtractor(URL); extractor.fetchPage(); } From ef71a5fa0f710eab04927a239d370e8f7201dd17 Mon Sep 17 00:00:00 2001 From: Stypox Date: Wed, 16 Mar 2022 17:24:33 +0100 Subject: [PATCH 2/3] static final instead of final static --- .../java/org/schabi/newpipe/downloader/DownloaderFactory.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/extractor/src/test/java/org/schabi/newpipe/downloader/DownloaderFactory.java b/extractor/src/test/java/org/schabi/newpipe/downloader/DownloaderFactory.java index db805e6bb..ee207c412 100644 --- a/extractor/src/test/java/org/schabi/newpipe/downloader/DownloaderFactory.java +++ b/extractor/src/test/java/org/schabi/newpipe/downloader/DownloaderFactory.java @@ -6,9 +6,9 @@ import java.io.IOException; public class DownloaderFactory { - public final static String RESOURCE_PATH = "src/test/resources/org/schabi/newpipe/extractor/"; + public static final String RESOURCE_PATH = "src/test/resources/org/schabi/newpipe/extractor/"; - private final static DownloaderType DEFAULT_DOWNLOADER = DownloaderType.REAL; + private static final DownloaderType DEFAULT_DOWNLOADER = DownloaderType.REAL; public static DownloaderType getDownloaderType() { try { From 73d1fd472fdf17204ae86cb18b6e01167e4339ff Mon Sep 17 00:00:00 2001 From: Stypox Date: Wed, 16 Mar 2022 17:24:55 +0100 Subject: [PATCH 3/3] Add MockOnly junit 5 test extension --- .../schabi/newpipe/downloader/MockOnly.java | 20 ++++++++++++++++++ .../newpipe/downloader/MockOnlyCondition.java | 21 +++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 extractor/src/test/java/org/schabi/newpipe/downloader/MockOnly.java create mode 100644 extractor/src/test/java/org/schabi/newpipe/downloader/MockOnlyCondition.java diff --git a/extractor/src/test/java/org/schabi/newpipe/downloader/MockOnly.java b/extractor/src/test/java/org/schabi/newpipe/downloader/MockOnly.java new file mode 100644 index 000000000..d6ca17124 --- /dev/null +++ b/extractor/src/test/java/org/schabi/newpipe/downloader/MockOnly.java @@ -0,0 +1,20 @@ +package org.schabi.newpipe.downloader; + +import org.junit.jupiter.api.extension.ExtendWith; + +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; + +/** + * Use this to annotate tests methods/classes that should only be run when the downloader is of type + * {@link DownloaderType#MOCK} or {@link DownloaderType#RECORDING}. This should be used when e.g. an + * extractor returns different results each time because the underlying service web page does so. In + * that case it makes sense to only run the tests with the mock downloader, since the real web page + * is not reliable, but we still want to make sure that the code correctly interprets the stored and + * mocked web page data. + * @see MockOnlyCondition + */ +@Retention(RetentionPolicy.RUNTIME) +@ExtendWith(MockOnlyCondition.class) +public @interface MockOnly { +} diff --git a/extractor/src/test/java/org/schabi/newpipe/downloader/MockOnlyCondition.java b/extractor/src/test/java/org/schabi/newpipe/downloader/MockOnlyCondition.java new file mode 100644 index 000000000..82f57fa65 --- /dev/null +++ b/extractor/src/test/java/org/schabi/newpipe/downloader/MockOnlyCondition.java @@ -0,0 +1,21 @@ +package org.schabi.newpipe.downloader; + +import org.junit.jupiter.api.extension.ConditionEvaluationResult; +import org.junit.jupiter.api.extension.ExecutionCondition; +import org.junit.jupiter.api.extension.ExtensionContext; + +/** + * @see MockOnly + */ +public class MockOnlyCondition implements ExecutionCondition { + private static final String MOCK_ONLY_REASON = "Mock only"; + + @Override + public ConditionEvaluationResult evaluateExecutionCondition(final ExtensionContext context) { + if (DownloaderFactory.getDownloaderType() == DownloaderType.REAL) { + return ConditionEvaluationResult.disabled(MOCK_ONLY_REASON); + } else { + return ConditionEvaluationResult.enabled(MOCK_ONLY_REASON); + } + } +}