Search in sources :

Example 6 with Employee

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

the class JDOQLTest method testFilterNotNull.

public void testFilterNotNull() throws Exception {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            Employee e = new Employee();
            e.setFirstName("Barney");
            e.setLastName("Rubble");
            e.setPersonNum(103);
            e.setGlobalNum("103");
            e.setSalary(124.50f);
            pm.makePersistent(e);
            pm.flush();
            // Query using not equality operator
            Query q1 = pm.newQuery("SELECT FROM " + Employee.class.getName() + " WHERE this.firstName != null");
            List<Employee> results1 = (List<Employee>) q1.execute();
            assertEquals(1, results1.size());
            Employee e1 = results1.iterator().next();
            assertEquals("Wrong Employee", "Rubble", e1.getLastName());
            tx.rollback();
        } catch (Exception e) {
            LOG.error("Exception during JDOQL inequality operator test", e);
            fail("Exception thrown when running test " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
    }
}
Also used : Employee(org.jpox.samples.models.company.Employee) Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) List(java.util.List)

Example 7 with Employee

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

the class JDOQLTest method testResult.

public void testResult() throws Exception {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            Person p1 = new Person();
            p1.setPersonNum(1);
            p1.setGlobalNum("1");
            p1.setFirstName("Bugs");
            p1.setLastName("Bunny");
            Person p2 = new Person();
            p2.setPersonNum(2);
            p2.setGlobalNum("2");
            p2.setFirstName("Daffy");
            p2.setLastName("Duck");
            Employee e3 = new Employee();
            e3.setFirstName("Barney");
            e3.setLastName("Rubble");
            e3.setPersonNum(103);
            e3.setGlobalNum("103");
            e3.setSalary(124.50f);
            pm.makePersistent(p1);
            pm.makePersistent(p2);
            pm.makePersistent(e3);
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception during persist", e);
            fail("Exception thrown when running test " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        pmf.getDataStoreCache().evictAll();
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            Query q2 = pm.newQuery("SELECT this.firstName, this.lastName FROM " + Person.class.getName());
            List<Object[]> results2 = (List<Object[]>) q2.execute();
            assertEquals(3, results2.size());
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception during query", e);
            fail("Exception thrown when running test " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        clean(Employee.class);
        clean(Person.class);
    }
}
Also used : Employee(org.jpox.samples.models.company.Employee) Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) List(java.util.List) Person(org.jpox.samples.models.company.Person)

Example 8 with Employee

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

the class JDOQLTest method testFilterNotEquals.

public void testFilterNotEquals() throws Exception {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            Employee e = new Employee();
            e.setFirstName("Barney");
            e.setLastName("Rubble");
            e.setPersonNum(103);
            e.setGlobalNum("103");
            e.setSalary(124.50f);
            pm.makePersistent(e);
            pm.flush();
            // Query using not equality operator
            Query q1 = pm.newQuery("SELECT FROM " + Employee.class.getName() + " WHERE this.firstName != 'Fred'");
            List<Employee> results1 = (List<Employee>) q1.execute();
            assertEquals(1, results1.size());
            Employee e1 = results1.iterator().next();
            assertEquals("Wrong Employee", "Rubble", e1.getLastName());
            tx.rollback();
        } catch (Exception e) {
            LOG.error("Exception during JDOQL equality operator test", e);
            fail("Exception thrown when running test " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
    }
}
Also used : Employee(org.jpox.samples.models.company.Employee) Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) List(java.util.List)

Example 9 with Employee

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

the class JDOQLTest method testCandidateWithSubclasses.

