Search in sources :

Example 1 with Manager

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

the class SQLQueryTest method testSelectStarQuery.

/**
 * Test of the use of SELECT * in a query.
 */
public void testSelectStarQuery() throws Exception {
    try {
        // Persist something to select
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            Manager m1 = new Manager(1, "Barney", "Rubble", "barney.rubble@flintstones.com", 100, "123456");
            pm.makePersistent(m1);
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // Do an SQL query to find the Manager
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            String sqlText = "SELECT * FROM MANAGER";
            Query query = pm.newQuery("javax.jdo.query.SQL", sqlText);
            query.setClass(Manager.class);
            List results = (List) query.execute();
            assertTrue("\"SELECT *\" query returned null, yet should have returned some results", results != null);
            assertEquals("Number of Manager objects retrieved from \"SELECT *\" query was incorrect", 1, results.size());
            Manager mgr = (Manager) results.iterator().next();
            // These will cause further SQL statements to retrieve the fields in the Person/Employee part of the object
            assertEquals("\"SELECT *\" query returned Manager with incorrect first name", "Barney", mgr.getFirstName());
            assertEquals("\"SELECT *\" query returned Manager with incorrect last name", "Rubble", mgr.getLastName());
            assertEquals("\"SELECT *\" query returned Manager with incorrect email", "barney.rubble@flintstones.com", mgr.getEmailAddress());
            tx.commit();
        } catch (Exception e) {
            e.printStackTrace();
            fail("Exception thrown from \"SELECT *\" query : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        // Clean out our data
        CompanyHelper.clearCompanyData(pmf);
    }
}
Also used : Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) List(java.util.List) Manager(org.jpox.samples.models.company.Manager) PersistenceManager(javax.jdo.PersistenceManager) JDOUserException(javax.jdo.JDOUserException) JDODataStoreException(javax.jdo.JDODataStoreException)

Example 2 with Manager

use of org.jpox.samples.models.company.Manager 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)

Example 3 with Manager

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

the class AttachDetachReplicateTest method testMoveAcrossDatastores_company.

/**
 * This is a complex testcase using the classes from org.jpox.samples.models.company.
 * It stores a Manager into datastore 1. This Manager has a mapped-by-Set of
 * his employees and a join-Set of his departments. Hence, this test checks for
 * the behaviour of Sets when copied from one
 */
