Search in sources :

Example 21 with BasicTypeHolder

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

the class JDOQLBasicTest method testModuloOperator.

/**
 * Test for modulo operator in a Query.
 */
public void testModuloOperator() {
    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();
        // Run the test
        tx.begin();
        Query q = pm.newQuery(pm.getExtent(BasicTypeHolder.class, false), "longField % 2 == 1");
        List c = (List) q.execute();
        assertTrue("Number of items returned from modulo query was incorrect : should have been 3 but was " + c.size(), c.size() == 3);
        for (int i = 0; i < c.size(); i++) {
            long value = ((BasicTypeHolder) c.get(i)).getLongField();
            assertTrue("Long value of returned object is incorrect : should have been 1, 3 or 5 but was " + value, value == 1 || value == 3 || value == 5);
        }
        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) List(java.util.List) ArrayList(java.util.ArrayList) BasicTypeHolder(org.jpox.samples.types.basic.BasicTypeHolder)

Example 22 with BasicTypeHolder

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

the class JDOQLBasicTest method testConcatStringAndNumbers.

/**
 * Tests the concatenation of strings and numbers
 */
public void testConcatStringAndNumbers() {
    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].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 + \"-\" + this.longField == \"1-1\" ");
        Collection c = (Collection) q.execute();
        Assert.assertEquals(1, c.size());
        Assert.assertEquals(ids[0], JDOHelper.getObjectId(c.iterator().next()));
        q = pm.newQuery(BasicTypeHolder.class, "\"\" + this.longField + \"-\" + this.longField == \"\" + 1 + \"-\" + 1");
        c = (Collection) q.execute();
        Assert.assertEquals(1, c.size());
        Assert.assertEquals(ids[0], JDOHelper.getObjectId(c.iterator().next()));
        q = pm.newQuery(BasicTypeHolder.class, "\"\" + this.longField + \"-\" + this.longField == \"\" + 1 + \"-\" + 1 + \"-\"");
        c = (Collection) q.execute();
        Assert.assertEquals(0, c.size());
        q = pm.newQuery(BasicTypeHolder.class, "this.longField + \"-\" + this.longField == 1 + \"-\" + 1");
        c = (Collection) q.execute();
        Assert.assertEquals(1, c.size());
        Assert.assertEquals(ids[0], JDOHelper.getObjectId(c.iterator().next()));
        q = pm.newQuery(BasicTypeHolder.class, "this.longField + \"-\" + this.longField == 1 + \"-\" + 1 + \"-\"");
        c = (Collection) q.execute();
        Assert.assertEquals(0, c.size());
        // test using parameters
        q = pm.newQuery(BasicTypeHolder.class, "\"\" + this.longField + \"-\" + this.longField == str ");
        q.declareParameters("java.lang.String str");
        c = (Collection) q.execute("1-1");
        Assert.assertEquals(1, c.size());
        Assert.assertEquals(ids[0], JDOHelper.getObjectId(c.iterator().next()));
        q = pm.newQuery(BasicTypeHolder.class, "\"\" + this.longField + \"-\" + this.longField == str + \"-\" + str");
        q.declareParameters("java.lang.String str");
        c = (Collection) q.execute("1");
        Assert.assertEquals(1, c.size());
        Assert.assertEquals(ids[0], JDOHelper.getObjectId(c.iterator().next()));
        q = pm.newQuery(BasicTypeHolder.class, "\"\" + this.longField + \"-\" + this.longField == str + \"-\" + str");
        q.declareParameters("java.lang.Integer str");
        c = (Collection) q.execute(new Integer(1));
        Assert.assertEquals(1, c.size());
        Assert.assertEquals(ids[0], JDOHelper.getObjectId(c.iterator().next()));
        q = pm.newQuery(BasicTypeHolder.class, "\"\" + this.longField + \"-\" + this.longField == \"\" + str + \"-\" + str + \"-\"");
        q.declareParameters("java.lang.Integer str");
        c = (Collection) q.execute(new Integer(1));
        Assert.assertEquals(0, c.size());
        q = pm.newQuery(BasicTypeHolder.class, "this.longField + \"-\" + this.longField == str + \"-\" + str");
        q.declareParameters("java.lang.Integer str");
        c = (Collection) q.execute(new Integer(1));
        Assert.assertEquals(1, c.size());
        Assert.assertEquals(ids[0], JDOHelper.getObjectId(c.iterator().next()));
        q = pm.newQuery(BasicTypeHolder.class, "this.longField + \"-\" + this.longField == str + \"-\" + str + \"-\"");
        q.declareParameters("java.lang.Integer str");
        c = (Collection) q.execute(new Integer(1));
        Assert.assertEquals(0, c.size());
        // todo null
        q = pm.newQuery(BasicTypeHolder.class, "\"\" + this.longField + \"-\" + this.longField == str ");
        q.declareParameters("java.lang.String str");
        c = (Collection) q.execute(null);
        Assert.assertEquals(0, c.size());
        q = pm.newQuery(BasicTypeHolder.class, "\"\" + this.longField + \"-\" + this.longField == str + \"-\" + str");
        q.declareParameters("java.lang.String str");
        c = (Collection) q.execute(null);
        Assert.assertEquals(0, c.size());
        q = pm.newQuery(BasicTypeHolder.class, "\"\" + this.longField + \"-\" + this.longField == str + \"-\" + str");
        q.declareParameters("java.lang.Integer str");
        c = (Collection) q.execute(null);
        Assert.assertEquals(0, c.size());
        q = pm.newQuery(BasicTypeHolder.class, "\"\" + this.longField + \"-\" + this.longField == \"\" + str + \"-\" + str + \"-\"");
        q.declareParameters("java.lang.Integer str");
        c = (Collection) q.execute(null);
        Assert.assertEquals(0, c.size());
        q = pm.newQuery(BasicTypeHolder.class, "this.longField + \"-\" + this.longField == str + \"-\" + str");
        q.declareParameters("java.lang.Integer str");
        c = (Collection) q.execute(new Integer(1));
        c = (Collection) q.execute(null);
        Assert.assertEquals(0, c.size());
        q = pm.newQuery(BasicTypeHolder.class, "this.longField + \"-\" + this.longField == str + \"-\" + str + \"-\"");
        q.declareParameters("java.lang.Integer str");
        c = (Collection) q.execute(null);
        Assert.assertEquals(0, c.size());
        q = pm.newQuery(BasicTypeHolder.class, "null == str + \"-\" + str + \"-\" && this.longField == 1");
        q.declareParameters("java.lang.Integer str");
        c = (Collection) q.execute(null);
        Assert.assertEquals(1, c.size());
        Assert.assertEquals(ids[0], 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)

