Search in sources :

Example 6 with MapHolder

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

the class SCOMapTests method checkQuery.

/**
 * Utility for checking the use of queries with JDOQL on a Map.
 * Uses containsKey(),containsValue(),containsEntry() methods.
 * @param pmf The PersistenceManager factory
 * @param container_class The container class e.g HashMapNormal
 * @param item_class_parent The parent element class
 * @param db_vendor_id Id of datastore e.g mysql (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
        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();
        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);
            m.put(new String("Key" + (i + 1)), item);
        }
        // Add the items to the container
        SCOHolderUtilities.addItemsToMap(container, m);
        pm.makePersistent(container);
        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();
    }
    // Query the Map
    pm = pmf.getPersistenceManager();
    tx = pm.currentTransaction();
    try {
        tx.begin();
        // Get all Maps that contain the key "Key1" - should return 1
        Extent e1 = pm.getExtent(container_class, true);
        Query q1 = pm.newQuery(e1, "items.containsKey(\"Key1\")");
        java.util.Collection c1 = (java.util.Collection) q1.execute();
        Assert.assertTrue("containsKey : No of containers with a key \"Key1\" is incorrect (" + c1.size() + ") : should have been 1", c1.size() == 1);
        // Get all Maps that contain the key "Key7" - should return 0
        Extent e2 = pm.getExtent(container_class, true);
        Query q2 = pm.newQuery(e2, "items.containsKey(\"Key7\")");
        java.util.Collection c2 = (java.util.Collection) q2.execute();
        Assert.assertTrue("containsKey : No of containers with a key \"Key7\" is incorrect (" + c2.size() + ") : should have been 0", c2.size() == 0);
        // TODO : remove the MySQL omittal when it supports subqueries
        if (db_vendor_id == null || !db_vendor_id.equals("mysql")) {
            Extent e3 = pm.getExtent(container_class, true);
            Query q3 = pm.newQuery(e3, "items.isEmpty()");
            java.util.Collection c3 = (java.util.Collection) q3.execute();
            Assert.assertTrue("No of containers that are empty is incorrect (" + c3.size() + ") : should have been 0", c3.size() == 0);
        }
        // Get all Maps containing a particular value
        Extent e4 = pm.getExtent(container_class, true);
        Query q4 = pm.newQuery(e4, "items.containsValue(the_value) && the_value.name==\"Item 1\"");
        q4.declareImports("import " + item_class_parent.getName());
        q4.declareVariables(item_class_parent.getName() + " the_value");
        java.util.Collection c4 = (java.util.Collection) q4.execute();
        Assert.assertTrue("containsValue : No of containers with the specified value is incorrect (" + c4.size() + ") : should have been 1", c4.size() == 1);
        // Get all Maps containing a particular value
        Extent e5 = pm.getExtent(container_class, true);
        Query q5 = pm.newQuery(e5, "items.containsEntry(\"Key1\",the_value) && the_value.name==\"Item 0\"");
        q5.declareImports("import " + item_class_parent.getName());
        q5.declareVariables(item_class_parent.getName() + " the_value");
        java.util.Collection c5 = (java.util.Collection) q5.execute();
        Assert.assertTrue("containsEntry : No of containers with the specified value is incorrect (" + c5.size() + ") : should have been 1", c5.size() == 1);
        tx.commit();
    } catch (JDOUserException e) {
        LOG.error(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) MapHolder(org.jpox.samples.types.container.MapHolder) Extent(javax.jdo.Extent) JDOUserException(javax.jdo.JDOUserException) JDOUserException(javax.jdo.JDOUserException) Transaction(javax.jdo.Transaction) Map(java.util.Map)

Example 7 with MapHolder

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

the class SCOMapTests method checkQueryPrimitive.

/**
 * Utility for checking the use of queries with JDOQL on a Map with
 * primitive key/value objects. This is separate from the checkQuery
 * because with primitive (String) value a different table is created.
 * Uses containsKey(),containsValue(),containsEntry() methods.
 * @param pmf The PersistenceManager factory
 * @param container_class The container class e.g HashMapNormal
 * @param db_vendor_id Id of datastore e.g mysql (TODO : remove this)
 */
