Search in sources :

Example 16 with Manager

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

the class JDOQLBasicTest method testKeepResultsQueryAfterPMClose.

/**
 * Test for the closure of a PM and the effect on query results.
 */
public void testKeepResultsQueryAfterPMClose() {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            Manager homer = new Manager(1, "Homer", "Simpson", "homer@simpson.com", 1, "serial 1");
            pm.makePersistent(homer);
            pm.flush();
            Query q = pm.newQuery(Manager.class);
            List results = (List) q.execute();
            assertFalse(results.isEmpty());
            Manager m1 = (Manager) results.iterator().next();
            tx.rollback();
            pm.close();
            Manager m3 = (Manager) results.iterator().next();
            assertEquals(m1, m3);
        } catch (Exception e) {
            LOG.error("Exception during test", e);
            fail("Exception thrown when trying to access QueryResult after closing the PM : " + e.getMessage());
        } finally {
            if (!pm.isClosed()) {
                if (tx.isActive()) {
                    tx.rollback();
                }
                pm.close();
            }
        }
    } finally {
        clean(Manager.class);
    }
}
Also used : Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) List(java.util.List) ArrayList(java.util.ArrayList) Manager(org.jpox.samples.models.company.Manager) StoreManager(org.datanucleus.store.StoreManager) RDBMSStoreManager(org.datanucleus.store.rdbms.RDBMSStoreManager) PersistenceManager(javax.jdo.PersistenceManager) JDOException(javax.jdo.JDOException) JDOUserException(javax.jdo.JDOUserException) NoSuchElementException(java.util.NoSuchElementException)

Example 17 with Manager

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

the class JDOQLBasicTest method testQueryWithDetachedObjects.

public void testQueryWithDetachedObjects() {
    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");
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        tx.setNontransactionalRead(true);
        try {
            tx.begin();
            pm.makePersistent(woody);
            pm.makePersistent(boss);
            pm.makePersistent(bart);
            tx.commit();
            // non transactional read
            Collection c = (Collection) pm.newQuery(Employee.class).execute();
            Employee detachedEmployee = (Employee) pm.detachCopy(c.iterator().next());
            pm.close();
            pm = pmf.getPersistenceManager();
            tx = pm.currentTransaction();
            tx.begin();
            Query q = pm.newQuery(Employee.class, "this == p");
            q.declareParameters("Employee p");
            c = (Collection) q.execute(detachedEmployee);
            assertEquals(JDOHelper.getObjectId(detachedEmployee), JDOHelper.getObjectId(c.iterator().next()));
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception during test", e);
            fail(e.toString());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        // Clean out our data
        CompanyHelper.clearCompanyData(pmf);
    }
}
Also used : Employee(org.jpox.samples.models.company.Employee) 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) JDOException(javax.jdo.JDOException) JDOUserException(javax.jdo.JDOUserException) NoSuchElementException(java.util.NoSuchElementException)

Example 18 with Manager

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

the class JDOQLContainerTest method testContainsInImplicitParameterCollection.

public void testContainsInImplicitParameterCollection() {
    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");
        Manager boss4 = new Manager(6, "Boss", "WakesUp4", "boss4@wakes.up", 7, "serial 6");
        bart.setManager(boss2);
        boss.setManager(boss4);
        homer.setManager(bart);
        Collection emps = new HashSet();
        emps.add(bart);
        emps.add(boss);
        emps.add(homer);
        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(boss4);
            tx.commit();
            tx.begin();
            Query q = pm.newQuery(Manager.class);
            q.setFilter(":emps.contains(this.manager)");
            Collection c = (Collection) q.execute(emps);
            assertEquals(1, c.size());
            assertEquals(((Manager) c.iterator().next()).getFirstName(), "Homer");
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        // Clean out our data
        CompanyHelper.clearCompanyData(pmf);
    }
}
Also used : Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) Collection(java.util.Collection) Manager(org.jpox.samples.models.company.Manager) PersistenceManager(javax.jdo.PersistenceManager) HashSet(java.util.HashSet)

Example 19 with Manager

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

the class JDOQLContainerTest method testContainsInImplicitParameterCollectionOfPCleanInstances.

