Search in sources :

Example 1 with JDOUserException

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

the class SQLQueryTest method testNullQuery.

/**
 * Test of a null query.
 */
public void testNullQuery() throws Exception {
    // Try a null query
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        Query query = pm.newQuery("javax.jdo.query.SQL", null);
        query.execute();
        fail("Should have thrown an exception on a null query, but allowed execution");
    } catch (JDOUserException ue) {
    // Do nothing, since this is expected
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
}
Also used : Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) JDOUserException(javax.jdo.JDOUserException)

Example 2 with JDOUserException

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

the class SQLQueryTest method testQueryWithTimeout.

/**
 * Test of a query with a timeout.
 */
public void testQueryWithTimeout() throws Exception {
    // Try a query
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        // TODO Change this to a query that will take a LONG time and check for it
        String sqlText = "SELECT count(*) FROM PERSON";
        Query query = pm.newQuery("javax.jdo.query.SQL", sqlText);
        query.addExtension("org.jpox.query.timeout", "1");
        query.execute();
        tx.commit();
    } catch (JDODataStoreException dse) {
        fail("JDODataStoreException thrown when using query timeout : " + dse.getCause().getMessage());
    } catch (JDOUserException ue) {
    // Do nothing, since this is expected
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
}
Also used : Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) JDOUserException(javax.jdo.JDOUserException) JDODataStoreException(javax.jdo.JDODataStoreException)

Example 3 with JDOUserException

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

the class SQLQueryTest method testQueryFromJdoqueryFile.

/**
 * Test of a query specified in a jdo query file.
 */
public void testQueryFromJdoqueryFile() throws Exception {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        // Create some inherited data to query and query using a ResultClass without constructor
        tx = pm.currentTransaction();
        tx.begin();
        Developer p3 = new Developer(13, "James", "Java", "james@java.com", (float) 15.00, "1234569", new Integer(3), "jdo");
        pm.makePersistent(p3);
        tx.commit();
        // Run the query (specified in a jdoquery file)
        tx.begin();
        Query query = pm.newNamedQuery(null, "DeveloperSkills");
        List results = (List) query.execute("jdo");
        Iterator resultsIter = results.iterator();
        while (resultsIter.hasNext()) {
            Object skill = resultsIter.next();
            LOG.debug(">> Skill : " + skill);
        }
    } catch (JDOUserException ue) {
        ue.printStackTrace();
        LOG.error(ue);
        fail("Exception thrown while persisting object and performing query : " + ue.getMessage());
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
        clean(Developer.class);
    }
}
Also used : BigInteger(java.math.BigInteger) Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) Iterator(java.util.Iterator) Developer(org.jpox.samples.models.company.Developer) List(java.util.List) JDOUserException(javax.jdo.JDOUserException)

Example 4 with JDOUserException

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

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

the class ValueGeneratorTest method testAppIdentityGenerator.

/**
 * Test the use of "identity" generator for application-identity.
 */
public void testAppIdentityGenerator() 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++) {
                AppIdStringGeneratorItem item = new AppIdStringGeneratorItem("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();
                AppIdStringGeneratorItem obj = (AppIdStringGeneratorItem) 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(AppIdStringGeneratorItem.class, true));
            Collection c = (Collection) q.execute();
            // Check on the number of items
            assertTrue("Number of AppIdStringGeneratorItems retrieved is incorrect (" + c.size() + ") : should have been 4", c.size() == 4);
            Iterator<AppIdStringGeneratorItem> iter = c.iterator();
            while (iter.hasNext()) {
                AppIdStringGeneratorItem 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(AppIdStringGeneratorItem.class);
    }
}
Also used : Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) AppIdStringGeneratorItem(org.datanucleus.tests.applicationid.AppIdStringGeneratorItem) Iterator(java.util.Iterator) Collection(java.util.Collection) JDOUserException(javax.jdo.JDOUserException) HashSet(java.util.HashSet)

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