Search in sources :

Example 1 with ShapeHolder2

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

the class ReachabilityTest method testIncorrectAssignment.

/**
 * Tests whether a ClassCastException is thrown upon incorrect (that is undeclared) assignment
 * to a interface field. See JDO spec 1.0 �6.4.3 Object Class type: "If an implementation
 * restricts instances to be assigned to the field, a ClassCastException must be thrown at the
 * time of any incorrect assignment."
 */
public void testIncorrectAssignment() {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            Square square1 = new Square(101, 250.5, 650.0);
            ShapeHolder2 holder = new ShapeHolder2(301);
            holder.setShape1(square1);
            holder.setShape2(null);
            try {
                // Try to set "shape2" field to this Square - should be prohibited.
                LOG.info(">> Persisting ShapeHolder.shape2 as Square");
                pm.makePersistent(holder);
                LOG.info(">> makePersistent called");
                tx.commit();
                LOG.info(">> committed");
                assertTrue("incorrect assignment of Square instance was accepted", false);
            } catch (ClassCastException e) {
            // this exception is expected
            }
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        clean(ShapeHolder2.class);
        clean(Square.class);
    }
}
Also used : ShapeHolder2(org.jpox.samples.interfaces.ShapeHolder2) Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) Square(org.jpox.samples.interfaces.Square)

Example 2 with ShapeHolder2

use of org.jpox.samples.interfaces.ShapeHolder2 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);
    }
}
Also used : Circle(org.jpox.samples.interfaces.Circle) Shape5Circle(org.jpox.samples.interfaces.Shape5Circle) ShapeHolder2(org.jpox.samples.interfaces.ShapeHolder2) Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) Shape5Rectangle(org.jpox.samples.interfaces.Shape5Rectangle) Rectangle(org.jpox.samples.interfaces.Rectangle) JDOUserException(javax.jdo.JDOUserException)

Aggregations

PersistenceManager (javax.jdo.PersistenceManager)2 Transaction (javax.jdo.Transaction)2 ShapeHolder2 (org.jpox.samples.interfaces.ShapeHolder2)2 JDOUserException (javax.jdo.JDOUserException)1 Circle (org.jpox.samples.interfaces.Circle)1 Rectangle (org.jpox.samples.interfaces.Rectangle)1 Shape5Circle (org.jpox.samples.interfaces.Shape5Circle)1 Shape5Rectangle (org.jpox.samples.interfaces.Shape5Rectangle)1 Square (org.jpox.samples.interfaces.Square)1