Search in sources :

Example 1 with Organisation

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

the class ApplicationIdPersistenceTest method testOptimisticVersionChecks.

/**
 * Test optimistic checking of surrogate version.
 */
public void testOptimisticVersionChecks() throws Exception {
    try {
        Object id = null;
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            Organisation o = new Organisation("DataNucleus");
            o.setDescription("The company behind this software");
            pm.makePersistent(o);
            tx.commit();
            id = pm.getObjectId(o);
        } catch (Exception e) {
            LOG.error("Exception during persist", e);
            fail("Exception thrown when running test " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        PersistenceManager pm1 = pmf.getPersistenceManager();
        Transaction tx1 = pm1.currentTransaction();
        tx1.begin();
        Organisation o1 = (Organisation) pm1.getObjectById(id);
        PersistenceManager pm2 = pmf.getPersistenceManager();
        Transaction tx2 = pm2.currentTransaction();
        tx2.begin();
        Organisation o2 = (Organisation) pm2.getObjectById(id);
        // Update o1 in tx1 and commit it
        try {
            o1.setDescription("Global dataservices company");
            tx1.commit();
        } catch (Exception e) {
            LOG.error("Exception during retrieve/update in tx1", e);
            fail("Exception thrown when running test " + e.getMessage());
        } finally {
            if (tx1.isActive()) {
                tx1.rollback();
            }
            pm1.close();
        }
        // Update o2 in tx2 and (try to) commit it
        try {
            o2.setDescription("Global dataservices company number 2");
            tx2.commit();
            fail("Should have thrown JDOOptimisticVerificationException!");
        } catch (Exception e) {
            if (e instanceof JDOOptimisticVerificationException) {
            // Expected
            } else {
                LOG.error("Incorrect exception during update in tx2", e);
                fail("Incorrect exception thrown when running test " + e.getMessage());
            }
        } finally {
            if (tx2.isActive()) {
                tx2.rollback();
            }
            pm2.close();
        }
    } finally {
        clean(Organisation.class);
    }
}
Also used : Transaction(javax.jdo.Transaction) Organisation(org.jpox.samples.models.company.Organisation) PersistenceManager(javax.jdo.PersistenceManager) JDOOptimisticVerificationException(javax.jdo.JDOOptimisticVerificationException) JDOException(javax.jdo.JDOException) JDOOptimisticVerificationException(javax.jdo.JDOOptimisticVerificationException) JDOObjectNotFoundException(javax.jdo.JDOObjectNotFoundException)

Example 2 with Organisation

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

the class ApplicationIdPersistenceTest method testSurrogateVersion.

/**
 * Test of persistence/retrieve of objects with surrogate version.
 */
public void testSurrogateVersion() {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        Object id = null;
        try {
            tx.begin();
            Organisation org1 = new Organisation("First");
            org1.setDescription("Original Description");
            pm.makePersistent(org1);
            tx.commit();
            id = JDOHelper.getObjectId(org1);
            assertEquals("Incorrect version after persist", new Long(1), JDOHelper.getVersion(org1));
        } catch (Exception e) {
            LOG.error("Exception persisting data", e);
            fail("Exception thrown persisting data " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        pmf.getDataStoreCache().evictAll();
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            Organisation org1 = (Organisation) pm.getObjectById(id);
            assertEquals("Incorrect version after getObjectById", new Long(1), JDOHelper.getVersion(org1));
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception retrieving data", e);
            fail("Exception thrown persisting data " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        pmf.getDataStoreCache().evictAll();
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            Query q = pm.newQuery(Organisation.class);
            List<Organisation> results = (List<Organisation>) q.execute();
            assertNotNull("No results from query!", results);
            assertEquals("Incorrect number of Organisation objects!", 1, results.size());
            Organisation org1 = results.iterator().next();
            assertEquals("Incorrect version after query", new Long(1), JDOHelper.getVersion(org1));
            // Provoke an update
            org1.setDescription("New Description");
            tx.commit();
            assertEquals("Incorrect version after update", new Long(2), JDOHelper.getVersion(org1));
        } catch (Exception e) {
            LOG.error("Exception retrieving data", e);
            fail("Exception thrown persisting data " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        pmf.getDataStoreCache().evictAll();
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            Organisation org1 = (Organisation) pm.getObjectById(id);
            assertEquals("Incorrect version after getObjectById", new Long(2), JDOHelper.getVersion(org1));
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception retrieving data", e);
            fail("Exception thrown persisting data " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        // Do clean up
        clean(Organisation.class);
    }
}
Also used : Transaction(javax.jdo.Transaction) Organisation(org.jpox.samples.models.company.Organisation) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) List(java.util.List) JDOException(javax.jdo.JDOException) JDOOptimisticVerificationException(javax.jdo.JDOOptimisticVerificationException) JDOObjectNotFoundException(javax.jdo.JDOObjectNotFoundException)

Example 3 with Organisation

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

the class ReachabilityTest method performOneToOneUniClass.

/**
 * Test for reachability using a 1-1 unidirectional relation between 2 classes.
 * Tests that when persisting the owner object the related object is also persisted.
 * @param optimisticTxn Whether to use optimistic txns
 */
protected void performOneToOneUniClass(boolean optimisticTxn) {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        tx.setOptimistic(optimisticTxn);
        Object qual1Id = null;
        Object org1Id = null;
        Object qual2Id = null;
        Object org2Id = null;
        try {
            // A). persist two objects without relation
            tx.begin();
            Qualification qual1 = new Qualification("ISO 2003 certificate number 34512");
            Organisation org1 = new Organisation("JPOX Consulting");
            pm.makePersistent(org1);
            pm.makePersistent(qual1);
            tx.commit();
            qual1Id = JDOHelper.getObjectId(qual1);
            org1Id = JDOHelper.getObjectId(org1);
            tx.begin();
            // B). Relate the previous objects
            qual1.setOrganisation(org1);
            // c). Create and relate two new objects
            Qualification qual2 = new Qualification("ISO 2001 certificate number 123045");
            Organisation org2 = new Organisation("JPOX Corporation");
            qual2.setOrganisation(org2);
            // Check that both are transient
            assertTrue("Object state of new Qualification is incorrect", !JDOHelper.isPersistent(qual2) && !JDOHelper.isNew(qual2) && !JDOHelper.isDirty(qual2));
            assertTrue("Object state of new Organisation is incorrect", !JDOHelper.isPersistent(org2) && !JDOHelper.isNew(org2) && !JDOHelper.isDirty(org2));
            // Persist the Qualification (so the Organisation should be persisted too)
            pm.makePersistent(qual2);
            // Check that both are persistent-new (JDO2 spec 12.6.7)
            assertTrue("Object state of newly persisted Qualification is incorrect", JDOHelper.isPersistent(qual2) && JDOHelper.isNew(qual2) && JDOHelper.isDirty(qual2));
            assertTrue("Object state of newly persisted (by reachability) Organisation is incorrect", JDOHelper.isPersistent(org2) && JDOHelper.isNew(org2) && JDOHelper.isDirty(org2));
            tx.commit();
            // Check that both are clean/hollow
            assertTrue("Object state of committed Qualification is incorrect", JDOHelper.isPersistent(qual2) && !JDOHelper.isNew(qual2) && !JDOHelper.isDirty(qual2));
            assertTrue("Object state of committed (by reachability) Organisation is incorrect", JDOHelper.isPersistent(org2) && !JDOHelper.isNew(org2) && !JDOHelper.isDirty(org2));
            qual2Id = pm.getObjectId(qual2);
            org2Id = 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 org1 = (Organisation) pm.getObjectById(org1Id);
            assertTrue("Organisation 1 is not in the datastore!", org1 != null);
            Qualification qual1 = (Qualification) pm.getObjectById(qual1Id);
            assertTrue("Qualification 1 is not in the datastore!", qual1 != null);
            Organisation org2 = (Organisation) pm.getObjectById(org2Id);
            assertTrue("Organisation 2 is not in the datastore!", org2 != null);
            Qualification qual2 = (Qualification) pm.getObjectById(qual2Id);
            assertTrue("Qualification 2 is not in the datastore!", qual2 != 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 4 with Organisation

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

the class PersistenceTest method testPersistOneToOne.

/**
 * Test the persistence/retrieval of class with 1-1 relation.
 */
public void testPersistOneToOne() {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        Object qId = null;
        Object oId = null;
        try {
            tx.begin();
            Qualification q = new Qualification("Cycling Proficiency");
            Organisation o = new Organisation("BSA");
            q.setOrganisation(o);
            pm.makePersistent(q);
            tx.commit();
            qId = JDOHelper.getObjectId(q);
            oId = JDOHelper.getObjectId(o);
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            Qualification q = (Qualification) pm.getObjectById(qId);
            assertEquals("Cycling Proficiency", q.getName());
            Organisation o = q.getOrganisation();
            assertNotNull("Organisation is null!", o);
            assertEquals("Organisation id is different", oId, JDOHelper.getObjectId(o));
            assertEquals("Organisation name is different", "BSA", o.getName());
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        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 5 with Organisation

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

the class ReachabilityTest method testOneToOneUniTemporaryStates.

/**
 * Test for temporary reachability using a 1-1 unidirectional relation between 2 classes looking
 * at the object states. See JDO2 spec 12.6.7
 * See JIRA "NUCCORE-26"
 */
public void testOneToOneUniTemporaryStates() {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        Object qualId = null;
        Object orgId = null;
        try {
            tx.setOptimistic(true);
            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));
            assertEquals("Field value of former persistent-new-deleted (now transient) object has been changed", "JPOX Corporation", org.getName());
            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();
            Query query = pm.newQuery(Organisation.class, "name == :param");
            Collection<Organisation> result = (Collection<Organisation>) query.execute("JPOX Corporation");
            Iterator<Organisation> iter = result.iterator();
            Organisation org = null;
            if (iter.hasNext()) {
                org = iter.next();
            }
            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);
            query.closeAll();
            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) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) Collection(java.util.Collection)

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