use of org.jpox.samples.interfaces.Shape 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.Shape 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 org.jpox.samples.interfaces.Shape in project tests by datanucleus.
the class ArrayTest method testInterfaceArray.
/**
* Test for a field of type Interface[] stored serialised or in a join table.
*/
public void testInterfaceArray() {
Shape[] shapes1 = new Shape[2];
shapes1[0] = new Rectangle(1, 25.0, 20.0);
shapes1[1] = new Rectangle(2, 35.0, 10.0);
Shape[] shapes2 = new Shape[2];
shapes2[0] = new Rectangle(3, 25.0, 20.0);
shapes2[1] = new Rectangle(4, 35.0, 10.0);
InterfaceArray holder = new InterfaceArray(shapes1, shapes2);
Shape[] expectedShapes1 = new Shape[2];
expectedShapes1[0] = (Shape) ((Rectangle) shapes1[0]).clone();
expectedShapes1[1] = (Shape) ((Rectangle) shapes1[1]).clone();
Shape[] expectedShapes2 = new Shape[2];
expectedShapes2[0] = (Shape) ((Rectangle) shapes2[0]).clone();
expectedShapes2[1] = (Shape) ((Rectangle) shapes2[1]).clone();
performArrayTest(holder, Shape[].class, expectedShapes1, expectedShapes2, 0.0);
holder = new InterfaceArray(null, null);
performArrayTest(holder, Shape[].class, null, null, 0.0);
clean(Rectangle.class);
}
use of org.jpox.samples.interfaces.Shape in project tests by datanucleus.
the class ArrayTest method testJoinTableInterfaceQueryArray.
/**
* Test for a field of type Interface[] stored using a join table.
*/
public void testJoinTableInterfaceQueryArray() {
Shape[] shapes1 = new Shape[2];
shapes1[0] = new Rectangle(1, 25.0, 20.0);
shapes1[1] = new Rectangle(2, 35.0, 10.0);
Shape[] shapes2 = new Shape[2];
shapes2[0] = new Rectangle(3, 25.0, 20.0);
shapes2[1] = new Rectangle(4, 35.0, 10.0);
InterfaceArray holder = new InterfaceArray(shapes1, shapes2);
Shape[] expectedShapes1 = new Shape[2];
expectedShapes1[0] = (Shape) ((Rectangle) shapes1[0]).clone();
expectedShapes1[1] = (Shape) ((Rectangle) shapes1[1]).clone();
performArrayQueryTest(holder, Shape[].class, expectedShapes1);
clean(Rectangle.class);
}
Aggregations