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();
}
}
Aggregations