Search in sources :

Example 1 with TestResource

use of org.apache.openejb.junit.jee.resources.TestResource 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();
    }
}
Also used : Field(java.lang.reflect.Field) OpenEJBException(org.apache.openejb.OpenEJBException) TestInstance(org.apache.openejb.testing.TestInstance) Hashtable(java.util.Hashtable) TestResource(org.apache.openejb.junit.jee.resources.TestResource)

Aggregations

Field (java.lang.reflect.Field)1 Hashtable (java.util.Hashtable)1 OpenEJBException (org.apache.openejb.OpenEJBException)1 TestResource (org.apache.openejb.junit.jee.resources.TestResource)1 TestInstance (org.apache.openejb.testing.TestInstance)1