Search in sources :

Example 56 with Person

use of org.jpox.samples.models.company.Person in project tests by datanucleus.

the class JDOQLInMemoryTest method testParameterMethodInvoke.

/**
 * Test of parameter.method().
 */
public void testParameterMethodInvoke() {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        Query q = pm.newQuery(getQueryLanguage(), "SELECT FROM " + Person.class.getName() + " WHERE firstName.toUpperCase() == param1.toUpperCase()");
        q.addExtension("datanucleus.query.evaluateInMemory", "true");
        q.declareParameters("java.lang.String param1");
        Collection results = (Collection) q.execute("aNa");
        assertEquals("Number of results is incorrect", 1, results.size());
        Person p = (Person) results.iterator().next();
        assertEquals("First name of result is incorrect", "Ana", p.getFirstName());
        tx.commit();
    } catch (Exception e) {
        LOG.error("Exception in execution of query", e);
        fail(e.getMessage());
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
}
Also used : Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) Collection(java.util.Collection) Person(org.jpox.samples.models.company.Person) JDOException(javax.jdo.JDOException) JDOUserException(javax.jdo.JDOUserException)

Example 57 with Person

use of org.jpox.samples.models.company.Person in project tests by datanucleus.

the class JDOQLInMemoryTest method testParamToUpperCaseStartsWith.

/**
 * Test for JDOQL chained String methods, using toUpperCase().startsWith() using parameters.
 */
public void testParamToUpperCaseStartsWith() {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        Query<Person> q = pm.newQuery(Person.class, ":param1.toUpperCase().startsWith(:param2.toUpperCase())");
        q.addExtension("datanucleus.query.evaluateInMemory", "true");
        Map<String, String> params = new HashMap<>();
        params.put("param1", "First");
        params.put("param2", "f");
        q.setNamedParameters(params);
        List results = q.executeList();
        assertEquals(3, results.size());
        tx.commit();
    } catch (Exception e) {
        LOG.error("Exception during param.toUpperCase().startsWith() evaluation", e);
        fail("Exception thrown querying data " + e.getMessage());
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) HashMap(java.util.HashMap) List(java.util.List) Person(org.jpox.samples.models.company.Person) JDOException(javax.jdo.JDOException) JDOUserException(javax.jdo.JDOUserException)

Example 58 with Person

use of org.jpox.samples.models.company.Person in project tests by datanucleus.

the class JDOQLInMemoryTest method testImplicitParameter.

/**
 * Test of implicit parameter.
 */
public void testImplicitParameter() {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        Query<Person> q = pm.newQuery(Person.class);
        q.addExtension("datanucleus.query.evaluateInMemory", "true");
        q.setFilter("personNum == :param1");
        Map<String, Integer> params = new HashMap<>();
        params.put("param1", 4);
        q.setNamedParameters(params);
        Collection<Person> c = q.executeList();
        assertEquals(1, c.size());
        tx.commit();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) HashMap(java.util.HashMap) Person(org.jpox.samples.models.company.Person)

Example 59 with Person

use of org.jpox.samples.models.company.Person in project tests by datanucleus.

the class JDOQLInMemoryTest method testStringStartsWith.

/**
 * Test for JDOQL String startsWith method.
 */
public void testStringStartsWith() {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        Query q = pm.newQuery(getQueryLanguage(), "SELECT FROM " + Person.class.getName() + " WHERE firstName.startsWith('B')");
        q.addExtension("datanucleus.query.evaluateInMemory", "true");
        Object res = q.execute();
        assertNotNull("Result set from JDOQL query is null!", res);
        assertTrue("Result set from JDOQL query is of incorrect type!", res instanceof List);
        Collection c = (Collection) res;
        assertEquals("Collection from String.startsWith() has wrong size", 1, c.size());
        // same test with parameter expression
        q = pm.newQuery(getQueryLanguage(), "SELECT FROM " + Person.class.getName());
        q.addExtension("datanucleus.query.evaluateInMemory", "true");
        q.declareParameters("java.lang.String p1");
        q.setFilter("firstName.startsWith(p1)");
        c = (Collection) q.execute("B");
        assertEquals(1, c.size());
        Iterator it = c.iterator();
        assertEquals("Bugs", ((Person) it.next()).getFirstName());
        tx.commit();
    } catch (Exception e) {
        e.printStackTrace();
        fail("Exception thrown querying data " + e.getMessage());
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
}
Also used : Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) Iterator(java.util.Iterator) Collection(java.util.Collection) List(java.util.List) Person(org.jpox.samples.models.company.Person) JDOException(javax.jdo.JDOException) JDOUserException(javax.jdo.JDOUserException)

Example 60 with Person

use of org.jpox.samples.models.company.Person in project tests by datanucleus.

the class JDOQLInMemoryTest method testStringEndsWith.

/**
 * Test for JDOQL String endsWith method.
 */
public void testStringEndsWith() {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        Query q = pm.newQuery(getQueryLanguage(), "SELECT FROM " + Person.class.getName() + " WHERE lastName.endsWith('ck')");
        q.addExtension("datanucleus.query.evaluateInMemory", "true");
        Object res = q.execute();
        assertNotNull("Result set from JDOQL query is null!", res);
        assertTrue("Result set from JDOQL query is of incorrect type!", res instanceof List);
        Collection c = (Collection) res;
        assertEquals("Collection from String.endsWith() has wrong size", 1, c.size());
        // same test with parameter expression
        q = pm.newQuery(getQueryLanguage(), "SELECT FROM " + Person.class.getName());
        q.addExtension("datanucleus.query.evaluateInMemory", "true");
        q.declareParameters("java.lang.String p1");
        q.setFilter("firstName.endsWith(p1)");
        c = (Collection) q.execute("a");
        assertEquals(1, c.size());
        Iterator it = c.iterator();
        assertEquals("Ana", ((Person) it.next()).getFirstName());
        tx.commit();
    } catch (Exception e) {
        e.printStackTrace();
        fail("Exception thrown querying data " + e.getMessage());
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
}
Also used : Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) Iterator(java.util.Iterator) Collection(java.util.Collection) List(java.util.List) Person(org.jpox.samples.models.company.Person) JDOException(javax.jdo.JDOException) JDOUserException(javax.jdo.JDOUserException)

Aggregations

Person (org.jpox.samples.models.company.Person)132 PersistenceManager (javax.jdo.PersistenceManager)130 Transaction (javax.jdo.Transaction)128 Query (javax.jdo.Query)75 List (java.util.List)60 JDOUserException (javax.jdo.JDOUserException)41 Iterator (java.util.Iterator)40 Collection (java.util.Collection)27 Employee (org.jpox.samples.models.company.Employee)22 HashMap (java.util.HashMap)21 JDODataStoreException (javax.jdo.JDODataStoreException)21 JDOException (javax.jdo.JDOException)16 JDOPersistenceManager (org.datanucleus.api.jdo.JDOPersistenceManager)16 ArrayList (java.util.ArrayList)15 BigInteger (java.math.BigInteger)14 JDOObjectNotFoundException (javax.jdo.JDOObjectNotFoundException)14 BigDecimal (java.math.BigDecimal)10 ClassLoaderResolver (org.datanucleus.ClassLoaderResolver)10 JDOQuery (org.datanucleus.api.jdo.JDOQuery)10 JDOQLCompiler (org.datanucleus.query.compiler.JDOQLCompiler)10