Search in sources :

Example 6 with Department

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

the class AttachDetachTest method testDetachAttach_ManyToOne.

/**
 * test pc objects aggregating other pcs. associations N-1
 */
public void testDetachAttach_ManyToOne() {
    try {
        Employee woody = new Employee(1, "Woody", "Woodpecker", "woody@woodpecker.com", 13, "serial 1", new Integer(10));
        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");
        Manager boss3 = new Manager(5, "Boss", "WakesUp3", "boss3@wakes.up", 6, "serial 5");
        woody.setManager(bart);
        Department deptB = new Department("DeptB");
        deptB.setManager(bart);
        Employee woodyDetached;
        Employee bossDetached;
        Object id;
        Object idBoss;
        PersistenceManager pm = newPM();
        Transaction tx = pm.currentTransaction();
        try {
            // -----------------------------------------------------------------------------------------
            // test 1 - test detach and attach
            // -----------------------------------------------------------------------------------------
            tx.begin();
            pm.makePersistent(woody);
            pm.makePersistent(boss);
            pm.makePersistent(deptB);
            woodyDetached = (Employee) pm.detachCopy(woody);
            tx.commit();
            id = pm.getObjectId(woody);
            idBoss = pm.getObjectId(boss);
            woodyDetached.getManager().setLastName("Simpson0");
            tx.begin();
            pm.makePersistent(woodyDetached);
            tx.commit();
            System.gc();
            tx.begin();
            Employee woody2 = (Employee) pm.getObjectById(id, true);
            assertEquals("expected change in attached instance", "Simpson0", woody2.getManager().getLastName());
            tx.commit();
            // -----------------------------------------------------------------------------------------
            // test 2 - test pc are the same after attach
            // -----------------------------------------------------------------------------------------
            tx.begin();
            woody = (Employee) pm.getObjectById(id, true);
            woodyDetached = (Employee) pm.detachCopy(woody);
            tx.commit();
            woodyDetached.setLastName("Simpson1");
            tx.begin();
            woody2 = (Employee) pm.getObjectById(id, true);
            Employee woodyAttached = (Employee) pm.makePersistent(woodyDetached);
            assertEquals("attached instance returned must be the one already enlisted in the PM", woodyAttached, woody2);
            assertEquals("attached instance returned must be the one already enlisted in the PM", woodyAttached.getManager(), woody2.getManager());
            tx.commit();
            // -----------------------------------------------------------------------------------------
            // test 3 - test pc are the same after attach, now in different order, first attach and later get object
            // -----------------------------------------------------------------------------------------
            tx.begin();
            woody = (Employee) pm.getObjectById(id, true);
            woodyDetached = (Employee) pm.detachCopy(woody);
            tx.commit();
            woodyDetached.setLastName("Simpson1");
            tx.begin();
            woodyAttached = (Employee) pm.makePersistent(woodyDetached);
            woody2 = (Employee) pm.getObjectById(id, true);
            assertEquals("attached instance returned must be the one already enlisted in the PM", woodyAttached, woody2);
            assertEquals("attached instance returned must be the one already enlisted in the PM", woodyAttached.getManager(), woody2.getManager());
            tx.commit();
            // -----------------------------------------------------------------------------------------
            // test 4 - test changing aggregated pc. aggregated pc is not yet persistent
            // -----------------------------------------------------------------------------------------
            tx.begin();
            woody = (Employee) pm.getObjectById(id, true);
            woodyDetached = (Employee) pm.detachCopy(woody);
            tx.commit();
            woodyDetached.setLastName("Simpson1");
            assertTrue("pc instance should not be already persistent", !JDOHelper.isPersistent(boss3));
            woodyDetached.setManager(boss3);
            tx.begin();
            woodyAttached = (Employee) pm.makePersistent(woodyDetached);
            woody2 = (Employee) pm.getObjectById(id, true);
            assertEquals("attached instance returned must be the one already enlisted in the PM", woodyAttached, woody2);
            assertEquals("changed aggregated pc instance was not applied to the datastore", woodyAttached.getManager(), boss3);
            assertTrue("aggregated pc instance was expected to be made persistent", JDOHelper.isPersistent(boss3));
            tx.commit();
            tx.begin();
            woody = (Employee) pm.getObjectById(id, true);
            assertEquals("changed aggregated pc instance was not applied to the datastore", woody.getManager(), boss3);
            tx.commit();
            // -----------------------------------------------------------------------------------------
            // test 5 - test changing aggregated pc. aggregated pc is already persistent
            // -----------------------------------------------------------------------------------------
            tx.begin();
            pm.makePersistent(boss2);
            tx.commit();
            tx.begin();
            woody = (Employee) pm.getObjectById(id, true);
            woodyDetached = (Employee) pm.detachCopy(woody);
            tx.commit();
            woodyDetached.setLastName("Simpson1");
            woodyDetached.setManager(boss2);
            tx.begin();
            woodyAttached = (Employee) pm.makePersistent(woodyDetached);
            woody2 = (Employee) pm.getObjectById(id, true);
            assertEquals("attached instance returned must be the one already enlisted in the PM", woodyAttached, woody2);
            assertEquals("changed aggregated pc instance was not applied to the datastore", woodyAttached.getManager(), boss2);
            assertTrue("aggregated pc instance was expected to be made persistent", JDOHelper.isPersistent(boss2));
            tx.commit();
            tx.begin();
            woody = (Employee) pm.getObjectById(id, true);
            assertEquals("changed aggregated pc instance was not applied to the datastore", woody.getManager(), boss2);
            tx.commit();
            // -----------------------------------------------------------------------------------------
            // test 6 - test setting aggregated pc to null
            // -----------------------------------------------------------------------------------------
            tx.begin();
            woody = (Employee) pm.getObjectById(id, true);
            woodyDetached = (Employee) pm.detachCopy(woody);
            tx.commit();
            woodyDetached.setLastName("Simpson1");
            woodyDetached.setManager(null);
            tx.begin();
            woodyAttached = (Employee) pm.makePersistent(woodyDetached);
            woody2 = (Employee) pm.getObjectById(id, true);
            assertEquals("attached instance returned must be the one already enlisted in the PM", woodyAttached, woody2);
            assertEquals("attached instance returned has incorrect last name", "Simpson1", woody2.getLastName());
            assertNull("changed aggregated pc instance was not applied to the datastore. it should be null", woodyAttached.getManager());
            tx.commit();
            tx.begin();
            woody = (Employee) pm.getObjectById(id, true);
            assertNull("changed aggregated pc instance was not applied to the datastore. it should be null", woody.getManager());
            tx.commit();
            // -----------------------------------------------------------------------------------------
            // test 7 - test detach and read aggregated pc field when its null
            // -----------------------------------------------------------------------------------------
            tx.begin();
            boss = (Manager) pm.getObjectById(idBoss, true);
            bossDetached = (Manager) pm.detachCopy(boss);
            tx.commit();
            assertNull("pc field should be null", bossDetached.getManager());
        } catch (Exception e) {
            LOG.error("Exception in test", e);
            fail(e.toString());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        CompanyHelper.clearCompanyData(pmf);
    }
}
Also used : Department(org.jpox.samples.models.company.Department) 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) JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager) JDODetachedFieldAccessException(javax.jdo.JDODetachedFieldAccessException) JDOUserException(javax.jdo.JDOUserException) JDOObjectNotFoundException(javax.jdo.JDOObjectNotFoundException)

