Search in sources :

Example 91 with Person

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

the class JDOQLEvaluatorTest method testFilterAndWithEqualityOperator.

/**
 * Test of filter with AND and equality operator.
 */
public void testFilterAndWithEqualityOperator() {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        // Create some instances to query over
        List<Person> instances = new ArrayList<>();
        Person p1 = new Person(101, "Mickey", "Mouse", "mickey.mouse@warnerbros.com");
        Person p2 = new Person(102, "Donald", "Duck", "donald.duck@warnerbros.com");
        Person p3 = new Person(103, "Minnie", "Mouse", "minnie.mouse@warnerbros.com");
        instances.add(p1);
        instances.add(p2);
        instances.add(p3);
        // Compile the query
        JDOQuery q = (JDOQuery) pm.newQuery(Person.class, "firstName == 'Mickey' && lastName == 'Mouse'");
        Query query = q.getInternalQuery();
        ClassLoaderResolver clr = query.getExecutionContext().getClassLoaderResolver();
        JavaQueryCompiler compiler = new JDOQLCompiler(query.getExecutionContext().getNucleusContext(), clr, null, query.getCandidateClass(), null, query.getFilter(), query.getParsedImports(), query.getOrdering(), query.getResult(), query.getGrouping(), query.getHaving(), query.getExplicitParametersDeclaration(), query.getExplicitVariablesDeclaration(), null);
        QueryCompilation compilation = compiler.compile(new HashMap(), null);
        // Execute the query
        JavaQueryInMemoryEvaluator eval = new JDOQLInMemoryEvaluator(query, instances, compilation, null, clr);
        List results = (List) eval.execute(true, true, true, true, true);
        assertEquals("Number of result instances was wrong", 1, results.size());
        Person p = (Person) results.get(0);
        assertEquals("Result instance has wrong first name", "Mickey", p.getFirstName());
        assertEquals("Result instance has wrong last name", "Mouse", p.getLastName());
        assertEquals("Person number of result instance is wrong", 101, p.getPersonNum());
        tx.commit();
    } catch (Exception e) {
        e.printStackTrace();
        fail("Exception thrown during query execution " + e.getMessage());
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
}
Also used : JDOQLCompiler(org.datanucleus.query.compiler.JDOQLCompiler) Query(org.datanucleus.store.query.Query) JDOQuery(org.datanucleus.api.jdo.JDOQuery) PersistenceManager(javax.jdo.PersistenceManager) HashMap(java.util.HashMap) JavaQueryInMemoryEvaluator(org.datanucleus.query.inmemory.JavaQueryInMemoryEvaluator) ArrayList(java.util.ArrayList) ClassLoaderResolver(org.datanucleus.ClassLoaderResolver) JDOQLInMemoryEvaluator(org.datanucleus.query.inmemory.JDOQLInMemoryEvaluator) JDOQuery(org.datanucleus.api.jdo.JDOQuery) JavaQueryCompiler(org.datanucleus.query.compiler.JavaQueryCompiler) Transaction(javax.jdo.Transaction) ArrayList(java.util.ArrayList) List(java.util.List) QueryCompilation(org.datanucleus.query.compiler.QueryCompilation) Person(org.jpox.samples.models.company.Person)

Example 92 with Person

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

the class JDOQLEvaluatorTest method testFilterEqualityOperator.

/**
 * Test of filter with == and != operators.
 */
public void testFilterEqualityOperator() {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        // Create some instances to query over
        List<Person> instances = new ArrayList<>();
        Person p1 = new Person(101, "Mickey", "Mouse", "mickey.mouse@warnerbros.com");
        Person p2 = new Person(102, "Donald", "Duck", "donald.duck@warnerbros.com");
        Person p3 = new Person(103, "Minnie", "Mouse", "minnie.mouse@warnerbros.com");
        instances.add(p1);
        instances.add(p2);
        instances.add(p3);
        // Compile the query
        JDOQuery q = (JDOQuery) pm.newQuery(Person.class, "firstName == 'Mickey'");
        Query query = q.getInternalQuery();
        ClassLoaderResolver clr = query.getExecutionContext().getClassLoaderResolver();
        JavaQueryCompiler compiler = new JDOQLCompiler(query.getExecutionContext().getNucleusContext(), clr, null, query.getCandidateClass(), null, query.getFilter(), query.getParsedImports(), query.getOrdering(), query.getResult(), query.getGrouping(), query.getHaving(), query.getExplicitParametersDeclaration(), query.getExplicitVariablesDeclaration(), null);
        QueryCompilation compilation = compiler.compile(new HashMap(), null);
        // Execute the query
        JavaQueryInMemoryEvaluator eval = new JDOQLInMemoryEvaluator(query, instances, compilation, null, clr);
        List results = (List) eval.execute(true, true, true, true, true);
        assertEquals("Number of result instances was wrong", 1, results.size());
        Person p = (Person) results.get(0);
        assertEquals("Result instance has wrong first name", "Mickey", p.getFirstName());
        assertEquals("Result instance has wrong last name", "Mouse", p.getLastName());
        assertEquals("Person number of result instance is wrong", 101, p.getPersonNum());
        // Compile the query
        q = (JDOQuery) pm.newQuery(Person.class, "lastName != 'Mouse'");
        query = q.getInternalQuery();
        clr = query.getExecutionContext().getClassLoaderResolver();
        compiler = new JDOQLCompiler(query.getExecutionContext().getNucleusContext(), clr, null, query.getCandidateClass(), null, query.getFilter(), query.getParsedImports(), query.getOrdering(), query.getResult(), query.getGrouping(), query.getHaving(), query.getExplicitParametersDeclaration(), query.getExplicitVariablesDeclaration(), null);
        compilation = compiler.compile(new HashMap(), null);
        // Execute the query
        eval = new JDOQLInMemoryEvaluator(query, instances, compilation, null, clr);
        results = (List) eval.execute(true, true, true, true, true);
        assertEquals("Number of result instances was wrong", 1, results.size());
        p = (Person) results.get(0);
        assertEquals("Result instance has wrong first name", "Donald", p.getFirstName());
        assertEquals("Result instance has wrong last name", "Duck", p.getLastName());
        assertEquals("Person number of result instance is wrong", 102, p.getPersonNum());
        tx.commit();
    } catch (Exception e) {
        e.printStackTrace();
        fail("Exception thrown during query execution " + e.getMessage());
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
}
Also used : JDOQLCompiler(org.datanucleus.query.compiler.JDOQLCompiler) Query(org.datanucleus.store.query.Query) JDOQuery(org.datanucleus.api.jdo.JDOQuery) PersistenceManager(javax.jdo.PersistenceManager) HashMap(java.util.HashMap) JavaQueryInMemoryEvaluator(org.datanucleus.query.inmemory.JavaQueryInMemoryEvaluator) ArrayList(java.util.ArrayList) ClassLoaderResolver(org.datanucleus.ClassLoaderResolver) JDOQLInMemoryEvaluator(org.datanucleus.query.inmemory.JDOQLInMemoryEvaluator) JDOQuery(org.datanucleus.api.jdo.JDOQuery) JavaQueryCompiler(org.datanucleus.query.compiler.JavaQueryCompiler) Transaction(javax.jdo.Transaction) ArrayList(java.util.ArrayList) List(java.util.List) QueryCompilation(org.datanucleus.query.compiler.QueryCompilation) Person(org.jpox.samples.models.company.Person)

