Search in sources :

Example 81 with Manager

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

the class ExtentTest method testExtentSubclasses.

/**
 * Test use of pm.getExtent and use of the subclasses flag.
 */
public void testExtentSubclasses() throws Exception {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            Employee empl = new Employee(0, "Homer", "Simpson", "homer@simpsons.com", (float) 200000.0, "123");
            Manager mgr = new Manager(1, "Matt", "Groening", "matt@simpsons.com", (float) 500000.0, "1");
            pm.makePersistent(empl);
            pm.makePersistent(mgr);
            pm.flush();
            // test subclasses argument == false (should contain Employee only)
            Extent<Employee> extent = pm.getExtent(Employee.class, false);
            java.util.Iterator<Employee> it = extent.iterator();
            Employee empl2 = it.next();
            assertEquals(empl.getPersonNum(), empl2.getPersonNum());
            assertEquals(false, it.hasNext());
            tx.commit();
            // test subclasses argument == true (should contain Employee AND Manager)
            tx.begin();
            extent = pm.getExtent(Employee.class, true);
            it = extent.iterator();
            empl2 = it.next();
            if (empl2 instanceof Manager) {
                assertEquals(1, empl2.getPersonNum());
                pm.deletePersistent(empl2);
                empl2 = (Employee) it.next();
                assertEquals(0, empl2.getPersonNum());
                pm.deletePersistent(empl2);
            } else {
                assertEquals(0, empl2.getPersonNum());
                pm.deletePersistent(empl2);
                empl2 = (Manager) it.next();
                assertEquals(1, empl2.getPersonNum());
                pm.deletePersistent(empl2);
            }
            assertEquals(false, it.hasNext());
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        clean(Manager.class);
        clean(Employee.class);
    }
}
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)

Example 82 with Manager

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

the class MultithreadedTest method processQueryAndDetach.

protected String processQueryAndDetach(boolean transaction) {
    List<Employee> results = null;
    PersistenceManager pm = pmf.getPersistenceManager();
    pm.setProperty(PropertyNames.PROPERTY_DETACH_ON_CLOSE, "true");
    pm.getFetchPlan().setGroup("all");
    pm.getFetchPlan().setMaxFetchDepth(-1);
    Transaction tx = pm.currentTransaction();
    try {
        if (transaction) {
            tx.begin();
        }
        Query<Employee> q = pm.newQuery(Employee.class);
        List<Employee> emps = q.executeList();
        results = new ArrayList<Employee>(emps);
        if (transaction) {
            tx.commit();
        }
    } catch (Throwable thr) {
        LOG.error("Exception query objects", thr);
        return "Exception in query : " + thr.getMessage();
    } finally {
        if (transaction && tx.isActive()) {
            tx.rollback();
        }
        // Detached the Employees and their loaded fields
        pm.close();
    }
    for (Employee e : results) {
        try {
            LOG.debug(">> Employee: " + e.getFirstName() + " " + e.getLastName() + " bestFriend=" + e.getBestFriend());
            if (e instanceof Manager) {
                Set subs = ((Manager) e).getSubordinates();
                if (subs == null) {
                    return "Manager object didnt have its subordinates detached!";
                } else if (subs.size() != 100) {
                    return "Manager had " + subs.size() + " subordinates instead of 100";
                }
            } else {
                Manager mgr = e.getManager();
                if (mgr == null) {
                    return "Employee=" + e + " didnt have its manager set!";
                } else {
                    Set<Employee> subs = mgr.getSubordinates();
                    if (subs == null) {
                        return "Employee=" + e + " didnt have its subordinates set!";
                    } else if (subs.size() != 100) {
                        return "Employee=" + e + " has Manager with " + subs.size() + " subordinates instead of 100";
                    }
                    for (Employee subE : subs) {
                        subE.toString();
                    }
                }
            }
        } catch (Exception exc) {
            LOG.error(">> Exception thrown on check of results", exc);
            return "Exception thrown : " + exc.getMessage();
        }
    }
    return null;
}
Also used : Employee(org.jpox.samples.models.company.Employee) Set(java.util.Set) Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) Manager(org.jpox.samples.models.company.Manager) PersistenceManager(javax.jdo.PersistenceManager)

Example 83 with Manager

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

the class MultithreadedTest method testFind.

/**
 * Test that populates the datastore, and then starts many threads retrieving objects.
 */