Example 7 with Department

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

the class CacheTest method clearEmployeeData.

protected void clearEmployeeData(PersistenceManagerFactory pmf) {
    Extent ext = null;
    java.util.Iterator it = null;
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        // disassociate all Employees and Departments from their Managers
        tx.begin();
        ext = pm.getExtent(Manager.class, false);
        it = ext.iterator();
        while (it.hasNext()) {
            Manager mgr = (Manager) it.next();
            mgr.clearSubordinates();
            mgr.clearDepartments();
        }
        tx.commit();
        // delete all Employee objects
        tx.begin();
        ext = pm.getExtent(Employee.class, false);
        it = ext.iterator();
        while (it.hasNext()) {
            Employee emp = (Employee) it.next();
            pm.deletePersistent(emp);
        }
        tx.commit();
        // delete all Qualification objects
        tx.begin();
        ext = pm.getExtent(Qualification.class, false);
        it = ext.iterator();
        while (it.hasNext()) {
            Qualification q = (Qualification) it.next();
            pm.deletePersistent(q);
        }
        tx.commit();
        // delete all Department objects
        tx.begin();
        ext = pm.getExtent(Department.class, false);
        it = ext.iterator();
        while (it.hasNext()) {
            Department d = (Department) it.next();
            pm.deletePersistent(d);
        }
        tx.commit();
        // delete all Manager objects
        tx.begin();
        ext = pm.getExtent(Manager.class, false);
        it = ext.iterator();
        while (it.hasNext()) {
            Manager mgr = (Manager) it.next();
            pm.deletePersistent(mgr);
        }
        tx.commit();
        // delete all Person objects
        tx.begin();
        ext = pm.getExtent(Person.class, true);
        it = ext.iterator();
        while (it.hasNext()) {
            Person person = (Person) it.next();
            pm.deletePersistent(person);
        }
        tx.commit();
    } finally {
        if (tx.isActive())
            tx.rollback();
        pm.close();
    }
}
Also used : Qualification(org.jpox.samples.models.company.Qualification) Department(org.jpox.samples.models.company.Department) Employee(org.jpox.samples.models.company.Employee) Iterator(java.util.Iterator) Transaction(javax.jdo.Transaction) Extent(javax.jdo.Extent) PersistenceManager(javax.jdo.PersistenceManager) Manager(org.jpox.samples.models.company.Manager) PersistenceManager(javax.jdo.PersistenceManager) Person(org.jpox.samples.models.company.Person)

