Skip to content

Commit 59e0aeb

Browse files
authored
Merge pull request #768 from litetex/junit-5
JUnit 5
2 parents 65df39b + a579ef2 commit 59e0aeb

File tree

116 files changed

+3172
-1220
lines changed

Some content is hidden

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

116 files changed

+3172
-1220
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ allprojects {
2929
ext {
3030
nanojsonVersion = "1d9e1aea9049fc9f85e68b43ba39fe7be1c1f751"
3131
spotbugsVersion = "4.5.2"
32-
junitVersion = "4.13.2"
32+
junitVersion = "5.8.2"
3333
}
3434
}
3535

extractor/build.gradle

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ test {
33
if (System.properties.containsKey('downloader')) {
44
systemProperty('downloader', System.getProperty('downloader'))
55
}
6+
useJUnitPlatform()
67
}
78

89
dependencies {
@@ -14,7 +15,11 @@ dependencies {
1415
implementation "com.github.spotbugs:spotbugs-annotations:$spotbugsVersion"
1516
implementation 'org.nibor.autolink:autolink:0.10.0'
1617

17-
testImplementation "junit:junit:$junitVersion"
18+
testImplementation platform("org.junit:junit-bom:$junitVersion")
19+
testImplementation 'org.junit.jupiter:junit-jupiter-api'
20+
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine'
21+
testImplementation 'org.junit.jupiter:junit-jupiter-params'
22+
1823
testImplementation "com.squareup.okhttp3:okhttp:3.12.13"
1924
testImplementation 'com.google.code.gson:gson:2.8.9'
2025
}

extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/SoundcloudParsingHelper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141

4242
public class SoundcloudParsingHelper {
4343
static final String HARDCODED_CLIENT_ID =
44-
"1NKODbzHzEpoowFHxTAmS7oB08DObPuK"; // Updated on 08/12/21
44+
"JpcTFmpEz9lMPDrM6TDAC9izag7Be06D"; // Updated on 2022-01-07
4545
private static String clientId;
4646
public static final String SOUNDCLOUD_API_V2_URL = "https://api-v2.soundcloud.com/";
4747

extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/extractors/SoundcloudSearchExtractor.java

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,14 @@
2020
import java.net.URL;
2121
import java.util.Collections;
2222
import java.util.List;
23+
import java.util.function.IntUnaryOperator;
2324

2425
import static org.schabi.newpipe.extractor.services.soundcloud.linkHandler.SoundcloudSearchQueryHandlerFactory.ITEMS_PER_PAGE;
2526
import static org.schabi.newpipe.extractor.utils.Utils.EMPTY_STRING;
2627
import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;
2728

2829
public class SoundcloudSearchExtractor extends SearchExtractor {
29-
private JsonArray searchCollection;
30+
private JsonArray initialSearchCollection;
3031

3132
public SoundcloudSearchExtractor(final StreamingService service,
3233
final SearchQueryHandler linkHandler) {
@@ -53,8 +54,9 @@ public List<MetaInfo> getMetaInfo() {
5354
@Nonnull
5455
@Override
5556
public InfoItemsPage<InfoItem> getInitialPage() throws IOException, ExtractionException {
56-
return new InfoItemsPage<>(collectItems(searchCollection), getNextPageFromCurrentUrl(
57-
getUrl()));
57+
return new InfoItemsPage<>(
58+
collectItems(initialSearchCollection),
59+
getNextPageFromCurrentUrl(getUrl(), currentOffset -> ITEMS_PER_PAGE));
5860
}
5961

6062
@Override
@@ -65,6 +67,7 @@ public InfoItemsPage<InfoItem> getPage(final Page page) throws IOException,
6567
}
6668

6769
final Downloader dl = getDownloader();
70+
final JsonArray searchCollection;
6871
try {
6972
final String response = dl.get(page.getUrl(), getExtractorLocalization())
7073
.responseBody();
@@ -73,8 +76,9 @@ public InfoItemsPage<InfoItem> getPage(final Page page) throws IOException,
7376
throw new ParsingException("Could not parse json response", e);
7477
}
7578

76-
return new InfoItemsPage<>(collectItems(searchCollection), getNextPageFromCurrentUrl(page
77-
.getUrl()));
79+
return new InfoItemsPage<>(
80+
collectItems(searchCollection),
81+
getNextPageFromCurrentUrl(page.getUrl(), currentOffset -> currentOffset + ITEMS_PER_PAGE));
7882
}
7983

8084
@Override
@@ -84,12 +88,12 @@ public void onFetchPage(@Nonnull final Downloader downloader) throws IOException
8488
final String url = getUrl();
8589
try {
8690
final String response = dl.get(url, getExtractorLocalization()).responseBody();
87-
searchCollection = JsonParser.object().from(response).getArray("collection");
91+
initialSearchCollection = JsonParser.object().from(response).getArray("collection");
8892
} catch (final JsonParserException e) {
8993
throw new ParsingException("Could not parse json response", e);
9094
}
9195

92-
if (searchCollection.isEmpty()) {
96+
if (initialSearchCollection.isEmpty()) {
9397
throw new SearchExtractor.NothingFoundException("Nothing found");
9498
}
9599
}
@@ -118,12 +122,14 @@ private InfoItemsCollector<InfoItem, InfoItemExtractor> collectItems(
118122
return collector;
119123
}
120124

121-
private Page getNextPageFromCurrentUrl(final String currentUrl)
125+
private Page getNextPageFromCurrentUrl(final String currentUrl, final IntUnaryOperator newPageOffsetCalculator)
122126
throws MalformedURLException, UnsupportedEncodingException {
123-
final int pageOffset = Integer.parseInt(
124-
Parser.compatParseMap(new URL(currentUrl).getQuery()).get("offset"));
127+
final int currentPageOffset = Integer.parseInt(
128+
Parser.compatParseMap(new URL(currentUrl).getQuery()).get("offset"));
125129

126-
return new Page(currentUrl.replace("&offset=" + pageOffset, "&offset="
127-
+ (pageOffset + ITEMS_PER_PAGE)));
130+
return new Page(
131+
currentUrl.replace(
132+
"&offset=" + currentPageOffset,
133+
"&offset=" + newPageOffsetCalculator.applyAsInt(currentPageOffset)));
128134
}
129135
}

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

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

extractor/src/test/java/org/schabi/newpipe/MockOnlyRule.java

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

extractor/src/test/java/org/schabi/newpipe/extractor/ExtractorAsserts.java

Lines changed: 63 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,16 @@
44
import java.net.URL;
55
import java.util.ArrayList;
66
import java.util.Collections;
7-
import java.util.Comparator;
87
import java.util.List;
98

109
import javax.annotation.Nonnull;
1110
import javax.annotation.Nullable;
1211

13-
import static org.junit.Assert.assertArrayEquals;
14-
import static org.junit.Assert.assertEquals;
15-
import static org.junit.Assert.assertFalse;
16-
import static org.junit.Assert.assertNotNull;
17-
import static org.junit.Assert.assertNull;
18-
import static org.junit.Assert.assertTrue;
12+
import static org.junit.jupiter.api.Assertions.assertEquals;
13+
import static org.junit.jupiter.api.Assertions.assertFalse;
14+
import static org.junit.jupiter.api.Assertions.assertNotNull;
15+
import static org.junit.jupiter.api.Assertions.assertNull;
16+
import static org.junit.jupiter.api.Assertions.assertTrue;
1917

2018
public class ExtractorAsserts {
2119
public static void assertEmptyErrors(String message, List<Throwable> errors) {
@@ -44,7 +42,7 @@ public static void assertIsValidUrl(String url) {
4442

4543
public static void assertIsSecureUrl(String urlToCheck) {
4644
URL url = urlFromString(urlToCheck);
47-
assertEquals("Protocol of URL is not secure", "https", url.getProtocol());
45+
assertEquals("https", url.getProtocol(), "Protocol of URL is not secure");
4846
}
4947

5048
public static void assertNotEmpty(String stringToCheck) {
@@ -53,7 +51,7 @@ public static void assertNotEmpty(String stringToCheck) {
5351

5452
public static void assertNotEmpty(@Nullable String message, String stringToCheck) {
5553
assertNotNull(message, stringToCheck);
56-
assertFalse(message, stringToCheck.isEmpty());
54+
assertFalse(stringToCheck.isEmpty(), message);
5755
}
5856

5957
public static void assertEmpty(String stringToCheck) {
@@ -62,12 +60,56 @@ public static void assertEmpty(String stringToCheck) {
6260

6361
public static void assertEmpty(@Nullable String message, String stringToCheck) {
6462
if (stringToCheck != null) {
65-
assertTrue(message, stringToCheck.isEmpty());
63+
assertTrue(stringToCheck.isEmpty(), message);
6664
}
6765
}
6866

69-
public static void assertAtLeast(long expected, long actual) {
70-
assertTrue(actual + " is not at least " + expected, actual >= expected);
67+
public static void assertGreater(final long expected, final long actual) {
68+
assertGreater(expected, actual, actual + " is not > " + expected);
69+
}
70+
71+
public static void assertGreater(
72+
final long expected,
73+
final long actual,
74+
final String message
75+
) {
76+
assertTrue(actual > expected, message);
77+
}
78+
79+
public static void assertGreaterOrEqual(final long expected, final long actual) {
80+
assertGreaterOrEqual(expected, actual, actual + " is not >= " + expected);
81+
}
82+
83+
public static void assertGreaterOrEqual(
84+
final long expected,
85+
final long actual,
86+
final String message
87+
) {
88+
assertTrue(actual >= expected, message);
89+
}
90+
91+
public static void assertLess(final long expected, final long actual) {
92+
assertLess(expected, actual, actual + " is not < " + expected);
93+
}
94+
95+
public static void assertLess(
96+
final long expected,
97+
final long actual,
98+
final String message
99+
) {
100+
assertTrue(actual < expected, message);
101+
}
102+
103+
public static void assertLessOrEqual(final long expected, final long actual) {
104+
assertLessOrEqual(expected, actual, actual + " is not <= " + expected);
105+
}
106+
107+
public static void assertLessOrEqual(
108+
final long expected,
109+
final long actual,
110+
final String message
111+
) {
112+
assertTrue(actual <= expected, message);
71113
}
72114

73115
// this assumes that sorting a and b in-place is not an issue, so it's only intended for tests
@@ -85,4 +127,13 @@ public static void assertEqualsOrderIndependent(final List<String> expected,
85127
// using new ArrayList<> to make sure the type is the same
86128
assertEquals(new ArrayList<>(expected), new ArrayList<>(actual));
87129
}
130+
131+
public static void assertContains(
132+
final String shouldBeContained,
133+
final String container) {
134+
assertNotNull(shouldBeContained, "shouldBeContained is null");
135+
assertNotNull(container, "container is null");
136+
assertTrue(container.contains(shouldBeContained),
137+
"'" + shouldBeContained + "' should be contained inside '" + container +"'");
138+
}
88139
}

extractor/src/test/java/org/schabi/newpipe/extractor/NewPipeTest.java

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

3-
import org.junit.Test;
3+
import org.junit.jupiter.api.Test;
44

55
import java.util.HashSet;
66

7-
import static org.junit.Assert.*;
7+
import static org.junit.jupiter.api.Assertions.*;
88
import static org.schabi.newpipe.extractor.NewPipe.getServiceByUrl;
99
import static org.schabi.newpipe.extractor.ServiceList.SoundCloud;
1010
import static org.schabi.newpipe.extractor.ServiceList.YouTube;
@@ -19,9 +19,11 @@ public void getAllServicesTest() throws Exception {
1919
public void testAllServicesHaveDifferentId() throws Exception {
2020
HashSet<Integer> servicesId = new HashSet<>();
2121
for (StreamingService streamingService : NewPipe.getServices()) {
22-
String errorMsg = "There are services with the same id = " + streamingService.getServiceId() + " (current service > " + streamingService.getServiceInfo().getName() + ")";
22+
final String errorMsg =
23+
"There are services with the same id = " + streamingService.getServiceId()
24+
+ " (current service > " + streamingService.getServiceInfo().getName() + ")";
2325

24-
assertTrue(errorMsg, servicesId.add(streamingService.getServiceId()));
26+
assertTrue(servicesId.add(streamingService.getServiceId()), errorMsg);
2527
}
2628
}
2729

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

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

3-
import org.junit.Test;
3+
import org.junit.jupiter.api.Test;
44
import org.schabi.newpipe.extractor.Extractor;
5+
import org.schabi.newpipe.extractor.ExtractorAsserts;
56
import org.schabi.newpipe.extractor.StreamingService;
67

7-
import static org.hamcrest.CoreMatchers.*;
8-
import static org.junit.Assert.*;
8+
import static org.junit.jupiter.api.Assertions.*;
99
import static org.schabi.newpipe.extractor.ExtractorAsserts.assertIsSecureUrl;
1010

1111
public abstract class DefaultExtractorTest<T extends Extractor> implements BaseExtractorTest {
@@ -40,14 +40,14 @@ public void testId() throws Exception {
4040
public void testUrl() throws Exception {
4141
final String url = extractor().getUrl();
4242
assertIsSecureUrl(url);
43-
assertThat(url, containsString(expectedUrlContains()));
43+
ExtractorAsserts.assertContains(expectedUrlContains(), url);
4444
}
4545

4646
@Test
4747
@Override
4848
public void testOriginalUrl() throws Exception {
4949
final String originalUrl = extractor().getOriginalUrl();
5050
assertIsSecureUrl(originalUrl);
51-
assertThat(originalUrl, containsString(expectedOriginalUrlContains()));
51+
ExtractorAsserts.assertContains(expectedOriginalUrlContains(), originalUrl);
5252
}
5353
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package org.schabi.newpipe.extractor.services;
22

3-
import org.junit.Test;
3+
import org.junit.jupiter.api.Test;
44
import org.schabi.newpipe.extractor.InfoItem;
55
import org.schabi.newpipe.extractor.ListExtractor;
66

0 commit comments

Comments
 (0)