io.datakernel.test.DatakernelRunner Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of datakernel-test Show documentation
Show all versions of datakernel-test Show documentation
Utils for test purposes, custom DataKernelRunner and custom JUnit rules.
The newest version!
package io.datakernel.test;
import io.datakernel.di.core.*;
import io.datakernel.di.module.Module;
import io.datakernel.di.module.Modules;
import io.datakernel.di.util.ReflectionUtils;
import io.datakernel.di.util.Types;
import io.datakernel.test.rules.LambdaStatement;
import org.junit.After;
import org.junit.Before;
import org.junit.runner.Description;
import org.junit.runner.notification.Failure;
import org.junit.runner.notification.RunNotifier;
import org.junit.runners.BlockJUnit4ClassRunner;
import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.InitializationError;
import org.junit.runners.model.MultipleFailureException;
import org.junit.runners.model.Statement;
import java.lang.annotation.Annotation;
import java.util.*;
import static io.datakernel.common.collection.CollectionUtils.union;
import static java.util.stream.Collectors.toSet;
public class DatakernelRunner extends BlockJUnit4ClassRunner {
private final Set surroundings = new HashSet<>();
private final Set staticDependencies;
private Module currentModule;
private Set currentDependencies;
protected Injector currentInjector;
public DatakernelRunner(Class> clazz) throws InitializationError {
super(clazz);
surroundings.addAll(getTestClass().getAnnotatedMethods(Before.class));
surroundings.addAll(getTestClass().getAnnotatedMethods(After.class));
staticDependencies = surroundings.stream()
.flatMap(m -> Arrays.stream(ReflectionUtils.toDependencies(clazz, m.getMethod().getParameters())))
.collect(toSet());
}
// runChild is always called before createTest
@Override
protected void runChild(FrameworkMethod method, RunNotifier notifier) {
Description description = describeChild(method);
if (isIgnored(method)) {
notifier.fireTestIgnored(description);
return;
}
try {
Class> cls = getTestClass().getJavaClass();
Set modules = new HashSet<>();
addClassModules(modules, cls); // add modules from class annotation
addMethodModules(modules, method); // add modules from current test method
for (FrameworkMethod m : surroundings) { // add modules from befores and afters
addMethodModules(modules, m);
}
currentModule = Modules.combine(modules);
currentDependencies =
Arrays.stream(ReflectionUtils.toDependencies(cls, method.getMethod().getParameters()))
.collect(toSet());
} catch (ExceptionInInitializerError e) {
Throwable cause = e.getCause();
notifier.fireTestFailure(new Failure(description, cause != null ? cause : e));
return;
} catch (Exception e) {
notifier.fireTestFailure(new Failure(description, e));
return;
}
runLeaf(methodBlock(method), description, notifier);
}
private static void addClassModules(Set modules, Class> cls) throws IllegalAccessException, InstantiationException, ExceptionInInitializerError {
while (cls != null) {
UseModules useModules = cls.getAnnotation(UseModules.class);
if (useModules != null) {
for (Class extends Module> moduleClass : useModules.value()) {
modules.add(moduleClass.newInstance());
}
}
cls = cls.getSuperclass();
}
}
private static void addMethodModules(Set modules, FrameworkMethod method) throws IllegalAccessException, InstantiationException, ExceptionInInitializerError {
UseModules useModules = method.getMethod().getAnnotation(UseModules.class);
if (useModules == null) {
return;
}
for (Class extends Module> moduleClass : useModules.value()) {
modules.add(moduleClass.newInstance());
}
}
private static final class DependencyToken {}
// createTest is always called after runChild
@Override
protected Object createTest() throws Exception {
Object instance = super.createTest();
Key © 2015 - 2025 Weber Informatics LLC | Privacy Policy