use of org.jpox.samples.models.company.Manager in project tests by datanucleus.
the class JDOQLContainerTest method testQueryUsesContainsTwiceOnFieldWithNamespaceInverse.
/**
* test query with "field.contains(x) && field.contains(y)"
*
* namespace put related expressions inside parentheses
*/
public void testQueryUsesContainsTwiceOnFieldWithNamespaceInverse() {
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 == \"DeptA\") && (departments.contains(dept2) && dept2.name == \"DeptB\")");
q.declareVariables("Department dept1; Department dept2");
Collection c = (Collection) q.execute();
assertEquals(1, c.size());
tx.commit();
tx.begin();
q = pm.newQuery(Manager.class);
q.setFilter("(departments.contains(dept1) && (dept1.name == \"DeptA\" || dept1.name == \"DeptC\")) && (departments.contains(dept2) && dept2.name == \"DeptB\")");
q.declareVariables("Department dept1; Department dept2");
c = (Collection) q.execute();
assertEquals(1, c.size());
tx.commit();
tx.begin();
q = pm.newQuery(Manager.class);
q.setFilter("(departments.contains(dept1) && (dept1.name == \"DeptA\" || dept1.name == \"DeptC\")) || (departments.contains(dept2) && dept2.name == \"DeptD\")");
q.declareVariables("Department dept1; Department dept2");
c = (Collection) q.execute();
assertEquals(3, c.size());
tx.commit();
tx.begin();
q = pm.newQuery(Manager.class);
q.setFilter("departments.contains(dept1) && (dept1.name == \"DeptA\" && (departments.contains(dept2) && (dept2.name.matches(\"Dept.*\") && dept1 != dept2)))");
q.declareVariables("Department dept1; Department dept2");
c = (Collection) q.execute();
assertEquals(1, c.size());
tx.commit();
} finally {
if (tx.isActive()) {
pm.currentTransaction().rollback();
}
pm.close();
// Clean out our data
CompanyHelper.clearCompanyData(pmf);
}
}
use of org.jpox.samples.models.company.Manager in project tests by datanucleus.
the class JDOQLContainerTest method testContainsResultVariableNestedContains.
public void testContainsResultVariableNestedContains() {
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");
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");
Project projA = new Project("projA", 1);
Project projB = new Project("projB", 2);
Project projC = new Project("projC", 3);
Project projD = new Project("projD", 4);
Project projE = new Project("projE", 5);
deptA.addProject(projA);
deptA.addProject(projB);
deptB.addProject(projC);
deptC.addProject(projA);
deptC.addProject(projB);
deptD.addProject(projD);
deptE.addProject(projA);
deptE.addProject(projE);
boss.addDepartment(deptA);
boss.addDepartment(deptB);
boss.addDepartment(deptC);
boss2.addDepartment(deptD);
boss2.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(d) && d.projects.contains(p)");
q.setResult("d, p");
q.declareVariables("Department d; Project p");
Collection c = (Collection) q.execute();
assertEquals(8, c.size());
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
// Clean out our data
CompanyHelper.clearCompanyData(pmf);
}
}
use of org.jpox.samples.models.company.Manager in project tests by datanucleus.
the class JDOQLContainerTest method testQueryUsesContainsOnceOnOneUnboundVariableInverse.
/**
* test query with "field.contains(x)" using a workaround
*/
public void testQueryUsesContainsOnceOnOneUnboundVariableInverse() {
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\"");
q.declareVariables("Department dept1");
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\")");
q.declareVariables("Department dept1");
c = (Collection) q.execute();
assertEquals(2, c.size());
q = pm.newQuery(Manager.class);
q.setFilter("departments.contains(dept1) && (dept1.name == \"DeptB\" || dept1.name == \"DeptA\")");
q.declareVariables("Department dept1");
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);
}
}
use of org.jpox.samples.models.company.Manager in project tests by datanucleus.
the class JDOQLContainerTest method testQueryUsesContainsOnceOnOneUnboundVariableUsingParameters.
/**
* test query with "field.contains(x)" using a workaround
* Totally stupid query ("param1.field.contains(param2)") wtf.
*/
public void testQueryUsesContainsOnceOnOneUnboundVariableUsingParameters() {
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);
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("name == 'DeptB'");
q.setUnique(true);
Department db = (Department) q.execute();
q = pm.newQuery(Manager.class);
q.setFilter("firstName == 'Bart'");
q.setUnique(true);
Manager e = (Manager) q.execute();
// ensure that field is loaded, because the query will use it
e.getDepartments();
q = pm.newQuery(Manager.class);
q.setFilter("departments.contains(db) && e.departments.contains(db)");
q.declareParameters("Department db, Employee e");
Collection c = (Collection) q.execute(db, e);
assertEquals(1, c.size());
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
// Clean out our data
CompanyHelper.clearCompanyData(pmf);
}
}
use of org.jpox.samples.models.company.Manager in project tests by datanucleus.
the class JDOQLContainerTest method testContainsInParameterArray.
public void testContainsInParameterArray() {
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);
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");
boss.addDepartment(deptA);
boss.addDepartment(deptB);
boss.addDepartment(deptC);
boss2.addDepartment(deptD);
boss2.addDepartment(deptE);
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();
Department[] depts = (Department[]) boss2.getDepartments().toArray(new Department[boss2.getDepartments().size()]);
// Get all Departments that are present in our array
Query q = pm.newQuery(Department.class);
q.setFilter("dept.contains(this)");
q.declareParameters("org.jpox.samples.models.company.Department[] dept");
q.setOrdering("this.name ascending");
Collection c = (Collection) q.execute(depts);
assertEquals(2, c.size());
Iterator it = c.iterator();
assertEquals(((Department) it.next()).getName(), "deptD");
assertEquals(((Department) it.next()).getName(), "deptE");
// Get all Managers whose Departments are present in our array
q = pm.newQuery(Manager.class);
q.setResult("distinct this");
q.setFilter("dept0.manager == this && dept.contains(dept0)");
q.declareParameters("org.jpox.samples.models.company.Department[] dept");
q.declareVariables("org.jpox.samples.models.company.Department dept0");
q.setOrdering("this.firstName ascending");
c = (Collection) q.execute(depts);
// boss2
assertEquals(1, c.size());
it = c.iterator();
// Get all Managers whose Departments are not present in our array
q = pm.newQuery(Manager.class);
q.setResult("distinct this");
q.setFilter("dept0.manager != this && dept.contains(dept0)");
q.declareParameters("org.jpox.samples.models.company.Department[] dept");
q.declareVariables("org.jpox.samples.models.company.Department dept0");
q.setOrdering("this.firstName ascending");
c = (Collection) q.execute(depts);
// not boss2
assertEquals(4, c.size());
it = c.iterator();
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
// Clean out our data
CompanyHelper.clearCompanyData(pmf);
}
}
Aggregations