Search in sources :

Example 1 with UdoDomainObject

use of org.estatio.module.base.dom.UdoDomainObject 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)

Example 2 with UdoDomainObject

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

the class UdoDomainObjectContract_jdoAnnotations_Test method searchAndTest.

@SuppressWarnings("rawtypes")
@Test
public void searchAndTest() {
    Reflections reflections = new Reflections("org.estatio.dom");
    Set<Class<? extends UdoDomainObject>> subtypes = reflections.getSubTypesOf(UdoDomainObject.class);
    for (Class<? extends UdoDomainObject> subtype : subtypes) {
        if (subtype.isAnonymousClass() || subtype.isLocalClass() || subtype.isMemberClass() || subtype.getName().endsWith("ForTesting")) {
            // skip (probably a testing class)
            continue;
        }
        if (UdoDomainObject.class == subtype || UdoDomainObject2.class == subtype) {
            // skip
            continue;
        }
        System.out.println(">>> " + subtype.getName());
        // must have a @PersistenceCapable(identityType=...) annotation
        final PersistenceCapable persistenceCapable = subtype.getAnnotation(PersistenceCapable.class);
        Assertions.assertThat(persistenceCapable).isNotNull();
        IdentityType identityType = persistenceCapable.identityType();
        Assertions.assertThat(identityType).isNotNull();
        if (identityType == IdentityType.DATASTORE) {
            // NOT mandatory to have a @DatastoreIdentity, but if does, then @DatastoreIdentity(..., column="id")
            final DatastoreIdentity datastoreIdentity = subtype.getAnnotation(DatastoreIdentity.class);
            if (datastoreIdentity != null) {
                Assertions.assertThat(datastoreIdentity.column()).isEqualTo("id");
            }
        }
        Inheritance inheritance = subtype.getAnnotation(Inheritance.class);
        if (inheritance != null && inheritance.strategy() == InheritanceStrategy.SUPERCLASS_TABLE) {
            // must NOT have a @Discriminator(..., column="discriminator")
            final Annotation[] declaredAnnotations = subtype.getDeclaredAnnotations();
            for (Annotation declaredAnnotation : declaredAnnotations) {
                if (declaredAnnotation.annotationType() == Discriminator.class) {
                    Assert.fail("Class " + subtype.getName() + " inherits from " + subtype.getSuperclass().getName() + "and has (incorrectly) been annotated with @Discriminator");
                }
            }
            // check if supertype has discriminator
            // must have a @Discriminator(..., column="discriminator") on one of its supertypes
            final Discriminator superDiscriminator = subtype.getSuperclass().getAnnotation(Discriminator.class);
            Assertions.assertThat(superDiscriminator).isNotNull();
            Assertions.assertThat(superDiscriminator.column()).isEqualTo("discriminator");
        }
        if (subtype.getSuperclass().equals(UdoDomainObject.class)) {
            // must have a @Version(..., column="version")
            final Version version = getAnnotationOfTypeOfItsSupertypes(subtype, Version.class);
            Assertions.assertThat(version).isNotNull();
            Assertions.assertThat(version.column()).isEqualTo("version");
        }
    }
}
Also used : IdentityType(javax.jdo.annotations.IdentityType) DatastoreIdentity(javax.jdo.annotations.DatastoreIdentity) UdoDomainObject(org.estatio.module.base.dom.UdoDomainObject) PersistenceCapable(javax.jdo.annotations.PersistenceCapable) Inheritance(javax.jdo.annotations.Inheritance) Annotation(java.lang.annotation.Annotation) Discriminator(javax.jdo.annotations.Discriminator) Version(javax.jdo.annotations.Version) UdoDomainObject2(org.estatio.module.base.dom.UdoDomainObject2) Reflections(org.reflections.Reflections) Test(org.junit.Test)

Aggregations

UdoDomainObject (org.estatio.module.base.dom.UdoDomainObject)2 Test (org.junit.Test)2 Reflections (org.reflections.Reflections)2 Predicate (com.google.common.base.Predicate)1 Annotation (java.lang.annotation.Annotation)1 Field (java.lang.reflect.Field)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 DatastoreIdentity (javax.jdo.annotations.DatastoreIdentity)1 Discriminator (javax.jdo.annotations.Discriminator)1 IdentityType (javax.jdo.annotations.IdentityType)1 Inheritance (javax.jdo.annotations.Inheritance)1 PersistenceCapable (javax.jdo.annotations.PersistenceCapable)1 Version (javax.jdo.annotations.Version)1 UdoDomainObject2 (org.estatio.module.base.dom.UdoDomainObject2)1 UdoDomainRepositoryAndFactory (org.estatio.module.base.dom.UdoDomainRepositoryAndFactory)1