Search in sources :

Example 21 with Department

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

the class JDOQLResultTest method testSetResultCartesianProduct1to1.

/**
 * Test cartesian products
 */
public void testSetResultCartesianProduct1to1() {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            Manager bart = new Manager(2, "Bart", "Simpson", "bart@simpson.com", 2, "serial 2");
            Manager[] boss = new Manager[5];
            boss[0] = new Manager(3, "Boss", "WakesUp", "boss@wakes.up", 4, "serial 3");
            boss[1] = new Manager(4, "Boss", "WakesUp2", "boss2@wakes.up", 5, "serial 4");
            boss[2] = new Manager(5, "Boss", "WakesUp3", "boss3@wakes.up", 6, "serial 5");
            boss[3] = new Manager(6, "Boss", "WakesUp4", "boss4@wakes.up", 7, "serial 6");
            boss[4] = new Manager(7, "Boss", "WakesUp5", "boss5@wakes.up", 8, "serial 7");
            Department deptA = new Department("DeptA");
            Department deptB = new Department("DeptB");
            deptA.setManager(bart);
            deptB.setManager(boss[1]);
            pm.makePersistent(bart);
            pm.makePersistentAll(boss);
            pm.makePersistent(deptA);
            pm.makePersistent(deptB);
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
        }
        try {
            tx.begin();
            Query q = pm.newQuery(pm.getExtent(Department.class, false));
            q.setResult("manager.firstName, manager.lastName");
            q.setOrdering("name ascending");
            List results = (List) q.execute();
            assertEquals(2, results.size());
            assertEquals("Bart", ((Object[]) results.get(0))[0]);
            assertEquals("Simpson", ((Object[]) results.get(0))[1]);
            assertEquals("Boss", ((Object[]) results.get(1))[0]);
            assertEquals("WakesUp2", ((Object[]) results.get(1))[1]);
            q.closeAll();
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
        }
        pm.close();
    } finally {
        CompanyHelper.clearCompanyData(pmf);
    }
}
Also used : 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) List(java.util.List) Manager(org.jpox.samples.models.company.Manager) PersistenceManager(javax.jdo.PersistenceManager)

Example 22 with Department

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

the class PersistenceManagerProxyTest method testLifecycleListenerRegisteredInPMFforAllClasses.

/**
 * Test of lifecycle listener registered for all classes
 */
