Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add @MockOnly Junit 5 extension #816

Merged
merged 3 commits into from
Mar 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,17 @@

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 {
return DownloaderType.valueOf(System.getProperty("downloader"));
} catch (final Exception e) {
return DEFAULT_DOWNLOADER;
}
}

/**
* <p>
Expand All @@ -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();
Expand All @@ -44,7 +46,7 @@ public Downloader getDownloader(String path) throws IOException {
case RECORDING:
return new RecordingDownloader(path);
default:
throw new UnsupportedOperationException("Unknown downloader type: " + type.toString());
throw new UnsupportedOperationException("Unknown downloader type: " + type);
}
}
}
Original file line number Diff line number Diff line change
@@ -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 {
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public static class NotAvailable {
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
Expand Down Expand Up @@ -132,7 +132,7 @@ public static class NotSupported {
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
Expand All @@ -151,7 +151,7 @@ public static class Gronkh implements BaseChannelExtractorTest {
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();
Expand Down Expand Up @@ -248,7 +248,7 @@ public static class VSauce implements BaseChannelExtractorTest {
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();
Expand Down Expand Up @@ -344,7 +344,7 @@ public static class Kurzgesagt implements BaseChannelExtractorTest {
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();
Expand Down Expand Up @@ -461,7 +461,7 @@ public static class CaptainDisillusion implements BaseChannelExtractorTest {
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();
Expand Down Expand Up @@ -556,7 +556,7 @@ public static class RandomChannel implements BaseChannelExtractorTest {
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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public static class Thomas {
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();
Expand Down Expand Up @@ -129,7 +129,7 @@ public static class EmptyComment {
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();
Expand Down Expand Up @@ -169,7 +169,7 @@ public static class HeartedByCreator {
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();
Expand Down Expand Up @@ -212,7 +212,7 @@ public static class Pinned {
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();
Expand Down Expand Up @@ -254,7 +254,7 @@ public static class LikesVotes {
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();
Expand Down Expand Up @@ -286,7 +286,7 @@ public static class LocalizedVoteCount {
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
Expand Down Expand Up @@ -315,7 +315,7 @@ public static class RepliesTest {
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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public static class Kurzgesagt implements BaseListExtractorTest {
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();
Expand Down Expand Up @@ -84,7 +84,7 @@ public static class NotAvailable {

@BeforeAll
public static void setUp() throws IOException {
NewPipe.init(new DownloaderFactory().getDownloader(RESOURCE_PATH + "notAvailable/"));
NewPipe.init(DownloaderFactory.getDownloader(RESOURCE_PATH + "notAvailable/"));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public static class Trending implements BaseListExtractorTest {
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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public static class Mix {
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
Expand Down Expand Up @@ -131,7 +131,7 @@ public static class MixWithIndex {
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
Expand Down Expand Up @@ -212,7 +212,7 @@ public static class MyMix {
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
Expand Down Expand Up @@ -297,7 +297,7 @@ public static class Invalid {
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");
}

Expand Down Expand Up @@ -332,7 +332,7 @@ public static class ChannelMix {
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public static class NotAvailable {
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
Expand All @@ -67,7 +67,7 @@ public static class TimelessPopHits implements BasePlaylistExtractorTest {
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();
Expand Down Expand Up @@ -170,7 +170,7 @@ public static class HugePlaylist implements BasePlaylistExtractorTest {
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();
Expand Down Expand Up @@ -289,7 +289,7 @@ public static class LearningPlaylist implements BasePlaylistExtractorTest {
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();
Expand Down Expand Up @@ -392,7 +392,7 @@ public static class ContinuationsTests {
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
Expand Down
Loading