use of org.jpox.samples.interfaces.Circle in project tests by datanucleus.
the class SerializationTest method testSerialisedInterface.
/**
* Test for serialisation of Interface fields
*/
public void testSerialisedInterface() {
try {
Object holderId = null;
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
// Persist the object with serialised fields
try {
tx.begin();
ShapeHolder holder = new ShapeHolder(1);
holder.setShape2(new Circle(1, 25.0));
pm.makePersistent(holder);
// Update holder and serialised object fields to check that they get to the datastore
holder.setId(2);
((Circle) holder.getShape1()).setRadius(40.0);
tx.commit();
holderId = pm.getObjectId(holder);
} catch (Exception e) {
LOG.error(">> Exception thrown in test", e);
fail("Exception thrown while persisted object with serialised Interface field : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// Retrieve the object
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
ShapeHolder holder = (ShapeHolder) pm.getObjectById(holderId);
assertTrue("Holder of serialised PC could not be retrieved!", holder != null);
assertTrue("Holder 'id' field is incorrect", holder.getId() == 2);
assertTrue("Retrieved holder has null serialised object!", holder.getShape1() != null);
assertTrue("Retrieved holder has serialised object of incorrect type!", holder.getShape1() instanceof Circle);
assertEquals("Retrieved serialised object description is incorrect : ", ((Circle) holder.getShape1()).getRadius(), 40.0, 0.01);
// Update holder and serialised object fields to check that they get to the datastore
((Circle) holder.getShape1()).setId(40);
holder.setId(7);
tx.commit();
} catch (Exception e) {
LOG.error(">> Exception thrown in test", e);
fail("Exception thrown while retrieving object with serialised Interface field : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// Retrieve the object again to check the most recent update
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
ShapeHolder holder = (ShapeHolder) pm.getObjectById(holderId);
assertTrue("Holder of serialised PC could not be retrieved!", holder != null);
assertTrue("Holder name is incorrect", holder.getId() == 7);
assertTrue("Retrieved holder has null serialised object!", holder.getShape1() != null);
assertTrue("Retrieved holder has serialised object of incorrect type!", holder.getShape1() instanceof Circle);
assertEquals("Retrieved serialised object description is incorrect : ", ((Circle) holder.getShape1()).getId(), 40);
tx.commit();
} catch (Exception e) {
LOG.error(">> Exception thrown in test", e);
fail("Exception thrown while retrieving object with serialised Interface field : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
// Clean up our data
clean(ShapeHolder.class);
clean(Circle.class);
}
}
use of org.jpox.samples.interfaces.Circle in project tests by datanucleus.
the class InterfacesTest method createSampleDataForQueryTests.
/**
* Convenience method to create data for querying.
*/
protected void createSampleDataForQueryTests() {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
// Create some objects for our query tests
try {
tx.begin();
// create some shapes
ShapeHolder[] shapeHolders = new ShapeHolder[25];
for (int i = 0; i < 5; ++i) {
// ShapeHolder with small circle
shapeHolders[i * 5] = new ShapeHolder(r.nextInt());
shapeHolders[i * 5].setShape1(new Circle(r.nextInt(), 1 + i * 0.1));
// ShapeHolder with large circle
shapeHolders[i * 5 + 1] = new ShapeHolder(r.nextInt());
shapeHolders[i * 5 + 1].setShape1(new Circle(r.nextInt(), 10 + i * 0.1));
// ShapeHolder with small rectangle
shapeHolders[i * 5 + 2] = new ShapeHolder(r.nextInt());
shapeHolders[i * 5 + 2].setShape1(new Rectangle(r.nextInt(), 1 + i * 0.1, 2 + i * 0.1));
// ShapeHolder with large rectangle
shapeHolders[i * 5 + 3] = new ShapeHolder(r.nextInt());
shapeHolders[i * 5 + 3].setShape1(new Rectangle(r.nextInt(), 10 + i * 0.1, 11 + i * .01));
// ShapeHolder with null
shapeHolders[i * 5 + 4] = new ShapeHolder(r.nextInt());
shapeHolders[i * 5 + 4].setShape1(null);
}
pm.makePersistentAll(shapeHolders);
tx.commit();
} catch (Exception e) {
LOG.error("Exception thrown during creation of test objects : ", e);
assertTrue("Exception thrown during creation of test objects : " + e.getMessage(), false);
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
}
use of org.jpox.samples.interfaces.Circle in project tests by datanucleus.
the class InterfacesTest method testMultipleImplementations.
/**
* Test the specification of multiple implementations. One field has duplicate impls, and another
* has full specification including columns (ORM only).
*/
public void testMultipleImplementations() throws Exception {
try {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
// Create container and some shapes
tx.begin();
ShapeHolder2 holder = new ShapeHolder2(r.nextInt());
Circle circle = new Circle(1, 102.0);
holder.setShape1(circle);
Rectangle rect = new Rectangle(2, 250, 200);
holder.setShape2(rect);
pm.makePersistent(holder);
tx.commit();
} catch (Exception e) {
fail(e.getLocalizedMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
// Clean out our data
clean(ShapeHolder2.class);
clean(Rectangle.class);
clean(Circle.class);
}
}
use of org.jpox.samples.interfaces.Circle in project tests by datanucleus.
the class InterfacesTest method testQueryOnInterfaceFields.
/**
* Run a simple query on a set of ShapeHolder objects whose fields are
* interfaces with datastore identity.
*/
public void testQueryOnInterfaceFields() {
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.declareImports("import org.jpox.samples.interfaces.Circle");
query.declareParameters("double radiusBound");
// set filter to return all circles with radius greater than given value
query.setFilter("((Circle)shape1).radius >= radiusBound");
Collection results = (Collection) query.execute(new Double(10));
assertEquals("Expected number of large circles returned by the query", 5, results.size());
for (Iterator i = results.iterator(); i.hasNext(); ) {
ShapeHolder result = (ShapeHolder) (i.next());
assertEquals("Query should only return circles", Circle.class, result.getShape1().getClass());
assertTrue("Query should only return large circles", ((Circle) (result.getShape1())).getRadius() >= 10.0);
}
tx.commit();
} catch (Exception e) {
LOG.error("Exception thrown during querying of objects : ", e);
assertTrue("Exception thrown during querying of objects : " + 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);
}
}
use of org.jpox.samples.interfaces.Circle 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);
}
}
Aggregations