Search in sources :

Example 6 with Extent

use of javax.jdo.Extent in project tests by datanucleus.

the class RelationshipTest method test1toNMappedByWithInheritance2.

/**
 * Test 1-N having a subclass on the source side (1=source, N=target) with the subclass having the
 * Set field.
 */
public void test1toNMappedByWithInheritance2() {
    try {
        addClassesToSchema(new Class[] { ContainerInheritanceRoot.class, ContainerInheritanceSub.class, ElementE.class });
        // Persist some objects
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            String orgaID = "test.jpox.org";
            ContainerInheritanceSub c0 = new ContainerInheritanceSub(orgaID, "abcxyz001");
            c0.setE(new ElementE(orgaID, 0, c0));
            ElementE e1 = new ElementE(orgaID, 1, c0);
            e1.setCollectionOwner(c0);
            c0.getEs().add(e1);
            ElementE e2 = new ElementE(orgaID, 2, c0);
            e2.setCollectionOwner(c0);
            c0.getEs().add(e2);
            pm.makePersistent(c0);
            tx.commit();
        } catch (Exception e) {
            LOG.error(">> Exception thrown persisting objects : ", e);
            fail("Could not persist objects! " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
        }
        pm.close();
    } finally {
        // Clean up our data
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            // Disconnect all relations
            tx.begin();
            Extent ex = pm.getExtent(ContainerInheritanceSub.class);
            Iterator containerIter = ex.iterator();
            while (containerIter.hasNext()) {
                ContainerInheritanceSub cont = (ContainerInheritanceSub) containerIter.next();
                ElementE e = cont.getE();
                cont.setE(null);
                e.setCollectionOwner(null);
                cont.getEs().clear();
            }
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        clean(ElementE.class);
        clean(ContainerInheritanceSub.class);
    }
}
Also used : ElementE(org.jpox.samples.models.inheritance_mapped_collection.ElementE) Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) Extent(javax.jdo.Extent) Iterator(java.util.Iterator) ContainerInheritanceSub(org.jpox.samples.models.inheritance_mapped_collection.ContainerInheritanceSub)

Example 7 with Extent

use of javax.jdo.Extent in project tests by datanucleus.

the class AbstractClassesTest method testAbstractWithCollection.

/**
 * Test of a complex self-referencing assembly, with collections of abstract objects
 */
public void testAbstractWithCollection() {
    Object id = null;
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            Module module = new Module();
            pm.makePersistent(module);
            tx.commit();
            id = pm.getObjectId(module);
            tx.begin();
            Extent extent = pm.getExtent(Module.class, true);
            Iterator mods = extent.iterator();
            Module mod = (Module) mods.next();
            mod.getRoot().traverse(1);
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            Module mod = (Module) pm.getObjectById(id);
            // Clears all relations
            mod.clearRoot();
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        clean(Module.class);
        clean(ComplexAssembly.class);
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) Extent(javax.jdo.Extent) Iterator(java.util.Iterator) Module(org.jpox.samples.abstractclasses.self.Module)

Example 8 with Extent

use of javax.jdo.Extent in project tests by datanucleus.

the class AttachDetachReplicateTest method testReplicateRelation_1to1_circular.

/**
 * This test creates a circular object-chain with unidirectional 1-1-relation (A.B.C.A,
 * where second A is the same as first A). After persisting it into datastore1,
 * it detaches A with FetchPlan.ALL and persists it into datastore2 using makePersistent.
 */
