Search in sources :

Example 1 with Product

use of org.datanucleus.samples.store.Product 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)

Example 2 with Product

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

the class ExtentTest method testCloseAll.

/**
 * Test the closure of Extent iterators.
 */
public void testCloseAll() throws Exception {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        Extent<Product> ex = pm.getExtent(Product.class, false);
        // none open
        ex.closeAll();
        Iterator<Product> i = ex.iterator();
        // one open
        ex.closeAll();
        assertEquals("iterator.hasNext() after extent.closeAll", false, i.hasNext());
        ex.iterator();
        ex.iterator();
        // two open
        ex.closeAll();
        tx.commit();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) Product(org.datanucleus.samples.store.Product)

Example 3 with Product

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

the class AttachDetachReplicateTest method testReplicateSimple.

/**
 * Test of replication, with no relations.
 * Just detaches simple objectsm abd attaches them to a different datastore
 * and so should retain the ids.
 */
public void testReplicateSimple() {
    PersistenceManagerFactory pmf2 = getPersistenceManagerFactory2();
    try {
        PersistenceManager pm1 = pmf.getPersistenceManager();
        Product prod = null;
        Object id = null;
        // Persist in first DB
        Transaction tx = null;
        try {
            tx = pm1.currentTransaction();
            tx.begin();
            Product prod1 = new Product("1", "Cup", "A tea cup", "http://www.jpox.org", "GBP", 12.50, 0.00, 0.00, 17.5, 1);
            prod1 = (Product) pm1.makePersistent(prod1);
            // Detach it for copying
            prod = (Product) pm1.detachCopy(prod1);
            tx.commit();
            id = pm1.getObjectId(prod1);
        } catch (JDOUserException ue) {
            LOG.error(ue);
            fail("Exception thrown while creating object in first datastore : " + ue.getMessage());
        } finally {
            if (tx != null && tx.isActive()) {
                tx.rollback();
            }
            pm1.close();
        }
        // Check the detached object
        if (!JDOHelper.isDetached(prod)) {
            fail("Product has not been detached!");
        }
        // Copy to other DB
        PersistenceManager pm2 = pmf2.getPersistenceManager();
        try {
            tx = pm2.currentTransaction();
            tx.begin();
            pm2.makePersistent(prod);
            tx.commit();
        } catch (JDOUserException ue) {
            LOG.error(ue);
            fail("Exception thrown while copying object into second datastore : " + ue.getMessage());
        } finally {
            if (tx != null && tx.isActive()) {
                tx.rollback();
            }
        }
        // Check the persistence in the second datastore
        try {
            tx = pm2.currentTransaction();
            tx.begin();
            // Use Extent since PM may have just put object in cache.
            Extent e = pm2.getExtent(Product.class);
            Iterator iter = e.iterator();
            boolean copied = false;
            while (iter.hasNext()) {
                Product p = (Product) iter.next();
                if (pm2.getObjectId(p).equals(id)) {
                    copied = true;
                    break;
                }
            }
            assertTrue("Product was not copied to second datastore!", copied);
            tx.commit();
        } catch (JDOUserException ue) {
            LOG.error(ue);
            fail("Exception thrown while querying object in second datastore : " + ue.getMessage());
        } finally {
            if (tx != null && tx.isActive()) {
                tx.rollback();
            }
            pm2.close();
        }
    } finally {
        PersistenceManagerFactory[] pmfs = new PersistenceManagerFactory[] { pmf, pmf2 };
        for (int i = 0; i < pmfs.length; ++i) {
            clean(pmf, Product.class);
        }
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) Extent(javax.jdo.Extent) Iterator(java.util.Iterator) Product(org.datanucleus.samples.store.Product) PersistenceManagerFactory(javax.jdo.PersistenceManagerFactory) JDOUserException(javax.jdo.JDOUserException)

Aggregations

PersistenceManager (javax.jdo.PersistenceManager)3 Transaction (javax.jdo.Transaction)3 Product (org.datanucleus.samples.store.Product)3 JDOUserException (javax.jdo.JDOUserException)2 Iterator (java.util.Iterator)1 Extent (javax.jdo.Extent)1 PersistenceManagerFactory (javax.jdo.PersistenceManagerFactory)1 Book (org.datanucleus.samples.store.Book)1 CompactDisc (org.datanucleus.samples.store.CompactDisc)1