use of javax.jdo.JDOUserException in project tests by datanucleus.
the class SCOMapTests method checkQueryPrimitive.
/**
* 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 db_vendor_id Id of datastore e.g mysql (TODO : remove this)
*/
public static void checkQueryPrimitive(PersistenceManagerFactory pmf, Class container_class, 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++) {
m.put(new String("Key" + (i + 1)), new String("Value " + (i + 1)));
}
// 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==\"Value 1\"");
q4.declareImports("import java.lang.String");
q4.declareVariables("java.lang.String 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 entry
Extent e5 = pm.getExtent(container_class, true);
Query q5 = pm.newQuery(e5, "items.containsEntry(\"Key1\",the_value) && the_value==\"Value 1\"");
q5.declareImports("import java.lang.String");
q5.declareVariables("java.lang.String 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();
}
}
use of javax.jdo.JDOUserException in project tests by datanucleus.
the class SCOMapTests method checkClearMap.
/**
* Utility for checking the clearing out of a Map. Calls the
* clear method on a Map container.
* @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 checkClearMap(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 and a few items
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 to the container
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 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 " + NO_OF_ITEMS, container_size == NO_OF_ITEMS);
// Clear out the container
SCOHolderUtilities.clearItemsFromContainer(container);
}
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 items
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
MapHolder container = (MapHolder) pm.getObjectById(container_id, false);
if (container != null) {
boolean is_empty = SCOHolderUtilities.isContainerEmpty(container);
Assert.assertTrue(container_class.getName() + " is not empty, yet should be since clear() was called.", is_empty);
}
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 checkPutNullValues.
/**
* Utility for checking the addition of a Map of elements. Calls the
* putAll method on a map container.
* @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 checkPutNullValues(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());
}
// 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++) {
m.put(new String("Key" + (i + 1)), null);
}
// 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 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);
Map items = container.getItems();
Iterator iter = items.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry entry = (Entry) iter.next();
Assert.assertNull(entry.getValue());
Assert.assertNotNull(entry.getKey());
}
}
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 InterfacesTest method testMappingStrategyIdentity1To1Query.
/**
* Test for use of mapping-strategy="identity" for 1-1 relation, with a query taking in the string form of the id.
*/
public void testMappingStrategyIdentity1To1Query() throws Exception {
try {
addClassesToSchema(new Class[] { Diet.class });
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
Object steakId = null;
try {
// Create some objects
tx.begin();
Diet diet = new Diet(1);
Food fave = new Steak();
diet.setFavouriteFood(fave);
pm.makePersistent(diet);
tx.commit();
steakId = JDOHelper.getObjectId(fave);
} catch (JDOUserException ue) {
fail("Exception thrown during create of interface objects.");
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// Try a query
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
// Pass in a parameter using a String form of the id
Query q = pm.newQuery("SELECT FROM " + Diet.class.getName() + " WHERE favouriteFood == :param");
q.execute(Steak.class.getName() + ":" + steakId.toString());
// TODO Compare the results
tx.commit();
} catch (JDOUserException ue) {
LOG.error("Exception thrown during query", ue);
fail("Exception thrown during query of objects");
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
clean(Diet.class);
clean(Steak.class);
}
}
use of javax.jdo.JDOUserException in project tests by datanucleus.
the class AttachDetachTest method testAttachDetachNonPCCollectionElements.
/**
* TODO Change to use a generic collection<nonPC> sample
*/
public void testAttachDetachNonPCCollectionElements() {
try {
PersistenceManager pm = newPM();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
pm.getFetchPlan().addGroup("collection");
Owner o = new Owner();
o.getElements().add("Elm 1");
o.getElements().add("Elm 2");
o.getElements().add("Elm 3");
o.getElements().add("Elm 4");
o.getSetElements().add("Elm 1");
o.getSetElements().add("Elm 2");
pm.makePersistent(o);
Owner o1 = (Owner) pm.detachCopy(o);
tx.commit();
pm.close();
pm = newPM();
tx = pm.currentTransaction();
tx.begin();
o1.getElements().add("Elm 5");
o1.getSetElements().add("Elm 3");
Owner o2 = (Owner) pm.makePersistent(o1);
assertEquals(5, o2.getElements().size());
assertEquals(3, o2.getSetElements().size());
tx.commit();
} catch (JDOUserException ue) {
LOG.error("Exception during test", ue);
fail("Exception thrown while performing test : " + ue.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
clean(Owner.class);
}
}
Aggregations