Search in sources :

Example 26 with ClassType

use of com.querydsl.codegen.utils.model.ClassType in project querydsl by querydsl.

the class ComplexEvaluationTest method ComplexPrimitiveType.

@Test
@SuppressWarnings("unchecked")
public void ComplexPrimitiveType() {
    ClassType resultType = new ClassType(TypeCategory.LIST, List.class, Types.BOOLEAN);
    StringBuilder source = new StringBuilder();
    source.append("java.util.List<Boolean> rv = new java.util.ArrayList<Boolean>();\n");
    source.append("for (boolean a : a_){\n");
    source.append("    for (boolean b : b_){\n");
    source.append("        if (a == b){\n");
    source.append("            rv.add(a);\n");
    source.append("        }\n");
    source.append("    }\n");
    source.append("}\n");
    source.append("return rv;");
    // cannot specify further than List.class
    @SuppressWarnings("rawtypes") Evaluator<List> evaluator = factory.createEvaluator(source.toString(), resultType, new String[] { "a_", "b_" }, new Type[] { resultType, resultType }, new Class<?>[] { List.class, List.class }, Collections.<String, Object>emptyMap());
    List<Boolean> a = Arrays.asList(true, true, true);
    List<Boolean> b = Arrays.asList(false, false, true);
    assertEquals(Arrays.asList(true, true, true), evaluator.evaluate(a, b));
}
Also used : List(java.util.List) ClassType(com.querydsl.codegen.utils.model.ClassType) Test(org.junit.Test)

Example 27 with ClassType

use of com.querydsl.codegen.utils.model.ClassType in project querydsl by querydsl.

the class ComplexEvaluationTest method ComplexClassLoading.

@Test
@SuppressWarnings("unchecked")
public void ComplexClassLoading() {
    ClassType resultType = new ClassType(TypeCategory.LIST, List.class, Types.OBJECTS);
    StringBuilder source = new StringBuilder();
    source.append("java.util.List<Object[]> rv = new java.util.ArrayList<Object[]>();\n");
    source.append("for (com.querydsl.codegen.utils.support.Cat cat : (java.util.List<com.querydsl.codegen.utils.support.Cat>)cat_){\n");
    source.append("for (com.querydsl.codegen.utils.support.Cat otherCat : (java.util.List<com.querydsl.codegen.utils.support.Cat>)otherCat_){\n");
    source.append("rv.add(new Object[]{cat,otherCat});\n");
    source.append("}\n");
    source.append("}\n");
    source.append("return rv;\n");
    Cat fuzzy = new Cat("fuzzy");
    Cat spot = new Cat("spot");
    Cat mittens = new Cat("mittens");
    Cat sparkles = new Cat("sparkles");
    List<Cat> a = Arrays.asList(fuzzy, spot);
    List<Cat> b = Arrays.asList(mittens, sparkles);
    ClassType argType = new ClassType(TypeCategory.LIST, List.class, new ClassType(Cat.class));
    // cannot specify further than List.class
    @SuppressWarnings("rawtypes") Evaluator<List> evaluator = factory.createEvaluator(source.toString(), resultType, new String[] { "cat_", "otherCat_" }, new Type[] { argType, argType }, new Class<?>[] { List.class, List.class }, Collections.<String, Object>emptyMap());
    Object[][] expResults = { { fuzzy, mittens }, { fuzzy, sparkles }, { spot, mittens }, { spot, sparkles } };
    List<Object[]> result = evaluator.evaluate(a, b);
    assertEquals(expResults.length, result.size());
    for (int i = 0; i < expResults.length; i++) {
        assertEquals(expResults[i].length, result.get(i).length);
        for (int j = 0; j < expResults[i].length; j++) {
            assertEquals(expResults[i][j], result.get(i)[j]);
        }
    }
}
Also used : Cat(com.querydsl.codegen.utils.support.Cat) List(java.util.List) ClassType(com.querydsl.codegen.utils.model.ClassType) Test(org.junit.Test)

Example 28 with ClassType

use of com.querydsl.codegen.utils.model.ClassType in project querydsl by querydsl.

the class ComplexEvaluationTest method ComplexDifferentConstants.

@Test
public void ComplexDifferentConstants() {
    ClassType resultType = new ClassType(TypeCategory.LIST, List.class, new ClassType(Cat.class));
    String source = new StringBuilder().append("java.util.List<com.querydsl.codegen.utils.support.Cat> rv = new java.util.ArrayList<com.querydsl.codegen.utils.support.Cat>();\n").append("for (com.querydsl.codegen.utils.support.Cat cat : (java.util.List<com.querydsl.codegen.utils.support.Cat>)cat_){\n").append("if (cat.equals(a1)) {\n").append("rv.add(cat);\n").append("}\n").append("}\n").append("return rv;\n").toString();
    List<Cat> cats = Arrays.asList(new Cat("fuzzy"), new Cat("spot"));
    String[] names = new String[] { "cat_" };
    Type[] types = new Type[] { new ClassType(TypeCategory.LIST, List.class, new ClassType(Cat.class)) };
    Class<?>[] classes = new Class<?>[] { List.class };
    // first pass
    factory.createEvaluator(source, resultType, names, types, classes, Collections.singletonMap("a1", (Object) new SuperCat("superMittens"))).evaluate(cats);
    // second pass
    factory.createEvaluator(source, resultType, names, types, classes, Collections.singletonMap("a1", (Object) new Cat("normalMittens"))).evaluate(cats);
}
Also used : Type(com.querydsl.codegen.utils.model.Type) ClassType(com.querydsl.codegen.utils.model.ClassType) Cat(com.querydsl.codegen.utils.support.Cat) List(java.util.List) ClassType(com.querydsl.codegen.utils.model.ClassType) Test(org.junit.Test)

