Search in sources :

Example 6 with Organisation

use of org.jpox.samples.models.company.Organisation in project tests by datanucleus.

the class ReachabilityTest method performOneToOneUniClassTemporary.

/**
 * Test for temporary reachability using a 1-1 unidirectional relation between 2 classes.
 * Tests that when persisting the owner object the initially reachable related object is NOT persisted.
 * See JDO2 spec 12.6.7
 * @param optimisticTxn Whether to use optimistic txns
 */
protected void performOneToOneUniClassTemporary(boolean optimisticTxn) {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        Object qualId = null;
        Object orgId = null;
        try {
            tx.setOptimistic(optimisticTxn);
            tx.begin();
            // Create the objects of the 1-1 uni relation
            Qualification qual = new Qualification("ISO 2001 certificate number 123045");
            Organisation org = new Organisation("JPOX Corporation");
            qual.setOrganisation(org);
            // Check that both are transient
            assertTrue("Object state of new Qualification is incorrect", !JDOHelper.isPersistent(qual) && !JDOHelper.isNew(qual) && !JDOHelper.isDirty(qual));
            assertTrue("Object state of new Organisation is incorrect", !JDOHelper.isPersistent(org) && !JDOHelper.isNew(org) && !JDOHelper.isDirty(org));
            // Persist the Qualification (so the Organisation should be persisted too)
            pm.makePersistent(qual);
            // Check that both are persistent-new (JDO2 spec 12.6.7)
            assertTrue("Object state of newly persisted Qualification is incorrect", JDOHelper.isPersistent(qual) && JDOHelper.isNew(qual) && JDOHelper.isDirty(qual));
            assertTrue("Object state of newly persisted (by reachability) Organisation is incorrect", JDOHelper.isPersistent(org) && JDOHelper.isNew(org) && JDOHelper.isDirty(org));
            Organisation org2 = new Organisation("JPOX Consulting");
            qual.setOrganisation(org2);
            // Commit
            tx.commit();
            // Check that both are clean/hollow
            assertTrue("Object state of committed Qualification is incorrect", JDOHelper.isPersistent(qual) && !JDOHelper.isNew(qual) && !JDOHelper.isDirty(qual));
            assertFalse("Object state of committed (by temp reachability) Organisation is incorrect", JDOHelper.isPersistent(org) && !JDOHelper.isNew(org) && !JDOHelper.isDirty(org));
            assertTrue("Object state of committed (by reachability) Organisation is incorrect", JDOHelper.isPersistent(org2) && !JDOHelper.isNew(org2) && !JDOHelper.isDirty(org2));
            qualId = pm.getObjectId(qual);
            orgId = pm.getObjectId(org2);
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // Check that the objects exist in the datastore
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            Organisation org = (Organisation) queryByName(pm, Organisation.class, "JPOX Corporation");
            assertTrue("Organisation is in the datastore!", org == null);
            Organisation org2 = (Organisation) pm.getObjectById(orgId);
            assertTrue("Organisation 2 is not in the datastore!", org2 != null);
            Qualification qual = (Qualification) pm.getObjectById(qualId);
            assertTrue("Qualification is not in the datastore!", qual != null);
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        // Clean out our data
        clean(Qualification.class);
        clean(Organisation.class);
    }
}
Also used : Qualification(org.jpox.samples.models.company.Qualification) Transaction(javax.jdo.Transaction) Organisation(org.jpox.samples.models.company.Organisation) PersistenceManager(javax.jdo.PersistenceManager)

Example 7 with Organisation

use of org.jpox.samples.models.company.Organisation in project tests by datanucleus.

the class ReachabilityTest method testOneToOneUniClassNewDeleted.

/**
 * Tests if reachability on commit does not try to persist an object that was reachable then deleted.
 * Uses 1-1 unidir relation.
 * Requires a JPOX extension so is outside the JDO2 spec.
 */
public void testOneToOneUniClassNewDeleted() {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        pm.setProperty(PropertyNames.PROPERTY_FLUSH_MODE, "manual");
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            Organisation org = new Organisation("JPOX Corporation");
            assertTrue("Object state of Organisation is incorrect", !JDOHelper.isPersistent(org) && !JDOHelper.isNew(org) && !JDOHelper.isDirty(org));
            Qualification qual = new Qualification("ISO 4002 Certificate 17001");
            // persist Qualification
            pm.makePersistent(qual);
            // Relate the objects so persisting Organisation by reachability
            qual.setOrganisation(org);
            pm.flush();
            // Remove the reference to the organisation and delete the organisation
            qual.setOrganisation(null);
            // Adding pm.flush(); here will make it work
            pm.deletePersistent(org);
            // Clear references and wait so that the references are GCed
            org = null;
            qual = null;
            Thread.sleep(2000);
            // now commit, should not fail but can if it gets the updates in the wrong order (since is UNIDIRECTIONAL relation)
            tx.commit();
        } catch (Exception e) {
            // This can happen if the commit processes the delete of the object before the update of the owner
            e.printStackTrace();
            fail("Exception thrown when commiting deleted object which should not have affected commit");
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        // Clear out our data
        clean(Qualification.class);
        clean(Organisation.class);
    }
}
Also used : Qualification(org.jpox.samples.models.company.Qualification) Transaction(javax.jdo.Transaction) Organisation(org.jpox.samples.models.company.Organisation) PersistenceManager(javax.jdo.PersistenceManager)

Example 8 with Organisation

use of org.jpox.samples.models.company.Organisation in project tests by datanucleus.

the class CacheTest method testSCOAndPCReuse.

/**
 * Demonstrate refreshing SCO and PC field values for a PC object returned from the L2 cache.
 */
