Search in sources :

Example 1 with ExpertGroupMember

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

the class AttachDetachReplicateTest method testReplicateApplicationIdentityWith1toN.

public void testReplicateApplicationIdentityWith1toN() {
    PersistenceManagerFactory pmf1 = null;
    PersistenceManagerFactory pmf2 = null;
    try {
        // Connect to "datastore1"
        Properties props = new Properties();
        props.setProperty(PropertyNames.PROPERTY_ATTACH_SAME_DATASTORE, "false");
        props.setProperty(PropertyNames.PROPERTY_AUTOSTART_MECHANISM, "Classes");
        props.setProperty(PropertyNames.PROPERTY_AUTOSTART_CLASSNAMES, "org.jpox.samples.one_many.unidir_2.GroupMember,org.jpox.samples.one_many.unidir_2.ExpertGroupMember," + "org.jpox.samples.one_many.unidir_2.UserGroup,org.jpox.samples.one_many.unidir_2.ModeratedUserGroup");
        pmf1 = getPMF(1, props);
        // Persist data to "datastore1"
        PersistenceManager pm1 = pmf1.getPersistenceManager();
        Transaction tx1 = pm1.currentTransaction();
        Object holderId = null;
        try {
            tx1.begin();
            ModeratedUserGroup holder = new ModeratedUserGroup(1, "HolderA", "First A");
            List<GroupMember> elements = new ArrayList<GroupMember>();
            elements.add(new ExpertGroupMember(25, "ElementB1", "First B"));
            elements.add(new ExpertGroupMember(26, "ElementB2", "Second B"));
            elements.add(new GroupMember(27, "ElementB3"));
            elements.add(new GroupMember(28, "ElementB4"));
            holder.setMembers(elements);
            pm1.makePersistent(holder);
            tx1.commit();
            holderId = JDOHelper.getObjectId(holder);
        } catch (Exception e) {
            LOG.error("Exception thrown creating data in datastore 1", e);
            e.printStackTrace();
            return;
        } finally {
            if (tx1.isActive()) {
                tx1.rollback();
            }
            pm1.close();
        }
        // Detach holder from "datastore1"
        UserGroup detachedHolder = null;
        pm1 = pmf1.getPersistenceManager();
        tx1 = pm1.currentTransaction();
        try {
            pm1.getFetchPlan().setGroups(new String[] { FetchPlan.DEFAULT, FetchPlan.ALL });
            pm1.getFetchPlan().setMaxFetchDepth(-1);
            tx1.begin();
            ModeratedUserGroup holder = (ModeratedUserGroup) pm1.getObjectById(holderId);
            detachedHolder = (ModeratedUserGroup) pm1.detachCopy(holder);
            tx1.commit();
        } catch (Exception e) {
            LOG.error("Exception thrown detaching data from datastore 1", e);
            fail("Exception in detach with datastore 1 : " + e.getMessage());
        } finally {
            if (tx1.isActive()) {
                tx1.rollback();
            }
            pm1.close();
        }
        // Connect to "datastore2"
        pmf2 = getPMF(2, props);
        // Attach data to "datastore2"
        PersistenceManager pm2 = pmf2.getPersistenceManager();
        Transaction tx2 = pm2.currentTransaction();
        try {
            tx2.begin();
            pm2.makePersistent(detachedHolder);
            tx2.commit();
        } catch (Exception e) {
            LOG.error("Exception thrown attaching data to datastore 2", e);
            fail("Exception in attach with datastore 2 : " + e.getMessage());
        } finally {
            if (tx2.isActive()) {
                tx2.rollback();
            }
            pm2.close();
        }
    } catch (Exception e) {
        fail("Exception on attach to datastore 2 : " + e.getMessage());
    } finally {
        // Clean out our data
        clean(ModeratedUserGroup.class);
        clean(UserGroup.class);
        clean(ExpertGroupMember.class);
        clean(GroupMember.class);
    }
}
Also used : ModeratedUserGroup(org.jpox.samples.one_many.unidir_2.ModeratedUserGroup) ExpertGroupMember(org.jpox.samples.one_many.unidir_2.ExpertGroupMember) GroupMember(org.jpox.samples.one_many.unidir_2.GroupMember) Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) ExpertGroupMember(org.jpox.samples.one_many.unidir_2.ExpertGroupMember) ArrayList(java.util.ArrayList) PersistenceManagerFactory(javax.jdo.PersistenceManagerFactory) Properties(java.util.Properties) JDOException(javax.jdo.JDOException) JDOUserException(javax.jdo.JDOUserException) ModeratedUserGroup(org.jpox.samples.one_many.unidir_2.ModeratedUserGroup) UserGroup(org.jpox.samples.one_many.unidir_2.UserGroup)

Example 2 with ExpertGroupMember

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

the class JDOQLBasicTest method testQueryUnboundVariablesInheritance2.

public void testQueryUnboundVariablesInheritance2() {
    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("this.members.contains(mem) && mem.name == \"Craig Russell\"").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)

Example 3 with ExpertGroupMember

use of org.jpox.samples.one_many.unidir_2.ExpertGroupMember 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

PersistenceManager (javax.jdo.PersistenceManager)3 Transaction (javax.jdo.Transaction)3 ExpertGroupMember (org.jpox.samples.one_many.unidir_2.ExpertGroupMember)3 ModeratedUserGroup (org.jpox.samples.one_many.unidir_2.ModeratedUserGroup)3 UserGroup (org.jpox.samples.one_many.unidir_2.UserGroup)3 Collection (java.util.Collection)2 Query (javax.jdo.Query)2 ArrayList (java.util.ArrayList)1 Properties (java.util.Properties)1 JDOException (javax.jdo.JDOException)1 JDOUserException (javax.jdo.JDOUserException)1 PersistenceManagerFactory (javax.jdo.PersistenceManagerFactory)1 GroupMember (org.jpox.samples.one_many.unidir_2.GroupMember)1