Code cleanup / improvements

* Deprecated Utils#UTF-8; see StandardCharsets
* Added more helpful methods to ``ExtractorAsserts``
* Use parameterized (cool new) tests
* Restore functionality of some tests + updated mockdata
* Other code cleanups + Sonarlint improvements
This commit is contained in:
litetex 2022-01-04 17:28:31 +01:00
parent 4291a90251
commit 3712a669b1
24 changed files with 2048 additions and 314 deletions

View File

@ -13,6 +13,10 @@ public class Utils {
public static final String HTTP = "http://"; public static final String HTTP = "http://";
public static final String HTTPS = "https://"; public static final String HTTPS = "https://";
/**
* @deprecated Use {@link java.nio.charset.StandardCharsets#UTF_8} instead
*/
@Deprecated
public static final String UTF_8 = "UTF-8"; public static final String UTF_8 = "UTF-8";
public static final String EMPTY_STRING = ""; public static final String EMPTY_STRING = "";
private static final Pattern M_PATTERN = Pattern.compile("(https?)?:\\/\\/m\\."); private static final Pattern M_PATTERN = Pattern.compile("(https?)?:\\/\\/m\\.");

View File

@ -64,8 +64,52 @@ public class ExtractorAsserts {
} }
} }
public static void assertAtLeast(long expected, long actual) { public static void assertGreater(final long expected, final long actual) {
assertTrue(actual >= expected, actual + " is not at least " + expected); 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 // this assumes that sorting a and b in-place is not an issue, so it's only intended for tests

View File

@ -1,6 +1,5 @@
package org.schabi.newpipe.extractor.services; package org.schabi.newpipe.extractor.services;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.schabi.newpipe.extractor.ExtractorAsserts; import org.schabi.newpipe.extractor.ExtractorAsserts;
import org.schabi.newpipe.extractor.InfoItemsCollector; import org.schabi.newpipe.extractor.InfoItemsCollector;
@ -30,7 +29,7 @@ import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.schabi.newpipe.extractor.ExtractorAsserts.assertAtLeast; import static org.schabi.newpipe.extractor.ExtractorAsserts.assertGreaterOrEqual;
import static org.schabi.newpipe.extractor.ExtractorAsserts.assertEqualsOrderIndependent; import static org.schabi.newpipe.extractor.ExtractorAsserts.assertEqualsOrderIndependent;
import static org.schabi.newpipe.extractor.ExtractorAsserts.assertIsSecureUrl; import static org.schabi.newpipe.extractor.ExtractorAsserts.assertIsSecureUrl;
import static org.schabi.newpipe.extractor.ExtractorAsserts.assertIsValidUrl; import static org.schabi.newpipe.extractor.ExtractorAsserts.assertIsValidUrl;
@ -174,7 +173,7 @@ public abstract class DefaultStreamExtractorTest extends DefaultExtractorTest<St
@Test @Test
@Override @Override
public void testViewCount() throws Exception { public void testViewCount() throws Exception {
assertAtLeast(expectedViewCountAtLeast(), extractor().getViewCount()); assertGreaterOrEqual(expectedViewCountAtLeast(), extractor().getViewCount());
} }
@Test @Test
@ -207,7 +206,7 @@ public abstract class DefaultStreamExtractorTest extends DefaultExtractorTest<St
if (expectedLikeCountAtLeast() == -1) { if (expectedLikeCountAtLeast() == -1) {
assertEquals(-1, extractor().getLikeCount()); assertEquals(-1, extractor().getLikeCount());
} else { } else {
assertAtLeast(expectedLikeCountAtLeast(), extractor().getLikeCount()); assertGreaterOrEqual(expectedLikeCountAtLeast(), extractor().getLikeCount());
} }
} }
@ -217,7 +216,7 @@ public abstract class DefaultStreamExtractorTest extends DefaultExtractorTest<St
if (expectedDislikeCountAtLeast() == -1) { if (expectedDislikeCountAtLeast() == -1) {
assertEquals(-1, extractor().getDislikeCount()); assertEquals(-1, extractor().getDislikeCount());
} else { } else {
assertAtLeast(expectedDislikeCountAtLeast(), extractor().getDislikeCount()); assertGreaterOrEqual(expectedDislikeCountAtLeast(), extractor().getDislikeCount());
} }
} }

View File

@ -2,13 +2,19 @@ package org.schabi.newpipe.extractor.services.media_ccc;
import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.schabi.newpipe.downloader.DownloaderTestImpl; import org.schabi.newpipe.downloader.DownloaderTestImpl;
import org.schabi.newpipe.extractor.ExtractorAsserts;
import org.schabi.newpipe.extractor.Info;
import org.schabi.newpipe.extractor.InfoItem; import org.schabi.newpipe.extractor.InfoItem;
import org.schabi.newpipe.extractor.NewPipe; import org.schabi.newpipe.extractor.NewPipe;
import org.schabi.newpipe.extractor.kiosk.KioskExtractor; import org.schabi.newpipe.extractor.kiosk.KioskExtractor;
import org.schabi.newpipe.extractor.services.media_ccc.extractors.MediaCCCConferenceKiosk; import org.schabi.newpipe.extractor.services.media_ccc.extractors.MediaCCCConferenceKiosk;
import org.schabi.newpipe.extractor.stream.StreamInfoItem;
import java.util.List; import java.util.List;
import java.util.Objects;
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.schabi.newpipe.extractor.ServiceList.MediaCCC; import static org.schabi.newpipe.extractor.ServiceList.MediaCCC;
@ -29,30 +35,25 @@ public class MediaCCCConferenceListExtractorTest {
} }
@Test @Test
public void getConferencesListTest() throws Exception { void getConferencesListTest() throws Exception {
assertTrue(extractor.getInitialPage().getItems().size() >= 174, ExtractorAsserts.assertGreaterOrEqual(174, extractor.getInitialPage().getItems().size());
"returned list was to small");
} }
@Test @ParameterizedTest
public void conferenceTypeTest() throws Exception { @ValueSource(strings = {
assertTrue(contains(extractor.getInitialPage().getItems(), "FrOSCon 2016")); "FrOSCon 2016",
assertTrue(contains(extractor.getInitialPage().getItems(), "ChaosWest @ 35c3")); "ChaosWest @ 35c3",
assertTrue(contains(extractor.getInitialPage().getItems(), "CTreffOS chaOStalks")); "CTreffOS chaOStalks",
assertTrue(contains(extractor.getInitialPage().getItems(), "Datenspuren 2015")); "Datenspuren 2015",
assertTrue(contains(extractor.getInitialPage().getItems(), "Chaos Singularity 2017")); "Chaos Singularity 2017",
assertTrue(contains(extractor.getInitialPage().getItems(), "SIGINT10")); "SIGINT10",
assertTrue(contains(extractor.getInitialPage().getItems(), "Vintage Computing Festival Berlin 2015")); "Vintage Computing Festival Berlin 2015",
assertTrue(contains(extractor.getInitialPage().getItems(), "FIfFKon 2015")); "FIfFKon 2015",
assertTrue(contains(extractor.getInitialPage().getItems(), "33C3: trailers")); "33C3: trailers",
assertTrue(contains(extractor.getInitialPage().getItems(), "Blinkenlights")); "Blinkenlights"
} })
void conferenceTypeTest(final String name) throws Exception {
private boolean contains(List<InfoItem> itemList, String name) { final List<InfoItem> itemList = extractor.getInitialPage().getItems();
for (InfoItem item : itemList) { assertTrue(itemList.stream().anyMatch(item -> name.equals(item.getName())));
if (item.getName().equals(name))
return true;
}
return false;
} }
} }

