use of javax.jdo.Extent in project tests by datanucleus.
the class PersistenceTest method testExtentSubclasses.
public void testExtentSubclasses() {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
Extent ex = pm.getExtent(Person.class, true);
Iterator it = ex.iterator();
boolean personFound = false;
boolean employeeFound = false;
int number = 0;
while (it.hasNext()) {
Object obj = it.next();
Object id = JDOHelper.getObjectId(obj);
if (id.equals(id1)) {
personFound = true;
} else if (id.equals(id2)) {
employeeFound = true;
}
number++;
}
assertEquals("Number of objects in Extent was wrong", 2, number);
assertTrue("Should have found Person but didnt", personFound);
assertTrue("Should have found Employee but didnt", employeeFound);
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
}
use of javax.jdo.Extent in project tests by datanucleus.
the class PersistenceModelsTest method test1toNInheritance.
/**
* Test case for 1-N inheritance relationships.
*/
public void test1toNInheritance() throws Exception {
try {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
Object id = null;
try {
tx.begin();
LeftBase base1 = new LeftBase(1);
LeftBase base2 = new LeftBase(2);
LeftBase base3 = new LeftBase(3);
LeftSub group1 = new LeftSub(4, new LeftBase[] { base1, base2 });
LeftSub group2 = new LeftSub(5, new LeftBase[] { group1, base3 });
RightSub gr1 = new RightSub(1, group2);
pm.makePersistent(gr1);
tx.commit();
id = pm.getObjectId(gr1);
} catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// test
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
RightSub gr2 = (RightSub) pm.getObjectById(id, true);
assertEquals("Expect id == 5", 5, gr2.getBase().getId());
assertEquals("Expect Group class instance.", LeftSub.class, gr2.getBase().getClass());
assertEquals("Expect 2 members", 2, ((LeftSub) gr2.getBase()).getMembers().size());
List members = ((LeftSub) gr2.getBase()).getMembers();
assertEquals("Expect id == 4", 4, ((LeftBase) members.get(0)).getId());
assertEquals("Expect id == 3", 3, ((LeftBase) members.get(1)).getId());
assertEquals("Expect Group class instance in member.", LeftSub.class, members.get(0).getClass());
assertEquals("Expect Base class instance in member.", LeftBase.class, members.get(1).getClass());
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
// Clean out all data
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
Extent ex = pm.getExtent(LeftSub.class);
Iterator iter = ex.iterator();
while (iter.hasNext()) {
LeftSub lsub = (LeftSub) iter.next();
lsub.getMembers().clear();
}
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
}
clean(RightSub.class);
clean(RightBase.class);
clean(LeftSub.class);
clean(LeftBase.class);
}
}
use of javax.jdo.Extent in project tests by datanucleus.
the class AbstractClassesTest method perform1to1Deletion.
/**
* Method to perform a test for deletion of abstract objects
* @param holderClass The holder
* @param abstractBaseClass The abstract base class
* @param concreteClass1 Concrete class 1
* @param concreteClass2 Concrete class 2
* @throws Exception Thrown if an error occurs
*/
private void perform1to1Deletion(Class holderClass, Class abstractBaseClass, Class concreteClass1, Class concreteClass2) throws Exception {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
// Retrieve the ids of the abstract objects
Object abstract_id_1 = null;
Object abstract_id_2 = null;
tx.begin();
Extent e1 = pm.getExtent(abstractBaseClass, true);
Iterator iter1 = e1.iterator();
while (iter1.hasNext()) {
Object base = iter1.next();
if (abstract_id_1 == null) {
abstract_id_1 = pm.getObjectId(base);
} else {
abstract_id_2 = pm.getObjectId(base);
}
}
tx.commit();
// Retrieve the ids of the holder objects
Object holder_id_1 = null;
Object holder_id_2 = null;
tx.begin();
Extent e2 = pm.getExtent(holderClass, false);
Iterator iter2 = e2.iterator();
while (iter2.hasNext()) {
Object holder = iter2.next();
if (holder_id_1 == null) {
holder_id_1 = pm.getObjectId(holder);
} else {
holder_id_2 = pm.getObjectId(holder);
}
}
tx.commit();
// Delete the objects
tx.begin();
Object holder_1 = pm.getObjectById(holder_id_1, false);
pm.deletePersistent(holder_1);
Object holder_2 = pm.getObjectById(holder_id_2, false);
pm.deletePersistent(holder_2);
Object abstract_1 = pm.getObjectById(abstract_id_1, false);
pm.deletePersistent(abstract_1);
Object abstract_2 = pm.getObjectById(abstract_id_2, false);
pm.deletePersistent(abstract_2);
tx.commit();
} catch (JDOUserException ue) {
ue.printStackTrace();
LOG.error(ue);
assertTrue("Exception thrown during retrieval and deletion of holder and abstract objects", false);
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
}
use of javax.jdo.Extent in project tests by datanucleus.
the class AbstractClassesTest method perform1toNJoinTableListRetrieval.
/**
* Test of the retrieval of the abstract contained objects and of the
* holder with its related abstract object.
*/
private void perform1toNJoinTableListRetrieval(Class holderClass, Class element1Class, Class element2Class) throws Exception {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
// Find the holder
Extent e = pm.getExtent(holderClass, true);
int numberOfHolders = 0;
Iterator iter = e.iterator();
while (iter.hasNext()) {
Object holder = iter.next();
numberOfHolders++;
// Extract the Set of elements
Method getSetMethod = holderClass.getMethod("getAbstractList1", new Class[] {});
Object obj = getSetMethod.invoke(holder, new Object[] {});
assertTrue("Elements for holder is NULL, but shouldn't be", obj != null);
assertTrue("Elements type is not a List", obj instanceof List);
List elements = (List) obj;
assertEquals("Number of elements is incorrect", 2, elements.size());
Object elem1 = elements.get(0);
Object elem2 = elements.get(1);
assertEquals("First element is of incorrect type", element1Class.getName(), elem1.getClass().getName());
assertEquals("First element is of incorrect type", element2Class.getName(), elem2.getClass().getName());
}
assertEquals("Number of container objects was incorrect.", 1, numberOfHolders);
tx.commit();
} catch (Exception e) {
LOG.error("Exception thrown retrieving objects with abstract superclass", e);
e.printStackTrace();
fail("Exception thrown during persistence : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
}
use of javax.jdo.Extent in project tests by datanucleus.
the class InheritanceTest method performNewTableSubclassTableNewTableTest.
/**
* Method to perform the test for strategies with "new-table", "subclass-table", "new-table" strategies.
* @param baseClass The base class that uses "new-table"
* @param noTableSubClass The sub class that uses "subclass-table"
* @param newTableSubSubClass The subsubclass that uses "new-table"
*/
protected void performNewTableSubclassTableNewTableTest(Class baseClass, Class noTableSubClass, Class newTableSubSubClass) {
// Create object
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
Object base_oid = null;
Object sub_oid = null;
try {
tx.begin();
Object baseobject = baseClass.newInstance();
Method setNameMethod = baseClass.getMethod("setName", new Class[] { String.class });
setNameMethod.invoke(baseobject, new Object[] { "My Base Example" });
pm.makePersistent(baseobject);
pm.flush();
Object subobject = newTableSubSubClass.newInstance();
Method subSetNameMethod = newTableSubSubClass.getMethod("setName", new Class[] { String.class });
subSetNameMethod.invoke(subobject, new Object[] { "My Sub Example" });
Method subSetValueMethod = newTableSubSubClass.getMethod("setValue", new Class[] { double.class });
subSetValueMethod.invoke(subobject, new Object[] { new Double(1234.56) });
pm.makePersistent(subobject);
pm.flush();
pm.getFetchPlan().addGroup(FetchPlan.ALL);
Query q1 = pm.newQuery(baseClass, "name == \"My Sub Example\"");
Collection coll1 = (Collection) q1.execute();
assertTrue("Unable to find an object of type " + baseClass.getName() + " when one should have been found", coll1 != null);
assertTrue("Should have found a single object of type " + baseClass.getName() + ", but found " + coll1.size(), coll1.size() == 1);
tx.commit();
base_oid = pm.getObjectId(baseobject);
sub_oid = pm.getObjectId(subobject);
} catch (Exception e) {
LOG.error("Exception thrown during query", e);
fail("Exception thrown during create of objects of classes " + baseClass.getName() + "," + newTableSubSubClass.getName() + " : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// Retrieve the objects using getObjectById
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
Object baseobject = pm.getObjectById(base_oid, false);
if (baseobject == null) {
fail("pm.getObjectById returned null when attempting to retrieve an object of type " + baseClass.getName());
}
assertTrue("Base object should have been of type " + baseClass.getName() + " but was of type " + baseobject.getClass().getName(), baseobject.getClass().getName().equals(baseClass.getName()));
Object subobject = pm.getObjectById(sub_oid, false);
if (subobject == null) {
fail("pm.getObjectById returned null when attempting to retrieve an object of type " + newTableSubSubClass.getName());
}
assertTrue("Base object should have been of type " + newTableSubSubClass.getName() + " but was of type " + subobject.getClass().getName(), subobject.getClass().getName().equals(newTableSubSubClass.getName()));
Method baseGetNameMethod = baseClass.getMethod("getName", new Class[] {});
String base_name = (String) baseGetNameMethod.invoke(baseobject, new Object[] {});
Method subGetNameMethod = newTableSubSubClass.getMethod("getName", new Class[] {});
String sub_name = (String) subGetNameMethod.invoke(subobject, new Object[] {});
Method getValueMethod = newTableSubSubClass.getMethod("getValue", new Class[] {});
Double value = (Double) getValueMethod.invoke(subobject, new Object[] {});
assertTrue(baseClass.getName() + " object \"name\" attribute is incorrect : is \"" + base_name + "\" but should have been \"My Base Example\"", base_name.equals("My Base Example"));
assertTrue(newTableSubSubClass.getName() + " object \"name\" attribute is incorrect : is \"" + sub_name + "\" but should have been \"My Sub Example\"", sub_name.equals("My Sub Example"));
assertTrue(newTableSubSubClass.getName() + " object \"value\" attribute is incorrect : is " + value.doubleValue() + " but should have been 1234.56", value.doubleValue() == 1234.56);
tx.commit();
} catch (Exception e) {
LOG.error("Exception thrown during query", e);
fail("Exception thrown during retrieval of objects of type " + baseClass.getName() + "," + newTableSubSubClass.getName() + " : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// Retrieve the object using Query, starting from base type
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
pm.getFetchPlan().addGroup(FetchPlan.ALL);
Query q = pm.newQuery(baseClass);
LOG.info(">> Query being performed on baseClass");
Collection coll = (Collection) q.execute();
LOG.info(">> Query for base class performed");
assertTrue("Unable to find objects when 2 should have been found", coll != null);
assertTrue("Should have found 2 objects, but found " + coll.size(), coll.size() == 2);
Iterator iter = coll.iterator();
while (iter.hasNext()) {
Object obj = iter.next();
if (obj.getClass().getName().equals(baseClass.getName())) {
Method getNameMethod = baseClass.getMethod("getName", new Class[] {});
String name = (String) getNameMethod.invoke(obj, new Object[] {});
assertTrue(baseClass.getName() + " object \"name\" attribute is incorrect : is \"" + name + "\" but should have been \"My Base Example\"", name.equals("My Base Example"));
} else if (obj.getClass().getName().equals(newTableSubSubClass.getName())) {
Method getNameMethod = newTableSubSubClass.getMethod("getName", new Class[] {});
String name = (String) getNameMethod.invoke(obj, new Object[] {});
Method getValueMethod = newTableSubSubClass.getMethod("getValue", new Class[] {});
Double value = (Double) getValueMethod.invoke(obj, new Object[] {});
assertTrue(newTableSubSubClass.getName() + " object \"name\" attribute is incorrect : is \"" + name + "\" but should have been \"My Sub Example\"", name.equals("My Sub Example"));
assertTrue(newTableSubSubClass.getName() + " object \"value\" attribute is incorrect : is " + value.doubleValue() + " but should have been 1234.56", value.doubleValue() == 1234.56);
} else {
fail("Query retrieved object of type " + obj.getClass().getName() + " !!!");
}
}
tx.commit();
} catch (Exception e) {
e.printStackTrace();
LOG.error(e);
fail("Exception thrown during retrieval of object of type " + baseClass.getName() + " : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// Retrieve the object using Query, starting from sub type
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
pm.getFetchPlan().addGroup(FetchPlan.ALL);
Query q = pm.newQuery(newTableSubSubClass, "value == 1234.56");
Collection coll = (Collection) q.execute();
assertTrue("Unable to find an " + newTableSubSubClass.getName() + " object when one should have been found", coll != null);
assertTrue("Should have found a single " + newTableSubSubClass.getName() + " object, but found " + coll.size(), coll.size() == 1);
Iterator iter = coll.iterator();
while (iter.hasNext()) {
Object obj = iter.next();
Method getNameMethod = newTableSubSubClass.getMethod("getName", new Class[] {});
String name = (String) getNameMethod.invoke(obj, new Object[] {});
Method getValueMethod = newTableSubSubClass.getMethod("getValue", new Class[] {});
Double value = (Double) getValueMethod.invoke(obj, new Object[] {});
assertTrue(newTableSubSubClass.getName() + " object \"name\" attribute is incorrect : is \"" + name + "\" but should have been \"My Sub Example\"", name.equals("My Sub Example"));
assertTrue(newTableSubSubClass.getName() + " object \"value\" attribute is incorrect : is " + value.doubleValue() + " but should have been 1234.56", value.doubleValue() == 1234.56);
}
tx.commit();
} catch (Exception e) {
fail("Exception thrown during retrieval of object of type " + newTableSubSubClass.getName() + " : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// Retrieve the object using Query, starting from no table type
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
pm.getFetchPlan().addGroup(FetchPlan.ALL);
Query q = pm.newQuery(noTableSubClass, "value == 1234.56");
Collection coll = (Collection) q.execute();
assertTrue("Unable to find an " + noTableSubClass.getName() + " object when one should have been found", coll != null);
assertTrue("Should have found a single " + noTableSubClass.getName() + " object, but found " + coll.size(), coll.size() == 1);
Iterator iter = coll.iterator();
while (iter.hasNext()) {
Object obj = iter.next();
Method getNameMethod = noTableSubClass.getMethod("getName", new Class[] {});
String name = (String) getNameMethod.invoke(obj, new Object[] {});
Method getValueMethod = noTableSubClass.getMethod("getValue", new Class[] {});
Double value = (Double) getValueMethod.invoke(obj, new Object[] {});
assertTrue(newTableSubSubClass.getName() + " object \"name\" attribute is incorrect : is \"" + name + "\" but should have been \"My Sub Example\"", name.equals("My Sub Example"));
assertTrue(newTableSubSubClass.getName() + " object \"value\" attribute is incorrect : is " + value.doubleValue() + " but should have been 1234.56", value.doubleValue() == 1234.56);
}
tx.commit();
} catch (Exception e) {
LOG.info(">> Exception thrown in execution of query", e);
fail("Exception thrown during retrieval of object of type " + noTableSubClass.getName() + " : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// Obtain an Extent of the base class.
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
Extent e = pm.getExtent(baseClass, true);
Iterator iter = e.iterator();
while (iter.hasNext()) {
LOG.info("Extent => " + iter.next());
}
tx.commit();
} catch (JDOUserException ue) {
LOG.error("Exception thrown", ue);
fail("Exception thrown during creation of Extent for type " + baseClass.getName() + " including subclasses : " + ue.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// Obtain an Extent of the sub class.
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
Extent e = pm.getExtent(newTableSubSubClass, true);
Iterator iter = e.iterator();
while (iter.hasNext()) {
LOG.info("Extent => " + iter.next());
}
tx.commit();
} catch (JDOUserException ue) {
LOG.error("Exception thrown", ue);
fail("Exception thrown during creation of Extent for type " + newTableSubSubClass.getName() + " including subclasses : " + ue.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// Obtain an Extent of the subclass-table class to see if this is possible. Should be possible, but will need to find
// all sub-classes for the extent and give the results of (potentiall) multiple base tables in the extent.
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
Extent e = pm.getExtent(noTableSubClass, true);
Iterator iter = e.iterator();
while (iter.hasNext()) {
LOG.info("Extent => " + iter.next());
}
// This should be possible since we have included instances of subclasses so should get some objects
tx.commit();
} catch (JDOUserException ue) {
LOG.error("Exception thrown", ue);
fail("Exception thrown during creation of Extent for type " + baseClass.getName() + " including subclasses : " + ue.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// Update the object
// Delete the object
}
Aggregations