Skip to content

Commit 434e885

Browse files
committed
Add utility methods in ExtractorAsserts to check whether a collection is empty and to test image collections
Two new methods have been added in ExtractorAsserts to check if a collection is empty: - assertNotEmpty(String, Collection<?>), checking: - the non nullity of the collection; - its non emptiness (if that's not case, an exception will be thrown using the provided message). - assertNotEmpty(Collection<?>), calling assertNotEmpty(String, Collection<?>) with null as the value of the string argument. A new one has been added to this assertion class to check the contrary: assertEmpty(Collection<?>), checking emptiness of the collection only if it is not null. Three new methods have been added in ExtractorAsserts as utility test methods for image collections: - assertContainsImageUrlInImageCollection(String, Collection<Image>), checking that: - the provided URL and image collection are not null; - the image collection contains at least one image which has the provided string value as its URL (which is a string) property. - assertContainsOnlyEquivalentImages(Collection<Image>, Collection<Image>), checking that: - both collections are not null; - they have the same size; - each image of the first collection has its equivalent in the second one. This means that the properties of an image in the first collection must be equal in an image of the second one. - assertNotOnlyContainsEquivalentImages(Collection<Image>, Collection<Image>), checking that: - both collections are not null; - one of the following conditions is met: - they have different sizes; - an image of the first collection has not its equivalent in the second one. This means that the properties of an image in the first collection must be not equal in an image of the second one. These methods will be used by services extractors tests (and default ones) to test image collections.
1 parent 5158472 commit 434e885

1 file changed

Lines changed: 63 additions & 0 deletions

File tree

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

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.net.URL;
55
import java.util.ArrayList;
66
import java.util.Arrays;
7+
import java.util.Collection;
78
import java.util.Collections;
89
import java.util.List;
910
import java.util.Set;
@@ -60,6 +61,16 @@ public static void assertNotEmpty(@Nullable String message, String stringToCheck
6061
assertFalse(stringToCheck.isEmpty(), message);
6162
}
6263

64+
public static void assertNotEmpty(@Nullable final Collection<?> collectionToCheck) {
65+
assertNotEmpty(null, collectionToCheck);
66+
}
67+
68+
public static void assertNotEmpty(@Nullable final String message,
69+
@Nullable final Collection<?> collectionToCheck) {
70+
assertNotNull(collectionToCheck);
71+
assertFalse(collectionToCheck.isEmpty(), message);
72+
}
73+
6374
public static void assertEmpty(String stringToCheck) {
6475
assertEmpty(null, stringToCheck);
6576
}
@@ -70,6 +81,12 @@ public static void assertEmpty(@Nullable String message, String stringToCheck) {
7081
}
7182
}
7283

84+
public static void assertEmpty(@Nullable final Collection<?> collectionToCheck) {
85+
if (collectionToCheck != null) {
86+
assertTrue(collectionToCheck.isEmpty());
87+
}
88+
}
89+
7390
public static void assertNotBlank(String stringToCheck) {
7491
assertNotBlank(stringToCheck, null);
7592
}
@@ -160,4 +177,50 @@ public static void assertTabsContain(@Nonnull final List<ListLinkHandler> tabs,
160177
.forEach(expectedTab -> assertTrue(tabSet.contains(expectedTab),
161178
"Missing " + expectedTab + " tab (got " + tabSet + ")"));
162179
}
180+
181+
public static void assertContainsImageUrlInImageCollection(
182+
@Nullable final String exceptedImageUrlContained,
183+
@Nullable final Collection<Image> imageCollection) {
184+
assertNotNull(exceptedImageUrlContained, "exceptedImageUrlContained is null");
185+
assertNotNull(imageCollection, "imageCollection is null");
186+
assertTrue(imageCollection.stream().anyMatch(image ->
187+
image.getUrl().equals(exceptedImageUrlContained)));
188+
}
189+
190+
public static void assertContainsOnlyEquivalentImages(
191+
@Nullable final Collection<Image> firstImageCollection,
192+
@Nullable final Collection<Image> secondImageCollection) {
193+
assertNotNull(firstImageCollection);
194+
assertNotNull(secondImageCollection);
195+
assertEquals(firstImageCollection.size(), secondImageCollection.size());
196+
197+
firstImageCollection.forEach(exceptedImage ->
198+
assertTrue(secondImageCollection.stream().anyMatch(image ->
199+
exceptedImage.getUrl().equals(image.getUrl())
200+
&& exceptedImage.getHeight() == image.getHeight()
201+
&& exceptedImage.getWidth() == image.getWidth())));
202+
}
203+
204+
public static void assertNotOnlyContainsEquivalentImages(
205+
@Nullable final Collection<Image> firstImageCollection,
206+
@Nullable final Collection<Image> secondImageCollection) {
207+
assertNotNull(firstImageCollection);
208+
assertNotNull(secondImageCollection);
209+
210+
if (secondImageCollection.size() != firstImageCollection.size()) {
211+
return;
212+
}
213+
214+
for (final Image unexpectedImage : firstImageCollection) {
215+
for (final Image image : secondImageCollection) {
216+
if (!image.getUrl().equals(unexpectedImage.getUrl())
217+
|| image.getHeight() != unexpectedImage.getHeight()
218+
|| image.getWidth() != unexpectedImage.getWidth()) {
219+
return;
220+
}
221+
}
222+
}
223+
224+
throw new AssertionError("All excepted images have an equivalent in the image list");
225+
}
163226
}

0 commit comments

Comments
 (0)