Search in sources :

Example 16 with JDOUserException

use of javax.jdo.JDOUserException 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)

Example 17 with JDOUserException

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

the class SCOCollectionTests method checkElementInheritance.

/**
 * Utility for checking the use of inherited elements within a collection.
 * @param pmf The PersistenceManager factory
 * @param container_class The container class e.g ArrayListNormal
 * @param collection_class The collection return e.g java.util.ArrayList
 * @param item_class_parent The parent element class
 * @param item_class_child The child element class
 */
public static void checkElementInheritance(PersistenceManagerFactory pmf, Class container_class, Class collection_class, Class item_class_parent, Class item_class_child) throws Exception {
    Object container_id = null;
    // Create the container
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        try {
            // Create a container and a few items
            CollectionHolder container = (CollectionHolder) container_class.newInstance();
            Object item = null;
            item = SCOHolderUtilities.createItemParent(item_class_parent, "Toaster", 12.99, 0);
            SCOHolderUtilities.addItemToCollection(container, item);
            item = SCOHolderUtilities.createItemChild(item_class_child, "Kettle", 15.00, 3, "KETT1");
            SCOHolderUtilities.addItemToCollection(container, item);
            item = SCOHolderUtilities.createItemChild(item_class_child, "Hifi", 99.99, 0, "HIFI");
            SCOHolderUtilities.addItemToCollection(container, item);
            item = SCOHolderUtilities.createItemParent(item_class_parent, "Curtains", 5.99, 1);
            SCOHolderUtilities.addItemToCollection(container, item);
            // Persist the container (and all of the items)
            pm.makePersistent(container);
            container_id = JDOHelper.getObjectId(container);
        } catch (Exception e1) {
            Assert.fail("Failed to find Container class " + container_class.getName());
        }
        tx.commit();
    } catch (JDOUserException e) {
        Assert.assertTrue("Exception thrown while creating " + container_class.getName() + " " + e.getMessage(), false);
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
    // Query 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 4", container_size == 4);
            // Interrogate the items
            Iterator item_iter = SCOHolderUtilities.getCollectionItemsIterator(container);
            int no_parent = 0;
            int no_child = 0;
            while (item_iter.hasNext()) {
                Object item = item_iter.next();
                if (item_class_child.isAssignableFrom(item.getClass())) {
                    no_child++;
                } else if (item_class_parent.isAssignableFrom(item.getClass())) {
                    no_parent++;
                }
            }
            Assert.assertTrue("No of " + item_class_parent.getName() + " is incorrect (" + no_parent + ") : should have been 2", no_parent == 2);
            Assert.assertTrue("No of " + item_class_child.getName() + " is incorrect (" + no_child + ") : should have been 2", no_child == 2);
        }
        tx.commit();
    } catch (JDOUserException e) {
        Assert.assertTrue("Exception thrown while querying " + container_class.getName() + e.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) JDOUserException(javax.jdo.JDOUserException) JDOUserException(javax.jdo.JDOUserException)

Example 18 with JDOUserException

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

the class SCOCollectionTests method checkPersistCollectionByElement.

/**
 * Utility for checking the persistence of a 1-N bidir relation by persisting the element.
 * @param pmf The PersistenceManager factory
 * @param container_class The container class e.g Set1
 * @param item_class_parent The parent element class
 */
public static void checkPersistCollectionByElement(PersistenceManagerFactory pmf, Class container_class, Class item_class_parent) throws Exception {
    int NO_OF_ITEMS = 2;
    Object container_id = null;
    // Create the container
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        CollectionHolder container = null;
        container = createContainer(container_class, container);
        Object itemToPersist = null;
        for (int i = 0; i < NO_OF_ITEMS; i++) {
            Collection c = new HashSet();
            // Create an item
            Object item = SCOHolderUtilities.createItemParent(item_class_parent, "Item " + i, 0.00 + (10.00 * i), i);
            SCOHolderUtilities.setContainerForItem(item_class_parent, item, container_class, container);
            if (i == 0) {
                // Persist from the first of the elements.
                itemToPersist = item;
            }
            // Add the items to the container
            c.add(item);
            SCOHolderUtilities.addItemsToCollection(container, c);
        }
        pm.makePersistent(itemToPersist);
        Assert.assertEquals("Number of elements in container just after persist is wrong", NO_OF_ITEMS, container.getNoOfItems());
        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) Collection(java.util.Collection) JDOUserException(javax.jdo.JDOUserException) HashSet(java.util.HashSet)

Example 19 with JDOUserException

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

the class SCOCollectionTests method checkRemoveCollection.

/**
 * Utility for checking the removal of a collection of elements. Calls the
 * removeAll 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 checkRemoveCollection(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.fail("Exception thrown while creating " + container_class.getName() + " " + e.getMessage());
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
    // Find the container and remove the odd numbered items
    pm = pmf.getPersistenceManager();
    tx = pm.currentTransaction();
    int no_to_remove = 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_remove++;
                }
                i++;
            }
            // Remove the items from the container
            SCOHolderUtilities.removeItemsFromCollection(container, c);
        }
        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();
    }
    // 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 - no_to_remove), container_size == (NO_OF_ITEMS - no_to_remove));
        }
        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) Iterator(java.util.Iterator) CollectionHolder(org.jpox.samples.types.container.CollectionHolder) Collection(java.util.Collection) JDOUserException(javax.jdo.JDOUserException) HashSet(java.util.HashSet)

Example 20 with JDOUserException

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

the class SCOMapTests method checkEntrySet.

/**
 * Utility for checking the use of entrySet. Calls the
 * entrySet method on a map container and checks the contents.
 * @param pmf The PersistenceManager factory
 * @param container_class The container class e.g HashMapNormal
 * @param item_class_parent The parent element class
 * @param key_class The key class
 */
public static void checkEntrySet(PersistenceManagerFactory pmf, Class container_class, Class item_class_parent, Class key_class) throws Exception {
    int NO_OF_ITEMS = 5;
    Object container_id = null;
    java.util.Collection keys = null;
    java.util.Collection values = null;
    java.util.Map entries = null;
    // Create the container
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.setRetainValues(true);
        tx.begin();
        // Create a container
        MapHolder container = null;
        try {
            container = (MapHolder) container_class.newInstance();
        } catch (Exception e1) {
            LOG.error(e1);
            Assert.fail("Failed to find Container class " + container_class.getName());
        }
        // Create a few items and add them to a Map
        java.util.Map m = new java.util.HashMap();
        entries = new java.util.HashMap();
        values = new java.util.HashSet();
        keys = new java.util.HashSet();
        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);
            Object k;
            if (key_class.getName().equals(String.class.getName())) {
                k = new String("Key" + i);
            } else {
                k = SCOHolderUtilities.createItemParent(key_class, "Key " + i, 0.00 + (11.13 * i), i);
            }
            m.put(k, item);
            entries.put(k, item);
            values.add(item);
            keys.add(k);
        }
        // Add the items to the container
        SCOHolderUtilities.addItemsToMap(container, m);
        pm.makePersistent(container);
        container_id = JDOHelper.getObjectId(container);
        tx.commit();
    } catch (JDOUserException e) {
        LOG.error(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();
        MapHolder container = (MapHolder) pm.getObjectById(container_id, false);
        if (container != null) {
            // Get the entry set from the container
            java.util.Set entry_set = SCOHolderUtilities.getEntrySetFromMap(container);
            Assert.assertTrue("EntrySet has incorrect number of items (" + entry_set.size() + ") : should have been " + NO_OF_ITEMS, entry_set.size() == NO_OF_ITEMS);
            Assert.assertTrue("Entries are not the same, result = " + entry_set + " expected = " + entries.entrySet(), SCOHolderUtilities.compareSet(entry_set, entries.entrySet()));
        }
        tx.commit();
    } catch (JDOUserException e2) {
        LOG.error(e2);
        Assert.assertTrue("Exception thrown while manipulating " + container_class.getName() + " " + e2.getMessage(), false);
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
}
Also used : PersistenceManager(javax.jdo.PersistenceManager) MapHolder(org.jpox.samples.types.container.MapHolder) JDOUserException(javax.jdo.JDOUserException) JDOUserException(javax.jdo.JDOUserException) Transaction(javax.jdo.Transaction) Map(java.util.Map)

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