Search in sources :

Example 26 with BasicTypeHolder

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

the class JDOQLResultTest method testSetResultWithAggregationWithoutSubclasses.

public void testSetResultWithAggregationWithoutSubclasses() {
    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(pm.getExtent(BasicTypeHolder.class, false));
            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)

Example 27 with BasicTypeHolder

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

the class JDOQLResultTest method testOrdering.

/**
 * Test of the ordering clause of a JDOQL statement.
 */
public void testOrdering() {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            // Try some ordering using basic fields
            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].setIntField(20040101);
                basics[i].setBooleanObjectField(new Boolean(i % 2 == 0));
                basics[i].setCharField('0');
            }
            pm.makePersistentAll(basics);
            StringHolder[] strings = new StringHolder[5];
            for (int i = 0; i < strings.length; i++) {
                strings[i] = new StringHolder();
            }
            strings[0].setNormalString("Aaa");
            strings[1].setNormalString("bBB");
            strings[2].setNormalString("ddd");
            strings[3].setNormalString("Ccc");
            strings[4].setNormalString("eEe");
            pm.makePersistentAll(strings);
            pm.flush();
            Query q = pm.newQuery(BasicTypeHolder.class);
            q.setOrdering("longField ascending");
            List l = (List) q.execute();
            Assert.assertEquals(5, l.size());
            q = pm.newQuery(BasicTypeHolder.class);
            q.setOrdering("longField - shortField ascending");
            l = (List) q.execute();
            Assert.assertEquals(5, l.size());
            q = pm.newQuery(BasicTypeHolder.class);
            q.setOrdering("(longField - shortField) ascending");
            l = (List) q.execute();
            Assert.assertEquals(5, l.size());
            q = pm.newQuery(BasicTypeHolder.class);
            q.setOrdering("Math.abs(-longField) ascending");
            l = (List) q.execute();
            Assert.assertEquals(5, l.size());
            Assert.assertEquals(1, ((BasicTypeHolder) l.get(0)).getLongField());
            Assert.assertEquals(2, ((BasicTypeHolder) l.get(1)).getLongField());
            Assert.assertEquals(3, ((BasicTypeHolder) l.get(2)).getLongField());
            Assert.assertEquals(4, ((BasicTypeHolder) l.get(3)).getLongField());
            Assert.assertEquals(5, ((BasicTypeHolder) l.get(4)).getLongField());
            // Query our 5 StringHolders and order by uppercase(normalString)
            q = pm.newQuery(StringHolder.class);
            q.setOrdering("normalString.toUpperCase() ascending");
            l = (List) q.execute();
            assertEquals("The number of returned elements is incorrect", 5, l.size());
            assertEquals("The first Primitive is incorrectly ordered", "Aaa", ((StringHolder) l.get(0)).getNormalString());
            assertEquals("The second Primitive is incorrectly ordered", "bBB", ((StringHolder) l.get(1)).getNormalString());
            assertEquals("The third Primitive is incorrectly ordered", "Ccc", ((StringHolder) l.get(2)).getNormalString());
            assertEquals("The fourth Primitive is incorrectly ordered", "ddd", ((StringHolder) l.get(3)).getNormalString());
            assertEquals("The fifth Primitive is incorrectly ordered", "eEe", ((StringHolder) l.get(4)).getNormalString());
            // Composite ordering
            q = pm.newQuery(BasicTypeHolder.class);
            q.setOrdering("(longField - shortField) ascending , this.longField  ascending");
            l = (List) q.execute();
            Assert.assertEquals(5, l.size());
            q = pm.newQuery(BasicTypeHolder.class);
            q.setOrdering("this.booleanObjField ascending");
            l = (List) q.execute();
            Assert.assertEquals(5, l.size());
            // Don't actually create the instances
            tx.rollback();
            // Try some ordering using self-referencing relations
            tx.begin();
            Person p1 = new Person(1, "Bart", "Simpson", "bart@simpsons.com");
            Person p2 = new Person(2, "Homer", "Simpson", "homer@simpsons.com");
            Person p3 = new Person(3, "Lisa", "Simpson", "lisa@simpsons.com");
            Person p4 = new Person(4, "Marge", "Simpson", "marge@simpsons.com");
            Person p5 = new Person(5, "Maggie", "Simpson", "maggie@simpsons.com");
            Person p6 = new Person(6, "Moe", "Bartender", "moe@simpsons.com");
            Person p7 = new Person(7, "Deputy", "Dawg", "deputy@cartoons.com");
            Person p8 = new Person(8, "Road", "Runner", "maggie@cartoons.com");
            Person p9 = new Person(9, "Donald", "Duck", "moe@cartoons.com");
            p4.setBestFriend(p2);
            p5.setBestFriend(p3);
            p6.setBestFriend(p1);
            pm.makePersistentAll(new Object[] { p4, p5, p6, p7, p8, p9 });
            pm.flush();
            q = pm.newQuery(Person.class);
            q.setOrdering("bestFriend.firstName ascending NULLS FIRST");
            l = (List) q.execute();
            // 9 Person objects so 9 results - 6 nulls, and then 3 results
            Assert.assertEquals(9, l.size());
            Iterator it = l.iterator();
            Person p = (Person) it.next();
            assertTrue(p.getBestFriend() == null);
            p = (Person) it.next();
            assertTrue(p.getBestFriend() == null);
            p = (Person) it.next();
            assertTrue(p.getBestFriend() == null);
            p = (Person) it.next();
            assertTrue(p.getBestFriend() == null);
            p = (Person) it.next();
            assertTrue(p.getBestFriend() == null);
            p = (Person) it.next();
            assertTrue(p.getBestFriend() == null);
            p = (Person) it.next();
            assertTrue(p.getBestFriend().getFirstName().equals("Bart"));
            assertTrue(p.getFirstName().equals("Moe"));
            p = (Person) it.next();
            assertTrue(p.getBestFriend().getFirstName().equals("Homer"));
            assertTrue(p.getFirstName().equals("Marge"));
            p = (Person) it.next();
            assertTrue(p.getBestFriend().getFirstName().equals("Lisa"));
            assertTrue(p.getFirstName().equals("Maggie"));
            // Don't actually create the instances
            tx.rollback();
        } 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(BasicTypeHolder.class);
    }
}
Also used : Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) BasicTypeHolder(org.jpox.samples.types.basic.BasicTypeHolder) JDOException(javax.jdo.JDOException) JDOUserException(javax.jdo.JDOUserException) StringHolder(org.jpox.samples.types.basic.StringHolder) Transaction(javax.jdo.Transaction) ListIterator(java.util.ListIterator) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) Person(org.jpox.samples.models.company.Person)

