Search in sources :

Example 6 with JDOUserException

use of javax.jdo.JDOUserException in project tests by datanucleus.

the class ValueGeneratorTest method xxxtestUUIDStringGenerator.

/**
 * Test the use of "uuid-string" generator. This is for all datastores.
 */
public void xxxtestUUIDStringGenerator() 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.
            UUIDStringGeneratorItem item = null;
            item = new UUIDStringGeneratorItem("First item");
            pm.makePersistent(item);
            idSet.add(item.getIdentifier());
            item = new UUIDStringGeneratorItem("Second item");
            pm.makePersistent(item);
            idSet.add(item.getIdentifier());
            item = new UUIDStringGeneratorItem("Third item");
            pm.makePersistent(item);
            idSet.add(item.getIdentifier());
            item = new UUIDStringGeneratorItem("Fourth item");
            pm.makePersistent(item);
            idSet.add(item.getIdentifier());
            idClass = JDOHelper.getObjectId(item).getClass();
            tx.commit();
        } catch (JDOUserException e) {
            fail("Exception thrown during insert of objects in \"uuid-string\" 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(UUIDStringGeneratorItem.class, true));
            Collection c = (Collection) q.execute();
            // Check on the number of items
            assertTrue("Number of UUIDStringGeneratorItem'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 (UUIDStringGeneratorItem.Oid.class.equals(idClass)) {
                    idSetCopy.remove(((UUIDStringGeneratorItem) o).getIdentifier());
                }
            }
            tx.commit();
            if (UUIDStringGeneratorItem.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(UUIDStringGeneratorItem.class);
    }
}
Also used : UUIDStringGeneratorItem(org.jpox.samples.valuegeneration.UUIDStringGeneratorItem) Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) Iterator(java.util.Iterator) Collection(java.util.Collection) JDOUserException(javax.jdo.JDOUserException) HashSet(java.util.HashSet)

Example 7 with JDOUserException

use of javax.jdo.JDOUserException 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)

Example 8 with JDOUserException

use of javax.jdo.JDOUserException in project tests by datanucleus.

the class ValueGeneratorTest method testAUIDGenerator.

/**
 * Test the use of "auid" generator. This is for all datastores.
 */
public void testAUIDGenerator() 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.
            AUIDGeneratorItem item = null;
            item = new AUIDGeneratorItem("First item");
            pm.makePersistent(item);
            idSet.add(item.getIdentifier());
            item = new AUIDGeneratorItem("Second item");
            pm.makePersistent(item);
            idSet.add(item.getIdentifier());
            item = new AUIDGeneratorItem("Third item");
            pm.makePersistent(item);
            idSet.add(item.getIdentifier());
            item = new AUIDGeneratorItem("Fourth item");
            pm.makePersistent(item);
            idSet.add(item.getIdentifier());
            idClass = JDOHelper.getObjectId(item).getClass();
            tx.commit();
        } catch (JDOUserException e) {
            fail("Exception thrown during insert of objects in \"auid\" 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(AUIDGeneratorItem.class, true));
            Collection c = (Collection) q.execute();
            // Check on the number of items
            assertTrue("Number of AUIDGeneratorItem'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 (AUIDGeneratorItem.Oid.class.equals(idClass)) {
                    idSetCopy.remove(((AUIDGeneratorItem) o).getIdentifier());
                }
            }
            tx.commit();
            if (AUIDGeneratorItem.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(AUIDGeneratorItem.class);
    }
}
Also used : AUIDGeneratorItem(org.jpox.samples.valuegeneration.AUIDGeneratorItem) Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) Iterator(java.util.Iterator) Collection(java.util.Collection) JDOUserException(javax.jdo.JDOUserException) HashSet(java.util.HashSet)

Example 9 with JDOUserException

use of javax.jdo.JDOUserException 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)

Example 10 with JDOUserException

use of javax.jdo.JDOUserException in project tests by datanucleus.

the class ValueGeneratorTest method testMixedGeneratorInherited.

/**
 * Test the use of multiple "value-strategy" fields with inheritance
 */
public void testMixedGeneratorInherited() throws Exception {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            // Create a few objects.
            MixedGeneratorItemSub item = null;
            item = new MixedGeneratorItemSub();
            pm.makePersistent(item);
            item = new MixedGeneratorItemSub();
            pm.makePersistent(item);
            item = new MixedGeneratorItemSub();
            pm.makePersistent(item);
            item = new MixedGeneratorItemSub();
            pm.makePersistent(item);
            tx.commit();
            JDOHelper.getObjectId(item).getClass();
        } catch (JDOUserException e) {
            fail("Exception thrown during insert of objects in mixed generators test " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            // Retrieve the items
            Query q = pm.newQuery(pm.getExtent(MixedGeneratorItemSub.class, true));
            Collection c = (Collection) q.execute();
            // Check on the number of items
            assertTrue("Number of MixedGeneratorItemSub's retrieved is incorrect (" + c.size() + ") : should have been 4", c.size() == 4);
            Iterator iter = c.iterator();
            while (iter.hasNext()) {
                iter.next();
            }
            tx.commit();
        } catch (JDOUserException ue) {
            assertTrue("Exception thrown during test " + ue.getMessage(), false);
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        // Clean out any data we have created
        clean(MixedGeneratorItemSub.class);
    }
}
Also used : MixedGeneratorItemSub(org.jpox.samples.valuegeneration.MixedGeneratorItemSub) Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) Iterator(java.util.Iterator) Collection(java.util.Collection) JDOUserException(javax.jdo.JDOUserException)

Aggregations

JDOUserException (javax.jdo.JDOUserException)191 PersistenceManager (javax.jdo.PersistenceManager)163 Transaction (javax.jdo.Transaction)161 Query (javax.jdo.Query)94 Iterator (java.util.Iterator)54 Collection (java.util.Collection)45 Employee (org.jpox.samples.models.company.Employee)35 List (java.util.List)33 HashSet (java.util.HashSet)30 ArrayList (java.util.ArrayList)22 Extent (javax.jdo.Extent)22 JDOException (javax.jdo.JDOException)15 JDOPersistenceManager (org.datanucleus.api.jdo.JDOPersistenceManager)15 CollectionHolder (org.jpox.samples.types.container.CollectionHolder)15 Map (java.util.Map)13 MapHolder (org.jpox.samples.types.container.MapHolder)13 JDOFatalUserException (javax.jdo.JDOFatalUserException)12 NucleusException (org.datanucleus.exceptions.NucleusException)10 TransactionActiveOnCloseException (org.datanucleus.exceptions.TransactionActiveOnCloseException)10 Person (org.jpox.samples.models.company.Person)10