package org.junit.jupiter.engine.support;
import java.util.function.Predicate;
import java.util.function.Supplier;
import org.junit.platform.commons.logging.Logger;
import org.junit.platform.commons.logging.LoggerFactory;
import org.junit.platform.commons.util.ReflectionUtils;
import org.junit.platform.commons.util.UnrecoverableExceptions;
import org.junit.platform.engine.support.hierarchical.ThrowableCollector;
import org.opentest4j.TestAbortedException;
class OpenTest4JAndJUnit4AwareThrowableCollector extends ThrowableCollector {
private static final Logger logger = LoggerFactory.getLogger(OpenTest4JAndJUnit4AwareThrowableCollector.class);
private static final String ASSUMPTION_VIOLATED_EXCEPTION = "org.junit.internal.AssumptionViolatedException";
private static final String COMMON_FAILURE_MESSAGE = "Failed to load class " + ASSUMPTION_VIOLATED_EXCEPTION
+ ": only supporting " + TestAbortedException.class.getName() + " for aborted execution.";
private static final Predicate<? super Throwable> abortedExecutionPredicate = createAbortedExecutionPredicate();
OpenTest4JAndJUnit4AwareThrowableCollector() {
super(abortedExecutionPredicate);
}
private static Predicate<? super Throwable> createAbortedExecutionPredicate() {
Predicate<Throwable> otaPredicate = TestAbortedException.class::isInstance;
try {
Class<?> clazz = ReflectionUtils.tryToLoadClass(ASSUMPTION_VIOLATED_EXCEPTION).get();
if (clazz != null) {
return otaPredicate.or(clazz::isInstance);
}
}
catch (Throwable throwable) {
UnrecoverableExceptions.rethrowIfUnrecoverable(throwable);
Supplier<String> messageSupplier = (throwable instanceof NoClassDefFoundError)
? () -> COMMON_FAILURE_MESSAGE + " Note that " + ASSUMPTION_VIOLATED_EXCEPTION
+ " requires that Hamcrest is on the classpath."
: () -> COMMON_FAILURE_MESSAGE;
logger.debug(throwable, messageSupplier);
}
return otaPredicate;
}
}