for (int i = 0; i < NB_PERSON; i++) {
Address a = new Address("p_street_" + i,
"p_city_" + i,
"p_state_" + i,
"p_zipcode_" + i);
Person p = new Person("person_fn_" + i, "person_ln_" + i, a, d);
pm.makePersistent(p);
}
final int NB_WORKER = 4;
for (int i = 0; i < NB_WORKER; i++) {
Address a = new Address("w_street_" + i,
"w_city_" + i,
"w_state_" + i,
"w_zipcode_" + i);
Worker w = new Worker("worker_fn_" + i, "worker_ln_" + i, a, d,
"jobName_" + i, "company_" + i, (float) (i * 2000.0));
pm.makePersistent(w);
}
pm.currentTransaction().commit();
pm.evictAll();
pm.close();
//find worker only
pm = pmf.getPersistenceManager();
pm.currentTransaction().begin();
Query q = pm.newQuery(Worker.class);
q.setOrdering("firstname ascending");
Collection c = (Collection) q.execute();
int cpt = 0;
try {
for (Iterator it = c.iterator(); it.hasNext();) {
Worker w = (Worker) it.next();
assertEquals("Bad worker first name",
"worker_fn_" + cpt, w.getFirstname());
cpt ++;
}
assertEquals("Bad worker number", NB_WORKER, cpt);
} finally {
q.closeAll();
pm.currentTransaction().commit();
pm.evictAll();
pm.close();
}
//find Person instance (without Worker)
pm = pmf.getPersistenceManager();
pm.currentTransaction().begin();
q = pm.newQuery(pm.getExtent(Person.class, false));
q.setOrdering("firstname ascending");
c = (Collection) q.execute();
cpt = 0;
try {
for (Iterator it = c.iterator(); it.hasNext();) {
Person p = (Person) it.next();
assertEquals("Bad person first name", "person_fn_" + cpt, p.getFirstname());
cpt ++;
}
assertEquals("Bad person and worker number", NB_PERSON, cpt);
} finally {
q.closeAll();
pm.currentTransaction().commit();
pm.evictAll();
pm.close();
}
//find all persons (including Worker)
pm = pmf.getPersistenceManager();
pm.currentTransaction().begin();
q = pm.newQuery(pm.getExtent(Person.class, true));
q.setOrdering("firstname ascending");
c = (Collection) q.execute();
cpt = 0;
try {
for (Iterator it = c.iterator(); it.hasNext();) {
Person p = (Person) it.next();
String expectedFN;
if (cpt < NB_PERSON) {
expectedFN = "person_fn_" + cpt;
} else {
expectedFN = "worker_fn_" + (cpt - NB_PERSON);
}
assertEquals("Bad person first name", expectedFN, p.getFirstname());
cpt ++;
}
assertEquals("Bad person and worker number", NB_WORKER + NB_PERSON, cpt);
} finally {
q.closeAll();