public void testLifecycleListenerRegisteredInPMFforAllClasses() {
    BasicListener listener = new BasicListener(true);
    PersistenceManagerFactory pmf = getConfigurablePMF(1, null);
    pmf.addInstanceLifecycleListener(listener, null);
    freezePMF(pmf);
    try {
        PersistenceManager pm = pmf.getPersistenceManagerProxy();
        Transaction tx = pm.currentTransaction();
        int i = 0;
        try {
            tx.begin();
            // Persist an object and check the events
            Person person = new Person(12345, "Fred", "Smith", "Fred.Smith@jpox.org");
            pm.makePersistent(person);
            // Persist related objects and check the events
            // Manager has a 1-N (FK) with Department
            Manager manager = new Manager(12346, "George", "Bush", "george.bush@thewhitehouse.com", 2000000, "ABC-DEF");
            Department dept1 = new Department("Invasions");
            Department dept2 = new Department("Propaganda");
            Department dept3 = new Department("Lies");
            manager.addDepartment(dept1);
            manager.addDepartment(dept2);
            manager.addDepartment(dept3);
            dept1.setManager(manager);
            dept2.setManager(manager);
            dept3.setManager(manager);
            pm.makePersistent(manager);
            pm.flush();
            Integer[] events = listener.getRegisteredEventsAsArray();
            assertEquals("Wrong number of lifecycle events", 15, events.length);
            if (tx.getOptimistic()) {
                // Person
                assertEquals(LifecycleListenerSpecification.EVENT_POST_CREATE, events[i++].intValue());
                // Manager
                assertEquals(LifecycleListenerSpecification.EVENT_POST_CREATE, events[i++].intValue());
                // Department 1
                assertEquals(LifecycleListenerSpecification.EVENT_POST_CREATE, events[i++].intValue());
                // Department 2
                assertEquals(LifecycleListenerSpecification.EVENT_POST_CREATE, events[i++].intValue());
                // Department 3
                assertEquals(LifecycleListenerSpecification.EVENT_POST_CREATE, events[i++].intValue());
                int numPreStore = 0;
                int numPostStore = 0;
                int numEventsToProcess = i + 10;
                for (int j = i; j < numEventsToProcess; j++) {
                    if (events[j].intValue() == LifecycleListenerSpecification.EVENT_PRE_STORE) {
                        numPreStore++;
                    } else if (events[j].intValue() == LifecycleListenerSpecification.EVENT_POST_STORE) {
                        numPostStore++;
                    }
                    i++;
                }
                // 1 for each object
                assertEquals("Number of PreStore events is wrong", 5, numPreStore);
                // 1 for each object
                assertEquals("Number of PostStore events is wrong", 5, numPostStore);
            } else {
                assertEquals(LifecycleListenerSpecification.EVENT_POST_CREATE, events[i++].intValue());
                assertEquals(LifecycleListenerSpecification.EVENT_PRE_STORE, events[i++].intValue());
                assertEquals(LifecycleListenerSpecification.EVENT_POST_STORE, events[i++].intValue());
                // Manager
                assertEquals(LifecycleListenerSpecification.EVENT_POST_CREATE, events[i++].intValue());
                // Manager
                assertEquals(LifecycleListenerSpecification.EVENT_PRE_STORE, events[i++].intValue());
                // Department 1
                assertEquals(LifecycleListenerSpecification.EVENT_POST_CREATE, events[i++].intValue());
                // Department 1
                assertEquals(LifecycleListenerSpecification.EVENT_PRE_STORE, events[i++].intValue());
                // Department 1
                assertEquals(LifecycleListenerSpecification.EVENT_POST_STORE, events[i++].intValue());
                // Department 2
                assertEquals(LifecycleListenerSpecification.EVENT_POST_CREATE, events[i++].intValue());
                // Department 2
                assertEquals(LifecycleListenerSpecification.EVENT_PRE_STORE, events[i++].intValue());
                // Department 2
                assertEquals(LifecycleListenerSpecification.EVENT_POST_STORE, events[i++].intValue());
                // Department 3
                assertEquals(LifecycleListenerSpecification.EVENT_POST_CREATE, events[i++].intValue());
                // Department 3
                assertEquals(LifecycleListenerSpecification.EVENT_PRE_STORE, events[i++].intValue());
                // Department 3
                assertEquals(LifecycleListenerSpecification.EVENT_POST_STORE, events[i++].intValue());
                // Manager
                assertEquals(LifecycleListenerSpecification.EVENT_POST_STORE, events[i++].intValue());
            }
            tx.rollback();
            listener.getRegisteredEvents().clear();
            PersistenceManager pm2 = pmf.getPersistenceManager();
            Transaction tx2 = pm.currentTransaction();
            try {
                tx2.begin();
                // Persist an object and check the events
                pm.makePersistent(person);
                pm.flush();
                events = listener.getRegisteredEventsAsArray();
                i = 0;
                assertEquals(LifecycleListenerSpecification.EVENT_POST_CREATE, events[i++].intValue());
                assertEquals(LifecycleListenerSpecification.EVENT_PRE_STORE, events[i++].intValue());
                assertEquals(LifecycleListenerSpecification.EVENT_POST_STORE, events[i++].intValue());
                tx2.rollback();
            } finally {
                if (tx2.isActive()) {
                    tx2.rollback();
                }
                pm2.close();
            }
        } catch (Exception e) {
            LOG.error("Exception while running lifecycle listener simple object test", e);
            fail("Exception thrown while running lifecycle listener simple object test : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
            listener.getRegisteredEvents().clear();
        }
    } finally {
        CompanyHelper.clearCompanyData(pmf);
        pmf.close();
    }
}
Also used : Department(org.jpox.samples.models.company.Department) BasicListener(org.datanucleus.samples.lifecyclelistener.BasicListener) Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) PersistenceManagerFactory(javax.jdo.PersistenceManagerFactory) Manager(org.jpox.samples.models.company.Manager) PersistenceManager(javax.jdo.PersistenceManager) Person(org.jpox.samples.models.company.Person)