Example 28 with BasicTypeHolder

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

the class JDOQLSubqueryTest method testSubqueryInResult.

public void testSubqueryInResult() {
    try {
        // Persist some objects
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            BasicTypeHolder holder1 = new BasicTypeHolder();
            holder1.setLongField(1);
            BasicTypeHolder holder2 = new BasicTypeHolder();
            holder2.setLongField(1);
            BasicTypeHolder holder3 = new BasicTypeHolder();
            holder3.setLongField(1);
            BasicTypeHolder holder4 = new BasicTypeHolder();
            holder4.setLongField(2);
            BasicTypeHolder holder5 = new BasicTypeHolder();
            holder5.setLongField(2);
            pm.makePersistent(holder1);
            pm.makePersistent(holder2);
            pm.makePersistent(holder3);
            pm.makePersistent(holder4);
            pm.makePersistent(holder5);
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception thrown persisting objects", e);
            fail("Exception thrown persisting objects : " + e.getMessage());
            return;
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // Query using a subquery that returns its value to the result and ordering clauses
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            Query subquery = pm.newQuery(BasicTypeHolder.class);
            subquery.setResult("count(this)");
            subquery.setFilter("this.longField == :tid");
            Map<String, Object> innerParams = new HashMap<String, Object>();
            innerParams.put("tid", "this.longField");
            Query q = pm.newQuery(BasicTypeHolder.class);
            q.setResult("this.longField, bcnt");
            q.addSubquery(subquery, "long bcnt", null, innerParams);
            q.setOrdering("bcnt desc");
            List<Object[]> results = (List<Object[]>) q.execute();
            assertEquals(5, results.size());
            // check first record
            Object[] o = results.get(0);
            Long num = (Long) o[0];
            Long cnt = (Long) o[1];
            assertEquals("Expected 1 as the first record because its count should be highest", new Long(1), num);
            assertEquals("Expected 3 as the count for 1", new Long(3), cnt);
            // check last record
            o = results.get(results.size() - 1);
            num = (Long) o[0];
            cnt = (Long) o[1];
            assertEquals("Expected 2 as the last record because its count should be lowest", new Long(2), num);
            assertEquals("Expected 2 as the count for 2", new Long(2), cnt);
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception thrown querying subquery in result", e);
            fail("Exception thrown querying subquery in result : " + e.getMessage());
            return;
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        // Clean out our data
        clean(BasicTypeHolder.class);
    }
}
Also used : Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) HashMap(java.util.HashMap) BasicTypeHolder(org.jpox.samples.types.basic.BasicTypeHolder) JDOUserException(javax.jdo.JDOUserException) Transaction(javax.jdo.Transaction) List(java.util.List)

Example 29 with BasicTypeHolder

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

the class JDOQLBasicTest method testMathSqrt.

/**
 * Tests the Math.sqrt expression
 */
public void testMathSqrt() {
    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 + 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();
        // use abs to make sure we are not taking square root from negative numbers
        Query q = pm.newQuery(BasicTypeHolder.class, "Math.sqrt(Math.abs(this.longField)) == 2");
        Collection c = (Collection) q.execute();
        Assert.assertEquals(1, c.size());
        Assert.assertEquals(ids[0], JDOHelper.getObjectId(c.iterator().next()));
        q = pm.newQuery(BasicTypeHolder.class, "java.lang.Math.sqrt(Math.abs(this.longField)) == 2");
        c = (Collection) q.execute();
        Assert.assertEquals(1, c.size());
        Assert.assertEquals(ids[0], JDOHelper.getObjectId(c.iterator().next()));
        q = pm.newQuery(BasicTypeHolder.class, "Math.sqrt(Math.abs(this.longField)) == Math.sqrt(9)");
        c = (Collection) q.execute();
        Assert.assertEquals(1, c.size());
        Assert.assertEquals(ids[1], JDOHelper.getObjectId(c.iterator().next()));
        q = pm.newQuery(BasicTypeHolder.class, "Math.sqrt(Math.abs(this.longField)) == Math.sqrt(16)");
        c = (Collection) q.execute();
        Assert.assertEquals(1, c.size());
        Assert.assertEquals(ids[2], JDOHelper.getObjectId(c.iterator().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) Collection(java.util.Collection) 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