public void testCandidateWithSubclasses() throws Exception {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            Person p1 = new Person();
            p1.setPersonNum(1);
            p1.setGlobalNum("1");
            p1.setFirstName("Bugs");
            p1.setLastName("Bunny");
            Person p2 = new Person();
            p2.setPersonNum(2);
            p2.setGlobalNum("2");
            p2.setFirstName("Daffy");
            p2.setLastName("Duck");
            Employee e3 = new Employee();
            e3.setFirstName("Barney");
            e3.setLastName("Rubble");
            e3.setPersonNum(103);
            e3.setGlobalNum("103");
            e3.setSalary(124.50f);
            pm.makePersistent(p1);
            pm.makePersistent(p2);
            pm.makePersistent(e3);
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception during persist", e);
            fail("Exception thrown when running test " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        pmf.getDataStoreCache().evictAll();
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            // Check number of objects present
            Query q1 = pm.newQuery(Person.class);
            List<Person> results1 = (List<Person>) q1.execute();
            assertEquals(3, results1.size());
            Iterator<Person> iter = results1.iterator();
            int numEmployees = 0;
            int numPeople = 0;
            while (iter.hasNext()) {
                Person p = iter.next();
                if (p instanceof Employee) {
                    numEmployees++;
                } else {
                    numPeople++;
                }
            }
            assertEquals("Number of Employees wrong", 1, numEmployees);
            assertEquals("Number of Person wrong", 2, numPeople);
            Query q2 = pm.newQuery("SELECT FROM " + Person.class.getName() + " EXCLUDE SUBCLASSES");
            List<Person> results2 = (List<Person>) q2.execute();
            assertEquals(2, results2.size());
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception during 1-N retrieve and check", e);
            fail("Exception thrown when running test " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        clean(Employee.class);
        clean(Person.class);
    }
}
Also used : Employee(org.jpox.samples.models.company.Employee) Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) List(java.util.List) Person(org.jpox.samples.models.company.Person)

Example 10 with Employee

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

the class ReachabilityTest method testDeepReachabilityByClass.

/**
 * Tests if a BaseItem is reachable through a BaseContainer by interface
 * field and does have the correct states at different times.
 */
/*public void testSimpleReachabilityOnOptimisticTxsDatastoreAttributedId()
    {
        try
        {
            PersistenceManager pm = pmf.getPersistenceManager();
            Transaction tx = pm.currentTransaction();
            try
            {
                tx.setOptimistic(true);
                tx.begin();
                //test with datastore attributed ids
                Rate rate0 = new Rate();
                Currency usd0 = new Currency("USD0");
                pm.makePersistent(rate0);
                pm.makePersistent(usd0);
                tx.commit();
                tx.begin();
                rate0.setTarget(usd0);
                //test with datastore attributed ids
                Rate rate1 = new Rate();
                Currency usd1 = new Currency("USD1");
                rate1.setTarget(usd1);
                pm.makePersistent(rate1);
                tx.commit();

                assertTrue("reachable persistent rate: isPersistent() == false", JDOHelper.isPersistent(rate1));
                assertTrue("reachable persistent usd1: isPersistent() == false", JDOHelper.isPersistent(usd1));
            }
            finally
            {
                if (tx.isActive())
                {
                    tx.rollback();
                }
                pm.close();
            }
        }
        finally
        {
            // Clean out our data
            PersistenceManager pm = pmf.getPersistenceManager();
            Transaction tx = pm.currentTransaction();
            try
            {
                tx.begin();
                Extent ex = pm.getExtent(Rate.class);
                Iterator iter = ex.iterator();
                while (iter.hasNext())
                {
                    Rate rate = (Rate)iter.next();
                    if (rate.getSource() != null)
                    {
                        Currency source = rate.getSource();
                        rate.setSource(null);
                        source.setRates(null);
                    }
                    if (rate.getTarget() != null)
                    {
                        rate.setTarget(null);
                    }
                }
                tx.commit();
            }
            finally
            {
                if (tx.isActive())
                {
                    tx.rollback();
                }
                pm.close();
            }
            clean(Rate.class);
            clean(Currency.class);
        }
    }*/
/**
 * Test if a Person is reachable through an Employee through a Manager
 * and that it has the correct state at various times.
 */
