Skip to content

Commit d38373d

Browse files
committed
Make ALL tests mockable
Also: * Removed unused mock only * Cache downloader type
1 parent d4f551c commit d38373d

94 files changed

Lines changed: 1926 additions & 2421 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeStreamInfoItemLockupExtractor.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import org.schabi.newpipe.extractor.utils.JsonUtils;
1818
import org.schabi.newpipe.extractor.utils.Utils;
1919

20+
import java.util.ArrayList;
2021
import java.util.List;
2122
import java.util.Optional;
2223
import java.util.stream.Collectors;

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

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,56 @@
22

33
import org.schabi.newpipe.extractor.downloader.Downloader;
44

5-
import java.io.IOException;
5+
import java.util.Locale;
66

77
public class DownloaderFactory {
88

9-
public static final String RESOURCE_PATH = "src/test/resources/org/schabi/newpipe/extractor/";
10-
119
private static final DownloaderType DEFAULT_DOWNLOADER = DownloaderType.REAL;
1210

13-
public static DownloaderType getDownloaderType() {
11+
private static DownloaderType cachedDownloaderType;
12+
13+
private static DownloaderType getDownloaderType() {
14+
if (cachedDownloaderType == null) {
15+
cachedDownloaderType = determineDownloaderType();
16+
}
17+
18+
return cachedDownloaderType;
19+
}
20+
21+
private static DownloaderType determineDownloaderType() {
22+
String propValue = System.getProperty("downloader");
23+
if (propValue == null) {
24+
return DEFAULT_DOWNLOADER;
25+
}
26+
propValue = propValue.toUpperCase();
27+
// Use shortcut because RECORDING is quite wrong
28+
if (propValue.equals("REC")) {
29+
return DownloaderType.RECORDING;
30+
}
1431
try {
15-
return DownloaderType.valueOf(System.getProperty("downloader"));
32+
return DownloaderType.valueOf(propValue);
1633
} catch (final Exception e) {
1734
return DEFAULT_DOWNLOADER;
1835
}
1936
}
2037

38+
public static Downloader getDownloader(final Class<?> clazz) {
39+
return getDownloader(clazz, null);
40+
}
41+
42+
public static Downloader getDownloader(final Class<?> clazz, final String specificUseCase) {
43+
String baseName = clazz.getName();
44+
if (specificUseCase != null) {
45+
baseName += "." + specificUseCase;
46+
}
47+
return getDownloader("src/test/resources/mocks/v1/"
48+
+ baseName
49+
.toLowerCase(Locale.ENGLISH)
50+
.replace('$', '.')
51+
.replace("test", "")
52+
.replace('.', '/'));
53+
}
54+
2155
/**
2256
* <p>
2357
* Returns a implementation of a {@link Downloader}.
@@ -34,9 +68,8 @@ public static DownloaderType getDownloaderType() {
3468
* </p>
3569
*
3670
* @param path The path to the folder where mocks are saved/retrieved.
37-
* Preferably starting with {@link DownloaderFactory#RESOURCE_PATH}
3871
*/
39-
public static Downloader getDownloader(final String path) {
72+
protected static Downloader getDownloader(final String path) {
4073
final DownloaderType type = getDownloaderType();
4174
switch (type) {
4275
case REAL:

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

Lines changed: 0 additions & 25 deletions
This file was deleted.

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

Lines changed: 0 additions & 21 deletions
This file was deleted.

extractor/src/test/java/org/schabi/newpipe/downloader/ratelimiting/RateLimitedClientWrapper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public class RateLimitedClientWrapper {
2323
Map.entry(host -> host.endsWith("youtube.com"),
2424
RateLimiter.create(1.6, Duration.ofSeconds(1))),
2525
Map.entry(host -> host.endsWith("bandcamp.com"),
26-
RateLimiter.create(2.5, Duration.ofSeconds(1)))
26+
RateLimiter.create(1.6, Duration.ofSeconds(1)))
2727
);
2828

2929
private final OkHttpClient client;
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package org.schabi.newpipe.extractor;
2+
3+
import org.junit.jupiter.api.BeforeAll;
4+
import org.junit.jupiter.api.TestInstance;
5+
import org.schabi.newpipe.downloader.DownloaderFactory;
6+
7+
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
8+
public interface InitNewPipeTest {
9+
10+
@BeforeAll
11+
default void setUp() throws Exception {
12+
initNewPipe(this.getClass());
13+
}
14+
15+
static void initNewPipe(final Class<?> clazz) {
16+
NewPipe.init(DownloaderFactory.getDownloader(clazz));
17+
}
18+
19+
static void initNewPipe(final Class<?> clazz, final String specificUseCase) {
20+
NewPipe.init(DownloaderFactory.getDownloader(clazz, specificUseCase));
21+
}
22+
23+
/**
24+
* Ensures that NewPipe is not initialized.
25+
* This can be used to ensure that a class works standalone and does e.g. no network requests.
26+
*/
27+
static void initEmpty() {
28+
NewPipe.init(null, null, null);
29+
}
30+
}

extractor/src/test/java/org/schabi/newpipe/extractor/services/BaseExtractorTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package org.schabi.newpipe.extractor.services;
22

33
import org.junit.jupiter.api.Test;
4+
import org.junit.jupiter.api.TestInstance;
45

6+
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
57
public interface BaseExtractorTest {
68
@Test
79
void testServiceId() throws Exception;

extractor/src/test/java/org/schabi/newpipe/extractor/services/DefaultExtractorTest.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
package org.schabi.newpipe.extractor.services;
22

3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.schabi.newpipe.extractor.ExtractorAsserts.assertIsSecureUrl;
5+
36
import org.junit.jupiter.api.Test;
47
import org.schabi.newpipe.extractor.Extractor;
58
import org.schabi.newpipe.extractor.ExtractorAsserts;
69
import org.schabi.newpipe.extractor.StreamingService;
710

8-
import static org.junit.jupiter.api.Assertions.*;
9-
import static org.schabi.newpipe.extractor.ExtractorAsserts.assertIsSecureUrl;
10-
11-
public abstract class DefaultExtractorTest<T extends Extractor> implements BaseExtractorTest {
12-
public abstract T extractor() throws Exception;
11+
public abstract class DefaultExtractorTest<T extends Extractor>
12+
extends DefaultSimpleExtractorTest<T>
13+
implements BaseExtractorTest {
1314

1415
public abstract StreamingService expectedService() throws Exception;
1516
public abstract String expectedName() throws Exception;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package org.schabi.newpipe.extractor.services;
2+
3+
import org.schabi.newpipe.extractor.Extractor;
4+
5+
public abstract class DefaultSimpleExtractorTest<T extends Extractor> extends DefaultSimpleUntypedExtractorTest<T> {
6+
7+
@Override
8+
protected void fetchExtractor(final T extractor) throws Exception {
9+
extractor.fetchPage();
10+
}
11+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package org.schabi.newpipe.extractor.services;
2+
3+
import org.schabi.newpipe.extractor.InitNewPipeTest;
4+
5+
public abstract class DefaultSimpleUntypedExtractorTest<T> implements InitNewPipeTest {
6+
7+
private T extractor;
8+
9+
protected void initExtractor() throws Exception {
10+
extractor = createExtractor();
11+
fetchExtractor(extractor);
12+
}
13+
14+
protected abstract T createExtractor() throws Exception;
15+
16+
protected void fetchExtractor(final T extractor) throws Exception {
17+
}
18+
19+
protected T extractor() {
20+
if (extractor == null) {
21+
try {
22+
initExtractor();
23+
} catch (final Exception ex) {
24+
if(ex instanceof RuntimeException) {
25+
throw (RuntimeException)ex;
26+
}
27+
throw new RuntimeException("Failed to init extractor", ex);
28+
}
29+
}
30+
return extractor;
31+
}
32+
}

0 commit comments

Comments
 (0)