use of org.jpox.samples.interfaces.ShapeHolder 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.ShapeHolder 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.ShapeHolder in project tests by datanucleus.
the class InterfacesTest method testCreation.
/**
* Check that we can create a persistent object with an interface FCO.
*/
public void testCreation() throws Exception {
try {
addClassesToSchema(new Class[] { ShapeHolder.class });
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
// Create some objects.
tx.begin();
ShapeHolder holder = new ShapeHolder(r.nextInt());
pm.makePersistent(holder);
tx.commit();
} catch (JDOUserException ue) {
assertTrue("Exception thrown during create of interface objects.", false);
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
clean(ShapeHolder.class);
}
}
use of org.jpox.samples.interfaces.ShapeHolder in project tests by datanucleus.
the class InterfacesTest method testSetJoinUnidir.
/**
* Test for the creation of a set for interface objects using Join table.
*/
public void testSetJoinUnidir() throws Exception {
try {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
// Create container and some shapes
tx.begin();
ShapeHolder container = new ShapeHolder(r.nextInt());
Circle circle = new Circle(r.nextInt(), 1.75);
container.getShapeSet1().add(circle);
Rectangle rectangle = new Rectangle(r.nextInt(), 1.0, 2.0);
container.getShapeSet1().add(rectangle);
pm.makePersistent(container);
tx.commit();
} catch (Exception e) {
LOG.error("Exception thrown during creation of set of interface objects using join table: ", e);
assertTrue("Exception thrown during create of set of interface objects using join table : " + e.getMessage(), false);
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// TODO Extend this to then query the elements in the collection
} finally {
clean(ShapeHolder.class);
clean(Circle.class);
clean(Rectangle.class);
}
}
use of org.jpox.samples.interfaces.ShapeHolder in project tests by datanucleus.
the class InterfacesTest method testQueryForNonNullShape.
/**
* Query ShapeHolder objects with a non-null Shape.
*/
public void testQueryForNonNullShape() {
try {
// Create sample data for querying
createSampleDataForQueryTests();
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
Query query = pm.newQuery(pm.getExtent(ShapeHolder.class, true));
query.setFilter("shape1 != null");
Collection results = (Collection) query.execute();
assertEquals("Expected number of ShapeHolder objects with null Shape is wrong", 20, results.size());
for (Iterator i = results.iterator(); i.hasNext(); ) {
ShapeHolder result = (ShapeHolder) (i.next());
assertNotNull("ShapeHolder has null Shape!", result.getShape1());
}
tx.commit();
} catch (Exception e) {
LOG.error("Exception thrown during querying for not-null shape", e);
assertTrue("Exception thrown during querying for not-null shape : " + e.getMessage(), false);
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
// Clean out data
clean(ShapeHolder.class);
clean(Square.class);
clean(Triangle.class);
clean(Rectangle.class);
clean(Circle.class);
}
}
Aggregations