Search in sources :

Example 66 with Manager

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

the class JDOQLResultTest method testOrderingUsingCollectionExpression.

public void testOrderingUsingCollectionExpression() {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            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");
            Department deptC = new Department("DeptC");
            boss[0].getDepartments().add(deptA);
            boss[0].getDepartments().add(deptB);
            boss[1].getDepartments().add(deptC);
            pm.makePersistentAll(boss);
            pm.flush();
            Query q = pm.newQuery(Manager.class);
            q.setOrdering("departments.size() ascending, lastName ascending");
            List l = (List) q.execute();
            Assert.assertEquals(5, l.size());
            Assert.assertEquals("WakesUp3", ((Manager) l.get(0)).getLastName());
            Assert.assertEquals("WakesUp4", ((Manager) l.get(1)).getLastName());
            Assert.assertEquals("WakesUp5", ((Manager) l.get(2)).getLastName());
            Assert.assertEquals("WakesUp2", ((Manager) l.get(3)).getLastName());
            Assert.assertEquals("WakesUp", ((Manager) l.get(4)).getLastName());
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception in query", e);
            fail("Exception thrown by query " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.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) 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) JDOException(javax.jdo.JDOException) JDOUserException(javax.jdo.JDOUserException)

Example 67 with Manager

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

the class JDOQLResultTest method testSetResultCartesianProductContains1.

/**
 * Test cartesian products
 */
public void testSetResultCartesianProductContains1() {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            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");
                Office office = new Office(5, "cubicle 1", "none");
                bart.addSubordinate(boss[0]);
                bart.addSubordinate(boss[1]);
                Department deptA = new Department("DeptA");
                Department deptB = new Department("DeptB");
                bart.getDepartments().add(deptA);
                boss[3].getDepartments().add(deptB);
                pm.makePersistent(bart);
                pm.makePersistentAll(boss);
                pm.makePersistent(office);
                tx.commit();
            } finally {
                if (tx.isActive()) {
                    tx.rollback();
                }
            }
            try {
                tx.begin();
                Query q = pm.newQuery(pm.getExtent(Department.class, false));
                q.declareVariables("Manager e");
                q.setResult("this, e");
                q.setFilter("e.departments.contains(this)");
                List results = (List) q.execute();
                assertEquals(2, results.size());
                q.closeAll();
                tx.commit();
            } catch (Exception e) {
                LOG.error("Exception during test", e);
                fail("Exception during test : " + e.getMessage());
            } finally {
                if (tx.isActive()) {
                    tx.rollback();
                }
            }
            try {
                tx.begin();
                try {
                    Query q = pm.newQuery(pm.getExtent(Department.class, false));
                    q.declareVariables("Manager e");
                    q.setResult("e");
                    q.setFilter("e.departments.contains(this)");
                    List results = (List) q.execute();
                    assertEquals(2, results.size());
                    q.closeAll();
                } catch (RuntimeException e) {
                    fail(e.getMessage());
                }
                tx.commit();
            } finally {
                if (tx.isActive()) {
                    tx.rollback();
                }
            }
        } finally {
            pm.close();
        }
    } finally {
        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) List(java.util.List) Manager(org.jpox.samples.models.company.Manager) PersistenceManager(javax.jdo.PersistenceManager) JDOException(javax.jdo.JDOException) JDOUserException(javax.jdo.JDOUserException)

Example 68 with Manager

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

the class JDOQLResultTest method testSetResultCartesianProductCollection.

/**
 * Test cartesian products
 */
