Search in sources :

Example 1 with MediaWork

use of org.jpox.samples.one_one.unidir_2.MediaWork in project tests by datanucleus.

the class ExtentTest method testExtentOfSubclassTable.

/**
 * Test the use of extents with a class using "subclass-table" inheritance strategy.
 */
public void testExtentOfSubclassTable() throws Exception {
    try {
        // Create some objects.
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            Newspaper paper = new Newspaper("Daily Vermin", 2, "George Bush", "Sleaze");
            Magazine mag = new Magazine("Buenos Dias", 4, "Cachorro sa");
            pm.makePersistent(paper);
            pm.makePersistent(mag);
            tx.commit();
        } catch (JDOUserException ue) {
            assertTrue("Exception thrown during create of objects using subclass-table inheritance", false);
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // Try to get extent of ExtentSub (superclass of both objects, using subclass-table)
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            Extent<MediaWork> e = pm.getExtent(MediaWork.class, true);
            int number = 0;
            Iterator<MediaWork> iter = e.iterator();
            while (iter.hasNext()) {
                iter.next();
                number++;
            }
            assertEquals("Extent for classes with subclass-table inheritance strategy returned incorrect number of objects", number, 2);
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception thrown for Extent of class using subclass-table", e);
            e.printStackTrace();
            fail("Exception was thrown when requesting Extent of class using subclass-table inheritance strategy");
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        // Clean out created data
        clean(Newspaper.class);
        clean(Magazine.class);
    }
}
Also used : Newspaper(org.jpox.samples.one_one.unidir_2.Newspaper) Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) JDOUserException(javax.jdo.JDOUserException) MediaWork(org.jpox.samples.one_one.unidir_2.MediaWork) Magazine(org.jpox.samples.one_one.unidir_2.Magazine) JDOUserException(javax.jdo.JDOUserException)

Aggregations

JDOUserException (javax.jdo.JDOUserException)1 PersistenceManager (javax.jdo.PersistenceManager)1 Transaction (javax.jdo.Transaction)1 Magazine (org.jpox.samples.one_one.unidir_2.Magazine)1 MediaWork (org.jpox.samples.one_one.unidir_2.MediaWork)1 Newspaper (org.jpox.samples.one_one.unidir_2.Newspaper)1