Search in sources :

Example 26 with Department

use of org.jpox.samples.models.company.Department 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 27 with Department

use of org.jpox.samples.models.company.Department 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 28 with Department

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

the class PersistenceManagerTest method testFKCollectionFieldPersistenceByReachability1.

/**
 * Test that when a FK collection is persisted the element is persisted also.
 * TODO Move to reachability tests.
 */
public void testFKCollectionFieldPersistenceByReachability1() {
    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]);
    try {
        Department d = new Department("Engineering");
        d.setManager(mgr);
        mgr.addDepartment(d);
        tx.begin();
        pm.makePersistent(mgr);
        tx.commit();
    } catch (Exception e) {
        LOG.error("Exception thrown when persisting FK collection using reachability", e);
        fail("Exception thrown when persisting FK collection using reachability " + 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());
        mgr = (Manager) it.next();
        Collection c = mgr.getDepartments();
        assertEquals(1, c.size());
        ext = pm.getExtent(Department.class, false);
        it = ext.iterator();
        assertTrue(it.hasNext());
        Department d = (Department) it.next();
        assertTrue(c.contains(d));
    } 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 29 with Department

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

the class PersistenceManagerTest method tearDown.

/**
 * Invoked after each test is run
 */
public void tearDown() throws java.lang.Exception {
    super.tearDown();
    // TODO Remove all of this when each test cleans out its own data.
    Extent ext = null;
    java.util.Iterator it = null;
    PersistenceManager pm = pmf.getPersistenceManager();
    try {
        // delete all InstanceCallbackContainer objects
        pm.currentTransaction().begin();
        ext = pm.getExtent(InstanceCallbackContainer.class, false);
        it = ext.iterator();
        while (it.hasNext()) {
            InstanceCallbackContainer owner = (InstanceCallbackContainer) it.next();
            pm.deletePersistent(owner);
        }
        pm.currentTransaction().commit();
        // delete all InstanceCallbackTester objects
        pm.currentTransaction().begin();
        ext = pm.getExtent(InstanceCallbackTester.class, false);
        it = ext.iterator();
        while (it.hasNext()) {
            InstanceCallbackTester tester = (InstanceCallbackTester) it.next();
            // necesaary to avoid exception from jdoPreDelete() for this class only
            tester.setTransientValue("");
            pm.deletePersistent(tester);
        }
        pm.currentTransaction().commit();
        // delete all InversePrimitive objects
        pm.currentTransaction().begin();
        ext = pm.getExtent(org.datanucleus.samples.widget.InversePrimitive.class, false);
        it = ext.iterator();
        while (it.hasNext()) {
            InversePrimitive ip = (InversePrimitive) it.next();
            ip.setTester(null);
            pm.deletePersistent(ip);
        }
        pm.currentTransaction().commit();
        // delete all CollectionFieldTester objects
        pm.currentTransaction().begin();
        ext = pm.getExtent(CollectionFieldTester.class, false);
        it = ext.iterator();
        while (it.hasNext()) {
            CollectionFieldTester t = (CollectionFieldTester) it.next();
            if (t.getPrimitiveCollection() != null) {
                t.getPrimitiveCollection().clear();
            }
            if (t.getInversePrimitiveCollection() != null) {
                t.getInversePrimitiveCollection().clear();
            }
            pm.deletePersistent(t);
        }
        pm.currentTransaction().commit();
        // delete all Primative objects
        pm.currentTransaction().begin();
        ext = pm.getExtent(org.datanucleus.samples.widget.Primitive.class, false);
        it = ext.iterator();
        while (it.hasNext()) {
            Primitive p = (Primitive) it.next();
            pm.deletePersistent(p);
        }
        pm.currentTransaction().commit();
        // disassociate all Employees and Departments from their Managers
        pm.currentTransaction().begin();
        ext = pm.getExtent(Manager.class, false);
        it = ext.iterator();
        while (it.hasNext()) {
            Manager mgr = (Manager) it.next();
            if (mgr.getSubordinates() != null) {
                mgr.getSubordinates().clear();
            }
            if (mgr.getDepartments() != null) {
                mgr.getDepartments().clear();
            }
        }
        pm.currentTransaction().commit();
        // delete all Employee objects
        pm.currentTransaction().begin();
        ext = pm.getExtent(Employee.class, false);
        it = ext.iterator();
        while (it.hasNext()) {
            Employee emp = (Employee) it.next();
            pm.deletePersistent(emp);
        }
        pm.currentTransaction().commit();
        pm.currentTransaction().begin();
        // dekete all Department objects
        ext = pm.getExtent(Department.class, false);
        it = ext.iterator();
        while (it.hasNext()) {
            Department d = (Department) it.next();
            pm.deletePersistent(d);
        }
        pm.currentTransaction().commit();
        // delete all Manager objects
        pm.currentTransaction().begin();
        ext = pm.getExtent(Manager.class, false);
        it = ext.iterator();
        while (it.hasNext()) {
            Manager mgr = (Manager) it.next();
            pm.deletePersistent(mgr);
        }
        pm.currentTransaction().commit();
        // delete all Person objects
        pm.currentTransaction().begin();
        ext = pm.getExtent(Person.class, true);
        it = ext.iterator();
        while (it.hasNext()) {
            Person person = (Person) it.next();
            pm.deletePersistent(person);
        }
        pm.currentTransaction().commit();
    } finally {
        if (pm.currentTransaction().isActive())
            pm.currentTransaction().commit();
        pm.close();
    }
}
Also used : Iterator(java.util.Iterator) InversePrimitive(org.datanucleus.samples.widget.InversePrimitive) Extent(javax.jdo.Extent) JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager) InstanceCallbackContainer(org.datanucleus.samples.instancecallback.InstanceCallbackContainer) Manager(org.jpox.samples.models.company.Manager) StoreManager(org.datanucleus.store.StoreManager) JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager) InstanceCallbackTester(org.datanucleus.samples.instancecallback.InstanceCallbackTester) CollectionFieldTester(org.datanucleus.samples.widget.CollectionFieldTester) InversePrimitive(org.datanucleus.samples.widget.InversePrimitive) Primitive(org.datanucleus.samples.widget.Primitive) Department(org.jpox.samples.models.company.Department) Employee(org.jpox.samples.models.company.Employee) Person(org.jpox.samples.models.company.Person)

