Search in sources :

Example 11 with CollectionHolder

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

the class SCOCollectionTests method checkQuery.

/**
 * Utility for checking the use of queries with JDOQL on a container.
 * Uses contains(), isEmpty() operations.
 * @param pmf The PersistenceManager factory
 * @param container_class The container class e.g HashSetNormal
 * @param item_class_parent The parent element class
 * @param db_vendor_id Name of RDBMS. (TODO Remove this)
 */
public static void checkQuery(PersistenceManagerFactory pmf, Class container_class, Class item_class_parent, String db_vendor_id) throws Exception {
    int NO_OF_ITEMS = 5;
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        // Create a container
        CollectionHolder container = null;
        container = createContainer(container_class, container);
        // Create a few items and add them to a Collection
        java.util.Collection c = new java.util.HashSet();
        for (int i = 0; i < NO_OF_ITEMS; i++) {
            Object item = SCOHolderUtilities.createItemParent(item_class_parent, "Item " + i, 0.00 + (10.00 * i), i);
            c.add(item);
        }
        // Add the items to the container
        SCOHolderUtilities.addItemsToCollection(container, c);
        pm.makePersistent(container);
        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();
    }
    // Query the containers
    pm = pmf.getPersistenceManager();
    tx = pm.currentTransaction();
    try {
        tx.begin();
        // TODO : Remove this restriction when MySQL supports subqueries
        if (db_vendor_id == null || !db_vendor_id.equals("mysql")) {
            Extent e1 = pm.getExtent(container_class, true);
            Query q1 = pm.newQuery(e1, "items.isEmpty()");
            java.util.Collection c1 = (java.util.Collection) q1.execute();
            Assert.assertTrue("No of containers that are empty is incorrect (" + c1.size() + ") : should have been 0", c1.size() == 0);
        }
        // Get all containers containing a particular value
        Extent e2 = pm.getExtent(container_class, true);
        Query q2 = pm.newQuery(e2, "items.contains(element) && element.name==\"Item 1\"");
        q2.declareImports("import " + item_class_parent.getName());
        q2.declareVariables(item_class_parent.getName() + " element");
        java.util.Collection c2 = (java.util.Collection) q2.execute();
        Assert.assertTrue("No of containers with the specified value is incorrect (" + c2.size() + ") : should have been 1", c2.size() == 1);
        tx.commit();
    } catch (JDOUserException e) {
        e.printStackTrace();
        Assert.assertTrue("Exception thrown while querying container objects " + e.getMessage(), false);
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
    // Query the containers for the container containing an element.
    pm = pmf.getPersistenceManager();
    tx = pm.currentTransaction();
    try {
        tx.begin();
        // Get all elements
        Extent e = pm.getExtent(item_class_parent, true);
        Iterator e_iter = e.iterator();
        while (e_iter.hasNext()) {
            Object obj = e_iter.next();
            Query q = pm.newQuery(pm.getExtent(container_class, true), "items.contains(element)");
            q.declareImports("import " + item_class_parent.getName());
            q.declareParameters(item_class_parent.getName() + " element");
            java.util.Collection c = (java.util.Collection) q.execute(obj);
            Assert.assertTrue("No of containers with the specified element is incorrect (" + c.size() + ") : should have been 1", c.size() == 1);
        }
        tx.commit();
    } catch (JDOUserException e) {
        e.printStackTrace();
        Assert.assertTrue("Exception thrown while querying container objects " + e.getMessage(), false);
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
}
Also used : Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) Extent(javax.jdo.Extent) Collection(java.util.Collection) CollectionHolder(org.jpox.samples.types.container.CollectionHolder) JDOUserException(javax.jdo.JDOUserException) Transaction(javax.jdo.Transaction) Iterator(java.util.Iterator) Collection(java.util.Collection) HashSet(java.util.HashSet)

Example 12 with CollectionHolder

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

the class SCOCollectionTests method checkAttachDetach.

