Search in sources :

Example 41 with Department

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

the class JDOQLContainerTest method testQueryUsesContainsTwiceOnFieldWithNamespace.

/**
 * test query with "field.contains(x) && field.contains(y)"
 *
 * namespace put related expressions inside parentheses
 */
public void testQueryUsesContainsTwiceOnFieldWithNamespace() {
    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);
    boss.addSubordinate(boss2);
    Department deptA = new Department("DeptA");
    Department deptB = new Department("DeptB");
    bart.addDepartment(deptB);
    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\") && (subordinates.contains(emp2) && emp2.lastName == \"WakesUp2\")");
        q.declareVariables("Employee emp1; Employee emp2");
        q.declareImports("import org.jpox.samples.models.company.Employee");
        Collection c = (Collection) q.execute();
        assertEquals(1, c.size());
        tx.commit();
        tx.begin();
        q = pm.newQuery(Manager.class);
        q.setFilter("(subordinates.contains(emp1) && (emp1.lastName == \"WakesUp\" || emp1.lastName == \"WakesUp4\")) && (subordinates.contains(emp2) && emp2.lastName == \"WakesUp2\")");
        q.declareVariables("Employee emp1; Employee emp2");
        q.declareImports("import org.jpox.samples.models.company.Employee");
        c = (Collection) q.execute();
        assertEquals(1, c.size());
        tx.commit();
        tx.begin();
        q = pm.newQuery(Manager.class);
        q.setFilter("(subordinates.contains(emp1) && (emp1.lastName == \"WakesUp\" || emp1.lastName == \"WakesUp4\")) || (subordinates.contains(emp2) && emp2.lastName == \"WakesUp2\")");
        q.declareVariables("Employee emp1; Employee emp2");
        q.declareImports("import org.jpox.samples.models.company.Employee");
        c = (Collection) q.execute();
        assertEquals(3, c.size());
        tx.commit();
    } finally {
        if (tx.isActive()) {
            pm.currentTransaction().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)

Example 42 with Department

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

the class JDOQLContainerTest method testQueryUsesContainsOnceOnOneUnboundVariableInverseImplicitVariables.

/**
 * test query with "field.contains(x)" using a workaround
 */
public void testQueryUsesContainsOnceOnOneUnboundVariableInverseImplicitVariables() {
    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");
    Department deptA = new Department("DeptA");
    Department deptB = new Department("DeptB");
    Department deptC = new Department("DeptC");
    Department deptD = new Department("DeptD");
    Department deptE = new Department("DeptE");
    bart.addDepartment(deptB);
    bart.addDepartment(deptA);
    homer.addDepartment(deptC);
    boss.addDepartment(deptD);
    boss.addDepartment(deptE);
    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("departments.contains(dept1) && dept1.name == \"DeptB\"");
        Collection c = (Collection) q.execute();
        assertEquals(1, c.size());
        q = pm.newQuery(Manager.class);
        q.setFilter("departments.contains(dept1) && (dept1.name == \"DeptB\" || dept1.name == \"DeptC\")");
        c = (Collection) q.execute();
        assertEquals(2, c.size());
        q = pm.newQuery(Manager.class);
        q.setFilter("departments.contains(dept1) && (dept1.name == \"DeptB\" || dept1.name == \"DeptA\")");
        c = (Collection) q.execute();
        assertEquals(1, c.size());
        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)

Example 43 with Department

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

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

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

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