Search in sources :

Example 26 with JDOException

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

the class JDOQLBasicTest method testJDOHelperGetObjectID1.

/**
 * Tests the JDOHelper.getObjectId() expression
 */
public void testJDOHelperGetObjectID1() {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        // Persist some other objects
        tx.begin();
        BasicTypeHolder[] basics = new BasicTypeHolder[5];
        for (int i = 0; i < basics.length; i++) {
            basics[i] = new BasicTypeHolder();
            basics[i].setLongField((i + 2) * (i + 2));
            basics[i].setCharField('0');
        }
        pm.makePersistentAll(basics);
        tx.commit();
        Object[] ids = new Object[5];
        for (int i = 0; i < basics.length; i++) {
            ids[i] = pm.getObjectId(basics[i]);
        }
        tx.begin();
        Query q = pm.newQuery(BasicTypeHolder.class, "JDOHelper.getObjectId(this) == oid");
        q.declareParameters("Object oid");
        Collection c = null;
        try {
            // Invalid object identity
            c = (Collection) q.execute("nononononnon");
            // In case the query compilation just resolves it to false
            Assert.assertEquals(0, c.size());
        } catch (JDOException jdoe) {
        // Arguable that we should get this with invalid input
        }
        for (int i = 0; i < basics.length; i++) {
            q = pm.newQuery(BasicTypeHolder.class, "JDOHelper.getObjectId(this) == oid");
            q.declareParameters("Object oid");
            c = (Collection) q.execute(ids[i]);
            Assert.assertEquals(1, c.size());
            Assert.assertEquals(ids[i], JDOHelper.getObjectId(c.iterator().next()));
            q = pm.newQuery(BasicTypeHolder.class, "oid == JDOHelper.getObjectId(this)");
            q.declareParameters("Object oid");
            c = (Collection) q.execute(ids[i]);
            Assert.assertEquals(1, c.size());
            Assert.assertEquals(ids[i], JDOHelper.getObjectId(c.iterator().next()));
            q = pm.newQuery(BasicTypeHolder.class, "JDOHelper.getObjectId(this) == JDOHelper.getObjectId(obj)");
            q.declareParameters("Object obj");
            // test that query can be compiled
            q.compile();
            c = (Collection) q.execute(basics[i]);
            Assert.assertEquals(1, c.size());
            Assert.assertEquals(ids[i], JDOHelper.getObjectId(c.iterator().next()));
            q = pm.newQuery(BasicTypeHolder.class, "JDOHelper.getObjectId(this) == JDOHelper.getObjectId(obj)");
            q.declareParameters("BasicTypeHolder obj");
            // test that query can be compiled
            q.compile();
            c = (Collection) q.execute(basics[i]);
            Assert.assertEquals(1, c.size());
            Assert.assertEquals(ids[i], JDOHelper.getObjectId(c.iterator().next()));
        }
        tx.commit();
    } catch (Exception e) {
        LOG.error("Error performing test", e);
        fail("Exception performing test : " + e.getMessage());
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
        // Clean out our data
        clean(BasicTypeHolder.class);
    }
}
Also used : Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) Collection(java.util.Collection) BasicTypeHolder(org.jpox.samples.types.basic.BasicTypeHolder) JDOException(javax.jdo.JDOException) JDOUserException(javax.jdo.JDOUserException) NoSuchElementException(java.util.NoSuchElementException) JDOException(javax.jdo.JDOException)

Example 27 with JDOException

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

the class JDOQLResultTest method testNewObjectInResult.

/**
 * Test case to use the JDO 2.0 setResultClass().
 */
