Search in sources :

Example 11 with Account

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

the class AttachDetachTest method testDetachAllOnCommitViaQuery.

/**
 * Test "DetachAllOnCommit" when we retrieve objects via query and commit the txn
 */
public void testDetachAllOnCommitViaQuery() {
    try {
        // Persist some objects
        PersistenceManager pm = newPM();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            Employee woody = new Employee(1, "Woody", "Woodpecker", "woody@warnerbros.com", 125, "123409");
            Employee bugs = new Employee(2, "Bugs", "Bunny", "bugs@warnerbros.com", 200, "123410");
            Account bugsAcct = new Account();
            bugsAcct.setUsername("bugs");
            bugsAcct.setEnabled(true);
            bugs.setAccount(bugsAcct);
            Manager donald = new Manager(3, "Donald", "Duck", "donald@warnerbros.com", 400, "123400");
            donald.addSubordinate(woody);
            donald.addSubordinate(bugs);
            woody.setManager(donald);
            bugs.setManager(donald);
            pm.makePersistent(donald);
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception in test", e);
            fail("Exception thrown while persisting objects and committing transaction with detachAllOnCommit : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // Retrieve Employees and check the detached states
        pm = newPM();
        pm.getFetchPlan().addGroup("groupSubordinates");
        pm.getFetchPlan().addGroup("groupA");
        pm.getFetchPlan().addGroup("groupC");
        pm.getFetchPlan().setMaxFetchDepth(3);
        ((JDOPersistenceManager) pm).setDetachAllOnCommit(true);
        tx = pm.currentTransaction();
        try {
            tx.begin();
            Query q = pm.newQuery(Employee.class);
            Collection results = (Collection) q.execute();
            // Detach all of Employees on commit
            tx.commit();
            assertThat((Collection<?>) results).as("Number of Employees retrieved is incorrect").hasSize(3);
            Iterator empIter = results.iterator();
            while (empIter.hasNext()) {
                Employee emp = (Employee) empIter.next();
                assertTrue("Employee " + StringUtils.toJVMIDString(emp) + " is not detached!", JDOHelper.isDetached(emp));
            }
        } catch (Exception e) {
            LOG.error("Exception in test", e);
            fail("Exception thrown while retrieving objects with detachAllOnCommit : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        CompanyHelper.clearCompanyData(pmf);
    }
}
Also used : JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) 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) Query(javax.jdo.Query) JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager) Iterator(java.util.Iterator) Collection(java.util.Collection) ClassWithNonPCCollection(org.datanucleus.samples.detach.ClassWithNonPCCollection) 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 12 with Account

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

the class AttachDetachTest method testDetachAllOnCommitViaFetch.

/**
 * Test "DetachAllOnCommit" when we retrieve an object and commit the txn
 */
