Search in sources :

Example 1 with SequenceGeneratorItem

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

the class ValueGeneratorTest method testSequenceGenerator.

/**
 * Test the use of "sequence" generator. Run if supported by the datastore.
 */
public void testSequenceGenerator() throws Exception {
    if (!storeMgr.supportsValueGenerationStrategy("sequence")) {
        // Lets just say it passed :-)
        return;
    }
    try {
        HashSet idSet = new HashSet();
        Class idClass = null;
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            // Create a few objects.
            SequenceGeneratorItem item = null;
            item = new SequenceGeneratorItem("First item");
            pm.makePersistent(item);
            pm.flush();
            idSet.add(new Integer(item.getIdentifier()));
            item = new SequenceGeneratorItem("Second item");
            pm.makePersistent(item);
            pm.flush();
            idSet.add(new Integer(item.getIdentifier()));
            item = new SequenceGeneratorItem("Third item");
            pm.makePersistent(item);
            pm.flush();
            idSet.add(new Integer(item.getIdentifier()));
            item = new SequenceGeneratorItem("Fourth item");
            pm.makePersistent(item);
            pm.flush();
            idSet.add(new Integer(item.getIdentifier()));
            idClass = JDOHelper.getObjectId(item).getClass();
            tx.commit();
        } catch (JDOUserException e) {
            fail("Exception thrown during insert of objects in \"sequence\" generator test " + e.getMessage());
        } 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(SequenceGeneratorItem.class, true));
            Collection c = (Collection) q.execute();
            // Check on the number of items
            assertTrue("Number of SequenceGeneratorItem's retrieved is incorrect (" + c.size() + ") : should have been 4", c.size() == 4);
            Iterator iter = c.iterator();
            while (iter.hasNext()) {
                Object o = iter.next();
                if (SequenceGeneratorItem.Oid.class.equals(idClass)) {
                    idSetCopy.remove(new Integer(((SequenceGeneratorItem) o).getIdentifier()));
                }
            }
            tx.commit();
            if (SequenceGeneratorItem.Oid.class.equals(idClass)) {
                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 out any created data
        clean(SequenceGeneratorItem.class);
    }
}
Also used : Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) Iterator(java.util.Iterator) SequenceGeneratorItem(org.jpox.samples.valuegeneration.SequenceGeneratorItem) 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 SequenceGeneratorItem (org.jpox.samples.valuegeneration.SequenceGeneratorItem)1