Search in sources :

Example 6 with ShapeHolder

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

the class InterfacesTest method testQueryOnInterfaceFieldsBooleanOps.

/**
 * Combine expressions involving the interface fields using boolean
 * operators.
 */
public void testQueryOnInterfaceFieldsBooleanOps() {
    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; import org.jpox.samples.interfaces.Rectangle");
            // set filter to return large squares and circles
            query.setFilter("((Circle)shape1).radius >= 10 || ((Rectangle)shape1).length >= 10");
            Collection results = (Collection) query.execute();
            assertEquals("Expected number of large circles and rectangles in the query", 10, results.size());
            for (Iterator i = results.iterator(); i.hasNext(); ) {
                ShapeHolder result = (ShapeHolder) (i.next());
                if (result.getShape1() instanceof Circle) {
                    assertTrue("Query should only return large circles", ((Circle) (result.getShape1())).getRadius() >= 10.0);
                } else if (result.getShape1() instanceof Rectangle) {
                    assertTrue("Query should only return large rectangles", ((Rectangle) (result.getShape1())).getArea() >= 100.0);
                } else {
                    fail("Query returned a " + result.getShape1().getClass() + " when only circles and rectangles were expected");
                }
            }
            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);
    }
}
Also used : Circle(org.jpox.samples.interfaces.Circle) Shape5Circle(org.jpox.samples.interfaces.Shape5Circle) ShapeHolder(org.jpox.samples.interfaces.ShapeHolder) Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) Iterator(java.util.Iterator) Shape5Rectangle(org.jpox.samples.interfaces.Shape5Rectangle) Rectangle(org.jpox.samples.interfaces.Rectangle) Collection(java.util.Collection) JDOUserException(javax.jdo.JDOUserException)

Example 7 with ShapeHolder

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

the class InterfacesTest method testChangeImplementation.

/**
 * Persist a ShapeHolder with one implementation of Shape, then change to
 * a different implementation and check that the saved version also has
 * its implementation updated.
 */
public void testChangeImplementation() {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            // Create the ShapeHolder record
            tx.begin();
            ShapeHolder holder = new ShapeHolder(r.nextInt());
            Shape shape = new Rectangle(r.nextInt(), 5.0, 3.4);
            holder.setShape1(shape);
            pm.makePersistent(holder);
            tx.commit();
            // Load it back and change the Shape to a Circle
            tx.begin();
            Extent extent = pm.getExtent(ShapeHolder.class, false);
            holder = (ShapeHolder) (extent.iterator().next());
            assertEquals("The inserted rectangle", shape, holder.getShape1());
            shape = new Circle(r.nextInt(), 1.75);
            holder.setShape1(shape);
            extent.closeAll();
            tx.commit();
            // Load back the holder and check that its Shape is a Circle, then change
            // it back to a Rectangle
            tx.begin();
            extent = pm.getExtent(ShapeHolder.class, false);
            holder = (ShapeHolder) (extent.iterator().next());
            assertEquals("The inserted circle", shape, holder.getShape1());
            shape = new Rectangle(r.nextInt(), 3.897, 1.18);
            holder.setShape1(shape);
            extent.closeAll();
            tx.commit();
            // check that the holder's shape is a Rectangle again
            tx.begin();
            extent = pm.getExtent(ShapeHolder.class, false);
            holder = (ShapeHolder) (extent.iterator().next());
            assertEquals("The re-inserted rectangle", shape, holder.getShape1());
            extent.closeAll();
            tx.commit();
            // Do it again with application identity classes
            tx.begin();
            extent = pm.getExtent(ShapeHolder.class, false);
            holder = (ShapeHolder) (extent.iterator().next());
            shape = new Triangle(r.nextInt(), "tri" + r.nextInt(), 4.97, 7.29);
            holder.setShape1(shape);
            extent.closeAll();
            tx.commit();
            // check that the holder's shape is a Triangle again
            tx.begin();
            extent = pm.getExtent(ShapeHolder.class, false);
            holder = (ShapeHolder) (extent.iterator().next());
            assertEquals("The re-inserted triangle", shape, holder.getShape1());
            extent.closeAll();
            tx.commit();
        } catch (JDOUserException ue) {
            assertTrue("Exception thrown during test.", false);
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        clean(ShapeHolder.class);
        clean(Circle.class);
        clean(Triangle.class);
        clean(Rectangle.class);
        clean(Square.class);
    }
}
Also used : Circle(org.jpox.samples.interfaces.Circle) Shape5Circle(org.jpox.samples.interfaces.Shape5Circle) ShapeHolder(org.jpox.samples.interfaces.ShapeHolder) Shape(org.jpox.samples.interfaces.Shape) Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) Extent(javax.jdo.Extent) Shape5Rectangle(org.jpox.samples.interfaces.Shape5Rectangle) Rectangle(org.jpox.samples.interfaces.Rectangle) Triangle(org.jpox.samples.interfaces.Triangle) JDOUserException(javax.jdo.JDOUserException)

