Search in sources :

Example 11 with Employee

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

the class AttachDetachReplicateTest method testMoveAcrossDatastores_company_check.

/**
 * Convenience method to check the detached state of a graph of "Company" objects.
 * @param messagePrefix Prefix to any failure messages
 * @param manager The detached manager that starts the graph
 * @param detached Whether we should test for detached objects
 */
private void testMoveAcrossDatastores_company_check(String messagePrefix, Manager manager, boolean detached) {
    Employee detached_employee1 = null;
    Employee detached_employee2 = null;
    Department detached_department1 = null;
    Department detached_department2 = null;
    if (!JDOHelper.isDetached(manager) && detached) {
        fail(messagePrefix + "Manager is not detached!");
    }
    assertEquals("Manager.personNum is not correct", 1L, manager.getPersonNum());
    assertEquals("Manager.firstName is not correct", "Lucifer", manager.getFirstName());
    assertEquals("Manager.firstName is not correct", "Satan", manager.getLastName());
    assertEquals("Manager.emailAddress is not correct", "Lucifer.Satan@microsoft.hell", manager.getEmailAddress());
    assertEquals("Manager.serialNo is not correct", "jsdkhf8z23", manager.getSerialNo().trim());
    assertTrue("Manager.salary is not correct : expected " + 33666.99f + " but is " + manager.getSalary(), Math.abs(33666.99f - manager.getSalary()) <= 0.02f);
    assertEquals("Number of manager's departments has been changed", 2, manager.getDepartments().size());
    assertEquals("Number of manager's subordinates has been changed", 2, manager.getSubordinates().size());
    for (Iterator it = manager.getDepartments().iterator(); it.hasNext(); ) {
        Department dep = (Department) it.next();
        if (!JDOHelper.isDetached(dep) && detached) {
            fail(messagePrefix + "Department is not detached!");
        }
        if ("Brainwashing".equals(dep.getName())) {
            detached_department1 = dep;
        } else if ("Torture".equals(dep.getName())) {
            detached_department2 = dep;
        } else {
            fail(messagePrefix + "The Name of at least one Department is wrong: " + dep.getName());
        }
    }
    if (detached_department1 == null || detached_department2 == null) {
        fail(messagePrefix + "Both detached departments have the same name!");
    }
    for (Iterator it = manager.getSubordinates().iterator(); it.hasNext(); ) {
        Employee emp = (Employee) it.next();
        if (!JDOHelper.isDetached(emp) && detached) {
            fail(messagePrefix + "Employee is not detached!");
        }
        if (!JDOHelper.isDetached(emp.getManager()) && detached) {
            fail(messagePrefix + "Manager reference of Employee is not detached!");
        }
        switch((int) emp.getPersonNum()) {
            case 9593:
                detached_employee1 = emp;
                break;
            case 8723:
                detached_employee2 = emp;
                break;
            default:
                fail(messagePrefix + "Employee number is wrong: " + emp.getPersonNum());
        }
    }
    if (detached_employee1 == null || detached_employee2 == null) {
        fail(messagePrefix + "Both employees have the same personNum!");
    }
}
Also used : Department(org.jpox.samples.models.company.Department) Employee(org.jpox.samples.models.company.Employee) Iterator(java.util.Iterator)

Example 12 with Employee

use of org.jpox.samples.models.company.Employee 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 13 with Employee

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

the class AttachDetachTest method testDetachAttachWithNoChangeLifecycle.

/**
 * Test detach and then attach with no changes, and the effect on lifecycle states.
 */
public void testDetachAttachWithNoChangeLifecycle() {
    try {
        // Create object and detach it
        Employee woodyDetached = null;
        PersistenceManager pm = newPM();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            Employee woody = new Employee(1, "Woody", "Woodpecker", "woody@woodpecker.com", 13, "serial 1", new Integer(10));
            Account acct = new Account();
            acct.setId(101);
            acct.setEnabled(true);
            acct.setUsername("woodyw");
            woody.setAccount(acct);
            pm.makePersistent(woody);
            woodyDetached = (Employee) pm.detachCopy(woody);
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception in test", e);
            fail(e.toString());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // Attach the object
        pm = newPM();
        tx = pm.currentTransaction();
        // Delay all persistence ops til flush/commit
        tx.setOptimistic(true);
        try {
            tx.begin();
            Employee woody = pm.makePersistent(woodyDetached);
            Account acct = woody.getAccount();
            assertFalse("Attached object is dirty but shouldn't be", JDOHelper.isDirty(woody));
            assertFalse("Attached Account is dirty but shouldn't be", JDOHelper.isDirty(acct));
            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 : Account(org.jpox.samples.models.company.Account) LoginAccount(org.jpox.samples.one_one.unidir.LoginAccount) Employee(org.jpox.samples.models.company.Employee) Transaction(javax.jdo.Transaction) JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager) JDODetachedFieldAccessException(javax.jdo.JDODetachedFieldAccessException) JDOUserException(javax.jdo.JDOUserException) JDOObjectNotFoundException(javax.jdo.JDOObjectNotFoundException)

Example 14 with Employee

use of org.jpox.samples.models.company.Employee 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 15 with Employee

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

the class AttachDetachTest method testPersistWithDetachedRelative.

/**
 * Test that persists an object, and then detaches it. Then creates an object and adds a relation
 * to the detached object and persists the new object. The related object should be connected to
 * the new object (shouldn't create a new version of the object).
 * @throws Exception Thrown if an error occurs.
 */
public void testPersistWithDetachedRelative() throws Exception {
    try {
        PersistenceManager pm = newPM();
        Transaction tx = pm.currentTransaction();
        Account detachedAcct = null;
        try {
            tx.begin();
            Account acct = new Account();
            acct.setEnabled(true);
            acct.setUsername("john");
            pm.makePersistent(acct);
            detachedAcct = (Account) pm.detachCopy(acct);
            tx.commit();
        } catch (JDOUserException ue) {
            LOG.error("Exception in test", ue);
            fail("Exception thrown while performing test : " + ue.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // Persist the new object with the related detached
        // and detachCopy the new object
        pm = newPM();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            Employee woody = new Employee(1, "Woody", "Woodpecker", "woody@woodpecker.com", 13, "serial 1", new Integer(10));
            woody.setAccount(detachedAcct);
            pm.makePersistent(woody);
            tx.commit();
        } catch (JDOUserException ue) {
            LOG.error("Exception during test", ue);
            fail("Exception thrown while performing test : " + ue.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        CompanyHelper.clearCompanyData(pmf);
    }
}
Also used : Account(org.jpox.samples.models.company.Account) LoginAccount(org.jpox.samples.one_one.unidir.LoginAccount) Employee(org.jpox.samples.models.company.Employee) Transaction(javax.jdo.Transaction) JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager) JDOUserException(javax.jdo.JDOUserException)

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