Search in sources :

Example 11 with ShapeHolder

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

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

the class InterfacesTest method testInsertThenDelete.

/**
 * Insert a ShapeHolder object, delete it, then check that it's gone.
 */
public void testInsertThenDelete() throws Exception {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            // Create objects
            tx.begin();
            ShapeHolder holder = new ShapeHolder(r.nextInt());
            pm.makePersistent(holder);
            tx.commit();
            // Delete objects
            tx.begin();
            Extent extent = pm.getExtent(ShapeHolder.class, false);
            assertTrue("ShapeHolder should be available in extent.", extent.iterator().hasNext());
            pm.deletePersistent(extent.iterator().next());
            extent.closeAll();
            tx.commit();
            tx.begin();
            extent = pm.getExtent(ShapeHolder.class, false);
            assertFalse("There should be no more ShapeHolder objects.", extent.iterator().hasNext());
            extent.closeAll();
            tx.commit();
        } catch (JDOUserException ue) {
            assertTrue("Exception thrown during creation/deletion of interface objects.", false);
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        clean(ShapeHolder.class);
    }
}
Also used : ShapeHolder(org.jpox.samples.interfaces.ShapeHolder) Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) Extent(javax.jdo.Extent) JDOUserException(javax.jdo.JDOUserException)

Example 13 with ShapeHolder

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

the class InterfacesTest method testNullValues.

/**
 * Check that null shapes work correctly.
 */
public void testNullValues() throws Exception {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            ShapeHolder holder = new ShapeHolder(r.nextInt());
            holder.setShape1(null);
            pm.makePersistent(holder);
            tx.commit();
            tx.begin();
            Extent extent = pm.getExtent(ShapeHolder.class, false);
            holder = (ShapeHolder) (extent.iterator().next());
            assertNull("Shape should be read back from database as null", holder.getShape1());
            extent.closeAll();
            tx.commit();
        } catch (JDOUserException ue) {
            assertTrue("Exception thrown during create/query of interface objects", false);
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        clean(ShapeHolder.class);
    }
}
Also used : ShapeHolder(org.jpox.samples.interfaces.ShapeHolder) Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) Extent(javax.jdo.Extent) JDOUserException(javax.jdo.JDOUserException)

Example 14 with ShapeHolder

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

the class InterfacesTest method testQueryOnInterfaceFields.

/**
 * Run a simple query on a set of ShapeHolder objects whose fields are
 * interfaces with datastore identity.
 */
public void testQueryOnInterfaceFields() {
    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");
            query.declareParameters("double radiusBound");
            // set filter to return all circles with radius greater than given value
            query.setFilter("((Circle)shape1).radius >= radiusBound");
            Collection results = (Collection) query.execute(new Double(10));
            assertEquals("Expected number of large circles returned by the query", 5, results.size());
            for (Iterator i = results.iterator(); i.hasNext(); ) {
                ShapeHolder result = (ShapeHolder) (i.next());
                assertEquals("Query should only return circles", Circle.class, result.getShape1().getClass());
                assertTrue("Query should only return large circles", ((Circle) (result.getShape1())).getRadius() >= 10.0);
            }
            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) Collection(java.util.Collection) JDOUserException(javax.jdo.JDOUserException)

Example 15 with ShapeHolder

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

the class InterfacesTest method testQueryForNullShape.

/**
 * Query ShapeHolder objects with a null Shape.
 */
public void testQueryForNullShape() {
    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.setFilter("shape1 == null");
            Collection results = (Collection) query.execute();
            assertEquals("Expected number of ShapeHolder objects with null Shape is wrong", 5, results.size());
            for (Iterator i = results.iterator(); i.hasNext(); ) {
                ShapeHolder result = (ShapeHolder) (i.next());
                assertNull("ShapeHolder has non-null Shape!", result.getShape1());
            }
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception thrown during querying for 'null' shape", e);
            assertTrue("Exception thrown during querying for 'null' shape : " + 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 : ShapeHolder(org.jpox.samples.interfaces.ShapeHolder) Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) Iterator(java.util.Iterator) Collection(java.util.Collection) JDOUserException(javax.jdo.JDOUserException)

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