Search in sources :

Example 6 with BasicTypeHolder

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

the class JDOQLBasicTest method testUseOfParentheses.

/**
 * Tests use of parentheses inside a JDOQL
 */
public void testUseOfParentheses() {
    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, "(this.longField == 1 && this.shortField == 5) || (this.longField == 3 && this.shortField == 5)");
        Collection c = (Collection) q.execute();
        Assert.assertEquals(0, c.size());
        q = pm.newQuery(BasicTypeHolder.class, "(this.longField == 1 && this.shortField == 5) || (this.longField == 3 && this.shortField == 13)");
        c = (Collection) q.execute();
        Assert.assertEquals(1, c.size());
        Assert.assertEquals(ids[2], JDOHelper.getObjectId(c.iterator().next()));
        q = pm.newQuery(BasicTypeHolder.class, "(this.longField == 4 && this.shortField == 14) || (this.longField == 3 && this.shortField == 5)");
        c = (Collection) q.execute();
        Assert.assertEquals(1, c.size());
        Assert.assertEquals(ids[3], JDOHelper.getObjectId(c.iterator().next()));
        q = pm.newQuery(BasicTypeHolder.class, "(this.longField == 4 && this.shortField == 14) || (this.longField == 3 && this.shortField == 13)");
        q.setOrdering("longField ascending");
        c = (Collection) q.execute();
        Assert.assertEquals(2, c.size());
        Iterator it = c.iterator();
        Assert.assertEquals(ids[2], JDOHelper.getObjectId(it.next()));
        Assert.assertEquals(ids[3], JDOHelper.getObjectId(it.next()));
        q = pm.newQuery(BasicTypeHolder.class, "(this.longField == 4 && this.shortField == 14) || (this.longField != 5 && this.shortField == 13)");
        q.setOrdering("longField ascending");
        c = (Collection) q.execute();
        Assert.assertEquals(2, c.size());
        it = c.iterator();
        Assert.assertEquals(ids[2], JDOHelper.getObjectId(it.next()));
        Assert.assertEquals(ids[3], JDOHelper.getObjectId(it.next()));
        tx.commit();
    } 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) Iterator(java.util.Iterator) Collection(java.util.Collection) BasicTypeHolder(org.jpox.samples.types.basic.BasicTypeHolder)

Example 7 with BasicTypeHolder

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

the class JDOQLResultTest method testCandidateCollectionWithResultNotSameTypeAsCandidates.

/**
 * Test where the result class or result object instance is not of the same type as the candidates.
 */
public void testCandidateCollectionWithResultNotSameTypeAsCandidates() {
    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]);
            candidates.add(basics[4]);
            // Duplicate candidates, but with different result type
            Query q = pm.newQuery(BasicTypeHolder.class);
            q.setCandidates(candidates);
            q.setResult("this, this");
            Collection c = (Collection) q.execute();
            // Not returning object of candidate type so ignore dups in candidates
            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 8 with BasicTypeHolder

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

the class JDOQLResultTest method testSetResultWithAggregationAndNonAggregateExpression.

