Search in sources :

Example 36 with Manager

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

the class PersistenceManagerTest method testRetrieve.

/**
 * test of retrieve()
 */
public void testRetrieve() throws Exception {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = null;
        Object id = null;
        // Create a Department with its Manager (1-1 relationship)
        try {
            tx = pm.currentTransaction();
            tx.begin();
            Department d = new Department("dept1");
            d.setManager(new Manager(new Random().nextLong(), "mgrFN", "mgrLN", "mgr@mgr.com", (float) 100.10, "mgrSERIAL"));
            pm.makePersistent(d);
            tx.commit();
            id = pm.getObjectId(d);
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // Try making it transient without specifying the FetchPlan
        // The Manager shouldn't become transient here since it isn't in the default "FetchPlan" for Department.
        pm = pmf.getPersistenceManager();
        Department d = null;
        try {
            tx = pm.currentTransaction();
            tx.begin();
            d = (Department) pm.getObjectById(id, true);
            // Will retrieve all fields in "d"
            pm.retrieve(d);
            pm.makeTransient(d);
            pm.makeTransient(d.getManager());
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        assertTrue("The name attribute of Department wasn't retrieved", d.getName().trim().equals("dept1"));
        // Make Department and Manager transient, using FetchPlan and check after the close of the PM.
        pm = pmf.getPersistenceManager();
        Department d2 = null;
        try {
            tx = pm.currentTransaction();
            tx.begin();
            pm.getFetchPlan().addGroup(FetchPlan.ALL);
            d2 = (Department) pm.getObjectById(id, true);
            // Will retrieve all fields in "d2"
            pm.retrieve(d2);
            // Retrieve all fields in "d2.getManager()"
            pm.retrieve(d2.getManager());
            pm.makeTransient(d2);
            pm.makeTransient(d2.getManager());
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        assertTrue("The name attribute of Department wasn't retrieved", d2.getName().trim().equals("dept1"));
        assertTrue("The Manager attribute of Department wasn't retrieved", d2.getManager() != null);
        assertTrue("The serial number attribute of Department wasnt retrieved", d2.getManager().getSerialNo() != null);
        assertTrue("The Manager attribute of Department wasn't retrieved correctly", d2.getManager().getSerialNo().trim().equals("mgrSERIAL"));
        // Make Department and Manager transient, using FetchPlan and check after end of transaction but before close of PM.
        pm = pmf.getPersistenceManager();
        try {
            tx = pm.currentTransaction();
            tx.begin();
            pm.getFetchPlan().addGroup(FetchPlan.ALL);
            d2 = (Department) pm.getObjectById(id, true);
            pm.retrieve(d2);
            pm.retrieve(d2.getManager());
            pm.makeTransient(d2);
            pm.makeTransient(d2.getManager());
            tx.commit();
            assertTrue("The name attribute of Department wasn't retrieved", d2.getName().trim().equals("dept1"));
            assertTrue("The Manager attribute of Department wasn't retrieved", d2.getManager() != null);
            assertTrue("The serial number attribute of Department wasnt retrieved", d2.getManager().getSerialNo() != null);
            assertTrue("The Manager attribute of Department wasn't retrieved correctly", d2.getManager().getSerialNo().trim().equals("mgrSERIAL"));
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        clean(Manager.class);
        clean(Department.class);
    }
}
Also used : Department(org.jpox.samples.models.company.Department) Transaction(javax.jdo.Transaction) Random(java.util.Random) JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager) Manager(org.jpox.samples.models.company.Manager) StoreManager(org.datanucleus.store.StoreManager) JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager)

Example 37 with Manager

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

the class PersistenceManagerTest method testQueryPM.

/**
 * Tests that the persistence manager used to persist an object is the same
 * as returned by jdoGetPersistenceManager()
 */
public void testQueryPM() {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        Manager mgr = null;
        try {
            tx.begin();
            Department d = new Department("Engineering");
            mgr = new Manager(0, FIRSTNAME[0], LASTNAME[0], EMAIL[0], EMP_SALARY[0], EMP_SERIAL[0]);
            mgr.addDepartment(d);
            pm.makePersistent(mgr);
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
                pm.close();
                fail("Failed to persist object and commit transaction");
            }
            pm.close();
        }
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            Extent ext = pm.getExtent(Manager.class, false);
            java.util.Iterator it = ext.iterator();
            mgr = (Manager) it.next();
            assertSame(pm, JDOHelper.getPersistenceManager(mgr));
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        clean(Manager.class);
        clean(Department.class);
    }
}
Also used : Department(org.jpox.samples.models.company.Department) Transaction(javax.jdo.Transaction) Iterator(java.util.Iterator) JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager) Extent(javax.jdo.Extent) Manager(org.jpox.samples.models.company.Manager) StoreManager(org.datanucleus.store.StoreManager) JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager)

Example 38 with Manager

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

