Search in sources :

Example 1 with ShapeHolder

use of org.datanucleus.samples.types.interfaces.ShapeHolder 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);
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) Circle(org.datanucleus.samples.types.interfaces.Circle) EntityManager(javax.persistence.EntityManager) ShapeHolder(org.datanucleus.samples.types.interfaces.ShapeHolder) Shape(org.datanucleus.samples.types.interfaces.Shape) Rectangle(org.datanucleus.samples.types.interfaces.Rectangle)

Aggregations

EntityManager (javax.persistence.EntityManager)1 EntityTransaction (javax.persistence.EntityTransaction)1 Circle (org.datanucleus.samples.types.interfaces.Circle)1 Rectangle (org.datanucleus.samples.types.interfaces.Rectangle)1 Shape (org.datanucleus.samples.types.interfaces.Shape)1 ShapeHolder (org.datanucleus.samples.types.interfaces.ShapeHolder)1