use of org.jpox.samples.interfaces.Square in project tests by datanucleus.
the class ReachabilityTest method testBaseOnQuery.
/**
* Tests wether BaseItems are reachable through interface and base class
* fields and correctly persisted.
*/
public void testBaseOnQuery() {
try {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
Square square1 = new Square(101, 240.9, 310.9);
Square square2 = new Square(102, 303.7, 211.7);
ShapeHolder holder1 = new ShapeHolder(303);
holder1.setShape1(square2);
Square square3 = new Square(103, 120.0, 120.0);
ShapeHolder holder2 = new ShapeHolder(304);
holder2.setShape1(square3);
Square square4 = new Square(104, 101.0, 102.0);
ShapeHolder holder3 = new ShapeHolder(305);
holder3.setShape1(square4);
// now persist square1, square2 (through holder1), square3 (through holder3) and
// square4 (through holder4)
pm.makePersistent(square1);
pm.makePersistent(holder1);
pm.makePersistent(holder2);
pm.makePersistent(holder3);
tx.commit();
// assert square1, square2, square3 and square4 are persistent
assertTrue("committed square1: isPersistent() == false", JDOHelper.isPersistent(square1));
assertTrue("committed square2: isPersistent() == false", JDOHelper.isPersistent(square2));
assertTrue("committed square3: isPersistent() == false", JDOHelper.isPersistent(square3));
assertTrue("committed square4: isPersistent() == false", JDOHelper.isPersistent(square4));
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
// assert the DB contains the correct data
tx.begin();
Query q = pm.newQuery(Square.class, "id == :val");
Collection c = (Collection) q.execute(new Integer(101));
assertTrue("Square1 wasnt persisted but should have been", c.size() == 1);
c = (Collection) q.execute(new Integer(102));
assertTrue("Square2 wasnt persisted but should have been", c.size() == 1);
c = (Collection) q.execute(new Integer(103));
assertTrue("Square3 wasnt persisted but should have been", c.size() == 1);
c = (Collection) q.execute(new Integer(104));
assertTrue("Square4 wasnt persisted but should have been", c.size() == 1);
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
clean(ShapeHolder.class);
clean(Square.class);
}
}
use of org.jpox.samples.interfaces.Square 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 org.jpox.samples.interfaces.Square in project tests by datanucleus.
the class ReachabilityTest method testNewObjectUnreachableWithDirtyObjectOwner.
/**
* Tests if a Square is reachable through a ShapeHolder at makePersistent
* and unreachable upon commit and does have the correct states at different times.
* Spec section 12.6.7.
*/
public void testNewObjectUnreachableWithDirtyObjectOwner() {
try {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
Square square1 = new Square(101, 200.0, 350.5);
// assert alpha is indeed transient
assertFalse("newly created alpha: isPersistent() == true", JDOHelper.isPersistent(square1));
assertFalse("newly created alpha: isNew() == true", JDOHelper.isNew(square1));
assertFalse("newly created alpha: isDirty() == true", JDOHelper.isDirty(square1));
Square square2 = new Square(102, 400.0, 550.0);
ShapeHolder holder = new ShapeHolder(201);
// now persist holder with reference to object
holder.setShape1(square1);
pm.makePersistent(holder);
// assert object is persistent new
assertTrue("reachable persistent : isPersistent() == false", JDOHelper.isPersistent(square1));
assertTrue("reachable persistent : isNew() == false", JDOHelper.isNew(square1));
assertTrue("reachable persistent : isDirty() == false", JDOHelper.isDirty(square1));
// now replace reference to square1 with reference to square2 and commit
holder.setShape1(square2);
tx.commit();
assertFalse("unreachable committed : isPersistent() == true", JDOHelper.isPersistent(square1));
assertFalse("unreachable committed : isNew() == true", JDOHelper.isNew(square1));
assertFalse("unreachable committed : isDirty() == true", JDOHelper.isDirty(square1));
tx.begin();
Square square3 = new Square(103, 120.0, 240.0);
holder.setShape1(square3);
pm.flush();
holder.setShape1(null);
tx.commit();
// assert object reverted to transient - JDO spec 12.6.7
// reachability algorithm should be run again at commit() to check if instances
// are no longer reachable and so should revert to transient
assertFalse("unreachable committed delta: isPersistent() == true", JDOHelper.isPersistent(square3));
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
// assert there is no "alpha" record in DB
tx.begin();
Query q = pm.newQuery(Square.class, "id == :val");
Collection c = (Collection) q.execute(new Integer(101));
assertTrue("unexpectedly encountered Square that shouldnt have been persisted", c.size() == 0);
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
clean(ShapeHolder.class);
clean(Square.class);
}
}
use of org.jpox.samples.interfaces.Square in project tests by datanucleus.
the class ReachabilityTest method performOneToOneUniInterface.
/**
* Test for reachability using a 1-1 unidirectional relation between a class and an interface.
* Tests that when persisting the owner object the related object is also persisted.
* @param optimisticTxn Whether to use optimistic txns
*/
protected void performOneToOneUniInterface(boolean optimisticTxn) {
try {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
Object holderId = null;
Object squareId = null;
try {
tx.setOptimistic(optimisticTxn);
tx.begin();
// Create the objects of the 1-1 uni relation
ShapeHolder holder = new ShapeHolder(101);
Square square = new Square(1, 100, 200);
holder.setShape1(square);
// Check that both are transient
assertTrue("Object state of new ShapeHolder is incorrect", !JDOHelper.isPersistent(holder) && !JDOHelper.isNew(holder) && !JDOHelper.isDirty(holder));
assertTrue("Object state of new Square is incorrect", !JDOHelper.isPersistent(square) && !JDOHelper.isNew(square) && !JDOHelper.isDirty(square));
// Persist the ShapeHolder (so the Square should be persisted too)
pm.makePersistent(holder);
// Check that both are persistent-new (JDO2 spec 12.6.7)
assertTrue("Object state of newly persisted ShapeHolder is incorrect", JDOHelper.isPersistent(holder) && JDOHelper.isNew(holder) && JDOHelper.isDirty(holder));
assertTrue("Object state of newly persisted (by reachability) Square is incorrect", JDOHelper.isPersistent(square) && JDOHelper.isNew(square) && JDOHelper.isDirty(square));
// Commit
tx.commit();
// Check that both are clean/hollow
assertTrue("Object state of committed Qualification is incorrect", JDOHelper.isPersistent(holder) && !JDOHelper.isNew(holder) && !JDOHelper.isDirty(holder));
assertTrue("Object state of committed (by reachability) Organisation is incorrect", JDOHelper.isPersistent(square) && !JDOHelper.isNew(square) && !JDOHelper.isDirty(square));
holderId = pm.getObjectId(holder);
squareId = pm.getObjectId(square);
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// Check that the objects exist in the datastore
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
Square square = (Square) pm.getObjectById(squareId);
assertTrue("Square is not in the datastore!", square != null);
ShapeHolder holder = (ShapeHolder) pm.getObjectById(holderId);
assertTrue("ShapeeHolder is not in the datastore!", holder != null);
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
// Clean out our data
clean(ShapeHolder.class);
clean(Square.class);
}
}
use of org.jpox.samples.interfaces.Square in project tests by datanucleus.
the class ReachabilityTest method testIncorrectAssignment.
/**
* Tests whether a ClassCastException is thrown upon incorrect (that is undeclared) assignment
* to a interface field. See JDO spec 1.0 �6.4.3 Object Class type: "If an implementation
* restricts instances to be assigned to the field, a ClassCastException must be thrown at the
* time of any incorrect assignment."
*/
public void testIncorrectAssignment() {
try {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
Square square1 = new Square(101, 250.5, 650.0);
ShapeHolder2 holder = new ShapeHolder2(301);
holder.setShape1(square1);
holder.setShape2(null);
try {
// Try to set "shape2" field to this Square - should be prohibited.
LOG.info(">> Persisting ShapeHolder.shape2 as Square");
pm.makePersistent(holder);
LOG.info(">> makePersistent called");
tx.commit();
LOG.info(">> committed");
assertTrue("incorrect assignment of Square instance was accepted", false);
} catch (ClassCastException e) {
// this exception is expected
}
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
clean(ShapeHolder2.class);
clean(Square.class);
}
}
Aggregations