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;
}
}
}
Aggregations