public void testFind() {
    try {
        // Persist some data
        LOG.debug(">> Persisting data");
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        Object mgrId = null;
        try {
            tx.begin();
            Manager mgr = new Manager(1, "The", "Boss", "the.boss@datanucleus.com", 200000, "100000");
            pm.makePersistent(mgr);
            for (int i = 0; i < 100; i++) {
                Employee emp = new Employee(i + 2, "FirstName" + i, "LastName" + i, "first.last." + i + "@datanucleus.com", 100000 + i, "12345" + i);
                emp.setManager(mgr);
                mgr.addSubordinate(emp);
                pm.makePersistent(emp);
            }
            tx.commit();
            mgrId = JDOHelper.getObjectId(mgr);
        } catch (Throwable thr) {
            LOG.error("Exception persisting objects", thr);
            fail("Exception persisting data : " + thr.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        LOG.debug(">> Persisted data");
        // Verify the persistence
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            Query<Employee> q = pm.newQuery(Employee.class);
            List<Employee> emps = q.executeList();
            for (Employee e : emps) {
                LOG.debug(">> emp=" + e + " e.mgr=" + e.getManager());
            }
            LOG.debug(">> Queried Employees");
            tx.commit();
        } catch (Throwable thr) {
            LOG.error("Exception checking objects", thr);
            fail("Exception checking data : " + thr.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // Create the Threads
        int THREAD_SIZE = 500;
        final Object managerId = mgrId;
        final String[] threadErrors = new String[THREAD_SIZE];
        Thread[] threads = new Thread[THREAD_SIZE];
        for (int i = 0; i < THREAD_SIZE; i++) {
            final int threadNo = i;
            threads[i] = new Thread(new Runnable() {

                public void run() {
                    String errorMsg = processFind(managerId, true);
                    threadErrors[threadNo] = errorMsg;
                }
            });
        }
        // Run the Threads
        LOG.debug(">> Starting threads");
        for (int i = 0; i < THREAD_SIZE; i++) {
            threads[i].start();
        }
        for (int i = 0; i < THREAD_SIZE; i++) {
            try {
                threads[i].join();
            } catch (InterruptedException e) {
                fail(e.getMessage());
            }
        }
        LOG.debug(">> Completed threads");
        // Process any errors and fail the test if any threads failed present
        for (String error : threadErrors) {
            if (error != null) {
                fail(error);
            }
        }
    } finally {
        // Clean out data
        CompanyHelper.clearCompanyData(pmf);
    }
}
Also used : PersistenceManager(javax.jdo.PersistenceManager) Manager(org.jpox.samples.models.company.Manager) PersistenceManager(javax.jdo.PersistenceManager) Employee(org.jpox.samples.models.company.Employee) Transaction(javax.jdo.Transaction)

Example 84 with Manager

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

the class JDOQLBasicTest method testBoolean.

public void testBoolean() {
    try {
        Manager homer = new Manager(1, "Homer", "Simpson", "homer@simpson.com", 1, "serial 1");
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            pm.makePersistent(homer);
            tx.commit();
            tx.begin();
            Query q = pm.newQuery(Manager.class);
            q.setFilter("1 == 1");
            Collection c = (Collection) q.execute();
            assertEquals(1, c.size());
            q = pm.newQuery(Manager.class);
            q.setFilter("1 == 0");
            c = (Collection) q.execute();
            assertEquals(0, c.size());
            q = pm.newQuery(Manager.class);
            q.setFilter("true == true");
            c = (Collection) q.execute();
            assertEquals(1, c.size());
            q = pm.newQuery(Manager.class);
            q.setFilter("true == false");
            c = (Collection) q.execute();
            assertEquals(0, c.size());
            tx.commit();
        } finally {
            if (tx.isActive()) {
                pm.currentTransaction().rollback();
            }
            pm.close();
        }
    } finally {
        CompanyHelper.clearCompanyData(pmf);
    }
}
Also used : Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) Collection(java.util.Collection) Manager(org.jpox.samples.models.company.Manager) StoreManager(org.datanucleus.store.StoreManager) RDBMSStoreManager(org.datanucleus.store.rdbms.RDBMSStoreManager) PersistenceManager(javax.jdo.PersistenceManager)

Example 85 with Manager

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

the class JDOQLBasicTest method testNonTx.

/**
 * Test query executed outside of a transaction that will cause another object
 * to be fetched during the query (fetching that object must not close the
 * query's connection)
 */
public void testNonTx() {
    try {
        Manager homer = new Manager(1, "Homer", "Simpson", "homer@simpson.com", 1, "testNonTx 1");
        Employee bart = new Employee(2, "Bart", "Simpson", "bart@simpson.com", 1, "testNonTx 2");
        bart.setManager(homer);
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            pm.makePersistent(bart);
            pm.makePersistent(homer);
            tx.commit();
        } finally {
            if (tx.isActive()) {
                pm.currentTransaction().rollback();
            }
            pm.close();
        }
        // use a fresh PM so caches are empty
        pm = pmf.getPersistenceManager();
        // important for this test is that Employee.manager is in the default fetch group
        Query q = pm.newQuery(Employee.class);
        q.setFilter("personNum==2");
        Collection c = (Collection) q.execute();
        assertEquals(1, c.size());
    } finally {
        CompanyHelper.clearCompanyData(pmf);
    }
}
Also used : Employee(org.jpox.samples.models.company.Employee) Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) Collection(java.util.Collection) Manager(org.jpox.samples.models.company.Manager) StoreManager(org.datanucleus.store.StoreManager) RDBMSStoreManager(org.datanucleus.store.rdbms.RDBMSStoreManager) PersistenceManager(javax.jdo.PersistenceManager)

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