use of org.jpox.samples.types.container.MapHolder 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();
}
}
use of org.jpox.samples.types.container.MapHolder in project tests by datanucleus.
the class SCOMapTests method checkKeySet.
/**
* Utility for checking the use of keySet. Calls the
* keySet 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 checkKeySet(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 items = 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();
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);
}
items = new java.util.HashSet();
items.addAll(m.keySet());
// 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 key_set = SCOHolderUtilities.getKeySetFromMap(container);
Assert.assertTrue("KeySet has incorrect number of items (" + key_set.size() + ") : should have been " + NO_OF_ITEMS, key_set.size() == NO_OF_ITEMS);
Assert.assertTrue("Collections are not the same, result = " + key_set + " expected = " + items, SCOHolderUtilities.compareSet(key_set, items));
}
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();
}
}
use of org.jpox.samples.types.container.MapHolder in project tests by datanucleus.
the class SCOMapTests method checkValues.
/**
* Utility for checking the use of values. Calls the
* keySet 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 checkValues(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 items = 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();
items = 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);
}
items.addAll(m.values());
// 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.Collection value_set = SCOHolderUtilities.getValuesFromMap(container);
Assert.assertTrue("Values has incorrect number of items (" + value_set.size() + ") : should have been " + NO_OF_ITEMS, value_set.size() == NO_OF_ITEMS);
Assert.assertTrue("Collections are not the same, result = " + value_set + " expected = " + items, SCOHolderUtilities.compareSet(value_set, items));
}
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();
}
}
use of org.jpox.samples.types.container.MapHolder in project tests by datanucleus.
the class SCOMapTests method checkValueInheritance.
/**
* Utility for checking the use of inherited values within the map.
* @param pmf The PersistenceManager factory
* @param container_class The container class e.g MapNormal
* @param item_class_parent The parent element class
* @param item_class_child The child element class
*/
public static void checkValueInheritance(PersistenceManagerFactory pmf, Class container_class, Class item_class_parent, Class item_class_child) throws Exception {
int NO_OF_ITEMS = 4;
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
MapHolder container = (MapHolder) container_class.newInstance();
Object item = null;
item = SCOHolderUtilities.createItemParent(item_class_parent, "Toaster", 12.99, 0);
SCOHolderUtilities.addItemToMap(container, "Key1", item);
item = SCOHolderUtilities.createItemChild(item_class_child, "Kettle", 15.00, 3, "KETT1");
SCOHolderUtilities.addItemToMap(container, "Key2", item);
item = SCOHolderUtilities.createItemChild(item_class_child, "Hifi", 99.99, 0, "HIFI");
SCOHolderUtilities.addItemToMap(container, "Key3", item);
item = SCOHolderUtilities.createItemParent(item_class_parent, "Curtains", 5.99, 1);
SCOHolderUtilities.addItemToMap(container, "Key4", 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();
MapHolder container = (MapHolder) 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
Object item = null;
int no_parent = 0;
int no_child = 0;
for (int i = 0; i < NO_OF_ITEMS; i++) {
item = SCOHolderUtilities.getItemFromMap(container, "Key" + (i + 1));
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 org.jpox.samples.types.container.MapHolder in project tests by datanucleus.
the class SCOMapTests method checkAttachDetach.
/**
* Utility for checking the use of detach, then attach.
* @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 checkAttachDetach(PersistenceManagerFactory pmf, Class container_class, Class item_class_parent) throws Exception {
int NO_OF_ITEMS = 5;
Object container_id = null;
MapHolder detachedContainer = null;
// Create a container and some elements
PersistenceManager pm = pmf.getPersistenceManager();
pm.getFetchPlan().addGroup("items");
Transaction tx = pm.currentTransaction();
try {
tx.begin();
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());
}
java.util.Map m = new java.util.HashMap();
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);
m.put(new String("Key" + (i + 1)), item);
}
SCOHolderUtilities.addItemsToMap(container, m);
pm.makePersistent(container);
container_id = JDOHelper.getObjectId(container);
detachedContainer = (MapHolder) pm.detachCopy(container);
tx.commit();
} catch (JDOUserException e) {
LOG.error(e);
Assert.assertTrue("Exception thrown while creating and detaching " + container_class.getName() + " " + e.getMessage(), false);
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// Update the container
Object item = SCOHolderUtilities.createItemParent(item_class_parent, "Item 10", 100, 10);
SCOHolderUtilities.addItemToMap(detachedContainer, "Key10", item);
// Attach the updated container
pm = pmf.getPersistenceManager();
pm.getFetchPlan().addGroup("items");
tx = pm.currentTransaction();
try {
tx.begin();
MapHolder attachedContainer = (MapHolder) pm.makePersistent(detachedContainer);
// Add another item to the container
Object newItem = SCOHolderUtilities.createItemParent(item_class_parent, "Item 20", 200, 20);
SCOHolderUtilities.addItemToMap(attachedContainer, "Key20", newItem);
tx.commit();
} catch (JDOUserException e2) {
LOG.error(e2);
Assert.assertTrue("Exception thrown while retrieving " + container_class.getName() + " " + e2.getMessage(), false);
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// Find the container and check the items
pm = pmf.getPersistenceManager();
pm.getFetchPlan().addGroup("items");
tx = pm.currentTransaction();
try {
tx.begin();
MapHolder container = (MapHolder) 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 + 2), container_size == (NO_OF_ITEMS + 2));
}
detachedContainer = (MapHolder) pm.detachCopy(container);
tx.commit();
} catch (JDOUserException e2) {
LOG.error(e2);
Assert.assertTrue("Exception thrown while retrieving " + container_class.getName() + " " + e2.getMessage(), false);
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// Remove an item from the container
int container_size = SCOHolderUtilities.getContainerSize(detachedContainer);
Assert.assertTrue("Detached container " + container_class.getName() + " has incorrect number of items (" + container_size + ") : should have been " + (NO_OF_ITEMS + 2), container_size == (NO_OF_ITEMS + 2));
SCOHolderUtilities.removeItemFromMap(detachedContainer, "Key1");
container_size = SCOHolderUtilities.getContainerSize(detachedContainer);
Assert.assertTrue("Detached container " + container_class.getName() + " (after delete) has incorrect number of items (" + container_size + ") : should have been " + (NO_OF_ITEMS + 1), container_size == (NO_OF_ITEMS + 1));
// Attach the updated container
pm = pmf.getPersistenceManager();
pm.getFetchPlan().addGroup("items");
tx = pm.currentTransaction();
try {
tx.begin();
pm.makePersistent(detachedContainer);
tx.commit();
} catch (JDOUserException e2) {
LOG.error(e2);
Assert.assertTrue("Exception thrown while retrieving " + container_class.getName() + " " + e2.getMessage(), false);
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// Find the container and check the items
pm = pmf.getPersistenceManager();
pm.getFetchPlan().addGroup("items");
tx = pm.currentTransaction();
try {
tx.begin();
MapHolder container = (MapHolder) pm.getObjectById(container_id, false);
if (container != null) {
// Get the no of items in the container
container_size = SCOHolderUtilities.getContainerSize(container);
Assert.assertTrue("Attached container " + 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 e2) {
LOG.error(e2);
Assert.assertTrue("Exception thrown while retrieving " + container_class.getName() + " " + e2.getMessage(), false);
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
}
Aggregations