use of org.datanucleus.samples.types.interfaces.Circle in project tests by datanucleus.
the class EntityManagerTest method testPersistEntityWithInterfaceField.
/**
* Test of persistence/retrieval of object with an interface field.
*/
public void testPersistEntityWithInterfaceField() {
try {
EntityManager em = getEM();
EntityTransaction tx = em.getTransaction();
try {
tx.begin();
ShapeHolder holder = new ShapeHolder(100);
holder.setShape1(new Rectangle(200, 45, 55));
Circle circ = new Circle(300, 25.8);
holder.getShapeSet1().add(circ);
em.persist(holder);
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
// Retrieve it
em = getEM();
tx = em.getTransaction();
try {
tx.begin();
ShapeHolder holder = em.find(ShapeHolder.class, 100);
assertNotNull(holder);
Shape shape = holder.getShape1();
assertNotNull(shape);
assertTrue(shape instanceof Rectangle);
Rectangle rect = (Rectangle) shape;
assertEquals(45.0, rect.getWidth(), 0.1);
assertEquals(55.0, rect.getLength(), 0.1);
Set<Shape> shapes = holder.getShapeSet1();
assertNotNull(shapes);
assertEquals(1, shapes.size());
Shape setShape = shapes.iterator().next();
assertTrue(setShape instanceof Circle);
Circle circ = (Circle) setShape;
assertEquals(25.8, circ.getRadius(), 0.1);
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
} finally {
clean(ShapeHolder.class);
clean(Rectangle.class);
clean(Circle.class);
}
}
Aggregations