use of javax.jdo.JDOUserException in project tests by datanucleus.
the class SCOCollectionTests method checkEquals.
public static void checkEquals(PersistenceManagerFactory pmf, Class container_class, Class item_class_parent) {
int NO_OF_ITEMS = 5;
Object container1_id = null;
Object container2_id = null;
// Create two equal containers
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
CollectionHolder container1 = createAndFillContainer(container_class, item_class_parent, NO_OF_ITEMS);
CollectionHolder container2 = null;
container2 = createContainer(container_class, container2);
container2.addItems(container1.getItems());
// check before persisting
Assert.assertEquals(container1.getItems(), container2.getItems());
pm.makePersistent(container1);
pm.makePersistent(container2);
// check after persisting inside tx
Assert.assertEquals(container1.getItems(), container2.getItems());
container1_id = JDOHelper.getObjectId(container1);
container2_id = JDOHelper.getObjectId(container2);
tx.commit();
} catch (JDOUserException e) {
LOG.error("Exception in test", e);
Assert.assertTrue("Exception thrown while creating " + container_class.getName() + " " + e.getMessage(), false);
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// Retrieve the two containers, check equality again, change order in one of them
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
ListHolder container1 = (ListHolder) pm.getObjectById(container1_id, false);
Assert.assertNotNull(container1);
ListHolder container2 = (ListHolder) pm.getObjectById(container2_id, false);
Assert.assertNotNull(container2);
// must still be equal after retrieval
Assert.assertEquals(container1.getItems(), container2.getItems());
// exchange first and last item in first container
Object firstItem = container1.getItem(0);
Object lastItem = container1.getItem(NO_OF_ITEMS - 1);
((List) container1.getItems()).set(0, lastItem);
((List) container1.getItems()).set(NO_OF_ITEMS - 1, firstItem);
Assert.assertFalse(container1.getItems().equals(container2.getItems()));
tx.commit();
} catch (JDOUserException e2) {
LOG.error("Exception in test", e2);
Assert.assertTrue("Exception thrown while manipulating " + container_class.getName() + " " + e2.getMessage(), false);
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
}
use of javax.jdo.JDOUserException in project tests by datanucleus.
the class SCOCollectionTests method checkElementInheritance.
/**
* Utility for checking the use of inherited elements within a collection.
* @param pmf The PersistenceManager factory
* @param container_class The container class e.g ArrayListNormal
* @param collection_class The collection return e.g java.util.ArrayList
* @param item_class_parent The parent element class
* @param item_class_child The child element class
*/
public static void checkElementInheritance(PersistenceManagerFactory pmf, Class container_class, Class collection_class, Class item_class_parent, Class item_class_child) throws Exception {
Object container_id = null;
// Create the container
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
try {
// Create a container and a few items
CollectionHolder container = (CollectionHolder) container_class.newInstance();
Object item = null;
item = SCOHolderUtilities.createItemParent(item_class_parent, "Toaster", 12.99, 0);
SCOHolderUtilities.addItemToCollection(container, item);
item = SCOHolderUtilities.createItemChild(item_class_child, "Kettle", 15.00, 3, "KETT1");
SCOHolderUtilities.addItemToCollection(container, item);
item = SCOHolderUtilities.createItemChild(item_class_child, "Hifi", 99.99, 0, "HIFI");
SCOHolderUtilities.addItemToCollection(container, item);
item = SCOHolderUtilities.createItemParent(item_class_parent, "Curtains", 5.99, 1);
SCOHolderUtilities.addItemToCollection(container, item);
// Persist the container (and all of the items)
pm.makePersistent(container);
container_id = JDOHelper.getObjectId(container);
} catch (Exception e1) {
Assert.fail("Failed to find Container class " + container_class.getName());
}
tx.commit();
} catch (JDOUserException e) {
Assert.assertTrue("Exception thrown while creating " + container_class.getName() + " " + e.getMessage(), false);
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// Query the container and check the 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 4", container_size == 4);
// Interrogate the items
Iterator item_iter = SCOHolderUtilities.getCollectionItemsIterator(container);
int no_parent = 0;
int no_child = 0;
while (item_iter.hasNext()) {
Object item = item_iter.next();
if (item_class_child.isAssignableFrom(item.getClass())) {
no_child++;
} else if (item_class_parent.isAssignableFrom(item.getClass())) {
no_parent++;
}
}
Assert.assertTrue("No of " + item_class_parent.getName() + " is incorrect (" + no_parent + ") : should have been 2", no_parent == 2);
Assert.assertTrue("No of " + item_class_child.getName() + " is incorrect (" + no_child + ") : should have been 2", no_child == 2);
}
tx.commit();
} catch (JDOUserException e) {
Assert.assertTrue("Exception thrown while querying " + container_class.getName() + e.getMessage(), false);
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
}
use of javax.jdo.JDOUserException in project tests by datanucleus.
the class SCOCollectionTests method checkPersistCollectionByElement.
/**
* Utility for checking the persistence of a 1-N bidir relation by persisting the element.
* @param pmf The PersistenceManager factory
* @param container_class The container class e.g Set1
* @param item_class_parent The parent element class
*/
public static void checkPersistCollectionByElement(PersistenceManagerFactory pmf, Class container_class, Class item_class_parent) throws Exception {
int NO_OF_ITEMS = 2;
Object container_id = null;
// Create the container
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
CollectionHolder container = null;
container = createContainer(container_class, container);
Object itemToPersist = null;
for (int i = 0; i < NO_OF_ITEMS; i++) {
Collection c = new HashSet();
// Create an item
Object item = SCOHolderUtilities.createItemParent(item_class_parent, "Item " + i, 0.00 + (10.00 * i), i);
SCOHolderUtilities.setContainerForItem(item_class_parent, item, container_class, container);
if (i == 0) {
// Persist from the first of the elements.
itemToPersist = item;
}
// Add the items to the container
c.add(item);
SCOHolderUtilities.addItemsToCollection(container, c);
}
pm.makePersistent(itemToPersist);
Assert.assertEquals("Number of elements in container just after persist is wrong", NO_OF_ITEMS, container.getNoOfItems());
container_id = JDOHelper.getObjectId(container);
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 check the 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, container_size == NO_OF_ITEMS);
}
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();
}
}
use of javax.jdo.JDOUserException in project tests by datanucleus.
the class SCOCollectionTests method checkRemoveCollection.
/**
* Utility for checking the removal of a collection of elements. Calls the
* removeAll method on a collection container.
* @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 checkRemoveCollection(PersistenceManagerFactory pmf, Class container_class, Class collection_class, Class item_class_parent) throws Exception {
int NO_OF_ITEMS = 5;
Object container_id = null;
// Create the container
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
CollectionHolder container = createAndFillContainer(container_class, item_class_parent, NO_OF_ITEMS);
pm.makePersistent(container);
container_id = JDOHelper.getObjectId(container);
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 odd numbered items
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
int no_to_remove = 0;
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, container_size == NO_OF_ITEMS);
// Select the items to remove
Iterator item_iter = SCOHolderUtilities.getCollectionItemsIterator(container);
int i = 0;
Collection c = new HashSet();
while (item_iter.hasNext()) {
Object item = item_iter.next();
if ((i / 2) * 2 == i) {
c.add(item);
no_to_remove++;
}
i++;
}
// Remove the items from the container
SCOHolderUtilities.removeItemsFromCollection(container, c);
}
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 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 - no_to_remove), container_size == (NO_OF_ITEMS - no_to_remove));
}
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();
}
}
use of javax.jdo.JDOUserException in project tests by datanucleus.
the class SCOMapTests method checkEntrySet.
/**
* Utility for checking the use of entrySet. Calls the
* entrySet method on a map container and checks the contents.
* @param pmf The PersistenceManager factory
* @param container_class The container class e.g HashMapNormal
* @param item_class_parent The parent element class
* @param key_class The key class
*/
public static void checkEntrySet(PersistenceManagerFactory pmf, Class container_class, Class item_class_parent, Class key_class) throws Exception {
int NO_OF_ITEMS = 5;
Object container_id = null;
java.util.Collection keys = null;
java.util.Collection values = null;
java.util.Map entries = null;
// Create the container
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.setRetainValues(true);
tx.begin();
// Create a container
MapHolder container = null;
try {
container = (MapHolder) container_class.newInstance();
} catch (Exception e1) {
LOG.error(e1);
Assert.fail("Failed to find Container class " + container_class.getName());
}
// Create a few items and add them to a Map
java.util.Map m = new java.util.HashMap();
entries = new java.util.HashMap();
values = new java.util.HashSet();
keys = new java.util.HashSet();
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);
Object k;
if (key_class.getName().equals(String.class.getName())) {
k = new String("Key" + i);
} else {
k = SCOHolderUtilities.createItemParent(key_class, "Key " + i, 0.00 + (11.13 * i), i);
}
m.put(k, item);
entries.put(k, item);
values.add(item);
keys.add(k);
}
// Add the items to the container
SCOHolderUtilities.addItemsToMap(container, m);
pm.makePersistent(container);
container_id = JDOHelper.getObjectId(container);
tx.commit();
} catch (JDOUserException e) {
LOG.error(e);
Assert.assertTrue("Exception thrown while creating " + container_class.getName() + " " + e.getMessage(), false);
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// Find the container and check the items
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
MapHolder container = (MapHolder) pm.getObjectById(container_id, false);
if (container != null) {
// Get the entry set from the container
java.util.Set entry_set = SCOHolderUtilities.getEntrySetFromMap(container);
Assert.assertTrue("EntrySet has incorrect number of items (" + entry_set.size() + ") : should have been " + NO_OF_ITEMS, entry_set.size() == NO_OF_ITEMS);
Assert.assertTrue("Entries are not the same, result = " + entry_set + " expected = " + entries.entrySet(), SCOHolderUtilities.compareSet(entry_set, entries.entrySet()));
}
tx.commit();
} catch (JDOUserException e2) {
LOG.error(e2);
Assert.assertTrue("Exception thrown while manipulating " + container_class.getName() + " " + e2.getMessage(), false);
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
}
Aggregations