Search in sources :

Example 6 with Expression

use of com.querydsl.core.types.Expression in project querydsl by querydsl.

the class ExpressionTest method test.

@Test
public void test() throws Throwable {
    List<Expression<?>> exprs = new ArrayList<Expression<?>>();
    exprs.add(QAnimalTest_Animal.animal);
    exprs.add(QAnimalTest_Cat.cat);
    exprs.add(QConstructorTest_Category.category);
    exprs.add(QConstructorTest_ClassWithConstructor.classWithConstructor);
    exprs.add(QEntityTest_Entity1.entity1);
    exprs.add(QEntityTest_Entity2.entity2);
    exprs.add(QEntityTest_Entity3.entity3);
    exprs.add(QEmbeddableTest_EntityWithEmbedded.entityWithEmbedded);
    exprs.add(QGenericTest_GenericType.genericType);
    exprs.add(QInterfaceTypeTest_InterfaceType.interfaceType);
    exprs.add(QInterfaceTypeTest_InterfaceType2.interfaceType2);
    exprs.add(QInterfaceTypeTest_InterfaceType3.interfaceType3);
    exprs.add(QInterfaceTypeTest_InterfaceType4.interfaceType4);
    exprs.add(QInterfaceTypeTest_InterfaceType5.interfaceType5);
    exprs.add(QGenericTest_ItemType.itemType);
    exprs.add(QJodaTimeSupportTest_JodaTimeSupport.jodaTimeSupport);
    exprs.add(QQueryInitTest_PEntity.pEntity);
    exprs.add(QQueryInitTest_PEntity2.pEntity2);
    exprs.add(QQueryInitTest_PEntity3.pEntity3);
    exprs.add(QQueryInitTest_PEntity4.pEntity4);
    exprs.add(QQueryTypeTest_QueryTypeEntity.queryTypeEntity);
    exprs.add(QRelationTest_Reference.reference);
    exprs.add(QRelationTest_RelationType.relationType);
    exprs.add(QReservedNamesTest_ReservedNames.reservedNames);
    exprs.add(QSimpleTypesTest_SimpleTypes.simpleTypes);
    exprs.add(ConstantImpl.create("Hello World!"));
    exprs.add(ConstantImpl.create(1000));
    exprs.add(ConstantImpl.create(10L));
    exprs.add(ConstantImpl.create(true));
    exprs.add(ConstantImpl.create(false));
    Set<Expression<?>> toVisit = new HashSet<Expression<?>>();
    // all entities
    toVisit.addAll(exprs);
    // and all their direct properties
    for (Expression<?> expr : exprs) {
        for (Field field : expr.getClass().getFields()) {
            Object rv = field.get(expr);
            if (rv instanceof Expression) {
                if (rv instanceof StringExpression) {
                    StringExpression str = (StringExpression) rv;
                    toVisit.add(str.toLowerCase());
                    toVisit.add(str.charAt(0));
                    toVisit.add(str.isEmpty());
                } else if (rv instanceof BooleanExpression) {
                    BooleanExpression b = (BooleanExpression) rv;
                    toVisit.add(b.not());
                }
                toVisit.add((Expression<?>) rv);
            }
        }
    }
    Set<String> failures = new TreeSet<String>();
    for (Expression<?> expr : toVisit) {
        for (Method method : expr.getClass().getMethods()) {
            if (method.getName().equals("getParameter")) {
                continue;
            }
            if (method.getName().equals("getArg")) {
                continue;
            }
            if (method.getReturnType() != void.class && !method.getReturnType().isPrimitive()) {
                Class<?>[] types = method.getParameterTypes();
                Object[] args;
                if (types.length == 0) {
                    args = new Object[0];
                } else if (types.length == 1) {
                    if (types[0] == int.class) {
                        args = new Object[] { 1 };
                    } else if (types[0] == boolean.class) {
                        args = new Object[] { Boolean.TRUE };
                    } else {
                        continue;
                    }
                } else {
                    continue;
                }
                Object rv = method.invoke(expr, args);
                if (method.invoke(expr, args) != rv) {
                    failures.add(expr.getClass().getSimpleName() + "." + method.getName() + " is unstable");
                }
            }
        }
    }
    if (failures.size() > 0) {
        System.err.println("Got " + failures.size() + " failures\n");
    }
    for (String failure : failures) {
        System.err.println(failure);
    }
//        assertTrue("Got "+failures.size()+" failures",failures.isEmpty());
}
Also used : Method(java.lang.reflect.Method) Field(java.lang.reflect.Field) BooleanExpression(com.querydsl.core.types.dsl.BooleanExpression) BooleanExpression(com.querydsl.core.types.dsl.BooleanExpression) StringExpression(com.querydsl.core.types.dsl.StringExpression) Expression(com.querydsl.core.types.Expression) StringExpression(com.querydsl.core.types.dsl.StringExpression) Test(org.junit.Test)

Aggregations

Expression (com.querydsl.core.types.Expression)6 Test (org.junit.Test)3 FactoryExpression (com.querydsl.core.types.FactoryExpression)2 NativeSQLSerializer (com.querydsl.jpa.NativeSQLSerializer)2 Field (java.lang.reflect.Field)2 DefaultConfiguration (com.querydsl.apt.DefaultConfiguration)1 QueryEntities (com.querydsl.core.annotations.QueryEntities)1 QuerySupertype (com.querydsl.core.annotations.QuerySupertype)1 Operation (com.querydsl.core.types.Operation)1 Operator (com.querydsl.core.types.Operator)1 Path (com.querydsl.core.types.Path)1 BooleanExpression (com.querydsl.core.types.dsl.BooleanExpression)1 StringExpression (com.querydsl.core.types.dsl.StringExpression)1 StringPath (com.querydsl.core.types.dsl.StringPath)1 AbstractSQLQuery (com.querydsl.jpa.AbstractSQLQuery)1 FactoryExpressionTransformer (com.querydsl.jpa.FactoryExpressionTransformer)1 Method (java.lang.reflect.Method)1 Map (java.util.Map)1 Query (javax.persistence.Query)1 org.hibernate (org.hibernate)1