Example 8 with ShapeHolder

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

the class SerializationTest method testSerialisedInterface.

/**
 * Test for serialisation of Interface fields
 */
public void testSerialisedInterface() {
    try {
        Object holderId = null;
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        // Persist the object with serialised fields
        try {
            tx.begin();
            ShapeHolder holder = new ShapeHolder(1);
            holder.setShape2(new Circle(1, 25.0));
            pm.makePersistent(holder);
            // Update holder and serialised object fields to check that they get to the datastore
            holder.setId(2);
            ((Circle) holder.getShape1()).setRadius(40.0);
            tx.commit();
            holderId = pm.getObjectId(holder);
        } catch (Exception e) {
            LOG.error(">> Exception thrown in test", e);
            fail("Exception thrown while persisted object with serialised Interface field : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // Retrieve the object
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            ShapeHolder holder = (ShapeHolder) pm.getObjectById(holderId);
            assertTrue("Holder of serialised PC could not be retrieved!", holder != null);
            assertTrue("Holder 'id' field is incorrect", holder.getId() == 2);
            assertTrue("Retrieved holder has null serialised object!", holder.getShape1() != null);
            assertTrue("Retrieved holder has serialised object of incorrect type!", holder.getShape1() instanceof Circle);
            assertEquals("Retrieved serialised object description is incorrect : ", ((Circle) holder.getShape1()).getRadius(), 40.0, 0.01);
            // Update holder and serialised object fields to check that they get to the datastore
            ((Circle) holder.getShape1()).setId(40);
            holder.setId(7);
            tx.commit();
        } catch (Exception e) {
            LOG.error(">> Exception thrown in test", e);
            fail("Exception thrown while retrieving object with serialised Interface field : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // Retrieve the object again to check the most recent update
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            ShapeHolder holder = (ShapeHolder) pm.getObjectById(holderId);
            assertTrue("Holder of serialised PC could not be retrieved!", holder != null);
            assertTrue("Holder name is incorrect", holder.getId() == 7);
            assertTrue("Retrieved holder has null serialised object!", holder.getShape1() != null);
            assertTrue("Retrieved holder has serialised object of incorrect type!", holder.getShape1() instanceof Circle);
            assertEquals("Retrieved serialised object description is incorrect : ", ((Circle) holder.getShape1()).getId(), 40);
            tx.commit();
        } catch (Exception e) {
            LOG.error(">> Exception thrown in test", e);
            fail("Exception thrown while retrieving object with serialised Interface field : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        // Clean up our data
        clean(ShapeHolder.class);
        clean(Circle.class);
    }
}
Also used : Circle(org.jpox.samples.interfaces.Circle) ShapeHolder(org.jpox.samples.interfaces.ShapeHolder) Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) SerialisedObject(org.datanucleus.samples.serialised.SerialisedObject) IOException(java.io.IOException)

Example 9 with ShapeHolder

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

the class ReachabilityTest method testNewObjectUnreachableWithDirtyObjectOwner.

/**
 * Tests if a Square is reachable through a ShapeHolder at makePersistent
 * and unreachable upon commit and does have the correct states at different times.
 * Spec section 12.6.7.
 */
public void testNewObjectUnreachableWithDirtyObjectOwner() {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            Square square1 = new Square(101, 200.0, 350.5);
            // assert alpha is indeed transient
            assertFalse("newly created alpha: isPersistent() == true", JDOHelper.isPersistent(square1));
            assertFalse("newly created alpha: isNew() == true", JDOHelper.isNew(square1));
            assertFalse("newly created alpha: isDirty() == true", JDOHelper.isDirty(square1));
            Square square2 = new Square(102, 400.0, 550.0);
            ShapeHolder holder = new ShapeHolder(201);
            // now persist holder with reference to object
            holder.setShape1(square1);
            pm.makePersistent(holder);
            // assert object is persistent new
            assertTrue("reachable persistent : isPersistent() == false", JDOHelper.isPersistent(square1));
            assertTrue("reachable persistent : isNew() == false", JDOHelper.isNew(square1));
            assertTrue("reachable persistent : isDirty() == false", JDOHelper.isDirty(square1));
            // now replace reference to square1 with reference to square2 and commit
            holder.setShape1(square2);
            tx.commit();
            assertFalse("unreachable committed : isPersistent() == true", JDOHelper.isPersistent(square1));
            assertFalse("unreachable committed : isNew() == true", JDOHelper.isNew(square1));
            assertFalse("unreachable committed : isDirty() == true", JDOHelper.isDirty(square1));
            tx.begin();
            Square square3 = new Square(103, 120.0, 240.0);
            holder.setShape1(square3);
            pm.flush();
            holder.setShape1(null);
            tx.commit();
            // assert object reverted to transient - JDO spec 12.6.7
            // reachability algorithm should be run again at commit() to check if instances
            // are no longer reachable and so should revert to transient
            assertFalse("unreachable committed delta: isPersistent() == true", JDOHelper.isPersistent(square3));
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            // assert there is no "alpha" record in DB
            tx.begin();
            Query q = pm.newQuery(Square.class, "id == :val");
            Collection c = (Collection) q.execute(new Integer(101));
            assertTrue("unexpectedly encountered Square that shouldnt have been persisted", c.size() == 0);
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        clean(ShapeHolder.class);
        clean(Square.class);
    }
}
Also used : ShapeHolder(org.jpox.samples.interfaces.ShapeHolder) Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) Collection(java.util.Collection) Square(org.jpox.samples.interfaces.Square)

Example 10 with ShapeHolder

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

the class ReachabilityTest method performOneToOneUniInterface.

/**
 * Test for reachability using a 1-1 unidirectional relation between a class and an interface.
 * Tests that when persisting the owner object the related object is also persisted.
 * @param optimisticTxn Whether to use optimistic txns
 */
protected void performOneToOneUniInterface(boolean optimisticTxn) {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        Object holderId = null;
        Object squareId = null;
        try {
            tx.setOptimistic(optimisticTxn);
            tx.begin();
            // Create the objects of the 1-1 uni relation
            ShapeHolder holder = new ShapeHolder(101);
            Square square = new Square(1, 100, 200);
            holder.setShape1(square);
            // Check that both are transient
            assertTrue("Object state of new ShapeHolder is incorrect", !JDOHelper.isPersistent(holder) && !JDOHelper.isNew(holder) && !JDOHelper.isDirty(holder));
            assertTrue("Object state of new Square is incorrect", !JDOHelper.isPersistent(square) && !JDOHelper.isNew(square) && !JDOHelper.isDirty(square));
            // Persist the ShapeHolder (so the Square should be persisted too)
            pm.makePersistent(holder);
            // Check that both are persistent-new (JDO2 spec 12.6.7)
            assertTrue("Object state of newly persisted ShapeHolder is incorrect", JDOHelper.isPersistent(holder) && JDOHelper.isNew(holder) && JDOHelper.isDirty(holder));
            assertTrue("Object state of newly persisted (by reachability) Square is incorrect", JDOHelper.isPersistent(square) && JDOHelper.isNew(square) && JDOHelper.isDirty(square));
            // Commit
            tx.commit();
            // Check that both are clean/hollow
            assertTrue("Object state of committed Qualification is incorrect", JDOHelper.isPersistent(holder) && !JDOHelper.isNew(holder) && !JDOHelper.isDirty(holder));
            assertTrue("Object state of committed (by reachability) Organisation is incorrect", JDOHelper.isPersistent(square) && !JDOHelper.isNew(square) && !JDOHelper.isDirty(square));
            holderId = pm.getObjectId(holder);
            squareId = pm.getObjectId(square);
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // Check that the objects exist in the datastore
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            Square square = (Square) pm.getObjectById(squareId);
            assertTrue("Square is not in the datastore!", square != null);
            ShapeHolder holder = (ShapeHolder) pm.getObjectById(holderId);
            assertTrue("ShapeeHolder is not in the datastore!", holder != null);
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        // Clean out our data
        clean(ShapeHolder.class);
        clean(Square.class);
    }
}
Also used : ShapeHolder(org.jpox.samples.interfaces.ShapeHolder) Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) Square(org.jpox.samples.interfaces.Square)

Aggregations

PersistenceManager (javax.jdo.PersistenceManager)17 Transaction (javax.jdo.Transaction)17 ShapeHolder (org.jpox.samples.interfaces.ShapeHolder)17 JDOUserException (javax.jdo.JDOUserException)13 Circle (org.jpox.samples.interfaces.Circle)9 Collection (java.util.Collection)8 Query (javax.jdo.Query)8 Shape5Circle (org.jpox.samples.interfaces.Shape5Circle)8 Rectangle (org.jpox.samples.interfaces.Rectangle)7 Shape5Rectangle (org.jpox.samples.interfaces.Shape5Rectangle)7 Iterator (java.util.Iterator)5 Square (org.jpox.samples.interfaces.Square)5 Extent (javax.jdo.Extent)4 Shape (org.jpox.samples.interfaces.Shape)2 Shape5Square (org.jpox.samples.interfaces.Shape5Square)2 Triangle (org.jpox.samples.interfaces.Triangle)2 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 SerialisedObject (org.datanucleus.samples.serialised.SerialisedObject)1