the class PersistenceManagerTest method testElementDeletionRemovesFromFKCollection.

/**
 * Test that deleting an object that is a member of a FK Collection also removes it from the Collection.
 */
public void testElementDeletionRemovesFromFKCollection() {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        Manager mgr = new Manager(0, FIRSTNAME[0], LASTNAME[0], EMAIL[0], EMP_SALARY[0], EMP_SERIAL[0]);
        Department d = new Department("Engineering");
        d.setManager(mgr);
        mgr.addDepartment(d);
        tx.begin();
        pm.makePersistent(d);
        tx.commit();
        tx.begin();
        pm.deletePersistent(d);
        tx.commit();
    } catch (Exception e) {
        LOG.error("Exception thrown when deleting member of a FK collection", e);
        fail("Exception thrown when deleting a member of a FK collection " + e.getMessage());
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
    // get a fresh PM to ensure that any results aren't coming from the cache
    pm = pmf.getPersistenceManager();
    tx = pm.currentTransaction();
    try {
        tx.begin();
        Extent ext = pm.getExtent(Manager.class, false);
        java.util.Iterator it = ext.iterator();
        assertTrue(it.hasNext());
        Manager mgr = (Manager) it.next();
        Collection c = mgr.getDepartments();
        assertTrue("Departments should have been null or empty", c == null || c.size() == 0);
        ext = pm.getExtent(Department.class, false);
        it = ext.iterator();
        assertTrue(!(it.hasNext()));
        tx.commit();
    } catch (Exception e) {
        LOG.error("Exception in test", e);
        fail("Exception in test : " + e.getMessage());
    } finally {
        if (tx.isActive())
            tx.rollback();
        pm.close();
    }
}
Also used : Department(org.jpox.samples.models.company.Department) Transaction(javax.jdo.Transaction) Iterator(java.util.Iterator) JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager) Extent(javax.jdo.Extent) Collection(java.util.Collection) Manager(org.jpox.samples.models.company.Manager) StoreManager(org.datanucleus.store.StoreManager) JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager) TransactionNotActiveException(org.datanucleus.api.jdo.exceptions.TransactionNotActiveException) JDOException(javax.jdo.JDOException) JDOUserException(javax.jdo.JDOUserException) TransactionNotReadableException(org.datanucleus.api.jdo.exceptions.TransactionNotReadableException) SQLException(java.sql.SQLException) JDOUserCallbackException(javax.jdo.JDOUserCallbackException) JDOObjectNotFoundException(javax.jdo.JDOObjectNotFoundException) TransactionNotWritableException(org.datanucleus.api.jdo.exceptions.TransactionNotWritableException) JDOUnsupportedOptionException(javax.jdo.JDOUnsupportedOptionException)

Example 39 with Manager

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

the class PersistenceManagerTest method testNormalFCOCollectionFieldPersistence2.

/**
 * Test that deleting an object that is a member of a Collection field
 * throws an exception
 */
public void testNormalFCOCollectionFieldPersistence2() {
    /*
         * If constraints aren't being used then the assumptions of this test don't hold.
         */
    if (!getConfigurationForPMF(pmf).getBooleanProperty("datanucleus.validateConstraints")) {
        return;
    }
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    Manager mgr = new Manager(0, FIRSTNAME[0], LASTNAME[0], EMAIL[0], EMP_SALARY[0], EMP_SERIAL[0]);
    Employee emp1 = new Employee(1, FIRSTNAME[1], LASTNAME[1], EMAIL[1], EMP_SALARY[1], EMP_SERIAL[1]);
    try {
        mgr.addSubordinate(emp1);
        tx.begin();
        pm.makePersistent(mgr);
        tx.commit();
        tx.begin();
        try {
            pm.deletePersistent(emp1);
            tx.commit();
            fail("Commit transaction was successful but shouldn't have been");
        } catch (javax.jdo.JDODataStoreException e) {
            if (tx.isActive())
                tx.rollback();
        }
    } finally {
        if (tx.isActive()) {
            tx.rollback();
            pm.close();
            fail("Failed to perform test");
        }
        pm.close();
    }
}
Also used : Employee(org.jpox.samples.models.company.Employee) Transaction(javax.jdo.Transaction) JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager) Manager(org.jpox.samples.models.company.Manager) StoreManager(org.datanucleus.store.StoreManager) JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager)

Example 40 with Manager

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

the class PersistenceManagerTest method testMakeTransientOwnerAndElements.

/**
 * Test of makeTransient() for owner and makeTransientAll() for the  owner elements.
 */