Example 93 with Person

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

the class JDOQLInMemoryTest method testStringToUpperCaseStartsWith.

/**
 * Test for JDOQL chained String methods, using toUpperCase().startsWith().
 */
public void testStringToUpperCaseStartsWith() {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        Query q = pm.newQuery(getQueryLanguage(), "SELECT FROM " + Person.class.getName() + " WHERE firstName.toUpperCase().startsWith('BU')");
        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.toUpperCase().startsWith(p1)");
        c = (Collection) q.execute("BU");
        assertEquals(1, c.size());
        Iterator it = c.iterator();
        assertEquals("Bugs", ((Person) it.next()).getFirstName());
        tx.commit();
    } catch (Exception e) {
        LOG.error("Exception during 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) 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 94 with Person

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

the class JDOQLInMemoryTest method setUp.

protected void setUp() throws Exception {
    super.setUp();
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        Person p = new Person();
        p.setPersonNum(4);
        p.setGlobalNum("4");
        p.setFirstName("Bugs");
        p.setLastName("Bunny");
        pm.makePersistent(p);
        id[0] = pm.getObjectId(p);
        p = new Person();
        p.setPersonNum(5);
        p.setGlobalNum("5");
        p.setFirstName("Ana");
        p.setLastName("Hick");
        pm.makePersistent(p);
        id[1] = pm.getObjectId(p);
        p = new Person();
        p.setPersonNum(3);
        p.setGlobalNum("3");
        p.setFirstName("Lami");
        p.setLastName("Puxa");
        pm.makePersistent(p);
        id[2] = pm.getObjectId(p);
        tx.commit();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) Person(org.jpox.samples.models.company.Person)

Example 95 with Person

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

the class ApplicationIdPersistenceTest method testInsertThenUpdateDifferentPMs.

public void testInsertThenUpdateDifferentPMs() throws Exception {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            Person p = new Person();
            p.setPersonNum(1);
            p.setGlobalNum("1");
            p.setFirstName("Bugs");
            p.setLastName("Bunny");
            Person p2 = new Person();
            p2.setPersonNum(2);
            p2.setGlobalNum("2");
            p2.setFirstName("My");
            p2.setLastName("Friend");
            Person p3 = new Person();
            p3.setPersonNum(3);
            p3.setGlobalNum("3");
            p3.setFirstName("Daffy");
            p3.setLastName("Duck");
            pm.makePersistent(p);
            pm.makePersistent(p2);
            pm.makePersistent(p3);
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception when persisting 3 Person objects", e);
            fail("Exception thrown when persisting in PM : " + e.getMessage());
            return;
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        for (int i = 0; i < 3; i++) {
            PersistenceManager pm2 = pmf.getPersistenceManager();
            Transaction tx2 = pm2.currentTransaction();
            try {
                tx2.begin();
                Query q = pm2.newQuery("SELECT FROM " + Person.class.getName() + " WHERE this.lastName != \"Rabbit\"");
                List<Person> people = (List<Person>) q.execute();
                Person p = people.iterator().next();
                p.setLastName("Rabbit");
                tx2.commit();
            } catch (Exception e) {
                LOG.error("Exception when querying and updating Person objects", e);
                fail("Exception thrown when updating in PM2 : " + e.getMessage());
                return;
            } finally {
                if (tx2.isActive()) {
                    tx2.rollback();
                }
                pm2.close();
            }
        }
    } finally {
        clean(Person.class);
    }
}
Also used : Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) List(java.util.List) Person(org.jpox.samples.models.company.Person) JDOOptimisticVerificationException(javax.jdo.JDOOptimisticVerificationException)

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