Example 23 with Department

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

the class PersistenceManagerTest method testLifecycleListenerRegisteredInPMFforAllClasses.

/**
 * Test of lifecycle listener registered for all classes
 */
public void testLifecycleListenerRegisteredInPMFforAllClasses() {
    BasicListener listener = new BasicListener(true);
    PersistenceManagerFactory pmf = getConfigurablePMF(1, null);
    pmf.addInstanceLifecycleListener(listener, null);
    freezePMF(pmf);
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    int i = 0;
    try {
        tx.begin();
        // Persist an object and check the events
        Person person = new Person(12345, "Fred", "Smith", "Fred.Smith@jpox.org");
        pm.makePersistent(person);
        // Persist related objects and check the events
        // Manager has a 1-N (FK) with Department
        Manager manager = new Manager(12346, "George", "Bush", "george.bush@thewhitehouse.com", 2000000, "ABC-DEF");
        Department dept1 = new Department("Invasions");
        Department dept2 = new Department("Propaganda");
        Department dept3 = new Department("Lies");
        manager.addDepartment(dept1);
        manager.addDepartment(dept2);
        manager.addDepartment(dept3);
        dept1.setManager(manager);
        dept2.setManager(manager);
        dept3.setManager(manager);
        pm.makePersistent(manager);
        pm.flush();
        Integer[] events = listener.getRegisteredEventsAsArray();
        assertEquals("Wrong number of lifecycle events", 15, events.length);
        if (tx.getOptimistic()) {
            assertEquals(LifecycleListenerSpecification.EVENT_POST_CREATE, events[i++].intValue());
            // Manager
            assertEquals(LifecycleListenerSpecification.EVENT_POST_CREATE, events[i++].intValue());
            // Department 1
            assertEquals(LifecycleListenerSpecification.EVENT_POST_CREATE, events[i++].intValue());
            // Department 2
            assertEquals(LifecycleListenerSpecification.EVENT_POST_CREATE, events[i++].intValue());
            // Department 3
            assertEquals(LifecycleListenerSpecification.EVENT_POST_CREATE, events[i++].intValue());
            // Expecting 5 PreStore and 5 PostStore; 1 for each of the objects
            int numPreStore = 0;
            int numPostStore = 0;
            int numOther = 0;
            for (int j = i; j < 15; j++) {
                if (events[j].intValue() == LifecycleListenerSpecification.EVENT_PRE_STORE) {
                    numPreStore++;
                } else if (events[j].intValue() == LifecycleListenerSpecification.EVENT_POST_STORE) {
                    numPostStore++;
                } else {
                    numOther++;
                }
                i++;
            }
            assertEquals("Number of PreStore events was incorrect", 5, numPreStore);
            assertEquals("Number of PostStore events was incorrect", 5, numPostStore);
            assertEquals("Number of other events was incorrect", 0, numOther);
        } else {
            assertEquals(LifecycleListenerSpecification.EVENT_POST_CREATE, events[i++].intValue());
            assertEquals(LifecycleListenerSpecification.EVENT_PRE_STORE, events[i++].intValue());
            assertEquals(LifecycleListenerSpecification.EVENT_POST_STORE, events[i++].intValue());
            // Manager
            assertEquals(LifecycleListenerSpecification.EVENT_POST_CREATE, events[i++].intValue());
            // Manager
            assertEquals(LifecycleListenerSpecification.EVENT_PRE_STORE, events[i++].intValue());
            // Department 1
            assertEquals(LifecycleListenerSpecification.EVENT_POST_CREATE, events[i++].intValue());
            // Department 1
            assertEquals(LifecycleListenerSpecification.EVENT_PRE_STORE, events[i++].intValue());
            // Department 1
            assertEquals(LifecycleListenerSpecification.EVENT_POST_STORE, events[i++].intValue());
            // Department 2
            assertEquals(LifecycleListenerSpecification.EVENT_POST_CREATE, events[i++].intValue());
            // Department 2
            assertEquals(LifecycleListenerSpecification.EVENT_PRE_STORE, events[i++].intValue());
            // Department 2
            assertEquals(LifecycleListenerSpecification.EVENT_POST_STORE, events[i++].intValue());
            // Department 3
            assertEquals(LifecycleListenerSpecification.EVENT_POST_CREATE, events[i++].intValue());
            // Department 3
            assertEquals(LifecycleListenerSpecification.EVENT_PRE_STORE, events[i++].intValue());
            // Department 3
            assertEquals(LifecycleListenerSpecification.EVENT_POST_STORE, events[i++].intValue());
            // Manager
            assertEquals(LifecycleListenerSpecification.EVENT_POST_STORE, events[i++].intValue());
        }
        tx.rollback();
        listener.getRegisteredEvents().clear();
        PersistenceManager pm2 = pmf.getPersistenceManager();
        Transaction tx2 = pm.currentTransaction();
        try {
            tx2.begin();
            // Persist an object and check the events
            pm.makePersistent(person);
            pm.flush();
            events = listener.getRegisteredEventsAsArray();
            i = 0;
            assertEquals(LifecycleListenerSpecification.EVENT_POST_CREATE, events[i++].intValue());
            assertEquals(LifecycleListenerSpecification.EVENT_PRE_STORE, events[i++].intValue());
            assertEquals(LifecycleListenerSpecification.EVENT_POST_STORE, events[i++].intValue());
            tx2.rollback();
        } finally {
            if (tx2.isActive()) {
                tx2.rollback();
            }
            pm2.close();
        }
    } catch (Exception e) {
        LOG.error(">> Exception thrown in test", e);
        fail("Exception thrown while running lifecycle listener simple object test : " + e.getMessage());
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
        listener.getRegisteredEvents().clear();
        pmf.close();
    }
}
Also used : BigInteger(java.math.BigInteger) Department(org.jpox.samples.models.company.Department) BasicListener(org.datanucleus.samples.lifecyclelistener.BasicListener) Transaction(javax.jdo.Transaction) JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager) PersistenceManagerFactory(javax.jdo.PersistenceManagerFactory) Manager(org.jpox.samples.models.company.Manager) StoreManager(org.datanucleus.store.StoreManager) JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager) Person(org.jpox.samples.models.company.Person) 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 24 with Department

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

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