public void testDetachAllOnCommitViaFetch() {
    try {
        Object id = null;
        // Persist some objects
        PersistenceManager pm = newPM();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            Employee woody = new Employee(1, "Woody", "Woodpecker", "woody@warnerbros.com", 125, "123409");
            Employee bugs = new Employee(2, "Bugs", "Bunny", "bugs@warnerbros.com", 200, "123410");
            Account bugsAcct = new Account();
            bugsAcct.setUsername("bugs");
            bugsAcct.setEnabled(true);
            bugs.setAccount(bugsAcct);
            Manager donald = new Manager(3, "Donald", "Duck", "donald@warnerbros.com", 400, "123400");
            donald.addSubordinate(woody);
            donald.addSubordinate(bugs);
            woody.setManager(donald);
            bugs.setManager(donald);
            pm.makePersistent(donald);
            tx.commit();
            id = JDOHelper.getObjectId(donald);
        } catch (Exception e) {
            LOG.error("Exception in test", e);
            fail("Exception thrown while persisting objects and committing transaction with detachAllOnCommit : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // Retrieve an object and check the detached states
        pm = newPM();
        pm.getFetchPlan().addGroup("groupSubordinates");
        pm.getFetchPlan().addGroup("groupA");
        pm.getFetchPlan().addGroup("groupC");
        pm.getFetchPlan().setMaxFetchDepth(3);
        ((JDOPersistenceManager) pm).setDetachAllOnCommit(true);
        tx = pm.currentTransaction();
        try {
            tx.begin();
            Manager donald = (Manager) pm.getObjectById(id);
            tx.commit();
            Set employees = donald.getSubordinates();
            assertNotNull("Donald was not detached and should have been at commit", donald);
            assertNotNull("Employees of Donald were not detached and should have been at commit", employees);
            assertEquals("Donald Duck has incorrect number of employees after detach at commit", 2, donald.getSubordinates().size());
            Employee bugs = null;
            Employee woody = null;
            Account bugsAcct = null;
            Iterator emplIter = employees.iterator();
            while (emplIter.hasNext()) {
                Employee emp = (Employee) emplIter.next();
                if (emp.getFirstName().equals("Bugs")) {
                    bugs = emp;
                } else if (emp.getFirstName().equals("Woody")) {
                    woody = emp;
                }
            }
            assertNotNull("Bugs Bunny was not detached and should have been at commit", bugs);
            assertNotNull("Woody Woodpecker was not detached and should have been at commit", woody);
            bugsAcct = bugs.getAccount();
            assertNotNull("Account of Bugs Bunny was not detached and should have been at commit", bugsAcct);
            // Check that all are now detached
            if (!JDOHelper.isDetached(bugs) || JDOHelper.getObjectId(bugs) == null) {
                fail("Bugs Bunny is not detached or hasn't been detached correctly after closing the PM");
            }
            if (!JDOHelper.isDetached(woody) || JDOHelper.getObjectId(woody) == null) {
                fail("Woody Woodpecker is not detached or hasn't been detached correctly after closing the PM");
            }
            if (!JDOHelper.isDetached(donald) || JDOHelper.getObjectId(donald) == null) {
                fail("Donald Duck is not detached or hasn't been detached correctly after closing the PM");
            }
            // Check that the relationships are intact
            if (!woody.getFirstName().equals("Woody") || !woody.getLastName().equals("Woodpecker")) {
                fail("Woody Woodpecker has lost his name after closing the PM");
            }
            assertNotNull("Woody has a null Manager after detach", woody.getManager());
            assertEquals("Woody has lost the relation to his detached Manager after commit", woody.getManager(), donald);
            if (!bugs.getFirstName().equals("Bugs") || !bugs.getLastName().equals("Bunny")) {
                fail("Bugs Bunny has lost his name after closing the PM");
            }
            assertNotNull("Bugs has a null Manager after detach", bugs.getManager());
            assertEquals("Bugs has lost the relation to his detached Manager after commit", bugs.getManager(), donald);
            if (!donald.getFirstName().equals("Donald") || !donald.getLastName().equals("Duck")) {
                fail("Donald Duck has lost his name after closing the PM");
            }
        } catch (Exception e) {
            LOG.error("Exception in test", e);
            fail("Exception thrown while retrieving objects with detachAllOnCommit : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        CompanyHelper.clearCompanyData(pmf);
    }
}
Also used : JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) Account(org.jpox.samples.models.company.Account) LoginAccount(org.jpox.samples.one_one.unidir.LoginAccount) Employee(org.jpox.samples.models.company.Employee) Set(java.util.Set) Transaction(javax.jdo.Transaction) JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager) Iterator(java.util.Iterator) 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 13 with Account

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

the class AttachDetachTest method testDetachAllOnCommitViaFetchUnlimited.

/**
 * Test "DetachAllOnCommit" when we retrieve an object and commit the txn.
 * Slight variation on previous test in that this uses a max fetch depth of -1 so testing for stack overflow
 */
public void testDetachAllOnCommitViaFetchUnlimited() {
    try {
        Object id = null;
        // Persist some objects
        PersistenceManager pm = newPM();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            Employee woody = new Employee(1, "Woody", "Woodpecker", "woody@warnerbros.com", 125, "123409");
            Employee bugs = new Employee(2, "Bugs", "Bunny", "bugs@warnerbros.com", 200, "123410");
            Account bugsAcct = new Account();
            bugsAcct.setUsername("bugs");
            bugsAcct.setEnabled(true);
            bugs.setAccount(bugsAcct);
            Manager donald = new Manager(3, "Donald", "Duck", "donald@warnerbros.com", 400, "123400");
            donald.addSubordinate(woody);
            donald.addSubordinate(bugs);
            woody.setManager(donald);
            bugs.setManager(donald);
            pm.makePersistent(donald);
            tx.commit();
            id = JDOHelper.getObjectId(donald);
        } catch (Exception e) {
            LOG.error("Exception in test", e);
            fail("Exception thrown while persisting objects and committing transaction with detachAllOnCommit : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // Retrieve an object and check the detached states
        pm = newPM();
        pm.getFetchPlan().addGroup("groupSubordinates");
        pm.getFetchPlan().addGroup("groupA");
        pm.getFetchPlan().addGroup("groupC");
        pm.getFetchPlan().setMaxFetchDepth(-1);
        ((JDOPersistenceManager) pm).setDetachAllOnCommit(true);
        tx = pm.currentTransaction();
        try {
            tx.begin();
            Manager donald = (Manager) pm.getObjectById(id);
            tx.commit();
            Set employees = donald.getSubordinates();
            assertNotNull("Donald was not detached and should have been at commit", donald);
            assertNotNull("Employees of Donald were not detached and should have been at commit", employees);
            assertEquals("Donald Duck has incorrect number of employees after detach at commit", 2, donald.getSubordinates().size());
            Employee bugs = null;
            Employee woody = null;
            Account bugsAcct = null;
            Iterator emplIter = employees.iterator();
            while (emplIter.hasNext()) {
                Employee emp = (Employee) emplIter.next();
                if (emp.getFirstName().equals("Bugs")) {
                    bugs = emp;
                } else if (emp.getFirstName().equals("Woody")) {
                    woody = emp;
                }
            }
            assertNotNull("Bugs Bunny was not detached and should have been at commit", bugs);
            assertNotNull("Woody Woodpecker was not detached and should have been at commit", woody);
            bugsAcct = bugs.getAccount();
            assertNotNull("Account of Bugs Bunny was not detached and should have been at commit", bugsAcct);
            // Check that all are now detached
            if (!JDOHelper.isDetached(bugs) || JDOHelper.getObjectId(bugs) == null) {
                fail("Bugs Bunny is not detached or hasn't been detached correctly after closing the PM");
            }
            if (!JDOHelper.isDetached(woody) || JDOHelper.getObjectId(woody) == null) {
                fail("Woody Woodpecker is not detached or hasn't been detached correctly after closing the PM");
            }
            if (!JDOHelper.isDetached(donald) || JDOHelper.getObjectId(donald) == null) {
                fail("Donald Duck is not detached or hasn't been detached correctly after closing the PM");
            }
            // Check that the relationships are intact
            if (!woody.getFirstName().equals("Woody") || !woody.getLastName().equals("Woodpecker")) {
                fail("Woody Woodpecker has lost his name after closing the PM");
            }
            assertNotNull("Woody has a null Manager after detach", woody.getManager());
            assertEquals("Woody has lost the relation to his detached Manager after commit", woody.getManager(), donald);
            if (!bugs.getFirstName().equals("Bugs") || !bugs.getLastName().equals("Bunny")) {
                fail("Bugs Bunny has lost his name after closing the PM");
            }
            assertNotNull("Bugs has a null Manager after detach", bugs.getManager());
            assertEquals("Bugs has lost the relation to his detached Manager after commit", bugs.getManager(), donald);
            if (!donald.getFirstName().equals("Donald") || !donald.getLastName().equals("Duck")) {
                fail("Donald Duck has lost his name after closing the PM");
            }
        } catch (Exception e) {
            LOG.error("Exception in test", e);
            fail("Exception thrown while retrieving objects with detachAllOnCommit : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        CompanyHelper.clearCompanyData(pmf);
    }
}
Also used : JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) Account(org.jpox.samples.models.company.Account) LoginAccount(org.jpox.samples.one_one.unidir.LoginAccount) Employee(org.jpox.samples.models.company.Employee) Set(java.util.Set) Transaction(javax.jdo.Transaction) JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager) Iterator(java.util.Iterator) 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 14 with Account

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

the class ApplicationIdPersistenceTest method testInsertMultipleClassesToSameXPath.

public void testInsertMultipleClassesToSameXPath() throws Exception {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        Person p = new Person();
        p.setPersonNum(4);
        p.setGlobalNum("4");
        p.setFirstName("Bugs4");
        p.setLastName("Bunny4");
        Account a = new Account();
        a.setId(1);
        a.setUsername("testusername");
        pm.makePersistent(p);
        pm.makePersistent(a);
        tx.commit();
    } catch (Exception e) {
        e.printStackTrace();
        fail("Exception thrown when running test " + e.getMessage());
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
        clean(Person.class);
        clean(Account.class);
    }
}
Also used : LoginAccount(org.jpox.samples.one_one.unidir.LoginAccount) Account(org.jpox.samples.models.company.Account) Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) Person(org.jpox.samples.models.company.Person) JDOObjectNotFoundException(javax.jdo.JDOObjectNotFoundException)

Aggregations

PersistenceManager (javax.jdo.PersistenceManager)14 Account (org.jpox.samples.models.company.Account)14 LoginAccount (org.jpox.samples.one_one.unidir.LoginAccount)14 Transaction (javax.jdo.Transaction)12 UserTransaction (javax.transaction.UserTransaction)8 Collection (java.util.Collection)7 Query (javax.jdo.Query)7 JDOObjectNotFoundException (javax.jdo.JDOObjectNotFoundException)5 JDOUserException (javax.jdo.JDOUserException)5 JDOPersistenceManager (org.datanucleus.api.jdo.JDOPersistenceManager)5 Employee (org.jpox.samples.models.company.Employee)5 JDODetachedFieldAccessException (javax.jdo.JDODetachedFieldAccessException)4 Iterator (java.util.Iterator)3 Manager (org.jpox.samples.models.company.Manager)3 Set (java.util.Set)2 NamingException (javax.naming.NamingException)2 HeuristicMixedException (javax.transaction.HeuristicMixedException)2 HeuristicRollbackException (javax.transaction.HeuristicRollbackException)2 NotSupportedException (javax.transaction.NotSupportedException)2 RollbackException (javax.transaction.RollbackException)2