Search in sources :

Example 1 with Shape5Holder

use of org.jpox.samples.interfaces.Shape5Holder in project tests by datanucleus.

the class InterfacesTest method testSetJoinBidir.

/**
 * Test for the creation of a set of interface objects using join table and bidirectional relation.
 */
public void testSetJoinBidir() throws Exception {
    try {
        addClassesToSchema(new Class[] { Shape5Holder.class, Shape5Rectangle.class, Shape5Circle.class, Shape5Square.class });
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        Object id;
        try {
            // Create container and some shapes
            tx.begin();
            Shape5Holder container = new Shape5Holder(r.nextInt());
            Shape5Circle circle = new Shape5Circle(r.nextInt(), 1.75);
            container.getShapeSet().add(circle);
            Shape5Rectangle rectangle = new Shape5Rectangle(r.nextInt(), 1.0, 2.0);
            container.getShapeSet().add(rectangle);
            assertEquals(2, container.getShapeSet().size());
            pm.makePersistent(container);
            tx.commit();
            id = pm.getObjectId(container);
            pm.close();
            pmf.getDataStoreCache().evictAll();
            pm = pmf.getPersistenceManager();
            tx = pm.currentTransaction();
            tx.begin();
            Shape5Holder actual = (Shape5Holder) pm.getObjectById(id);
            assertEquals(2, actual.getShapeSet().size());
            Iterator<Shape5> shIter = actual.getShapeSet().iterator();
            boolean circlePresent = false;
            boolean rectPresent = false;
            while (shIter.hasNext()) {
                Shape5 sh = shIter.next();
                if (sh instanceof Shape5Circle) {
                    circlePresent = true;
                    assertEquals(actual, sh.getShapeHolder());
                    assertEquals(1.75, ((Shape5Circle) sh).getRadius());
                } else if (sh instanceof Shape5Rectangle) {
                    rectPresent = true;
                    assertEquals(actual, sh.getShapeHolder());
                    assertEquals(1.0, ((Shape5Rectangle) sh).getWidth());
                    assertEquals(2.0, ((Shape5Rectangle) sh).getLength());
                }
            }
            assertTrue(circlePresent);
            assertTrue(rectPresent);
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception thrown during creation of set of interface objects using FK : ", e);
            assertTrue("Exception thrown during create of set of interface objects using FK : " + e.getMessage(), false);
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    // TODO Extend this to then query the elements in the collection
    } catch (Exception e) {
        LOG.error("Exception thrown during test of interface objects using FK : ", e);
        assertTrue("Exception thrown during test of interface objects using FK : " + e.getMessage(), false);
    } finally {
        clean(Shape5Holder.class);
        clean(Shape5Circle.class);
        clean(Shape5Rectangle.class);
    }
}
Also used : Shape5Holder(org.jpox.samples.interfaces.Shape5Holder) Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) Shape5Rectangle(org.jpox.samples.interfaces.Shape5Rectangle) Shape5Circle(org.jpox.samples.interfaces.Shape5Circle) Shape5(org.jpox.samples.interfaces.Shape5) JDOUserException(javax.jdo.JDOUserException)

Aggregations

JDOUserException (javax.jdo.JDOUserException)1 PersistenceManager (javax.jdo.PersistenceManager)1 Transaction (javax.jdo.Transaction)1 Shape5 (org.jpox.samples.interfaces.Shape5)1 Shape5Circle (org.jpox.samples.interfaces.Shape5Circle)1 Shape5Holder (org.jpox.samples.interfaces.Shape5Holder)1 Shape5Rectangle (org.jpox.samples.interfaces.Shape5Rectangle)1