Example 8 with Department

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

the class JDOQLBasicTest method testQueryUnboundVariables.

public void testQueryUnboundVariables() {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            Office o1 = new Office(1, "Green", "Big spacious office");
            Office o2 = new Office(2, "Blue", "Pokey office at the back of the building");
            Office o3 = new Office(1, "Yellow", "Massive open plan office");
            Department d1 = new Department("Finance");
            Department d2 = new Department("Customer Support");
            Department d3 = new Department("Sales");
            Department d4 = new Department("IT");
            o1.addDepartment(d1);
            o1.addDepartment(d3);
            o2.addDepartment(d2);
            o3.addDepartment(d4);
            o3.addDepartment(d3);
            pm.makePersistent(o1);
            pm.makePersistent(o2);
            pm.makePersistent(o3);
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception during test", e);
            fail("Error persisting sample data : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            Query q = pm.newQuery(Office.class);
            q.setFilter("dept.name.equals(\"Finance\") && departments.contains(dept)");
            q.declareVariables("Department dept");
            q.declareImports("import org.jpox.samples.models.company.Department");
            List l = new ArrayList((Collection) q.execute());
            assertEquals(1, l.size());
            q = pm.newQuery(Office.class);
            q.setFilter("dept.name.equals(\"Sales\") && departments.contains(dept)");
            q.declareVariables("Department dept");
            q.declareImports("import org.jpox.samples.models.company.Department");
            q.setOrdering("roomName ascending");
            l = new ArrayList((Collection) q.execute());
            assertEquals(2, l.size());
            assertEquals(((Office) l.get(0)).getRoomName(), "Green");
            assertEquals(((Office) l.get(1)).getRoomName(), "Yellow");
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        // Clean out our data
        CompanyHelper.clearCompanyData(pmf);
    }
}
Also used : Office(org.jpox.samples.models.company.Office) Department(org.jpox.samples.models.company.Department) InsuranceDepartment(org.jpox.samples.models.company.InsuranceDepartment) Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) ArrayList(java.util.ArrayList) Collection(java.util.Collection) List(java.util.List) ArrayList(java.util.ArrayList) JDOException(javax.jdo.JDOException) JDOUserException(javax.jdo.JDOUserException) NoSuchElementException(java.util.NoSuchElementException)

