Search in sources :

Example 6 with Rectangle

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

the class InterfacesTest method createSampleDataForQueryTests.

/**
 * Convenience method to create data for querying.
 */
protected void createSampleDataForQueryTests() {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    // Create some objects for our query tests
    try {
        tx.begin();
        // create some shapes
        ShapeHolder[] shapeHolders = new ShapeHolder[25];
        for (int i = 0; i < 5; ++i) {
            // ShapeHolder with small circle
            shapeHolders[i * 5] = new ShapeHolder(r.nextInt());
            shapeHolders[i * 5].setShape1(new Circle(r.nextInt(), 1 + i * 0.1));
            // ShapeHolder with large circle
            shapeHolders[i * 5 + 1] = new ShapeHolder(r.nextInt());
            shapeHolders[i * 5 + 1].setShape1(new Circle(r.nextInt(), 10 + i * 0.1));
            // ShapeHolder with small rectangle
            shapeHolders[i * 5 + 2] = new ShapeHolder(r.nextInt());
            shapeHolders[i * 5 + 2].setShape1(new Rectangle(r.nextInt(), 1 + i * 0.1, 2 + i * 0.1));
            // ShapeHolder with large rectangle
            shapeHolders[i * 5 + 3] = new ShapeHolder(r.nextInt());
            shapeHolders[i * 5 + 3].setShape1(new Rectangle(r.nextInt(), 10 + i * 0.1, 11 + i * .01));
            // ShapeHolder with null
            shapeHolders[i * 5 + 4] = new ShapeHolder(r.nextInt());
            shapeHolders[i * 5 + 4].setShape1(null);
        }
        pm.makePersistentAll(shapeHolders);
        tx.commit();
    } catch (Exception e) {
        LOG.error("Exception thrown during creation of test objects : ", e);
        assertTrue("Exception thrown during creation of test objects : " + e.getMessage(), false);
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
}
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 7 with Rectangle

use of org.jpox.samples.interfaces.Rectangle 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)

Example 8 with Rectangle

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

the class InterfacesTest method testListJoin.

/**
 * Test for the creation of a set for interface objects using Join table.
 */
