Search in sources :

Example 1 with Identity2

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

the class JDOQLResultTest method testSetResultClass.

/**
 * Test case to use the JDO 2.0 setResultClass().
 */
public void testSetResultClass() {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            Person p1 = new Person(101, "Fred", "Flintstone", "fred.flintstone@jpox.com");
            p1.setAge(34);
            Person p2 = new Person(102, "Barney", "Rubble", "barney.rubble@jpox.com");
            p1.setAge(37);
            pm.makePersistent(p1);
            pm.makePersistent(p2);
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
        }
        try {
            tx.begin();
            try {
                Query q = pm.newQuery(pm.getExtent(Person.class, false));
                q.setFilter("age > 25");
                q.setResultClass(Object[].class);
                List results = (List) q.execute();
                Iterator resultsIter = results.iterator();
                while (resultsIter.hasNext()) {
                    Object obj = resultsIter.next();
                    assertEquals("ResultClass of query is incorrect.", Object[].class.getName(), obj.getClass().getName());
                }
                q.closeAll();
            } catch (JDOUserException e) {
                fail(e.getMessage());
            }
            // Query using user-defined result output : user class has constructor of right type
            try {
                Query q = pm.newQuery(pm.getExtent(Person.class, false));
                q.setResult("firstName, lastName, age");
                q.setResultClass(Identity1.class);
                List results = (List) q.execute();
                Iterator resultsIter = results.iterator();
                while (resultsIter.hasNext()) {
                    Object obj = resultsIter.next();
                    assertEquals("ResultClass of query is incorrect.", Identity1.class.getName(), obj.getClass().getName());
                }
                q.closeAll();
            } catch (JDOUserException e) {
                fail(e.getMessage());
            }
            // Query using user-defined result output : user class has public fields
            try {
                Query q = pm.newQuery(pm.getExtent(Person.class, false));
                q.setResult("firstName, lastName, age");
                q.setResultClass(Identity2.class);
                List results = (List) q.execute();
                Iterator resultsIter = results.iterator();
                while (resultsIter.hasNext()) {
                    Object obj = resultsIter.next();
                    assertEquals("ResultClass of query is incorrect.", Identity2.class.getName(), obj.getClass().getName());
                }
                q.closeAll();
            } catch (JDOUserException e) {
                fail(e.getMessage());
            }
            // Query using user-defined result output : user class has setters
            try {
                Query q = pm.newQuery(pm.getExtent(Person.class, false));
                q.setResult("firstName, lastName, age");
                q.setResultClass(Identity3.class);
                List results = (List) q.execute();
                Iterator resultsIter = results.iterator();
                while (resultsIter.hasNext()) {
                    Object obj = resultsIter.next();
                    assertEquals("ResultClass of query is incorrect.", Identity3.class.getName(), obj.getClass().getName());
                }
                q.closeAll();
            } catch (JDOUserException e) {
                fail(e.getMessage());
            }
            // Query using user-defined result output : user class has put method
            try {
                Query q = pm.newQuery(pm.getExtent(Person.class, false));
                q.setResult("firstName, lastName, age");
                q.setResultClass(Identity4.class);
                List results = (List) q.execute();
                Iterator resultsIter = results.iterator();
                while (resultsIter.hasNext()) {
                    Object obj = resultsIter.next();
                    assertEquals("ResultClass of query is incorrect.", Identity4.class.getName(), obj.getClass().getName());
                }
                q.closeAll();
            } catch (JDOUserException e) {
                fail(e.getMessage());
            }
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // Query using user-defined result output : invalid result class
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        Query q2 = null;
        try {
            tx.begin();
            q2 = pm.newQuery(pm.getExtent(Person.class, false));
            q2.setResult("firstName, lastName, age");
            q2.setResultClass(Identity5.class);
            List results = (List) q2.execute();
            Iterator resultsIter = results.iterator();
            while (resultsIter.hasNext()) {
                resultsIter.next();
            }
            fail("Query was executed but should have thrown an exception in the result extraction due to invalid ResultClass");
            tx.commit();
        } catch (JDOUserException e) {
            LOG.info("Exception thrown by query", e);
            // Success!
            q2.closeAll();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // Query using simple type result output
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            Query q = pm.newQuery(pm.getExtent(Person.class, false));
            // Field is of type String
            q.setResult("firstName");
            q.setResultClass(String.class);
            List results = (List) q.execute();
            Iterator resultsIter = results.iterator();
            while (resultsIter.hasNext()) {
                Object obj = resultsIter.next();
                assertEquals("ResultClass of query is incorrect.", String.class.getName(), obj.getClass().getName());
            }
            q.closeAll();
            tx.commit();
        } catch (JDOUserException e) {
            fail(e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
        }
        // Query using simple type result output but with too many fields
        try {
            tx.begin();
            Query q = pm.newQuery(pm.getExtent(Person.class, false));
            // Fields of type String, int
            q.setResult("firstName, age");
            q.setResultClass(String.class);
            q.execute();
            fail("ResultClass was specified as String yet 2 fields were returned. An exception should have been thrown");
            q.closeAll();
            tx.commit();
        } catch (JDOUserException e) {
        // Success. JPOX detected the erroneous resultClass spec.
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        // Clean out our data
        clean(Person.class);
    }
}
Also used : Identity4(org.jpox.samples.models.company.Identity4) Identity3(org.jpox.samples.models.company.Identity3) Identity2(org.jpox.samples.models.company.Identity2) Query(javax.jdo.Query) Identity1(org.jpox.samples.models.company.Identity1) PersistenceManager(javax.jdo.PersistenceManager) JDOUserException(javax.jdo.JDOUserException) Transaction(javax.jdo.Transaction) ListIterator(java.util.ListIterator) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) Person(org.jpox.samples.models.company.Person)

Aggregations

ArrayList (java.util.ArrayList)1 Iterator (java.util.Iterator)1 List (java.util.List)1 ListIterator (java.util.ListIterator)1 JDOUserException (javax.jdo.JDOUserException)1 PersistenceManager (javax.jdo.PersistenceManager)1 Query (javax.jdo.Query)1 Transaction (javax.jdo.Transaction)1 Identity1 (org.jpox.samples.models.company.Identity1)1 Identity2 (org.jpox.samples.models.company.Identity2)1 Identity3 (org.jpox.samples.models.company.Identity3)1 Identity4 (org.jpox.samples.models.company.Identity4)1 Person (org.jpox.samples.models.company.Person)1