Search in sources :

Example 1 with TestInstance

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();
    }
}
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)

Example 2 with TestInstance

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;
            }
        }
    }
}
Also used : Field(java.lang.reflect.Field) TestInstance(org.apache.openejb.testing.TestInstance) Annotation(java.lang.annotation.Annotation) Mock(org.mockito.Mock)

Aggregations

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