Search in sources :

Example 66 with Person

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

the class JDOQLBasicTest method testStringEqualsIgnoreCase.

public void testStringEqualsIgnoreCase() {
    try {
        Person p1 = new Person(1, "Bugs", "Bunny", "bugs.bunny@warnerbros.com");
        p1.setAge(27);
        Person p2 = new Person(2, "Road", "Runner", "road.runner@warnerbros.com");
        p2.setAge(28);
        Person p3 = new Person(3, "Bart", "Simpson", "bart.simpson@simpsons.com");
        p3.setAge(28);
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            pm.makePersistent(p1);
            pm.makePersistent(p2);
            pm.makePersistent(p3);
            tx.commit();
            tx.begin();
            // field.equalsIgnoreCase(param)
            Query q1 = pm.newQuery("SELECT FROM " + Person.class.getName() + " WHERE firstName.equalsIgnoreCase(:name)");
            Collection c1 = (Collection) q1.execute("Road");
            assertEquals(1, c1.size());
            assertEquals("Road", ((Person) c1.iterator().next()).getFirstName());
            c1 = (Collection) q1.execute("Bugs");
            assertEquals(1, c1.size());
            assertEquals("Bugs", ((Person) c1.iterator().next()).getFirstName());
            // param.equalsIgnoreCase(field)
            Query q2 = pm.newQuery("SELECT FROM " + Person.class.getName() + " WHERE :name.equalsIgnoreCase(firstName)");
            Collection c2 = (Collection) q2.execute("Road");
            assertEquals(1, c2.size());
            assertEquals("Road", ((Person) c2.iterator().next()).getFirstName());
            c2 = (Collection) q2.execute("Bugs");
            assertEquals(1, c2.size());
            assertEquals("Bugs", ((Person) c2.iterator().next()).getFirstName());
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        clean(Person.class);
    }
}
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)

Example 67 with Person

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

the class JDOQLContainerTest method testContainsOfPrimitive.

/**
 * Tests contains() of a primitive in a Collection<Object wrapper>.
 */
public void testContainsOfPrimitive() {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        Person per1 = new Person(1, "Harvey", "Hacker", "harvey.hacker@acme.com");
        per1.setAge(35);
        Person per2 = new Person(2, "Geoffrey", "Gimp", "geoffrey.gimp@acme.com");
        per2.setAge(37);
        pm.makePersistentAll(new Object[] { per1, per2 });
        pm.flush();
        Collection<Integer> ages = new HashSet();
        ages.add(new Integer(35));
        Query q = pm.newQuery(Person.class, ":param.contains(age)");
        Collection c = (Collection) q.execute(ages);
        assertEquals(1, c.size());
        Person per = (Person) c.iterator().next();
        assertEquals("Wrong person", "Harvey", per.getFirstName());
        assertEquals("Wrong person", "Hacker", per.getLastName());
        tx.rollback();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
        clean(Person.class);
    }
}
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) HashSet(java.util.HashSet)

Example 68 with Person

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

the class JDOQLResultTest method testNewObjectInResult.

/**
 * Test case to use the JDO 2.0 setResultClass().
 */
