Search in sources :

Example 1 with BasicTypeHolder

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

the class JDOQLBasicTest method testArrayContains.

/**
 * Tests the Array.contains expression
 */
public void testArrayContains() {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        // Persist some objects
        tx.begin();
        BasicTypeHolder[] basics = new BasicTypeHolder[5];
        for (int i = 0; i < basics.length; i++) {
            basics[i] = new BasicTypeHolder();
            basics[i].setLongField(-10 - i);
            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]);
        }
        // Test array.contains
        tx.begin();
        Query q = pm.newQuery(BasicTypeHolder.class, "{1,3,-11}.contains(this.longField)");
        Collection c = (Collection) q.execute();
        Assert.assertEquals(1, c.size());
        q = pm.newQuery(BasicTypeHolder.class, "{1,3,4}.contains(this.longField)");
        c = (Collection) q.execute();
        Assert.assertEquals(0, c.size());
        q = pm.newQuery(BasicTypeHolder.class, "{1,3,4,this.longField}.contains(this.longField)");
        c = (Collection) q.execute();
        Assert.assertEquals(5, c.size());
        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 2 with BasicTypeHolder

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

the class JDOQLBasicTest method testArrayParameter.

/**
 * Tests the Array in parameter expression
 */
public void testArrayParameter() {
    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(-10 - i);
            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, "p.length() == 3");
        q.declareParameters("Integer[] p");
        Collection c = (Collection) q.execute(new Integer[] { new Integer(1), new Integer(3), new Integer(-11) });
        Assert.assertEquals(5, c.size());
        q = pm.newQuery(BasicTypeHolder.class, "3 == p.length()");
        q.declareParameters("Integer[] p");
        c = (Collection) q.execute(new Integer[] { new Integer(1), new Integer(3), new Integer(-11) });
        Assert.assertEquals(5, c.size());
        q = pm.newQuery(BasicTypeHolder.class, "p.contains(1)");
        q.declareParameters("Integer[] p");
        c = (Collection) q.execute(new Integer[] { new Integer(1), new Integer(3), new Integer(-11) });
        Assert.assertEquals(5, c.size());
        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 3 with BasicTypeHolder

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

the class JDOQLBasicTest method testNullEqualsNull.

public void testNullEqualsNull() {
    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, "null == null");
        q.declareParameters("java.lang.Integer str");
        Collection c = (Collection) q.execute(null);
        Assert.assertEquals(5, c.size());
        q = pm.newQuery(BasicTypeHolder.class, "null != null");
        q.declareParameters("java.lang.Integer str");
        c = (Collection) q.execute(null);
        Assert.assertEquals(0, c.size());
        q = pm.newQuery(BasicTypeHolder.class, "null == null || null != null");
        q.declareParameters("java.lang.Integer str");
        c = (Collection) q.execute(null);
        Assert.assertEquals(5, c.size());
        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 4 with BasicTypeHolder

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

the class JDOQLBasicTest method testMathAbs.

/**
 * Tests the Math.abs expression
 */
public void testMathAbs() {
    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(-10 - i);
            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, "Math.abs(this.longField) == 10");
        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.abs(this.longField) == 10");
        c = (Collection) q.execute();
        Assert.assertEquals(1, c.size());
        Assert.assertEquals(ids[0], JDOHelper.getObjectId(c.iterator().next()));
        q = pm.newQuery(BasicTypeHolder.class, "Math.abs(this.longField) == Math.abs(-11)");
        c = (Collection) q.execute();
        Assert.assertEquals(1, c.size());
        Assert.assertEquals(ids[1], JDOHelper.getObjectId(c.iterator().next()));
        q = pm.newQuery(BasicTypeHolder.class, "Math.abs(this.longField) == Math.abs(-12.0)");
        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)

Example 5 with BasicTypeHolder

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

the class JDOQLBasicTest method testPCLiteralOnQueryCompile.

public void testPCLiteralOnQueryCompile() {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        // Persist some simple objects
        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();
        for (int i = 0; i < basics.length; i++) {
            Query q = pm.newQuery(BasicTypeHolder.class, "this == obj");
            q.declareParameters("BasicTypeHolder obj");
            // test that query can be compiled
            q.compile();
            Collection c = (Collection) q.execute(basics[i]);
            Assert.assertEquals(1, c.size());
            Assert.assertEquals(ids[i], 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