From e6e8e39def57f71b635c2a47d078125adf8ea6f5 Mon Sep 17 00:00:00 2001 From: XiangRongLin <41164160+XiangRongLin@users.noreply.github.com> Date: Tue, 15 Dec 2020 15:19:01 +0100 Subject: [PATCH] Add DownloaderFactory to return a specific downloader based on 2 variables. If the system property 'downloader' is set that use that specific downloader. This is used from gradle by appending `-Ddownloader=ABCD to the command. ABCD is one of DownloaderType. The other variable is the static property `DEFAULT_DOWNLOADER` in DownloaderFactory, which can be easily changed as needed inside the IDE according to development needs`. Normal workflow would be to first use the recording downloader and afterwards only use mocks, if the requests are always staying the same. --- .../newpipe/downloader/DownloaderFactory.java | 30 +++++++++++++++++++ .../newpipe/downloader/DownloaderType.java | 5 ++++ 2 files changed, 35 insertions(+) create mode 100644 extractor/src/test/java/org/schabi/newpipe/downloader/DownloaderFactory.java create mode 100644 extractor/src/test/java/org/schabi/newpipe/downloader/DownloaderType.java diff --git a/extractor/src/test/java/org/schabi/newpipe/downloader/DownloaderFactory.java b/extractor/src/test/java/org/schabi/newpipe/downloader/DownloaderFactory.java new file mode 100644 index 000000000..96cbc6621 --- /dev/null +++ b/extractor/src/test/java/org/schabi/newpipe/downloader/DownloaderFactory.java @@ -0,0 +1,30 @@ +package org.schabi.newpipe.downloader; + +import org.schabi.newpipe.extractor.downloader.Downloader; + +import java.io.IOException; + +public class DownloaderFactory { + + private final static DownloaderType DEFAULT_DOWNLOADER = DownloaderType.REAL; + + public Downloader getDownloader(String path) throws IOException { + DownloaderType type; + try { + type = DownloaderType.valueOf(System.getProperty("downloader")); + } catch (Exception e) { + type = DEFAULT_DOWNLOADER; + } + + switch (type) { + case REAL: + return DownloaderTestImpl.getInstance(); + case MOCK: + return new MockDownloader(path); + case RECORDING: + return new RecordingDownloader(path); + default: + throw new UnsupportedOperationException("Unknown downloader type: " + type.toString()); + } + } +} diff --git a/extractor/src/test/java/org/schabi/newpipe/downloader/DownloaderType.java b/extractor/src/test/java/org/schabi/newpipe/downloader/DownloaderType.java new file mode 100644 index 000000000..090a5067c --- /dev/null +++ b/extractor/src/test/java/org/schabi/newpipe/downloader/DownloaderType.java @@ -0,0 +1,5 @@ +package org.schabi.newpipe.downloader; + +public enum DownloaderType { + REAL, MOCK, RECORDING +} \ No newline at end of file