Search in sources :

Example 66 with JDOUserException

use of javax.jdo.JDOUserException in project tests by datanucleus.

the class ObjectsTest method testOneToOneRelation.

/**
 * Test for 1-1 relations where 1 side is marked as an Object, though is
 * really PersistenceCapable.
 */
public void testOneToOneRelation() {
    try {
        Object holderId = null;
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            // Create some objects.
            tx.begin();
            ObjectImpl1 impl1 = new ObjectImpl1("First implementation");
            ObjectHolder holder = new ObjectHolder("First Holder");
            holder.setObject3(impl1);
            pm.makePersistent(holder);
            tx.commit();
            holderId = pm.getObjectId(holder);
        } catch (JDOUserException ue) {
            assertTrue("Exception thrown during create of java.lang.Object objects.", false);
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // Retrieve the holder and check its contents
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            ObjectHolder holder = (ObjectHolder) pm.getObjectById(holderId);
            assertTrue("Holder was not retrieved correctly", holder != null);
            assertTrue("Holder nonserialised object is null!", holder.getObject3() != null);
            assertEquals("Holder nonserialised object was of incorrect type", holder.getObject3().getClass().getName(), "org.jpox.samples.objects.ObjectImpl1");
            assertEquals("Holder nonserialised object has incorrect name!", ((ObjectImpl1) holder.getObject3()).getName(), "First implementation");
            tx.commit();
        } catch (JDOUserException ue) {
            assertTrue("Exception thrown during retrieval of java.lang.Object objects.", false);
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // Update the object field to use a different object
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            ObjectHolder holder = (ObjectHolder) pm.getObjectById(holderId);
            holder.setObject3(new ObjectImpl1("Second implementation"));
            tx.commit();
        } catch (JDOUserException ue) {
            fail("Exception thrown during update of object container with new object reference : " + ue.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // Retrieve the container and check its contents
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            ObjectHolder holder = (ObjectHolder) pm.getObjectById(holderId);
            assertTrue("Holder was not retrieved correctly", holder != null);
            assertTrue("Holder nonserialised object is null!", holder.getObject3() != null);
            assertEquals("Holder nonserialised object was of incorrect type", holder.getObject3().getClass().getName(), "org.jpox.samples.objects.ObjectImpl1");
            assertEquals("Holder nonserialised object has incorrect name!", ((ObjectImpl1) holder.getObject3()).getName(), "Second implementation");
            tx.commit();
        } catch (JDOUserException ue) {
            assertTrue("Exception thrown during retrieval of java.lang.Object objects.", false);
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        // Clean out our data
        clean(ObjectHolder.class);
        clean(ObjectImpl1.class);
    }
}
Also used : ObjectHolder(org.jpox.samples.objects.ObjectHolder) ObjectImpl1(org.jpox.samples.objects.ObjectImpl1) Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) JDOUserException(javax.jdo.JDOUserException)

Example 67 with JDOUserException

use of javax.jdo.JDOUserException in project tests by datanucleus.

the class AbstractClassesTest method perform1to1Deletion.

/**
 * Method to perform a test for deletion of abstract objects
 * @param holderClass The holder
 * @param abstractBaseClass The abstract base class
 * @param concreteClass1 Concrete class 1
 * @param concreteClass2 Concrete class 2
 * @throws Exception Thrown if an error occurs
 */
