Search in sources :

Example 21 with Manager

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

the class JDOQLContainerTest method testQueryUsesContainsOnceAndEqualsOnOneUnboundVariable.

/**
 * test query with "field.contains(x)" using a workaround
 */
public void testQueryUsesContainsOnceAndEqualsOnOneUnboundVariable() {
    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(Department.class);
        q.setFilter("m1.departments.contains(this) && m1.firstName == \"Bart\" && m1.lastName == \"Simpson\"");
        q.setResult("distinct this");
        q.declareVariables("Manager m1");
        q.declareImports("import org.jpox.samples.models.company.Manager");
        Collection 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 22 with Manager

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

the class JDOQLContainerTest method testCollectionSize.

/**
 * Test for the Collection.size() method.
 */
public void testCollectionSize() {
    try {
        Manager homer = new Manager(1, "Homer", "Simpson", "homer@simpson.com", 1, "serial 1");
        Employee bart = new Employee(2, "Bart", "Simpson", "bart@simpson.com", 2, "serial 2");
        Employee lisa = new Employee(3, "Lisa", "Simpson", "lisa@simpson.com", 4, "serial 3");
        Manager homersBrother = new Manager(4, "Homer Jr", "Simpson", "homerjr@simpson.com", 1, "serial 4");
        Employee lisasSister = new Employee(5, "Lisa Sr", "Simpson", "lisasr@simpson.com", 4, "serial 5");
        bart.setManager(homer);
        lisa.setManager(homer);
        homer.addSubordinate(lisa);
        homer.addSubordinate(bart);
        lisasSister.setManager(homersBrother);
        homersBrother.addSubordinate(lisasSister);
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            pm.makePersistent(bart);
            pm.makePersistent(homer);
            pm.makePersistent(lisa);
            pm.makePersistent(homersBrother);
            pm.makePersistent(lisasSister);
            tx.commit();
            tx.begin();
            Query q = pm.newQuery(Manager.class);
            q.setFilter("subordinates.size() == 2");
            Collection c = (Collection) q.execute();
            assertEquals(1, c.size());
            assertEquals(((Manager) c.iterator().next()).getFirstName(), "Homer");
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception thrown during test", e);
            fail("Exception thrown while executing query with collection.size()");
        } 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) PersistenceManager(javax.jdo.PersistenceManager) JDOUserException(javax.jdo.JDOUserException)

Example 23 with Manager

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

the class JDOQLContainerTest method testQueryUsesContainsTwiceOnOneUnboundVariableInverse.

/**
 * test query with "field.contains(x) && field.contains(x)" using a workaround
 */
public void testQueryUsesContainsTwiceOnOneUnboundVariableInverse() {
    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) && departments.contains(dept1) && (dept1.name == \"DeptA\" || dept1.name == \"DeptB\")");
        q.declareVariables("Department dept1");
        Collection c = (Collection) q.execute();
        assertEquals(1, c.size());
        q = pm.newQuery(Manager.class);
        q.setFilter("departments.contains(dept1) && departments.contains(dept1) && (dept1.name == \"DeptA\" || dept1.name == \"DeptD\")");
        q.declareVariables("Department dept1");
        c = (Collection) q.execute();
        assertEquals(2, 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 24 with Manager

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

the class JDOQLContainerTest method testQueryUsesContainsOnceOnOneUnboundVariableInverseUsingParameter.

/**
 * test query with "field.contains(x)" using a workaround
 */
public void testQueryUsesContainsOnceOnOneUnboundVariableInverseUsingParameter() {
    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(Department.class);
        q.setFilter("name == 'DeptA'");
        q.setUnique(true);
        Department da = (Department) q.execute();
        q = pm.newQuery(Manager.class);
        q.setFilter("departments.contains(deptA)");
        q.declareParameters("Department deptA");
        Collection c = (Collection) q.execute(da);
        assertEquals(1, c.size());
        q = pm.newQuery(Department.class);
        q.setFilter("name == 'DeptC'");
        q.setUnique(true);
        Department dc = (Department) q.execute();
        q = pm.newQuery(Manager.class);
        q.setFilter("departments.contains(deptA) || departments.contains(deptC) ");
        q.declareParameters("Department deptA, Department deptC");
        c = (Collection) q.execute(da, dc);
        assertEquals(2, c.size());
        q = pm.newQuery(Manager.class);
        q.setFilter("departments.contains(deptA) && deptA.manager == this");
        q.declareParameters("Department deptA");
        c = (Collection) q.execute(da);
        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 25 with Manager

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

the class JDOQLContainerTest method testCollectionIsEmpty.

/**
 * Test for the Collection.isEmpty() method.
 */
public void testCollectionIsEmpty() {
    try {
        Manager homer = new Manager(1, "Homer", "Simpson", "homer@simpson.com", 1, "serial 1");
        Employee bart = new Employee(2, "Bart", "Simpson", "bart@simpson.com", 2, "serial 2");
        Employee lisa = new Employee(3, "Lisa", "Simpson", "lisa@simpson.com", 4, "serial 3");
        Manager homersBrother = new Manager(4, "Homer Jr", "Simpson", "homerjr@simpson.com", 1, "serial 4");
        bart.setManager(homer);
        lisa.setManager(homer);
        homer.addSubordinate(lisa);
        homer.addSubordinate(bart);
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            pm.makePersistent(bart);
            pm.makePersistent(homer);
            pm.makePersistent(lisa);
            pm.makePersistent(homersBrother);
            tx.commit();
            // Try isEmpty() - should just return "homersBrother" since has no subordinates
            tx.begin();
            Query q = pm.newQuery(Manager.class);
            q.setFilter("subordinates.isEmpty()");
            Collection c = (Collection) q.execute();
            assertEquals(1, c.size());
            assertEquals(((Manager) c.iterator().next()).getFirstName(), "Homer Jr");
            tx.commit();
            // Try !isEmpty() - should just return "homer" since has subordinates
            tx.begin();
            q = pm.newQuery(Manager.class);
            q.setFilter("!subordinates.isEmpty()");
            c = (Collection) q.execute();
            assertEquals(1, c.size());
            assertEquals(((Manager) c.iterator().next()).getFirstName(), "Homer");
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception thrown during test", e);
            fail("Exception thrown while executing query with collection.isEmpty()");
        } 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) PersistenceManager(javax.jdo.PersistenceManager) JDOUserException(javax.jdo.JDOUserException)

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