use of org.jpox.samples.interfaces.ShapeHolder 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.ShapeHolder in project tests by datanucleus.
the class InterfacesTest method testInsertThenDelete.
/**
* Insert a ShapeHolder object, delete it, then check that it's gone.
*/
public void testInsertThenDelete() throws Exception {
try {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
// Create objects
tx.begin();
ShapeHolder holder = new ShapeHolder(r.nextInt());
pm.makePersistent(holder);
tx.commit();
// Delete objects
tx.begin();
Extent extent = pm.getExtent(ShapeHolder.class, false);
assertTrue("ShapeHolder should be available in extent.", extent.iterator().hasNext());
pm.deletePersistent(extent.iterator().next());
extent.closeAll();
tx.commit();
tx.begin();
extent = pm.getExtent(ShapeHolder.class, false);
assertFalse("There should be no more ShapeHolder objects.", extent.iterator().hasNext());
extent.closeAll();
tx.commit();
} catch (JDOUserException ue) {
assertTrue("Exception thrown during creation/deletion 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 testNullValues.
/**
* Check that null shapes work correctly.
*/
public void testNullValues() throws Exception {
try {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
ShapeHolder holder = new ShapeHolder(r.nextInt());
holder.setShape1(null);
pm.makePersistent(holder);
tx.commit();
tx.begin();
Extent extent = pm.getExtent(ShapeHolder.class, false);
holder = (ShapeHolder) (extent.iterator().next());
assertNull("Shape should be read back from database as null", holder.getShape1());
extent.closeAll();
tx.commit();
} catch (JDOUserException ue) {
assertTrue("Exception thrown during create/query 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 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.ShapeHolder in project tests by datanucleus.
the class InterfacesTest method testQueryForNullShape.
/**
* Query ShapeHolder objects with a null Shape.
*/
public void testQueryForNullShape() {
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", 5, results.size());
for (Iterator i = results.iterator(); i.hasNext(); ) {
ShapeHolder result = (ShapeHolder) (i.next());
assertNull("ShapeHolder has non-null Shape!", result.getShape1());
}
tx.commit();
} catch (Exception e) {
LOG.error("Exception thrown during querying for 'null' shape", e);
assertTrue("Exception thrown during querying for '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