use of javax.jdo.Extent in project tests by datanucleus.
the class InheritanceTest method performNewTableNewTableTest.
/**
* Method to perform the test for "new-table" strategies.
* @param baseClass The base class that uses "new-table"
* @param subClass The sub class that uses "new-table"
*/
private void performNewTableNewTableTest(Class baseClass, Class subClass) {
// Create objects
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
Object oid_base = null;
Object oid_sub = null;
try {
tx.begin();
// Create a base object
Object baseobject = baseClass.newInstance();
Method baseSetNameMethod = baseClass.getMethod("setName", new Class[] { String.class });
baseSetNameMethod.invoke(baseobject, new Object[] { "Base Object" });
pm.makePersistent(baseobject);
// Create a sub object
Object subobject = subClass.newInstance();
Method subSetNameMethod = subClass.getMethod("setName", new Class[] { String.class });
subSetNameMethod.invoke(subobject, new Object[] { "Sub Object" });
Method subSetValueMethod = subClass.getMethod("setValue", new Class[] { double.class });
subSetValueMethod.invoke(subobject, new Object[] { new Double(1234.56) });
pm.makePersistent(subobject);
tx.commit();
oid_base = pm.getObjectId(baseobject);
oid_sub = pm.getObjectId(subobject);
} catch (Exception e) {
LOG.error("Exception thrown", e);
fail("Exception thrown during create of base and subclass objects for \"new-table\" strategy inheritance tree : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// Retrieve the objects
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
Object baseobject = pm.getObjectById(oid_base, false);
assertTrue("Unable to retrieve object of type " + baseClass.getName(), baseobject != null);
Method baseGetNameMethod = baseClass.getMethod("getName", new Class[] {});
String baseName = (String) baseGetNameMethod.invoke(baseobject, new Object[] {});
assertTrue("The retrieved object of type " + baseClass.getName() + " is incorrect : name=" + baseName, baseName.equals("Base Object"));
assertTrue("The retrieved object of type " + baseClass.getName() + " is an instance of " + subClass.getName() + " which is incorrect", !(subClass.isAssignableFrom(baseobject.getClass())));
Object subobject = pm.getObjectById(oid_sub, false);
assertTrue("Unable to retrieve object of type " + subClass.getName(), subobject != null);
Method subGetNameMethod = subClass.getMethod("getName", new Class[] {});
String subName = (String) subGetNameMethod.invoke(subobject, new Object[] {});
assertTrue("The retrieved object of type " + subClass.getName() + " is incorrect : name=" + subName, subName.equals("Sub Object"));
tx.commit();
} catch (Exception e) {
LOG.error("Exception thrown", e);
fail("Exception thrown during update of base and subclass objects for \"new-table\" strategy inheritance tree : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// Retrieve the base object using Query, starting from actual object type
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
pm.getFetchPlan().addGroup(FetchPlan.ALL);
Query q1 = pm.newQuery(baseClass, "name == \"Base Object\"");
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);
Iterator iter1 = coll1.iterator();
while (iter1.hasNext()) {
Object baseobject = iter1.next();
Method baseGetNameMethod = baseClass.getMethod("getName", new Class[] {});
String baseName = (String) baseGetNameMethod.invoke(baseobject, new Object[] {});
assertTrue(baseClass.getName() + " \"name\" attribute is incorrect : is \"" + baseName + "\" but should have been \"Base Object\"", baseName.equals("Base Object"));
}
pm.getFetchPlan().addGroup(FetchPlan.ALL);
Query q2 = pm.newQuery(baseClass, "name == \"Sub Object\"");
Collection coll2 = (Collection) q2.execute();
assertTrue("Unable to find an object of type " + subClass.getName() + " when one should have been found", coll2 != null);
assertTrue("Should have found a single object of type " + subClass.getName() + ", but found " + coll2.size(), coll2.size() == 1);
Iterator iter2 = coll2.iterator();
while (iter2.hasNext()) {
Object baseobject = iter2.next();
Method baseGetNameMethod = baseClass.getMethod("getName", new Class[] {});
String baseName = (String) baseGetNameMethod.invoke(baseobject, new Object[] {});
assertTrue(subClass.getName() + " \"name\" attribute is incorrect : is \"" + baseName + "\" but should have been \"Sub Object\"", baseName.equals("Sub Object"));
}
tx.commit();
} catch (Exception e) {
LOG.error("Exception thrown", e);
fail("Exception thrown during retrieval of object of type " + baseClass.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()) {
iter.next();
}
tx.commit();
} catch (JDOUserException ue) {
LOG.error("Exception thrown", ue);
fail("Exception thrown during creation of Extent for " + 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(subClass, true);
Iterator iter = e.iterator();
while (iter.hasNext()) {
iter.next();
}
tx.commit();
} catch (JDOUserException ue) {
LOG.error("Exception thrown", ue);
fail("Exception thrown during creation of Extent for " + subClass.getName() + " including subclasses" + ue.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
}
use of javax.jdo.Extent in project tests by datanucleus.
the class InheritanceTest method performNewTableSuperclassTableTest.
/**
* Method to perform the test for "superclass-table" strategies.
* @param baseClass The base class that uses "new-table"
* @param subClass1 The sub class that uses "superclass-table"
* @param subClass2 The second sub class using "superclass-table"
*/
private void performNewTableSuperclassTableTest(Class baseClass, Class subClass1, Class subClass2) {
// Create base object
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
Object oid_base = null;
Object oid_sub1 = null;
Object oid_sub2 = null;
try {
tx.begin();
// Create a base object
Object obj = baseClass.newInstance();
Method setNameMethod = baseClass.getMethod("setName", new Class[] { String.class });
setNameMethod.invoke(obj, new Object[] { "Base Object" });
pm.makePersistent(obj);
tx.commit();
oid_base = pm.getObjectId(obj);
} catch (Exception e) {
LOG.error("Exception thrown", e);
fail("Exception thrown during create of object of type " + baseClass.getName() + " for \"superclass-table\" strategy inheritance tree : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// Create sub objects
// This is done in separate transaction since if we do it in same tx
// we get a deadlock due to an insert on the table before a modification to the table structure
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
// Create a sub object
Object obj1 = subClass1.newInstance();
Method setNameMethod = subClass1.getMethod("setName", new Class[] { String.class });
setNameMethod.invoke(obj1, new Object[] { "Sub Object 1" });
Method setValueMethod = subClass1.getMethod("setValue", new Class[] { double.class });
setValueMethod.invoke(obj1, new Object[] { new Double(1234.56) });
pm.makePersistent(obj1);
tx.commit();
oid_sub1 = pm.getObjectId(obj1);
tx = pm.currentTransaction();
tx.begin();
// Create a sub object
Object obj2 = subClass2.newInstance();
Method setNameMethod2 = subClass2.getMethod("setName", new Class[] { String.class });
setNameMethod2.invoke(obj2, new Object[] { "Sub Object 2" });
Method setValueMethod2 = subClass2.getMethod("setValue2", new Class[] { float.class });
setValueMethod2.invoke(obj2, new Object[] { new Float(2345.67) });
pm.makePersistent(obj2);
tx.commit();
oid_sub2 = pm.getObjectId(obj2);
} catch (Exception e) {
LOG.error("Exception thrown", e);
fail("Exception thrown during create of object of type " + subClass1.getName() + " for \"superclass-table\" strategy inheritance tree : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// Retrieve the objects
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
// Base
Object baseobject = pm.getObjectById(oid_base, false);
assertTrue("Unable to retrieve object of type " + baseClass.getName(), baseobject != null);
Method baseGetNameMethod = baseClass.getMethod("getName", new Class[] {});
String baseName = (String) baseGetNameMethod.invoke(baseobject, new Object[] {});
assertTrue("The retrieved " + baseClass.getName() + " object is incorrect : name=" + baseName, baseName.equals("Base Object"));
assertTrue("The retrieved " + baseClass.getName() + " object is a " + subClass1.getName() + " object which is incorrect", !(subClass1.isAssignableFrom(baseobject.getClass())));
// Sub 1
Object subobject1 = pm.getObjectById(oid_sub1, false);
assertTrue("Unable to retrieve object of type " + subClass1.getName(), subobject1 != null);
Method subGetNameMethod = subClass1.getMethod("getName", new Class[] {});
String subName = (String) subGetNameMethod.invoke(subobject1, new Object[] {});
assertTrue("The retrieved object of type " + subClass1.getName() + " is incorrect : name=" + subName, subName.equals("Sub Object 1"));
// Sub 2
Object subobject2 = pm.getObjectById(oid_sub2, false);
assertTrue("Unable to retrieve object of type " + subClass2.getName(), subobject2 != null);
Method subGetNameMethod2 = subClass2.getMethod("getName", new Class[] {});
String subName2 = (String) subGetNameMethod2.invoke(subobject2, new Object[] {});
assertTrue("The retrieved object of type " + subClass2.getName() + " is incorrect : name=" + subName2, subName2.equals("Sub Object 2"));
tx.commit();
} catch (Exception e) {
LOG.error("Exception thrown", e);
fail("Exception thrown during update of base and subclass objects for \"superclass-table\" strategy inheritance tree : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// Retrieve the base object using Query, starting from actual object type
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
pm.getFetchPlan().addGroup(FetchPlan.ALL);
Query q1 = pm.newQuery(baseClass, "name == \"Base Object\"");
Collection coll1 = (Collection) q1.execute();
assertTrue("Unable to find an " + baseClass.getName() + " object when one should have been found", coll1 != null);
assertTrue("Should have found a single " + baseClass.getName() + " object, but found " + coll1.size(), coll1.size() == 1);
Iterator iter1 = coll1.iterator();
while (iter1.hasNext()) {
Object baseobject = iter1.next();
Method baseGetNameMethod = baseClass.getMethod("getName", new Class[] {});
String baseName = (String) baseGetNameMethod.invoke(baseobject, new Object[] {});
assertTrue(baseClass.getName() + " object \"name\" attribute is incorrect : is \"" + baseName + "\" but should have been \"Base Object\"", baseName.equals("Base Object"));
}
pm.getFetchPlan().addGroup(FetchPlan.ALL);
Query q2 = pm.newQuery(baseClass, "name == \"Sub Object 1\"");
Collection coll2 = (Collection) q2.execute();
assertTrue("Unable to find an " + subClass1.getName() + " object when one should have been found", coll2 != null);
assertTrue("Should have found a single " + subClass1.getName() + " object, but found " + coll2.size(), coll2.size() == 1);
Iterator iter2 = coll2.iterator();
while (iter2.hasNext()) {
Object baseobject = iter2.next();
Method baseGetNameMethod = baseClass.getMethod("getName", new Class[] {});
String baseName = (String) baseGetNameMethod.invoke(baseobject, new Object[] {});
assertTrue(subClass1.getName() + " object \"name\" attribute is incorrect : is \"" + baseName + "\" but should have been \"Sub Object 1\"", baseName.equals("Sub Object 1"));
}
tx.commit();
} catch (Exception e) {
LOG.error("Exception thrown", e);
fail("Exception thrown during retrieval of object of type " + baseClass.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();
pm.getExtent(baseClass, true);
Extent e = pm.getExtent(baseClass, true);
Iterator iter = e.iterator();
int size = 0;
while (iter.hasNext()) {
iter.next();
size++;
}
assertTrue("Number of elements returned by Extent of base class in newtable-superclasstable hierarchy was wrong :" + " is " + size + " but should have been 3", size == 3);
tx.commit();
} catch (JDOUserException ue) {
LOG.error("Exception thrown", ue);
fail("Exception thrown during creation of Extent for " + baseClass.getName() + " including subclasses" + ue.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// Obtain an Extent of the sub class 1
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
pm.getExtent(subClass1, true);
Extent e = pm.getExtent(subClass1, true);
Iterator iter = e.iterator();
int size = 0;
while (iter.hasNext()) {
iter.next();
size++;
}
assertTrue("Number of elements returned by Extent of sub class in newtable-superclasstable hierarchy was wrong :" + " is " + size + " but should have been 1", size == 1);
tx.commit();
} catch (JDOUserException ue) {
LOG.error("Exception thrown", ue);
fail("Exception thrown during creation of Extent for " + subClass1.getName() + " including subclasses" + ue.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// Obtain an Extent of the sub class 2
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
pm.getExtent(subClass2, true);
Extent e = pm.getExtent(subClass2, true);
Iterator iter = e.iterator();
int size = 0;
while (iter.hasNext()) {
iter.next();
size++;
}
assertTrue("Number of elements returned by Extent of sub class in newtable-superclasstable hierarchy was wrong :" + " is " + size + " but should have been 1", size == 1);
tx.commit();
} catch (JDOUserException ue) {
LOG.error("Exception thrown", ue);
fail("Exception thrown during creation of Extent for " + subClass2.getName() + " including subclasses" + ue.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
}
use of javax.jdo.Extent in project tests by datanucleus.
the class InterfacesTest method testInsertThenDelete.
/**
* Insert a ShapeHolder object, delete it, then check that it's gone.
*/
public void testInsertThenDelete() throws Exception {
try {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
// Create objects
tx.begin();
ShapeHolder holder = new ShapeHolder(r.nextInt());
pm.makePersistent(holder);
tx.commit();
// Delete objects
tx.begin();
Extent extent = pm.getExtent(ShapeHolder.class, false);
assertTrue("ShapeHolder should be available in extent.", extent.iterator().hasNext());
pm.deletePersistent(extent.iterator().next());
extent.closeAll();
tx.commit();
tx.begin();
extent = pm.getExtent(ShapeHolder.class, false);
assertFalse("There should be no more ShapeHolder objects.", extent.iterator().hasNext());
extent.closeAll();
tx.commit();
} catch (JDOUserException ue) {
assertTrue("Exception thrown during creation/deletion of interface objects.", false);
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
clean(ShapeHolder.class);
}
}
use of javax.jdo.Extent in project tests by datanucleus.
the class InterfacesTest method testNullValues.
/**
* Check that null shapes work correctly.
*/
public void testNullValues() throws Exception {
try {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
ShapeHolder holder = new ShapeHolder(r.nextInt());
holder.setShape1(null);
pm.makePersistent(holder);
tx.commit();
tx.begin();
Extent extent = pm.getExtent(ShapeHolder.class, false);
holder = (ShapeHolder) (extent.iterator().next());
assertNull("Shape should be read back from database as null", holder.getShape1());
extent.closeAll();
tx.commit();
} catch (JDOUserException ue) {
assertTrue("Exception thrown during create/query of interface objects", false);
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
clean(ShapeHolder.class);
}
}
use of javax.jdo.Extent in project tests by datanucleus.
the class AbstractClassesTest method perform1toNJoinTableSetRetrieval.
/**
* Test of the retrieval of the abstract contained objects and of the
* holder with its related abstract object.
*/
private void perform1toNJoinTableSetRetrieval(Class holderClass) 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("getAbstractSet1", 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 Set", obj instanceof Set);
Collection elements = (Collection) obj;
assertEquals("Number of elements is incorrect", 2, elements.size());
Iterator elementsIter = elements.iterator();
while (elementsIter.hasNext()) {
elementsIter.next();
}
}
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();
}
}
Aggregations