Search in sources :

Example 1 with MapHolder

use of org.jpox.samples.one_many.map.MapHolder 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 MapHolder

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

the class SerializationTest method testSerialisedMapValues.

/**
 * Test for serialisation of map values.
 */
public void testSerialisedMapValues() {
    if (!storeMgr.getSupportedOptions().contains(StoreManager.OPTION_ORM_SERIALISED_MAP_VALUE)) {
        return;
    }
    try {
        Object holderId = null;
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        // Persist the object with serialised fields
        try {
            tx.begin();
            MapHolder holder = new MapHolder("Holder(4)");
            MapValueItem val = new MapValueItem("Value 1", "Value 1 Desc");
            holder.getJoinMapNonPCSerial().put("1", val);
            pm.makePersistent(holder);
            tx.commit();
            holderId = pm.getObjectId(holder);
        } catch (Exception e) {
            LOG.error(">> Exception thrown in test", e);
            fail("Exception thrown while persisted object with serialised map values field : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // Retrieve the object
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            MapHolder holder = (MapHolder) pm.getObjectById(holderId);
            assertTrue("Holder of serialised map values could not be retrieved!", holder != null);
            assertTrue("Holder name is incorrect", holder.getName().equals("Holder(4)"));
            assertEquals("Number of serialised values is incorrect", holder.getJoinMapNonPCSerial().size(), 1);
            MapValueItem val = (MapValueItem) holder.getJoinMapNonPCSerial().values().iterator().next();
            assertEquals("Serialised map value has incorrect description", val.getName(), "Value 1");
            // Add 2 new values and remove original
            holder.getJoinMapNonPCSerial().clear();
            holder.getJoinMapNonPCSerial().put("2", new MapValueItem("Value 2", "Value 2 desc"));
            holder.getJoinMapNonPCSerial().put("3", new MapValueItem("Value 3", "Value 3 desc"));
            holder.setName("Holder(5)");
            tx.commit();
        } catch (Exception e) {
            LOG.error(">> Exception thrown in test", e);
            fail("Exception thrown while retrieving object with serialised map values : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // Retrieve the object again to check the most recent update
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            MapHolder holder = (MapHolder) pm.getObjectById(holderId);
            assertTrue("Holder of serialised map values could not be retrieved!", holder != null);
            assertTrue("Holder name is incorrect", holder.getName().equals("Holder(5)"));
            assertEquals("Number of serialised values is incorrect", holder.getJoinMapNonPCSerial().size(), 2);
            Iterator valuesIter = holder.getJoinMapNonPCSerial().values().iterator();
            boolean containsVal2 = false;
            boolean containsVal3 = false;
            while (valuesIter.hasNext()) {
                MapValueItem val = (MapValueItem) valuesIter.next();
                if (val.getName().equals("Value 2")) {
                    containsVal2 = true;
                } else if (val.getName().equals("Value 3")) {
                    containsVal3 = true;
                }
            }
            assertTrue("Element 2 is missing from map with serialised values", containsVal2);
            assertTrue("Element 3 is missing from map with serialised values", containsVal3);
            tx.commit();
        } catch (Exception e) {
            LOG.error(">> Exception thrown in test", e);
            fail("Exception thrown while retrieving object with serialised map values field : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        // Clean up our data
        clean(MapHolder.class);
        clean(MapValueItem.class);
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) MapHolder(org.jpox.samples.one_many.map.MapHolder) Iterator(java.util.Iterator) MapValueItem(org.jpox.samples.one_many.map.MapValueItem) SerialisedObject(org.datanucleus.samples.serialised.SerialisedObject) IOException(java.io.IOException)

Example 3 with MapHolder

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

the class JDOQLBasicTest method testMultipleActiveQueryMapGet.

/**
 * Test for multithreaded queries for Map.get().
 */
public void testMultipleActiveQueryMapGet() {
    int THREAD_SIZE = 40;
    Thread[] threads = new Thread[THREAD_SIZE];
    MultithreadRunnerMapGet[] runner = new MultithreadRunnerMapGet[THREAD_SIZE];
    PersistenceManager pm = pmf.getPersistenceManager();
    try {
        Object idHolder1 = null;
        Object idValue1 = null;
        Object idValue2 = null;
        Object idValue3 = null;
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            MapHolder holder1 = new MapHolder("First");
            MapHolder holder2 = new MapHolder("Second");
            MapValueItem item1 = new MapValueItem("Item 1", "First Item");
            MapValueItem item2 = new MapValueItem("Item 2", "Second Item");
            MapValueItem item3 = new MapValueItem("Item 3", "Third Item");
            holder1.getJoinMapNonPC().put(item1.getName(), item1);
            holder1.getJoinMapNonPC().put(item2.getName(), item2);
            holder1.getJoinMapNonPC().put(item3.getName(), item3);
            holder2.getJoinMapNonPC().put(item3.getName(), item3);
            pm.makePersistent(holder1);
            pm.makePersistent(holder2);
            tx.commit();
            idHolder1 = JDOHelper.getObjectId(holder1);
            idValue1 = JDOHelper.getObjectId(item1);
            idValue2 = JDOHelper.getObjectId(item2);
            idValue3 = JDOHelper.getObjectId(item3);
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        try {
            for (int i = 0; i < THREAD_SIZE; i++) {
                runner[i] = new MultithreadRunnerMapGet(idHolder1, idValue1, idValue2, idValue3);
                threads[i] = new Thread(runner[i]);
                threads[i].start();
            }
            for (int i = 0; i < THREAD_SIZE; i++) {
                threads[i].join();
                if (runner[i].exception != null) {
                    LOG.error("Exception in query", runner[i].exception);
                    fail("Map.get thread runner failed; consult the log for details : " + runner[i].exception.getMessage());
                }
            }
        } catch (RuntimeException e) {
            fail(e.getMessage());
        } catch (InterruptedException e) {
            fail(e.getMessage());
        }
    } finally {
        clean(MapHolder.class);
        clean(MapValueItem.class);
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) MapHolder(org.jpox.samples.one_many.map.MapHolder) MapValueItem(org.jpox.samples.one_many.map.MapValueItem)

Example 4 with MapHolder

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

the class JDOQLContainerTest method testMapGetInOrderBy.

/**
 * Test for Map.get() in the ORDER BY clause.
 */
public void testMapGetInOrderBy() {
    try {
        // Create some data
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            MapHolder holder1 = new MapHolder("First Holder");
            holder1.getJoinMapNonNon().put("Order", "First");
            pm.makePersistent(holder1);
            MapHolder holder2 = new MapHolder("Second Holder");
            holder2.getJoinMapNonNon().put("Order", "Second");
            pm.makePersistent(holder2);
            MapHolder holder3 = new MapHolder("Third Holder");
            holder3.getJoinMapNonNon().put("Order", "Third");
            pm.makePersistent(holder3);
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception thrown during test", e);
            fail("Exception thrown during test : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // Query the data
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            // ORDER BY Map.get()
            Query q = pm.newQuery("SELECT FROM " + MapHolder.class.getName() + " ORDER BY joinMapNonNon.get(\"order\")");
            List results = (List) q.execute();
            LOG.info("results=" + StringUtils.collectionToString(results));
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception thrown during test", e);
            fail("Exception thrown during test : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        clean(MapHolder.class);
    }
}
Also used : Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) MapHolder(org.jpox.samples.one_many.map.MapHolder) List(java.util.List) JDOUserException(javax.jdo.JDOUserException)

Example 5 with MapHolder

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

the class MapForeignKeyTest method testMapWithValueAsFieldInKey.

/**
 * Test for persistence of a Map with the value stored in the key object.
 */
public void testMapWithValueAsFieldInKey() {
    try {
        Object containerId = null;
        MapHolder container = new MapHolder();
        MapFKKeyItem item1 = new MapFKKeyItem("First", "First element", "Item1");
        MapFKKeyItem item3 = new MapFKKeyItem("Third", "Third element", "Item3");
        MapFKKeyItem item2 = new MapFKKeyItem("Second", "Second element", "Item2");
        container.getFkMapValue().put(item1, item1.getValue());
        container.getFkMapValue().put(item3, item3.getValue());
        container.getFkMapValue().put(item2, item2.getValue());
        // 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.getFkMapValue();
            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();
                MapFKKeyItem item = (MapFKKeyItem) entry.getKey();
                if (entry.getValue().equals("Item1")) {
                    assertEquals("item has incorrect name for value Item1", item.getName(), "First");
                } else if (entry.getValue().equals("Item2")) {
                    assertEquals("item has incorrect name for value Item2", item.getName(), "Second");
                } else if (entry.getValue().equals("Item3")) {
                    assertEquals("item has incorrect name for value Item3", item.getName(), "Third");
                } else {
                    fail("Unknown Map entry found with value " + entry.getValue());
                }
            }
            // 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 key objects returned from Map.keySet().iterator() is incorrect", obj.getClass().getName(), MapFKKeyItem.class.getName());
                MapFKKeyItem key = (MapFKKeyItem) obj;
                if (key.getName().equals("First")) {
                    item1Present = true;
                } else if (key.getName().equals("Second")) {
                    item2Present = true;
                } else if (key.getName().equals("Third")) {
                    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(), String.class.getName());
                String value = (String) obj;
                if (value.equals("Item1")) {
                    item1Present = true;
                } else if (value.equals("Item2")) {
                    item2Present = true;
                } else if (value.equals("Item3")) {
                    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) MapFKKeyItem(org.jpox.samples.one_many.map.MapFKKeyItem) Transaction(javax.jdo.Transaction) Iterator(java.util.Iterator) Collection(java.util.Collection) Map(java.util.Map)

Aggregations

PersistenceManager (javax.jdo.PersistenceManager)10 Transaction (javax.jdo.Transaction)10 MapHolder (org.jpox.samples.one_many.map.MapHolder)10 Iterator (java.util.Iterator)5 Collection (java.util.Collection)4 List (java.util.List)3 MapValueItem (org.jpox.samples.one_many.map.MapValueItem)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Set (java.util.Set)2 JDOObjectNotFoundException (javax.jdo.JDOObjectNotFoundException)2 JDOUserException (javax.jdo.JDOUserException)2 ClassLoaderResolver (org.datanucleus.ClassLoaderResolver)2 JDOQuery (org.datanucleus.api.jdo.JDOQuery)2 JDOQLCompiler (org.datanucleus.query.compiler.JDOQLCompiler)2 JavaQueryCompiler (org.datanucleus.query.compiler.JavaQueryCompiler)2 QueryCompilation (org.datanucleus.query.compiler.QueryCompilation)2 JDOQLInMemoryEvaluator (org.datanucleus.query.inmemory.JDOQLInMemoryEvaluator)2 JavaQueryInMemoryEvaluator (org.datanucleus.query.inmemory.JavaQueryInMemoryEvaluator)2