use of org.jpox.samples.persistentinterfaces.Mouse in project tests by datanucleus.
the class JDOQLBasicTest method testQueryOfInterface.
/**
* Test for the querying of an interface (persistent).
*/
public void testQueryOfInterface() {
Mouse mouse1 = new Mouse();
mouse1.setId(101);
mouse1.setManufacturer("Logitech");
mouse1.setModel("M305");
Keyboard kb1 = new Keyboard();
kb1.setId(102);
kb1.setManufacturer("Logitech");
kb1.setModel("K304");
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
pm.makePersistent(mouse1);
pm.makePersistent(kb1);
tx.commit();
tx.begin();
try {
Query q = pm.newQuery(ComputerPeripheral.class);
List<ComputerPeripheral> results = (List<ComputerPeripheral>) q.execute();
assertEquals("Number of results incorrect", 2, results.size());
Iterator<ComputerPeripheral> resultsIter = results.iterator();
boolean mousePresent = false;
boolean kbPresent = false;
while (resultsIter.hasNext()) {
ComputerPeripheral peri = resultsIter.next();
if (peri instanceof Mouse && peri.getId() == 101 && peri.getManufacturer().equals("Logitech") && peri.getModel().equals("M305")) {
mousePresent = true;
}
if (peri instanceof Keyboard && peri.getId() == 102 && peri.getManufacturer().equals("Logitech") && peri.getModel().equals("K304")) {
kbPresent = true;
}
}
if (!mousePresent) {
fail("Mouse not present in results");
}
if (!kbPresent) {
fail("Keyboard not present in results");
}
q.closeAll();
} catch (JDOUserException e) {
fail(e.getMessage());
}
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
// Clean out our data
clean(Mouse.class);
clean(Keyboard.class);
}
}
Aggregations