Example 9 with Department

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

the class JDOQLBasicTest method testOrderingAndFunction2Parms.

public void testOrderingAndFunction2Parms() {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            Office o1 = new Office(1, "Green", "Big spacious office");
            Office o2 = new Office(2, "Blue", "Pokey office at the back of the building");
            Office o3 = new Office(1, "Yellow", "Massive open plan office");
            Department d1 = new Department("Finance");
            Department d2 = new Department("Customer Support");
            Department d3 = new Department("Sales");
            Department d4 = new Department("IT");
            o1.addDepartment(d1);
            o1.addDepartment(d3);
            o2.addDepartment(d2);
            o3.addDepartment(d4);
            o3.addDepartment(d3);
            pm.makePersistent(o1);
            pm.makePersistent(o2);
            pm.makePersistent(o3);
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception during test", e);
            fail("Error persisting sample data : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            Query q = pm.newQuery(Office.class);
            q.setFilter("dept.name.equals(\"Finance\") && departments.contains(dept)");
            q.declareVariables("Department dept");
            q.declareImports("import org.jpox.samples.models.company.Department");
            List l = new ArrayList((Collection) q.execute());
            assertEquals(1, l.size());
            q = pm.newQuery(Office.class);
            q.setFilter("dept.name.equals(\"Sales\") && departments.contains(dept)");
            q.declareVariables("Department dept");
            q.declareImports("import org.jpox.samples.models.company.Department");
            q.setOrdering("roomName.substring(0,10) ascending, description ascending");
            l = new ArrayList((Collection) q.execute());
            assertEquals(2, l.size());
            assertEquals(((Office) l.get(0)).getRoomName(), "Green");
            assertEquals(((Office) l.get(1)).getRoomName(), "Yellow");
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        // Clean out our data
        CompanyHelper.clearCompanyData(pmf);
    }
}
Also used : Office(org.jpox.samples.models.company.Office) Department(org.jpox.samples.models.company.Department) InsuranceDepartment(org.jpox.samples.models.company.InsuranceDepartment) Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) ArrayList(java.util.ArrayList) Collection(java.util.Collection) List(java.util.List) ArrayList(java.util.ArrayList) JDOException(javax.jdo.JDOException) JDOUserException(javax.jdo.JDOUserException) NoSuchElementException(java.util.NoSuchElementException)

Example 10 with Department

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

the class JDOQLContainerTest method testNegateContains.

/**
 * Test of !contains in a JDOQL statement.
 */
public void testNegateContains() {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            // Persist some data
            tx.begin();
            Office o1 = new Office(1, "Green", "Big spacious office");
            Office o2 = new Office(2, "Blue", "Pokey office at the back of the building");
            Office o3 = new Office(1, "Yellow", "Massive open plan office");
            Department d1 = new Department("Finance");
            Department d2 = new Department("Customer Support");
            Department d3 = new Department("Sales");
            Department d4 = new Department("IT");
            o1.addDepartment(d1);
            o1.addDepartment(d3);
            o2.addDepartment(d2);
            o3.addDepartment(d4);
            o3.addDepartment(d3);
            pm.makePersistent(o1);
            pm.makePersistent(o2);
            pm.makePersistent(o3);
            tx.commit();
            tx.begin();
            Query q = pm.newQuery(Office.class);
            q.setFilter("!(departments.contains(dept) && dept.name.equals(\"Sales\"))");
            q.declareVariables("Department dept");
            q.declareImports("import org.jpox.samples.models.company.Department");
            Collection c = (Collection) q.execute();
            // Only Office 2 doesnt have "Sales"
            assertEquals(1, c.size());
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        // Clean out our data
        CompanyHelper.clearCompanyData(pmf);
    }
}
Also used : Office(org.jpox.samples.models.company.Office) Department(org.jpox.samples.models.company.Department) InsuranceDepartment(org.jpox.samples.models.company.InsuranceDepartment) Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) Collection(java.util.Collection)

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