Search in sources :

Example 1 with A

use of org.datanucleus.samples.detach.fetchdepth.A 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)

Aggregations

Iterator (java.util.Iterator)1 Extent (javax.jdo.Extent)1 JDOException (javax.jdo.JDOException)1 JDOUserException (javax.jdo.JDOUserException)1 PersistenceManager (javax.jdo.PersistenceManager)1 PersistenceManagerFactory (javax.jdo.PersistenceManagerFactory)1 Transaction (javax.jdo.Transaction)1 A (org.datanucleus.samples.detach.fetchdepth.A)1 B (org.datanucleus.samples.detach.fetchdepth.B)1 C (org.datanucleus.samples.detach.fetchdepth.C)1 PESSIMISTIC (org.datanucleus.tests.annotations.TransactionMode.Mode.PESSIMISTIC)1