use of org.jpox.samples.types.container.MapHolder in project tests by datanucleus.
the class SCOMapTests method checkRemove.
/**
* Utility for checking the removal of an element with a key in the Map.
* @param pmf The PersistenceManager factory
* @param container_class The container class e.g MapNormal
* @param item_class_parent The parent element class
*/
public static void checkRemove(PersistenceManagerFactory pmf, Class container_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();
// 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());
}
// Add a few items
for (int i = 0; i < NO_OF_ITEMS; i++) {
Object item = SCOHolderUtilities.createItemParent(item_class_parent, "Item " + i, 0.00 + (10.00 * i), i);
SCOHolderUtilities.addItemToMap(container, new String("Key" + (i + 1)), item);
}
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 remove an item
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 " + NO_OF_ITEMS, container_size == NO_OF_ITEMS);
// Remove an item
SCOHolderUtilities.removeItemFromMap(container, new String("Key2"));
}
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();
}
// Find the container and check the new number of 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 " + (NO_OF_ITEMS - 1), container_size == (NO_OF_ITEMS - 1));
}
tx.commit();
} catch (JDOUserException e4) {
LOG.error(e4);
Assert.assertTrue("Exception thrown while manipulating " + container_class.getName() + " " + e4.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 checkSameKeyValueInstances.
/**
* Utility for checking the use of key/value same instances
* @param pmf The PersistenceManager factory
* @param container_class The container class e.g HashMapNormal
* @param item_class_parent The parent element class
*/
public static void checkSameKeyValueInstances(PersistenceManagerFactory pmf, Class container_class, Class item_class_parent) 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();
Object detachedItem = null;
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);
m.put(item, item);
entries.put(item, item);
values.add(item);
keys.add(item);
}
// Add the items to the container
SCOHolderUtilities.addItemsToMap(container, m);
// Create an item
Object item = SCOHolderUtilities.createItemParent(item_class_parent, "Item " + NO_OF_ITEMS + 1, 0.00 + (10.00 * (NO_OF_ITEMS + 1)), NO_OF_ITEMS + 1);
m.put(item, item);
entries.put(item, item);
values.add(item);
keys.add(item);
SCOHolderUtilities.addItemToMap(container, item, item);
// Create an item
item = SCOHolderUtilities.createItemParent(item_class_parent, "Item " + NO_OF_ITEMS + 2, 0.00 + (10.00 * (NO_OF_ITEMS + 2)), NO_OF_ITEMS + 2);
m.put(item, item);
entries.put(item, item);
values.add(item);
keys.add(item);
pm.makePersistent(item);
pm.flush();
detachedItem = pm.detachCopy(item);
SCOHolderUtilities.addItemToMap(container, detachedItem, item);
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 + 2), entry_set.size() == (NO_OF_ITEMS + 2));
Assert.assertTrue("Entries are not the same, result = " + entry_set + " expected = " + entries.entrySet(), SCOHolderUtilities.compareSet(entry_set, entries.entrySet()));
Assert.assertTrue("Entries does not contain expected value", container.getValues().contains(detachedItem));
Assert.assertTrue("Entries does not contain expected key", container.containsKey(detachedItem));
Assert.assertTrue("Entries does not contain expected value", container.containsValue(detachedItem));
}
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 checkQueryNonPrimitiveKey.
/**
* Utility for checking the use of queries with JDOQL on a Map with
* primitive key/value objects. This is separate from the checkQuery
* because with primitive (String) value a different table is created.
* Uses containsKey(),containsValue(),containsEntry() methods.
* @param pmf The PersistenceManager factory
* @param container_class The container class e.g HashMapNormal
* @param key_class The key class
* @param value_class The value class
*/
public static void checkQueryNonPrimitiveKey(PersistenceManagerFactory pmf, Class container_class, Class key_class, Class value_class) throws Exception {
int NO_OF_ITEMS = 5;
Object key_id = null;
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
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();
Object saved_key = null;
for (int i = 0; i < NO_OF_ITEMS; i++) {
// Create an item
Object item = SCOHolderUtilities.createItemParent(value_class, "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);
// Save the first key in the map for future use
if (i == 0) {
saved_key = k;
}
}
for (int i = 0; i < NO_OF_ITEMS; i++) {
// Create an item
Object item = SCOHolderUtilities.createItemParent(value_class, "Item " + i, 0.00 + (10.00 * i), i);
m.put(item, item);
}
// Add the items to the container
SCOHolderUtilities.addItemsToMap(container, m);
pm.makePersistent(container);
JDOHelper.getObjectId(container);
key_id = pm.getObjectId(saved_key);
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();
}
// Query the Map
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
Object saved_key = pm.getObjectById(key_id, false);
// Get all Maps that contain the key "Key1" - should return 1
Extent e1 = pm.getExtent(container_class, true);
Query q1 = pm.newQuery(e1, "items.containsKey(theKey)");
q1.declareParameters(key_class.getName() + " theKey");
java.util.Collection c1 = (java.util.Collection) q1.execute(saved_key);
Assert.assertTrue("containsKey : No of containers with a key with id \"" + key_id + "\" is incorrect (" + c1.size() + ") : should have been 1", c1.size() == 1);
tx.commit();
} catch (JDOUserException e) {
LOG.error(e);
e.printStackTrace();
Assert.assertTrue("Exception thrown while querying container objects " + e.getMessage(), false);
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
}
Aggregations