Search in sources :

Example 1 with Circle

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

the class InterfacesTest method testReadAllShapeHolders.

/**
 * Check that ShapeHolder objects can be read back again using an Extent.
 */
public void testReadAllShapeHolders() throws Exception {
    try {
        addClassesToSchema(new Class[] { ShapeHolder.class, Rectangle.class, Circle.class, Square.class, Triangle.class });
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            Rectangle rectangle = new Rectangle(r.nextInt(), 1.0, 2.0);
            ShapeHolder rectangleHolder = new ShapeHolder(r.nextInt());
            rectangleHolder.setShape1(rectangle);
            pm.makePersistent(rectangleHolder);
            ShapeHolder circleHolder = new ShapeHolder(r.nextInt());
            Circle circle = (Circle) circleHolder.getShape1();
            pm.makePersistent(circleHolder);
            ShapeHolder squareHolder = new ShapeHolder(r.nextInt());
            Square square = new Square(r.nextInt(), 2.0, 3.5);
            squareHolder.setShape1(square);
            pm.makePersistent(squareHolder);
            ShapeHolder triangleHolder = new ShapeHolder(r.nextInt());
            Triangle triangle = new Triangle(r.nextInt(), "tri " + r.nextInt(), 2.1, 3.2);
            triangleHolder.setShape1(triangle);
            pm.makePersistent(triangleHolder);
            tx.commit();
            tx.begin();
            Extent extent = pm.getExtent(ShapeHolder.class, false);
            for (Iterator i = extent.iterator(); i.hasNext(); ) {
                ShapeHolder holder = (ShapeHolder) (i.next());
                Shape shape = holder.getShape1();
                if (holder.getShape1() instanceof Circle) {
                    // found the circle
                    assertNotNull("Two ShapeHolders with circles found in database, only one inserted.", circle);
                    assertEquals("Circle read back in ShapeHolder does not match the one inserted.", circle, shape);
                    circle = null;
                } else if (holder.getShape1() instanceof Rectangle) {
                    // found the rectangle
                    assertNotNull("Two ShapeHolders with rectangles found in database, only one inserted.", rectangle);
                    assertEquals("Rectangle read back in ShapeHolder does not match the one inserted.", rectangle, shape);
                    rectangle = null;
                } else if (holder.getShape1() instanceof Square) {
                    // found the square
                    assertNotNull("Two ShapeHolders with squares found in database, only one inserted.", square);
                    assertEquals("Square read back in ShapeHolder does not match the one inserted.", square, shape);
                    square = null;
                } else if (holder.getShape1() instanceof Triangle) {
                    // found the triangle
                    assertNotNull("Two ShapeHolders with triangles found in database, only one inserted.", triangle);
                    assertEquals("Triangle read back in ShapeHolder does not match the one inserted.", triangle, shape);
                    triangle = null;
                } else {
                    fail("Unknown shape [" + shape + "] found in ShapeHolder");
                }
            }
            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) Iterator(java.util.Iterator) Shape5Square(org.jpox.samples.interfaces.Shape5Square) Square(org.jpox.samples.interfaces.Square) JDOUserException(javax.jdo.JDOUserException)

Example 2 with Circle

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

the class InterfacesTest method testSetJoinUnidir.

/**
 * Test for the creation of a set for interface objects using Join table.
 */
public void testSetJoinUnidir() throws Exception {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            // Create container and some shapes
            tx.begin();
            ShapeHolder container = new ShapeHolder(r.nextInt());
            Circle circle = new Circle(r.nextInt(), 1.75);
            container.getShapeSet1().add(circle);
            Rectangle rectangle = new Rectangle(r.nextInt(), 1.0, 2.0);
            container.getShapeSet1().add(rectangle);
            pm.makePersistent(container);
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception thrown during creation of set of interface objects  using join table: ", e);
            assertTrue("Exception thrown during create of set of interface objects using join table : " + e.getMessage(), false);
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    // TODO Extend this to then query the elements in the collection
    } finally {
        clean(ShapeHolder.class);
        clean(Circle.class);
        clean(Rectangle.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) PersistenceManager(javax.jdo.PersistenceManager) Shape5Rectangle(org.jpox.samples.interfaces.Shape5Rectangle) Rectangle(org.jpox.samples.interfaces.Rectangle) JDOUserException(javax.jdo.JDOUserException)

Example 3 with Circle

use of org.jpox.samples.interfaces.Circle 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 4 with Circle

use of org.jpox.samples.interfaces.Circle 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 5 with Circle

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

the class InterfacesTest method testMultipleImplWithMissingColumns.

/**
 * Test a scenario that all columns are declared for an interface field
 * that has ColumnMetaData specified for only 1 implementation.
 */
public void testMultipleImplWithMissingColumns() throws Exception {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            // Create container and some shapes
            tx.begin();
            ShapeHolder4 holder = new ShapeHolder4(101);
            Circle circle = new Circle(1, 102.0);
            holder.setShape1(circle);
            pm.makePersistent(holder);
            tx.commit();
            fail("Expected exception, for wrong declared columns");
        } catch (Exception e) {
        // Do nothing
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
    }
}
Also used : ShapeHolder4(org.jpox.samples.interfaces.ShapeHolder4) Circle(org.jpox.samples.interfaces.Circle) Shape5Circle(org.jpox.samples.interfaces.Shape5Circle) Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) JDOUserException(javax.jdo.JDOUserException)

Aggregations

PersistenceManager (javax.jdo.PersistenceManager)11 Transaction (javax.jdo.Transaction)11 Circle (org.jpox.samples.interfaces.Circle)11 JDOUserException (javax.jdo.JDOUserException)10 Shape5Circle (org.jpox.samples.interfaces.Shape5Circle)10 ShapeHolder (org.jpox.samples.interfaces.ShapeHolder)9 Rectangle (org.jpox.samples.interfaces.Rectangle)8 Shape5Rectangle (org.jpox.samples.interfaces.Shape5Rectangle)8 Collection (java.util.Collection)4 Query (javax.jdo.Query)4 Iterator (java.util.Iterator)3 Extent (javax.jdo.Extent)2 Shape (org.jpox.samples.interfaces.Shape)2 Shape5Square (org.jpox.samples.interfaces.Shape5Square)2 Square (org.jpox.samples.interfaces.Square)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