public void testReplicateRelation_1to1_circular() {
    PersistenceManagerFactory pmf2 = getPersistenceManagerFactory2();
    try {
        Transaction tx1 = null;
        Transaction tx2 = null;
        // Persist the object A with two 1-1-relations (A-B-C) into the first datastore.
        PersistenceManager pm1 = pmf.getPersistenceManager();
        try {
            tx1 = pm1.currentTransaction();
            tx1.begin();
            A a = new A("a");
            B b = new B("b");
            C c = new C("c");
            a.setB(b);
            b.setC(c);
            pm1.makePersistent(a);
            a.getB().getC().setA(a);
            tx1.commit();
        } finally {
            if (tx1 != null && tx1.isActive()) {
                tx1.rollback();
            }
            pm1.close();
        }
        // Detach A with FetchPlan.ALL from datastore 1.
        A detachedA = null;
        pm1 = pmf.getPersistenceManager();
        try {
            tx1 = pm1.currentTransaction();
            tx1.begin();
            try {
                pm1.getFetchPlan().setGroup(FetchPlan.ALL);
                pm1.getFetchPlan().setMaxFetchDepth(2);
                // So we have fetch-depth allowing the detach of all objects
                pm1.getFetchPlan().addGroup("includingB");
                A a = (A) pm1.getExtent(A.class).iterator().next();
                detachedA = (A) pm1.detachCopy(a);
            } catch (Exception x) {
                LOG.error("Loading instance of A from datastore 1 or detaching it with FetchPlan.ALL failed!", x);
                fail("Loading instance of A from datastore 1 or detaching it with FetchPlan.ALL failed: " + x.getMessage());
            }
            tx1.commit();
        } finally {
            if (tx1 != null && tx1.isActive()) {
                tx1.rollback();
            }
            pm1.close();
        }
        // check, whether A.b and A.b.c exist and are correct in detachedA
        try {
            if (!"a".equals(detachedA.getName()))
                fail("detachedA.name was corrupted somewhere after creation; either during makePersistent or detachCopy! Should be \"a\", but is \"" + detachedA.getName() + "\"!");
            if (!"b".equals(detachedA.getB().getName()))
                fail("detachedA.b.name was corrupted somewhere after creation; either during makePersistent or detachCopy! Should be \"b\", but is \"" + detachedA.getB().getName() + "\"!");
            if (!"c".equals(detachedA.getB().getC().getName()))
                fail("detachedA.b.c.name was corrupted somewhere after creation; either during makePersistent or detachCopy! Should be \"c\", but is \"" + detachedA.getB().getC().getName() + "\"!");
        } catch (Exception x) {
            LOG.error("Accessing object graph detached from datastore1 failed!", x);
            fail("Accessing object graph detached from datastore1 failed: " + x.getMessage());
        }
        // Store detachedA into datastore 2 using makePersistent (the object does NOT yet exist there and should be created)
        PersistenceManager pm2 = pmf2.getPersistenceManager();
        try {
            tx2 = pm2.currentTransaction();
            tx2.begin();
            try {
                pm2.makePersistent(detachedA);
            } catch (Exception x) {
                LOG.error("makePersistent with object detached from datastore1 failed on datastore2!", x);
                fail("makePersistent with object detached from datastore1 failed on datastore2: " + x.getMessage());
            }
            tx2.commit();
        } finally {
            if (tx2 != null && tx2.isActive()) {
                tx2.rollback();
            }
            pm2.close();
        }
        // check, whether A.b and A.b.c exist and have been stored correctly into datastore2.
        pm2 = pmf2.getPersistenceManager();
        try {
            tx2 = pm2.currentTransaction();
            tx2.begin();
            try {
                A a = (A) pm2.getExtent(A.class).iterator().next();
                if (!"a".equals(a.getName()))
                    fail("a.name was corrupted during makePersistent on datastore2! Should be \"a\", but is \"" + a.getName() + "\"!");
                if (!"b".equals(a.getB().getName()))
                    fail("a.b.name was corrupted during makePersistent on datastore2! Should be \"b\", but is \"" + a.getB().getName() + "\"!");
                if (!"c".equals(a.getB().getC().getName()))
                    fail("a.b.c.name was corrupted during makePersistent on datastore2! Should be \"c\", but is \"" + a.getB().getC().getName() + "\"!");
            } catch (Exception x) {
                LOG.error("Accessing datastore2 failed!", x);
                fail("Accessing datastore2 failed: " + x.getMessage());
            }
            tx2.commit();
        } finally {
            if (tx2 != null && tx2.isActive()) {
                tx2.rollback();
            }
            pm2.close();
        }
    } finally {
        // Clean out our data
        PersistenceManagerFactory[] pmfs = new PersistenceManagerFactory[] { pmf, pmf2 };
        for (int i = 0; i < pmfs.length; ++i) {
            PersistenceManagerFactory pmf = pmfs[i];
            PersistenceManager pm = pmf.getPersistenceManager();
            Transaction tx = pm.currentTransaction();
            tx.begin();
            Extent ext = pm.getExtent(A.class, false);
            Iterator it = ext.iterator();
            while (it.hasNext()) {
                A a = (A) it.next();
                a.setB(null);
            }
            tx.commit();
            tx.begin();
            ext = pm.getExtent(B.class, false);
            it = ext.iterator();
            while (it.hasNext()) {
                B b = (B) it.next();
                b.setC(null);
            }
            tx.commit();
            tx.begin();
            ext = pm.getExtent(C.class, false);
            it = ext.iterator();
            while (it.hasNext()) {
                C c = (C) it.next();
                c.setA(null);
            }
            tx.commit();
            pm.close();
            clean(pmf, A.class);
            clean(pmf, B.class);
            clean(pmf, C.class);
        }
    }
}
Also used : A(org.datanucleus.samples.detach.fetchdepth.A) B(org.datanucleus.samples.detach.fetchdepth.B) C(org.datanucleus.samples.detach.fetchdepth.C) PESSIMISTIC(org.datanucleus.tests.annotations.TransactionMode.Mode.PESSIMISTIC) Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) Extent(javax.jdo.Extent) Iterator(java.util.Iterator) PersistenceManagerFactory(javax.jdo.PersistenceManagerFactory) JDOException(javax.jdo.JDOException) JDOUserException(javax.jdo.JDOUserException)