public void testSetResultCartesianProductCollection() {
    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");
            Office office = new Office(5, "cubicle 1", "none");
            bart.addSubordinate(boss[0]);
            bart.addSubordinate(boss[1]);
            Department deptA = new Department("DeptA");
            Department deptB = new Department("DeptB");
            bart.getDepartments().add(deptA);
            boss[3].getDepartments().add(deptB);
            pm.makePersistent(bart);
            pm.makePersistentAll(boss);
            pm.makePersistent(office);
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
        }
        try {
            tx.begin();
            try {
                Query q = pm.newQuery(pm.getExtent(Department.class, false));
                q.declareVariables("Manager e");
                q.setResult("this, e.departments");
                q.execute();
                q.closeAll();
                fail("Expected user exception");
            } catch (JDOUserException e) {
            // expected
            }
            try {
                Query q = pm.newQuery(pm.getExtent(Department.class, false));
                q.declareVariables("Manager e");
                q.setResult("this, e.departments");
                q.compile();
                q.closeAll();
                fail("Expected user exception");
            } catch (JDOUserException e) {
            // expected
            }
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        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) Manager(org.jpox.samples.models.company.Manager) PersistenceManager(javax.jdo.PersistenceManager) JDOUserException(javax.jdo.JDOUserException)

Example 69 with Manager

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

the class JDOQLResultTest method testSetResultCartesianProduct1.

/**
 * Test cartesian products
 */
public void testSetResultCartesianProduct1() {
    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");
            Office office = new Office(5, "cubicle 1", "none");
            bart.addSubordinate(boss[0]);
            bart.addSubordinate(boss[1]);
            Department deptA = new Department("DeptA");
            Department deptB = new Department("DeptB");
            bart.getDepartments().add(deptA);
            boss[3].getDepartments().add(deptB);
            pm.makePersistent(bart);
            pm.makePersistentAll(boss);
            pm.makePersistent(office);
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
        }
        try {
            tx.begin();
            try {
                Query q = pm.newQuery(pm.getExtent(Department.class, false));
                q.declareVariables("Employee e");
                List results = (List) q.execute();
                assertEquals(12, results.size());
                q.closeAll();
                fail("JDOUserException expected");
            } catch (JDOUserException e) {
            // expected;
            }
            try {
                Query q = pm.newQuery(pm.getExtent(Department.class, false));
                q.declareVariables("Employee e");
                q.setResult("this");
                List results = (List) q.execute();
                assertEquals(12, results.size());
                q.closeAll();
                fail("JDOUserException expected");
            } catch (JDOUserException e) {
            // expected;
            }
            try {
                Query q = pm.newQuery(pm.getExtent(Department.class, false));
                q.declareVariables("Employee e");
                q.setResult("this, e");
                List results = (List) q.execute();
                assertEquals(12, results.size());
                q.closeAll();
            } catch (JDOUserException e) {
                fail(e.getMessage());
            }
            try {
                Query q = pm.newQuery(pm.getExtent(Department.class, false));
                q.declareVariables("Employee e; Office o");
                q.setResult("this, e, o");
                List results = (List) q.execute();
                assertEquals(12, results.size());
                q.closeAll();
            } catch (JDOUserException e) {
                fail(e.getMessage());
            }
            Office office = new Office(6, "cubicle 2", "none");
            pm.makePersistent(office);
            pm.flush();
            try {
                Query q = pm.newQuery(pm.getExtent(Department.class, false));
                q.declareVariables("Employee e; Office o");
                q.setResult("this, e, o");
                List results = (List) q.execute();
                assertEquals(24, results.size());
                q.closeAll();
            } catch (JDOUserException e) {
                fail(e.getMessage());
            }
            try {
                Query q = pm.newQuery(pm.getExtent(Department.class, false));
                q.declareVariables("Department d");
                q.setResult("this, d");
                List results = (List) q.execute();
                assertEquals(4, results.size());
                q.closeAll();
            } catch (JDOUserException e) {
                fail(e.getMessage());
            }
            try {
                Query q = pm.newQuery(pm.getExtent(Department.class, false));
                q.declareVariables("Department d");
                q.setResult("this, d");
                q.setFilter("d==this");
                List results = (List) q.execute();
                assertEquals(2, results.size());
                q.closeAll();
            } catch (JDOUserException e) {
                fail(e.getMessage());
            }
            try {
                Query q = pm.newQuery(pm.getExtent(Department.class, false));
                q.declareVariables("Department d");
                q.setResult("this, d");
                q.setFilter("d!=this");
                List results = (List) q.execute();
                assertEquals(2, results.size());
                q.closeAll();
            } catch (JDOUserException e) {
                fail(e.getMessage());
            }
            try {
                Query q = pm.newQuery(pm.getExtent(Department.class, false));
                q.declareVariables("Manager e");
                q.setResult("this, e");
                List results = (List) q.execute();
                assertEquals(12, results.size());
                q.closeAll();
            } catch (RuntimeException e) {
                fail(e.getMessage());
            }
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
        }
    } finally {
        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) List(java.util.List) Manager(org.jpox.samples.models.company.Manager) PersistenceManager(javax.jdo.PersistenceManager) JDOUserException(javax.jdo.JDOUserException)

