use of javax.jdo.Extent in project tests by datanucleus.
the class InheritanceTest method performSubclassTableNewTableTest.
// --------------------------------- Perform the tests -----------------------------------------
/**
* Method to perform the test for "subclass-table" strategies.
* All classes passed into this should fit the idea of a baseclass
* with "subclass-table" strategy and subclass with "new-table" strategy.
* @param baseClass The base class that uses "subclass-table"
* @param subClass The sub class that uses "new-table"
*/
protected void performSubclassTableNewTableTest(Class baseClass, Class subClass) {
// Create object
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
Object oid = null;
try {
tx.begin();
Object obj = subClass.newInstance();
Method setNameMethod = subClass.getMethod("setName", new Class[] { String.class });
setNameMethod.invoke(obj, new Object[] { "My Example" });
Method setValueMethod = subClass.getMethod("setValue", new Class[] { double.class });
setValueMethod.invoke(obj, new Object[] { new Double(1234.56) });
pm.makePersistent(obj);
pm.flush();
pm.getFetchPlan().addGroup(FetchPlan.ALL);
Query q1 = pm.newQuery(subClass, "name == \"My Example\"");
Collection coll1 = (Collection) q1.execute();
assertTrue("Unable to find an object of type " + subClass.getName() + " when one should have been found", coll1 != null);
assertTrue("Should have found a single object of type " + subClass.getName() + ", but found " + coll1.size(), coll1.size() == 1);
tx.commit();
oid = pm.getObjectId(obj);
} catch (Exception e) {
e.printStackTrace();
LOG.error(e);
fail("Exception thrown during create of object of class " + subClass.getName() + " : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// Retrieve the object using getObjectById
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
Object obj = pm.getObjectById(oid, false);
if (obj == null) {
fail("pm.getObjectById returned null when attempting to retrieve an object of type InheritSubNoTable");
}
LOG.info(">> Object retrieved is of type " + obj.getClass().getName());
Method getNameMethod = subClass.getMethod("getName", new Class[] {});
String name = (String) getNameMethod.invoke(obj, new Object[] {});
Method getValueMethod = subClass.getMethod("getValue", new Class[] {});
Double value = (Double) getValueMethod.invoke(obj, new Object[] {});
assertTrue(subClass.getName() + " object \"name\" attribute is incorrect : is \"" + name + "\" but should have been \"My Example\"", name.equals("My Example"));
assertTrue(subClass.getName() + " object \"value\" attribute is incorrect : is " + value.doubleValue() + " but should have been 1234.56", value.doubleValue() == 1234.56);
tx.commit();
} catch (Exception e) {
e.printStackTrace();
LOG.error(e);
fail("Exception thrown during retrieval of object of type " + subClass.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);
Collection coll = (Collection) q.execute();
assertTrue("Unable to find a " + baseClass.getName() + " object when one should have been found", coll != null);
assertTrue("Should have found a single " + baseClass.getName() + " object, but found " + coll.size(), coll.size() == 1);
Iterator iter = coll.iterator();
while (iter.hasNext()) {
Object obj = iter.next();
Method getNameMethod = subClass.getMethod("getName", new Class[] {});
String name = (String) getNameMethod.invoke(obj, new Object[] {});
Method getValueMethod = subClass.getMethod("getValue", new Class[] {});
Double value = (Double) getValueMethod.invoke(obj, new Object[] {});
assertTrue(subClass.getName() + " object \"name\" attribute is incorrect : is \"" + name + "\" but should have been \"My Example\"", name.equals("My Example"));
assertTrue(subClass.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 " + baseClass.getName() + " : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// Retrieve the object using Query, starting from actual object type
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
pm.getFetchPlan().addGroup(FetchPlan.ALL);
Query q = pm.newQuery(subClass, "value == 1234.56");
Collection coll = (Collection) q.execute();
assertTrue("Unable to find an " + subClass.getName() + " object when one should have been found", coll != null);
assertTrue("Should have found a single " + subClass.getName() + " object, but found " + coll.size(), coll.size() == 1);
Iterator iter = coll.iterator();
while (iter.hasNext()) {
Object obj = iter.next();
Method getNameMethod = subClass.getMethod("getName", new Class[] {});
String name = (String) getNameMethod.invoke(obj, new Object[] {});
Method getValueMethod = subClass.getMethod("getValue", new Class[] {});
Double value = (Double) getValueMethod.invoke(obj, new Object[] {});
assertTrue(subClass.getName() + " object \"name\" attribute is incorrect : is \"" + name + "\" but should have been \"My Example\"", name.equals("My Example"));
assertTrue(subClass.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 " + subClass.getName() + " : " + e.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()) {
LOG.info("Extent => " + iter.next());
}
tx.commit();
} catch (JDOUserException ue) {
ue.printStackTrace();
fail("Exception thrown during creation of Extent for type " + subClass.getName() + " including subclasses : " + ue.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// Obtain an Extent of the base 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(baseClass, 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) {
ue.printStackTrace();
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
}
use of javax.jdo.Extent in project tests by datanucleus.
the class InterfacesTest method testReadAllShapeHolders.
/**
* Check that ShapeHolder objects can be read back again using an Extent.
*/
public void testReadAllShapeHolders() throws Exception {
try {
addClassesToSchema(new Class[] { ShapeHolder.class, Rectangle.class, Circle.class, Square.class, Triangle.class });
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
Rectangle rectangle = new Rectangle(r.nextInt(), 1.0, 2.0);
ShapeHolder rectangleHolder = new ShapeHolder(r.nextInt());
rectangleHolder.setShape1(rectangle);
pm.makePersistent(rectangleHolder);
ShapeHolder circleHolder = new ShapeHolder(r.nextInt());
Circle circle = (Circle) circleHolder.getShape1();
pm.makePersistent(circleHolder);
ShapeHolder squareHolder = new ShapeHolder(r.nextInt());
Square square = new Square(r.nextInt(), 2.0, 3.5);
squareHolder.setShape1(square);
pm.makePersistent(squareHolder);
ShapeHolder triangleHolder = new ShapeHolder(r.nextInt());
Triangle triangle = new Triangle(r.nextInt(), "tri " + r.nextInt(), 2.1, 3.2);
triangleHolder.setShape1(triangle);
pm.makePersistent(triangleHolder);
tx.commit();
tx.begin();
Extent extent = pm.getExtent(ShapeHolder.class, false);
for (Iterator i = extent.iterator(); i.hasNext(); ) {
ShapeHolder holder = (ShapeHolder) (i.next());
Shape shape = holder.getShape1();
if (holder.getShape1() instanceof Circle) {
// found the circle
assertNotNull("Two ShapeHolders with circles found in database, only one inserted.", circle);
assertEquals("Circle read back in ShapeHolder does not match the one inserted.", circle, shape);
circle = null;
} else if (holder.getShape1() instanceof Rectangle) {
// found the rectangle
assertNotNull("Two ShapeHolders with rectangles found in database, only one inserted.", rectangle);
assertEquals("Rectangle read back in ShapeHolder does not match the one inserted.", rectangle, shape);
rectangle = null;
} else if (holder.getShape1() instanceof Square) {
// found the square
assertNotNull("Two ShapeHolders with squares found in database, only one inserted.", square);
assertEquals("Square read back in ShapeHolder does not match the one inserted.", square, shape);
square = null;
} else if (holder.getShape1() instanceof Triangle) {
// found the triangle
assertNotNull("Two ShapeHolders with triangles found in database, only one inserted.", triangle);
assertEquals("Triangle read back in ShapeHolder does not match the one inserted.", triangle, shape);
triangle = null;
} else {
fail("Unknown shape [" + shape + "] found in ShapeHolder");
}
}
tx.commit();
} catch (JDOUserException ue) {
assertTrue("Exception thrown during test.", false);
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
clean(ShapeHolder.class);
clean(Circle.class);
clean(Triangle.class);
clean(Rectangle.class);
clean(Square.class);
}
}
use of javax.jdo.Extent in project tests by datanucleus.
the class InterfacesTest method testChangeImplementation.
/**
* Persist a ShapeHolder with one implementation of Shape, then change to
* a different implementation and check that the saved version also has
* its implementation updated.
*/
public void testChangeImplementation() {
try {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
// Create the ShapeHolder record
tx.begin();
ShapeHolder holder = new ShapeHolder(r.nextInt());
Shape shape = new Rectangle(r.nextInt(), 5.0, 3.4);
holder.setShape1(shape);
pm.makePersistent(holder);
tx.commit();
// Load it back and change the Shape to a Circle
tx.begin();
Extent extent = pm.getExtent(ShapeHolder.class, false);
holder = (ShapeHolder) (extent.iterator().next());
assertEquals("The inserted rectangle", shape, holder.getShape1());
shape = new Circle(r.nextInt(), 1.75);
holder.setShape1(shape);
extent.closeAll();
tx.commit();
// Load back the holder and check that its Shape is a Circle, then change
// it back to a Rectangle
tx.begin();
extent = pm.getExtent(ShapeHolder.class, false);
holder = (ShapeHolder) (extent.iterator().next());
assertEquals("The inserted circle", shape, holder.getShape1());
shape = new Rectangle(r.nextInt(), 3.897, 1.18);
holder.setShape1(shape);
extent.closeAll();
tx.commit();
// check that the holder's shape is a Rectangle again
tx.begin();
extent = pm.getExtent(ShapeHolder.class, false);
holder = (ShapeHolder) (extent.iterator().next());
assertEquals("The re-inserted rectangle", shape, holder.getShape1());
extent.closeAll();
tx.commit();
// Do it again with application identity classes
tx.begin();
extent = pm.getExtent(ShapeHolder.class, false);
holder = (ShapeHolder) (extent.iterator().next());
shape = new Triangle(r.nextInt(), "tri" + r.nextInt(), 4.97, 7.29);
holder.setShape1(shape);
extent.closeAll();
tx.commit();
// check that the holder's shape is a Triangle again
tx.begin();
extent = pm.getExtent(ShapeHolder.class, false);
holder = (ShapeHolder) (extent.iterator().next());
assertEquals("The re-inserted triangle", shape, holder.getShape1());
extent.closeAll();
tx.commit();
} catch (JDOUserException ue) {
assertTrue("Exception thrown during test.", false);
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
clean(ShapeHolder.class);
clean(Circle.class);
clean(Triangle.class);
clean(Rectangle.class);
clean(Square.class);
}
}
use of javax.jdo.Extent in project tests by datanucleus.
the class PersistenceManagerTest method testQueryPM.
/**
* Tests that the persistence manager used to persist an object is the same
* as returned by jdoGetPersistenceManager()
*/
public void testQueryPM() {
try {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
Manager mgr = null;
try {
tx.begin();
Department d = new Department("Engineering");
mgr = new Manager(0, FIRSTNAME[0], LASTNAME[0], EMAIL[0], EMP_SALARY[0], EMP_SERIAL[0]);
mgr.addDepartment(d);
pm.makePersistent(mgr);
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
pm.close();
fail("Failed to persist object and commit transaction");
}
pm.close();
}
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
Extent ext = pm.getExtent(Manager.class, false);
java.util.Iterator it = ext.iterator();
mgr = (Manager) it.next();
assertSame(pm, JDOHelper.getPersistenceManager(mgr));
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
clean(Manager.class);
clean(Department.class);
}
}
use of javax.jdo.Extent in project tests by datanucleus.
the class PersistenceManagerTest method testElementDeletionRemovesFromFKCollection.
/**
* Test that deleting an object that is a member of a FK Collection also removes it from the Collection.
*/
public void testElementDeletionRemovesFromFKCollection() {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
Manager mgr = new Manager(0, FIRSTNAME[0], LASTNAME[0], EMAIL[0], EMP_SALARY[0], EMP_SERIAL[0]);
Department d = new Department("Engineering");
d.setManager(mgr);
mgr.addDepartment(d);
tx.begin();
pm.makePersistent(d);
tx.commit();
tx.begin();
pm.deletePersistent(d);
tx.commit();
} catch (Exception e) {
LOG.error("Exception thrown when deleting member of a FK collection", e);
fail("Exception thrown when deleting a member of a FK collection " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// get a fresh PM to ensure that any results aren't coming from the cache
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
Extent ext = pm.getExtent(Manager.class, false);
java.util.Iterator it = ext.iterator();
assertTrue(it.hasNext());
Manager mgr = (Manager) it.next();
Collection c = mgr.getDepartments();
assertTrue("Departments should have been null or empty", c == null || c.size() == 0);
ext = pm.getExtent(Department.class, false);
it = ext.iterator();
assertTrue(!(it.hasNext()));
tx.commit();
} catch (Exception e) {
LOG.error("Exception in test", e);
fail("Exception in test : " + e.getMessage());
} finally {
if (tx.isActive())
tx.rollback();
pm.close();
}
}
Aggregations