#778 Delayed runner: add support for annotations, add validation
- Add support for dependencies identified by annotations - Add some more usage validation - Change a few test classes to use the DelayedInjectionRunner
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
package fr.xephi.authme.runner;
|
||||
|
||||
import fr.xephi.authme.ReflectionTestUtils;
|
||||
import fr.xephi.authme.initialization.Injection;
|
||||
import fr.xephi.authme.initialization.InjectionHelper;
|
||||
import org.junit.runner.notification.RunNotifier;
|
||||
@@ -11,13 +10,9 @@ import org.junit.runners.model.InitializationError;
|
||||
import org.junit.runners.model.Statement;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.mockito.internal.runners.util.FrameworkUsageValidator;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Custom JUnit runner which adds support for {@link InjectDelayed} and {@link BeforeInjecting}.
|
||||
@@ -63,7 +58,7 @@ public class DelayedInjectionRunner extends BlockJUnit4ClassRunner {
|
||||
@Override
|
||||
public void run(final RunNotifier notifier) {
|
||||
// add listener that validates framework usage at the end of each test
|
||||
notifier.addListener(new FrameworkUsageValidator(notifier));
|
||||
notifier.addListener(new DelayedInjectionRunnerValidator(notifier, getTestClass()));
|
||||
super.run(notifier);
|
||||
}
|
||||
|
||||
@@ -87,53 +82,23 @@ public class DelayedInjectionRunner extends BlockJUnit4ClassRunner {
|
||||
|
||||
List<PendingInjection> pendingInjections = new ArrayList<>(delayedFields.size());
|
||||
for (FrameworkField field : delayedFields) {
|
||||
pendingInjections.add(createPendingInjection(target, field.getField()));
|
||||
pendingInjections.add(new PendingInjection(field.getField(), getInjection(field)));
|
||||
}
|
||||
return new RunDelayedInjects(statement, pendingInjections, target);
|
||||
InjectionResolver injectionResolver = new InjectionResolver(getTestClass(), target);
|
||||
return new RunDelayedInjects(statement, pendingInjections, target, injectionResolver);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a {@link PendingInjection} for the given field's type, using the target's values.
|
||||
* Gets the injection method for the given field's type and ensures an injection method has been found.
|
||||
*
|
||||
* @param target the target to get dependencies from
|
||||
* @param field the field to prepare an injection for
|
||||
* @return the resulting object
|
||||
* @param field the field to get the injection for
|
||||
* @return the injection
|
||||
*/
|
||||
private PendingInjection createPendingInjection(Object target, Field field) {
|
||||
private static Injection<?> getInjection(FrameworkField field) {
|
||||
final Injection<?> injection = InjectionHelper.getInjection(field.getType());
|
||||
if (injection == null) {
|
||||
throw new IllegalStateException("No injection method available for field '" + field.getName() + "'");
|
||||
}
|
||||
final Object[] dependencies = fulfillDependencies(target, injection.getDependencies());
|
||||
return new PendingInjection(field, injection, dependencies);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list of all objects for the given list of dependencies, retrieved from the given
|
||||
* target's {@link @Mock} fields.
|
||||
*
|
||||
* @param target the target to get the required dependencies from
|
||||
* @param dependencies the required dependency types
|
||||
* @return the resolved dependencies
|
||||
*/
|
||||
private Object[] fulfillDependencies(Object target, Class<?>[] dependencies) {
|
||||
List<FrameworkField> availableMocks = getTestClass().getAnnotatedFields(Mock.class);
|
||||
Map<Class<?>, Object> mocksByType = new HashMap<>();
|
||||
for (FrameworkField frameworkField : availableMocks) {
|
||||
Field field = frameworkField.getField();
|
||||
Object fieldValue = ReflectionTestUtils.getFieldValue(field, target);
|
||||
mocksByType.put(field.getType(), fieldValue);
|
||||
}
|
||||
|
||||
Object[] resolvedValues = new Object[dependencies.length];
|
||||
for (int i = 0; i < dependencies.length; ++i) {
|
||||
Object o = mocksByType.get(dependencies[i]);
|
||||
if (o == null) {
|
||||
throw new IllegalStateException("No mock found for '" + dependencies[i] + "'. "
|
||||
+ "All dependencies of @InjectDelayed must be provided as @Mock fields");
|
||||
}
|
||||
resolvedValues[i] = o;
|
||||
}
|
||||
return resolvedValues;
|
||||
return injection;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
package fr.xephi.authme.runner;
|
||||
|
||||
import org.junit.runner.Description;
|
||||
import org.junit.runner.notification.Failure;
|
||||
import org.junit.runner.notification.RunListener;
|
||||
import org.junit.runner.notification.RunNotifier;
|
||||
import org.junit.runners.model.TestClass;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mockito;
|
||||
|
||||
/**
|
||||
* Validates that {@link DelayedInjectionRunner} is used as intended.
|
||||
*/
|
||||
class DelayedInjectionRunnerValidator extends RunListener {
|
||||
|
||||
private final RunNotifier notifier;
|
||||
private final TestClass testClass;
|
||||
|
||||
public DelayedInjectionRunnerValidator(RunNotifier notifier, TestClass testClass) {
|
||||
this.notifier = notifier;
|
||||
this.testClass = testClass;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void testFinished(Description description) throws Exception {
|
||||
try {
|
||||
Mockito.validateMockitoUsage();
|
||||
if (!testClass.getAnnotatedFields(InjectMocks.class).isEmpty()) {
|
||||
throw new IllegalStateException("Do not use @InjectMocks with the DelayedInjectionRunner:" +
|
||||
" use @InjectDelayed or change runner");
|
||||
}
|
||||
} catch (Throwable t) {
|
||||
notifier.fireTestFailure(new Failure(description, t));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
package fr.xephi.authme.runner;
|
||||
|
||||
import fr.xephi.authme.ReflectionTestUtils;
|
||||
import fr.xephi.authme.initialization.Injection;
|
||||
import fr.xephi.authme.initialization.InjectionHelper;
|
||||
import org.junit.runners.model.FrameworkField;
|
||||
import org.junit.runners.model.TestClass;
|
||||
import org.mockito.Mock;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Resolves the dependencies of an injection based on the provided {@link TestClass} and {@link #target target}.
|
||||
*/
|
||||
class InjectionResolver {
|
||||
|
||||
private final TestClass testClass;
|
||||
private final Object target;
|
||||
private final Map<Class<?>, Object> mocksByType;
|
||||
|
||||
public InjectionResolver(TestClass testClass, Object target) {
|
||||
this.testClass = testClass;
|
||||
this.target = target;
|
||||
this.mocksByType = gatherAvailableMocks();
|
||||
}
|
||||
|
||||
public Object instantiate(Injection<?> injection) {
|
||||
Object[] dependencies = resolveDependencies(injection);
|
||||
Object object = injection.instantiateWith(dependencies);
|
||||
executePostConstructMethod(object);
|
||||
return object;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list of all objects for the given list of dependencies, retrieved from the given
|
||||
* target's {@link Mock} fields.
|
||||
*
|
||||
* @param injection the injection whose dependencies to gather
|
||||
* @return the resolved dependencies
|
||||
*/
|
||||
private Object[] resolveDependencies(Injection<?> injection) {
|
||||
final Class<?>[] dependencies = injection.getDependencies();
|
||||
final Class<?>[] annotations = injection.getDependencyAnnotations();
|
||||
Object[] resolvedValues = new Object[dependencies.length];
|
||||
for (int i = 0; i < dependencies.length; ++i) {
|
||||
Object dependency = (annotations[i] == null)
|
||||
? resolveDependency(dependencies[i])
|
||||
: resolveAnnotation(annotations[i]);
|
||||
resolvedValues[i] = dependency;
|
||||
}
|
||||
return resolvedValues;
|
||||
}
|
||||
|
||||
private Object resolveDependency(Class<?> clazz) {
|
||||
Object o = mocksByType.get(clazz);
|
||||
if (o == null) {
|
||||
throw new IllegalStateException("No mock found for '" + clazz + "'. "
|
||||
+ "All dependencies of @InjectDelayed must be provided as @Mock fields");
|
||||
}
|
||||
return o;
|
||||
}
|
||||
|
||||
private Object resolveAnnotation(Class<?> clazz) {
|
||||
Class<? extends Annotation> annotation = (Class<? extends Annotation>) clazz;
|
||||
List<FrameworkField> matches = testClass.getAnnotatedFields(annotation);
|
||||
if (matches.isEmpty()) {
|
||||
throw new IllegalStateException("No field found with @" + annotation.getSimpleName() + " in test class,"
|
||||
+ "but a dependency in an @InjectDelayed field is using it");
|
||||
} else if (matches.size() > 1) {
|
||||
throw new IllegalStateException("You cannot have multiple fields with @" + annotation.getSimpleName());
|
||||
}
|
||||
return ReflectionTestUtils.getFieldValue(matches.get(0).getField(), target);
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes the class' PostConstruct method if available. Validates that all rules for
|
||||
* {@link javax.annotation.PostConstruct} are met.
|
||||
*
|
||||
* @param object the object whose PostConstruct method should be run, if available
|
||||
* @see InjectionHelper#getAndValidatePostConstructMethod
|
||||
*/
|
||||
private static void executePostConstructMethod(Object object) {
|
||||
Method postConstructMethod = InjectionHelper.getAndValidatePostConstructMethod(object.getClass());
|
||||
if (postConstructMethod != null) {
|
||||
ReflectionTestUtils.invokeMethod(postConstructMethod, object);
|
||||
}
|
||||
}
|
||||
|
||||
private Map<Class<?>, Object> gatherAvailableMocks() {
|
||||
List<FrameworkField> availableMocks = testClass.getAnnotatedFields(Mock.class);
|
||||
Map<Class<?>, Object> mocksByType = new HashMap<>();
|
||||
for (FrameworkField frameworkField : availableMocks) {
|
||||
Field field = frameworkField.getField();
|
||||
Object fieldValue = ReflectionTestUtils.getFieldValue(field, target);
|
||||
mocksByType.put(field.getType(), fieldValue);
|
||||
}
|
||||
return mocksByType;
|
||||
}
|
||||
}
|
||||
@@ -1,36 +1,29 @@
|
||||
package fr.xephi.authme.runner;
|
||||
|
||||
import fr.xephi.authme.ReflectionTestUtils;
|
||||
import fr.xephi.authme.initialization.Injection;
|
||||
import fr.xephi.authme.initialization.InjectionHelper;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
/**
|
||||
* Contains all necessary information to initialize a {@link InjectDelayed} field.
|
||||
* Contains an injection and the field it's for.
|
||||
*/
|
||||
class PendingInjection {
|
||||
|
||||
private Field field;
|
||||
private Object[] dependencies;
|
||||
private Injection<?> injection;
|
||||
private final Field field;
|
||||
private final Injection<?> injection;
|
||||
|
||||
public PendingInjection(Field field, Injection<?> injection, Object[] dependencies) {
|
||||
public PendingInjection(Field field, Injection<?> injection) {
|
||||
this.field = field;
|
||||
this.injection = injection;
|
||||
this.dependencies = dependencies;
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs an object with the stored injection information.
|
||||
* Returns the injection to perform.
|
||||
*
|
||||
* @return the constructed object
|
||||
* @return the injection
|
||||
*/
|
||||
public Object instantiate() {
|
||||
Object object = injection.instantiateWith(dependencies);
|
||||
executePostConstructMethod(object);
|
||||
return object;
|
||||
public Injection<?> getInjection() {
|
||||
return injection;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -42,26 +35,4 @@ class PendingInjection {
|
||||
return field;
|
||||
}
|
||||
|
||||
/**
|
||||
* Clears all fields (avoids keeping a reference to all dependencies).
|
||||
*/
|
||||
public void clearFields() {
|
||||
field = null;
|
||||
dependencies = null;
|
||||
injection = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes the class' PostConstruct method if available. Validates that all rules for
|
||||
* {@link javax.annotation.PostConstruct} are met.
|
||||
*
|
||||
* @param object the object whose PostConstruct method should be run, if available
|
||||
* @see InjectionHelper#getAndValidatePostConstructMethod
|
||||
*/
|
||||
private static void executePostConstructMethod(Object object) {
|
||||
Method postConstructMethod = InjectionHelper.getAndValidatePostConstructMethod(object.getClass());
|
||||
if (postConstructMethod != null) {
|
||||
ReflectionTestUtils.invokeMethod(postConstructMethod, object);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,21 +13,28 @@ class RunDelayedInjects extends Statement {
|
||||
|
||||
private final Statement next;
|
||||
private final Object target;
|
||||
private final List<PendingInjection> pendingInjections;
|
||||
private List<PendingInjection> pendingInjections;
|
||||
private InjectionResolver injectionResolver;
|
||||
|
||||
public RunDelayedInjects(Statement next, List<PendingInjection> pendingInjections, Object target) {
|
||||
public RunDelayedInjects(Statement next, List<PendingInjection> pendingInjections, Object target,
|
||||
InjectionResolver injectionResolver) {
|
||||
this.next = next;
|
||||
this.pendingInjections = pendingInjections;
|
||||
this.target = target;
|
||||
this.injectionResolver = injectionResolver;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void evaluate() throws Throwable {
|
||||
for (PendingInjection pendingInjection : pendingInjections) {
|
||||
Object object = pendingInjection.instantiate();
|
||||
if (ReflectionTestUtils.getFieldValue(pendingInjection.getField(), target) != null) {
|
||||
throw new IllegalStateException("Field with @InjectDelayed must be null on startup");
|
||||
}
|
||||
Object object = injectionResolver.instantiate(pendingInjection.getInjection());
|
||||
ReflectionTestUtils.setField(pendingInjection.getField(), target, object);
|
||||
pendingInjection.clearFields();
|
||||
}
|
||||
this.pendingInjections = null;
|
||||
this.injectionResolver = null;
|
||||
next.evaluate();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user