Example 70 with Manager

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

the class PersistenceManagerProxyTest method xtestProxyInMultiThread.

/**
 * Test for use of PM proxy using multiple threads.
 * In this test we create a proxy in main thread and sub threads, so re-use the underlying PM of the main thread.
 * This means that we are using a single underlying PM and so totally dependent on the Multithreaded capability of the PM. TODO enable this when we fully support it
 */
public void xtestProxyInMultiThread() throws Exception {
    try {
        // Create a PM proxy
        PersistenceManager pm = pmf.getPersistenceManagerProxy();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            Manager m1 = new Manager(101, "Daffy", "Duck", "daffy.duck@warnerbros.com", 105.45f, "123407");
            pm.makePersistent(m1);
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            // Close the proxy, which will close the underlying PM but not the proxy
            pm.close();
        }
        // Try to access something on the proxy to check if it is closed
        try {
            tx = pm.currentTransaction();
        // The proxy should have created a new delegate
        } catch (Exception e) {
            fail("Access to the PM methods after close should have worked, but failed " + e.getMessage());
        }
        try {
            tx.begin();
            Manager m2 = new Manager(102, "Donald", "Duck", "donald.duck@warnerbros.com", 105.46f, "123408");
            pm.makePersistent(m2);
            pm.flush();
            // Start multiple threads to persist the object
            int THREAD_SIZE = 5;
            Thread[] threads = new Thread[THREAD_SIZE];
            for (int i = 0; i < THREAD_SIZE; i++) {
                final int threadNo = i;
                threads[i] = new Thread(new Runnable() {

                    public void run() {
                        PersistenceManager pmthread = pmf.getPersistenceManagerProxy();
                        try {
                            Manager m2 = new Manager(110 + threadNo, "Donald", "Duck", "donald.duck@warnerbros.com", 105.46f, "123408");
                            pmthread.makePersistent(m2);
                        } catch (Exception e) {
                            NucleusLogger.GENERAL.error("Exception while persisting object in thread " + threadNo, e);
                            fail("Exception thrown while accessing object in thread " + threadNo + " : " + e.getMessage());
                        }
                    }
                });
            }
            // Start the threads
            for (int i = 0; i < THREAD_SIZE; i++) {
                threads[i].start();
            }
            // Wait for the end of the threads
            for (int i = 0; i < THREAD_SIZE; i++) {
                try {
                    threads[i].join();
                } catch (InterruptedException e) {
                    fail(e.getMessage());
                }
            }
            // Commit the transaction once all have completed
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            // Close the proxy, which will close the underlying PM but not the proxy
            pm.close();
        }
        // PM closed so just re-get the txn
        tx = pm.currentTransaction();
        try {
            // Check that our objects are persisted
            tx.begin();
            Query<Manager> q = pm.newQuery(Manager.class);
            Collection<Manager> results = q.executeList();
            assertEquals("Number of persisted objects is incorrect", 7, results.size());
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            // Close the proxy, which will close the underlying PM but not the proxy
            pm.close();
        }
    } finally {
        // Clean out our data
        clean(Manager.class);
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) Manager(org.jpox.samples.models.company.Manager) 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