Search in sources :

Example 1 with ComputerPeripheral

use of org.jpox.samples.persistentinterfaces.ComputerPeripheral 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);
    }
}
Also used : Mouse(org.jpox.samples.persistentinterfaces.Mouse) Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) Keyboard(org.jpox.samples.persistentinterfaces.Keyboard) List(java.util.List) ArrayList(java.util.ArrayList) JDOUserException(javax.jdo.JDOUserException) ComputerPeripheral(org.jpox.samples.persistentinterfaces.ComputerPeripheral)

Aggregations

ArrayList (java.util.ArrayList)1 List (java.util.List)1 JDOUserException (javax.jdo.JDOUserException)1 PersistenceManager (javax.jdo.PersistenceManager)1 Query (javax.jdo.Query)1 Transaction (javax.jdo.Transaction)1 ComputerPeripheral (org.jpox.samples.persistentinterfaces.ComputerPeripheral)1 Keyboard (org.jpox.samples.persistentinterfaces.Keyboard)1 Mouse (org.jpox.samples.persistentinterfaces.Mouse)1