public static void checkQueryPrimitive(PersistenceManagerFactory pmf, Class container_class, 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
        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();
        for (int i = 0; i < NO_OF_ITEMS; i++) {
            m.put(new String("Key" + (i + 1)), new String("Value " + (i + 1)));
        }
        // Add the items to the container
        SCOHolderUtilities.addItemsToMap(container, m);
        pm.makePersistent(container);
        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();
    }
    // Query the Map
    pm = pmf.getPersistenceManager();
    tx = pm.currentTransaction();
    try {
        tx.begin();
        // Get all Maps that contain the key "Key1" - should return 1
        Extent e1 = pm.getExtent(container_class, true);
        Query q1 = pm.newQuery(e1, "items.containsKey(\"Key1\")");
        java.util.Collection c1 = (java.util.Collection) q1.execute();
        Assert.assertTrue("containsKey : No of containers with a key \"Key1\" is incorrect (" + c1.size() + ") : should have been 1", c1.size() == 1);
        // Get all Maps that contain the key "Key7" - should return 0
        Extent e2 = pm.getExtent(container_class, true);
        Query q2 = pm.newQuery(e2, "items.containsKey(\"Key7\")");
        java.util.Collection c2 = (java.util.Collection) q2.execute();
        Assert.assertTrue("containsKey : No of containers with a key \"Key7\" is incorrect (" + c2.size() + ") : should have been 0", c2.size() == 0);
        // TODO : remove the MySQL omittal when it supports subqueries
        if (db_vendor_id == null || !db_vendor_id.equals("mysql")) {
            Extent e3 = pm.getExtent(container_class, true);
            Query q3 = pm.newQuery(e3, "items.isEmpty()");
            java.util.Collection c3 = (java.util.Collection) q3.execute();
            Assert.assertTrue("No of containers that are empty is incorrect (" + c3.size() + ") : should have been 0", c3.size() == 0);
        }
        // Get all Maps containing a particular value
        Extent e4 = pm.getExtent(container_class, true);
        Query q4 = pm.newQuery(e4, "items.containsValue(the_value) && the_value==\"Value 1\"");
        q4.declareImports("import java.lang.String");
        q4.declareVariables("java.lang.String the_value");
        java.util.Collection c4 = (java.util.Collection) q4.execute();
        Assert.assertTrue("containsValue : No of containers with the specified value is incorrect (" + c4.size() + ") : should have been 1", c4.size() == 1);
        // Get all Maps containing a particular entry
        Extent e5 = pm.getExtent(container_class, true);
        Query q5 = pm.newQuery(e5, "items.containsEntry(\"Key1\",the_value) && the_value==\"Value 1\"");
        q5.declareImports("import java.lang.String");
        q5.declareVariables("java.lang.String the_value");
        java.util.Collection c5 = (java.util.Collection) q5.execute();
        Assert.assertTrue("containsEntry : No of containers with the specified value is incorrect (" + c5.size() + ") : should have been 1", c5.size() == 1);
        tx.commit();
    } catch (JDOUserException e) {
        LOG.error(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) MapHolder(org.jpox.samples.types.container.MapHolder) Extent(javax.jdo.Extent) JDOUserException(javax.jdo.JDOUserException) JDOUserException(javax.jdo.JDOUserException) Transaction(javax.jdo.Transaction) Map(java.util.Map)

Example 8 with MapHolder

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

the class SCOMapTests method checkClearMap.

/**
 * Utility for checking the clearing out of a Map. Calls the
 * clear method on a Map container.
 * @param pmf The PersistenceManager factory
 * @param container_class The container class e.g HashMapNormal
 * @param item_class_parent The parent element class
 */
public static void checkClearMap(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();
        // Create a container and a few items
        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());
        }
        // Add a few items to the container
        for (int i = 0; i < NO_OF_ITEMS; i++) {
            Object item = SCOHolderUtilities.createItemParent(item_class_parent, "Item " + i, 0.00 + (10.00 * i), i);
            SCOHolderUtilities.addItemToMap(container, new String("Key" + (i + 1)), item);
        }
        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 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(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();
        MapHolder container = (MapHolder) 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(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) MapHolder(org.jpox.samples.types.container.MapHolder) JDOUserException(javax.jdo.JDOUserException) JDOUserException(javax.jdo.JDOUserException)

Example 9 with MapHolder

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

the class SCOMapTests method checkPutNullValues.

/**
 * Utility for checking the addition of a Map of elements. Calls the
 * putAll method on a map container.
 * @param pmf The PersistenceManager factory
 * @param container_class The container class e.g HashMapNormal
 * @param item_class_parent The parent element class
 */
public static void checkPutNullValues(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();
        // 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();
        for (int i = 0; i < NO_OF_ITEMS; i++) {
            m.put(new String("Key" + (i + 1)), null);
        }
        // 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 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);
            Map items = container.getItems();
            Iterator iter = items.entrySet().iterator();
            while (iter.hasNext()) {
                Map.Entry entry = (Entry) iter.next();
                Assert.assertNull(entry.getValue());
                Assert.assertNotNull(entry.getKey());
            }
        }
        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 : Entry(java.util.Map.Entry) PersistenceManager(javax.jdo.PersistenceManager) MapHolder(org.jpox.samples.types.container.MapHolder) JDOUserException(javax.jdo.JDOUserException) JDOUserException(javax.jdo.JDOUserException) Entry(java.util.Map.Entry) Transaction(javax.jdo.Transaction) Map(java.util.Map) Iterator(java.util.Iterator) Map(java.util.Map)

Example 10 with MapHolder

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

the class SCOMapTests method checkPutItems.

/**
 * Utility for checking the addition of a Map of elements. Calls the
 * putAll method on a map container.
 * @param pmf The PersistenceManager factory
 * @param container_class The container class e.g HashMapNormal
 * @param item_class_parent The parent element class
 */
public static void checkPutItems(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();
        // 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();
        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);
            m.put(new String("Key" + (i + 1)), item);
        }
        // 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 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(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) MapHolder(org.jpox.samples.types.container.MapHolder) Map(java.util.Map) JDOUserException(javax.jdo.JDOUserException) JDOUserException(javax.jdo.JDOUserException)

Aggregations

JDOUserException (javax.jdo.JDOUserException)13 PersistenceManager (javax.jdo.PersistenceManager)13 Transaction (javax.jdo.Transaction)13 MapHolder (org.jpox.samples.types.container.MapHolder)13 Map (java.util.Map)10 Extent (javax.jdo.Extent)3 Query (javax.jdo.Query)3 Iterator (java.util.Iterator)1 Entry (java.util.Map.Entry)1