public void testMoveAcrossDatastores_company() {
    PersistenceManagerFactory pmf2 = getPersistenceManagerFactory2();
    try {
        Transaction tx1 = null;
        Transaction tx2 = null;
        // Create a Manager with two Departments and two Employees and persist it
        // into datastore 1.
        PersistenceManager pm1 = pmf.getPersistenceManager();
        Object managerId = null;
        try {
            tx1 = pm1.currentTransaction();
            tx1.begin();
            Manager ds1_manager = new Manager(1L, "Lucifer", "Satan", "Lucifer.Satan@microsoft.hell", 33666.99f, "jsdkhf8z23");
            Employee ds1_employee1 = new Employee(9593L, "McCreevy", "Charlie", "Charlie.McCreevy@microsoft.hell", 9948.57f, "8967bjjhg", new Integer(94));
            Employee ds1_employee2 = new Employee(8723L, "Gates", "Bill", "Bill.Gates@microsoft.hell", 11835.17f, "3894lknsd", new Integer(42));
            Department ds1_department1 = new Department("Brainwashing");
            ds1_department1.setManager(ds1_manager);
            ds1_manager.addDepartment(ds1_department1);
            // TODO Change this to be an inherited type to show up an error
            Department ds1_department2 = new Department("Torture");
            ds1_department2.setManager(ds1_manager);
            ds1_manager.addDepartment(ds1_department2);
            ds1_employee1.setManager(ds1_manager);
            ds1_manager.addSubordinate(ds1_employee1);
            ds1_employee2.setManager(ds1_manager);
            ds1_manager.addSubordinate(ds1_employee2);
            try {
                pm1.makePersistent(ds1_manager);
            } catch (Exception x) {
                LOG.error("Persisting Manager with Departments and Employees into datastore1 failed!", x);
                fail("Persisting Manager with Departments and Employees into datastore1 failed: " + x.getMessage());
            }
            tx1.commit();
            managerId = JDOHelper.getObjectId(ds1_manager);
        } finally {
            if (tx1 != null && tx1.isActive()) {
                tx1.rollback();
            }
            pm1.close();
        }
        // Detach the Manager (with FetchPlan.ALL) from datastore 1.
        Manager detached_manager = null;
        pm1 = pmf.getPersistenceManager();
        try {
            tx1 = pm1.currentTransaction();
            tx1.begin();
            pm1.getFetchPlan().setGroup(FetchPlan.ALL);
            pm1.getFetchPlan().setMaxFetchDepth(-1);
            try {
                Manager ds1_manager = (Manager) pm1.getObjectById(managerId);
                detached_manager = (Manager) pm1.detachCopy(ds1_manager);
            } catch (Exception x) {
                LOG.error("Loading and detaching Manager from datastore1 failed!", x);
                fail("Loading and detaching Manager from datastore1 failed: " + x.getMessage());
            }
            tx1.commit();
        } finally {
            if (tx1 != null && tx1.isActive()) {
                tx1.rollback();
            }
            pm1.close();
        }
        // check, whether the detached data equals the original data
        testMoveAcrossDatastores_company_check("makePersistent or detachCopy (with datastore1) has corrupted data: ", detached_manager, true);
        // put the detached manager into datastore2 using makePersistent
        PersistenceManager pm2 = pmf2.getPersistenceManager();
        try {
            tx2 = pm2.currentTransaction();
            tx2.begin();
            try {
                pm2.makePersistent(detached_manager);
            } catch (Exception x) {
                LOG.error("makePersistent failed on datastore2!", x);
                fail("makePersistent failed on datastore2: " + x.getMessage());
            }
            tx2.commit();
        } finally {
            if (tx2 != null && tx2.isActive()) {
                tx2.rollback();
            }
            pm2.close();
        }
        // load the manager from datastore2 and check whether data is still correct
        pm2 = pmf2.getPersistenceManager();
        try {
            tx2 = pm2.currentTransaction();
            tx2.begin();
            Manager ds2_manager = null;
            try {
                Extent ex = pm2.getExtent(Manager.class);
                Iterator exIter = ex.iterator();
                ds2_manager = (Manager) exIter.next();
                if (exIter.hasNext()) {
                    fail("Returned more than 1 Manager object in second datastore when should only have 1");
                }
            } catch (Exception e) {
                e.printStackTrace();
                LOG.error("Loading Manager from datastore2 failed!", e);
                fail("Loading Manager from datastore2 failed: " + e.getMessage());
            }
            testMoveAcrossDatastores_company_check("makePersistent on datastore2 has corrupted data: ", ds2_manager, false);
            tx2.commit();
        } finally {
            if (tx2 != null && tx2.isActive()) {
                tx2.rollback();
            }
            pm2.close();
        }
    } finally {
        // Clean out our data
        PersistenceManagerFactory[] pmfs = new PersistenceManagerFactory[] { pmf, pmf2 };
        for (int i = 0; i < pmfs.length; ++i) {
            PersistenceManagerFactory pmf = pmfs[i];
            PersistenceManager pm = pmf.getPersistenceManager();
            Transaction tx = pm.currentTransaction();
            tx.begin();
            for (Iterator it = pm.getExtent(Employee.class, false).iterator(); it.hasNext(); ) {
                Employee e = (Employee) it.next();
                e.setManager(null);
            }
            tx.commit();
            tx.begin();
            for (Iterator it = pm.getExtent(Department.class, false).iterator(); it.hasNext(); ) {
                Department d = (Department) it.next();
                d.setManager(null);
            }
            tx.commit();
            pm.close();
            clean(pmf, Manager.class);
            clean(pmf, Employee.class);
            clean(pmf, Department.class);
        }
    }
}
Also used : PersistenceManager(javax.jdo.PersistenceManager) Extent(javax.jdo.Extent) PersistenceManagerFactory(javax.jdo.PersistenceManagerFactory) Manager(org.jpox.samples.models.company.Manager) PersistenceManager(javax.jdo.PersistenceManager) JDOException(javax.jdo.JDOException) JDOUserException(javax.jdo.JDOUserException) Department(org.jpox.samples.models.company.Department) Employee(org.jpox.samples.models.company.Employee) Transaction(javax.jdo.Transaction) Iterator(java.util.Iterator)

