use of org.apache.openejb.testing.TestInstance in project tomee by apache.
the class InjectStatement method evaluate.
@Override
public void evaluate() throws Throwable {
if (startingStatement != null) {
Class<?> clazz = this.clazz;
while (!Object.class.equals(clazz)) {
for (final Field field : clazz.getDeclaredFields()) {
final TestResource resource = field.getAnnotation(TestResource.class);
if (resource != null) {
if (Context.class.isAssignableFrom(field.getType())) {
field.setAccessible(true);
field.set(Modifier.isStatic(field.getModifiers()) ? null : test, startingStatement.getContainer().getContext());
} else if (Hashtable.class.isAssignableFrom(field.getType())) {
field.setAccessible(true);
field.set(Modifier.isStatic(field.getModifiers()) ? null : test, startingStatement.getProperties());
} else if (EJBContainer.class.isAssignableFrom(field.getType())) {
field.setAccessible(true);
field.set(Modifier.isStatic(field.getModifiers()) ? null : test, startingStatement.getContainer());
} else {
throw new OpenEJBException("can't inject field '" + field.getName() + "'");
}
}
}
clazz = clazz.getSuperclass();
}
}
if (test != null) {
SystemInstance.get().setComponent(TestInstance.class, new TestInstance(test.getClass(), test));
// force eager init (MockitoInjector initialize eveything in its constructor)
SystemInstance.get().getComponent(FallbackPropertyInjector.class);
Injector.inject(test);
}
if (statement != null) {
statement.evaluate();
}
}
use of org.apache.openejb.testing.TestInstance in project tomee by apache.
the class MockRegistry method ensureInit.
private static void ensureInit() {
if (!initialized) {
synchronized (MockRegistry.class) {
if (!initialized) {
final TestInstance instance = SystemInstance.get().getComponent(TestInstance.class);
if (instance != null) {
Class<?> current = instance.getTestClass();
while (!current.equals(Object.class)) {
for (Field f : current.getDeclaredFields()) {
for (Annotation annotation : f.getAnnotations()) {
if (annotation.annotationType().getName().startsWith("org.mockito.")) {
final boolean acc = f.isAccessible();
try {
f.setAccessible(true);
final Object mockInstance = f.get(instance.getInstance());
if (Mock.class.equals(annotation.annotationType())) {
final Mock mock = (Mock) annotation;
if (!"".equals(mock.name())) {
mockInstancesByName.put(mock.name(), mockInstance);
} else {
mockInstancesByType.put(f.getType(), mockInstance);
}
} else {
mockInstancesByType.put(f.getType(), mockInstance);
}
} catch (IllegalAccessException e) {
// no-op
} finally {
f.setAccessible(acc);
}
break;
}
}
}
current = current.getSuperclass();
}
}
initialized = true;
}
}
}
}
Aggregations