Search in sources :

Example 1 with Cereal

use of org.jpox.samples.interfaces.Cereal in project tests by datanucleus.

the class InterfacesTest method testMappingStrategyIdentity1ToN.

/**
 * Test for use of mapping-strategy="identity" for 1-N relation
 */
public void testMappingStrategyIdentity1ToN() throws Exception {
    try {
        addClassesToSchema(new Class[] { Diet.class });
        Object id = null;
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            // Create some objects.
            tx.begin();
            Diet diet = new Diet(1);
            Steak steak = new Steak();
            Salad salad = new Salad();
            diet.getFoods().add(steak);
            diet.getFoods().add(salad);
            pm.makePersistent(diet);
            tx.commit();
            id = JDOHelper.getObjectId(diet);
        } catch (JDOUserException ue) {
            assertTrue("Exception thrown during create of interface objects.", false);
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            Diet diet = (Diet) pm.getObjectById(id);
            assertNotNull("Diet foods is null!!", diet.getFoods());
            assertEquals("Number of foods in diet is wrong", 2, diet.getFoods().size());
            Set foods = diet.getFoods();
            boolean steakFound = false;
            boolean saladFound = false;
            Iterator iter = foods.iterator();
            while (iter.hasNext()) {
                Food food = (Food) iter.next();
                if (food instanceof Steak) {
                    steakFound = true;
                }
                if (food instanceof Salad) {
                    saladFound = true;
                }
            }
            assertTrue("Steak was not retrieved as a food!", steakFound);
            assertTrue("Salad was not retrieved as a food!", saladFound);
            // Go on a healthy diet!
            foods.clear();
            foods.add(new Cereal());
            foods.add(new Salad());
            tx.commit();
        } catch (JDOUserException ue) {
            assertTrue("Exception thrown during retrieval of interface objects.", false);
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            Diet diet = (Diet) pm.getObjectById(id);
            assertNotNull("Diet foods is null!!", diet.getFoods());
            assertEquals("Number of foods in diet is wrong", 2, diet.getFoods().size());
            Set foods = diet.getFoods();
            boolean cerealFound = false;
            boolean saladFound = false;
            Iterator iter = foods.iterator();
            while (iter.hasNext()) {
                Food food = (Food) iter.next();
                if (food instanceof Cereal) {
                    cerealFound = true;
                }
                if (food instanceof Salad) {
                    saladFound = true;
                }
            }
            assertTrue("Cereal was not retrieved as a food!", cerealFound);
            assertTrue("Salad was not retrieved as a food!", saladFound);
            tx.commit();
        } catch (JDOUserException ue) {
            assertTrue("Exception thrown during create of interface objects.", false);
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        clean(Diet.class);
        clean(Steak.class);
        clean(Salad.class);
        clean(Cereal.class);
    }
}
Also used : Set(java.util.Set) Transaction(javax.jdo.Transaction) Diet(org.jpox.samples.interfaces.Diet) PersistenceManager(javax.jdo.PersistenceManager) Iterator(java.util.Iterator) Steak(org.jpox.samples.interfaces.Steak) Cereal(org.jpox.samples.interfaces.Cereal) JDOUserException(javax.jdo.JDOUserException) Salad(org.jpox.samples.interfaces.Salad) Food(org.jpox.samples.interfaces.Food)

Aggregations

Iterator (java.util.Iterator)1 Set (java.util.Set)1 JDOUserException (javax.jdo.JDOUserException)1 PersistenceManager (javax.jdo.PersistenceManager)1 Transaction (javax.jdo.Transaction)1 Cereal (org.jpox.samples.interfaces.Cereal)1 Diet (org.jpox.samples.interfaces.Diet)1 Food (org.jpox.samples.interfaces.Food)1 Salad (org.jpox.samples.interfaces.Salad)1 Steak (org.jpox.samples.interfaces.Steak)1