Search in sources :

Example 16 with BasicTypeHolder

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

the class JDOQLBasicTest method testArrayLength.

/**
 * Tests the Array.length expression
 */
public void testArrayLength() {
    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]);
        }
        // Query array.length
        tx.begin();
        Query q = pm.newQuery(BasicTypeHolder.class, "{1,3,-11}.length == 3");
        Collection c = (Collection) q.execute();
        Assert.assertEquals(5, c.size());
        q = pm.newQuery(BasicTypeHolder.class, "3 == {1,3,-11}.length");
        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 17 with BasicTypeHolder

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

the class ApplicationIdPersistenceTest method testBasicTypes.

public void testBasicTypes() throws Exception {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        Object id1 = null;
        Object id2 = null;
        try {
            tx.begin();
            BasicTypeHolder basic1 = new BasicTypeHolder();
            basic1.setIntObjField(new Integer(1));
            BasicTypeHolder basic2 = new BasicTypeHolder();
            basic2.setIntObjField(null);
            pm.makePersistent(basic1);
            pm.makePersistent(basic2);
            tx.commit();
            id1 = pm.getObjectId(basic1);
            id2 = pm.getObjectId(basic2);
        } catch (Exception e) {
            LOG.error("Exception when persisting 2 BasicTypeHolder objects", e);
            fail("Exception thrown when running test " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            BasicTypeHolder basic1 = (BasicTypeHolder) pm.getObjectById(id1);
            assertNotNull(basic1);
            assertEquals(new Integer(1), basic1.getIntObjField());
            BasicTypeHolder basic2 = (BasicTypeHolder) pm.getObjectById(id2);
            assertNotNull(basic2);
            assertNull(basic2.getIntObjField());
            tx.commit();
        } catch (Exception e) {
            LOG.error(">> Exception in retrieve", e);
            fail("Exception thrown when running test " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        clean(BasicTypeHolder.class);
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) BasicTypeHolder(org.jpox.samples.types.basic.BasicTypeHolder) JDOOptimisticVerificationException(javax.jdo.JDOOptimisticVerificationException)

Example 18 with BasicTypeHolder

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

the class JDOQLAvgTest method testAvgTypesSubquery.

public void testAvgTypesSubquery() throws Exception {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    // Create sample holders for each value that will avg to a decimal
    BasicTypeHolder holder1 = newBasicTypeHolderWith(10);
    BasicTypeHolder holder2 = newBasicTypeHolderWith(5);
    holder1.setCharField('a');
    holder2.setCharField('a');
    // Additional entry that to be filtered by the where clause
    BasicTypeHolder holder3 = newBasicTypeHolderWith(3);
    holder3.setCharField('b');
    try {
        tx.begin();
        pm.makePersistentAll(holder1, holder2);
        tx.commit();
        // Do a query for each numeric field to check all the types
        for (String field : extractNumericFields(holder1)) {
            Query query = pm.newQuery("SELECT charField FROM org.jpox.samples.types.basic.BasicTypeHolder WHERE avg(" + field + ") == 7.5 GROUP BY charField ");
            tx.begin();
            List<Character> result = (List<Character>) query.execute();
            assertThat(result).as("Should filter based on AVG decimal value").containsExactly('a');
            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) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) BasicTypeHolder(org.jpox.samples.types.basic.BasicTypeHolder)

Example 19 with BasicTypeHolder

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

the class JDOQLAvgTest method newBasicTypeHolderWith.

private BasicTypeHolder newBasicTypeHolderWith(int value) {
    short shortValue = (short) value;
    BasicTypeHolder typeHolder = new BasicTypeHolder();
    typeHolder.setCharField((char) value);
    typeHolder.setShortField(shortValue);
    typeHolder.setShortObjField(shortValue);
    typeHolder.setIntField(value);
    typeHolder.setIntObjField(value);
    typeHolder.setLongField(value);
    typeHolder.setLongObjField((long) value);
    return typeHolder;
}
Also used : BasicTypeHolder(org.jpox.samples.types.basic.BasicTypeHolder)

Example 20 with BasicTypeHolder

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

the class JDOQLBasicTest method testJDOHelperGetObjectID1.

/**
 * Tests the JDOHelper.getObjectId() expression
 */
public void testJDOHelperGetObjectID1() {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        // Persist some other 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();
        Query q = pm.newQuery(BasicTypeHolder.class, "JDOHelper.getObjectId(this) == oid");
        q.declareParameters("Object oid");
        Collection c = null;
        try {
            // Invalid object identity
            c = (Collection) q.execute("nononononnon");
            // In case the query compilation just resolves it to false
            Assert.assertEquals(0, c.size());
        } catch (JDOException jdoe) {
        // Arguable that we should get this with invalid input
        }
        for (int i = 0; i < basics.length; i++) {
            q = pm.newQuery(BasicTypeHolder.class, "JDOHelper.getObjectId(this) == oid");
            q.declareParameters("Object oid");
            c = (Collection) q.execute(ids[i]);
            Assert.assertEquals(1, c.size());
            Assert.assertEquals(ids[i], JDOHelper.getObjectId(c.iterator().next()));
            q = pm.newQuery(BasicTypeHolder.class, "oid == JDOHelper.getObjectId(this)");
            q.declareParameters("Object oid");
            c = (Collection) q.execute(ids[i]);
            Assert.assertEquals(1, c.size());
            Assert.assertEquals(ids[i], JDOHelper.getObjectId(c.iterator().next()));
            q = pm.newQuery(BasicTypeHolder.class, "JDOHelper.getObjectId(this) == JDOHelper.getObjectId(obj)");
            q.declareParameters("Object obj");
            // test that query can be compiled
            q.compile();
            c = (Collection) q.execute(basics[i]);
            Assert.assertEquals(1, c.size());
            Assert.assertEquals(ids[i], JDOHelper.getObjectId(c.iterator().next()));
            q = pm.newQuery(BasicTypeHolder.class, "JDOHelper.getObjectId(this) == JDOHelper.getObjectId(obj)");
            q.declareParameters("BasicTypeHolder obj");
            // test that query can be compiled
            q.compile();
            c = (Collection) q.execute(basics[i]);
            Assert.assertEquals(1, c.size());
            Assert.assertEquals(ids[i], JDOHelper.getObjectId(c.iterator().next()));
        }
        tx.commit();
    } catch (Exception e) {
        LOG.error("Error performing test", e);
        fail("Exception performing test : " + 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) JDOException(javax.jdo.JDOException)

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