Example 23 with BasicTypeHolder

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

the class JDOQLBasicTest method testAvg.

/**
 * Tests the avg expression
 */
public void testAvg() {
    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].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 == avg(p.longField)");
        q.declareVariables("BasicTypeHolder p");
        Collection c = (Collection) q.execute();
        Assert.assertEquals(1, c.size());
        Assert.assertEquals(ids[2], JDOHelper.getObjectId(c.iterator().next()));
        tx.commit();
    } catch (Exception e) {
        LOG.error("Exception thrown executing JDOQL query with AVG", e);
        fail("Exception thrown executing JDOQL query with AVG : " + 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)

Example 24 with BasicTypeHolder

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

the class JDOQLResultTest method testSetResultDistinct.

/**
 * Test the use of setResult with the DISTINCT keyword.
 */
public void testSetResultDistinct() {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            // Populate some data
            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();
            // Query the data
            tx.begin();
            Query q = pm.newQuery(BasicTypeHolder.class);
            q.setFilter("longField == 2 || longField == 3");
            q.setResult("distinct this.longField");
            q.setOrdering("this.longField ascending");
            Collection c = (Collection) q.execute();
            Assert.assertEquals(2, c.size());
            try {
                Iterator it = c.iterator();
                assertEquals(2, ((Long) it.next()).longValue());
                assertEquals(3, ((Long) it.next()).longValue());
            } catch (Exception e) {
                e.printStackTrace();
                fail(e.getMessage());
            }
            tx.commit();
        } 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 25 with BasicTypeHolder

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

the class JDOQLResultTest method testSetResult.

/**
 * Test the use of setResult().
 */
public void testSetResult() {
    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("this.longField == 2 || this.longField == 3");
            q.setResult("this.longField");
            q.setOrdering("this.longField ascending");
            Collection c = (Collection) q.execute();
            Assert.assertEquals(2, c.size());
            try {
                Iterator it = c.iterator();
                assertEquals(2, ((Long) it.next()).longValue());
                assertEquals(3, ((Long) it.next()).longValue());
            } catch (Exception e) {
                fail(e.getMessage());
            }
            q = pm.newQuery(BasicTypeHolder.class);
            q.setResult("this.longField, this.shortField");
            q.setFilter("this.longField == 2 || this.longField == 3");
            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[0]).longValue());
                assertEquals(12, ((Short) object[1]).shortValue());
                object = (Object[]) it.next();
                assertEquals(3, ((Long) object[0]).longValue());
                assertEquals(13, ((Short) object[1]).shortValue());
            } catch (Exception e) {
                fail(e.getMessage());
            }
            q = pm.newQuery(BasicTypeHolder.class);
            q.setResult("this.longField, this.shortField, this");
            q.setFilter("this.longField == 2 || this.longField == 3");
            q.setOrdering("this.longField ascending");
            c = (Collection) q.execute();
            try {
                Iterator it = c.iterator();
                Object[] object = (Object[]) it.next();
                assertEquals(2, ((Long) object[0]).longValue());
                assertEquals(12, ((Short) object[1]).shortValue());
                assertEquals(ids[1], JDOHelper.getObjectId(object[2]));
                object = (Object[]) it.next();
                assertEquals(3, ((Long) object[0]).longValue());
                assertEquals(13, ((Short) object[1]).shortValue());
                assertEquals(ids[2], JDOHelper.getObjectId(object[2]));
            } catch (Exception e) {
                fail(e.getMessage());
            }
            q = pm.newQuery(BasicTypeHolder.class);
            q.setFilter("this.longField == 1 || this.longField == 2");
            q.setResult("JDOHelper.getObjectId(this)");
            c = (Collection) q.execute();
            Assert.assertEquals(2, c.size());
            Iterator it = c.iterator();
            assertEquals(ids[0], it.next());
            assertEquals(ids[1], it.next());
            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) 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)

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