Search in sources :

Example 1 with ListHolder

use of org.jpox.samples.types.container.ListHolder 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 2 with ListHolder

use of org.jpox.samples.types.container.ListHolder 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 3 with ListHolder

use of org.jpox.samples.types.container.ListHolder 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 4 with ListHolder

use of org.jpox.samples.types.container.ListHolder in project tests by datanucleus.

the class SCOCollectionTests method checkEquals.

public static void checkEquals(PersistenceManagerFactory pmf, Class container_class, Class item_class_parent) {
    int NO_OF_ITEMS = 5;
    Object container1_id = null;
    Object container2_id = null;
    // Create two equal containers
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        CollectionHolder container1 = createAndFillContainer(container_class, item_class_parent, NO_OF_ITEMS);
        CollectionHolder container2 = null;
        container2 = createContainer(container_class, container2);
        container2.addItems(container1.getItems());
        // check before persisting
        Assert.assertEquals(container1.getItems(), container2.getItems());
        pm.makePersistent(container1);
        pm.makePersistent(container2);
        // check after persisting inside tx
        Assert.assertEquals(container1.getItems(), container2.getItems());
        container1_id = JDOHelper.getObjectId(container1);
        container2_id = JDOHelper.getObjectId(container2);
        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();
    }
    // Retrieve the two containers, check equality again, change order in one of them
    pm = pmf.getPersistenceManager();
    tx = pm.currentTransaction();
    try {
        tx.begin();
        ListHolder container1 = (ListHolder) pm.getObjectById(container1_id, false);
        Assert.assertNotNull(container1);
        ListHolder container2 = (ListHolder) pm.getObjectById(container2_id, false);
        Assert.assertNotNull(container2);
        // must still be equal after retrieval
        Assert.assertEquals(container1.getItems(), container2.getItems());
        // exchange first and last item in first container
        Object firstItem = container1.getItem(0);
        Object lastItem = container1.getItem(NO_OF_ITEMS - 1);
        ((List) container1.getItems()).set(0, lastItem);
        ((List) container1.getItems()).set(NO_OF_ITEMS - 1, firstItem);
        Assert.assertFalse(container1.getItems().equals(container2.getItems()));
        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) CollectionHolder(org.jpox.samples.types.container.CollectionHolder) List(java.util.List) JDOUserException(javax.jdo.JDOUserException) ListHolder(org.jpox.samples.types.container.ListHolder)

Aggregations

JDOUserException (javax.jdo.JDOUserException)4 PersistenceManager (javax.jdo.PersistenceManager)4 Transaction (javax.jdo.Transaction)4 CollectionHolder (org.jpox.samples.types.container.CollectionHolder)4 ListHolder (org.jpox.samples.types.container.ListHolder)4 List (java.util.List)1