use of org.jpox.samples.interfaces.ShapeHolder in project tests by datanucleus.
the class InterfacesTest method testListJoin.
/**
* Test for the creation of a set for interface objects using Join table.
*/
public void testListJoin() 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.getShapeList1().add(circle);
Rectangle rectangle = new Rectangle(r.nextInt(), 1.0, 2.0);
container.getShapeList1().add(rectangle);
pm.makePersistent(container);
pm.flush();
Query q = pm.newQuery(ShapeHolder.class);
q.setFilter("shapeList1.contains(c)");
q.declareParameters("Circle c");
Collection results = (Collection) q.execute(circle);
assertEquals(1, results.size());
q = pm.newQuery(ShapeHolder.class);
q.setFilter("shapeList1.contains(c) && c.id == -1");
q.declareVariables("Circle c");
results = (Collection) q.execute();
assertEquals(0, results.size());
q = pm.newQuery(ShapeHolder.class);
q.setFilter("shapeList1.contains(c) && c.id == " + circle.getId());
q.declareVariables("Circle c");
results = (Collection) q.execute();
assertEquals(1, results.size());
tx.commit();
} catch (Exception e) {
LOG.error("Exception thrown during creation of list of interface objects using join table", e);
assertTrue("Exception thrown during create of list 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 testQueryForOneToOneShape.
/**
* Query ShapeHolder objects that have a particular shape in the 1-1 field.
*/
public void testQueryForOneToOneShape() {
try {
// Create sample data for querying
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
Object holder1Id = null;
Object rect1Id = null;
try {
tx.begin();
ShapeHolder holder1 = new ShapeHolder(101);
Rectangle rect1 = new Rectangle(200, 140, 250);
Circle circ = new Circle(300, 45);
holder1.setShape1(rect1);
holder1.setShape2(circ);
pm.makePersistent(holder1);
ShapeHolder holder2 = new ShapeHolder(102);
Rectangle rect2 = new Rectangle(201, 250, 140);
Square sq = new Square(400, 500, 500);
holder2.setShape1(rect2);
holder2.setShape2(sq);
pm.makePersistent(holder2);
tx.commit();
holder1Id = pm.getObjectId(holder1);
rect1Id = pm.getObjectId(rect1);
} catch (Exception e) {
LOG.error("Exception during persist of sample data", e);
fail("Exception creating sample data : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
Rectangle rect1 = (Rectangle) pm.getObjectById(rect1Id);
Query query = pm.newQuery("SELECT FROM " + ShapeHolder.class.getName() + " WHERE shape1 == :myShape");
Map params = new HashMap();
params.put("myShape", rect1);
Collection results = (Collection) query.executeWithMap(params);
assertEquals("Number of ShapeHolders with 1-1 field as our Rectangle was wrong", 1, results.size());
ShapeHolder holder = (ShapeHolder) results.iterator().next();
assertEquals("Id of ShapeHolder was wrong", holder1Id, pm.getObjectId(holder));
tx.commit();
} catch (Exception e) {
LOG.error("Exception thrown during querying for 1-1 shape", e);
assertTrue("Exception thrown during querying for 1-1 shape : " + e.getMessage(), false);
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
// Clean out data
clean(ShapeHolder.class);
clean(Rectangle.class);
clean(Circle.class);
clean(Square.class);
}
}
Aggregations