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

60 lines
1.8 KiB
Java
Raw Normal View History

2017-11-25 03:13:26 +01:00
package org.schabi.newpipe.extractor;
2018-01-04 17:21:03 +01:00
import javax.annotation.Nonnull;
2018-03-04 21:26:13 +01:00
import javax.annotation.Nullable;
2017-12-07 09:46:59 +01:00
import java.net.MalformedURLException;
import java.net.URL;
2017-11-25 03:13:26 +01:00
import java.util.List;
2018-03-04 21:26:13 +01:00
import static org.junit.Assert.*;
2018-01-04 17:21:03 +01:00
2017-11-25 03:13:26 +01:00
public class ExtractorAsserts {
public static void assertEmptyErrors(String message, List<Throwable> errors) {
2018-03-04 21:26:13 +01:00
if (!errors.isEmpty()) {
StringBuilder messageBuilder = new StringBuilder(message);
for (Throwable e : errors) {
messageBuilder.append("\n * ").append(e.getMessage());
2017-11-25 03:13:26 +01:00
}
2018-03-04 21:26:13 +01:00
messageBuilder.append(" ");
throw new AssertionError(messageBuilder.toString(), errors.get(0));
2017-11-25 03:13:26 +01:00
}
}
2017-12-07 09:46:59 +01:00
2018-01-04 17:21:03 +01:00
@Nonnull
private static URL urlFromString(String url) {
2017-12-07 09:46:59 +01:00
try {
2018-01-04 17:21:03 +01:00
return new URL(url);
2017-12-07 09:46:59 +01:00
} catch (MalformedURLException e) {
2018-03-04 21:26:13 +01:00
throw new AssertionError("Invalid url: " + "\"" + url + "\"", e);
2017-12-07 09:46:59 +01:00
}
}
2018-01-04 17:21:03 +01:00
public static void assertIsValidUrl(String url) {
urlFromString(url);
}
public static void assertIsSecureUrl(String urlToCheck) {
URL url = urlFromString(urlToCheck);
assertEquals("Protocol of URL is not secure", "https", url.getProtocol());
}
2018-03-04 21:26:13 +01:00
public static void assertNotEmpty(String stringToCheck) {
assertNotEmpty(null, stringToCheck);
}
public static void assertNotEmpty(@Nullable String message, String stringToCheck) {
assertNotNull(message, stringToCheck);
assertFalse(message, stringToCheck.isEmpty());
}
public static void assertEmpty(String stringToCheck) {
assertEmpty(null, stringToCheck);
}
public static void assertEmpty(@Nullable String message, String stringToCheck) {
if (stringToCheck != null) {
assertTrue(message, stringToCheck.isEmpty());
}
}
2017-11-25 03:13:26 +01:00
}