public void testMakeTransientOwnerAndElements() throws Exception {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            // JoinTable 1-N relationship
            // Create a Manager with subordinate number 0
            createNewManager(pm, 0);
            // Make the Manager transient with all subordinates
            tx.begin();
            Manager m = (Manager) pm.getExtent(Manager.class, true).iterator().next();
            pm.retrieveAll(m.getSubordinates());
            pm.retrieveAll(m.getDepartments());
            pm.makeTransient(m);
            pm.makeTransientAll(m.getSubordinates());
            pm.makeTransientAll(m.getDepartments());
            tx.commit();
            // Check the result
            assertNull(JDOHelper.getObjectId(m));
            // Compare the managers
            Manager m1 = queryManager(0, pm);
            tx.begin();
            Assert.assertEquals(m.getBestFriend(), m1.getBestFriend());
            Assert.assertEquals(m.getLastName(), m1.getLastName());
            Assert.assertEquals(m.getFirstName(), m1.getFirstName());
            Assert.assertEquals(m.getEmailAddress(), m1.getEmailAddress());
            Assert.assertEquals(m.getPersonNum(), m1.getPersonNum());
            Assert.assertTrue("subordinates are not the same", Manager.compareSet(m.getSubordinates(), m1.getSubordinates()));
            Assert.assertTrue("departments are not the same", Manager.compareSet(m.getDepartments(), m1.getDepartments()));
            tx.commit();
            // FK 1-N relationship
            // Create owner with 2 elements.
            tx = pm.currentTransaction();
            tx.begin();
            Farm farm = new Farm("Giles Farm");
            Animal an1 = new Animal("Dog");
            an1.setFarm(farm);
            Animal an2 = new Animal("Cow");
            an2.setFarm(farm);
            farm.addAnimal(an1);
            farm.addAnimal(an2);
            pm.makePersistent(farm);
            tx.commit();
            Object id = pm.getObjectId(farm);
            // Make the owner and its elements transient
            tx = pm.currentTransaction();
            tx.begin();
            pm.getFetchPlan().addGroup(FetchPlan.ALL);
            Farm transientFarm = (Farm) pm.getObjectById(id, true);
            pm.retrieveAll(transientFarm.getAnimals());
            pm.makeTransient(transientFarm);
            pm.makeTransientAll(transientFarm.getAnimals());
            tx.commit();
            // Check the result
            Set transientAnimals = transientFarm.getAnimals();
            assertFalse("Owner object is not transient!", JDOHelper.isPersistent(transientFarm));
            assertEquals("Number of elements in transient owner is incorrect", transientAnimals.size(), 2);
            Iterator transientAnimalsIter = transientAnimals.iterator();
            boolean dogPresent = false;
            boolean cowPresent = false;
            while (transientAnimalsIter.hasNext()) {
                Animal transientAnimal = (Animal) transientAnimalsIter.next();
                assertFalse("Element object is not transient!", JDOHelper.isPersistent(transientAnimal));
                // Check that the Animal name field is the same as that persisted
                if (transientAnimal.getName().equals("Dog")) {
                    dogPresent = true;
                } else if (transientAnimal.getName().equals("Cow")) {
                    cowPresent = true;
                }
                // Check that the owner is the transient one
                assertEquals("Owner of transient animal is incorrect", StringUtils.toJVMIDString(transientAnimal.getFarm()), StringUtils.toJVMIDString(transientFarm));
            }
            assertTrue("First value animal is not present in the transient set", dogPresent);
            assertTrue("Second value animal is not present in the transient set", cowPresent);
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        clean(Farm.class);
        clean(Animal.class);
        clean(Manager.class);
        clean(Widget.class);
    }
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) Transaction(javax.jdo.Transaction) JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager) Animal(org.jpox.samples.one_many.bidir.Animal) Farm(org.jpox.samples.one_many.bidir.Farm) Iterator(java.util.Iterator) Manager(org.jpox.samples.models.company.Manager) StoreManager(org.datanucleus.store.StoreManager) JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager)

Aggregations

PersistenceManager (javax.jdo.PersistenceManager)106 Manager (org.jpox.samples.models.company.Manager)106 Transaction (javax.jdo.Transaction)97 Department (org.jpox.samples.models.company.Department)50 Query (javax.jdo.Query)48 Employee (org.jpox.samples.models.company.Employee)45 JDOUserException (javax.jdo.JDOUserException)41 Collection (java.util.Collection)40 JDOPersistenceManager (org.datanucleus.api.jdo.JDOPersistenceManager)38 StoreManager (org.datanucleus.store.StoreManager)34 InsuranceDepartment (org.jpox.samples.models.company.InsuranceDepartment)29 JDOObjectNotFoundException (javax.jdo.JDOObjectNotFoundException)27 Iterator (java.util.Iterator)22 JDODetachedFieldAccessException (javax.jdo.JDODetachedFieldAccessException)17 RDBMSStoreManager (org.datanucleus.store.rdbms.RDBMSStoreManager)15 JDOException (javax.jdo.JDOException)14 Extent (javax.jdo.Extent)13 PersistenceManagerFactory (javax.jdo.PersistenceManagerFactory)13 List (java.util.List)11 Properties (java.util.Properties)10