Search in sources :

Example 11 with BasicTypeHolder

use of org.jpox.samples.types.basic.BasicTypeHolder in project tests by datanucleus.

the class JDOQLResultTest method testResultWithIfElse.

/**
 * Test the use of result with "IF ... ELSE ...".
 */
public void testResultWithIfElse() {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            BasicTypeHolder[] basics = new BasicTypeHolder[5];
            for (int i = 0; i < basics.length; i++) {
                basics[i] = new BasicTypeHolder();
                basics[i].setLongField(i + 1);
                basics[i].setShortField((short) (i + 11));
                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);
            q.setResult("IF (this.longField > 2) 1 ELSE 0");
            Collection c = (Collection) q.execute();
            Assert.assertEquals(5, c.size());
            int numZeros = 0;
            int numOnes = 0;
            for (Object obj : c) {
                Long l = Long.valueOf("" + obj);
                if (l == 1) {
                    numOnes++;
                } else if (l == 0) {
                    numZeros++;
                }
            }
            assertEquals(3, numOnes);
            assertEquals(2, numZeros);
            tx.commit();
            tx.begin();
            try {
                // Test construct with no final "ELSE expr"
                q = pm.newQuery(BasicTypeHolder.class);
                q.setResult("IF (this.longField > 2) 1 ELSE IF (this.logField < 0) 0");
                q.compile();
                fail("Should have thrown exception with missing ELSE on IF construct, but didnt");
            } catch (JDOException jdoe) {
            // Expected
            }
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        // 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)

Example 12 with BasicTypeHolder

use of org.jpox.samples.types.basic.BasicTypeHolder in project tests by datanucleus.

the class JDOQLResultTest method testSetResultParameter.

/**
 * Test the use of setResult() returning the parameter
 */
public void testSetResultParameter() {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            BasicTypeHolder[] basics = new BasicTypeHolder[5];
            for (int i = 0; i < basics.length; i++) {
                basics[i] = new BasicTypeHolder();
                basics[i].setLongField(i + 1);
                basics[i].setShortField((short) (i + 11));
                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();
            // obtain a primite persistent instance
            Query q = pm.newQuery(BasicTypeHolder.class);
            q.setFilter("this.longField == 1 || this.longField == 2");
            q.setResult("this");
            Collection c = (Collection) q.execute();
            Iterator it = c.iterator();
            BasicTypeHolder p = (BasicTypeHolder) it.next();
            // test return String
            q = pm.newQuery(BasicTypeHolder.class);
            q.setFilter("this.longField == 1 || this.longField == 2");
            q.setResult("s");
            q.declareParameters("String s");
            c = (Collection) q.execute("result");
            Assert.assertEquals(2, c.size());
            it = c.iterator();
            assertEquals("result", it.next());
            assertEquals("result", it.next());
            // test return Long
            q = pm.newQuery(BasicTypeHolder.class);
            q.setFilter("this.longField == 1 || this.longField == 2");
            q.setResult("l");
            q.declareParameters("Long l");
            c = (Collection) q.execute(new Long(10));
            Assert.assertEquals(2, c.size());
            it = c.iterator();
            assertEquals(new Long(10), it.next());
            assertEquals(new Long(10), it.next());
            // test return Primitive
            q = pm.newQuery(BasicTypeHolder.class);
            q.setFilter("this.longField == 1 || this.longField == 2");
            q.setResult("p");
            q.declareParameters("BasicTypeHolder p");
            c = (Collection) q.execute(p);
            Assert.assertEquals(2, c.size());
            it = c.iterator();
            assertEquals(p, it.next());
            assertEquals(p, it.next());
            // test return Primitive
            q = pm.newQuery(BasicTypeHolder.class);
            q.setFilter("this.longField == 1");
            q.setResult("p");
            q.declareParameters("BasicTypeHolder p");
            c = (Collection) q.execute(p);
            Assert.assertEquals(1, c.size());
            it = c.iterator();
            assertEquals(p, it.next());
            // test return null
            q = pm.newQuery(BasicTypeHolder.class);
            q.setFilter("this.longField == 1");
            q.setResult("p");
            q.declareParameters("BasicTypeHolder p");
            c = (Collection) q.execute(null);
            Assert.assertEquals(1, c.size());
            it = c.iterator();
            assertNull(it.next());
            // test return Primitive
            q = pm.newQuery(BasicTypeHolder.class);
            q.setFilter("this.longField == 1");
            q.setResult("this, p");
            q.declareParameters("BasicTypeHolder p");
            c = (Collection) q.execute(p);
            Assert.assertEquals(1, c.size());
            it = c.iterator();
            assertEquals(p, ((Object[]) it.next())[1]);
            // test return Long
            q = pm.newQuery(BasicTypeHolder.class);
            q.setFilter("this.longField == 1");
            q.setResult("p.longField");
            q.declareParameters("BasicTypeHolder p");
            c = (Collection) q.execute(p);
            Assert.assertEquals(1, c.size());
            it = c.iterator();
            assertEquals(1, p.getLongField());
            assertEquals(new Long(1), it.next());
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception in query", e);
            fail("Exception thrown by query " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        // Clean out our data
        clean(BasicTypeHolder.class);
    }
}
Also used : Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) ListIterator(java.util.ListIterator) Iterator(java.util.Iterator) Collection(java.util.Collection) BasicTypeHolder(org.jpox.samples.types.basic.BasicTypeHolder) JDOException(javax.jdo.JDOException) JDOUserException(javax.jdo.JDOUserException)

Example 13 with BasicTypeHolder

use of org.jpox.samples.types.basic.BasicTypeHolder in project tests by datanucleus.

the class JDOQLResultTest method testCandidateCollection.

/**
 * Test the use of setResult().
 */
public void testCandidateCollection() {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            BasicTypeHolder[] basics = new BasicTypeHolder[5];
            for (int i = 0; i < basics.length; i++) {
                basics[i] = new BasicTypeHolder();
                basics[i].setLongField(i + 1);
                basics[i].setShortField((short) (i + 11));
                basics[i].setCharField('0');
            }
            pm.makePersistentAll(basics);
            List candidates = new ArrayList();
            candidates.add(basics[0]);
            candidates.add(basics[1]);
            candidates.add(basics[2]);
            candidates.add(basics[3]);
            candidates.add(basics[4]);
            // Basic candidate test
            Query q = pm.newQuery(BasicTypeHolder.class);
            q.setCandidates(candidates);
            Collection c = (Collection) q.execute();
            assertEquals(5, c.size());
            // Add duplicate of one candidate (so should get dup in result)
            candidates.add(basics[4]);
            q.setCandidates(candidates);
            c = (Collection) q.execute();
            assertEquals(6, c.size());
            // Same thing but with result
            q.setResult("this");
            c = (Collection) q.execute();
            assertEquals(6, c.size());
            // Same thing but with distinct result so we eliminate the dup candidate
            q.setResult("distinct this");
            c = (Collection) q.execute();
            assertEquals(5, c.size());
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        // Clean out our data
        clean(BasicTypeHolder.class);
    }
}
Also used : Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) ArrayList(java.util.ArrayList) Collection(java.util.Collection) ArrayList(java.util.ArrayList) List(java.util.List) BasicTypeHolder(org.jpox.samples.types.basic.BasicTypeHolder)

