use of javax.jdo.JDOUserException 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 javax.jdo.JDOUserException 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 javax.jdo.JDOUserException 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 javax.jdo.JDOUserException 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();
}
}
use of javax.jdo.JDOUserException in project tests by datanucleus.
the class SCOMapTests method checkQuery.
/**
* Utility for checking the use of queries with JDOQL on a Map.
* Uses containsKey(),containsValue(),containsEntry() methods.
* @param pmf The PersistenceManager factory
* @param container_class The container class e.g HashMapNormal
* @param item_class_parent The parent element class
* @param db_vendor_id Id of datastore e.g mysql (TODO : remove this)
*/
public static void checkQuery(PersistenceManagerFactory pmf, Class container_class, Class item_class_parent, String db_vendor_id) throws Exception {
int NO_OF_ITEMS = 5;
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();
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);
}
// Add the items to the container
SCOHolderUtilities.addItemsToMap(container, m);
pm.makePersistent(container);
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();
}
// Query the Map
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
// 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(\"Key1\")");
java.util.Collection c1 = (java.util.Collection) q1.execute();
Assert.assertTrue("containsKey : No of containers with a key \"Key1\" is incorrect (" + c1.size() + ") : should have been 1", c1.size() == 1);
// Get all Maps that contain the key "Key7" - should return 0
Extent e2 = pm.getExtent(container_class, true);
Query q2 = pm.newQuery(e2, "items.containsKey(\"Key7\")");
java.util.Collection c2 = (java.util.Collection) q2.execute();
Assert.assertTrue("containsKey : No of containers with a key \"Key7\" is incorrect (" + c2.size() + ") : should have been 0", c2.size() == 0);
// TODO : remove the MySQL omittal when it supports subqueries
if (db_vendor_id == null || !db_vendor_id.equals("mysql")) {
Extent e3 = pm.getExtent(container_class, true);
Query q3 = pm.newQuery(e3, "items.isEmpty()");
java.util.Collection c3 = (java.util.Collection) q3.execute();
Assert.assertTrue("No of containers that are empty is incorrect (" + c3.size() + ") : should have been 0", c3.size() == 0);
}
// Get all Maps containing a particular value
Extent e4 = pm.getExtent(container_class, true);
Query q4 = pm.newQuery(e4, "items.containsValue(the_value) && the_value.name==\"Item 1\"");
q4.declareImports("import " + item_class_parent.getName());
q4.declareVariables(item_class_parent.getName() + " the_value");
java.util.Collection c4 = (java.util.Collection) q4.execute();
Assert.assertTrue("containsValue : No of containers with the specified value is incorrect (" + c4.size() + ") : should have been 1", c4.size() == 1);
// Get all Maps containing a particular value
Extent e5 = pm.getExtent(container_class, true);
Query q5 = pm.newQuery(e5, "items.containsEntry(\"Key1\",the_value) && the_value.name==\"Item 0\"");
q5.declareImports("import " + item_class_parent.getName());
q5.declareVariables(item_class_parent.getName() + " the_value");
java.util.Collection c5 = (java.util.Collection) q5.execute();
Assert.assertTrue("containsEntry : No of containers with the specified value is incorrect (" + c5.size() + ") : should have been 1", c5.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