mockit.internal.injection.TestedClassInstantiations Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jmockit Show documentation
Show all versions of jmockit Show documentation
JMockit is a Java toolkit for automated developer testing.
It contains mocking/faking APIs and a code coverage tool, supporting both JUnit and TestNG.
The mocking APIs allow all kinds of Java code, without testability restrictions, to be tested
in isolation from selected dependencies.
/*
* Copyright (c) 2006 Rogério Liesenfeld
* This file is subject to the terms of the MIT license (see LICENSE.txt).
*/
package mockit.internal.injection;
import java.lang.annotation.*;
import java.lang.reflect.*;
import java.util.*;
import javax.annotation.*;
import mockit.*;
import mockit.internal.expectations.mocking.*;
public final class TestedClassInstantiations
{
@Nonnull private final List testedFields;
@Nonnull private final List injectableFields;
@Nonnull private final InjectionState injectionState;
public TestedClassInstantiations()
{
testedFields = new LinkedList();
injectableFields = new ArrayList();
injectionState = new InjectionState();
}
public boolean findTestedAndInjectableFields(@Nonnull Class> testClass)
{
findAllTestedAndInjectableFieldsInTestClassHierarchy(testClass);
return !testedFields.isEmpty();
}
private void findAllTestedAndInjectableFieldsInTestClassHierarchy(@Nonnull Class> testClass)
{
Class> superclass = testClass.getSuperclass();
if (superclass.getClassLoader() != null) {
findAllTestedAndInjectableFieldsInTestClassHierarchy(superclass);
}
for (Field field : testClass.getDeclaredFields()) {
addAsTestedOrInjectableFieldIfApplicable(field);
}
}
private void addAsTestedOrInjectableFieldIfApplicable(@Nonnull Field fieldFromTestClass)
{
for (Annotation fieldAnnotation : fieldFromTestClass.getDeclaredAnnotations()) {
Tested testedMetadata = getTestedAnnotationIfPresent(fieldAnnotation);
if (testedMetadata != null) {
TestedField testedField = new TestedField(injectionState, fieldFromTestClass, testedMetadata);
testedFields.add(testedField);
break;
}
else if (fieldAnnotation instanceof Injectable) {
InjectionPointProvider mockedType = new MockedType(fieldFromTestClass);
injectableFields.add(mockedType);
break;
}
}
}
@Nullable
private static Tested getTestedAnnotationIfPresent(@Nonnull Annotation fieldAnnotation)
{
if (fieldAnnotation instanceof Tested) {
return (Tested) fieldAnnotation;
}
return fieldAnnotation.annotationType().getAnnotation(Tested.class);
}
public void assignNewInstancesToTestedFields(@Nonnull Object testClassInstance, boolean beforeSetup)
{
injectionState.buildListsOfInjectables(testClassInstance, injectableFields);
for (TestedField testedField : testedFields) {
if (!beforeSetup || testedField.isAvailableDuringSetup()) {
try {
testedField.instantiateWithInjectableValues(testClassInstance);
}
finally {
injectionState.resetConsumedInjectables();
}
}
}
}
public void clearTestedFields()
{
injectionState.lifecycleMethods.executeTerminationMethodsIfAny();
for (TestedField testedField : testedFields) {
testedField.clearIfAutomaticCreation();
}
}
@Nonnull
public BeanExporter getBeanExporter() { return injectionState; }
}