public void testListJoin() 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.getShapeList1().add(circle);
            Rectangle rectangle = new Rectangle(r.nextInt(), 1.0, 2.0);
            container.getShapeList1().add(rectangle);
            pm.makePersistent(container);
            pm.flush();
            Query q = pm.newQuery(ShapeHolder.class);
            q.setFilter("shapeList1.contains(c)");
            q.declareParameters("Circle c");
            Collection results = (Collection) q.execute(circle);
            assertEquals(1, results.size());
            q = pm.newQuery(ShapeHolder.class);
            q.setFilter("shapeList1.contains(c) && c.id == -1");
            q.declareVariables("Circle c");
            results = (Collection) q.execute();
            assertEquals(0, results.size());
            q = pm.newQuery(ShapeHolder.class);
            q.setFilter("shapeList1.contains(c) && c.id == " + circle.getId());
            q.declareVariables("Circle c");
            results = (Collection) q.execute();
            assertEquals(1, results.size());
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception thrown during creation of list of interface objects  using join table", e);
            assertTrue("Exception thrown during create of list 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) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) Shape5Rectangle(org.jpox.samples.interfaces.Shape5Rectangle) Rectangle(org.jpox.samples.interfaces.Rectangle) Collection(java.util.Collection) JDOUserException(javax.jdo.JDOUserException)

Example 9 with Rectangle

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

the class InterfacesTest method testQueryForOneToOneShape.

/**
 * Query ShapeHolder objects that have a particular shape in the 1-1 field.
 */
public void testQueryForOneToOneShape() {
    try {
        // Create sample data for querying
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        Object holder1Id = null;
        Object rect1Id = null;
        try {
            tx.begin();
            ShapeHolder holder1 = new ShapeHolder(101);
            Rectangle rect1 = new Rectangle(200, 140, 250);
            Circle circ = new Circle(300, 45);
            holder1.setShape1(rect1);
            holder1.setShape2(circ);
            pm.makePersistent(holder1);
            ShapeHolder holder2 = new ShapeHolder(102);
            Rectangle rect2 = new Rectangle(201, 250, 140);
            Square sq = new Square(400, 500, 500);
            holder2.setShape1(rect2);
            holder2.setShape2(sq);
            pm.makePersistent(holder2);
            tx.commit();
            holder1Id = pm.getObjectId(holder1);
            rect1Id = pm.getObjectId(rect1);
        } catch (Exception e) {
            LOG.error("Exception during persist of sample data", e);
            fail("Exception creating sample data : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            Rectangle rect1 = (Rectangle) pm.getObjectById(rect1Id);
            Query query = pm.newQuery("SELECT FROM " + ShapeHolder.class.getName() + " WHERE shape1 == :myShape");
            Map params = new HashMap();
            params.put("myShape", rect1);
            Collection results = (Collection) query.executeWithMap(params);
            assertEquals("Number of ShapeHolders with 1-1 field as our Rectangle was wrong", 1, results.size());
            ShapeHolder holder = (ShapeHolder) results.iterator().next();
            assertEquals("Id of ShapeHolder was wrong", holder1Id, pm.getObjectId(holder));
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception thrown during querying for 1-1 shape", e);
            assertTrue("Exception thrown during querying for 1-1 shape : " + e.getMessage(), false);
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        // Clean out data
        clean(ShapeHolder.class);
        clean(Rectangle.class);
        clean(Circle.class);
        clean(Square.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) HashMap(java.util.HashMap) Shape5Rectangle(org.jpox.samples.interfaces.Shape5Rectangle) Rectangle(org.jpox.samples.interfaces.Rectangle) Collection(java.util.Collection) Shape5Square(org.jpox.samples.interfaces.Shape5Square) Square(org.jpox.samples.interfaces.Square) HashMap(java.util.HashMap) Map(java.util.Map) JDOUserException(javax.jdo.JDOUserException)

Example 10 with Rectangle

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

the class ArrayTest method testJoinTableInterfaceQueryArray.

/**
 * Test for a field of type Interface[] stored using a join table.
 */
public void testJoinTableInterfaceQueryArray() {
    Shape[] shapes1 = new Shape[2];
    shapes1[0] = new Rectangle(1, 25.0, 20.0);
    shapes1[1] = new Rectangle(2, 35.0, 10.0);
    Shape[] shapes2 = new Shape[2];
    shapes2[0] = new Rectangle(3, 25.0, 20.0);
    shapes2[1] = new Rectangle(4, 35.0, 10.0);
    InterfaceArray holder = new InterfaceArray(shapes1, shapes2);
    Shape[] expectedShapes1 = new Shape[2];
    expectedShapes1[0] = (Shape) ((Rectangle) shapes1[0]).clone();
    expectedShapes1[1] = (Shape) ((Rectangle) shapes1[1]).clone();
    performArrayQueryTest(holder, Shape[].class, expectedShapes1);
    clean(Rectangle.class);
}
Also used : Shape(org.jpox.samples.interfaces.Shape) Rectangle(org.jpox.samples.interfaces.Rectangle) InterfaceArray(org.jpox.samples.array.InterfaceArray)

Aggregations

Rectangle (org.jpox.samples.interfaces.Rectangle)10 JDOUserException (javax.jdo.JDOUserException)8 PersistenceManager (javax.jdo.PersistenceManager)8 Transaction (javax.jdo.Transaction)8 Circle (org.jpox.samples.interfaces.Circle)8 Shape5Circle (org.jpox.samples.interfaces.Shape5Circle)8 Shape5Rectangle (org.jpox.samples.interfaces.Shape5Rectangle)8 ShapeHolder (org.jpox.samples.interfaces.ShapeHolder)7 Shape (org.jpox.samples.interfaces.Shape)4 Collection (java.util.Collection)3 Query (javax.jdo.Query)3 Iterator (java.util.Iterator)2 Extent (javax.jdo.Extent)2 InterfaceArray (org.jpox.samples.array.InterfaceArray)2 Shape5Square (org.jpox.samples.interfaces.Shape5Square)2 Square (org.jpox.samples.interfaces.Square)2 Triangle (org.jpox.samples.interfaces.Triangle)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ShapeHolder2 (org.jpox.samples.interfaces.ShapeHolder2)1