Skip to content

Commit d9cf1fd

Browse files
committed
Improve Channel and Playlist tests
1 parent 11216f3 commit d9cf1fd

10 files changed

Lines changed: 1154 additions & 317 deletions

File tree

src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeChannelExtractor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ private String getNextPageUrlFrom(Document d) throws ParsingException {
201201
return "";
202202
}
203203
} catch (Exception e) {
204-
throw new ParsingException("could not get next streams' url", e);
204+
throw new ParsingException("Could not get next page url", e);
205205
}
206206
}
207207

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

33
import javax.annotation.Nonnull;
4+
import javax.annotation.Nullable;
45
import java.net.MalformedURLException;
56
import java.net.URL;
67
import java.util.List;
78

8-
import static org.junit.Assert.assertEquals;
9+
import static org.junit.Assert.*;
910

1011
public class ExtractorAsserts {
1112
public static void assertEmptyErrors(String message, List<Throwable> errors) {
12-
if(!errors.isEmpty()) {
13-
for (Throwable throwable : errors) {
14-
message += "\n * " + throwable.getMessage();
13+
if (!errors.isEmpty()) {
14+
StringBuilder messageBuilder = new StringBuilder(message);
15+
for (Throwable e : errors) {
16+
messageBuilder.append("\n * ").append(e.getMessage());
1517
}
16-
throw new AssertionError(message, errors.get(0));
18+
messageBuilder.append(" ");
19+
throw new AssertionError(messageBuilder.toString(), errors.get(0));
1720
}
1821
}
1922

@@ -22,7 +25,7 @@ private static URL urlFromString(String url) {
2225
try {
2326
return new URL(url);
2427
} catch (MalformedURLException e) {
25-
throw new AssertionError("Invalid url: " + url, e);
28+
throw new AssertionError("Invalid url: " + "\"" + url + "\"", e);
2629
}
2730
}
2831

@@ -34,4 +37,23 @@ public static void assertIsSecureUrl(String urlToCheck) {
3437
URL url = urlFromString(urlToCheck);
3538
assertEquals("Protocol of URL is not secure", "https", url.getProtocol());
3639
}
40+
41+
public static void assertNotEmpty(String stringToCheck) {
42+
assertNotEmpty(null, stringToCheck);
43+
}
44+
45+
public static void assertNotEmpty(@Nullable String message, String stringToCheck) {
46+
assertNotNull(message, stringToCheck);
47+
assertFalse(message, stringToCheck.isEmpty());
48+
}
49+
50+
public static void assertEmpty(String stringToCheck) {
51+
assertEmpty(null, stringToCheck);
52+
}
53+
54+
public static void assertEmpty(@Nullable String message, String stringToCheck) {
55+
if (stringToCheck != null) {
56+
assertTrue(message, stringToCheck.isEmpty());
57+
}
58+
}
3759
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package org.schabi.newpipe.extractor.services;
2+
3+
@SuppressWarnings("unused")
4+
public interface BaseChannelExtractorTest extends BaseListExtractorTest {
5+
void testDescription() throws Exception;
6+
void testAvatarUrl() throws Exception;
7+
void testBannerUrl() throws Exception;
8+
void testFeedUrl() throws Exception;
9+
void testSubscriberCount() throws Exception;
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package org.schabi.newpipe.extractor.services;
2+
3+
@SuppressWarnings("unused")
4+
public interface BaseExtractorTest {
5+
void testServiceId() throws Exception;
6+
void testName() throws Exception;
7+
void testId() throws Exception;
8+
void testCleanUrl() throws Exception;
9+
void testOriginalUrl() throws Exception;
10+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package org.schabi.newpipe.extractor.services;
2+
3+
import org.schabi.newpipe.extractor.InfoItem;
4+
import org.schabi.newpipe.extractor.InfoItemsCollector;
5+
import org.schabi.newpipe.extractor.ListExtractor;
6+
import org.schabi.newpipe.extractor.stream.StreamInfoItem;
7+
8+
import java.util.List;
9+
10+
import static org.junit.Assert.*;
11+
import static org.schabi.newpipe.extractor.ExtractorAsserts.*;
12+
13+
public interface BaseListExtractorTest extends BaseExtractorTest {
14+
@SuppressWarnings("unused")
15+
void testRelatedItems() throws Exception;
16+
@SuppressWarnings("unused")
17+
void testMoreRelatedItems() throws Exception;
18+
19+
20+
static void defaultTestListOfItems(int expectedServiceId, List<? extends InfoItem> itemsList, List<Throwable> errors) {
21+
assertTrue("List of items is empty", !itemsList.isEmpty());
22+
assertFalse("List of items contains a null element", itemsList.contains(null));
23+
assertEmptyErrors("Errors during stream list extraction", errors);
24+
25+
for (InfoItem item : itemsList) {
26+
assertIsSecureUrl(item.getUrl());
27+
if (item.getThumbnailUrl() != null && !item.getThumbnailUrl().isEmpty()) {
28+
assertIsSecureUrl(item.getThumbnailUrl());
29+
}
30+
assertNotNull("InfoItem type not set: " + item, item.getInfoType());
31+
assertEquals("Service id doesn't match: " + item, expectedServiceId, item.getServiceId());
32+
33+
if (item instanceof StreamInfoItem) {
34+
StreamInfoItem streamInfoItem = (StreamInfoItem) item;
35+
assertNotEmpty("Uploader name not set: " + item, streamInfoItem.getUploaderName());
36+
assertNotEmpty("Uploader url not set: " + item, streamInfoItem.getUploaderUrl());
37+
}
38+
}
39+
}
40+
41+
static void defaultTestRelatedItems(ListExtractor extractor, int expectedServiceId) throws Exception {
42+
final InfoItemsCollector<? extends InfoItem, ?> itemsCollector = extractor.getInfoItems();
43+
final List<? extends InfoItem> itemsList = itemsCollector.getItemList();
44+
List<Throwable> errors = itemsCollector.getErrors();
45+
46+
defaultTestListOfItems(expectedServiceId, itemsList, errors);
47+
}
48+
49+
static ListExtractor.InfoItemPage<? extends InfoItem> defaultTestMoreItems(ListExtractor extractor, int expectedServiceId) throws Exception {
50+
assertTrue("Doesn't have more items", extractor.hasNextPage());
51+
ListExtractor.InfoItemPage<? extends InfoItem> nextPage = extractor.getPage(extractor.getNextPageUrl());
52+
assertTrue("Next page is empty", !nextPage.getItemsList().isEmpty());
53+
assertEmptyErrors("Next page have errors", nextPage.getErrors());
54+
55+
defaultTestListOfItems(expectedServiceId, nextPage.getItemsList(), nextPage.getErrors());
56+
return nextPage;
57+
}
58+
59+
static void defaultTestGetPageInNewExtractor(ListExtractor extractor, ListExtractor newExtractor, int expectedServiceId) throws Exception {
60+
final String nextPageUrl = extractor.getNextPageUrl();
61+
62+
final ListExtractor.InfoItemPage<? extends InfoItem> page = newExtractor.getPage(nextPageUrl);
63+
BaseListExtractorTest.defaultTestListOfItems(expectedServiceId, page.getItemsList(), page.getErrors());
64+
}
65+
}
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+
@SuppressWarnings("unused")
4+
public interface BasePlaylistExtractorTest extends BaseListExtractorTest {
5+
void testThumbnailUrl() throws Exception;
6+
void testBannerUrl() throws Exception;
7+
void testUploaderUrl() throws Exception;
8+
void testUploaderName() throws Exception;
9+
void testUploaderAvatarUrl() throws Exception;
10+
void testStreamCount() throws Exception;
11+
}

0 commit comments

Comments
 (0)