Search in sources :

Example 6 with Triangle

use of org.jpox.samples.interfaces.Triangle in project tests by datanucleus.

the class ApplicationIdentityTest method testNonNullPrimaryKeyFieldsAfterQuery.

/**
 * check if primary key fields are never null after loading from database
 */
public void testNonNullPrimaryKeyFieldsAfterQuery() {
    try {
        Object tid1;
        Object tid2;
        Object upkfid1;
        Object upkfid2;
        boolean retainValues;
        PersistenceManager pm = pmf.getPersistenceManager();
        try {
            retainValues = false;
            pm.currentTransaction().setRetainValues(retainValues);
            pm.currentTransaction().begin();
            Triangle t1 = new Triangle(1, "t1", 7.0, 8.0);
            Triangle t2 = new Triangle(2, "t2", 9.0, 7.0);
            pm.makePersistent(t1);
            pm.makePersistent(t2);
            pm.flush();
            UnorderedPrimaryKeyFields upkf1 = new UnorderedPrimaryKeyFields(1, "upkf1", "first1", "medium1", "last1");
            UnorderedPrimaryKeyFields upkf2 = new UnorderedPrimaryKeyFields(2, "upkf2", "first2", "medium2", "last2");
            pm.makePersistent(upkf1);
            pm.makePersistent(upkf2);
            pm.flush();
            verifyTriangleByQueryInMakePersistentTransaction(pm);
            verifyUPKFieldsByQueryInMakePersistentTransaction(pm);
            pm.currentTransaction().commit();
            assertTriangleT2AfterCommit(t2, retainValues);
            assertUPKFields2AfterCommit(upkf2, retainValues);
            tid1 = pm.getObjectId(t1);
            tid2 = pm.getObjectId(t2);
            upkfid1 = pm.getObjectId(upkf1);
            upkfid2 = pm.getObjectId(upkf2);
            // ///////////////
            // retainValues = false
            // ///////////////
            retainValues = false;
            // try 1
            verifyTriangleByQuery(pm, retainValues);
            verifyUPKFieldsByQuery(pm, retainValues);
            // do iy again
            verifyTriangleByQuery(pm, retainValues);
            verifyUPKFieldsByQuery(pm, retainValues);
            // by objectid validate=false
            verifyTriangleByGetObjectById(pm, tid1, tid2, false, retainValues);
            verifyUPKFieldsByGetObjectById(pm, upkfid1, upkfid2, false, retainValues);
            // by objectid validate=true
            verifyTriangleByGetObjectById(pm, tid1, tid2, true, retainValues);
            verifyUPKFieldsByGetObjectById(pm, upkfid1, upkfid2, true, retainValues);
            System.gc();
            // verify after garbage collector
            verifyTriangleByQuery(pm, retainValues);
            // try 1
            verifyTriangleByQuery(pm, retainValues);
            verifyUPKFieldsByQuery(pm, retainValues);
            // do iy again
            verifyTriangleByQuery(pm, retainValues);
            verifyUPKFieldsByQuery(pm, retainValues);
            // by objectid validate=false
            verifyTriangleByGetObjectById(pm, tid1, tid2, false, retainValues);
            verifyUPKFieldsByGetObjectById(pm, upkfid1, upkfid2, false, retainValues);
            // by objectid validate=true
            verifyTriangleByGetObjectById(pm, tid1, tid2, true, retainValues);
            verifyUPKFieldsByGetObjectById(pm, upkfid1, upkfid2, true, retainValues);
            // ///////////////
            // retainValues = true
            // ///////////////
            retainValues = true;
            // try 1
            verifyTriangleByQuery(pm, retainValues);
            verifyUPKFieldsByQuery(pm, retainValues);
            // do iy again
            verifyTriangleByQuery(pm, retainValues);
            verifyUPKFieldsByQuery(pm, retainValues);
            // by objectid validate=false
            verifyTriangleByGetObjectById(pm, tid1, tid2, false, retainValues);
            verifyUPKFieldsByGetObjectById(pm, upkfid1, upkfid2, false, retainValues);
            // by objectid validate=true
            verifyTriangleByGetObjectById(pm, tid1, tid2, true, retainValues);
            verifyUPKFieldsByGetObjectById(pm, upkfid1, upkfid2, true, retainValues);
            System.gc();
            // verify after garbage collector
            verifyTriangleByQuery(pm, retainValues);
            // try 1
            verifyTriangleByQuery(pm, retainValues);
            verifyUPKFieldsByQuery(pm, retainValues);
            // do iy again
            verifyTriangleByQuery(pm, retainValues);
            verifyUPKFieldsByQuery(pm, retainValues);
            // by objectid validate=false
            verifyTriangleByGetObjectById(pm, tid1, tid2, false, retainValues);
            verifyUPKFieldsByGetObjectById(pm, upkfid1, upkfid2, false, retainValues);
            // by objectid validate=true
            verifyTriangleByGetObjectById(pm, tid1, tid2, true, retainValues);
            verifyUPKFieldsByGetObjectById(pm, upkfid1, upkfid2, true, retainValues);
        } finally {
            if (pm.currentTransaction().isActive()) {
                pm.currentTransaction().rollback();
            }
            pm.close();
        }
        System.gc();
        // test it all again with a new PM
        pm = pmf.getPersistenceManager();
        try {
            // ///////////////
            // retainValues = false
            // ///////////////
            retainValues = false;
            // try 1
            verifyTriangleByQuery(pm, retainValues);
            verifyUPKFieldsByQuery(pm, retainValues);
            // do iy again
            verifyTriangleByQuery(pm, retainValues);
            verifyUPKFieldsByQuery(pm, retainValues);
            // by objectid validate=false
            verifyTriangleByGetObjectById(pm, tid1, tid2, false, retainValues);
            verifyUPKFieldsByGetObjectById(pm, upkfid1, upkfid2, false, retainValues);
            // by objectid validate=true
            verifyTriangleByGetObjectById(pm, tid1, tid2, true, retainValues);
            verifyUPKFieldsByGetObjectById(pm, upkfid1, upkfid2, true, retainValues);
            System.gc();
            // verify after garbage collector
            verifyTriangleByQuery(pm, retainValues);
            // try 1
            verifyTriangleByQuery(pm, retainValues);
            verifyUPKFieldsByQuery(pm, retainValues);
            // do iy again
            verifyTriangleByQuery(pm, retainValues);
            verifyUPKFieldsByQuery(pm, retainValues);
            // by objectid validate=false
            verifyTriangleByGetObjectById(pm, tid1, tid2, false, retainValues);
            verifyUPKFieldsByGetObjectById(pm, upkfid1, upkfid2, false, retainValues);
            // by objectid validate=true
            verifyTriangleByGetObjectById(pm, tid1, tid2, true, retainValues);
            verifyUPKFieldsByGetObjectById(pm, upkfid1, upkfid2, true, retainValues);
            // ///////////////
            // retainValues = true
            // ///////////////
            retainValues = true;
            // try 1
            verifyTriangleByQuery(pm, retainValues);
            verifyUPKFieldsByQuery(pm, retainValues);
            // do iy again
            verifyTriangleByQuery(pm, retainValues);
            verifyUPKFieldsByQuery(pm, retainValues);
            // by objectid validate=false
            verifyTriangleByGetObjectById(pm, tid1, tid2, false, retainValues);
            verifyUPKFieldsByGetObjectById(pm, upkfid1, upkfid2, false, retainValues);
            // by objectid validate=true
            verifyTriangleByGetObjectById(pm, tid1, tid2, true, retainValues);
            verifyUPKFieldsByGetObjectById(pm, upkfid1, upkfid2, true, retainValues);
            System.gc();
            // verify after garbage collector
            verifyTriangleByQuery(pm, retainValues);
            // try 1
            verifyTriangleByQuery(pm, retainValues);
            verifyUPKFieldsByQuery(pm, retainValues);
            // do iy again
            verifyTriangleByQuery(pm, retainValues);
            verifyUPKFieldsByQuery(pm, retainValues);
            // by objectid validate=false
            verifyTriangleByGetObjectById(pm, tid1, tid2, false, retainValues);
            verifyUPKFieldsByGetObjectById(pm, upkfid1, upkfid2, false, retainValues);
            // by objectid validate=true
            verifyTriangleByGetObjectById(pm, tid1, tid2, true, retainValues);
            verifyUPKFieldsByGetObjectById(pm, upkfid1, upkfid2, true, retainValues);
        } finally {
            if (pm.currentTransaction().isActive()) {
                pm.currentTransaction().rollback();
            }
            pm.close();
        }
    } finally {
        clean(Triangle.class);
        clean(UnorderedPrimaryKeyFields.class);
    }
}
Also used : UnorderedPrimaryKeyFields(org.jpox.samples.identity.application.UnorderedPrimaryKeyFields) PersistenceManager(javax.jdo.PersistenceManager) Triangle(org.jpox.samples.interfaces.Triangle)

Aggregations

Triangle (org.jpox.samples.interfaces.Triangle)6 Iterator (java.util.Iterator)3 PersistenceManager (javax.jdo.PersistenceManager)3 Collection (java.util.Collection)2 Extent (javax.jdo.Extent)2 JDOUserException (javax.jdo.JDOUserException)2 Query (javax.jdo.Query)2 Transaction (javax.jdo.Transaction)2 Circle (org.jpox.samples.interfaces.Circle)2 Rectangle (org.jpox.samples.interfaces.Rectangle)2 Shape (org.jpox.samples.interfaces.Shape)2 Shape5Circle (org.jpox.samples.interfaces.Shape5Circle)2 Shape5Rectangle (org.jpox.samples.interfaces.Shape5Rectangle)2 ShapeHolder (org.jpox.samples.interfaces.ShapeHolder)2 UnorderedPrimaryKeyFields (org.jpox.samples.identity.application.UnorderedPrimaryKeyFields)1 Shape5Square (org.jpox.samples.interfaces.Shape5Square)1 Square (org.jpox.samples.interfaces.Square)1