Search in sources :

Example 16 with CollectionHolder

use of org.jpox.samples.types.container.CollectionHolder in project tests by datanucleus.

the class SCOCollectionTests method checkRemoveItem.

/**
 * Utility for checking the removal of an element from a Collection.
 * @param pmf The PersistenceManager factory
 * @param container_class The container class e.g ArrayListNormal
 * @param item_class_parent The parent element class
 */
public static void checkRemoveItem(PersistenceManagerFactory pmf, Class container_class, Class item_class_parent) throws Exception {
    int NO_OF_ITEMS = 5;
    Object container_id = null;
    Object element_id = null;
    // Create the container
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        CollectionHolder container = null;
        Object element = null;
        container = createContainer(container_class, container);
        for (int i = 0; i < NO_OF_ITEMS; i++) {
            // Create an item
            Object item = SCOHolderUtilities.createItemParent(item_class_parent, "Item " + i, 0.00 + (10.00 * i), i);
            if (i == 2) {
                element = item;
            }
            // Add the item to the container
            SCOHolderUtilities.addItemToCollection(container, item);
        }
        pm.makePersistent(container);
        container_id = JDOHelper.getObjectId(container);
        element_id = JDOHelper.getObjectId(element);
        tx.commit();
    } catch (JDOUserException e) {
        LOG.error("Exception in test", e);
        Assert.fail("Exception thrown while creating " + container_class.getName() + " " + e.getMessage());
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
    // Find the container and remove the item
    pm = pmf.getPersistenceManager();
    tx = pm.currentTransaction();
    try {
        tx.begin();
        Object element = pm.getObjectById(element_id, false);
        CollectionHolder container = (CollectionHolder) pm.getObjectById(container_id, false);
        if (container != null) {
            // Get the no of items in the container
            int container_size = SCOHolderUtilities.getContainerSize(container);
            Assert.assertTrue(container_class.getName() + " has incorrect number of items (" + container_size + ") : should have been " + NO_OF_ITEMS, container_size == NO_OF_ITEMS);
            // Remove the required element
            SCOHolderUtilities.removeItemFromCollection(container, element);
        }
        tx.commit();
    } catch (JDOUserException e2) {
        LOG.error("Exception in test", e2);
        Assert.fail("Exception thrown while manipulating " + container_class.getName() + " " + e2.getMessage());
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
    // Find the container and check the new number of items
    pm = pmf.getPersistenceManager();
    tx = pm.currentTransaction();
    try {
        tx.begin();
        CollectionHolder container = (CollectionHolder) pm.getObjectById(container_id, false);
        if (container != null) {
            // Get the no of items in the container
            int container_size = SCOHolderUtilities.getContainerSize(container);
            Assert.assertTrue(container_class.getName() + " has incorrect number of items (" + container_size + ") : should have been " + (NO_OF_ITEMS - 1), container_size == (NO_OF_ITEMS - 1));
        }
        tx.commit();
    } catch (JDOUserException e4) {
        LOG.error("Exception in test", e4);
        Assert.fail("Exception thrown while manipulating " + container_class.getName() + " " + e4.getMessage());
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) CollectionHolder(org.jpox.samples.types.container.CollectionHolder) JDOUserException(javax.jdo.JDOUserException)

Aggregations

CollectionHolder (org.jpox.samples.types.container.CollectionHolder)16 JDOUserException (javax.jdo.JDOUserException)15 PersistenceManager (javax.jdo.PersistenceManager)15 Transaction (javax.jdo.Transaction)15 Collection (java.util.Collection)6 HashSet (java.util.HashSet)6 Iterator (java.util.Iterator)5 ListHolder (org.jpox.samples.types.container.ListHolder)4 List (java.util.List)1 Extent (javax.jdo.Extent)1 Query (javax.jdo.Query)1