use of org.jpox.samples.models.company.Manager in project tests by datanucleus.
the class JDOQLBasicTest method testExecuteUnique.
/**
* test use of executeUnique()
*/
public void testExecuteUnique() {
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();
try {
Query q = pm.newQuery(Manager.class).filter("firstName == :myName").setParameters("Homer");
// Should return single object not a List
Object obj = q.executeUnique();
assertNotNull(obj);
assertTrue(obj instanceof Manager);
} catch (JDOUserException ue) {
// Expected
}
tx.rollback();
} finally {
if (tx.isActive()) {
pm.currentTransaction().rollback();
}
pm.close();
}
}
use of org.jpox.samples.models.company.Manager in project tests by datanucleus.
the class JDOQLBasicTest method testQueryUsesInnerJoin.
public void testQueryUsesInnerJoin() {
try {
Manager woody = new Manager(1, "Woody", "Woodpecker", "woody@woodpecker.com", 13, "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");
Department deptA = new Department("DeptA");
Department deptB = new Department("DeptB");
deptB.setManager(bart);
Department deptC = new Department("DeptC");
deptC.setManager(boss);
// I hate the gravity
Person coyote = new Person(5, "Wile", "E. Coyote", "wile.coyote@acme.com");
// paranoid, and neurotic
Person duck = new Person(6, "Daffy", "Duck", "daffy.duck@warnerbros.com");
// You are my peanut.
Person pepe = new Person(7, "Pepe", "le Pew", "pepe.lepew@warnerbros.com");
// You are my peanut.
Person pepe2 = new Person(8, "Pepe", "le Dawn", "pepe.dawn@warnerbros.com");
Qualification qA = new Qualification("QA");
Qualification qB = new Qualification("QB");
qB.setPerson(duck);
Qualification qC = new Qualification("QC");
qC.setPerson(pepe);
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
// test with inheritance
tx.begin();
pm.makePersistent(deptC);
pm.makePersistent(deptA);
pm.makePersistent(deptB);
pm.makePersistent(woody);
pm.makePersistent(boss2);
tx.commit();
tx.begin();
Query q = pm.newQuery(Department.class, "manager.firstName == \"Boss\"");
Collection c = q.executeList();
assertEquals(c.size(), 1);
assertEquals(((Department) c.iterator().next()).getName(), "DeptC");
tx.commit();
// test without inheritance
tx.begin();
pm.makePersistent(qC);
pm.makePersistent(qA);
pm.makePersistent(qB);
pm.makePersistent(coyote);
pm.makePersistent(pepe2);
tx.commit();
tx.begin();
q = pm.newQuery(Qualification.class, "person.firstName == \"Pepe\"");
c = q.executeList();
assertEquals(c.size(), 1);
assertEquals(((Qualification) c.iterator().next()).getName(), "QC");
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 JDOQLBasicTest method testInstanceof.
/**
* Test case to use JDOQL "instanceof"
*/
public void testInstanceof() {
try {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
// 1. instanceof where the candidate class uses "new-table" (union)
tx.begin();
Employee woody = new Employee(1, "Woody", "Woodpecker", "woody@woodpecker.com", 13, "serial 1");
Employee bart = new Employee(2, "Bart", "Simpson", "bart@simpson.com", 2, "serial 2");
// Eh, what's up, doc?
Employee bunny = new Employee(3, "Bugs", "Bunny", "bugs.bunny@warnerbros.com", 12, "serial 3");
// Meep! Meep!
Manager roadrunner = new Manager(4, "Road", "Runner", "road.runner@warnerbros.com", 11, "serial 4");
pm.makePersistent(woody);
pm.makePersistent(bart);
pm.makePersistent(bunny);
pm.makePersistent(roadrunner);
tx.commit();
tx.begin();
try {
Query q = pm.newQuery(Employee.class, "this instanceof " + Manager.class.getName());
List results = (List) q.execute();
assertEquals("Number of objects returned from instanceof was incorrect", results.size(), 1);
Object obj = results.get(0);
assertTrue("Type of object returned should have been Manager but wasnt", obj instanceof Manager);
q.closeAll();
} catch (JDOUserException e) {
fail(e.getMessage());
}
tx.commit();
// 2. instanceof where the candidate class using "superclass-table" (discriminator)
tx.begin();
ElectronicJournal elecJournal = new ElectronicJournal(1, "Electronics Weekly");
PrintJournal printJournal = new PrintJournal(2, "Glossy magazine");
pm.makePersistent(elecJournal);
pm.makePersistent(printJournal);
tx.commit();
tx.begin();
try {
Query q = pm.newQuery(AbstractJournal.class, "this instanceof " + PrintJournal.class.getName());
List results = (List) q.execute();
assertEquals("Number of objects returned from instanceof was incorrect", results.size(), 1);
Object obj = results.get(0);
assertTrue("Type of object returned should have been PrintJournal but wasnt", obj instanceof PrintJournal);
q.closeAll();
} catch (JDOUserException e) {
fail(e.getMessage());
}
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
// Clean out our data
clean(PrintJournal.class);
clean(ElectronicJournal.class);
CompanyHelper.clearCompanyData(pmf);
}
}
use of org.jpox.samples.models.company.Manager in project tests by datanucleus.
the class JDOQLBasicTest method testQueryUnboundVariablesInheritanceRightHandDeclared1.
public void testQueryUnboundVariablesInheritanceRightHandDeclared1() {
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.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(deptA);
pm.makePersistent(deptB);
tx.commit();
tx.begin();
Query q = pm.newQuery(Manager.class);
Collection c = q.filter("this.subordinates.contains(emp) && emp.lastName == \"WakesUp2\" ").variables("Employee emp").imports("import org.jpox.samples.models.company.Employee").executeList();
assertEquals(1, c.size());
assertEquals(((Manager) c.iterator().next()).getFirstName(), "Bart");
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 testQueryUsesContainsOnceOnOneUnboundVariable.
/**
* test query with "field.contains(x)" using a workaround
*/
public void testQueryUsesContainsOnceOnOneUnboundVariable() {
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\"");
q.declareVariables("Employee emp1");
q.declareImports("import org.jpox.samples.models.company.Employee");
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\")");
q.declareVariables("Employee emp1");
q.declareImports("import org.jpox.samples.models.company.Employee");
c = (Collection) q.execute();
assertEquals(1, c.size());
q = pm.newQuery(Manager.class);
q.setFilter("subordinates.contains(emp1) && (emp1.lastName == \"WakesUp\" || emp1.lastName == \"WakesUp4\")");
q.declareVariables("Employee emp1");
q.declareImports("import org.jpox.samples.models.company.Employee");
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);
}
}
Aggregations