public void testSetResultWithAggregationAndNonAggregateExpression() {
    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);
            Object[] ids = new Object[5];
            for (int i = 0; i < basics.length; i++) {
                ids[i] = pm.getObjectId(basics[i]);
            }
            pm.flush();
            // -----------
            // COUNT + non aggregate expression
            // -----------
            Query q = pm.newQuery(pm.getExtent(BasicTypeHolder.class, false));
            q.setFilter("this.longField == 2 || this.longField == 3");
            q.setResult("count(this.longField),this.longField");
            q.setGrouping("this.longField");
            q.setOrdering("this.longField ascending");
            Collection c = (Collection) q.execute();
            Assert.assertEquals(2, c.size());
            try {
                Iterator it = c.iterator();
                Object[] object = (Object[]) it.next();
                assertEquals(1, ((Long) object[0]).longValue());
                assertEquals(2, ((Long) object[1]).longValue());
                object = (Object[]) it.next();
                assertEquals(1, ((Long) object[0]).longValue());
                assertEquals(3, ((Long) object[1]).longValue());
            } catch (Exception e) {
                fail(e.getMessage());
            }
            // -----------
            // COUNT + non aggregate expression
            // -----------
            q = pm.newQuery(pm.getExtent(BasicTypeHolder.class, false));
            q.setFilter("this.longField == 2 || this.longField == 3");
            q.setResult("this.longField,count(this.longField)");
            q.setGrouping("this.longField");
            q.setOrdering("this.longField ascending");
            c = (Collection) q.execute();
            Assert.assertEquals(2, c.size());
            try {
                Iterator it = c.iterator();
                Object[] object = (Object[]) it.next();
                assertEquals(1, ((Long) object[1]).longValue());
                assertEquals(2, ((Long) object[0]).longValue());
                object = (Object[]) it.next();
                assertEquals(1, ((Long) object[1]).longValue());
                assertEquals(3, ((Long) object[0]).longValue());
            } catch (Exception e) {
                fail(e.getMessage());
            }
            // -----------
            // SUM + non aggregate expression
            // -----------
            q = pm.newQuery(pm.getExtent(BasicTypeHolder.class, false));
            q.setFilter("this.longField == 2 || this.longField == 3");
            q.setResult("this.longField,sum(this.longField)");
            q.setGrouping("this.longField");
            q.setOrdering("this.longField ascending");
            c = (Collection) q.execute();
            Assert.assertEquals(2, c.size());
            try {
                Iterator it = c.iterator();
                Object[] object = (Object[]) it.next();
                assertEquals(2, ((Long) object[1]).longValue());
                assertEquals(2, ((Long) object[0]).longValue());
                object = (Object[]) it.next();
                assertEquals(3, ((Long) object[1]).longValue());
                assertEquals(3, ((Long) object[0]).longValue());
            } catch (Exception e) {
                fail(e.getMessage());
            }
            // -----------
            // COUNT + non aggregate expression
            // -----------
            q = pm.newQuery(pm.getExtent(BasicTypeHolder.class, false));
            q.setFilter("this.longField == 2 || this.longField == 3");
            q.setResult("count(this.shortField),this.longField");
            q.setGrouping("this.longField");
            q.setOrdering("this.longField ascending");
            c = (Collection) q.execute();
            Assert.assertEquals(2, c.size());
            try {
                Iterator it = c.iterator();
                Object[] object = (Object[]) it.next();
                assertEquals(1, ((Long) object[0]).longValue());
                assertEquals(2, ((Long) object[1]).longValue());
                object = (Object[]) it.next();
                assertEquals(1, ((Long) object[0]).longValue());
                assertEquals(3, ((Long) object[1]).longValue());
            } catch (Exception e) {
                fail(e.getMessage());
            }
            // -----------
            // COUNT + non aggregate expression
            // -----------
            q = pm.newQuery(pm.getExtent(BasicTypeHolder.class, false));
            q.setFilter("this.longField == 2 || this.longField == 3");
            q.setResult("this.longField,count(this.shortField)");
            q.setGrouping("this.longField");
            q.setOrdering("this.longField ascending");
            c = (Collection) q.execute();
            Assert.assertEquals(2, c.size());
            try {
                Iterator it = c.iterator();
                Object[] object = (Object[]) it.next();
                assertEquals(1, ((Long) object[1]).longValue());
                assertEquals(2, ((Long) object[0]).longValue());
                object = (Object[]) it.next();
                assertEquals(1, ((Long) object[1]).longValue());
                assertEquals(3, ((Long) object[0]).longValue());
            } catch (Exception e) {
                fail(e.getMessage());
            }
            // -----------
            // SUM + non aggregate expression
            // -----------
            q = pm.newQuery(pm.getExtent(BasicTypeHolder.class, false));
            q.setFilter("this.longField == 2 || this.longField == 3");
            q.setResult("this.longField, sum(this.shortField)");
            q.setGrouping("this.longField");
            q.setOrdering("this.longField ascending");
            c = (Collection) q.execute();
            Assert.assertEquals(2, c.size());
            Iterator it = c.iterator();
            Object[] object = (Object[]) it.next();
            assertEquals(12, ((Long) object[1]).longValue());
            assertEquals(2, ((Long) object[0]).longValue());
            object = (Object[]) it.next();
            assertEquals(13, ((Long) object[1]).longValue());
            assertEquals(3, ((Long) object[0]).longValue());
            // -----------
            // SUM(this.XXX) + non aggregate expression
            // -----------
            q = pm.newQuery(pm.getExtent(BasicTypeHolder.class, false));
            q.setFilter("this.longField == 2 || this.longField == 3");
            q.setResult("this.longField, sum(this.shortField)");
            q.setGrouping("this.longField");
            c = (Collection) q.execute();
            Assert.assertEquals(2, c.size());
            it = c.iterator();
            object = (Object[]) it.next();
            assertEquals(12, ((Long) object[1]).longValue());
            assertEquals(2, ((Long) object[0]).longValue());
            object = (Object[]) it.next();
            assertEquals(13, ((Long) object[1]).longValue());
            assertEquals(3, ((Long) object[0]).longValue());
            // -----------
            // SUM(XXX) + non aggregate expression
            // -----------
            q = pm.newQuery(pm.getExtent(BasicTypeHolder.class, false));
            q.setFilter("this.longField == 2 || this.longField == 3");
            q.setResult("this.longField, sum(shortField)");
            q.setGrouping("this.longField");
            c = (Collection) q.execute();
            Assert.assertEquals(2, c.size());
            it = c.iterator();
            object = (Object[]) it.next();
            assertEquals(12, ((Long) object[1]).longValue());
            assertEquals(2, ((Long) object[0]).longValue());
            object = (Object[]) it.next();
            assertEquals(13, ((Long) object[1]).longValue());
            assertEquals(3, ((Long) object[0]).longValue());
            // Dont persist the objects
            tx.rollback();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        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 9 with BasicTypeHolder

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