private void perform1to1Deletion(Class holderClass, Class abstractBaseClass, Class concreteClass1, Class concreteClass2) throws Exception {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        // Retrieve the ids of the abstract objects
        Object abstract_id_1 = null;
        Object abstract_id_2 = null;
        tx.begin();
        Extent e1 = pm.getExtent(abstractBaseClass, true);
        Iterator iter1 = e1.iterator();
        while (iter1.hasNext()) {
            Object base = iter1.next();
            if (abstract_id_1 == null) {
                abstract_id_1 = pm.getObjectId(base);
            } else {
                abstract_id_2 = pm.getObjectId(base);
            }
        }
        tx.commit();
        // Retrieve the ids of the holder objects
        Object holder_id_1 = null;
        Object holder_id_2 = null;
        tx.begin();
        Extent e2 = pm.getExtent(holderClass, false);
        Iterator iter2 = e2.iterator();
        while (iter2.hasNext()) {
            Object holder = iter2.next();
            if (holder_id_1 == null) {
                holder_id_1 = pm.getObjectId(holder);
            } else {
                holder_id_2 = pm.getObjectId(holder);
            }
        }
        tx.commit();
        // Delete the objects
        tx.begin();
        Object holder_1 = pm.getObjectById(holder_id_1, false);
        pm.deletePersistent(holder_1);
        Object holder_2 = pm.getObjectById(holder_id_2, false);
        pm.deletePersistent(holder_2);
        Object abstract_1 = pm.getObjectById(abstract_id_1, false);
        pm.deletePersistent(abstract_1);
        Object abstract_2 = pm.getObjectById(abstract_id_2, false);
        pm.deletePersistent(abstract_2);
        tx.commit();
    } catch (JDOUserException ue) {
        ue.printStackTrace();
        LOG.error(ue);
        assertTrue("Exception thrown during retrieval and deletion of holder and abstract objects", false);
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) Extent(javax.jdo.Extent) Iterator(java.util.Iterator) JDOUserException(javax.jdo.JDOUserException)

Example 68 with JDOUserException

use of javax.jdo.JDOUserException in project tests by datanucleus.

the class InheritanceTest method testInheritanceWithNoTable.

/**
 * Test when a class is using subclass-table and no super/sub class define a table,
 * a JDOUserException is expected.
 */