View File

@ -3,6 +3,7 @@ package org.schabi.newpipe.extractor.services.peertube;
import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.schabi.newpipe.downloader.DownloaderTestImpl; import org.schabi.newpipe.downloader.DownloaderTestImpl;
import org.schabi.newpipe.extractor.ExtractorAsserts;
import org.schabi.newpipe.extractor.NewPipe; import org.schabi.newpipe.extractor.NewPipe;
import org.schabi.newpipe.extractor.channel.ChannelExtractor; import org.schabi.newpipe.extractor.channel.ChannelExtractor;
import org.schabi.newpipe.extractor.exceptions.ParsingException; import org.schabi.newpipe.extractor.exceptions.ParsingException;
@ -101,7 +102,7 @@ public class PeertubeAccountExtractorTest {
@Test @Test
public void testSubscriberCount() throws ParsingException { public void testSubscriberCount() throws ParsingException {
assertTrue(extractor.getSubscriberCount() >= 700, "Wrong subscriber count"); ExtractorAsserts.assertGreaterOrEqual(700, extractor.getSubscriberCount());
} }
@Override @Override
@ -202,7 +203,7 @@ public class PeertubeAccountExtractorTest {
@Test @Test
public void testSubscriberCount() throws ParsingException { public void testSubscriberCount() throws ParsingException {
assertTrue(extractor.getSubscriberCount() >= 100, "Wrong subscriber count"); ExtractorAsserts.assertGreaterOrEqual(100, extractor.getSubscriberCount());
} }
@Override @Override

View File

@ -3,6 +3,7 @@ package org.schabi.newpipe.extractor.services.peertube;
import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.schabi.newpipe.downloader.DownloaderTestImpl; import org.schabi.newpipe.downloader.DownloaderTestImpl;
import org.schabi.newpipe.extractor.ExtractorAsserts;
import org.schabi.newpipe.extractor.NewPipe; import org.schabi.newpipe.extractor.NewPipe;
import org.schabi.newpipe.extractor.channel.ChannelExtractor; import org.schabi.newpipe.extractor.channel.ChannelExtractor;
import org.schabi.newpipe.extractor.exceptions.ParsingException; import org.schabi.newpipe.extractor.exceptions.ParsingException;
@ -116,7 +117,7 @@ public class PeertubeChannelExtractorTest {
@Test @Test
public void testSubscriberCount() throws ParsingException { public void testSubscriberCount() throws ParsingException {
assertTrue(extractor.getSubscriberCount() >= 230, "Wrong subscriber count"); ExtractorAsserts.assertGreaterOrEqual(230, extractor.getSubscriberCount());
} }
@Override @Override
@ -233,7 +234,7 @@ public class PeertubeChannelExtractorTest {
@Test @Test
public void testSubscriberCount() throws ParsingException { public void testSubscriberCount() throws ParsingException {
assertTrue(extractor.getSubscriberCount() >= 700, "Wrong subscriber count"); ExtractorAsserts.assertGreaterOrEqual(700, extractor.getSubscriberCount());
} }
@Override @Override

View File

@ -53,7 +53,7 @@ public class PeertubePlaylistExtractorTest {
@Test @Test
public void testGetStreamCount() throws ParsingException { public void testGetStreamCount() throws ParsingException {
ExtractorAsserts.assertAtLeast(39, extractor.getStreamCount()); ExtractorAsserts.assertGreaterOrEqual(39, extractor.getStreamCount());
} }
@Test @Test

View File

@ -91,7 +91,7 @@ public class SoundcloudPlaylistExtractorTest {
} }
@Test @Test
public void testUploaderUrl() { void testUploaderUrl() {
final String uploaderUrl = extractor.getUploaderUrl(); final String uploaderUrl = extractor.getUploaderUrl();
assertIsSecureUrl(uploaderUrl); assertIsSecureUrl(uploaderUrl);
ExtractorAsserts.assertContains("liluzivert", uploaderUrl); ExtractorAsserts.assertContains("liluzivert", uploaderUrl);
@ -109,11 +109,10 @@ public class SoundcloudPlaylistExtractorTest {
@Test @Test
public void testStreamCount() { public void testStreamCount() {
assertTrue(extractor.getStreamCount() >= 10, ExtractorAsserts.assertGreaterOrEqual(10, extractor.getStreamCount());
"Stream count does not fit: " + extractor.getStreamCount());
} }
@Override @Test
public void testUploaderVerified() throws Exception { public void testUploaderVerified() throws Exception {
assertTrue(extractor.isUploaderVerified()); assertTrue(extractor.isUploaderVerified());
} }
@ -189,7 +188,7 @@ public class SoundcloudPlaylistExtractorTest {
} }
@Test @Test
public void testUploaderUrl() { void testUploaderUrl() {
final String uploaderUrl = extractor.getUploaderUrl(); final String uploaderUrl = extractor.getUploaderUrl();
assertIsSecureUrl(uploaderUrl); assertIsSecureUrl(uploaderUrl);
ExtractorAsserts.assertContains("micky96", uploaderUrl); ExtractorAsserts.assertContains("micky96", uploaderUrl);
@ -207,11 +206,10 @@ public class SoundcloudPlaylistExtractorTest {
@Test @Test
public void testStreamCount() { public void testStreamCount() {
assertTrue(extractor.getStreamCount() >= 10, ExtractorAsserts.assertGreaterOrEqual(10, extractor.getStreamCount());
"Stream count does not fit: " + extractor.getStreamCount());
} }
@Override @Test
public void testUploaderVerified() throws Exception { public void testUploaderVerified() throws Exception {
assertFalse(extractor.isUploaderVerified()); assertFalse(extractor.isUploaderVerified());
} }
@ -233,7 +231,7 @@ public class SoundcloudPlaylistExtractorTest {
//////////////////////////////////////////////////////////////////////////*/ //////////////////////////////////////////////////////////////////////////*/
@Test @Test
public void testGetPageInNewExtractor() throws Exception { void testGetPageInNewExtractor() throws Exception {
PlaylistExtractor newExtractor = SoundCloud.getPlaylistExtractor(extractor.getUrl()); PlaylistExtractor newExtractor = SoundCloud.getPlaylistExtractor(extractor.getUrl());
defaultTestGetPageInNewExtractor(extractor, newExtractor); defaultTestGetPageInNewExtractor(extractor, newExtractor);
} }
@ -302,7 +300,7 @@ public class SoundcloudPlaylistExtractorTest {
} }
@Test @Test
public void testUploaderUrl() { void testUploaderUrl() {
final String uploaderUrl = extractor.getUploaderUrl(); final String uploaderUrl = extractor.getUploaderUrl();
assertIsSecureUrl(uploaderUrl); assertIsSecureUrl(uploaderUrl);
ExtractorAsserts.assertContains("user350509423", uploaderUrl); ExtractorAsserts.assertContains("user350509423", uploaderUrl);
@ -320,11 +318,10 @@ public class SoundcloudPlaylistExtractorTest {
@Test @Test
public void testStreamCount() { public void testStreamCount() {
assertTrue(extractor.getStreamCount() >= 370, ExtractorAsserts.assertGreaterOrEqual(370, extractor.getStreamCount());
"Stream count does not fit: " + extractor.getStreamCount());
} }
@Override @Test
public void testUploaderVerified() throws Exception { public void testUploaderVerified() throws Exception {
assertFalse(extractor.isUploaderVerified()); assertFalse(extractor.isUploaderVerified());
} }
@ -407,7 +404,7 @@ public class SoundcloudPlaylistExtractorTest {
} }
@Test @Test
public void testUploaderUrl() { void testUploaderUrl() {
final String uploaderUrl = extractor.getUploaderUrl(); final String uploaderUrl = extractor.getUploaderUrl();
assertIsSecureUrl(uploaderUrl); assertIsSecureUrl(uploaderUrl);
ExtractorAsserts.assertContains("breezy-123", uploaderUrl); ExtractorAsserts.assertContains("breezy-123", uploaderUrl);
@ -428,7 +425,7 @@ public class SoundcloudPlaylistExtractorTest {
assertEquals(2, extractor.getStreamCount()); assertEquals(2, extractor.getStreamCount());
} }
@Override @Test
public void testUploaderVerified() throws Exception { public void testUploaderVerified() throws Exception {
assertFalse(extractor.isUploaderVerified()); assertFalse(extractor.isUploaderVerified());
} }

View File

@ -2,6 +2,8 @@ package org.schabi.newpipe.extractor.services.soundcloud;
import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.schabi.newpipe.downloader.DownloaderTestImpl; import org.schabi.newpipe.downloader.DownloaderTestImpl;
import org.schabi.newpipe.extractor.NewPipe; import org.schabi.newpipe.extractor.NewPipe;
import org.schabi.newpipe.extractor.ServiceList; import org.schabi.newpipe.extractor.ServiceList;
@ -31,47 +33,42 @@ public class SoundcloudSubscriptionExtractorTest {
urlHandler = ServiceList.SoundCloud.getChannelLHFactory(); urlHandler = ServiceList.SoundCloud.getChannelLHFactory();
} }
@Test @ParameterizedTest
public void testFromChannelUrl() throws Exception { @ValueSource(strings = {
testList(subscriptionExtractor.fromChannelUrl("https://soundcloud.com/monstercat")); "https://soundcloud.com/monstercat",
testList(subscriptionExtractor.fromChannelUrl("http://soundcloud.com/monstercat")); "http://soundcloud.com/monstercat",
testList(subscriptionExtractor.fromChannelUrl("soundcloud.com/monstercat")); "soundcloud.com/monstercat",
testList(subscriptionExtractor.fromChannelUrl("monstercat")); "monstercat",
// Empty followings user
//Empty followings user "some-random-user-184047028"
testList(subscriptionExtractor.fromChannelUrl("some-random-user-184047028")); })
} void testFromChannelUrl(final String channelUrl) throws Exception {
for (SubscriptionItem item : subscriptionExtractor.fromChannelUrl(channelUrl)) {
@Test
public void testInvalidSourceException() {
List<String> invalidList = Arrays.asList(
"httttps://invalid.com/user",
".com/monstercat",
"ithinkthatthisuserdontexist",
"",
null
);
for (String invalidUser : invalidList) {
try {
subscriptionExtractor.fromChannelUrl(invalidUser);
fail("didn't throw exception");
} catch (IOException e) {
// Ignore it, could be an unstable network on the CI server
} catch (Exception e) {
boolean isExpectedException = e instanceof SubscriptionExtractor.InvalidSourceException;
assertTrue(isExpectedException, e.getClass().getSimpleName() + " is not the expected exception");
}
}
}
private void testList(List<SubscriptionItem> subscriptionItems) throws ParsingException {
for (SubscriptionItem item : subscriptionItems) {
assertNotNull(item.getName()); assertNotNull(item.getName());
assertNotNull(item.getUrl()); assertNotNull(item.getUrl());
assertTrue(urlHandler.acceptUrl(item.getUrl())); assertTrue(urlHandler.acceptUrl(item.getUrl()));
assertFalse(item.getServiceId() == -1); assertNotEquals(-1, item.getServiceId());
} }
} }
@ParameterizedTest
@ValueSource(strings = {
"httttps://invalid.com/user",
".com/monstercat",
"ithinkthatthisuserdontexist",
""
})
void testInvalidSourceException(final String invalidUser) {
assertThrows(
SubscriptionExtractor.InvalidSourceException.class,
() -> subscriptionExtractor.fromChannelUrl(invalidUser));
}
// null can't be added to the above value source because it's not a constant
@Test
void testInvalidSourceExceptionWhenUrlIsNull() {
assertThrows(
SubscriptionExtractor.InvalidSourceException.class,
() -> subscriptionExtractor.fromChannelUrl(null));
}
} }

View File

@ -136,13 +136,11 @@ public class YoutubeChannelExtractorTest {
} }
@Test @Test
public void noVideoTab() throws Exception { void noVideoTab() throws Exception {
final ChannelExtractor extractor = YouTube.getChannelExtractor("https://invidio.us/channel/UC-9-kyTW8ZkZNDHQJ6FgpwQ"); final ChannelExtractor extractor = YouTube.getChannelExtractor("https://invidio.us/channel/UC-9-kyTW8ZkZNDHQJ6FgpwQ");
assertThrows(ContentNotSupportedException.class, () -> { extractor.fetchPage();
extractor.fetchPage(); assertThrows(ContentNotSupportedException.class, extractor::getInitialPage);
extractor.getInitialPage();
});
} }
} }
@ -215,14 +213,14 @@ public class YoutubeChannelExtractorTest {
public void testAvatarUrl() throws Exception { public void testAvatarUrl() throws Exception {
String avatarUrl = extractor.getAvatarUrl(); String avatarUrl = extractor.getAvatarUrl();
assertIsSecureUrl(avatarUrl); assertIsSecureUrl(avatarUrl);
assertTrue(avatarUrl.contains("yt3"), avatarUrl); ExtractorAsserts.assertContains("yt3", avatarUrl);
} }
@Test @Test
public void testBannerUrl() throws Exception { public void testBannerUrl() throws Exception {
String bannerUrl = extractor.getBannerUrl(); String bannerUrl = extractor.getBannerUrl();
assertIsSecureUrl(bannerUrl); assertIsSecureUrl(bannerUrl);
assertTrue(bannerUrl.contains("yt3"), bannerUrl); ExtractorAsserts.assertContains("yt3", bannerUrl);
} }
@Test @Test
@ -232,8 +230,7 @@ public class YoutubeChannelExtractorTest {
@Test @Test
public void testSubscriberCount() throws Exception { public void testSubscriberCount() throws Exception {
assertTrue(extractor.getSubscriberCount() >= 0, "Wrong subscriber count"); ExtractorAsserts.assertGreaterOrEqual(4_900_000, extractor.getSubscriberCount());
assertTrue(extractor.getSubscriberCount() >= 4e6, "Subscriber count too small");
} }
@Override @Override
@ -313,14 +310,14 @@ public class YoutubeChannelExtractorTest {
public void testAvatarUrl() throws Exception { public void testAvatarUrl() throws Exception {
String avatarUrl = extractor.getAvatarUrl(); String avatarUrl = extractor.getAvatarUrl();
assertIsSecureUrl(avatarUrl); assertIsSecureUrl(avatarUrl);
assertTrue(avatarUrl.contains("yt3"), avatarUrl); ExtractorAsserts.assertContains("yt3", avatarUrl);
} }
@Test @Test
public void testBannerUrl() throws Exception { public void testBannerUrl() throws Exception {
String bannerUrl = extractor.getBannerUrl(); String bannerUrl = extractor.getBannerUrl();
assertIsSecureUrl(bannerUrl); assertIsSecureUrl(bannerUrl);
assertTrue(bannerUrl.contains("yt3"), bannerUrl); ExtractorAsserts.assertContains("yt3", bannerUrl);
} }
@Test @Test
@ -330,11 +327,10 @@ public class YoutubeChannelExtractorTest {
@Test @Test
public void testSubscriberCount() throws Exception { public void testSubscriberCount() throws Exception {
assertTrue(extractor.getSubscriberCount() >= 0, "Wrong subscriber count"); ExtractorAsserts.assertGreaterOrEqual(17_000_000, extractor.getSubscriberCount());
assertTrue(extractor.getSubscriberCount() >= 10e6, "Subscriber count too small");
} }
@Override @Test
public void testVerified() throws Exception { public void testVerified() throws Exception {
assertTrue(extractor.isVerified()); assertTrue(extractor.isVerified());
} }
@ -412,14 +408,14 @@ public class YoutubeChannelExtractorTest {
public void testAvatarUrl() throws Exception { public void testAvatarUrl() throws Exception {
String avatarUrl = extractor.getAvatarUrl(); String avatarUrl = extractor.getAvatarUrl();
assertIsSecureUrl(avatarUrl); assertIsSecureUrl(avatarUrl);
assertTrue(avatarUrl.contains("yt3"), avatarUrl); ExtractorAsserts.assertContains("yt3", avatarUrl);
} }
@Test @Test
public void testBannerUrl() throws Exception { public void testBannerUrl() throws Exception {
String bannerUrl = extractor.getBannerUrl(); String bannerUrl = extractor.getBannerUrl();
assertIsSecureUrl(bannerUrl); assertIsSecureUrl(bannerUrl);
assertTrue(bannerUrl.contains("yt3"), bannerUrl); ExtractorAsserts.assertContains("yt3", bannerUrl);
} }
@Test @Test
@ -429,10 +425,10 @@ public class YoutubeChannelExtractorTest {
@Test @Test
public void testSubscriberCount() throws Exception { public void testSubscriberCount() throws Exception {
assertTrue(extractor.getSubscriberCount() >= 5e6, "Wrong subscriber count"); ExtractorAsserts.assertGreaterOrEqual(17_000_000, extractor.getSubscriberCount());
} }
@Override @Test
public void testVerified() throws Exception { public void testVerified() throws Exception {
assertTrue(extractor.isVerified()); assertTrue(extractor.isVerified());
} }
@ -527,14 +523,14 @@ public class YoutubeChannelExtractorTest {
public void testAvatarUrl() throws Exception { public void testAvatarUrl() throws Exception {
String avatarUrl = extractor.getAvatarUrl(); String avatarUrl = extractor.getAvatarUrl();
assertIsSecureUrl(avatarUrl); assertIsSecureUrl(avatarUrl);
assertTrue(avatarUrl.contains("yt3"), avatarUrl); ExtractorAsserts.assertContains("yt3", avatarUrl);
} }
@Test @Test
public void testBannerUrl() throws Exception { public void testBannerUrl() throws Exception {
String bannerUrl = extractor.getBannerUrl(); String bannerUrl = extractor.getBannerUrl();
assertIsSecureUrl(bannerUrl); assertIsSecureUrl(bannerUrl);
assertTrue(bannerUrl.contains("yt3"), bannerUrl); ExtractorAsserts.assertContains("yt3", bannerUrl);
} }
@Test @Test
@ -544,10 +540,10 @@ public class YoutubeChannelExtractorTest {
@Test @Test
public void testSubscriberCount() throws Exception { public void testSubscriberCount() throws Exception {
assertTrue(extractor.getSubscriberCount() >= 5e5, "Wrong subscriber count"); ExtractorAsserts.assertGreaterOrEqual(2_000_000, extractor.getSubscriberCount());
} }
@Override @Test
public void testVerified() throws Exception { public void testVerified() throws Exception {
assertTrue(extractor.isVerified()); assertTrue(extractor.isVerified());
} }
@ -628,14 +624,14 @@ public class YoutubeChannelExtractorTest {
public void testAvatarUrl() throws Exception { public void testAvatarUrl() throws Exception {
String avatarUrl = extractor.getAvatarUrl(); String avatarUrl = extractor.getAvatarUrl();
assertIsSecureUrl(avatarUrl); assertIsSecureUrl(avatarUrl);
assertTrue(avatarUrl.contains("yt3"), avatarUrl); ExtractorAsserts.assertContains("yt3", avatarUrl);
} }
@Test @Test
public void testBannerUrl() throws Exception { public void testBannerUrl() throws Exception {
String bannerUrl = extractor.getBannerUrl(); String bannerUrl = extractor.getBannerUrl();
assertIsSecureUrl(bannerUrl); assertIsSecureUrl(bannerUrl);
assertTrue(bannerUrl.contains("yt3"), bannerUrl); ExtractorAsserts.assertContains("yt3", bannerUrl);
} }
@Test @Test
@ -645,10 +641,10 @@ public class YoutubeChannelExtractorTest {
@Test @Test
public void testSubscriberCount() throws Exception { public void testSubscriberCount() throws Exception {
assertTrue(extractor.getSubscriberCount() >= 50, "Wrong subscriber count"); ExtractorAsserts.assertGreaterOrEqual(50, extractor.getSubscriberCount());
} }
@Override @Test
public void testVerified() throws Exception { public void testVerified() throws Exception {
assertFalse(extractor.isVerified()); assertFalse(extractor.isVerified());
} }

View File

@ -15,6 +15,7 @@ import org.schabi.newpipe.extractor.services.youtube.extractors.YoutubeMixPlayli
import org.schabi.newpipe.extractor.stream.StreamInfoItem; import org.schabi.newpipe.extractor.stream.StreamInfoItem;
import java.io.IOException; import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.*; import java.util.*;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
@ -24,19 +25,16 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.schabi.newpipe.extractor.ExtractorAsserts.assertIsSecureUrl; import static org.schabi.newpipe.extractor.ExtractorAsserts.assertIsSecureUrl;
import static org.schabi.newpipe.extractor.ServiceList.YouTube; import static org.schabi.newpipe.extractor.ServiceList.YouTube;
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.*; import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.*;
import static org.schabi.newpipe.extractor.utils.Utils.UTF_8;
public class YoutubeMixPlaylistExtractorTest { public class YoutubeMixPlaylistExtractorTest {
private static final String VIDEO_ID = "_AzeUSL9lZc"; private static final String VIDEO_ID = "QMVCAPd5cwBcg";
private static final String VIDEO_TITLE = private static final String VIDEO_TITLE = "Mix ";
"Most Beautiful And Emotional Piano: Anime Music Shigatsu wa Kimi no Uso OST IMO";
private static final String RESOURCE_PATH = DownloaderFactory.RESOURCE_PATH + "services/youtube/extractor/mix/"; private static final String RESOURCE_PATH = DownloaderFactory.RESOURCE_PATH + "services/youtube/extractor/mix/";
private static final Map<String, String> dummyCookie = new HashMap<>(); private static final Map<String, String> dummyCookie = new HashMap<>();
private static YoutubeMixPlaylistExtractor extractor; private static YoutubeMixPlaylistExtractor extractor;
@Disabled("Test broken, video was blocked by SME and is only available in Japan")
public static class Mix { public static class Mix {
@BeforeAll @BeforeAll
@ -52,19 +50,19 @@ public class YoutubeMixPlaylistExtractorTest {
} }
@Test @Test
public void getServiceId() { void getServiceId() {
assertEquals(YouTube.getServiceId(), extractor.getServiceId()); assertEquals(YouTube.getServiceId(), extractor.getServiceId());
} }
@Test @Test
public void getName() throws Exception { void getName() throws Exception {
final String name = extractor.getName(); final String name = extractor.getName();
ExtractorAsserts.assertContains("Mix", name); ExtractorAsserts.assertContains("Mix", name);
ExtractorAsserts.assertContains(VIDEO_TITLE, name); ExtractorAsserts.assertContains(VIDEO_TITLE, name);
} }
@Test @Test
public void getThumbnailUrl() throws Exception { void getThumbnailUrl() throws Exception {
final String thumbnailUrl = extractor.getThumbnailUrl(); final String thumbnailUrl = extractor.getThumbnailUrl();
assertIsSecureUrl(thumbnailUrl); assertIsSecureUrl(thumbnailUrl);
ExtractorAsserts.assertContains("yt", thumbnailUrl); ExtractorAsserts.assertContains("yt", thumbnailUrl);
@ -72,21 +70,21 @@ public class YoutubeMixPlaylistExtractorTest {
} }
@Test @Test
public void getInitialPage() throws Exception { void getInitialPage() throws Exception {
final InfoItemsPage<StreamInfoItem> streams = extractor.getInitialPage(); final InfoItemsPage<StreamInfoItem> streams = extractor.getInitialPage();
assertFalse(streams.getItems().isEmpty()); assertFalse(streams.getItems().isEmpty());
assertTrue(streams.hasNextPage()); assertTrue(streams.hasNextPage());
} }
@Test @Test
public void getPage() throws Exception { void getPage() throws Exception {
final byte[] body = JsonWriter.string(prepareDesktopJsonBuilder( final byte[] body = JsonWriter.string(prepareDesktopJsonBuilder(
NewPipe.getPreferredLocalization(), NewPipe.getPreferredContentCountry()) NewPipe.getPreferredLocalization(), NewPipe.getPreferredContentCountry())
.value("videoId", VIDEO_ID) .value("videoId", VIDEO_ID)
.value("playlistId", "RD" + VIDEO_ID) .value("playlistId", "RD" + VIDEO_ID)
.value("params", "OAE%3D") .value("params", "OAE%3D")
.done()) .done())
.getBytes(UTF_8); .getBytes(StandardCharsets.UTF_8);
final InfoItemsPage<StreamInfoItem> streams = extractor.getPage(new Page( final InfoItemsPage<StreamInfoItem> streams = extractor.getPage(new Page(
YOUTUBEI_V1_URL + "next?key=" + getKey(), null, null, dummyCookie, body)); YOUTUBEI_V1_URL + "next?key=" + getKey(), null, null, dummyCookie, body));
@ -95,7 +93,7 @@ public class YoutubeMixPlaylistExtractorTest {
} }
@Test @Test
public void getContinuations() throws Exception { void getContinuations() throws Exception {
InfoItemsPage<StreamInfoItem> streams = extractor.getInitialPage(); InfoItemsPage<StreamInfoItem> streams = extractor.getInitialPage();
final Set<String> urls = new HashSet<>(); final Set<String> urls = new HashSet<>();
@ -117,16 +115,15 @@ public class YoutubeMixPlaylistExtractorTest {
} }
@Test @Test
public void getStreamCount() { void getStreamCount() {
assertEquals(ListExtractor.ITEM_COUNT_INFINITE, extractor.getStreamCount()); assertEquals(ListExtractor.ITEM_COUNT_INFINITE, extractor.getStreamCount());
} }
} }
@Disabled("Test broken, video was removed by the uploader")
public static class MixWithIndex { public static class MixWithIndex {
private static final int INDEX = 13; private static final int INDEX = 4;
private static final String VIDEO_ID_NUMBER_13 = "qHtzO49SDmk"; private static final String VIDEO_ID_NUMBER_4 = "lWA2pjMjpBs";
@BeforeAll @BeforeAll
public static void setUp() throws Exception { public static void setUp() throws Exception {
@ -135,20 +132,20 @@ public class YoutubeMixPlaylistExtractorTest {
NewPipe.init(new DownloaderFactory().getDownloader(RESOURCE_PATH + "mixWithIndex")); NewPipe.init(new DownloaderFactory().getDownloader(RESOURCE_PATH + "mixWithIndex"));
dummyCookie.put(YoutubeMixPlaylistExtractor.COOKIE_NAME, "whatever"); dummyCookie.put(YoutubeMixPlaylistExtractor.COOKIE_NAME, "whatever");
extractor = (YoutubeMixPlaylistExtractor) YouTube extractor = (YoutubeMixPlaylistExtractor) YouTube
.getPlaylistExtractor("https://www.youtube.com/watch?v=" + VIDEO_ID_NUMBER_13 .getPlaylistExtractor("https://www.youtube.com/watch?v=" + VIDEO_ID_NUMBER_4
+ "&list=RD" + VIDEO_ID + "&index=" + INDEX); + "&list=RD" + VIDEO_ID + "&index=" + INDEX);
extractor.fetchPage(); extractor.fetchPage();
} }
@Test @Test
public void getName() throws Exception { void getName() throws Exception {
final String name = extractor.getName(); final String name = extractor.getName();
ExtractorAsserts.assertContains("Mix", name); ExtractorAsserts.assertContains("Mix", name);
ExtractorAsserts.assertContains(VIDEO_TITLE, name); ExtractorAsserts.assertContains(VIDEO_TITLE, name);
} }
@Test @Test
public void getThumbnailUrl() throws Exception { void getThumbnailUrl() throws Exception {
final String thumbnailUrl = extractor.getThumbnailUrl(); final String thumbnailUrl = extractor.getThumbnailUrl();
assertIsSecureUrl(thumbnailUrl); assertIsSecureUrl(thumbnailUrl);
ExtractorAsserts.assertContains("yt", thumbnailUrl); ExtractorAsserts.assertContains("yt", thumbnailUrl);
@ -156,14 +153,14 @@ public class YoutubeMixPlaylistExtractorTest {
} }
@Test @Test
public void getInitialPage() throws Exception { void getInitialPage() throws Exception {
final InfoItemsPage<StreamInfoItem> streams = extractor.getInitialPage(); final InfoItemsPage<StreamInfoItem> streams = extractor.getInitialPage();
assertFalse(streams.getItems().isEmpty()); assertFalse(streams.getItems().isEmpty());
assertTrue(streams.hasNextPage()); assertTrue(streams.hasNextPage());
} }
@Test @Test
public void getPage() throws Exception { void getPage() throws Exception {
final byte[] body = JsonWriter.string(prepareDesktopJsonBuilder( final byte[] body = JsonWriter.string(prepareDesktopJsonBuilder(
NewPipe.getPreferredLocalization(), NewPipe.getPreferredContentCountry()) NewPipe.getPreferredLocalization(), NewPipe.getPreferredContentCountry())
.value("videoId", VIDEO_ID) .value("videoId", VIDEO_ID)
@ -171,7 +168,7 @@ public class YoutubeMixPlaylistExtractorTest {
.value("playlistIndex", INDEX) .value("playlistIndex", INDEX)
.value("params", "OAE%3D") .value("params", "OAE%3D")
.done()) .done())
.getBytes(UTF_8); .getBytes(StandardCharsets.UTF_8);
final InfoItemsPage<StreamInfoItem> streams = extractor.getPage(new Page( final InfoItemsPage<StreamInfoItem> streams = extractor.getPage(new Page(
YOUTUBEI_V1_URL + "next?key=" + getKey(), null, null, dummyCookie, body)); YOUTUBEI_V1_URL + "next?key=" + getKey(), null, null, dummyCookie, body));
@ -180,7 +177,7 @@ public class YoutubeMixPlaylistExtractorTest {
} }
@Test @Test
public void getContinuations() throws Exception { void getContinuations() throws Exception {
InfoItemsPage<StreamInfoItem> streams = extractor.getInitialPage(); InfoItemsPage<StreamInfoItem> streams = extractor.getInitialPage();
final Set<String> urls = new HashSet<>(); final Set<String> urls = new HashSet<>();
@ -201,12 +198,11 @@ public class YoutubeMixPlaylistExtractorTest {
} }
@Test @Test
public void getStreamCount() { void getStreamCount() {
assertEquals(ListExtractor.ITEM_COUNT_INFINITE, extractor.getStreamCount()); assertEquals(ListExtractor.ITEM_COUNT_INFINITE, extractor.getStreamCount());
} }
} }
@Disabled("Test broken")
public static class MyMix { public static class MyMix {
@BeforeAll @BeforeAll
@ -222,39 +218,39 @@ public class YoutubeMixPlaylistExtractorTest {
} }
@Test @Test
public void getServiceId() { void getServiceId() {
assertEquals(YouTube.getServiceId(), extractor.getServiceId()); assertEquals(YouTube.getServiceId(), extractor.getServiceId());
} }
@Test @Test
public void getName() throws Exception { void getName() throws Exception {
final String name = extractor.getName(); final String name = extractor.getName();
assertEquals("My Mix", name); assertEquals("My Mix", name);
} }
@Test @Test
public void getThumbnailUrl() throws Exception { void getThumbnailUrl() throws Exception {
final String thumbnailUrl = extractor.getThumbnailUrl(); final String thumbnailUrl = extractor.getThumbnailUrl();
assertIsSecureUrl(thumbnailUrl); assertIsSecureUrl(thumbnailUrl);
assertTrue(thumbnailUrl.startsWith("https://i.ytimg.com/vi/_AzeUSL9lZc")); assertTrue(thumbnailUrl.startsWith("https://i.ytimg.com/vi/_AzeUSL9lZc"));
} }
@Test @Test
public void getInitialPage() throws Exception { void getInitialPage() throws Exception {
final InfoItemsPage<StreamInfoItem> streams = extractor.getInitialPage(); final InfoItemsPage<StreamInfoItem> streams = extractor.getInitialPage();
assertFalse(streams.getItems().isEmpty()); assertFalse(streams.getItems().isEmpty());
assertTrue(streams.hasNextPage()); assertTrue(streams.hasNextPage());
} }
@Test @Test
public void getPage() throws Exception { void getPage() throws Exception {
final byte[] body = JsonWriter.string(prepareDesktopJsonBuilder( final byte[] body = JsonWriter.string(prepareDesktopJsonBuilder(
NewPipe.getPreferredLocalization(), NewPipe.getPreferredContentCountry()) NewPipe.getPreferredLocalization(), NewPipe.getPreferredContentCountry())
.value("videoId", VIDEO_ID) .value("videoId", VIDEO_ID)
.value("playlistId", "RDMM" + VIDEO_ID) .value("playlistId", "RDMM" + VIDEO_ID)
.value("params", "OAE%3D") .value("params", "OAE%3D")
.done()) .done())
.getBytes(UTF_8); .getBytes(StandardCharsets.UTF_8);
final InfoItemsPage<StreamInfoItem> streams = extractor.getPage(new Page( final InfoItemsPage<StreamInfoItem> streams = extractor.getPage(new Page(
YOUTUBEI_V1_URL + "next?key=" + getKey(), null, null, dummyCookie, body)); YOUTUBEI_V1_URL + "next?key=" + getKey(), null, null, dummyCookie, body));
@ -263,7 +259,7 @@ public class YoutubeMixPlaylistExtractorTest {
} }
@Test @Test
public void getContinuations() throws Exception { void getContinuations() throws Exception {
InfoItemsPage<StreamInfoItem> streams = extractor.getInitialPage(); InfoItemsPage<StreamInfoItem> streams = extractor.getInitialPage();
final Set<String> urls = new HashSet<>(); final Set<String> urls = new HashSet<>();
@ -285,7 +281,7 @@ public class YoutubeMixPlaylistExtractorTest {
} }
@Test @Test
public void getStreamCount() { void getStreamCount() {
assertEquals(ListExtractor.ITEM_COUNT_INFINITE, extractor.getStreamCount()); assertEquals(ListExtractor.ITEM_COUNT_INFINITE, extractor.getStreamCount());
} }
} }
@ -300,29 +296,13 @@ public class YoutubeMixPlaylistExtractorTest {
dummyCookie.put(YoutubeMixPlaylistExtractor.COOKIE_NAME, "whatever"); dummyCookie.put(YoutubeMixPlaylistExtractor.COOKIE_NAME, "whatever");
} }
@Disabled
@Test @Test
public void getPageEmptyUrl() throws Exception { void invalidVideoId() throws Exception {
extractor = (YoutubeMixPlaylistExtractor) YouTube
.getPlaylistExtractor("https://www.youtube.com/watch?v=" + VIDEO_ID
+ "&list=RD" + VIDEO_ID);
assertThrows(IllegalArgumentException.class, () -> {
extractor.fetchPage();
extractor.getPage(new Page(""));
});
}
@Test
public void invalidVideoId() throws Exception {
extractor = (YoutubeMixPlaylistExtractor) YouTube extractor = (YoutubeMixPlaylistExtractor) YouTube
.getPlaylistExtractor("https://www.youtube.com/watch?v=" + "abcde" .getPlaylistExtractor("https://www.youtube.com/watch?v=" + "abcde"
+ "&list=RD" + "abcde"); + "&list=RD" + "abcde");
assertThrows(ExtractionException.class, () -> { assertThrows(ExtractionException.class, extractor::fetchPage);
extractor.fetchPage();
extractor.getName();
});
} }
} }
@ -346,35 +326,35 @@ public class YoutubeMixPlaylistExtractorTest {
} }
@Test @Test
public void getName() throws Exception { void getName() throws Exception {
final String name = extractor.getName(); final String name = extractor.getName();
ExtractorAsserts.assertContains("Mix", name); ExtractorAsserts.assertContains("Mix", name);
ExtractorAsserts.assertContains(CHANNEL_TITLE, name); ExtractorAsserts.assertContains(CHANNEL_TITLE, name);
} }
@Test @Test
public void getThumbnailUrl() throws Exception { void getThumbnailUrl() throws Exception {
final String thumbnailUrl = extractor.getThumbnailUrl(); final String thumbnailUrl = extractor.getThumbnailUrl();
assertIsSecureUrl(thumbnailUrl); assertIsSecureUrl(thumbnailUrl);
ExtractorAsserts.assertContains("yt", thumbnailUrl); ExtractorAsserts.assertContains("yt", thumbnailUrl);
} }
@Test @Test
public void getInitialPage() throws Exception { void getInitialPage() throws Exception {
final InfoItemsPage<StreamInfoItem> streams = extractor.getInitialPage(); final InfoItemsPage<StreamInfoItem> streams = extractor.getInitialPage();
assertFalse(streams.getItems().isEmpty()); assertFalse(streams.getItems().isEmpty());
assertTrue(streams.hasNextPage()); assertTrue(streams.hasNextPage());
} }
@Test @Test
public void getPage() throws Exception { void getPage() throws Exception {
final byte[] body = JsonWriter.string(prepareDesktopJsonBuilder( final byte[] body = JsonWriter.string(prepareDesktopJsonBuilder(
NewPipe.getPreferredLocalization(), NewPipe.getPreferredContentCountry()) NewPipe.getPreferredLocalization(), NewPipe.getPreferredContentCountry())
.value("videoId", VIDEO_ID_OF_CHANNEL) .value("videoId", VIDEO_ID_OF_CHANNEL)
.value("playlistId", "RDCM" + CHANNEL_ID) .value("playlistId", "RDCM" + CHANNEL_ID)
.value("params", "OAE%3D") .value("params", "OAE%3D")
.done()) .done())
.getBytes(UTF_8); .getBytes(StandardCharsets.UTF_8);
final InfoItemsPage<StreamInfoItem> streams = extractor.getPage(new Page( final InfoItemsPage<StreamInfoItem> streams = extractor.getPage(new Page(
YOUTUBEI_V1_URL + "next?key=" + getKey(), null, null, dummyCookie, body)); YOUTUBEI_V1_URL + "next?key=" + getKey(), null, null, dummyCookie, body));
@ -383,7 +363,7 @@ public class YoutubeMixPlaylistExtractorTest {
} }
@Test @Test
public void getStreamCount() { void getStreamCount() {
assertEquals(ListExtractor.ITEM_COUNT_INFINITE, extractor.getStreamCount()); assertEquals(ListExtractor.ITEM_COUNT_INFINITE, extractor.getStreamCount());
} }
} }

View File

@ -154,7 +154,7 @@ public class YoutubePlaylistExtractorTest {
@Test @Test
public void testStreamCount() throws Exception { public void testStreamCount() throws Exception {
assertTrue(extractor.getStreamCount() > 100, "Error in the streams count"); ExtractorAsserts.assertGreater(100, extractor.getStreamCount());
} }
@Override @Override
@ -273,7 +273,7 @@ public class YoutubePlaylistExtractorTest {
@Test @Test
public void testStreamCount() throws Exception { public void testStreamCount() throws Exception {
assertTrue(extractor.getStreamCount() > 100, "Error in the streams count"); ExtractorAsserts.assertGreater(100, extractor.getStreamCount());
} }
@Override @Override
@ -377,7 +377,7 @@ public class YoutubePlaylistExtractorTest {
@Test @Test
public void testStreamCount() throws Exception { public void testStreamCount() throws Exception {
assertTrue(extractor.getStreamCount() > 40, "Error in the streams count"); ExtractorAsserts.assertGreater(40, extractor.getStreamCount());
} }
@Override @Override