Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JUnit 5 #768

Merged
merged 10 commits into from
Jan 24, 2022
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ allprojects {
ext {
nanojsonVersion = "1d9e1aea9049fc9f85e68b43ba39fe7be1c1f751"
spotbugsVersion = "4.5.2"
junitVersion = "4.13.2"
junitVersion = "5.8.2"
}
}

Expand Down
7 changes: 6 additions & 1 deletion extractor/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ test {
if (System.properties.containsKey('downloader')) {
systemProperty('downloader', System.getProperty('downloader'))
}
useJUnitPlatform()
}

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

testImplementation "junit:junit:$junitVersion"
testImplementation platform("org.junit:junit-bom:$junitVersion")
testImplementation 'org.junit.jupiter:junit-jupiter-api'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine'
testImplementation 'org.junit.jupiter:junit-jupiter-params'

testImplementation "com.squareup.okhttp3:okhttp:3.12.13"
testImplementation 'com.google.code.gson:gson:2.8.9'
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@
import java.net.URL;
import java.util.Collections;
import java.util.List;
import java.util.function.IntUnaryOperator;

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

public class SoundcloudSearchExtractor extends SearchExtractor {
private JsonArray searchCollection;
private JsonArray initialSearchCollection;

public SoundcloudSearchExtractor(final StreamingService service,
final SearchQueryHandler linkHandler) {
Expand All @@ -53,8 +54,9 @@ public List<MetaInfo> getMetaInfo() {
@Nonnull
@Override
public InfoItemsPage<InfoItem> getInitialPage() throws IOException, ExtractionException {
return new InfoItemsPage<>(collectItems(searchCollection), getNextPageFromCurrentUrl(
getUrl()));
return new InfoItemsPage<>(
collectItems(initialSearchCollection),
getNextPageFromCurrentUrl(getUrl(), currentOffset -> ITEMS_PER_PAGE));
}

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

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

return new InfoItemsPage<>(collectItems(searchCollection), getNextPageFromCurrentUrl(page
.getUrl()));
return new InfoItemsPage<>(
collectItems(searchCollection),
getNextPageFromCurrentUrl(page.getUrl(), currentOffset -> currentOffset + ITEMS_PER_PAGE));
}

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

if (searchCollection.isEmpty()) {
if (initialSearchCollection.isEmpty()) {
throw new SearchExtractor.NothingFoundException("Nothing found");
}
}
Expand Down Expand Up @@ -118,12 +122,14 @@ private InfoItemsCollector<InfoItem, InfoItemExtractor> collectItems(
return collector;
}

private Page getNextPageFromCurrentUrl(final String currentUrl)
private Page getNextPageFromCurrentUrl(final String currentUrl, final IntUnaryOperator newPageOffsetCalculator)
throws MalformedURLException, UnsupportedEncodingException {
final int pageOffset = Integer.parseInt(
Parser.compatParseMap(new URL(currentUrl).getQuery()).get("offset"));
final int currentPageOffset = Integer.parseInt(
Parser.compatParseMap(new URL(currentUrl).getQuery()).get("offset"));

return new Page(currentUrl.replace("&offset=" + pageOffset, "&offset="
+ (pageOffset + ITEMS_PER_PAGE)));
return new Page(
currentUrl.replace(
"&offset=" + currentPageOffset,
"&offset=" + newPageOffsetCalculator.applyAsInt(currentPageOffset)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ public class Utils {

public static final String HTTP = "http://";
public static final String HTTPS = "https://";
/**
* @deprecated Use {@link java.nio.charset.StandardCharsets#UTF_8} instead
litetex marked this conversation as resolved.
Show resolved Hide resolved
*/
@Deprecated
public static final String UTF_8 = "UTF-8";
public static final String EMPTY_STRING = "";
private static final Pattern M_PATTERN = Pattern.compile("(https?)?:\\/\\/m\\.");
Expand Down
25 changes: 0 additions & 25 deletions extractor/src/test/java/org/schabi/newpipe/MockOnly.java

This file was deleted.

51 changes: 0 additions & 51 deletions extractor/src/test/java/org/schabi/newpipe/MockOnlyRule.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,16 @@
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

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

public static void assertIsSecureUrl(String urlToCheck) {
URL url = urlFromString(urlToCheck);
assertEquals("Protocol of URL is not secure", "https", url.getProtocol());
assertEquals("https", url.getProtocol(), "Protocol of URL is not secure");
}

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

public static void assertNotEmpty(@Nullable String message, String stringToCheck) {
assertNotNull(message, stringToCheck);
assertFalse(message, stringToCheck.isEmpty());
assertFalse(stringToCheck.isEmpty(), message);
}

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

public static void assertEmpty(@Nullable String message, String stringToCheck) {
if (stringToCheck != null) {
assertTrue(message, stringToCheck.isEmpty());
assertTrue(stringToCheck.isEmpty(), message);
}
}

public static void assertAtLeast(long expected, long actual) {
assertTrue(actual + " is not at least " + expected, actual >= expected);
public static void assertGreater(final long expected, final long actual) {
assertGreater(expected, actual, actual + " is not > " + expected);
}

public static void assertGreater(
final long expected,
final long actual,
final String message
) {
assertTrue(actual > expected, message);
}

public static void assertGreaterOrEqual(final long expected, final long actual) {
assertGreaterOrEqual(expected, actual, actual + " is not >= " + expected);
}

public static void assertGreaterOrEqual(
final long expected,
final long actual,
final String message
) {
assertTrue(actual >= expected, message);
}

public static void assertLess(final long expected, final long actual) {
assertLess(expected, actual, actual + " is not < " + expected);
}

public static void assertLess(
final long expected,
final long actual,
final String message
) {
assertTrue(actual < expected, message);
}

public static void assertLessOrEqual(final long expected, final long actual) {
assertLessOrEqual(expected, actual, actual + " is not <= " + expected);
}

public static void assertLessOrEqual(
final long expected,
final long actual,
final String message
) {
assertTrue(actual <= expected, message);
}

// this assumes that sorting a and b in-place is not an issue, so it's only intended for tests
Expand All @@ -85,4 +127,13 @@ public static void assertEqualsOrderIndependent(final List<String> expected,
// using new ArrayList<> to make sure the type is the same
assertEquals(new ArrayList<>(expected), new ArrayList<>(actual));
}

public static void assertContains(
final String shouldBeContained,
final String container) {
assertNotNull(shouldBeContained, "shouldBeContained is null");
assertNotNull(container, "container is null");
assertTrue(container.contains(shouldBeContained),
"'" + shouldBeContained + "' should be contained inside '" + container +"'");
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package org.schabi.newpipe.extractor;

import org.junit.Test;
import org.junit.jupiter.api.Test;

import java.util.HashSet;

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

assertTrue(errorMsg, servicesId.add(streamingService.getServiceId()));
assertTrue(servicesId.add(streamingService.getServiceId()), errorMsg);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package org.schabi.newpipe.extractor.services;

import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.schabi.newpipe.extractor.Extractor;
import org.schabi.newpipe.extractor.ExtractorAsserts;
import org.schabi.newpipe.extractor.StreamingService;

import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import static org.junit.jupiter.api.Assertions.*;
import static org.schabi.newpipe.extractor.ExtractorAsserts.assertIsSecureUrl;

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

@Test
@Override
public void testOriginalUrl() throws Exception {
final String originalUrl = extractor().getOriginalUrl();
assertIsSecureUrl(originalUrl);
assertThat(originalUrl, containsString(expectedOriginalUrlContains()));
ExtractorAsserts.assertContains(expectedOriginalUrlContains(), originalUrl);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.schabi.newpipe.extractor.services;

import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.schabi.newpipe.extractor.InfoItem;
import org.schabi.newpipe.extractor.ListExtractor;

Expand Down
Loading