Skip to content

Commit 3712a66

Browse files
committed
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
1 parent 4291a90 commit 3712a66

24 files changed

Lines changed: 2048 additions & 314 deletions

extractor/src/main/java/org/schabi/newpipe/extractor/utils/Utils.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ public class Utils {
1313

1414
public static final String HTTP = "http://";
1515
public static final String HTTPS = "https://";
16+
/**
17+
* @deprecated Use {@link java.nio.charset.StandardCharsets#UTF_8} instead
18+
*/
19+
@Deprecated
1620
public static final String UTF_8 = "UTF-8";
1721
public static final String EMPTY_STRING = "";
1822
private static final Pattern M_PATTERN = Pattern.compile("(https?)?:\\/\\/m\\.");

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

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,52 @@ public static void assertEmpty(@Nullable String message, String stringToCheck) {
6464
}
6565
}
6666

67-
public static void assertAtLeast(long expected, long actual) {
68-
assertTrue(actual >= expected, actual + " is not at least " + 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);
69113
}
70114

71115
// this assumes that sorting a and b in-place is not an issue, so it's only intended for tests

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

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

3-
import org.junit.jupiter.api.Assertions;
43
import org.junit.jupiter.api.Test;
54
import org.schabi.newpipe.extractor.ExtractorAsserts;
65
import org.schabi.newpipe.extractor.InfoItemsCollector;
@@ -30,7 +29,7 @@
3029
import static org.junit.jupiter.api.Assertions.assertNotNull;
3130
import static org.junit.jupiter.api.Assertions.assertNull;
3231
import static org.junit.jupiter.api.Assertions.assertTrue;
33-
import static org.schabi.newpipe.extractor.ExtractorAsserts.assertAtLeast;
32+
import static org.schabi.newpipe.extractor.ExtractorAsserts.assertGreaterOrEqual;
3433
import static org.schabi.newpipe.extractor.ExtractorAsserts.assertEqualsOrderIndependent;
3534
import static org.schabi.newpipe.extractor.ExtractorAsserts.assertIsSecureUrl;
3635
import static org.schabi.newpipe.extractor.ExtractorAsserts.assertIsValidUrl;
@@ -174,7 +173,7 @@ public void testTimestamp() throws Exception {
174173
@Test
175174
@Override
176175
public void testViewCount() throws Exception {
177-
assertAtLeast(expectedViewCountAtLeast(), extractor().getViewCount());
176+
assertGreaterOrEqual(expectedViewCountAtLeast(), extractor().getViewCount());
178177
}
179178

180179
@Test
@@ -207,7 +206,7 @@ public void testLikeCount() throws Exception {
207206
if (expectedLikeCountAtLeast() == -1) {
208207
assertEquals(-1, extractor().getLikeCount());
209208
} else {
210-
assertAtLeast(expectedLikeCountAtLeast(), extractor().getLikeCount());
209+
assertGreaterOrEqual(expectedLikeCountAtLeast(), extractor().getLikeCount());
211210
}
212211
}
213212

@@ -217,7 +216,7 @@ public void testDislikeCount() throws Exception {
217216
if (expectedDislikeCountAtLeast() == -1) {
218217
assertEquals(-1, extractor().getDislikeCount());
219218
} else {
220-
assertAtLeast(expectedDislikeCountAtLeast(), extractor().getDislikeCount());
219+
assertGreaterOrEqual(expectedDislikeCountAtLeast(), extractor().getDislikeCount());
221220
}
222221
}
223222

extractor/src/test/java/org/schabi/newpipe/extractor/services/media_ccc/MediaCCCConferenceListExtractorTest.java

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,19 @@
22

33
import org.junit.jupiter.api.BeforeAll;
44
import org.junit.jupiter.api.Test;
5+
import org.junit.jupiter.params.ParameterizedTest;
6+
import org.junit.jupiter.params.provider.ValueSource;
57
import org.schabi.newpipe.downloader.DownloaderTestImpl;
8+
import org.schabi.newpipe.extractor.ExtractorAsserts;
9+
import org.schabi.newpipe.extractor.Info;
610
import org.schabi.newpipe.extractor.InfoItem;
711
import org.schabi.newpipe.extractor.NewPipe;
812
import org.schabi.newpipe.extractor.kiosk.KioskExtractor;
913
import org.schabi.newpipe.extractor.services.media_ccc.extractors.MediaCCCConferenceKiosk;
14+
import org.schabi.newpipe.extractor.stream.StreamInfoItem;
1015

1116
import java.util.List;
17+
import java.util.Objects;
1218

1319
import static org.junit.jupiter.api.Assertions.assertTrue;
1420
import static org.schabi.newpipe.extractor.ServiceList.MediaCCC;
@@ -29,30 +35,25 @@ public static void setUpClass() throws Exception {
2935
}
3036

3137
@Test
32-
public void getConferencesListTest() throws Exception {
33-
assertTrue(extractor.getInitialPage().getItems().size() >= 174,
34-
"returned list was to small");
38+
void getConferencesListTest() throws Exception {
39+
ExtractorAsserts.assertGreaterOrEqual(174, extractor.getInitialPage().getItems().size());
3540
}
3641

37-
@Test
38-
public void conferenceTypeTest() throws Exception {
39-
assertTrue(contains(extractor.getInitialPage().getItems(), "FrOSCon 2016"));
40-
assertTrue(contains(extractor.getInitialPage().getItems(), "ChaosWest @ 35c3"));
41-
assertTrue(contains(extractor.getInitialPage().getItems(), "CTreffOS chaOStalks"));
42-
assertTrue(contains(extractor.getInitialPage().getItems(), "Datenspuren 2015"));
43-
assertTrue(contains(extractor.getInitialPage().getItems(), "Chaos Singularity 2017"));
44-
assertTrue(contains(extractor.getInitialPage().getItems(), "SIGINT10"));
45-
assertTrue(contains(extractor.getInitialPage().getItems(), "Vintage Computing Festival Berlin 2015"));
46-
assertTrue(contains(extractor.getInitialPage().getItems(), "FIfFKon 2015"));
47-
assertTrue(contains(extractor.getInitialPage().getItems(), "33C3: trailers"));
48-
assertTrue(contains(extractor.getInitialPage().getItems(), "Blinkenlights"));
49-
}
50-
51-
private boolean contains(List<InfoItem> itemList, String name) {
52-
for (InfoItem item : itemList) {
53-
if (item.getName().equals(name))
54-
return true;
55-
}
56-
return false;
42+
@ParameterizedTest
43+
@ValueSource(strings = {
44+
"FrOSCon 2016",
45+
"ChaosWest @ 35c3",
46+
"CTreffOS chaOStalks",
47+
"Datenspuren 2015",
48+
"Chaos Singularity 2017",
49+
"SIGINT10",
50+
"Vintage Computing Festival Berlin 2015",
51+
"FIfFKon 2015",
52+
"33C3: trailers",
53+
"Blinkenlights"
54+
})
55+
void conferenceTypeTest(final String name) throws Exception {
56+
final List<InfoItem> itemList = extractor.getInitialPage().getItems();
57+
assertTrue(itemList.stream().anyMatch(item -> name.equals(item.getName())));
5758
}
5859
}

extractor/src/test/java/org/schabi/newpipe/extractor/services/peertube/PeertubeAccountExtractorTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import org.junit.jupiter.api.BeforeAll;
44
import org.junit.jupiter.api.Test;
55
import org.schabi.newpipe.downloader.DownloaderTestImpl;
6+
import org.schabi.newpipe.extractor.ExtractorAsserts;
67
import org.schabi.newpipe.extractor.NewPipe;
78
import org.schabi.newpipe.extractor.channel.ChannelExtractor;
89
import org.schabi.newpipe.extractor.exceptions.ParsingException;
@@ -101,7 +102,7 @@ public void testFeedUrl() throws ParsingException {
101102

102103
@Test
103104
public void testSubscriberCount() throws ParsingException {
104-
assertTrue(extractor.getSubscriberCount() >= 700, "Wrong subscriber count");
105+
ExtractorAsserts.assertGreaterOrEqual(700, extractor.getSubscriberCount());
105106
}
106107

107108
@Override
@@ -202,7 +203,7 @@ public void testFeedUrl() throws ParsingException {
202203

203204
@Test
204205
public void testSubscriberCount() throws ParsingException {
205-
assertTrue(extractor.getSubscriberCount() >= 100, "Wrong subscriber count");
206+
ExtractorAsserts.assertGreaterOrEqual(100, extractor.getSubscriberCount());
206207
}
207208

208209
@Override

extractor/src/test/java/org/schabi/newpipe/extractor/services/peertube/PeertubeChannelExtractorTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import org.junit.jupiter.api.BeforeAll;
44
import org.junit.jupiter.api.Test;
55
import org.schabi.newpipe.downloader.DownloaderTestImpl;
6+
import org.schabi.newpipe.extractor.ExtractorAsserts;
67
import org.schabi.newpipe.extractor.NewPipe;
78
import org.schabi.newpipe.extractor.channel.ChannelExtractor;
89
import org.schabi.newpipe.extractor.exceptions.ParsingException;
@@ -116,7 +117,7 @@ public void testFeedUrl() throws ParsingException {
116117

117118
@Test
118119
public void testSubscriberCount() throws ParsingException {
119-
assertTrue(extractor.getSubscriberCount() >= 230, "Wrong subscriber count");
120+
ExtractorAsserts.assertGreaterOrEqual(230, extractor.getSubscriberCount());
120121
}
121122

122123
@Override
@@ -233,7 +234,7 @@ public void testFeedUrl() throws ParsingException {
233234

234235
@Test
235236
public void testSubscriberCount() throws ParsingException {
236-
assertTrue(extractor.getSubscriberCount() >= 700, "Wrong subscriber count");
237+
ExtractorAsserts.assertGreaterOrEqual(700, extractor.getSubscriberCount());
237238
}
238239

239240
@Override

extractor/src/test/java/org/schabi/newpipe/extractor/services/peertube/PeertubePlaylistExtractorTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public void testGetUploaderName() throws ParsingException {
5353

5454
@Test
5555
public void testGetStreamCount() throws ParsingException {
56-
ExtractorAsserts.assertAtLeast(39, extractor.getStreamCount());
56+
ExtractorAsserts.assertGreaterOrEqual(39, extractor.getStreamCount());
5757
}
5858

5959
@Test

extractor/src/test/java/org/schabi/newpipe/extractor/services/soundcloud/SoundcloudPlaylistExtractorTest.java

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public void testBannerUrl() {
9191
}
9292

9393
@Test
94-
public void testUploaderUrl() {
94+
void testUploaderUrl() {
9595
final String uploaderUrl = extractor.getUploaderUrl();
9696
assertIsSecureUrl(uploaderUrl);
9797
ExtractorAsserts.assertContains("liluzivert", uploaderUrl);
@@ -109,11 +109,10 @@ public void testUploaderAvatarUrl() {
109109

110110
@Test
111111
public void testStreamCount() {
112-
assertTrue(extractor.getStreamCount() >= 10,
113-
"Stream count does not fit: " + extractor.getStreamCount());
112+
ExtractorAsserts.assertGreaterOrEqual(10, extractor.getStreamCount());
114113
}
115114

116-
@Override
115+
@Test
117116
public void testUploaderVerified() throws Exception {
118117
assertTrue(extractor.isUploaderVerified());
119118
}
@@ -189,7 +188,7 @@ public void testBannerUrl() {
189188
}
190189

191190
@Test
192-
public void testUploaderUrl() {
191+
void testUploaderUrl() {
193192
final String uploaderUrl = extractor.getUploaderUrl();
194193
assertIsSecureUrl(uploaderUrl);
195194
ExtractorAsserts.assertContains("micky96", uploaderUrl);
@@ -207,11 +206,10 @@ public void testUploaderAvatarUrl() {
207206

208207
@Test
209208
public void testStreamCount() {
210-
assertTrue(extractor.getStreamCount() >= 10,
211-
"Stream count does not fit: " + extractor.getStreamCount());
209+
ExtractorAsserts.assertGreaterOrEqual(10, extractor.getStreamCount());
212210
}
213211

214-
@Override
212+
@Test
215213
public void testUploaderVerified() throws Exception {
216214
assertFalse(extractor.isUploaderVerified());
217215
}
@@ -233,7 +231,7 @@ public static void setUp() throws Exception {
233231
//////////////////////////////////////////////////////////////////////////*/
234232

235233
@Test
236-
public void testGetPageInNewExtractor() throws Exception {
234+
void testGetPageInNewExtractor() throws Exception {
237235
PlaylistExtractor newExtractor = SoundCloud.getPlaylistExtractor(extractor.getUrl());
238236
defaultTestGetPageInNewExtractor(extractor, newExtractor);
239237
}
@@ -302,7 +300,7 @@ public void testBannerUrl() {
302300
}
303301

304302
@Test
305-
public void testUploaderUrl() {
303+
void testUploaderUrl() {
306304
final String uploaderUrl = extractor.getUploaderUrl();
307305
assertIsSecureUrl(uploaderUrl);
308306
ExtractorAsserts.assertContains("user350509423", uploaderUrl);
@@ -320,11 +318,10 @@ public void testUploaderAvatarUrl() {
320318

321319
@Test
322320
public void testStreamCount() {
323-
assertTrue(extractor.getStreamCount() >= 370,
324-
"Stream count does not fit: " + extractor.getStreamCount());
321+
ExtractorAsserts.assertGreaterOrEqual(370, extractor.getStreamCount());
325322
}
326323

327-
@Override
324+
@Test
328325
public void testUploaderVerified() throws Exception {
329326
assertFalse(extractor.isUploaderVerified());
330327
}
@@ -407,7 +404,7 @@ public void testBannerUrl() {
407404
}
408405

409406
@Test
410-
public void testUploaderUrl() {
407+
void testUploaderUrl() {
411408
final String uploaderUrl = extractor.getUploaderUrl();
412409
assertIsSecureUrl(uploaderUrl);
413410
ExtractorAsserts.assertContains("breezy-123", uploaderUrl);
@@ -428,7 +425,7 @@ public void testStreamCount() {
428425
assertEquals(2, extractor.getStreamCount());
429426
}
430427

431-
@Override
428+
@Test
432429
public void testUploaderVerified() throws Exception {
433430
assertFalse(extractor.isUploaderVerified());
434431
}

0 commit comments

Comments
 (0)