the class PersistenceManagerTest method testRefresh.

/**
 * Test of refresh().
 */
public void testRefresh() throws Exception {
    try {
        PersistenceManager pm1 = pmf.getPersistenceManager();
        Transaction tx1 = pm1.currentTransaction();
        Object id = null;
        Department d = null;
        // Create a Department object
        try {
            tx1.begin();
            d = new Department("Nobody's Department");
            pm1.makePersistent(d);
            tx1.commit();
            id = pm1.getObjectId(d);
        } finally {
            if (tx1.isActive()) {
                tx1.rollback();
            }
        }
        // Create a new PM and retrieve/update the name of the Department
        PersistenceManager pm2 = pmf.getPersistenceManager();
        Transaction tx2 = pm2.currentTransaction();
        try {
            tx2.begin();
            Department d2 = (Department) pm2.getObjectById(id, true);
            d2.setName("Fred's Department");
            tx2.commit();
        } finally {
            if (tx2.isActive()) {
                tx2.rollback();
            }
        }
        // Do a refresh and check that we have the updated Department value
        Transaction tx3 = pm1.currentTransaction();
        try {
            tx3.begin();
            // This will refresh a HOLLOW object and should pull in the updated DB values
            pm1.refresh(d);
            assertTrue("Name of department had been updated in a separate PM, but refresh() hasn't retrieved the updated value", d.getName().equals("Fred's Department"));
            tx3.commit();
        } finally {
            if (tx3.isActive()) {
                tx3.rollback();
            }
            pm1.close();
            pm2.close();
        }
    } finally {
        clean(Department.class);
    }
}
Also used : Department(org.jpox.samples.models.company.Department) Transaction(javax.jdo.Transaction) JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) 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