Search in sources :

Example 1 with CompactDisc

use of org.datanucleus.samples.store.CompactDisc in project tests by datanucleus.

the class ExtentTest method testExtentOfNewTable.

/**
 * Test the use of extents with a class using "new-table" inheritance strategy.
 */
public void testExtentOfNewTable() throws Exception {
    try {
        // Create some objects.
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            CompactDisc cd = new CompactDisc("ABC", "Greatest Hits", "CD of our greatest hits", "http://www.jpox.org", "GBP", 10.00, 10.00, 10.00, 17.5, 1, "JPOX", "Greatest Hits", 2006, "JPOX Production", "JPOX Publishing");
            Book book = new Book("DEF", "Long Stories", "Book of long stories", "http://www.amazon.com", "EUR", 5.99, 5.99, 5.99, 12.0, 2, "54321672578", "John Storyteller", "Long stories", 1, "McGraw Hill");
            pm.makePersistent(cd);
            pm.makePersistent(book);
            tx.commit();
        } catch (JDOUserException ue) {
            assertTrue("Exception thrown during create of objects using new-table inheritance", false);
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // Try to get extent of base class
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            Extent<Product> e = pm.getExtent(Product.class, true);
            int number = 0;
            Iterator<Product> iter = e.iterator();
            while (iter.hasNext()) {
                iter.next();
                number++;
            }
            assertEquals("Extent for classes with new-table inheritance strategy returned incorrect number of objects", number, 2);
            tx.commit();
        } catch (Exception e) {
            fail("Exception was thrown when requesting Extent of class using new-table inheritance strategy");
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        // Clean out created data
        clean(CompactDisc.class);
        clean(Book.class);
    }
}
Also used : CompactDisc(org.datanucleus.samples.store.CompactDisc) Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) Book(org.datanucleus.samples.store.Book) Product(org.datanucleus.samples.store.Product) JDOUserException(javax.jdo.JDOUserException) JDOUserException(javax.jdo.JDOUserException)

Aggregations

JDOUserException (javax.jdo.JDOUserException)1 PersistenceManager (javax.jdo.PersistenceManager)1 Transaction (javax.jdo.Transaction)1 Book (org.datanucleus.samples.store.Book)1 CompactDisc (org.datanucleus.samples.store.CompactDisc)1 Product (org.datanucleus.samples.store.Product)1