Search in sources :

Example 11 with JDOUserException

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

the class SCOCollectionTests method checkAddElement.

/**
 * Utility for checking the addition of an element to a collection by
 * specification at the element end.
 * @param pmf The PersistenceManager factory
 * @param container_class The container class e.g ArrayListNormal
 * @param item_class_parent The parent element class
 */
public static void checkAddElement(PersistenceManagerFactory pmf, Class container_class, Class item_class_parent) throws Exception {
    int NO_OF_ITEMS = 5;
    Object container_id = null;
    // Create the container
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        Object container = null;
        // Create a container and a few items
        try {
            container = container_class.newInstance();
        } catch (Exception e1) {
            LOG.error("Exception in test", e1);
            Assert.fail("Failed to find Container class " + container_class.getName());
        }
        pm.makePersistent(container);
        for (int i = 0; i < NO_OF_ITEMS; i++) {
            // Create an item and set its container
            Object item = SCOHolderUtilities.createItemParent(item_class_parent, "Item " + i, 0.00 + (10.00 * i), i);
            // Add the items to the container
            SCOHolderUtilities.setContainerForItem(item_class_parent, item, container_class, container);
            pm.makePersistent(item);
        }
        container_id = JDOHelper.getObjectId(container);
        tx.commit();
    } catch (JDOUserException e) {
        LOG.error("Exception in test", e);
        Assert.fail("Exception thrown while creating " + container_class.getName() + " " + e.getMessage());
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
    // Find the container and check the items
    pm = pmf.getPersistenceManager();
    tx = pm.currentTransaction();
    try {
        tx.begin();
        CollectionHolder container = (CollectionHolder) pm.getObjectById(container_id, false);
        if (container != null) {
            // Get the no of items in the container
            int container_size = SCOHolderUtilities.getContainerSize(container);
            Assert.assertTrue(container_class.getName() + " has incorrect number of items (" + container_size + ") : should have been " + NO_OF_ITEMS, container_size == NO_OF_ITEMS);
        }
        tx.commit();
    } catch (JDOUserException e2) {
        LOG.error("Exception in test", e2);
        Assert.fail("Exception thrown while manipulating " + container_class.getName() + " " + e2.getMessage());
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) CollectionHolder(org.jpox.samples.types.container.CollectionHolder) JDOUserException(javax.jdo.JDOUserException) JDOUserException(javax.jdo.JDOUserException)

Example 12 with JDOUserException

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

the class SCOCollectionTests method checkGetAt.

/**
 * Utility for checking the retrieval of an item from a position in a
 * Collection. This is specific to List based collections.
 * @param pmf The PersistenceManager factory
 * @param container_class The container class e.g ArrayListNormal
 * @param item_class_parent The parent element class
 */
public static void checkGetAt(PersistenceManagerFactory pmf, Class container_class, Class item_class_parent) throws Exception {
    int NO_OF_ITEMS = 5;
    Object container_id = null;
    // Create some data
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        CollectionHolder container = createAndFillContainer(container_class, item_class_parent, NO_OF_ITEMS);
        pm.makePersistent(container);
        container_id = JDOHelper.getObjectId(container);
        tx.commit();
    } catch (JDOUserException e) {
        LOG.error("Exception in test", e);
        Assert.fail("Exception thrown while creating " + container_class.getName() + " " + e.getMessage());
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
    // Find the container and get an item
    pm = pmf.getPersistenceManager();
    tx = pm.currentTransaction();
    try {
        tx.begin();
        ListHolder container = (ListHolder) pm.getObjectById(container_id, false);
        if (container != null) {
            // Get the no of items in the container
            int container_size = SCOHolderUtilities.getContainerSize(container);
            Assert.assertTrue(container_class.getName() + " has incorrect number of items (" + container_size + ") : should have been " + NO_OF_ITEMS, container_size == NO_OF_ITEMS);
            // Get middle item
            Object obj = SCOHolderUtilities.getItemFromList(container, 2);
            Assert.assertTrue("Requested item could not be retrieved", obj != null);
        }
        tx.commit();
    } catch (JDOUserException e2) {
        LOG.error("Exception in test", e2);
        Assert.fail("Exception thrown while manipulating " + container_class.getName() + " " + e2.getMessage());
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) CollectionHolder(org.jpox.samples.types.container.CollectionHolder) JDOUserException(javax.jdo.JDOUserException) ListHolder(org.jpox.samples.types.container.ListHolder)

Example 13 with JDOUserException

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

the class SCOCollectionTests method checkAddAt.

/**
 * Utility for checking the addition of an element to a position in a
 * Collection. This is specific to List based collections.
 * @param pmf The PersistenceManager factory
 * @param container_class The container class e.g ArrayListNormal
 * @param item_class_parent The parent element class
 */
public static void checkAddAt(PersistenceManagerFactory pmf, Class container_class, Class item_class_parent) throws Exception {
    int NO_OF_ITEMS = 5;
    Object container_id = null;
    // Create the container
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        CollectionHolder container = createAndFillContainer(container_class, item_class_parent, NO_OF_ITEMS);
        pm.makePersistent(container);
        container_id = JDOHelper.getObjectId(container);
        tx.commit();
    } catch (JDOUserException e) {
        LOG.error("Exception in test", e);
        Assert.assertTrue("Exception thrown while creating " + container_class.getName() + " " + e.getMessage(), false);
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
    // Find the container and add an item
    pm = pmf.getPersistenceManager();
    tx = pm.currentTransaction();
    try {
        tx.begin();
        ListHolder container = (ListHolder) pm.getObjectById(container_id, false);
        if (container != null) {
            // Get the no of items in the container
            int container_size = SCOHolderUtilities.getContainerSize(container);
            Assert.assertTrue(container_class.getName() + " has incorrect number of items (" + container_size + ") : should have been " + NO_OF_ITEMS, container_size == NO_OF_ITEMS);
            Object item = SCOHolderUtilities.createItemParent(item_class_parent, "Item 7", 225.00, 7);
            // Add item in middle of list
            SCOHolderUtilities.addItemToCollection(container, item, 2);
        }
        tx.commit();
    } catch (JDOUserException e2) {
        LOG.error("Exception in test", e2);
        Assert.assertTrue("Exception thrown while manipulating " + container_class.getName() + " " + e2.getMessage(), false);
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
    // Find the container and check the new number of items
    pm = pmf.getPersistenceManager();
    tx = pm.currentTransaction();
    try {
        tx.begin();
        CollectionHolder container = (CollectionHolder) pm.getObjectById(container_id, false);
        if (container != null) {
            // Get the no of items in the container
            int container_size = SCOHolderUtilities.getContainerSize(container);
            Assert.assertTrue(container_class.getName() + " has incorrect number of items (" + container_size + ") : should have been " + (NO_OF_ITEMS + 1), container_size == (NO_OF_ITEMS + 1));
        }
        tx.commit();
    } catch (JDOUserException e4) {
        LOG.error("Exception in test", e4);
        Assert.assertTrue("Exception thrown while manipulating " + container_class.getName() + " " + e4.getMessage(), false);
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) CollectionHolder(org.jpox.samples.types.container.CollectionHolder) JDOUserException(javax.jdo.JDOUserException) ListHolder(org.jpox.samples.types.container.ListHolder)

Example 14 with JDOUserException

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

the class SCOCollectionTests method checkRemoveAt.

/**
 * Utility for checking the removal of an element from a position in a
 * Collection. This is specific to List based collections.
 * @param pmf The PersistenceManager factory
 * @param container_class The container class e.g ArrayListNormal
 * @param item_class_parent The parent element class
 */
public static void checkRemoveAt(PersistenceManagerFactory pmf, Class container_class, Class item_class_parent) throws Exception {
    int NO_OF_ITEMS = 5;
    Object container_id = null;
    // Create the container
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        CollectionHolder container = createAndFillContainer(container_class, item_class_parent, NO_OF_ITEMS);
        pm.makePersistent(container);
        container_id = JDOHelper.getObjectId(container);
        tx.commit();
    } catch (JDOUserException e) {
        LOG.error("Exception in test", e);
        Assert.assertTrue("Exception thrown while creating " + container_class.getName() + " " + e.getMessage(), false);
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
    // Find the container and remove an item
    pm = pmf.getPersistenceManager();
    tx = pm.currentTransaction();
    try {
        tx.begin();
        ListHolder container = (ListHolder) pm.getObjectById(container_id, false);
        if (container != null) {
            // Get the no of items in the container
            int container_size = SCOHolderUtilities.getContainerSize(container);
            Assert.assertTrue(container_class.getName() + " has incorrect number of items (" + container_size + ") : should have been " + NO_OF_ITEMS, container_size == NO_OF_ITEMS);
            // Remove middle item
            SCOHolderUtilities.removeItemFromCollection(container, 2);
        }
        tx.commit();
    } catch (JDOUserException e2) {
        LOG.error("Exception in test", e2);
        Assert.assertTrue("Exception thrown while manipulating " + container_class.getName() + " " + e2.getMessage(), false);
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
    // Find the container and check the new number of items
    pm = pmf.getPersistenceManager();
    tx = pm.currentTransaction();
    try {
        tx.begin();
        CollectionHolder container = (CollectionHolder) pm.getObjectById(container_id, false);
        if (container != null) {
            // Get the no of items in the container
            int container_size = SCOHolderUtilities.getContainerSize(container);
            Assert.assertTrue(container_class.getName() + " has incorrect number of items (" + container_size + ") : should have been " + (NO_OF_ITEMS - 1), container_size == (NO_OF_ITEMS - 1));
        }
        tx.commit();
    } catch (JDOUserException e4) {
        LOG.error("Exception in test", e4);
        Assert.assertTrue("Exception thrown while manipulating " + container_class.getName() + " " + e4.getMessage(), false);
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) CollectionHolder(org.jpox.samples.types.container.CollectionHolder) JDOUserException(javax.jdo.JDOUserException) ListHolder(org.jpox.samples.types.container.ListHolder)

Example 15 with JDOUserException

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

the class SCOCollectionTests method checkRetainCollection.

/**
 * Utility for checking the retention of a collection of elements. Calls the
 * retainAll method on a collection container.
 * @param pmf The PersistenceManager factory
 * @param container_class The container class e.g ArrayListNormal
 * @param item_class_parent The parent element class
 */
public static void checkRetainCollection(PersistenceManagerFactory pmf, Class container_class, Class collection_class, Class item_class_parent) throws Exception {
    int NO_OF_ITEMS = 5;
    Object container_id = null;
    // Create the container
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        CollectionHolder container = createAndFillContainer(container_class, item_class_parent, NO_OF_ITEMS);
        pm.makePersistent(container);
        container_id = JDOHelper.getObjectId(container);
        tx.commit();
    } catch (JDOUserException e) {
        LOG.error("Exception in test", e);
        Assert.assertTrue("Exception thrown while creating " + container_class.getName() + " " + e.getMessage(), false);
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
    // Find the container and retain the odd numbered items
    pm = pmf.getPersistenceManager();
    tx = pm.currentTransaction();
    int no_to_retain = 0;
    try {
        tx.begin();
        CollectionHolder container = (CollectionHolder) pm.getObjectById(container_id, false);
        if (container != null) {
            // Get the no of items in the container
            int container_size = SCOHolderUtilities.getContainerSize(container);
            Assert.assertTrue(container_class.getName() + " has incorrect number of items (" + container_size + ") : should have been " + NO_OF_ITEMS, container_size == NO_OF_ITEMS);
            // Select the items to remove
            Iterator item_iter = SCOHolderUtilities.getCollectionItemsIterator(container);
            int i = 0;
            Collection c = new HashSet();
            while (item_iter.hasNext()) {
                Object item = item_iter.next();
                if ((i / 2) * 2 == i) {
                    c.add(item);
                    no_to_retain++;
                }
                i++;
            }
            // Remove the items from the container
            SCOHolderUtilities.retainItemsInCollection(container, c);
        }
        tx.commit();
    } catch (JDOUserException e2) {
        LOG.error("Exception in test", e2);
        Assert.assertTrue("Exception thrown while manipulating " + container_class.getName() + " " + e2.getMessage(), false);
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
    // Find the container and check the items
    pm = pmf.getPersistenceManager();
    tx = pm.currentTransaction();
    try {
        tx.begin();
        CollectionHolder container = (CollectionHolder) pm.getObjectById(container_id, false);
        if (container != null) {
            // Get the no of items in the container
            int container_size = SCOHolderUtilities.getContainerSize(container);
            Assert.assertTrue(container_class.getName() + " has incorrect number of items (" + container_size + ") : should have been " + (no_to_retain), container_size == (no_to_retain));
        }
        tx.commit();
    } catch (JDOUserException e2) {
        LOG.error("Exception in test", e2);
        Assert.assertTrue("Exception thrown while manipulating " + container_class.getName() + " " + e2.getMessage(), false);
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) Iterator(java.util.Iterator) CollectionHolder(org.jpox.samples.types.container.CollectionHolder) Collection(java.util.Collection) JDOUserException(javax.jdo.JDOUserException) HashSet(java.util.HashSet)

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