public void testInheritanceWithNoTable() {
    try {
        HBase single;
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        boolean success = false;
        try {
            tx.begin();
            single = new HBase();
            single.setId(new Integer(1));
            pm.makePersistent(single);
            pm.flush();
            JDOHelper.getObjectId(single);
            tx.commit();
        } catch (JDOUserException ex) {
            success = true;
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        assertTrue("Expected JDOUserException", success);
    } finally {
    // Nothing to do since nothing created
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) JDOUserException(javax.jdo.JDOUserException) HBase(org.jpox.samples.inheritance.HBase)

Example 69 with JDOUserException

use of javax.jdo.JDOUserException in project tests by datanucleus.

the class InheritanceTest method performNewTableSubclassTableNewTableTest.

/**
 * Method to perform the test for strategies with "new-table", "subclass-table", "new-table" strategies.
 * @param baseClass The base class that uses "new-table"
 * @param noTableSubClass The sub class that uses "subclass-table"
 * @param newTableSubSubClass The subsubclass that uses "new-table"
 */
protected void performNewTableSubclassTableNewTableTest(Class baseClass, Class noTableSubClass, Class newTableSubSubClass) {
    // Create object
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    Object base_oid = null;
    Object sub_oid = null;
    try {
        tx.begin();
        Object baseobject = baseClass.newInstance();
        Method setNameMethod = baseClass.getMethod("setName", new Class[] { String.class });
        setNameMethod.invoke(baseobject, new Object[] { "My Base Example" });
        pm.makePersistent(baseobject);
        pm.flush();
        Object subobject = newTableSubSubClass.newInstance();
        Method subSetNameMethod = newTableSubSubClass.getMethod("setName", new Class[] { String.class });
        subSetNameMethod.invoke(subobject, new Object[] { "My Sub Example" });
        Method subSetValueMethod = newTableSubSubClass.getMethod("setValue", new Class[] { double.class });
        subSetValueMethod.invoke(subobject, new Object[] { new Double(1234.56) });
        pm.makePersistent(subobject);
        pm.flush();
        pm.getFetchPlan().addGroup(FetchPlan.ALL);
        Query q1 = pm.newQuery(baseClass, "name == \"My Sub Example\"");
        Collection coll1 = (Collection) q1.execute();
        assertTrue("Unable to find an object of type " + baseClass.getName() + " when one should have been found", coll1 != null);
        assertTrue("Should have found a single object of type " + baseClass.getName() + ", but found " + coll1.size(), coll1.size() == 1);
        tx.commit();
        base_oid = pm.getObjectId(baseobject);
        sub_oid = pm.getObjectId(subobject);
    } catch (Exception e) {
        LOG.error("Exception thrown during query", e);
        fail("Exception thrown during create of objects of classes " + baseClass.getName() + "," + newTableSubSubClass.getName() + " : " + e.getMessage());
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
    // Retrieve the objects using getObjectById
    pm = pmf.getPersistenceManager();
    tx = pm.currentTransaction();
    try {
        tx.begin();
        Object baseobject = pm.getObjectById(base_oid, false);
        if (baseobject == null) {
            fail("pm.getObjectById returned null when attempting to retrieve an object of type " + baseClass.getName());
        }
        assertTrue("Base object should have been of type " + baseClass.getName() + " but was of type " + baseobject.getClass().getName(), baseobject.getClass().getName().equals(baseClass.getName()));
        Object subobject = pm.getObjectById(sub_oid, false);
        if (subobject == null) {
            fail("pm.getObjectById returned null when attempting to retrieve an object of type " + newTableSubSubClass.getName());
        }
        assertTrue("Base object should have been of type " + newTableSubSubClass.getName() + " but was of type " + subobject.getClass().getName(), subobject.getClass().getName().equals(newTableSubSubClass.getName()));
        Method baseGetNameMethod = baseClass.getMethod("getName", new Class[] {});
        String base_name = (String) baseGetNameMethod.invoke(baseobject, new Object[] {});
        Method subGetNameMethod = newTableSubSubClass.getMethod("getName", new Class[] {});
        String sub_name = (String) subGetNameMethod.invoke(subobject, new Object[] {});
        Method getValueMethod = newTableSubSubClass.getMethod("getValue", new Class[] {});
        Double value = (Double) getValueMethod.invoke(subobject, new Object[] {});
        assertTrue(baseClass.getName() + " object \"name\" attribute is incorrect : is \"" + base_name + "\" but should have been \"My Base Example\"", base_name.equals("My Base Example"));
        assertTrue(newTableSubSubClass.getName() + " object \"name\" attribute is incorrect : is \"" + sub_name + "\" but should have been \"My Sub Example\"", sub_name.equals("My Sub Example"));
        assertTrue(newTableSubSubClass.getName() + " object \"value\" attribute is incorrect : is " + value.doubleValue() + " but should have been 1234.56", value.doubleValue() == 1234.56);
        tx.commit();
    } catch (Exception e) {
        LOG.error("Exception thrown during query", e);
        fail("Exception thrown during retrieval of objects of type " + baseClass.getName() + "," + newTableSubSubClass.getName() + " : " + e.getMessage());
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
    // Retrieve the object using Query, starting from base type
    pm = pmf.getPersistenceManager();
    tx = pm.currentTransaction();
    try {
        tx.begin();
        pm.getFetchPlan().addGroup(FetchPlan.ALL);
        Query q = pm.newQuery(baseClass);
        LOG.info(">> Query being performed on baseClass");
        Collection coll = (Collection) q.execute();
        LOG.info(">> Query for base class performed");
        assertTrue("Unable to find objects when 2 should have been found", coll != null);
        assertTrue("Should have found 2 objects, but found " + coll.size(), coll.size() == 2);
        Iterator iter = coll.iterator();
        while (iter.hasNext()) {
            Object obj = iter.next();
            if (obj.getClass().getName().equals(baseClass.getName())) {
                Method getNameMethod = baseClass.getMethod("getName", new Class[] {});
                String name = (String) getNameMethod.invoke(obj, new Object[] {});
                assertTrue(baseClass.getName() + " object \"name\" attribute is incorrect : is \"" + name + "\" but should have been \"My Base Example\"", name.equals("My Base Example"));
            } else if (obj.getClass().getName().equals(newTableSubSubClass.getName())) {
                Method getNameMethod = newTableSubSubClass.getMethod("getName", new Class[] {});
                String name = (String) getNameMethod.invoke(obj, new Object[] {});
                Method getValueMethod = newTableSubSubClass.getMethod("getValue", new Class[] {});
                Double value = (Double) getValueMethod.invoke(obj, new Object[] {});
                assertTrue(newTableSubSubClass.getName() + " object \"name\" attribute is incorrect : is \"" + name + "\" but should have been \"My Sub Example\"", name.equals("My Sub Example"));
                assertTrue(newTableSubSubClass.getName() + " object \"value\" attribute is incorrect : is " + value.doubleValue() + " but should have been 1234.56", value.doubleValue() == 1234.56);
            } else {
                fail("Query retrieved object of type " + obj.getClass().getName() + " !!!");
            }
        }
        tx.commit();
    } catch (Exception e) {
        e.printStackTrace();
        LOG.error(e);
        fail("Exception thrown during retrieval of object of type " + baseClass.getName() + " : " + e.getMessage());
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
    // Retrieve the object using Query, starting from sub type
    pm = pmf.getPersistenceManager();
    tx = pm.currentTransaction();
    try {
        tx.begin();
        pm.getFetchPlan().addGroup(FetchPlan.ALL);
        Query q = pm.newQuery(newTableSubSubClass, "value == 1234.56");
        Collection coll = (Collection) q.execute();
        assertTrue("Unable to find an " + newTableSubSubClass.getName() + " object when one should have been found", coll != null);
        assertTrue("Should have found a single " + newTableSubSubClass.getName() + " object, but found " + coll.size(), coll.size() == 1);
        Iterator iter = coll.iterator();
        while (iter.hasNext()) {
            Object obj = iter.next();
            Method getNameMethod = newTableSubSubClass.getMethod("getName", new Class[] {});
            String name = (String) getNameMethod.invoke(obj, new Object[] {});
            Method getValueMethod = newTableSubSubClass.getMethod("getValue", new Class[] {});
            Double value = (Double) getValueMethod.invoke(obj, new Object[] {});
            assertTrue(newTableSubSubClass.getName() + " object \"name\" attribute is incorrect : is \"" + name + "\" but should have been \"My Sub Example\"", name.equals("My Sub Example"));
            assertTrue(newTableSubSubClass.getName() + " object \"value\" attribute is incorrect : is " + value.doubleValue() + " but should have been 1234.56", value.doubleValue() == 1234.56);
        }
        tx.commit();
    } catch (Exception e) {
        fail("Exception thrown during retrieval of object of type " + newTableSubSubClass.getName() + " : " + e.getMessage());
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
    // Retrieve the object using Query, starting from no table type
    pm = pmf.getPersistenceManager();
    tx = pm.currentTransaction();
    try {
        tx.begin();
        pm.getFetchPlan().addGroup(FetchPlan.ALL);
        Query q = pm.newQuery(noTableSubClass, "value == 1234.56");
        Collection coll = (Collection) q.execute();
        assertTrue("Unable to find an " + noTableSubClass.getName() + " object when one should have been found", coll != null);
        assertTrue("Should have found a single " + noTableSubClass.getName() + " object, but found " + coll.size(), coll.size() == 1);
        Iterator iter = coll.iterator();
        while (iter.hasNext()) {
            Object obj = iter.next();
            Method getNameMethod = noTableSubClass.getMethod("getName", new Class[] {});
            String name = (String) getNameMethod.invoke(obj, new Object[] {});
            Method getValueMethod = noTableSubClass.getMethod("getValue", new Class[] {});
            Double value = (Double) getValueMethod.invoke(obj, new Object[] {});
            assertTrue(newTableSubSubClass.getName() + " object \"name\" attribute is incorrect : is \"" + name + "\" but should have been \"My Sub Example\"", name.equals("My Sub Example"));
            assertTrue(newTableSubSubClass.getName() + " object \"value\" attribute is incorrect : is " + value.doubleValue() + " but should have been 1234.56", value.doubleValue() == 1234.56);
        }
        tx.commit();
    } catch (Exception e) {
        LOG.info(">> Exception thrown in execution of query", e);
        fail("Exception thrown during retrieval of object of type " + noTableSubClass.getName() + " : " + e.getMessage());
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
    // Obtain an Extent of the base class.
    pm = pmf.getPersistenceManager();
    tx = pm.currentTransaction();
    try {
        tx.begin();
        Extent e = pm.getExtent(baseClass, true);
        Iterator iter = e.iterator();
        while (iter.hasNext()) {
            LOG.info("Extent => " + iter.next());
        }
        tx.commit();
    } catch (JDOUserException ue) {
        LOG.error("Exception thrown", ue);
        fail("Exception thrown during creation of Extent for type " + baseClass.getName() + " including subclasses : " + ue.getMessage());
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
    // Obtain an Extent of the sub class.
    pm = pmf.getPersistenceManager();
    tx = pm.currentTransaction();
    try {
        tx.begin();
        Extent e = pm.getExtent(newTableSubSubClass, true);
        Iterator iter = e.iterator();
        while (iter.hasNext()) {
            LOG.info("Extent => " + iter.next());
        }
        tx.commit();
    } catch (JDOUserException ue) {
        LOG.error("Exception thrown", ue);
        fail("Exception thrown during creation of Extent for type " + newTableSubSubClass.getName() + " including subclasses : " + ue.getMessage());
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
    // Obtain an Extent of the subclass-table class to see if this is possible. Should be possible, but will need to find
    // all sub-classes for the extent and give the results of (potentiall) multiple base tables in the extent.
    pm = pmf.getPersistenceManager();
    tx = pm.currentTransaction();
    try {
        tx.begin();
        Extent e = pm.getExtent(noTableSubClass, true);
        Iterator iter = e.iterator();
        while (iter.hasNext()) {
            LOG.info("Extent => " + iter.next());
        }
        // This should be possible since we have included instances of subclasses so should get some objects
        tx.commit();
    } catch (JDOUserException ue) {
        LOG.error("Exception thrown", ue);
        fail("Exception thrown during creation of Extent for type " + baseClass.getName() + " including subclasses : " + ue.getMessage());
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
// Update the object
// Delete the object
}
Also used : Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) Extent(javax.jdo.Extent) Method(java.lang.reflect.Method) JDOUserException(javax.jdo.JDOUserException) JDOUserException(javax.jdo.JDOUserException) Transaction(javax.jdo.Transaction) Iterator(java.util.Iterator) Collection(java.util.Collection)

Example 70 with JDOUserException

use of javax.jdo.JDOUserException in project tests by datanucleus.

the class InheritanceTest method performSubclassTableNewTableTest.

// --------------------------------- Perform the tests -----------------------------------------
/**
 * Method to perform the test for "subclass-table" strategies.
 * All classes passed into this should fit the idea of a baseclass
 * with "subclass-table" strategy and subclass with "new-table" strategy.
 * @param baseClass The base class that uses "subclass-table"
 * @param subClass The sub class that uses "new-table"
 */
protected void performSubclassTableNewTableTest(Class baseClass, Class subClass) {
    // Create object
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    Object oid = null;
    try {
        tx.begin();
        Object obj = subClass.newInstance();
        Method setNameMethod = subClass.getMethod("setName", new Class[] { String.class });
        setNameMethod.invoke(obj, new Object[] { "My Example" });
        Method setValueMethod = subClass.getMethod("setValue", new Class[] { double.class });
        setValueMethod.invoke(obj, new Object[] { new Double(1234.56) });
        pm.makePersistent(obj);
        pm.flush();
        pm.getFetchPlan().addGroup(FetchPlan.ALL);
        Query q1 = pm.newQuery(subClass, "name == \"My Example\"");
        Collection coll1 = (Collection) q1.execute();
        assertTrue("Unable to find an object of type " + subClass.getName() + " when one should have been found", coll1 != null);
        assertTrue("Should have found a single object of type " + subClass.getName() + ", but found " + coll1.size(), coll1.size() == 1);
        tx.commit();
        oid = pm.getObjectId(obj);
    } catch (Exception e) {
        e.printStackTrace();
        LOG.error(e);
        fail("Exception thrown during create of object of class " + subClass.getName() + " : " + e.getMessage());
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
    // Retrieve the object using getObjectById
    pm = pmf.getPersistenceManager();
    tx = pm.currentTransaction();
    try {
        tx.begin();
        Object obj = pm.getObjectById(oid, false);
        if (obj == null) {
            fail("pm.getObjectById returned null when attempting to retrieve an object of type InheritSubNoTable");
        }
        LOG.info(">> Object retrieved is of type " + obj.getClass().getName());
        Method getNameMethod = subClass.getMethod("getName", new Class[] {});
        String name = (String) getNameMethod.invoke(obj, new Object[] {});
        Method getValueMethod = subClass.getMethod("getValue", new Class[] {});
        Double value = (Double) getValueMethod.invoke(obj, new Object[] {});
        assertTrue(subClass.getName() + " object \"name\" attribute is incorrect : is \"" + name + "\" but should have been \"My Example\"", name.equals("My Example"));
        assertTrue(subClass.getName() + " object \"value\" attribute is incorrect : is " + value.doubleValue() + " but should have been 1234.56", value.doubleValue() == 1234.56);
        tx.commit();
    } catch (Exception e) {
        e.printStackTrace();
        LOG.error(e);
        fail("Exception thrown during retrieval of object of type " + subClass.getName() + " : " + e.getMessage());
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
    // Retrieve the object using Query, starting from base type
    pm = pmf.getPersistenceManager();
    tx = pm.currentTransaction();
    try {
        tx.begin();
        pm.getFetchPlan().addGroup(FetchPlan.ALL);
        Query q = pm.newQuery(baseClass);
        Collection coll = (Collection) q.execute();
        assertTrue("Unable to find a " + baseClass.getName() + " object when one should have been found", coll != null);
        assertTrue("Should have found a single " + baseClass.getName() + " object, but found " + coll.size(), coll.size() == 1);
        Iterator iter = coll.iterator();
        while (iter.hasNext()) {
            Object obj = iter.next();
            Method getNameMethod = subClass.getMethod("getName", new Class[] {});
            String name = (String) getNameMethod.invoke(obj, new Object[] {});
            Method getValueMethod = subClass.getMethod("getValue", new Class[] {});
            Double value = (Double) getValueMethod.invoke(obj, new Object[] {});
            assertTrue(subClass.getName() + " object \"name\" attribute is incorrect : is \"" + name + "\" but should have been \"My Example\"", name.equals("My Example"));
            assertTrue(subClass.getName() + " object \"value\" attribute is incorrect : is " + value.doubleValue() + " but should have been 1234.56", value.doubleValue() == 1234.56);
        }
        tx.commit();
    } catch (Exception e) {
        fail("Exception thrown during retrieval of object of type " + baseClass.getName() + " : " + e.getMessage());
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
    // Retrieve the object using Query, starting from actual object type
    pm = pmf.getPersistenceManager();
    tx = pm.currentTransaction();
    try {
        tx.begin();
        pm.getFetchPlan().addGroup(FetchPlan.ALL);
        Query q = pm.newQuery(subClass, "value == 1234.56");
        Collection coll = (Collection) q.execute();
        assertTrue("Unable to find an " + subClass.getName() + " object when one should have been found", coll != null);
        assertTrue("Should have found a single " + subClass.getName() + " object, but found " + coll.size(), coll.size() == 1);
        Iterator iter = coll.iterator();
        while (iter.hasNext()) {
            Object obj = iter.next();
            Method getNameMethod = subClass.getMethod("getName", new Class[] {});
            String name = (String) getNameMethod.invoke(obj, new Object[] {});
            Method getValueMethod = subClass.getMethod("getValue", new Class[] {});
            Double value = (Double) getValueMethod.invoke(obj, new Object[] {});
            assertTrue(subClass.getName() + " object \"name\" attribute is incorrect : is \"" + name + "\" but should have been \"My Example\"", name.equals("My Example"));
            assertTrue(subClass.getName() + " object \"value\" attribute is incorrect : is " + value.doubleValue() + " but should have been 1234.56", value.doubleValue() == 1234.56);
        }
        tx.commit();
    } catch (Exception e) {
        fail("Exception thrown during retrieval of object of type " + subClass.getName() + " : " + e.getMessage());
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
    // Obtain an Extent of the sub class.
    pm = pmf.getPersistenceManager();
    tx = pm.currentTransaction();
    try {
        tx.begin();
        Extent e = pm.getExtent(subClass, true);
        Iterator iter = e.iterator();
        while (iter.hasNext()) {
            LOG.info("Extent => " + iter.next());
        }
        tx.commit();
    } catch (JDOUserException ue) {
        ue.printStackTrace();
        fail("Exception thrown during creation of Extent for type " + subClass.getName() + " including subclasses : " + ue.getMessage());
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
    // Obtain an Extent of the base class to see if this is possible. Should be possible, but will need to find
    // all sub-classes for the extent and give the results of (potentiall) multiple base tables in the extent.
    pm = pmf.getPersistenceManager();
    tx = pm.currentTransaction();
    try {
        tx.begin();
        Extent e = pm.getExtent(baseClass, true);
        Iterator iter = e.iterator();
        while (iter.hasNext()) {
            LOG.info("Extent => " + iter.next());
        }
        // This should be possible since we have included instances of subclasses so should get some objects
        tx.commit();
    } catch (JDOUserException ue) {
        ue.printStackTrace();
        fail("Exception thrown during creation of Extent for type " + baseClass.getName() + " including subclasses : " + ue.getMessage());
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
// Update the object
// Delete the object
}
Also used : Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) Extent(javax.jdo.Extent) Iterator(java.util.Iterator) Collection(java.util.Collection) Method(java.lang.reflect.Method) JDOUserException(javax.jdo.JDOUserException) JDOUserException(javax.jdo.JDOUserException)

Aggregations

JDOUserException (javax.jdo.JDOUserException)191 PersistenceManager (javax.jdo.PersistenceManager)163 Transaction (javax.jdo.Transaction)161 Query (javax.jdo.Query)94 Iterator (java.util.Iterator)54 Collection (java.util.Collection)45 Employee (org.jpox.samples.models.company.Employee)35 List (java.util.List)33 HashSet (java.util.HashSet)30 ArrayList (java.util.ArrayList)22 Extent (javax.jdo.Extent)22 JDOException (javax.jdo.JDOException)15 JDOPersistenceManager (org.datanucleus.api.jdo.JDOPersistenceManager)15 CollectionHolder (org.jpox.samples.types.container.CollectionHolder)15 Map (java.util.Map)13 MapHolder (org.jpox.samples.types.container.MapHolder)13 JDOFatalUserException (javax.jdo.JDOFatalUserException)12 NucleusException (org.datanucleus.exceptions.NucleusException)10 TransactionActiveOnCloseException (org.datanucleus.exceptions.TransactionActiveOnCloseException)10 Person (org.jpox.samples.models.company.Person)10