/**
 * Utility for checking the use of detach, then attach.
 * @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 checkAttachDetach(PersistenceManagerFactory pmf, Class container_class, Class item_class_parent, Class collection_class) throws Exception {
    int NO_OF_ITEMS = 5;
    Object container_id = null;
    CollectionHolder detachedContainer = null;
    // Create a container and some elements
    PersistenceManager pm = pmf.getPersistenceManager();
    pm.getFetchPlan().addGroup("items");
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        CollectionHolder container = null;
        container = createContainer(container_class, container);
        for (int i = 0; i < NO_OF_ITEMS; i++) {
            Collection c = new HashSet();
            Object item = SCOHolderUtilities.createItemParent(item_class_parent, "Item " + i, 0.00 + (10.00 * i), i);
            c.add(item);
            SCOHolderUtilities.addItemsToCollection(container, c);
        }
        pm.makePersistent(container);
        container_id = JDOHelper.getObjectId(container);
        detachedContainer = (CollectionHolder) pm.detachCopy(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));
        container_size = SCOHolderUtilities.getContainerSize(detachedContainer);
        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 e) {
        LOG.error("Exception in test", e);
        Assert.assertTrue("Exception thrown while creating and detaching " + container_class.getName() + " " + e.getMessage(), false);
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
    pmf.getDataStoreCache().evictAll();
    // Update the container
    Object item = SCOHolderUtilities.createItemParent(item_class_parent, "Item 10", 100, 10);
    SCOHolderUtilities.addItemToCollection(detachedContainer, item);
    int container_size = SCOHolderUtilities.getContainerSize(detachedContainer);
    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));
    // Attach the updated container
    pm = pmf.getPersistenceManager();
    pm.getFetchPlan().addGroup("items");
    tx = pm.currentTransaction();
    try {
        tx.begin();
        CollectionHolder attachedContainer = (CollectionHolder) pm.makePersistent(detachedContainer);
        // Add another item to the container
        Object newItem = SCOHolderUtilities.createItemParent(item_class_parent, "Item 20", 200, 20);
        SCOHolderUtilities.addItemToCollection(attachedContainer, newItem);
        tx.commit();
    } catch (JDOUserException e2) {
        LOG.error("Exception in test", e2);
        Assert.fail("Exception thrown while retrieving " + container_class.getName() + " : " + e2.getMessage());
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
    // Find the container and check the items
    pm = pmf.getPersistenceManager();
    pm.getFetchPlan().addGroup("items");
    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
            container_size = SCOHolderUtilities.getContainerSize(container);
            Assert.assertTrue(container_class.getName() + " has incorrect number of items (" + container_size + ") : should have been " + (NO_OF_ITEMS + 2), container_size == (NO_OF_ITEMS + 2));
        }
        detachedContainer = (CollectionHolder) pm.detachCopy(container);
        tx.commit();
    } catch (JDOUserException e2) {
        LOG.error("Exception in test", e2);
        Assert.fail("Exception thrown while retrieving " + container_class.getName() + " " + e2.getMessage());
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
    // Remove an item from the container
    container_size = SCOHolderUtilities.getContainerSize(detachedContainer);
    Assert.assertTrue("Detached container " + container_class.getName() + " has incorrect number of items (" + container_size + ") : should have been " + (NO_OF_ITEMS + 2), container_size == (NO_OF_ITEMS + 2));
    Iterator containerIter = SCOHolderUtilities.getCollectionItemsIterator(detachedContainer);
    item = containerIter.next();
    SCOHolderUtilities.removeItemFromCollection(detachedContainer, item);
    container_size = SCOHolderUtilities.getContainerSize(detachedContainer);
    Assert.assertTrue("Detached container " + container_class.getName() + " (after delete) has incorrect number of items (" + container_size + ") : should have been " + (NO_OF_ITEMS + 1), container_size == (NO_OF_ITEMS + 1));
    // Attach the updated container
    pm = pmf.getPersistenceManager();
    pm.getFetchPlan().addGroup("items");
    tx = pm.currentTransaction();
    try {
        tx.begin();
        pm.makePersistent(detachedContainer);
        tx.commit();
    } catch (JDOUserException e2) {
        LOG.error("Exception in test", e2);
        Assert.fail("Exception thrown while retrieving " + container_class.getName() + " " + e2.getMessage());
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
    // Find the container and check the items
    pm = pmf.getPersistenceManager();
    pm.getFetchPlan().addGroup("items");
    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
            container_size = SCOHolderUtilities.getContainerSize(container);
            Assert.assertTrue("Attached container " + 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 e2) {
        LOG.error("Exception in test", e2);
        Assert.fail("Exception thrown while retrieving " + container_class.getName() + " " + e2.getMessage());
    } 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)

Example 13 with CollectionHolder

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

the class SCOCollectionTests method checkClearCollection.

/**
 * Utility for checking the clearing out of a collection. Calls the
 * clear 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 checkClearCollection(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 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);
            // Clear out the container
            SCOHolderUtilities.clearItemsFromContainer(container);
        }
        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) {
            boolean is_empty = SCOHolderUtilities.isContainerEmpty(container);
            Assert.assertTrue(container_class.getName() + " is not empty, yet should be since clear() was called.", is_empty);
        }
        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) JDOUserException(javax.jdo.JDOUserException)

Example 14 with CollectionHolder

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

the class SCOCollectionTests method checkContains.

/**
 * Utility for checking the contains
 * @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 checkContains(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();
    Object detachedItem = null;
    try {
        tx.begin();
        CollectionHolder container = createAndFillContainer(container_class, item_class_parent, NO_OF_ITEMS);
        Object item = SCOHolderUtilities.createItemParent(item_class_parent, "Item " + NO_OF_ITEMS + 1, 0.00 + (10.00 * (NO_OF_ITEMS + 1)), NO_OF_ITEMS + 1);
        pm.makePersistent(item);
        pm.flush();
        detachedItem = pm.detachCopy(item);
        SCOHolderUtilities.addItemToCollection(container, detachedItem);
        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();
        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);
            Assert.assertTrue("Requested item could not be retrieved", container.contains(detachedItem));
        }
        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)

Example 15 with CollectionHolder

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

the class SCOCollectionTests method createAndFillContainer.

private static CollectionHolder createAndFillContainer(Class container_class, Class item_class_parent, int NO_OF_ITEMS) {
    CollectionHolder container = null;
    // Create a container and a few items
    container = createContainer(container_class, container);
    for (int i = 0; i < NO_OF_ITEMS; i++) {
        // Create an item
        Object item = SCOHolderUtilities.createItemParent(item_class_parent, "Item " + i, 0.00 + (10.00 * i), i);
        // Add the item to the container
        SCOHolderUtilities.addItemToCollection(container, item);
    }
    return container;
}
Also used : CollectionHolder(org.jpox.samples.types.container.CollectionHolder)

Aggregations

CollectionHolder (org.jpox.samples.types.container.CollectionHolder)16 JDOUserException (javax.jdo.JDOUserException)15 PersistenceManager (javax.jdo.PersistenceManager)15 Transaction (javax.jdo.Transaction)15 Collection (java.util.Collection)6 HashSet (java.util.HashSet)6 Iterator (java.util.Iterator)5 ListHolder (org.jpox.samples.types.container.ListHolder)4 List (java.util.List)1 Extent (javax.jdo.Extent)1 Query (javax.jdo.Query)1