Example 4 with Manager

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

the class AttachDetachTest method testDetachLoadUnloadFields.

/**
 * Test of DETACH_LOAD_FIELDS, DETACH_UNLOAD_FIELDS flags.
 */
public void testDetachLoadUnloadFields() {
    try {
        Manager detachedM1a = null;
        Manager detachedM1b = null;
        // Persist some data
        PersistenceManager pm = newPM();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            Employee e1 = new Employee(1, "Yogi", "Bear", "yogi@warnerbros.com", 124, "10123");
            Employee e2 = new Employee(2, "Fred", "Flintstone", "fred.flintstone@hannabarbara.com", 167, "10019");
            Manager m1 = new Manager(3, "Wily", "Coyote", "wily.coyote@warnerbros.com", 202, "10067");
            m1.addSubordinate(e1);
            m1.addSubordinate(e2);
            e1.setManager(m1);
            e2.setManager(m1);
            Department d1 = new Department("Cartoon");
            m1.addDepartment(d1);
            d1.setManager(m1);
            // This should persist all objects
            pm.makePersistent(e1);
            // Detach just the FetchPlan fields
            pm.getFetchPlan().setDetachmentOptions(FetchPlan.DETACH_LOAD_FIELDS | FetchPlan.DETACH_UNLOAD_FIELDS);
            detachedM1a = (Manager) pm.detachCopy(m1);
            pm.getFetchPlan().setDetachmentOptions(FetchPlan.DETACH_LOAD_FIELDS);
            detachedM1b = (Manager) pm.detachCopy(m1);
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception in test", e);
            fail("Exception thrown while persisting test data : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // Check what has been detached - when detaching just fetch plan fields
        try {
            detachedM1a.getEmailAddress();
        } catch (JDODetachedFieldAccessException dfae) {
            fail("Field Manager.emailAddress hasn't been detached yet this should have been since was in fetch-plan");
        }
        try {
            detachedM1a.getSerialNo();
        } catch (JDODetachedFieldAccessException dfae) {
            fail("Field Manager.serialNo hasn't been detached yet this should have been since was in fetch-plan");
        }
        try {
            detachedM1a.getDepartments();
            fail("Field Manager.departments has been detached yet this should not have been since wasn't in fetch-plan");
        } catch (JDODetachedFieldAccessException dfae) {
        // Expected
        }
        try {
            detachedM1a.getSubordinates();
            fail("Field Manager.subordinates has been detached yet this should not have been since wasn't in fetch-plan");
        } catch (JDODetachedFieldAccessException dfae) {
        // Expected
        }
        // Check what has been detached - when detaching all loaded fields
        try {
            detachedM1b.getEmailAddress();
        } catch (JDODetachedFieldAccessException dfae) {
            fail("Field Manager.emailAddress hasn't been detached yet this should have been since was in fetch-plan");
        }
        try {
            detachedM1b.getSerialNo();
        } catch (JDODetachedFieldAccessException dfae) {
            fail("Field Manager.serialNo hasn't been detached yet this should have been since was in fetch-plan");
        }
        try {
            detachedM1b.getDepartments();
        } catch (JDODetachedFieldAccessException dfae) {
            fail("Field Manager.departments hasn't been detached yet this should have been since was loaded at detach");
        }
        try {
            detachedM1b.getSubordinates();
        } catch (JDODetachedFieldAccessException dfae) {
            fail("Field Manager.subordinates hasn't been detached yet this should have been since was loaded at detach");
        }
    } finally {
        CompanyHelper.clearCompanyData(pmf);
    }
}
Also used : JDODetachedFieldAccessException(javax.jdo.JDODetachedFieldAccessException) Department(org.jpox.samples.models.company.Department) Employee(org.jpox.samples.models.company.Employee) Transaction(javax.jdo.Transaction) JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager) Manager(org.jpox.samples.models.company.Manager) JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager) JDODetachedFieldAccessException(javax.jdo.JDODetachedFieldAccessException) JDOUserException(javax.jdo.JDOUserException) JDOObjectNotFoundException(javax.jdo.JDOObjectNotFoundException)

