Search in sources :

Example 1 with MapFKValueItem

use of org.jpox.samples.one_many.map.MapFKValueItem in project tests by datanucleus.

the class MapForeignKeyTest method testMapWithKeyAsFieldInValue.

/**
 * Test for persistence of a Map with the key stored in the value object.
 */
public void testMapWithKeyAsFieldInValue() {
    try {
        Object containerId = null;
        MapHolder container = new MapHolder();
        MapFKValueItem item1 = new MapFKValueItem("First", "First element", "Item1");
        MapFKValueItem item3 = new MapFKValueItem("Third", "Third element", "Item3");
        MapFKValueItem item2 = new MapFKValueItem("Second", "Second element", "Item2");
        container.getFkMapKey().put(item1.getKey(), item1);
        container.getFkMapKey().put(item3.getKey(), item3);
        container.getFkMapKey().put(item2.getKey(), item2);
        // Persist the objects
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            pm.makePersistent(container);
            tx.commit();
            containerId = JDOHelper.getObjectId(container);
        } catch (Exception e) {
            e.printStackTrace();
            fail(e.toString());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // Retrieve the object and inspect it
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            container = (MapHolder) pm.getObjectById(containerId);
            assertTrue("Container map was not found!", container != null);
            Map map = container.getFkMapKey();
            assertEquals("Number of items in the container map is incorrect", map.size(), 3);
            // Check Entry set
            Set entries = map.entrySet();
            Iterator entryIter = entries.iterator();
            while (entryIter.hasNext()) {
                Map.Entry entry = (Map.Entry) entryIter.next();
                MapFKValueItem item = (MapFKValueItem) entry.getValue();
                if (entry.getKey().equals("Item1")) {
                    assertEquals("item has incorrect name for key Item1", item.getName(), "First");
                } else if (entry.getKey().equals("Item2")) {
                    assertEquals("item has incorrect name for key Item2", item.getName(), "Second");
                } else if (entry.getKey().equals("Item3")) {
                    assertEquals("item has incorrect name for key Item3", item.getName(), "Third");
                } else {
                    fail("Unknown Map entry found with key " + entry.getKey());
                }
            }
            // Check Key set
            Set keys = map.keySet();
            assertEquals("Number of keys in Map.keySet() is incorrect", keys.size(), 3);
            Iterator keyIter = keys.iterator();
            boolean item1Present = false;
            boolean item2Present = false;
            boolean item3Present = false;
            while (keyIter.hasNext()) {
                Object obj = keyIter.next();
                assertEquals("Type of value objects returned from Map.keySet().iterator() is incorrect", obj.getClass().getName(), String.class.getName());
                String key = (String) obj;
                if (key.equals("Item1")) {
                    item1Present = true;
                } else if (key.equals("Item2")) {
                    item2Present = true;
                } else if (key.equals("Item3")) {
                    item3Present = true;
                }
            }
            assertTrue("Item1 was not present in the keySet", item1Present);
            assertTrue("Item2 was not present in the keySet", item2Present);
            assertTrue("Item3 was not present in the keySet", item3Present);
            // Check Value set
            Collection values = map.values();
            assertEquals("Number of values in Map.values() is incorrect", values.size(), 3);
            Iterator valueIter = values.iterator();
            item1Present = false;
            item2Present = false;
            item3Present = false;
            while (valueIter.hasNext()) {
                Object obj = valueIter.next();
                assertEquals("Type of value objects returned from Map.values().iterator() is incorrect", obj.getClass().getName(), MapFKValueItem.class.getName());
                MapFKValueItem value = (MapFKValueItem) obj;
                if (value.getName().equals("First")) {
                    item1Present = true;
                } else if (value.getName().equals("Second")) {
                    item2Present = true;
                } else if (value.getName().equals("Third")) {
                    item3Present = true;
                }
            }
            assertTrue("Item1 was not present in the values()", item1Present);
            assertTrue("Item2 was not present in the values()", item2Present);
            assertTrue("Item3 was not present in the values()", item3Present);
            tx.commit();
        } catch (Exception e) {
            e.printStackTrace();
            fail(e.toString());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        // Clean out our data
        clean(MapHolder.class);
    }
}
Also used : Set(java.util.Set) MapHolder(org.jpox.samples.one_many.map.MapHolder) PersistenceManager(javax.jdo.PersistenceManager) MapFKValueItem(org.jpox.samples.one_many.map.MapFKValueItem) Transaction(javax.jdo.Transaction) Iterator(java.util.Iterator) Collection(java.util.Collection) Map(java.util.Map)

Example 2 with MapFKValueItem

use of org.jpox.samples.one_many.map.MapFKValueItem in project tests by datanucleus.

the class RelationshipTest method test1toNUnidirFKMap.

/**
 * Test case for 1-N unidir FK, using a Map<String, PC>.
 */
public void test1toNUnidirFKMap() throws Exception {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        Object holderId = null;
        // Check the persistence of owner and elements
        try {
            tx.begin();
            // Create some data
            MapHolder holder = new MapHolder("First");
            MapFKValueItem value1 = new MapFKValueItem("Value 1", "First value", "1");
            MapFKValueItem value2 = new MapFKValueItem("Value 2", "Second value", "2");
            holder.getFkMapKey().put(value1.getKey(), value1);
            holder.getFkMapKey().put(value2.getKey(), value2);
            pm.makePersistent(holder);
            tx.commit();
            holderId = pm.getObjectId(holder);
        } catch (Exception e) {
            LOG.error("Exception in test", e);
            fail("Exception thrown while creating 1-N unidir FK relationships (Map) : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
        }
        // Check the retrieval of the owner and values
        tx = pm.currentTransaction();
        try {
            tx.begin();
            MapHolder holder = (MapHolder) pm.getObjectById(holderId);
            assertNotNull("Unable to retrieve container object for 1-N unidir FK relationship (Map)", holder);
            Collection values = holder.getFkMapKey().values();
            Iterator iter = values.iterator();
            while (iter.hasNext()) {
                iter.next();
            }
            assertEquals("Number of values in 1-N unidir FK relationship (Map) is wrong", 2, values.size());
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception in test", e);
            fail("Exception thrown while querying 1-N unidir FK relationships (Map) : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
        }
        pm.close();
    } finally {
        // Clean out our data
        clean(MapHolder.class);
        clean(MapFKValueItem.class);
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) MapHolder(org.jpox.samples.one_many.map.MapHolder) Iterator(java.util.Iterator) Collection(java.util.Collection) MapFKValueItem(org.jpox.samples.one_many.map.MapFKValueItem) JDOObjectNotFoundException(javax.jdo.JDOObjectNotFoundException)

Aggregations

Collection (java.util.Collection)2 Iterator (java.util.Iterator)2 PersistenceManager (javax.jdo.PersistenceManager)2 Transaction (javax.jdo.Transaction)2 MapFKValueItem (org.jpox.samples.one_many.map.MapFKValueItem)2 MapHolder (org.jpox.samples.one_many.map.MapHolder)2 Map (java.util.Map)1 Set (java.util.Set)1 JDOObjectNotFoundException (javax.jdo.JDOObjectNotFoundException)1