public void testNewObjectInResult() {
    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");
            p2.setAge(37);
            pm.makePersistent(p1);
            pm.makePersistent(p2);
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
        }
        // Query using PersonHolder result output
        try {
            tx.begin();
            // check with default constructor
            Query q = pm.newQuery(pm.getExtent(Person.class, false));
            q.setFilter("age > 35");
            q.setResult("new org.jpox.samples.models.company.PersonHolder()");
            List results = (List) q.execute();
            assertEquals(1, results.size());
            Iterator resultsIter = results.iterator();
            if (resultsIter.hasNext()) {
                Object obj = resultsIter.next();
                assertTrue("ResultClass of query is incorrect. Should have been " + PersonHolder.class.getName() + " but is " + obj.getClass().getName(), PersonHolder.class == obj.getClass());
                PersonHolder holder = (PersonHolder) obj;
                assertNull(holder.getPerson1());
            }
            q.closeAll();
            // check with simple argument
            q = pm.newQuery(pm.getExtent(Person.class, false));
            q.setFilter("age > 35");
            q.setResult("new org.jpox.samples.models.company.PersonHolder(this)");
            results = (List) q.execute();
            assertEquals(1, results.size());
            resultsIter = results.iterator();
            if (resultsIter.hasNext()) {
                Object obj = resultsIter.next();
                assertTrue("ResultClass of query is incorrect. Should have been " + PersonHolder.class.getName() + " but is " + obj.getClass().getName(), PersonHolder.class == obj.getClass());
                PersonHolder holder = (PersonHolder) obj;
                assertNotNull(holder.getPerson1());
                assertNull(holder.getPerson2());
            }
            q.closeAll();
            // check multiple args and with a literal null
            q = pm.newQuery(pm.getExtent(Person.class, false));
            q.setFilter("age > 35");
            q.setResult("new org.jpox.samples.models.company.PersonHolder(this,null,this.firstName)");
            results = (List) q.execute();
            assertEquals(1, results.size());
            resultsIter = results.iterator();
            if (resultsIter.hasNext()) {
                Object obj = resultsIter.next();
                assertTrue("ResultClass of query is incorrect. Should have been " + PersonHolder.class.getName() + " but is " + obj.getClass().getName(), PersonHolder.class == obj.getClass());
                PersonHolder holder = (PersonHolder) obj;
                assertNotNull(holder.getPerson1());
                assertNull(holder.getPerson2());
                assertEquals("Barney", holder.getFirstName().trim());
            }
            q.closeAll();
            // add more literals
            q = pm.newQuery(pm.getExtent(Person.class, false));
            q.setFilter("age > 35");
            q.setResult("new org.jpox.samples.models.company.PersonHolder(this,null,this.firstName,'string',1)");
            results = (List) q.execute();
            assertEquals(1, results.size());
            resultsIter = results.iterator();
            if (resultsIter.hasNext()) {
                Object obj = resultsIter.next();
                assertTrue("ResultClass of query is incorrect. Should have been " + PersonHolder.class.getName() + " but is " + obj.getClass().getName(), PersonHolder.class == obj.getClass());
                PersonHolder holder = (PersonHolder) obj;
                assertNotNull(holder.getPerson1());
                assertNull(holder.getPerson2());
                assertEquals("Barney", holder.getFirstName().trim());
                assertEquals("string", holder.getLiteralString());
                assertEquals(1, holder.getLiteralInt());
            }
            q.closeAll();
            // make sure if objects after literal, they work
            q = pm.newQuery(pm.getExtent(Person.class, false));
            q.setFilter("age > 35");
            q.setResult("new org.jpox.samples.models.company.PersonHolder(this,null,this.firstName,'string',1,this)");
            results = (List) q.execute();
            assertEquals(1, results.size());
            resultsIter = results.iterator();
            if (resultsIter.hasNext()) {
                Object obj = resultsIter.next();
                assertTrue("ResultClass of query is incorrect. Should have been " + PersonHolder.class.getName() + " but is " + obj.getClass().getName(), PersonHolder.class == obj.getClass());
                PersonHolder holder = (PersonHolder) obj;
                assertNotNull(holder.getPerson1());
                assertNull(holder.getPerson2());
                assertEquals("Barney", holder.getFirstName().trim());
                assertEquals("string", holder.getLiteralString());
                assertEquals(1, holder.getLiteralInt());
            }
            q.closeAll();
            // test new Object(new Object())
            q = pm.newQuery(pm.getExtent(Person.class, false));
            q.setFilter("age > 35");
            q.setResult("new org.jpox.samples.models.company.PersonHolder(new org.jpox.samples.models.company.PersonHolder(new org.jpox.samples.models.company.PersonHolder(this),this),this)");
            results = (List) q.execute();
            assertEquals(1, results.size());
            resultsIter = results.iterator();
            if (resultsIter.hasNext()) {
                Object obj = resultsIter.next();
                assertTrue("ResultClass of query is incorrect. Should have been " + PersonHolder.class.getName() + " but is " + obj.getClass().getName(), PersonHolder.class == obj.getClass());
                PersonHolder holder = (PersonHolder) obj;
                assertNotNull(holder.getPerson1());
                assertNotNull(holder.getPerson2());
            }
            q.closeAll();
            // test new Object(new Object())
            q = pm.newQuery(pm.getExtent(Person.class, false));
            q.setFilter("age > 35");
            q.setResult("this, 0, new org.jpox.samples.models.company.PersonHolder(new org.jpox.samples.models.company.PersonHolder(new org.jpox.samples.models.company.PersonHolder(this),this),this), 1, null");
            results = (List) q.execute();
            assertEquals(1, results.size());
            resultsIter = results.iterator();
            if (resultsIter.hasNext()) {
                Object[] obj = (Object[]) resultsIter.next();
                assertTrue("ResultClass of query is incorrect. Should have been " + PersonHolder.class.getName() + " but is " + obj[2].getClass().getName(), PersonHolder.class == obj[2].getClass());
                PersonHolder holder = (PersonHolder) obj[2];
                assertNotNull(holder.getPerson1());
                assertNotNull(holder.getPerson2());
                assertNotNull(obj[0]);
                assertEquals(obj[0], holder.getPerson1());
                assertEquals(0, ((Long) obj[1]).intValue());
                assertNotNull(obj[3]);
                assertEquals(1, ((Long) obj[3]).intValue());
                assertNull(obj[4]);
            }
            q.closeAll();
            tx.commit();
        } catch (JDOException ex) {
            LOG.error("Exception in query", ex);
            fail(ex.getMessage());
        } catch (Exception e) {
            LOG.error("Exception caught", e);
            fail(e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        // Clean out our data
        clean(Person.class);
    }
}
Also used : Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) ListIterator(java.util.ListIterator) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) Person(org.jpox.samples.models.company.Person) PersonHolder(org.jpox.samples.models.company.PersonHolder) JDOException(javax.jdo.JDOException) JDOUserException(javax.jdo.JDOUserException) JDOException(javax.jdo.JDOException)