public void testNewObjectInResult() {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            Person p1 = new Person(101, "Fred", "Flintstone", "fred.flintstone@jpox.com");
            p1.setAge(34);
            Person p2 = new Person(102, "Barney", "Rubble", "barney.rubble@jpox.com");
            p2.setAge(37);
            pm.makePersistent(p1);
            pm.makePersistent(p2);
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
        }
        // Query using PersonHolder result output
        try {
            tx.begin();
            // check with default constructor
            Query q = pm.newQuery(pm.getExtent(Person.class, false));
            q.setFilter("age > 35");
            q.setResult("new org.jpox.samples.models.company.PersonHolder()");
            List results = (List) q.execute();
            assertEquals(1, results.size());
            Iterator resultsIter = results.iterator();
            if (resultsIter.hasNext()) {
                Object obj = resultsIter.next();
                assertTrue("ResultClass of query is incorrect. Should have been " + PersonHolder.class.getName() + " but is " + obj.getClass().getName(), PersonHolder.class == obj.getClass());
                PersonHolder holder = (PersonHolder) obj;
                assertNull(holder.getPerson1());
            }
            q.closeAll();
            // check with simple argument
            q = pm.newQuery(pm.getExtent(Person.class, false));
            q.setFilter("age > 35");
            q.setResult("new org.jpox.samples.models.company.PersonHolder(this)");
            results = (List) q.execute();
            assertEquals(1, results.size());
            resultsIter = results.iterator();
            if (resultsIter.hasNext()) {
                Object obj = resultsIter.next();
                assertTrue("ResultClass of query is incorrect. Should have been " + PersonHolder.class.getName() + " but is " + obj.getClass().getName(), PersonHolder.class == obj.getClass());
                PersonHolder holder = (PersonHolder) obj;
                assertNotNull(holder.getPerson1());
                assertNull(holder.getPerson2());
            }
            q.closeAll();
            // check multiple args and with a literal null
            q = pm.newQuery(pm.getExtent(Person.class, false));
            q.setFilter("age > 35");
            q.setResult("new org.jpox.samples.models.company.PersonHolder(this,null,this.firstName)");
            results = (List) q.execute();
            assertEquals(1, results.size());
            resultsIter = results.iterator();
            if (resultsIter.hasNext()) {
                Object obj = resultsIter.next();
                assertTrue("ResultClass of query is incorrect. Should have been " + PersonHolder.class.getName() + " but is " + obj.getClass().getName(), PersonHolder.class == obj.getClass());
                PersonHolder holder = (PersonHolder) obj;
                assertNotNull(holder.getPerson1());
                assertNull(holder.getPerson2());
                assertEquals("Barney", holder.getFirstName().trim());
            }
            q.closeAll();
            // add more literals
            q = pm.newQuery(pm.getExtent(Person.class, false));
            q.setFilter("age > 35");
            q.setResult("new org.jpox.samples.models.company.PersonHolder(this,null,this.firstName,'string',1)");
            results = (List) q.execute();
            assertEquals(1, results.size());
            resultsIter = results.iterator();
            if (resultsIter.hasNext()) {
                Object obj = resultsIter.next();
                assertTrue("ResultClass of query is incorrect. Should have been " + PersonHolder.class.getName() + " but is " + obj.getClass().getName(), PersonHolder.class == obj.getClass());
                PersonHolder holder = (PersonHolder) obj;
                assertNotNull(holder.getPerson1());
                assertNull(holder.getPerson2());
                assertEquals("Barney", holder.getFirstName().trim());
                assertEquals("string", holder.getLiteralString());
                assertEquals(1, holder.getLiteralInt());
            }
            q.closeAll();
            // make sure if objects after literal, they work
            q = pm.newQuery(pm.getExtent(Person.class, false));
            q.setFilter("age > 35");
            q.setResult("new org.jpox.samples.models.company.PersonHolder(this,null,this.firstName,'string',1,this)");
            results = (List) q.execute();
            assertEquals(1, results.size());
            resultsIter = results.iterator();
            if (resultsIter.hasNext()) {
                Object obj = resultsIter.next();
                assertTrue("ResultClass of query is incorrect. Should have been " + PersonHolder.class.getName() + " but is " + obj.getClass().getName(), PersonHolder.class == obj.getClass());
                PersonHolder holder = (PersonHolder) obj;
                assertNotNull(holder.getPerson1());
                assertNull(holder.getPerson2());
                assertEquals("Barney", holder.getFirstName().trim());
                assertEquals("string", holder.getLiteralString());
                assertEquals(1, holder.getLiteralInt());
            }
            q.closeAll();
            // test new Object(new Object())
            q = pm.newQuery(pm.getExtent(Person.class, false));
            q.setFilter("age > 35");
            q.setResult("new org.jpox.samples.models.company.PersonHolder(new org.jpox.samples.models.company.PersonHolder(new org.jpox.samples.models.company.PersonHolder(this),this),this)");
            results = (List) q.execute();
            assertEquals(1, results.size());
            resultsIter = results.iterator();
            if (resultsIter.hasNext()) {
                Object obj = resultsIter.next();
                assertTrue("ResultClass of query is incorrect. Should have been " + PersonHolder.class.getName() + " but is " + obj.getClass().getName(), PersonHolder.class == obj.getClass());
                PersonHolder holder = (PersonHolder) obj;
                assertNotNull(holder.getPerson1());
                assertNotNull(holder.getPerson2());
            }
            q.closeAll();
            // test new Object(new Object())
            q = pm.newQuery(pm.getExtent(Person.class, false));
            q.setFilter("age > 35");
            q.setResult("this, 0, new org.jpox.samples.models.company.PersonHolder(new org.jpox.samples.models.company.PersonHolder(new org.jpox.samples.models.company.PersonHolder(this),this),this), 1, null");
            results = (List) q.execute();
            assertEquals(1, results.size());
            resultsIter = results.iterator();
            if (resultsIter.hasNext()) {
                Object[] obj = (Object[]) resultsIter.next();
                assertTrue("ResultClass of query is incorrect. Should have been " + PersonHolder.class.getName() + " but is " + obj[2].getClass().getName(), PersonHolder.class == obj[2].getClass());
                PersonHolder holder = (PersonHolder) obj[2];
                assertNotNull(holder.getPerson1());
                assertNotNull(holder.getPerson2());
                assertNotNull(obj[0]);
                assertEquals(obj[0], holder.getPerson1());
                assertEquals(0, ((Long) obj[1]).intValue());
                assertNotNull(obj[3]);
                assertEquals(1, ((Long) obj[3]).intValue());
                assertNull(obj[4]);
            }
            q.closeAll();
            tx.commit();
        } catch (JDOException ex) {
            LOG.error("Exception in query", ex);
            fail(ex.getMessage());
        } catch (Exception e) {
            LOG.error("Exception caught", e);
            fail(e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        // Clean out our data
        clean(Person.class);
    }
}
Also used : Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) ListIterator(java.util.ListIterator) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) Person(org.jpox.samples.models.company.Person) PersonHolder(org.jpox.samples.models.company.PersonHolder) JDOException(javax.jdo.JDOException) JDOUserException(javax.jdo.JDOUserException) JDOException(javax.jdo.JDOException)

