Skip to content

Commit d8f2031

Browse files
authored
Merge pull request #816 from Stypox/mock-only-extension
Add `@MockOnly` Junit 5 extension
2 parents cc2e4d7 + 73d1fd4 commit d8f2031

19 files changed

+108
-65
lines changed

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

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,17 @@
66

77
public class DownloaderFactory {
88

9-
public final static String RESOURCE_PATH = "src/test/resources/org/schabi/newpipe/extractor/";
9+
public static final String RESOURCE_PATH = "src/test/resources/org/schabi/newpipe/extractor/";
1010

11-
private final static DownloaderType DEFAULT_DOWNLOADER = DownloaderType.REAL;
11+
private static final DownloaderType DEFAULT_DOWNLOADER = DownloaderType.REAL;
12+
13+
public static DownloaderType getDownloaderType() {
14+
try {
15+
return DownloaderType.valueOf(System.getProperty("downloader"));
16+
} catch (final Exception e) {
17+
return DEFAULT_DOWNLOADER;
18+
}
19+
}
1220

1321
/**
1422
* <p>
@@ -28,14 +36,8 @@ public class DownloaderFactory {
2836
* @param path The path to the folder where mocks are saved/retrieved.
2937
* Preferably starting with {@link DownloaderFactory#RESOURCE_PATH}
3038
*/
31-
public Downloader getDownloader(String path) throws IOException {
32-
DownloaderType type;
33-
try {
34-
type = DownloaderType.valueOf(System.getProperty("downloader"));
35-
} catch (Exception e) {
36-
type = DEFAULT_DOWNLOADER;
37-
}
38-
39+
public static Downloader getDownloader(final String path) throws IOException {
40+
final DownloaderType type = getDownloaderType();
3941
switch (type) {
4042
case REAL:
4143
return DownloaderTestImpl.getInstance();
@@ -44,7 +46,7 @@ public Downloader getDownloader(String path) throws IOException {
4446
case RECORDING:
4547
return new RecordingDownloader(path);
4648
default:
47-
throw new UnsupportedOperationException("Unknown downloader type: " + type.toString());
49+
throw new UnsupportedOperationException("Unknown downloader type: " + type);
4850
}
4951
}
5052
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package org.schabi.newpipe.downloader;
2+
3+
import org.junit.jupiter.api.extension.ExtendWith;
4+
5+
import java.lang.annotation.Retention;
6+
import java.lang.annotation.RetentionPolicy;
7+
8+
/**
9+
* Use this to annotate tests methods/classes that should only be run when the downloader is of type
10+
* {@link DownloaderType#MOCK} or {@link DownloaderType#RECORDING}. This should be used when e.g. an
11+
* extractor returns different results each time because the underlying service web page does so. In
12+
* that case it makes sense to only run the tests with the mock downloader, since the real web page
13+
* is not reliable, but we still want to make sure that the code correctly interprets the stored and
14+
* mocked web page data.
15+
* @see MockOnlyCondition
16+
*/
17+
@Retention(RetentionPolicy.RUNTIME)
18+
@ExtendWith(MockOnlyCondition.class)
19+
public @interface MockOnly {
20+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package org.schabi.newpipe.downloader;
2+
3+
import org.junit.jupiter.api.extension.ConditionEvaluationResult;
4+
import org.junit.jupiter.api.extension.ExecutionCondition;
5+
import org.junit.jupiter.api.extension.ExtensionContext;
6+
7+
/**
8+
* @see MockOnly
9+
*/
10+
public class MockOnlyCondition implements ExecutionCondition {
11+
private static final String MOCK_ONLY_REASON = "Mock only";
12+
13+
@Override
14+
public ConditionEvaluationResult evaluateExecutionCondition(final ExtensionContext context) {
15+
if (DownloaderFactory.getDownloaderType() == DownloaderType.REAL) {
16+
return ConditionEvaluationResult.disabled(MOCK_ONLY_REASON);
17+
} else {
18+
return ConditionEvaluationResult.enabled(MOCK_ONLY_REASON);
19+
}
20+
}
21+
}

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public static class NotAvailable {
3535
public static void setUp() throws IOException {
3636
YoutubeParsingHelper.resetClientVersionAndKey();
3737
YoutubeParsingHelper.setNumberGenerator(new Random(1));
38-
NewPipe.init(new DownloaderFactory().getDownloader(RESOURCE_PATH + "notAvailable"));
38+
NewPipe.init(DownloaderFactory.getDownloader(RESOURCE_PATH + "notAvailable"));
3939
}
4040

4141
@Test
@@ -132,7 +132,7 @@ public static class NotSupported {
132132
public static void setUp() throws IOException {
133133
YoutubeParsingHelper.resetClientVersionAndKey();
134134
YoutubeParsingHelper.setNumberGenerator(new Random(1));
135-
NewPipe.init(new DownloaderFactory().getDownloader(RESOURCE_PATH + "notSupported"));
135+
NewPipe.init(DownloaderFactory.getDownloader(RESOURCE_PATH + "notSupported"));
136136
}
137137

138138
@Test
@@ -151,7 +151,7 @@ public static class Gronkh implements BaseChannelExtractorTest {
151151
public static void setUp() throws Exception {
152152
YoutubeParsingHelper.resetClientVersionAndKey();
153153
YoutubeParsingHelper.setNumberGenerator(new Random(1));
154-
NewPipe.init(new DownloaderFactory().getDownloader(RESOURCE_PATH + "gronkh"));
154+
NewPipe.init(DownloaderFactory.getDownloader(RESOURCE_PATH + "gronkh"));
155155
extractor = (YoutubeChannelExtractor) YouTube
156156
.getChannelExtractor("http://www.youtube.com/user/Gronkh");
157157
extractor.fetchPage();
@@ -248,7 +248,7 @@ public static class VSauce implements BaseChannelExtractorTest {
248248
public static void setUp() throws Exception {
249249
YoutubeParsingHelper.resetClientVersionAndKey();
250250
YoutubeParsingHelper.setNumberGenerator(new Random(1));
251-
NewPipe.init(new DownloaderFactory().getDownloader(RESOURCE_PATH + "VSauce"));
251+
NewPipe.init(DownloaderFactory.getDownloader(RESOURCE_PATH + "VSauce"));
252252
extractor = (YoutubeChannelExtractor) YouTube
253253
.getChannelExtractor("https://www.youtube.com/user/Vsauce");
254254
extractor.fetchPage();
@@ -344,7 +344,7 @@ public static class Kurzgesagt implements BaseChannelExtractorTest {
344344
public static void setUp() throws Exception {
345345
YoutubeParsingHelper.resetClientVersionAndKey();
346346
YoutubeParsingHelper.setNumberGenerator(new Random(1));
347-
NewPipe.init(new DownloaderFactory().getDownloader(RESOURCE_PATH + "kurzgesagt"));
347+
NewPipe.init(DownloaderFactory.getDownloader(RESOURCE_PATH + "kurzgesagt"));
348348
extractor = (YoutubeChannelExtractor) YouTube
349349
.getChannelExtractor("https://www.youtube.com/channel/UCsXVk37bltHxD1rDPwtNM8Q");
350350
extractor.fetchPage();
@@ -461,7 +461,7 @@ public static class CaptainDisillusion implements BaseChannelExtractorTest {
461461
public static void setUp() throws Exception {
462462
YoutubeParsingHelper.resetClientVersionAndKey();
463463
YoutubeParsingHelper.setNumberGenerator(new Random(1));
464-
NewPipe.init(new DownloaderFactory().getDownloader(RESOURCE_PATH + "captainDisillusion"));
464+
NewPipe.init(DownloaderFactory.getDownloader(RESOURCE_PATH + "captainDisillusion"));
465465
extractor = (YoutubeChannelExtractor) YouTube
466466
.getChannelExtractor("https://www.youtube.com/user/CaptainDisillusion/videos");
467467
extractor.fetchPage();
@@ -556,7 +556,7 @@ public static class RandomChannel implements BaseChannelExtractorTest {
556556
public static void setUp() throws Exception {
557557
YoutubeParsingHelper.resetClientVersionAndKey();
558558
YoutubeParsingHelper.setNumberGenerator(new Random(1));
559-
NewPipe.init(new DownloaderFactory().getDownloader(RESOURCE_PATH + "random"));
559+
NewPipe.init(DownloaderFactory.getDownloader(RESOURCE_PATH + "random"));
560560
extractor = (YoutubeChannelExtractor) YouTube
561561
.getChannelExtractor("https://www.youtube.com/channel/UCUaQMQS9lY5lit3vurpXQ6w");
562562
extractor.fetchPage();

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public class YoutubeChannelLocalizationTest {
3232
public void testAllSupportedLocalizations() throws Exception {
3333
YoutubeParsingHelper.resetClientVersionAndKey();
3434
YoutubeParsingHelper.setNumberGenerator(new Random(1));
35-
NewPipe.init(new DownloaderFactory().getDownloader(RESOURCE_PATH + "localization"));
35+
NewPipe.init(DownloaderFactory.getDownloader(RESOURCE_PATH + "localization"));
3636

3737
testLocalizationsFor("https://www.youtube.com/user/NBCNews");
3838
testLocalizationsFor("https://www.youtube.com/channel/UCcmpeVbSSQlZRvHfdC-CRwg/videos");

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public static class Thomas {
4040
public static void setUp() throws Exception {
4141
YoutubeParsingHelper.resetClientVersionAndKey();
4242
YoutubeParsingHelper.setNumberGenerator(new Random(1));
43-
NewPipe.init(new DownloaderFactory().getDownloader(RESOURCE_PATH + "thomas"));
43+
NewPipe.init(DownloaderFactory.getDownloader(RESOURCE_PATH + "thomas"));
4444
extractor = (YoutubeCommentsExtractor) YouTube
4545
.getCommentsExtractor(url);
4646
extractor.fetchPage();
@@ -129,7 +129,7 @@ public static class EmptyComment {
129129
public static void setUp() throws Exception {
130130
YoutubeParsingHelper.resetClientVersionAndKey();
131131
YoutubeParsingHelper.setNumberGenerator(new Random(1));
132-
NewPipe.init(new DownloaderFactory().getDownloader(RESOURCE_PATH + "empty"));
132+
NewPipe.init(DownloaderFactory.getDownloader(RESOURCE_PATH + "empty"));
133133
extractor = (YoutubeCommentsExtractor) YouTube
134134
.getCommentsExtractor(url);
135135
extractor.fetchPage();
@@ -169,7 +169,7 @@ public static class HeartedByCreator {
169169
public static void setUp() throws Exception {
170170
YoutubeParsingHelper.resetClientVersionAndKey();
171171
YoutubeParsingHelper.setNumberGenerator(new Random(1));
172-
NewPipe.init(new DownloaderFactory().getDownloader(RESOURCE_PATH + "hearted"));
172+
NewPipe.init(DownloaderFactory.getDownloader(RESOURCE_PATH + "hearted"));
173173
extractor = (YoutubeCommentsExtractor) YouTube
174174
.getCommentsExtractor(url);
175175
extractor.fetchPage();
@@ -212,7 +212,7 @@ public static class Pinned {
212212
public static void setUp() throws Exception {
213213
YoutubeParsingHelper.resetClientVersionAndKey();
214214
YoutubeParsingHelper.setNumberGenerator(new Random(1));
215-
NewPipe.init(new DownloaderFactory().getDownloader(RESOURCE_PATH + "pinned"));
215+
NewPipe.init(DownloaderFactory.getDownloader(RESOURCE_PATH + "pinned"));
216216
extractor = (YoutubeCommentsExtractor) YouTube
217217
.getCommentsExtractor(url);
218218
extractor.fetchPage();
@@ -254,7 +254,7 @@ public static class LikesVotes {
254254
public static void setUp() throws Exception {
255255
YoutubeParsingHelper.resetClientVersionAndKey();
256256
YoutubeParsingHelper.setNumberGenerator(new Random(1));
257-
NewPipe.init(new DownloaderFactory().getDownloader(RESOURCE_PATH + "likes"));
257+
NewPipe.init(DownloaderFactory.getDownloader(RESOURCE_PATH + "likes"));
258258
extractor = (YoutubeCommentsExtractor) YouTube
259259
.getCommentsExtractor(url);
260260
extractor.fetchPage();
@@ -286,7 +286,7 @@ public static class LocalizedVoteCount {
286286
public static void setUp() throws Exception {
287287
YoutubeParsingHelper.resetClientVersionAndKey();
288288
YoutubeParsingHelper.setNumberGenerator(new Random(1));
289-
NewPipe.init(new DownloaderFactory().getDownloader(RESOURCE_PATH + "localized_vote_count"));
289+
NewPipe.init(DownloaderFactory.getDownloader(RESOURCE_PATH + "localized_vote_count"));
290290
extractor = (YoutubeCommentsExtractor) YouTube
291291
.getCommentsExtractor(url);
292292
// Force non english local here
@@ -315,7 +315,7 @@ public static class RepliesTest {
315315
public static void setUp() throws Exception {
316316
YoutubeParsingHelper.resetClientVersionAndKey();
317317
YoutubeParsingHelper.setNumberGenerator(new Random(1));
318-
NewPipe.init(new DownloaderFactory().getDownloader(RESOURCE_PATH + "replies"));
318+
NewPipe.init(DownloaderFactory.getDownloader(RESOURCE_PATH + "replies"));
319319
extractor = (YoutubeCommentsExtractor) YouTube
320320
.getCommentsExtractor(url);
321321
extractor.fetchPage();

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public static class Kurzgesagt implements BaseListExtractorTest {
3030
public static void setUp() throws Exception {
3131
YoutubeParsingHelper.resetClientVersionAndKey();
3232
YoutubeParsingHelper.setNumberGenerator(new Random(1));
33-
NewPipe.init(new DownloaderFactory().getDownloader(RESOURCE_PATH));
33+
NewPipe.init(DownloaderFactory.getDownloader(RESOURCE_PATH));
3434
extractor = (YoutubeFeedExtractor) YouTube
3535
.getFeedExtractor("https://www.youtube.com/user/Kurzgesagt");
3636
extractor.fetchPage();
@@ -84,7 +84,7 @@ public static class NotAvailable {
8484

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

9090
@Test

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public static class Trending implements BaseListExtractorTest {
2626
public static void setUp() throws Exception {
2727
YoutubeParsingHelper.resetClientVersionAndKey();
2828
YoutubeParsingHelper.setNumberGenerator(new Random(1));
29-
NewPipe.init(new DownloaderFactory().getDownloader(RESOURCE_PATH + "trending"));
29+
NewPipe.init(DownloaderFactory.getDownloader(RESOURCE_PATH + "trending"));
3030
extractor = (YoutubeTrendingExtractor) YouTube.getKioskList().getDefaultKioskExtractor();
3131
extractor.fetchPage();
3232
}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public static class Mix {
4141
public static void setUp() throws Exception {
4242
YoutubeParsingHelper.resetClientVersionAndKey();
4343
YoutubeParsingHelper.setNumberGenerator(new Random(1));
44-
NewPipe.init(new DownloaderFactory().getDownloader(RESOURCE_PATH + "mix"));
44+
NewPipe.init(DownloaderFactory.getDownloader(RESOURCE_PATH + "mix"));
4545
dummyCookie.put(YoutubeMixPlaylistExtractor.COOKIE_NAME, "whatever");
4646
extractor = (YoutubeMixPlaylistExtractor) YouTube
4747
.getPlaylistExtractor("https://www.youtube.com/watch?v=" + VIDEO_ID
@@ -131,7 +131,7 @@ public static class MixWithIndex {
131131
public static void setUp() throws Exception {
132132
YoutubeParsingHelper.resetClientVersionAndKey();
133133
YoutubeParsingHelper.setNumberGenerator(new Random(1));
134-
NewPipe.init(new DownloaderFactory().getDownloader(RESOURCE_PATH + "mixWithIndex"));
134+
NewPipe.init(DownloaderFactory.getDownloader(RESOURCE_PATH + "mixWithIndex"));
135135
dummyCookie.put(YoutubeMixPlaylistExtractor.COOKIE_NAME, "whatever");
136136
extractor = (YoutubeMixPlaylistExtractor) YouTube
137137
.getPlaylistExtractor("https://www.youtube.com/watch?v=" + VIDEO_ID_NUMBER_4
@@ -212,7 +212,7 @@ public static class MyMix {
212212
public static void setUp() throws Exception {
213213
YoutubeParsingHelper.resetClientVersionAndKey();
214214
YoutubeParsingHelper.setNumberGenerator(new Random(1));
215-
NewPipe.init(new DownloaderFactory().getDownloader(RESOURCE_PATH + "myMix"));
215+
NewPipe.init(DownloaderFactory.getDownloader(RESOURCE_PATH + "myMix"));
216216
dummyCookie.put(YoutubeMixPlaylistExtractor.COOKIE_NAME, "whatever");
217217
extractor = (YoutubeMixPlaylistExtractor) YouTube
218218
.getPlaylistExtractor("https://www.youtube.com/watch?v=" + VIDEO_ID
@@ -297,7 +297,7 @@ public static class Invalid {
297297
public static void setUp() throws IOException {
298298
YoutubeParsingHelper.resetClientVersionAndKey();
299299
YoutubeParsingHelper.setNumberGenerator(new Random(1));
300-
NewPipe.init(new DownloaderFactory().getDownloader(RESOURCE_PATH + "invalid"));
300+
NewPipe.init(DownloaderFactory.getDownloader(RESOURCE_PATH + "invalid"));
301301
dummyCookie.put(YoutubeMixPlaylistExtractor.COOKIE_NAME, "whatever");
302302
}
303303

@@ -332,7 +332,7 @@ public static class ChannelMix {
332332
public static void setUp() throws Exception {
333333
YoutubeParsingHelper.resetClientVersionAndKey();
334334
YoutubeParsingHelper.setNumberGenerator(new Random(1));
335-
NewPipe.init(new DownloaderFactory().getDownloader(RESOURCE_PATH + "channelMix"));
335+
NewPipe.init(DownloaderFactory.getDownloader(RESOURCE_PATH + "channelMix"));
336336
dummyCookie.put(YoutubeMixPlaylistExtractor.COOKIE_NAME, "whatever");
337337
extractor = (YoutubeMixPlaylistExtractor) YouTube
338338
.getPlaylistExtractor("https://www.youtube.com/watch?v=" + VIDEO_ID_OF_CHANNEL

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
@@ -21,7 +21,7 @@ public class YoutubeParsingHelperTest {
2121
public static void setUp() throws IOException {
2222
YoutubeParsingHelper.resetClientVersionAndKey();
2323
YoutubeParsingHelper.setNumberGenerator(new Random(1));
24-
NewPipe.init(new DownloaderFactory().getDownloader(RESOURCE_PATH + "youtubeParsingHelper"));
24+
NewPipe.init(DownloaderFactory.getDownloader(RESOURCE_PATH + "youtubeParsingHelper"));
2525
}
2626

2727
@Test

0 commit comments

Comments
 (0)