Example 5 with Manager

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

the class AttachDetachTest method testDetachAttach_OneToManyFK.

/**
 * test pc objects aggregating other pcs. associations 1-N FK
 */
public void testDetachAttach_OneToManyFK() {
    try {
        Employee woody = new Employee(1, "Woody", "Woodpecker", "woody@woodpecker.com", 13, "serial 1", new Integer(10));
        Manager bart = new Manager(2, "Bart", "Simpson", "bart@simpson.com", 2, "serial 2");
        Manager boss = new Manager(3, "Boss", "WakesUp", "boss@wakes.up", 4, "serial 3");
        woody.setManager(bart);
        Manager bossDetached;
        Object idBoss;
        PersistenceManager pm = newPM();
        Transaction tx = pm.currentTransaction();
        try {
            // -----------------------------------------------------------------------------------------
            // test 1 - test detach and attach
            // -----------------------------------------------------------------------------------------
            tx.begin();
            pm.makePersistent(woody);
            pm.makePersistent(boss);
            pm.getFetchPlan().addGroup("groupDepartments");
            bossDetached = (Manager) pm.detachCopy(boss);
            tx.commit();
            idBoss = pm.getObjectId(boss);
            Department deptB = new Department("DeptB");
            deptB.setManager(bossDetached);
            bossDetached.addDepartment(deptB);
            tx.begin();
            pm.makePersistent(bossDetached);
            tx.commit();
            System.gc();
            tx.begin();
            Manager theBoss = (Manager) pm.getObjectById(idBoss, true);
            Assert.assertEquals(1, theBoss.getDepartments().size());
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception in test", e);
            fail(e.toString());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        CompanyHelper.clearCompanyData(pmf);
    }
}
Also used : Department(org.jpox.samples.models.company.Department) Employee(org.jpox.samples.models.company.Employee) Transaction(javax.jdo.Transaction) JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager) Manager(org.jpox.samples.models.company.Manager) JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager) JDODetachedFieldAccessException(javax.jdo.JDODetachedFieldAccessException) JDOUserException(javax.jdo.JDOUserException) JDOObjectNotFoundException(javax.jdo.JDOObjectNotFoundException)

Aggregations

PersistenceManager (javax.jdo.PersistenceManager)106 Manager (org.jpox.samples.models.company.Manager)106 Transaction (javax.jdo.Transaction)97 Department (org.jpox.samples.models.company.Department)50 Query (javax.jdo.Query)48 Employee (org.jpox.samples.models.company.Employee)45 JDOUserException (javax.jdo.JDOUserException)41 Collection (java.util.Collection)40 JDOPersistenceManager (org.datanucleus.api.jdo.JDOPersistenceManager)38 StoreManager (org.datanucleus.store.StoreManager)34 InsuranceDepartment (org.jpox.samples.models.company.InsuranceDepartment)29 JDOObjectNotFoundException (javax.jdo.JDOObjectNotFoundException)27 Iterator (java.util.Iterator)22 JDODetachedFieldAccessException (javax.jdo.JDODetachedFieldAccessException)17 RDBMSStoreManager (org.datanucleus.store.rdbms.RDBMSStoreManager)15 JDOException (javax.jdo.JDOException)14 Extent (javax.jdo.Extent)13 PersistenceManagerFactory (javax.jdo.PersistenceManagerFactory)13 List (java.util.List)11 Properties (java.util.Properties)10