Example 29 with ClassType

use of com.querydsl.codegen.utils.model.ClassType in project querydsl by querydsl.

the class ComplexEvaluationTest method ComplexEmbeddedClass.

@Test
@SuppressWarnings("unchecked")
public void ComplexEmbeddedClass() {
    ClassType resultType = new ClassType(TypeCategory.LIST, List.class, Types.BOOLEAN);
    StringBuilder source = new StringBuilder();
    source.append("java.util.List<Boolean> rv = new java.util.ArrayList<Boolean>();\n");
    source.append("for (boolean a : a_){\n");
    source.append("    for (boolean b : b_){\n");
    source.append("        if (a == b && new TestEmbedded().DO_RETURN()){\n");
    source.append("            rv.add(a);\n");
    source.append("        }\n");
    source.append("    }\n");
    source.append("}\n");
    source.append("return rv;} private static class TestEmbedded { public TestEmbedded() {} public boolean DO_RETURN() { return true; } ");
    // cannot specify further than List.class
    @SuppressWarnings("rawtypes") Evaluator<List> evaluator = factory.createEvaluator(source.toString(), resultType, new String[] { "a_", "b_" }, new Type[] { resultType, resultType }, new Class<?>[] { List.class, List.class }, Collections.<String, Object>emptyMap());
    List<Boolean> a = Arrays.asList(true, true, true);
    List<Boolean> b = Arrays.asList(false, false, true);
    assertEquals(Arrays.asList(true, true, true), evaluator.evaluate(a, b));
}
Also used : List(java.util.List) ClassType(com.querydsl.codegen.utils.model.ClassType) Test(org.junit.Test)

Example 30 with ClassType

use of com.querydsl.codegen.utils.model.ClassType in project querydsl by querydsl.

the class ComplexEvaluationTest method Complex.

@Test
@SuppressWarnings("unchecked")
public void Complex() {
    ClassType resultType = new ClassType(TypeCategory.LIST, List.class, Types.STRING);
    StringBuilder source = new StringBuilder();
    source.append("java.util.List<String> rv = new java.util.ArrayList<String>();\n");
    source.append("for (String a : a_){\n");
    source.append("    for (String b : b_){\n");
    source.append("        if (a.equals(b)){\n");
    source.append("            rv.add(a);\n");
    source.append("        }\n");
    source.append("    }\n");
    source.append("}\n");
    source.append("return rv;");
    // cannot specify further than List.class
    @SuppressWarnings("rawtypes") Evaluator<List> evaluator = factory.createEvaluator(source.toString(), resultType, new String[] { "a_", "b_" }, new Type[] { resultType, resultType }, new Class<?>[] { List.class, List.class }, Collections.<String, Object>emptyMap());
    List<String> a = Arrays.asList("1", "2", "3", "4");
    List<String> b = Arrays.asList("2", "4", "6", "8");
    assertEquals(Arrays.asList("2", "4"), evaluator.evaluate(a, b));
}
Also used : List(java.util.List) ClassType(com.querydsl.codegen.utils.model.ClassType) Test(org.junit.Test)

Aggregations

ClassType (com.querydsl.codegen.utils.model.ClassType)35 Test (org.junit.Test)24 SimpleType (com.querydsl.codegen.utils.model.SimpleType)18 Type (com.querydsl.codegen.utils.model.Type)14 List (java.util.List)7 JavaWriter (com.querydsl.codegen.utils.JavaWriter)6 StringWriter (java.io.StringWriter)6 Parameter (com.querydsl.codegen.utils.model.Parameter)4 TypeCategory (com.querydsl.codegen.utils.model.TypeCategory)3 PrimaryKeyData (com.querydsl.sql.codegen.support.PrimaryKeyData)3 ArrayList (java.util.ArrayList)3 Matchers.containsString (org.hamcrest.Matchers.containsString)3 Property (com.querydsl.codegen.Property)2 Cat (com.querydsl.codegen.utils.support.Cat)2 JoinType (com.querydsl.core.JoinType)2 ColumnImpl (com.querydsl.sql.ColumnImpl)2 Annotation (java.lang.annotation.Annotation)2 Before (org.junit.Before)2 EntityType (com.querydsl.codegen.EntityType)1 ScalaWriter (com.querydsl.codegen.utils.ScalaWriter)1