Search in sources :

Example 1 with StringHolder

use of org.jpox.samples.types.basic.StringHolder 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)

Aggregations

ArrayList (java.util.ArrayList)1 Iterator (java.util.Iterator)1 List (java.util.List)1 ListIterator (java.util.ListIterator)1 JDOException (javax.jdo.JDOException)1 JDOUserException (javax.jdo.JDOUserException)1 PersistenceManager (javax.jdo.PersistenceManager)1 Query (javax.jdo.Query)1 Transaction (javax.jdo.Transaction)1 Person (org.jpox.samples.models.company.Person)1 BasicTypeHolder (org.jpox.samples.types.basic.BasicTypeHolder)1 StringHolder (org.jpox.samples.types.basic.StringHolder)1