Example 69 with Person

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

the class JDOQLResultTest method testOrdering.

/**
 * Test of the ordering clause of a JDOQL statement.
 */
public void testOrdering() {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            // Try some ordering using basic fields
            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].setShortField((short) (i + 11));
                basics[i].setIntField(20040101);
                basics[i].setBooleanObjectField(new Boolean(i % 2 == 0));
                basics[i].setCharField('0');
            }
            pm.makePersistentAll(basics);
            StringHolder[] strings = new StringHolder[5];
            for (int i = 0; i < strings.length; i++) {
                strings[i] = new StringHolder();
            }
            strings[0].setNormalString("Aaa");
            strings[1].setNormalString("bBB");
            strings[2].setNormalString("ddd");
            strings[3].setNormalString("Ccc");
            strings[4].setNormalString("eEe");
            pm.makePersistentAll(strings);
            pm.flush();
            Query q = pm.newQuery(BasicTypeHolder.class);
            q.setOrdering("longField ascending");
            List l = (List) q.execute();
            Assert.assertEquals(5, l.size());
            q = pm.newQuery(BasicTypeHolder.class);
            q.setOrdering("longField - shortField ascending");
            l = (List) q.execute();
            Assert.assertEquals(5, l.size());
            q = pm.newQuery(BasicTypeHolder.class);
            q.setOrdering("(longField - shortField) ascending");
            l = (List) q.execute();
            Assert.assertEquals(5, l.size());
            q = pm.newQuery(BasicTypeHolder.class);
            q.setOrdering("Math.abs(-longField) ascending");
            l = (List) q.execute();
            Assert.assertEquals(5, l.size());
            Assert.assertEquals(1, ((BasicTypeHolder) l.get(0)).getLongField());
            Assert.assertEquals(2, ((BasicTypeHolder) l.get(1)).getLongField());
            Assert.assertEquals(3, ((BasicTypeHolder) l.get(2)).getLongField());
            Assert.assertEquals(4, ((BasicTypeHolder) l.get(3)).getLongField());
            Assert.assertEquals(5, ((BasicTypeHolder) l.get(4)).getLongField());
            // Query our 5 StringHolders and order by uppercase(normalString)
            q = pm.newQuery(StringHolder.class);
            q.setOrdering("normalString.toUpperCase() ascending");
            l = (List) q.execute();
            assertEquals("The number of returned elements is incorrect", 5, l.size());
            assertEquals("The first Primitive is incorrectly ordered", "Aaa", ((StringHolder) l.get(0)).getNormalString());
            assertEquals("The second Primitive is incorrectly ordered", "bBB", ((StringHolder) l.get(1)).getNormalString());
            assertEquals("The third Primitive is incorrectly ordered", "Ccc", ((StringHolder) l.get(2)).getNormalString());
            assertEquals("The fourth Primitive is incorrectly ordered", "ddd", ((StringHolder) l.get(3)).getNormalString());
            assertEquals("The fifth Primitive is incorrectly ordered", "eEe", ((StringHolder) l.get(4)).getNormalString());
            // Composite ordering
            q = pm.newQuery(BasicTypeHolder.class);
            q.setOrdering("(longField - shortField) ascending , this.longField  ascending");
            l = (List) q.execute();
            Assert.assertEquals(5, l.size());
            q = pm.newQuery(BasicTypeHolder.class);
            q.setOrdering("this.booleanObjField ascending");
            l = (List) q.execute();
            Assert.assertEquals(5, l.size());
            // Don't actually create the instances
            tx.rollback();
            // Try some ordering using self-referencing relations
            tx.begin();
            Person p1 = new Person(1, "Bart", "Simpson", "bart@simpsons.com");
            Person p2 = new Person(2, "Homer", "Simpson", "homer@simpsons.com");
            Person p3 = new Person(3, "Lisa", "Simpson", "lisa@simpsons.com");
            Person p4 = new Person(4, "Marge", "Simpson", "marge@simpsons.com");
            Person p5 = new Person(5, "Maggie", "Simpson", "maggie@simpsons.com");
            Person p6 = new Person(6, "Moe", "Bartender", "moe@simpsons.com");
            Person p7 = new Person(7, "Deputy", "Dawg", "deputy@cartoons.com");
            Person p8 = new Person(8, "Road", "Runner", "maggie@cartoons.com");
            Person p9 = new Person(9, "Donald", "Duck", "moe@cartoons.com");
            p4.setBestFriend(p2);
            p5.setBestFriend(p3);
            p6.setBestFriend(p1);
            pm.makePersistentAll(new Object[] { p4, p5, p6, p7, p8, p9 });
            pm.flush();
            q = pm.newQuery(Person.class);
            q.setOrdering("bestFriend.firstName ascending NULLS FIRST");
            l = (List) q.execute();
            // 9 Person objects so 9 results - 6 nulls, and then 3 results
            Assert.assertEquals(9, l.size());
            Iterator it = l.iterator();
            Person p = (Person) it.next();
            assertTrue(p.getBestFriend() == null);
            p = (Person) it.next();
            assertTrue(p.getBestFriend() == null);
            p = (Person) it.next();
            assertTrue(p.getBestFriend() == null);
            p = (Person) it.next();
            assertTrue(p.getBestFriend() == null);
            p = (Person) it.next();
            assertTrue(p.getBestFriend() == null);
            p = (Person) it.next();
            assertTrue(p.getBestFriend() == null);
            p = (Person) it.next();
            assertTrue(p.getBestFriend().getFirstName().equals("Bart"));
            assertTrue(p.getFirstName().equals("Moe"));
            p = (Person) it.next();
            assertTrue(p.getBestFriend().getFirstName().equals("Homer"));
            assertTrue(p.getFirstName().equals("Marge"));
            p = (Person) it.next();
            assertTrue(p.getBestFriend().getFirstName().equals("Lisa"));
            assertTrue(p.getFirstName().equals("Maggie"));
            // Don't actually create the instances
            tx.rollback();
        } catch (Exception e) {
            LOG.error("Exception in query", e);
            fail("Exception thrown by query " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        clean(BasicTypeHolder.class);
    }
}
Also used : Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) BasicTypeHolder(org.jpox.samples.types.basic.BasicTypeHolder) JDOException(javax.jdo.JDOException) JDOUserException(javax.jdo.JDOUserException) StringHolder(org.jpox.samples.types.basic.StringHolder) 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)