public void testSCOAndPCReuse() {
    Properties userProps = new Properties();
    userProps.setProperty(PropertyNames.PROPERTY_CACHE_L1_TYPE, "weak");
    userProps.setProperty(PropertyNames.PROPERTY_CACHE_L2_TYPE, "weak");
    PersistenceManagerFactory cachePMF = getPMF(1, userProps);
    try {
        // Create some data
        PersistenceManager pm = cachePMF.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        Object qualId = null;
        try {
            DataStoreCache l2Cache = cachePMF.getDataStoreCache();
            l2Cache.pinAll(true, Qualification.class);
            l2Cache.pinAll(true, Organisation.class);
            l2Cache.pinAll(true, Person.class);
            tx.begin();
            Person tweety = new Person(1, "Tweety", "Pie", "tweety.pie@warnerbros.com");
            pm.makePersistent(tweety);
            Organisation org = new Organisation("The Training Company");
            pm.makePersistent(org);
            Qualification qual = new Qualification("Certified JPOX Developer");
            qual.setPerson(tweety);
            qual.setOrganisation(org);
            qual.setDate(new GregorianCalendar(2005, 8, 02).getTime());
            pm.makePersistent(qual);
            qualId = pm.getObjectId(qual);
            tx.commit();
        } catch (Exception e) {
            e.printStackTrace();
            fail("Error persisting basic data necessary to run SCO/PC field reuse test");
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        /*
             * OK, now we have a Person, an Organisation and a Qualification object in 2L cache.
             * Previously getting the Qualification object from 2L cache cleared SCO
             * and PC fields, so accessing the room/guest or any of the date
             * fields resulted in SQL query, even though they are in the cache.
             * Let's see if that changed. The following should cause no SQL
             * execution at all.
             */
        PersistenceManager pm2 = cachePMF.getPersistenceManager();
        tx = pm2.currentTransaction();
        try {
            tx.begin();
            Qualification qual = (Qualification) pm2.getObjectById(qualId);
            // these are PC fields, their primary keys are kept even in cache
            assertEquals("Person's first name is not what expected", qual.getPerson().getFirstName(), "Tweety");
            assertEquals("Person's last name is not what expected", qual.getPerson().getLastName(), "Pie");
            // this is a SCO field, value is kept even in cache
            assertEquals("From date is not what expected", qual.getDate(), new GregorianCalendar(2005, 8, 02).getTime());
            tx.commit();
        } catch (Exception e) {
            e.printStackTrace();
            fail("Error using objects");
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm2.close();
        }
        /*
             * Now we unpin all objects and clean out the cache. Then we make
             * sure, that in spite of the changes, it is still reloading fields,
             * when they are not available
             */
        PersistenceManager pm3 = cachePMF.getPersistenceManager();
        tx = pm3.currentTransaction();
        try {
            DataStoreCache l2Cache = cachePMF.getDataStoreCache();
            l2Cache.unpinAll(true, Qualification.class);
            l2Cache.unpinAll(true, Person.class);
            l2Cache.unpinAll(true, Organisation.class);
            l2Cache.evictAll();
            tx.begin();
            Qualification qual = (Qualification) pm3.getObjectById(qualId);
            assertEquals("Person's first name is not what expected", qual.getPerson().getFirstName(), "Tweety");
            assertEquals("Person's last name is not what expected", qual.getPerson().getLastName(), "Pie");
            assertEquals("From date is not what expected", qual.getDate(), new GregorianCalendar(2005, 8, 02).getTime());
            tx.commit();
        } catch (Exception e) {
            e.printStackTrace();
            fail("Error using objects");
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm3.close();
        }
    } finally {
        clean(cachePMF, Qualification.class);
        clean(cachePMF, Organisation.class);
        clean(cachePMF, Person.class);
        cachePMF.close();
    }
}
Also used : Qualification(org.jpox.samples.models.company.Qualification) Transaction(javax.jdo.Transaction) Organisation(org.jpox.samples.models.company.Organisation) PersistenceManager(javax.jdo.PersistenceManager) GregorianCalendar(java.util.GregorianCalendar) PersistenceManagerFactory(javax.jdo.PersistenceManagerFactory) JDOPersistenceManagerFactory(org.datanucleus.api.jdo.JDOPersistenceManagerFactory) Properties(java.util.Properties) JDODataStoreCache(org.datanucleus.api.jdo.JDODataStoreCache) DataStoreCache(javax.jdo.datastore.DataStoreCache) Person(org.jpox.samples.models.company.Person) JDOObjectNotFoundException(javax.jdo.JDOObjectNotFoundException)

Aggregations

PersistenceManager (javax.jdo.PersistenceManager)8 Transaction (javax.jdo.Transaction)8 Organisation (org.jpox.samples.models.company.Organisation)8 Qualification (org.jpox.samples.models.company.Qualification)6 JDOObjectNotFoundException (javax.jdo.JDOObjectNotFoundException)3 JDOException (javax.jdo.JDOException)2 JDOOptimisticVerificationException (javax.jdo.JDOOptimisticVerificationException)2 Query (javax.jdo.Query)2 Collection (java.util.Collection)1 GregorianCalendar (java.util.GregorianCalendar)1 List (java.util.List)1 Properties (java.util.Properties)1 PersistenceManagerFactory (javax.jdo.PersistenceManagerFactory)1 DataStoreCache (javax.jdo.datastore.DataStoreCache)1 JDODataStoreCache (org.datanucleus.api.jdo.JDODataStoreCache)1 JDOPersistenceManagerFactory (org.datanucleus.api.jdo.JDOPersistenceManagerFactory)1 Person (org.jpox.samples.models.company.Person)1