Search in sources :

Example 6 with UserGroup

use of org.jpox.samples.one_many.unidir_2.UserGroup in project tests by datanucleus.

the class JDOQLEvaluatorTest method testFilterCollectionContains.

/**
 * Test of filter with collectionField.contains(element).
 */
public void testFilterCollectionContains() {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        // Create some instances to query over
        List<UserGroup> instances = new ArrayList<>();
        UserGroup grp1 = new UserGroup(101, "First Group");
        UserGroup grp2 = new UserGroup(102, "Second Group");
        UserGroup grp3 = new UserGroup(103, "Third Group");
        GroupMember mem1 = new GroupMember(201, "Donald Duck");
        GroupMember mem2 = new GroupMember(202, "Mickey Mouse");
        GroupMember mem3 = new GroupMember(203, "Minnie Mouse");
        grp1.getMembers().add(mem1);
        grp2.getMembers().add(mem1);
        grp2.getMembers().add(mem2);
        grp3.getMembers().add(mem2);
        grp3.getMembers().add(mem3);
        instances.add(grp1);
        instances.add(grp2);
        instances.add(grp3);
        // Compile the query
        JDOQuery q = (JDOQuery) pm.newQuery(UserGroup.class, "members.contains(el) && el.name == 'Minnie Mouse'");
        q.declareVariables(GroupMember.class.getName() + " el");
        Query query = q.getInternalQuery();
        ClassLoaderResolver clr = query.getExecutionContext().getClassLoaderResolver();
        JavaQueryCompiler compiler = new JDOQLCompiler(query.getExecutionContext().getNucleusContext(), clr, null, query.getCandidateClass(), null, query.getFilter(), query.getParsedImports(), query.getOrdering(), query.getResult(), query.getGrouping(), query.getHaving(), query.getExplicitParametersDeclaration(), query.getExplicitVariablesDeclaration(), null);
        QueryCompilation compilation = compiler.compile(new HashMap(), null);
        // Execute the query
        JavaQueryInMemoryEvaluator eval = new JDOQLInMemoryEvaluator(query, instances, compilation, null, clr);
        List results = (List) eval.execute(true, true, true, true, true);
        assertEquals("Number of result instances was wrong", 1, results.size());
        UserGroup grp = (UserGroup) results.get(0);
        assertEquals("Result instance has wrong name", "Third Group", grp.getName());
        tx.commit();
    } catch (Exception e) {
        LOG.info(">> Unexpected exception thrown during test", e);
        fail("Exception thrown during query execution " + e.getMessage());
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
}
Also used : JDOQLCompiler(org.datanucleus.query.compiler.JDOQLCompiler) GroupMember(org.jpox.samples.one_many.unidir_2.GroupMember) Query(org.datanucleus.store.query.Query) JDOQuery(org.datanucleus.api.jdo.JDOQuery) PersistenceManager(javax.jdo.PersistenceManager) HashMap(java.util.HashMap) JavaQueryInMemoryEvaluator(org.datanucleus.query.inmemory.JavaQueryInMemoryEvaluator) ArrayList(java.util.ArrayList) ClassLoaderResolver(org.datanucleus.ClassLoaderResolver) JDOQLInMemoryEvaluator(org.datanucleus.query.inmemory.JDOQLInMemoryEvaluator) JDOQuery(org.datanucleus.api.jdo.JDOQuery) UserGroup(org.jpox.samples.one_many.unidir_2.UserGroup) JavaQueryCompiler(org.datanucleus.query.compiler.JavaQueryCompiler) Transaction(javax.jdo.Transaction) ArrayList(java.util.ArrayList) List(java.util.List) QueryCompilation(org.datanucleus.query.compiler.QueryCompilation)

Example 7 with UserGroup

use of org.jpox.samples.one_many.unidir_2.UserGroup in project tests by datanucleus.

the class JDOQLBasicTest method testQueryUnboundVariablesInheritanceRightHandDeclared2.

public void testQueryUnboundVariablesInheritanceRightHandDeclared2() {
    try {
        ModeratedUserGroup grp1 = new ModeratedUserGroup(1, "JDO Expert Group");
        ModeratedUserGroup grp2 = new ModeratedUserGroup(1, "JPA Expert Group");
        ModeratedUserGroup grp3 = new ModeratedUserGroup(1, "J2EE Expert Group");
        ExpertGroupMember mem1 = new ExpertGroupMember(1, "Craig Russell");
        ExpertGroupMember mem2 = new ExpertGroupMember(1, "Linda De Michiel");
        ExpertGroupMember mem3 = new ExpertGroupMember(1, "Bill Shannon");
        grp1.getMembers().add(mem1);
        grp2.getMembers().add(mem2);
        grp3.getMembers().add(mem2);
        grp3.getMembers().add(mem3);
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            pm.makePersistent(grp1);
            pm.makePersistent(grp2);
            pm.makePersistent(grp3);
            tx.commit();
            tx.begin();
            Query q = pm.newQuery(pm.getExtent(UserGroup.class, true));
            Collection c = q.filter("(mem.name == \"Craig Russell\") && this.members.contains(mem)").variables("ExpertGroupMember mem").imports("import org.jpox.samples.one_many.unidir_2.ExpertGroupMember").executeList();
            assertEquals(1, c.size());
            assertEquals(((UserGroup) c.iterator().next()).getName(), "JDO Expert Group");
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        // Clean out our data
        clean(ModeratedUserGroup.class);
        clean(ExpertGroupMember.class);
    }
}
Also used : ModeratedUserGroup(org.jpox.samples.one_many.unidir_2.ModeratedUserGroup) Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) ExpertGroupMember(org.jpox.samples.one_many.unidir_2.ExpertGroupMember) Collection(java.util.Collection) ModeratedUserGroup(org.jpox.samples.one_many.unidir_2.ModeratedUserGroup) UserGroup(org.jpox.samples.one_many.unidir_2.UserGroup)

Aggregations

ArrayList (java.util.ArrayList)5 UserGroup (org.jpox.samples.one_many.unidir_2.UserGroup)5 List (java.util.List)4 PersistenceManager (javax.jdo.PersistenceManager)4 Transaction (javax.jdo.Transaction)4 EntityManager (javax.persistence.EntityManager)3 EntityTransaction (javax.persistence.EntityTransaction)3 ExpertGroupMember (org.jpox.samples.one_many.unidir_2.ExpertGroupMember)3 GroupMember (org.jpox.samples.one_many.unidir_2.GroupMember)3 ModeratedUserGroup (org.jpox.samples.one_many.unidir_2.ModeratedUserGroup)3 Collection (java.util.Collection)2 Query (javax.jdo.Query)2 ExpertGroupMember (org.datanucleus.samples.annotations.one_many.unidir_2.ExpertGroupMember)2 GroupMember (org.datanucleus.samples.annotations.one_many.unidir_2.GroupMember)2 ModeratedUserGroup (org.datanucleus.samples.annotations.one_many.unidir_2.ModeratedUserGroup)2 UserGroup (org.datanucleus.samples.annotations.one_many.unidir_2.UserGroup)2 HashMap (java.util.HashMap)1 Properties (java.util.Properties)1 JDOException (javax.jdo.JDOException)1 JDOUserException (javax.jdo.JDOUserException)1