Example 30 with Department

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

the class JDOQLBasicTest method testCast.

public void testCast() {
    try {
        Manager homer = new Manager(1, "Homer", "Simpson", "homer@simpson.com", 1, "serial 1");
        Manager bart = new Manager(2, "Bart", "Simpson", "bart@simpson.com", 2, "serial 2");
        Manager boss = new Manager(3, "Boss", "WakesUp", "boss@wakes.up", 4, "serial 3");
        Manager boss2 = new Manager(4, "Boss", "WakesUp2", "boss2@wakes.up", 5, "serial 4");
        bart.addSubordinate(boss);
        bart.addSubordinate(boss2);
        Department deptB = new Department("DeptB");
        bart.addDepartment(deptB);
        Qualification q1 = new Qualification("q1");
        q1.setPerson(boss);
        Qualification q2 = new Qualification("q2");
        q2.setPerson(boss2);
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            pm.makePersistent(bart);
            pm.makePersistent(homer);
            pm.makePersistent(boss);
            pm.makePersistent(boss2);
            pm.makePersistent(q1);
            pm.makePersistent(q2);
            tx.commit();
            tx.begin();
            Query q = pm.newQuery(Qualification.class);
            Collection c = q.filter("((Employee)person).serialNo == \"serial 3\"").imports("import org.jpox.samples.models.company.Employee").executeList();
            assertEquals(1, c.size());
            assertEquals("q1", ((Qualification) c.iterator().next()).getName());
            q = pm.newQuery(Qualification.class);
            c = q.filter("((Manager)person).serialNo == \"serial 4\"").imports("import org.jpox.samples.models.company.Employee").executeList();
            assertEquals(1, c.size());
            assertEquals("q2", ((Qualification) c.iterator().next()).getName());
            tx.commit();
        } finally {
            if (tx.isActive()) {
                pm.currentTransaction().rollback();
            }
            pm.close();
        }
    } finally {
        // Clean out our data
        CompanyHelper.clearCompanyData(pmf);
    }
}
Also used : Department(org.jpox.samples.models.company.Department) InsuranceDepartment(org.jpox.samples.models.company.InsuranceDepartment) Qualification(org.jpox.samples.models.company.Qualification) Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) Collection(java.util.Collection) Manager(org.jpox.samples.models.company.Manager) StoreManager(org.datanucleus.store.StoreManager) RDBMSStoreManager(org.datanucleus.store.rdbms.RDBMSStoreManager) PersistenceManager(javax.jdo.PersistenceManager)

Aggregations

Department (org.jpox.samples.models.company.Department)56 PersistenceManager (javax.jdo.PersistenceManager)55 Transaction (javax.jdo.Transaction)54 Manager (org.jpox.samples.models.company.Manager)50 Query (javax.jdo.Query)33 InsuranceDepartment (org.jpox.samples.models.company.InsuranceDepartment)32 Collection (java.util.Collection)31 JDOUserException (javax.jdo.JDOUserException)20 JDOPersistenceManager (org.datanucleus.api.jdo.JDOPersistenceManager)19 JDOObjectNotFoundException (javax.jdo.JDOObjectNotFoundException)13 StoreManager (org.datanucleus.store.StoreManager)13 Employee (org.jpox.samples.models.company.Employee)12 Iterator (java.util.Iterator)11 JDOException (javax.jdo.JDOException)11 Extent (javax.jdo.Extent)8 List (java.util.List)7 JDODetachedFieldAccessException (javax.jdo.JDODetachedFieldAccessException)7 ArrayList (java.util.ArrayList)6 Office (org.jpox.samples.models.company.Office)6 SQLException (java.sql.SQLException)5