public void testContainsInImplicitParameterCollectionOfPCleanInstances() {
    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");
        Manager boss4 = new Manager(6, "Boss", "WakesUp4", "boss4@wakes.up", 7, "serial 6");
        bart.setManager(boss2);
        boss.setManager(boss4);
        homer.setManager(bart);
        Collection emps = new HashSet();
        emps.add(bart);
        emps.add(boss);
        emps.add(homer);
        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(boss4);
            tx.commit();
            tx.begin();
            Query q1 = pm.newQuery(Manager.class);
            Collection coll1 = (Collection) q1.execute();
            Query q2 = pm.newQuery(Manager.class);
            q2.setFilter(":emps.contains(this.manager)");
            Collection coll2 = (Collection) q2.execute(coll1);
            assertEquals(3, coll2.size());
            Iterator iter = coll2.iterator();
            boolean bartPresent = false;
            boolean homerPresent = false;
            boolean bossPresent = false;
            while (iter.hasNext()) {
                Manager m = (Manager) iter.next();
                if (m.getFirstName().equals("Homer")) {
                    homerPresent = true;
                } else if (m.getFirstName().equals("Bart")) {
                    bartPresent = true;
                } else if (m.getFirstName().equals("Boss") && m.getLastName().equals("WakesUp")) {
                    bossPresent = true;
                }
            }
            assertTrue("Homer is not present!", homerPresent);
            assertTrue("Bart is not present!", bartPresent);
            assertTrue("Boss is not present!", bossPresent);
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        // Clean out our data
        CompanyHelper.clearCompanyData(pmf);
    }
}
Also used : Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) Iterator(java.util.Iterator) Collection(java.util.Collection) Manager(org.jpox.samples.models.company.Manager) PersistenceManager(javax.jdo.PersistenceManager) HashSet(java.util.HashSet)

Example 20 with Manager

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

the class JDOQLContainerTest method testQueryUsesContainsOnceOnOneUnboundVariableImplicitVariables.

/**
 * test query with "field.contains(x)" using a workaround
 */
public void testQueryUsesContainsOnceOnOneUnboundVariableImplicitVariables() {
    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");
    Manager boss4 = new Manager(6, "Boss", "WakesUp4", "boss4@wakes.up", 7, "serial 6");
    bart.addSubordinate(boss);
    bart.addSubordinate(boss2);
    homer.addSubordinate(boss4);
    Department deptA = new Department("DeptA");
    Department deptB = new Department("DeptB");
    bart.addDepartment(deptB);
    boss4.addSubordinate(bart);
    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(boss4);
        pm.makePersistent(deptA);
        pm.makePersistent(deptB);
        tx.commit();
        tx.begin();
        Query q = pm.newQuery(Manager.class);
        q.setFilter("subordinates.contains(emp1) && emp1.lastName == \"WakesUp\"");
        Collection c = (Collection) q.execute();
        assertEquals(1, c.size());
        q = pm.newQuery(Manager.class);
        q.setFilter("subordinates.contains(emp1) && (emp1.lastName == \"WakesUp\" || emp1.lastName == \"WakesUp2\")");
        c = (Collection) q.execute();
        assertEquals(1, c.size());
        q = pm.newQuery(Manager.class);
        q.setFilter("subordinates.contains(emp1) && (emp1.lastName == \"WakesUp\" || emp1.lastName == \"WakesUp4\")");
        c = (Collection) q.execute();
        assertEquals(2, c.size());
        q = pm.newQuery(Manager.class);
        q.setFilter("departments.contains(db) && e.departments.contains(db) && db.name =='DeptB'");
        q.declareVariables("Department db; Manager e");
        c = (Collection) q.execute();
        assertEquals(1, c.size());
        q = pm.newQuery(Manager.class);
        q.setFilter("subordinates.contains(e) && (e.departments.contains(db) && db.name =='DeptB')");
        q.declareVariables("Department db; Manager e");
        c = (Collection) q.execute();
        assertEquals(1, c.size());
        assertEquals("WakesUp4", ((Manager) c.iterator().next()).getLastName());
        tx.commit();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
        // 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) Collection(java.util.Collection) 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