Example 28 with JDOException

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

the class PersistenceManagerTest method testMakeTransactionalSingleFieldId.

/**
 * Simple test of makeTransactional method with single-field-id.
 * JDO TCK only tests for user-provided application id.
 */
public void testMakeTransactionalSingleFieldId() throws Exception {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            Vote v = new Vote(101, "Liberal");
            pm.makeTransactional(v);
            tx.commit();
        } catch (JDOException jdoe) {
            LOG.error("Exception in test", jdoe);
            fail("Exception thrown during makeTransactional : " + jdoe.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        clean(Vote.class);
    }
}
Also used : Vote(org.jpox.samples.models.voting.Vote) Transaction(javax.jdo.Transaction) JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager) JDOException(javax.jdo.JDOException)

Example 29 with JDOException

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

the class ApplicationIdPersistenceTest method testUniqueConstraint.

public void testUniqueConstraint() throws Exception {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            ClassWithUniqueField u1 = new ClassWithUniqueField(1, "First Name", "Value");
            pm.makePersistent(u1);
            ClassWithUniqueField u2 = new ClassWithUniqueField(2, "Second Name", "Value");
            pm.makePersistent(u2);
            tx.commit();
            fail("Allowed to insert dupd objects when unique constraint exists");
        } catch (JDOException e) {
        // Expected
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        clean(ClassWithUniqueField.class);
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) ClassWithUniqueField(org.datanucleus.tests.applicationid.ClassWithUniqueField) JDOException(javax.jdo.JDOException)

Example 30 with JDOException

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

the class JDOQLBasicTest method runPrepareTestCandidateCollection.