Example 14 with BasicTypeHolder

use of org.jpox.samples.types.basic.BasicTypeHolder in project tests by datanucleus.

the class JDOQLBasicTest method testDeleteByQuery.

/**
 * Test for deletion by query.
 */
public void testDeleteByQuery() {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        // Create some sample data (to delete)
        tx.begin();
        BasicTypeHolder[] basics = new BasicTypeHolder[5];
        for (int i = 0; i < basics.length; i++) {
            basics[i] = new BasicTypeHolder();
            basics[i].setLongField(i + 1);
            basics[i].setShortField((short) (i + 11));
            basics[i].setCharField('0');
        }
        pm.makePersistentAll(basics);
        tx.commit();
        // Run the test
        tx.begin();
        Query q = pm.newQuery(BasicTypeHolder.class);
        long numberDeleted = q.deletePersistentAll();
        assertEquals("Number of BasicTypeHolder objects deleted by query was incorrect", 5, numberDeleted);
        tx.commit();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
        clean(BasicTypeHolder.class);
    }
}
Also used : Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) BasicTypeHolder(org.jpox.samples.types.basic.BasicTypeHolder)

Example 15 with BasicTypeHolder

use of org.jpox.samples.types.basic.BasicTypeHolder in project tests by datanucleus.

the class JDOQLBasicTest method testClose.