public void testDeepReachabilityByClass() {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        Object per1Id = null;
        Object emp1Id = null;
        Object mgr1Id = null;
        try {
            tx.begin();
            Employee emp1 = new Employee(101, "Barney", "Rubble", "barney.rubble@jpox.com", (float) 123.45, "12346");
            Manager mgr1 = new Manager(102, "Fred", "Flintstone", "fred.flintstone@jpox.com", (float) 240.00, "12348");
            Person per1 = new Person(103, "Rob", "Rock", "rob.rock@yahoo.com");
            assertFalse("newly created Person: isPersistent() == true", JDOHelper.isPersistent(per1));
            assertFalse("newly created Person: isNew() == true", JDOHelper.isNew(per1));
            assertFalse("newly created Person: isDirty() == true", JDOHelper.isDirty(per1));
            // persist chain of objects Manager -> Employee -> Person
            emp1.setBestFriend(per1);
            mgr1.addSubordinate(emp1);
            emp1.setManager(mgr1);
            pm.makePersistent(mgr1);
            assertTrue("reachable persistent Person: isPersistent() == false", JDOHelper.isPersistent(per1));
            assertTrue("reachable persistent Person: isNew() == false", JDOHelper.isNew(per1));
            assertTrue("reachable persistent Person: isDirty() == false", JDOHelper.isDirty(per1));
            tx.commit();
            // assert Person is now persistent clean or hollow
            assertTrue("committed Person: isPersistent() == false", JDOHelper.isPersistent(per1));
            assertFalse("committed Person: isNew() == true", JDOHelper.isNew(per1));
            assertFalse("committed Person: isDirty() == true", JDOHelper.isDirty(per1));
            per1Id = JDOHelper.getObjectId(per1);
            emp1Id = JDOHelper.getObjectId(emp1);
            mgr1Id = JDOHelper.getObjectId(mgr1);
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            // assert the DB contains the correct data
            tx.begin();
            Person checkPer1 = (Person) pm.getObjectById(per1Id);
            assertTrue("Person not in database", checkPer1 != null);
            Employee checkEmp1 = (Employee) pm.getObjectById(emp1Id);
            assertTrue("Employee not in database", checkEmp1 != null);
            Manager checkMgr1 = (Manager) pm.getObjectById(mgr1Id);
            assertTrue("Manager not in database", checkMgr1 != null);
            assertSame("Employee by query not the same as Employee by navigation", checkEmp1, checkMgr1.getSubordinates().iterator().next());
            assertSame("Person by query not the same as Person by navigation", checkPer1, checkEmp1.getBestFriend());
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        CompanyHelper.clearCompanyData(pmf);
    }
}
Also used : Employee(org.jpox.samples.models.company.Employee) Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) Manager(org.jpox.samples.models.company.Manager) PersistenceManager(javax.jdo.PersistenceManager) Person(org.jpox.samples.models.company.Person)

Aggregations

Employee (org.jpox.samples.models.company.Employee)129 PersistenceManager (javax.jdo.PersistenceManager)126 Transaction (javax.jdo.Transaction)119 Query (javax.jdo.Query)68 JDOUserException (javax.jdo.JDOUserException)62 Manager (org.jpox.samples.models.company.Manager)46 List (java.util.List)44 JDOPersistenceManager (org.datanucleus.api.jdo.JDOPersistenceManager)37 JDOObjectNotFoundException (javax.jdo.JDOObjectNotFoundException)24 Person (org.jpox.samples.models.company.Person)24 Collection (java.util.Collection)21 PersistenceManagerFactory (javax.jdo.PersistenceManagerFactory)18 Iterator (java.util.Iterator)17 Properties (java.util.Properties)17 JDODetachedFieldAccessException (javax.jdo.JDODetachedFieldAccessException)16 SQLException (java.sql.SQLException)12 StoreManager (org.datanucleus.store.StoreManager)12 Department (org.jpox.samples.models.company.Department)12 ArrayList (java.util.ArrayList)11 HashSet (java.util.HashSet)11