/**
 * Tests the map get method
 */
protected void runPrepareTestCandidateCollection(PersistenceManager pm) {
    allEmployeeIds = new HashSet();
    warnerEmployeeIds = new HashSet();
    expectedEmployeeIds = new HashSet();
    Employee woody = new Employee(1, "Woody", "Woodpecker", "woody@woodpecker.com", 13, "serial 1");
    Employee bart = new Employee(2, "Bart", "Simpson", "bart@simpson.com", 2, "serial 2");
    // Eh, what's up, doc?
    Employee bunny = new Employee(3, "Bugs", "Bunny", "bugs.bunny@warnerbros.com", 12, "serial 3");
    // Beep! Beep!
    Employee roadrunner = new Employee(4, "Road", "Runner", "road.runner@warnerbros.com", 11, "serial 4");
    // I hate the gravity
    Employee coyote = new Employee(5, "Wile", "E. Coyote", "wile.coyote@acme.com", 9, "serial 5");
    // paranoid, and neurotic
    Employee duck = new Employee(6, "Daffy", "Duck", "daffy.duck@warnerbros.com", 7, "serial 6");
    // You are my peanut.
    Employee pepe = new Employee(7, "Pepe", "le Pew", "pepe.lepew@warnerbros.com", 8, "serial 7");
    Transaction tx = pm.currentTransaction();
    pm.currentTransaction().setNontransactionalRead(true);
    try {
        tx.begin();
        pm.newQuery(Employee.class).deletePersistentAll();
        pm.makePersistent(woody);
        pm.makePersistent(bart);
        pm.makePersistent(bunny);
        pm.makePersistent(roadrunner);
        pm.makePersistent(coyote);
        pm.makePersistent(duck);
        pm.makePersistent(pepe);
        tx.commit();
        Object id = JDOHelper.getObjectId(woody);
        allEmployeeIds.add(id);
        id = JDOHelper.getObjectId(bart);
        allEmployeeIds.add(id);
        id = JDOHelper.getObjectId(bunny);
        allEmployeeIds.add(id);
        warnerEmployeeIds.add(id);
        expectedEmployeeIds.add(id);
        id = JDOHelper.getObjectId(roadrunner);
        allEmployeeIds.add(id);
        warnerEmployeeIds.add(id);
        expectedEmployeeIds.add(id);
        id = JDOHelper.getObjectId(coyote);
        allEmployeeIds.add(id);
        warnerEmployeeIds.add(id);
        id = JDOHelper.getObjectId(duck);
        allEmployeeIds.add(id);
        warnerEmployeeIds.add(id);
        id = JDOHelper.getObjectId(pepe);
        allEmployeeIds.add(id);
        warnerEmployeeIds.add(id);
    } catch (JDOException jdoe) {
        LOG.error("Exception thrown preparing test for candidate collection", jdoe);
        throw jdoe;
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
    }
}
Also used : Employee(org.jpox.samples.models.company.Employee) Transaction(javax.jdo.Transaction) HashSet(java.util.HashSet) JDOException(javax.jdo.JDOException)

Aggregations

JDOException (javax.jdo.JDOException)32 PersistenceManager (javax.jdo.PersistenceManager)14 Transaction (javax.jdo.Transaction)13 InvocationTargetException (java.lang.reflect.InvocationTargetException)7 Query (javax.jdo.Query)7 Iterator (java.util.Iterator)6 JDOUserException (javax.jdo.JDOUserException)6 NucleusException (org.datanucleus.exceptions.NucleusException)6 JDOUnsupportedOptionException (javax.jdo.JDOUnsupportedOptionException)5 Constructor (java.lang.reflect.Constructor)4 ArrayList (java.util.ArrayList)4 List (java.util.List)4 JDOFatalUserException (javax.jdo.JDOFatalUserException)4 PersistableExpression (javax.jdo.query.PersistableExpression)4 Collection (java.util.Collection)3 ClassNotResolvedException (org.datanucleus.exceptions.ClassNotResolvedException)3 Person (org.jpox.samples.models.company.Person)3 Method (java.lang.reflect.Method)2 Date (java.sql.Date)2 SQLException (java.sql.SQLException)2