Search in sources :

Example 1 with TableGeneratorItem

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

the class ValueGeneratorTest method testTableGenerator.

/**
 * Test the use of "table" generator. This is for all datastores.
 */
public void testTableGenerator() throws Exception {
    try {
        HashSet idSet = new HashSet();
        Class idClass = null;
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            // Create a few objects.
            TableGeneratorItem item = null;
            item = new TableGeneratorItem("First item");
            pm.makePersistent(item);
            idSet.add(new Integer(item.getIdentifier()));
            item = new TableGeneratorItem("Second item");
            pm.makePersistent(item);
            idSet.add(new Integer(item.getIdentifier()));
            item = new TableGeneratorItem("Third item");
            pm.makePersistent(item);
            idSet.add(new Integer(item.getIdentifier()));
            item = new TableGeneratorItem("Fourth item");
            pm.makePersistent(item);
            idSet.add(new Integer(item.getIdentifier()));
            idClass = JDOHelper.getObjectId(item).getClass();
            tx.commit();
        } catch (JDOUserException e) {
            fail("Exception thrown during insert of objects in \"table\" 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(TableGeneratorItem.class, true));
            Collection c = (Collection) q.execute();
            // Check on the number of items
            assertEquals("Number of TableGeneratorItem's retrieved is incorrect", 4, c.size());
            Iterator iter = c.iterator();
            while (iter.hasNext()) {
                Object o = iter.next();
                if (TableGeneratorItem.Oid.class.equals(idClass)) {
                    idSetCopy.remove(new Integer(((TableGeneratorItem) o).getIdentifier()));
                }
            }
            tx.commit();
            if (TableGeneratorItem.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(TableGeneratorItem.class);
    }
}
Also used : Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) TableGeneratorItem(org.jpox.samples.valuegeneration.TableGeneratorItem) Iterator(java.util.Iterator) Collection(java.util.Collection) JDOUserException(javax.jdo.JDOUserException) HashSet(java.util.HashSet)

Example 2 with TableGeneratorItem

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

the class ValueGeneratorTest method testTableGenerator.

/**
 * Test the use of "increment"/"table" generator
 */
public void testTableGenerator() throws Exception {
    try {
        HashSet idSet = new HashSet();
        Class idClass = null;
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            // Create a few objects.
            TableGeneratorItem item = null;
            item = new TableGeneratorItem("First item");
            pm.makePersistent(item);
            idSet.add(new Integer(item.getIdentifier()));
            item = new TableGeneratorItem("Second item");
            pm.makePersistent(item);
            idSet.add(new Integer(item.getIdentifier()));
            item = new TableGeneratorItem("Third item");
            pm.makePersistent(item);
            idSet.add(new Integer(item.getIdentifier()));
            item = new TableGeneratorItem("Fourth item");
            pm.makePersistent(item);
            idSet.add(new Integer(item.getIdentifier()));
            idClass = JDOHelper.getObjectId(item).getClass();
            tx.commit();
        } catch (JDOUserException e) {
            fail("Exception thrown during insert of objects in \"table\" 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(TableGeneratorItem.class, true));
            Collection c = (Collection) q.execute();
            // Check on the number of items
            assertEquals("Number of TableGeneratorItem's retrieved is incorrect", 4, c.size());
            Iterator iter = c.iterator();
            while (iter.hasNext()) {
                Object o = iter.next();
                if (TableGeneratorItem.Oid.class.equals(idClass)) {
                    idSetCopy.remove(new Integer(((TableGeneratorItem) o).getIdentifier()));
                }
            }
            tx.commit();
            if (TableGeneratorItem.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(TableGeneratorItem.class);
    }
}
Also used : Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) TableGeneratorItem(org.jpox.samples.valuegeneration.TableGeneratorItem) Iterator(java.util.Iterator) Collection(java.util.Collection) JDOUserException(javax.jdo.JDOUserException) HashSet(java.util.HashSet)

Example 3 with TableGeneratorItem

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

the class ValueGeneratorTest method testIncrementStrategy.

/**
 * Test the use of "increment" generator for a loop of persists.
 */
public void testIncrementStrategy() throws Exception {
    try {
        HashSet idSet = new HashSet();
        Class idClass = null;
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            // Create a few objects.
            for (int i = 0; i < 100; i++) {
                TableGeneratorItem item = new TableGeneratorItem("Item " + i);
                pm.makePersistent(item);
                idSet.add(new Integer(item.getIdentifier()));
                idClass = JDOHelper.getObjectId(item).getClass();
            }
            tx.commit();
        } catch (JDOUserException e) {
            fail("Exception thrown during insert of objects in \"increment\" generator test " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            Collection idSetCopy = new HashSet(idSet);
            // Retrieve the items
            Query q = pm.newQuery(TableGeneratorItem.class);
            Collection c = (Collection) q.execute();
            // Check on the number of items
            assertEquals("Number of TableGeneratorItem's retrieved is incorrect", 100, c.size());
            Iterator iter = c.iterator();
            while (iter.hasNext()) {
                Object o = iter.next();
                if (TableGeneratorItem.Oid.class.equals(idClass)) {
                    idSetCopy.remove(new Integer(((TableGeneratorItem) o).getIdentifier()));
                }
            }
            tx.commit();
            if (TableGeneratorItem.Oid.class.equals(idClass)) {
                assertEquals("Wrong number of different IDs", 100, 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(TableGeneratorItem.class);
    }
}
Also used : Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) TableGeneratorItem(org.jpox.samples.valuegeneration.TableGeneratorItem) Iterator(java.util.Iterator) Collection(java.util.Collection) JDOUserException(javax.jdo.JDOUserException) HashSet(java.util.HashSet)

Aggregations

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