Search in sources :

Example 1 with JTransparentMarble

use of org.jpox.samples.inheritance.JTransparentMarble in project tests by datanucleus.

the class InheritanceTest method testInverseCollectionWithElementUsingSuperclassTable.

/**
 * Test of a collection of objects which are of type "new-table" (base) with 2 subclasses
 * both using "superclass-table".
 */
public void testInverseCollectionWithElementUsingSuperclassTable() {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        Object[] id = new Object[4];
        try {
            tx.begin();
            // Create an assortment of bowls
            JBowl[] bowl = new JBowl[4];
            bowl[0] = new JBowl(1, "Bowl of Spotted Marbles");
            bowl[1] = new JBowl(2, "Bowl of Transparent Marbles");
            bowl[2] = new JBowl(3, "Bowl of Marbles");
            bowl[3] = new JBowl(4, "Bowl of Plain Marbles");
            // Create some marbles
            JMarble[] marble = new JMarble[18];
            marble[0] = new JSpottedMarble(1, "purple", "yellow");
            marble[1] = new JSpottedMarble(2, "navy", "red");
            marble[2] = new JSpottedMarble(3, "burgandy", "blue");
            marble[3] = new JSpottedMarble(4, "teal", "green");
            marble[4] = new JSpottedMarble(5, "purple", "yellow");
            marble[5] = new JSpottedMarble(6, "navy", "red");
            marble[6] = new JSpottedMarble(7, "burgandy", "blue");
            marble[7] = new JSpottedMarble(8, "teal", "green");
            // Assign marbles to bowls
            bowl[0].addMarble(marble[0]);
            bowl[0].addMarble(marble[1]);
            bowl[0].addMarble(marble[2]);
            bowl[0].addMarble(marble[3]);
            bowl[2].addMarble(marble[4]);
            bowl[2].addMarble(marble[5]);
            bowl[2].addMarble(marble[6]);
            bowl[2].addMarble(marble[7]);
            marble[8] = new JMarble(100, "burgandy");
            marble[9] = new JMarble(101, "burgandy");
            // bowl[3].getSpottedMarbles().add(marble[8]);
            // bowl[3].getSpottedMarbles().add(marble[9]);
            marble[10] = new JTransparentMarble(11, "indigo", 95);
            marble[11] = new JTransparentMarble(12, "taupe", 64);
            marble[12] = new JTransparentMarble(13, "cyan", 41);
            marble[13] = new JTransparentMarble(14, "magenta", 17);
            bowl[1].addMarble(marble[10]);
            bowl[1].addMarble(marble[11]);
            bowl[1].addMarble(marble[12]);
            bowl[1].addMarble(marble[13]);
            marble[14] = new JTransparentMarble(15, "indigo", 59);
            marble[15] = new JTransparentMarble(16, "taupe", 46);
            marble[16] = new JTransparentMarble(17, "cyan", 14);
            marble[17] = new JTransparentMarble(18, "magenta", 71);
            bowl[2].addMarble(marble[14]);
            bowl[2].addMarble(marble[15]);
            bowl[2].addMarble(marble[16]);
            bowl[2].addMarble(marble[17]);
            pm.makePersistentAll(bowl);
            tx.commit();
            id[0] = pm.getObjectId(bowl[0]);
            id[1] = pm.getObjectId(bowl[1]);
            id[2] = pm.getObjectId(bowl[2]);
            id[3] = pm.getObjectId(bowl[3]);
            // Query the bowls
            tx.begin();
            bowl[0] = (JBowl) pm.getObjectById(id[0], true);
            bowl[1] = (JBowl) pm.getObjectById(id[1], true);
            bowl[2] = (JBowl) pm.getObjectById(id[2], true);
            bowl[3] = (JBowl) pm.getObjectById(id[3], true);
            assertEquals(4, bowl[0].getSpottedMarbles().size());
            assertEquals(0, bowl[0].getTransparentMarbles().size());
            assertEquals(0, bowl[1].getSpottedMarbles().size());
            assertEquals(4, bowl[1].getTransparentMarbles().size());
            assertEquals(4, bowl[2].getSpottedMarbles().size());
            assertEquals(4, bowl[2].getTransparentMarbles().size());
            assertEquals(0, bowl[3].getSpottedMarbles().size());
            assertEquals(0, bowl[3].getTransparentMarbles().size());
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        PersistenceManager pm2 = pmf.getPersistenceManager();
        tx = pm2.currentTransaction();
        try {
            tx.begin();
            JBowl bowl = (JBowl) pm2.getObjectById(id[2]);
            // Check the correct determination of number of marbles of the 2 types
            assertTrue("Number of spotted marbles should have been 4 but was " + bowl.getNumberOfSpottedMarbles(), bowl.getNumberOfSpottedMarbles() == 4);
            assertTrue("Number of transparent marbles should have been 4 but was " + bowl.getNumberOfTransparentMarbles(), bowl.getNumberOfTransparentMarbles() == 4);
            // Check the spotted marbles
            Set spottedMarbles = bowl.getSpottedMarbles();
            assertTrue("Number of spotted marbles retrieved via collection should have been 4 but was " + spottedMarbles.size(), spottedMarbles.size() == 4);
            Iterator spottedIter = spottedMarbles.iterator();
            while (spottedIter.hasNext()) {
                spottedIter.next();
            }
            // Check the transparent marbles
            Set transparentMarbles = bowl.getTransparentMarbles();
            assertTrue("Number of transparent marbles retrieved via collection should have been 4 but was " + transparentMarbles.size(), transparentMarbles.size() == 4);
            Iterator transparentIter = transparentMarbles.iterator();
            while (transparentIter.hasNext()) {
                transparentIter.next();
            }
            tx.commit();
        } catch (Exception e) {
            e.printStackTrace();
            LOG.error(e);
            fail("Exception thrown while querying container with superclass-table objects : " + e.getMessage());
        } finally {
            if (tx != null && tx.isActive()) {
                tx.rollback();
            }
            pm2.close();
        }
    } finally {
        clean(JBowl.class);
        clean(JSpottedMarble.class);
        clean(JTransparentMarble.class);
        clean(JMarble.class);
    }
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) PersistenceManager(javax.jdo.PersistenceManager) JTransparentMarble(org.jpox.samples.inheritance.JTransparentMarble) JDOUserException(javax.jdo.JDOUserException) Transaction(javax.jdo.Transaction) Iterator(java.util.Iterator) JMarble(org.jpox.samples.inheritance.JMarble) JBowl(org.jpox.samples.inheritance.JBowl) JSpottedMarble(org.jpox.samples.inheritance.JSpottedMarble)

Aggregations

HashSet (java.util.HashSet)1 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 JBowl (org.jpox.samples.inheritance.JBowl)1 JMarble (org.jpox.samples.inheritance.JMarble)1 JSpottedMarble (org.jpox.samples.inheritance.JSpottedMarble)1 JTransparentMarble (org.jpox.samples.inheritance.JTransparentMarble)1