public void testClose() {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        try {
            tx.begin();
            BasicTypeHolder[] basics = new BasicTypeHolder[5];
            for (int i = 0; i < basics.length; i++) {
                basics[i] = new BasicTypeHolder();
                basics[i].setLongField(i + 1);
                basics[i].setShortField((short) (i + 11));
                basics[i].setCharField('0');
            }
            pm.makePersistentAll(basics);
            tx.commit();
        } catch (Exception e) {
            fail(e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
        }
        try {
            tx.begin();
            Query q = pm.newQuery(BasicTypeHolder.class, "(this.longField == longVar)").parameters("java.lang.Long longVar");
            Collection c = (Collection) q.execute(new Long(1));
            Assert.assertEquals(1, c.size());
            q.closeAll();
            tx.commit();
        } catch (Exception e) {
            fail(e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
        }
        boolean success = false;
        try {
            success = false;
            tx.begin();
            Query q = pm.newQuery(BasicTypeHolder.class, "(this.longField == longVar)").parameters("java.lang.Long longVar");
            Collection c = (Collection) q.execute(new Long(1));
            q.closeAll();
            c.size();
            tx.commit();
        } catch (JDOUserException e) {
            success = true;
        } finally {
            Assert.assertTrue("Query result has been closed and exception was expected", success);
            if (tx.isActive()) {
                tx.rollback();
            }
        }
        try {
            tx.begin();
            Query q = pm.newQuery(BasicTypeHolder.class, "(this.longField == longVar)").parameters("java.lang.Long longVar");
            Collection c = (Collection) q.execute(new Long(1));
            Iterator iterator = c.iterator();
            q.closeAll();
            Assert.assertFalse("Query result has been closed and iterator should be closed", iterator.hasNext());
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
        }
        try {
            success = false;
            tx.begin();
            Query q = pm.newQuery(BasicTypeHolder.class, "(this.longField == longVar)").parameters("java.lang.Long longVar");
            Collection c = (Collection) q.execute(new Long(1));
            Iterator iterator = c.iterator();
            q.closeAll();
            iterator.next();
            tx.commit();
        } catch (NoSuchElementException e) {
            success = true;
        } finally {
            Assert.assertTrue("Query result has been closed and exception was expected", success);
            if (tx.isActive()) {
                tx.rollback();
            }
        }
        try {
            tx.begin();
            Query q = pm.newQuery(BasicTypeHolder.class, "(this.longField == longVar)").parameters("java.lang.Long longVar");
            Collection c = (Collection) q.execute(new Long(1));
            Iterator iterator = c.iterator();
            q.close(c);
            Assert.assertFalse("Query result has been closed and iterator should be closed", iterator.hasNext());
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
        }
        try {
            success = false;
            tx.begin();
            Query q = pm.newQuery(BasicTypeHolder.class, "(this.longField == longVar)").parameters("java.lang.Long longVar");
            Collection c = (Collection) q.execute(new Long(1));
            Iterator iterator = c.iterator();
            q.close(c);
            iterator.next();
            tx.commit();
        } catch (NoSuchElementException e) {
            success = true;
        } finally {
            Assert.assertTrue("Query result has been closed and exception was expected", success);
            if (tx.isActive()) {
                tx.rollback();
            }
        }
        try {
            tx.begin();
            Query q = pm.newQuery(BasicTypeHolder.class, "(this.longField == longVar)").parameters("java.lang.Long longVar");
            Collection c = (Collection) q.execute(new Long(1));
            Iterator iterator = c.iterator();
            q.close(new HashSet());
            Assert.assertTrue("Query result has not been closed and iterator should not be closed", iterator.hasNext());
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
        }
        try {
            tx.begin();
            Query q = pm.newQuery(BasicTypeHolder.class, "(this.longField == longVar)").parameters("java.lang.Long longVar");
            Collection c = (Collection) q.execute(new Long(1));
            Iterator iterator = c.iterator();
            q.close(new HashSet());
            iterator.next();
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
        }
        try {
            tx.begin();
            Query q = pm.newQuery(BasicTypeHolder.class, "(this.longField == longVar)").parameters("java.lang.Long longVar");
            Collection c = (Collection) q.executeWithArray(new Object[] { new Long(1) });
            Iterator iterator = c.iterator();
            q.close(c);
            Assert.assertFalse("Query result has been closed and iterator should be closed", iterator.hasNext());
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
        }
        try {
            success = false;
            tx.begin();
            Query q = pm.newQuery(BasicTypeHolder.class, "(this.longField == longVar)").parameters("java.lang.Long longVar");
            Collection c = (Collection) q.executeWithArray(new Object[] { new Long(1) });
            Iterator iterator = c.iterator();
            q.close(c);
            iterator.next();
            tx.commit();
        } catch (NoSuchElementException e) {
            success = true;
        } finally {
            Assert.assertTrue("Query result has been closed and exception was expected", success);
            if (tx.isActive()) {
                tx.rollback();
            }
        }
        try {
            tx.begin();
            Query q = pm.newQuery(BasicTypeHolder.class, "(this.longField == longVar)");
            q.declareParameters("java.lang.Long longVar");
            q.setCandidates(new HashSet());
            Collection c = (Collection) q.executeWithArray(new Object[] { new Long(1) });
            Iterator iterator = c.iterator();
            q.close(c);
            Assert.assertFalse("Query result has been closed and iterator should be closed", iterator.hasNext());
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
        }
        try {
            success = false;
            tx.begin();
            Query q = pm.newQuery(BasicTypeHolder.class, "(this.longField == longVar)").parameters("java.lang.Long longVar");
            q.setCandidates(new HashSet());
            Collection c = (Collection) q.executeWithArray(new Object[] { new Long(1) });
            Iterator iterator = c.iterator();
            q.close(c);
            iterator.next();
            tx.commit();
        } catch (NoSuchElementException e) {
            success = true;
        } finally {
            Assert.assertTrue("Query result has been closed and exception was expected", success);
            if (tx.isActive()) {
                tx.rollback();
            }
        }
    } finally {
        pm.close();
        // Clean out our data
        clean(BasicTypeHolder.class);
    }
}
Also used : Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) JDOUserException(javax.jdo.JDOUserException) BasicTypeHolder(org.jpox.samples.types.basic.BasicTypeHolder) JDOException(javax.jdo.JDOException) JDOUserException(javax.jdo.JDOUserException) NoSuchElementException(java.util.NoSuchElementException) Transaction(javax.jdo.Transaction) Iterator(java.util.Iterator) Collection(java.util.Collection) NoSuchElementException(java.util.NoSuchElementException) HashSet(java.util.HashSet)

Aggregations

BasicTypeHolder (org.jpox.samples.types.basic.BasicTypeHolder)29 PersistenceManager (javax.jdo.PersistenceManager)28 Transaction (javax.jdo.Transaction)28 Query (javax.jdo.Query)27 Collection (java.util.Collection)20 JDOException (javax.jdo.JDOException)10 JDOUserException (javax.jdo.JDOUserException)10 Iterator (java.util.Iterator)8 List (java.util.List)6 ListIterator (java.util.ListIterator)6 ArrayList (java.util.ArrayList)4 NoSuchElementException (java.util.NoSuchElementException)3 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Collectors.toList (java.util.stream.Collectors.toList)1 JDOOptimisticVerificationException (javax.jdo.JDOOptimisticVerificationException)1 Person (org.jpox.samples.models.company.Person)1 StringHolder (org.jpox.samples.types.basic.StringHolder)1