Example 70 with Person

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

the class PersistenceManagerFactoryTest method testSerialize.

public void testSerialize() {
    try {
        Object id = null;
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            Person p = new Person(101, "Fred", "Flintstone", "fred.flintstone@warnerbros.com");
            pm.makePersistent(p);
            tx.commit();
            id = pm.getObjectId(p);
        } catch (Exception e) {
            LOG.error("Exception persisting object", e);
            fail("Exception persisting object : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // Serialize the current PMF
        byte[] bytes = null;
        try {
            bytes = TestHelper.serializeObject(pmf);
        } catch (RuntimeException re) {
            LOG.error("Error serializing PMF", re);
            fail("Error in serialization : " + re.getMessage());
        }
        // Deserialise the PMF
        JDOPersistenceManagerFactory pmf = null;
        try {
            pmf = (JDOPersistenceManagerFactory) TestHelper.deserializeObject(bytes);
        } catch (RuntimeException re) {
            LOG.error("Error deserializing PMF", re);
            fail("Error in deserialization : " + re.getMessage());
        }
        assertNotNull(pmf);
        assertNotNull(pmf.getNucleusContext());
        // Retrieve the object using the deserialised PM(F)
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            Person p = (Person) pm.getObjectById(id);
            assertNotNull(p);
            assertEquals("Fred", p.getFirstName());
            assertEquals("Flintstone", p.getLastName());
            assertEquals("fred.flintstone@warnerbros.com", p.getEmailAddress());
            Person p2 = new Person(102, "Barney", "Rubble", "barney.rubble@warnerbros.com");
            pm.makePersistent(p2);
            pm.flush();
            tx.rollback();
        } catch (Exception e) {
            LOG.error("Exception retrieving object with deserialised PMF", e);
            fail("Exception retrieving object with deserialised PMF: " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        clean(Person.class);
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) JDOPersistenceManagerFactory(org.datanucleus.api.jdo.JDOPersistenceManagerFactory) Person(org.jpox.samples.models.company.Person) JDOException(javax.jdo.JDOException) JDOUserException(javax.jdo.JDOUserException) JDOFatalUserException(javax.jdo.JDOFatalUserException)

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