Search in sources :

Example 1 with IdentityStringGeneratorItem

use of org.jpox.samples.valuegeneration.IdentityStringGeneratorItem in project tests by datanucleus.

the class ValueGeneratorTest method testIdentityGenerator.

/**
 * Test the use of "identity" generator.
 */
public void testIdentityGenerator() throws Exception {
    try {
        HashSet idSet = new HashSet();
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            // Create a few objects.
            for (int i = 0; i < 4; i++) {
                IdentityStringGeneratorItem item = new IdentityStringGeneratorItem("First item");
                pm.makePersistent(item);
                pm.flush();
                idSet.add(JDOHelper.getObjectId(item));
            }
            tx.commit();
            assertEquals("Number of unique ids persisted is wrong", 4, idSet.size());
        } catch (JDOUserException e) {
            fail("Exception thrown during insert of objects with \"identity\" generator : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            Iterator idIter = idSet.iterator();
            while (idIter.hasNext()) {
                Object id = idIter.next();
                IdentityStringGeneratorItem obj = (IdentityStringGeneratorItem) pm.getObjectById(id);
                assertEquals("Incorrect name", "First item", obj.getName());
            }
            tx.commit();
        } catch (JDOUserException ue) {
            assertTrue("Exception thrown during test " + ue.getMessage(), false);
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            HashSet idSetCopy = new HashSet(idSet);
            // Retrieve the items
            Query q = pm.newQuery(pm.getExtent(IdentityStringGeneratorItem.class, true));
            Collection c = (Collection) q.execute();
            // Check on the number of items
            assertTrue("Number of IdentityStringGeneratorItem's retrieved is incorrect (" + c.size() + ") : should have been 4", c.size() == 4);
            Iterator<IdentityStringGeneratorItem> iter = c.iterator();
            while (iter.hasNext()) {
                IdentityStringGeneratorItem item = iter.next();
                Object id = JDOHelper.getObjectId(item);
                if (idSetCopy.contains(id)) {
                    idSetCopy.remove(id);
                }
            }
            tx.commit();
            assertEquals("Wrong number of different IDs", 4, idSet.size());
            assertTrue("Retrieved IDs did not match created IDs", 0 == idSetCopy.size());
        } catch (JDOUserException ue) {
            assertTrue("Exception thrown during test " + ue.getMessage(), false);
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        clean(IdentityStringGeneratorItem.class);
    }
}
Also used : Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) Iterator(java.util.Iterator) IdentityStringGeneratorItem(org.jpox.samples.valuegeneration.IdentityStringGeneratorItem) Collection(java.util.Collection) JDOUserException(javax.jdo.JDOUserException) HashSet(java.util.HashSet)

Aggregations

Collection (java.util.Collection)1 HashSet (java.util.HashSet)1 Iterator (java.util.Iterator)1 JDOUserException (javax.jdo.JDOUserException)1 PersistenceManager (javax.jdo.PersistenceManager)1 Query (javax.jdo.Query)1 Transaction (javax.jdo.Transaction)1 IdentityStringGeneratorItem (org.jpox.samples.valuegeneration.IdentityStringGeneratorItem)1