the class JDOQLResultTest method testSetResultWithAggregation1.

public void testSetResultWithAggregation1() {
    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();
            // -----------
            // MAX
            // -----------
            Query q = pm.newQuery(pm.getExtent(BasicTypeHolder.class, false));
            q.setFilter("this.longField == 2 || this.longField == 3");
            q.setResult("max(this.longField)");
            Object result = q.execute();
            assertEquals("Type of result object is incorrect", result.getClass().getName(), "java.lang.Long");
            assertEquals("Result is incorrect", 3, ((Long) result).longValue());
            // -----------
            // MIN
            // -----------
            q = pm.newQuery(pm.getExtent(BasicTypeHolder.class, false));
            q.setFilter("this.longField == 2 || this.longField == 3");
            q.setResult("min(this.longField)");
            result = q.execute();
            assertEquals("Type of result object is incorrect", result.getClass().getName(), "java.lang.Long");
            assertEquals("Result is incorrect", 2, ((Long) result).longValue());
            // -----------
            // SUM
            // -----------
            q = pm.newQuery(pm.getExtent(BasicTypeHolder.class, false));
            q.setFilter("this.longField == 2 || this.longField == 3");
            q.setResult("sum(this.longField)");
            result = q.execute();
            assertEquals("Type of result object is incorrect", result.getClass().getName(), "java.lang.Long");
            assertEquals("Result is incorrect", 5, ((Long) result).longValue());
            q = pm.newQuery(pm.getExtent(BasicTypeHolder.class, false));
            q.setFilter("this.longField == 2 || this.longField == 3");
            q.setResult("count(this.longField)");
            result = q.execute();
            assertEquals("Type of result object is incorrect", result.getClass().getName(), "java.lang.Long");
            assertEquals("Result is incorrect", 2, ((Long) result).longValue());
            q = pm.newQuery(pm.getExtent(BasicTypeHolder.class, false));
            q.setFilter("this.longField == 2 || this.longField == 3");
            q.setResult("count(this.intField)");
            result = q.execute();
            assertEquals("Type of result object is incorrect", result.getClass().getName(), "java.lang.Long");
            assertEquals("Result is incorrect", 2, ((Long) result).longValue());
            q = pm.newQuery(pm.getExtent(BasicTypeHolder.class, false));
            q.setFilter("this.longField == 2 || this.longField == 3");
            q.setResult("count(this)");
            result = q.execute();
            assertEquals("Type of result object is incorrect", result.getClass().getName(), "java.lang.Long");
            assertEquals("Result is incorrect", 2, ((Long) result).longValue());
            // -----------
            // COUNT + non aggregate expression
            // -----------
            q = pm.newQuery(pm.getExtent(BasicTypeHolder.class, false));
            q.setFilter("this.longField == 2 || this.longField == 3");
            q.setResult("count(this.longField),this.longField");
            q.setGrouping("this.longField");
            q.setOrdering("this.longField ascending");
            Collection c = (Collection) q.execute();
            Assert.assertEquals(2, c.size());
            try {
                Iterator it = c.iterator();
                Object[] object = (Object[]) it.next();
                assertEquals(1, ((Long) object[0]).longValue());
                assertEquals(2, ((Long) object[1]).longValue());
                object = (Object[]) it.next();
                assertEquals(1, ((Long) object[0]).longValue());
                assertEquals(3, ((Long) object[1]).longValue());
            } catch (Exception e) {
                fail(e.getMessage());
            }
            // -----------
            // AVG
            // -----------
            q = pm.newQuery(pm.getExtent(BasicTypeHolder.class, false));
            q.setFilter("this.longField == 2 || this.longField == 4");
            q.setResult("avg(this.longField)");
            result = q.execute();
            assertEquals("Type of result object is incorrect", "java.lang.Double", result.getClass().getName());
            assertEquals("Result is incorrect", 3, ((Double) result).longValue());
            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 10 with BasicTypeHolder

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

the class JDOQLResultTest method testSetResultWithAggregationWithSubclasses.

public void testSetResultWithAggregationWithSubclasses() {
    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.setFilter("longField == 2 || longField == 3");
            q.setResult("max(longField)");
            Object result = q.execute();
            assertEquals("Type of result object is incorrect", result.getClass().getName(), "java.lang.Long");
            assertEquals("Result is incorrect", 3, ((Long) result).longValue());
            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) BasicTypeHolder(org.jpox.samples.types.basic.BasicTypeHolder)

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