Example 9 with Extent

use of javax.jdo.Extent in project tests by datanucleus.

the class AttachDetachReplicateTest method testReplicateSimple2.

/**
 * Another test of replication, with no relations. Just the object
 */
public void testReplicateSimple2() {
    PersistenceManagerFactory pmf2 = getPersistenceManagerFactory2();
    try {
        PersistenceManager pm1 = pmf.getPersistenceManager();
        JFireOrganisation organisation = null;
        JFireOrganisationID organisationID = JFireOrganisationID.create("datanucleus.jfire.org");
        // Persist in first DB
        Transaction tx = null;
        try {
            tx = pm1.currentTransaction();
            tx.begin();
            JFireOrganisation org1 = new JFireOrganisation(organisationID.organisationID);
            org1 = pm1.makePersistent(org1);
            // Detach it for copying
            organisation = pm1.detachCopy(org1);
            tx.commit();
        } catch (JDOException ue) {
            LOG.error("Exception thrown persisting/detaching object", ue);
            fail("Exception thrown while creating object in first datastore : " + ue.getMessage());
        } finally {
            if (tx != null && tx.isActive()) {
                tx.rollback();
            }
            pm1.close();
        }
        // Check the detached object
        if (!JDOHelper.isDetached(organisation)) {
            fail("Organisation has not been detached!");
        }
        // Copy to other DB
        PersistenceManager pm2 = pmf2.getPersistenceManager();
        tx = pm2.currentTransaction();
        try {
            tx.begin();
            pm2.makePersistent(organisation);
            tx.commit();
        } catch (JDOException ue) {
            LOG.error("Exception thrown replicating object", ue);
            fail("Exception thrown while copying object into second datastore : " + ue.getMessage());
        } finally {
            if (tx != null && tx.isActive()) {
                tx.rollback();
            }
        }
        // Check the persistence in the second datastore
        try {
            tx = pm2.currentTransaction();
            tx.begin();
            // Use Extent since PM may have just put object in cache.
            Extent e = pm2.getExtent(JFireOrganisation.class);
            Iterator iter = e.iterator();
            boolean copied = false;
            while (iter.hasNext()) {
                JFireOrganisation o = (JFireOrganisation) iter.next();
                if (pm2.getObjectId(o).equals(organisationID)) {
                    copied = true;
                    break;
                }
            }
            assertTrue("Organisation was not copied to second datastore!", copied);
            tx.commit();
        } catch (JDOException ue) {
            LOG.error("Exception thrown checking results", ue);
            fail("Exception thrown while querying object in second datastore : " + ue.getMessage());
        } finally {
            if (tx != null && tx.isActive()) {
                tx.rollback();
            }
            pm2.close();
        }
    } finally {
        PersistenceManagerFactory[] pmfs = new PersistenceManagerFactory[] { pmf, pmf2 };
        for (int i = 0; i < pmfs.length; ++i) {
            clean(pmf, JFireOrganisation.class);
        }
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) JFireOrganisation(org.datanucleus.samples.jfire.organisation.JFireOrganisation) Extent(javax.jdo.Extent) JFireOrganisationID(org.datanucleus.samples.jfire.organisation.JFireOrganisationID) Iterator(java.util.Iterator) PersistenceManagerFactory(javax.jdo.PersistenceManagerFactory) JDOException(javax.jdo.JDOException)

Example 10 with Extent

use of javax.jdo.Extent in project tests by datanucleus.

the class AttachDetachReplicateTest method testMoveAcrossDatastores_company.

/**
 * This is a complex testcase using the classes from org.jpox.samples.models.company.
 * It stores a Manager into datastore 1. This Manager has a mapped-by-Set of
 * his employees and a join-Set of his departments. Hence, this test checks for
 * the behaviour of Sets when copied from one
 */
public void testMoveAcrossDatastores_company() {
    PersistenceManagerFactory pmf2 = getPersistenceManagerFactory2();
    try {
        Transaction tx1 = null;
        Transaction tx2 = null;
        // Create a Manager with two Departments and two Employees and persist it
        // into datastore 1.
        PersistenceManager pm1 = pmf.getPersistenceManager();
        Object managerId = null;
        try {
            tx1 = pm1.currentTransaction();
            tx1.begin();
            Manager ds1_manager = new Manager(1L, "Lucifer", "Satan", "Lucifer.Satan@microsoft.hell", 33666.99f, "jsdkhf8z23");
            Employee ds1_employee1 = new Employee(9593L, "McCreevy", "Charlie", "Charlie.McCreevy@microsoft.hell", 9948.57f, "8967bjjhg", new Integer(94));
            Employee ds1_employee2 = new Employee(8723L, "Gates", "Bill", "Bill.Gates@microsoft.hell", 11835.17f, "3894lknsd", new Integer(42));
            Department ds1_department1 = new Department("Brainwashing");
            ds1_department1.setManager(ds1_manager);
            ds1_manager.addDepartment(ds1_department1);
            // TODO Change this to be an inherited type to show up an error
            Department ds1_department2 = new Department("Torture");
            ds1_department2.setManager(ds1_manager);
            ds1_manager.addDepartment(ds1_department2);
            ds1_employee1.setManager(ds1_manager);
            ds1_manager.addSubordinate(ds1_employee1);
            ds1_employee2.setManager(ds1_manager);
            ds1_manager.addSubordinate(ds1_employee2);
            try {
                pm1.makePersistent(ds1_manager);
            } catch (Exception x) {
                LOG.error("Persisting Manager with Departments and Employees into datastore1 failed!", x);
                fail("Persisting Manager with Departments and Employees into datastore1 failed: " + x.getMessage());
            }
            tx1.commit();
            managerId = JDOHelper.getObjectId(ds1_manager);
        } finally {
            if (tx1 != null && tx1.isActive()) {
                tx1.rollback();
            }
            pm1.close();
        }
        // Detach the Manager (with FetchPlan.ALL) from datastore 1.
        Manager detached_manager = null;
        pm1 = pmf.getPersistenceManager();
        try {
            tx1 = pm1.currentTransaction();
            tx1.begin();
            pm1.getFetchPlan().setGroup(FetchPlan.ALL);
            pm1.getFetchPlan().setMaxFetchDepth(-1);
            try {
                Manager ds1_manager = (Manager) pm1.getObjectById(managerId);
                detached_manager = (Manager) pm1.detachCopy(ds1_manager);
            } catch (Exception x) {
                LOG.error("Loading and detaching Manager from datastore1 failed!", x);
                fail("Loading and detaching Manager from datastore1 failed: " + x.getMessage());
            }
            tx1.commit();
        } finally {
            if (tx1 != null && tx1.isActive()) {
                tx1.rollback();
            }
            pm1.close();
        }
        // check, whether the detached data equals the original data
        testMoveAcrossDatastores_company_check("makePersistent or detachCopy (with datastore1) has corrupted data: ", detached_manager, true);
        // put the detached manager into datastore2 using makePersistent
        PersistenceManager pm2 = pmf2.getPersistenceManager();
        try {
            tx2 = pm2.currentTransaction();
            tx2.begin();
            try {
                pm2.makePersistent(detached_manager);
            } catch (Exception x) {
                LOG.error("makePersistent failed on datastore2!", x);
                fail("makePersistent failed on datastore2: " + x.getMessage());
            }
            tx2.commit();
        } finally {
            if (tx2 != null && tx2.isActive()) {
                tx2.rollback();
            }
            pm2.close();
        }
        // load the manager from datastore2 and check whether data is still correct
        pm2 = pmf2.getPersistenceManager();
        try {
            tx2 = pm2.currentTransaction();
            tx2.begin();
            Manager ds2_manager = null;
            try {
                Extent ex = pm2.getExtent(Manager.class);
                Iterator exIter = ex.iterator();
                ds2_manager = (Manager) exIter.next();
                if (exIter.hasNext()) {
                    fail("Returned more than 1 Manager object in second datastore when should only have 1");
                }
            } catch (Exception e) {
                e.printStackTrace();
                LOG.error("Loading Manager from datastore2 failed!", e);
                fail("Loading Manager from datastore2 failed: " + e.getMessage());
            }
            testMoveAcrossDatastores_company_check("makePersistent on datastore2 has corrupted data: ", ds2_manager, false);
            tx2.commit();
        } finally {
            if (tx2 != null && tx2.isActive()) {
                tx2.rollback();
            }
            pm2.close();
        }
    } finally {
        // Clean out our data
        PersistenceManagerFactory[] pmfs = new PersistenceManagerFactory[] { pmf, pmf2 };
        for (int i = 0; i < pmfs.length; ++i) {
            PersistenceManagerFactory pmf = pmfs[i];
            PersistenceManager pm = pmf.getPersistenceManager();
            Transaction tx = pm.currentTransaction();
            tx.begin();
            for (Iterator it = pm.getExtent(Employee.class, false).iterator(); it.hasNext(); ) {
                Employee e = (Employee) it.next();
                e.setManager(null);
            }
            tx.commit();
            tx.begin();
            for (Iterator it = pm.getExtent(Department.class, false).iterator(); it.hasNext(); ) {
                Department d = (Department) it.next();
                d.setManager(null);
            }
            tx.commit();
            pm.close();
            clean(pmf, Manager.class);
            clean(pmf, Employee.class);
            clean(pmf, Department.class);
        }
    }
}
Also used : PersistenceManager(javax.jdo.PersistenceManager) Extent(javax.jdo.Extent) PersistenceManagerFactory(javax.jdo.PersistenceManagerFactory) Manager(org.jpox.samples.models.company.Manager) PersistenceManager(javax.jdo.PersistenceManager) JDOException(javax.jdo.JDOException) JDOUserException(javax.jdo.JDOUserException) Department(org.jpox.samples.models.company.Department) Employee(org.jpox.samples.models.company.Employee) Transaction(javax.jdo.Transaction) Iterator(java.util.Iterator)

Aggregations

Extent (javax.jdo.Extent)72 PersistenceManager (javax.jdo.PersistenceManager)72 Transaction (javax.jdo.Transaction)70 Iterator (java.util.Iterator)62 JDOUserException (javax.jdo.JDOUserException)35 Collection (java.util.Collection)22 JDOPersistenceManager (org.datanucleus.api.jdo.JDOPersistenceManager)22 Query (javax.jdo.Query)15 Manager (org.jpox.samples.models.company.Manager)13 StoreManager (org.datanucleus.store.StoreManager)11 Method (java.lang.reflect.Method)10 JDOObjectNotFoundException (javax.jdo.JDOObjectNotFoundException)10 PersistenceManagerFactory (javax.jdo.PersistenceManagerFactory)10 Employee (org.jpox.samples.models.company.Employee)10 JDOException (javax.jdo.JDOException)9 SQLException (java.sql.SQLException)8 Department (org.jpox.samples.models.company.Department)8 InversePrimitive (org.datanucleus.samples.widget.InversePrimitive)7 Primitive (org.datanucleus.samples.widget.Primitive)6 List (java.util.List)5