From 73d1fd472fdf17204ae86cb18b6e01167e4339ff Mon Sep 17 00:00:00 2001 From: Stypox Date: Wed, 16 Mar 2022 17:24:55 +0100 Subject: [PATCH] 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 00000000..d6ca1712 --- /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 00000000..82f57fa6 --- /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); + } + } +}