Search in sources :

Example 1 with UdoDomainRepositoryAndFactory

use of org.estatio.module.base.dom.UdoDomainRepositoryAndFactory in project estatio by estatio.

the class DomainServicesInjection_inject_ContractTestAbstract method searchAndTest.

@SuppressWarnings({ "rawtypes" })
@Test
public void searchAndTest() {
    Reflections reflections = new Reflections(prefix);
    final Map<Class<? extends UdoDomainRepositoryAndFactory>, UdoDomainRepositoryAndFactory<?>> domainServices = Maps.newHashMap();
    Set<Class<? extends UdoDomainRepositoryAndFactory>> subtypes = reflections.getSubTypesOf(UdoDomainRepositoryAndFactory.class);
    for (Class<? extends UdoDomainRepositoryAndFactory> subtype : subtypes) {
        if (subtype.isInterface() || subtype.isAnonymousClass() || subtype.isLocalClass() || subtype.isMemberClass()) {
            // skip (probably a testing class)
            continue;
        }
        if (!domainServices.containsKey(subtype)) {
            UdoDomainRepositoryAndFactory dos;
            try {
                dos = subtype.newInstance();
                domainServices.put(subtype, dos);
            } catch (Exception e) {
            // ignore
            }
        }
    }
    Set<Class<? extends UdoDomainObject>> domainObjectClasses = reflections.getSubTypesOf(UdoDomainObject.class);
    for (final Class<? extends UdoDomainObject> subtype : domainObjectClasses) {
        if (subtype.isAnonymousClass() || subtype.isLocalClass() || subtype.isMemberClass()) {
            // skip (probably a testing class)
            continue;
        }
        Predicate<? super Method> injectors = new Predicate<Method>() {

            public boolean apply(Method m) {
                if (!m.getName().startsWith("inject")) {
                    return false;
                }
                final Class<?>[] parameterTypes = m.getParameterTypes();
                if (parameterTypes.length != 1) {
                    return false;
                }
                if (!domainServices.containsKey(parameterTypes[0])) {
                    return false;
                }
                return true;
            }
        };
        final Set<Method> injectorMethods = ReflectionUtils.getAllMethods(subtype, injectors);
        if (injectorMethods.isEmpty()) {
            continue;
        }
        final Iterable<InjectorAndField> injectorAndFields = Iterables.transform(injectorMethods, new Function<Method, InjectorAndField>() {

            public InjectorAndField apply(Method m) {
                final UdoDomainRepositoryAndFactory<?> ds = domainServices.get(m.getParameterTypes()[0]);
                return new InjectorAndField(subtype, m, ds);
            }
        });
        try {
            final UdoDomainObject edo = subtype.newInstance();
            for (final InjectorAndField injector : injectorAndFields) {
                Predicate<? super Field> injectorFields = new Predicate<Field>() {

                    public boolean apply(Field f) {
                        return f.getType() == injector.m.getParameterTypes()[0];
                    }
                };
                final Set<Field> fields = ReflectionUtils.getAllFields(subtype, injectorFields);
                if (fields.size() != 1) {
                    continue;
                }
                injector.f = fields.iterator().next();
                injector.invokeAndAssert(edo);
            }
        } catch (Exception e) {
            continue;
        }
    }
}
Also used : UdoDomainRepositoryAndFactory(org.estatio.module.base.dom.UdoDomainRepositoryAndFactory) UdoDomainObject(org.estatio.module.base.dom.UdoDomainObject) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) Predicate(com.google.common.base.Predicate) Field(java.lang.reflect.Field) Reflections(org.reflections.Reflections) Test(org.junit.Test)

Aggregations

Predicate (com.google.common.base.Predicate)1 Field (java.lang.reflect.Field)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 UdoDomainObject (org.estatio.module.base.dom.UdoDomainObject)1 UdoDomainRepositoryAndFactory (org.estatio.module.base.dom.UdoDomainRepositoryAndFactory)1 Test (org.junit.Test)1 Reflections (org.reflections.Reflections)1