Search in sources :

Example 31 with Manager

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

the class JDOQLContainerTest method testContainsResultVariable.

public void testContainsResultVariable() {
    try {
        Manager homer = new Manager(1, "Homer", "Simpson", "homer@simpson.com", 1, "serial 1");
        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");
        Manager boss2 = new Manager(4, "Boss", "WakesUp2", "boss2@wakes.up", 5, "serial 4");
        Manager boss4 = new Manager(6, "Boss", "WakesUp4", "boss4@wakes.up", 7, "serial 6");
        Department deptA = new Department("deptA");
        Department deptB = new Department("deptB");
        Department deptC = new Department("deptC");
        Department deptD = new Department("deptD");
        Department deptE = new Department("deptE");
        boss.addDepartment(deptA);
        boss.addDepartment(deptB);
        boss.addDepartment(deptC);
        boss2.addDepartment(deptD);
        boss2.addDepartment(deptE);
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            pm.makePersistent(bart);
            pm.makePersistent(homer);
            pm.makePersistent(boss);
            pm.makePersistent(boss2);
            pm.makePersistent(boss4);
            tx.commit();
            tx.begin();
            Query q = pm.newQuery(Manager.class);
            q.setFilter("departments.contains(d)");
            q.setResult("d");
            q.declareVariables("Department d");
            Collection c = (Collection) q.execute();
            assertEquals(5, c.size());
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        // Clean out our data
        CompanyHelper.clearCompanyData(pmf);
    }
}
Also used : Department(org.jpox.samples.models.company.Department) InsuranceDepartment(org.jpox.samples.models.company.InsuranceDepartment) Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) Collection(java.util.Collection) Manager(org.jpox.samples.models.company.Manager) PersistenceManager(javax.jdo.PersistenceManager)

Example 32 with Manager

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

the class JDOQLResultTest method testSetResultCartesianProduct1to1.

/**
 * Test cartesian products
 */
public void testSetResultCartesianProduct1to1() {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            Manager bart = new Manager(2, "Bart", "Simpson", "bart@simpson.com", 2, "serial 2");
            Manager[] boss = new Manager[5];
            boss[0] = new Manager(3, "Boss", "WakesUp", "boss@wakes.up", 4, "serial 3");
            boss[1] = new Manager(4, "Boss", "WakesUp2", "boss2@wakes.up", 5, "serial 4");
            boss[2] = new Manager(5, "Boss", "WakesUp3", "boss3@wakes.up", 6, "serial 5");
            boss[3] = new Manager(6, "Boss", "WakesUp4", "boss4@wakes.up", 7, "serial 6");
            boss[4] = new Manager(7, "Boss", "WakesUp5", "boss5@wakes.up", 8, "serial 7");
            Department deptA = new Department("DeptA");
            Department deptB = new Department("DeptB");
            deptA.setManager(bart);
            deptB.setManager(boss[1]);
            pm.makePersistent(bart);
            pm.makePersistentAll(boss);
            pm.makePersistent(deptA);
            pm.makePersistent(deptB);
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
        }
        try {
            tx.begin();
            Query q = pm.newQuery(pm.getExtent(Department.class, false));
            q.setResult("manager.firstName, manager.lastName");
            q.setOrdering("name ascending");
            List results = (List) q.execute();
            assertEquals(2, results.size());
            assertEquals("Bart", ((Object[]) results.get(0))[0]);
            assertEquals("Simpson", ((Object[]) results.get(0))[1]);
            assertEquals("Boss", ((Object[]) results.get(1))[0]);
            assertEquals("WakesUp2", ((Object[]) results.get(1))[1]);
            q.closeAll();
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
        }
        pm.close();
    } finally {
        CompanyHelper.clearCompanyData(pmf);
    }
}
Also used : Department(org.jpox.samples.models.company.Department) InsuranceDepartment(org.jpox.samples.models.company.InsuranceDepartment) Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) ArrayList(java.util.ArrayList) List(java.util.List) Manager(org.jpox.samples.models.company.Manager) PersistenceManager(javax.jdo.PersistenceManager)

Example 33 with Manager

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

the class JDOQLBasicTest method testQueryWithUnsetParameter.

/**
 * test query with missing parameter value.
 */
public void testQueryWithUnsetParameter() {
    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();
            try {
                Query q = pm.newQuery(Manager.class);
                q.setFilter("firstName != :myName");
                q.execute();
                fail("Should have thrown an exception for missing parameter but didn't");
            } catch (JDOUserException ue) {
            // Expected
            }
            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) Manager(org.jpox.samples.models.company.Manager) StoreManager(org.datanucleus.store.StoreManager) RDBMSStoreManager(org.datanucleus.store.rdbms.RDBMSStoreManager) PersistenceManager(javax.jdo.PersistenceManager) JDOUserException(javax.jdo.JDOUserException)

Example 34 with Manager

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

the class PersistenceManagerProxyTest method testLifecycleListenerRegisteredInPMFforAllClasses.

/**
 * Test of lifecycle listener registered for all classes
 */
public void testLifecycleListenerRegisteredInPMFforAllClasses() {
    BasicListener listener = new BasicListener(true);
    PersistenceManagerFactory pmf = getConfigurablePMF(1, null);
    pmf.addInstanceLifecycleListener(listener, null);
    freezePMF(pmf);
    try {
        PersistenceManager pm = pmf.getPersistenceManagerProxy();
        Transaction tx = pm.currentTransaction();
        int i = 0;
        try {
            tx.begin();
            // Persist an object and check the events
            Person person = new Person(12345, "Fred", "Smith", "Fred.Smith@jpox.org");
            pm.makePersistent(person);
            // Persist related objects and check the events
            // Manager has a 1-N (FK) with Department
            Manager manager = new Manager(12346, "George", "Bush", "george.bush@thewhitehouse.com", 2000000, "ABC-DEF");
            Department dept1 = new Department("Invasions");
            Department dept2 = new Department("Propaganda");
            Department dept3 = new Department("Lies");
            manager.addDepartment(dept1);
            manager.addDepartment(dept2);
            manager.addDepartment(dept3);
            dept1.setManager(manager);
            dept2.setManager(manager);
            dept3.setManager(manager);
            pm.makePersistent(manager);
            pm.flush();
            Integer[] events = listener.getRegisteredEventsAsArray();
            assertEquals("Wrong number of lifecycle events", 15, events.length);
            if (tx.getOptimistic()) {
                // Person
                assertEquals(LifecycleListenerSpecification.EVENT_POST_CREATE, events[i++].intValue());
                // Manager
                assertEquals(LifecycleListenerSpecification.EVENT_POST_CREATE, events[i++].intValue());
                // Department 1
                assertEquals(LifecycleListenerSpecification.EVENT_POST_CREATE, events[i++].intValue());
                // Department 2
                assertEquals(LifecycleListenerSpecification.EVENT_POST_CREATE, events[i++].intValue());
                // Department 3
                assertEquals(LifecycleListenerSpecification.EVENT_POST_CREATE, events[i++].intValue());
                int numPreStore = 0;
                int numPostStore = 0;
                int numEventsToProcess = i + 10;
                for (int j = i; j < numEventsToProcess; j++) {
                    if (events[j].intValue() == LifecycleListenerSpecification.EVENT_PRE_STORE) {
                        numPreStore++;
                    } else if (events[j].intValue() == LifecycleListenerSpecification.EVENT_POST_STORE) {
                        numPostStore++;
                    }
                    i++;
                }
                // 1 for each object
                assertEquals("Number of PreStore events is wrong", 5, numPreStore);
                // 1 for each object
                assertEquals("Number of PostStore events is wrong", 5, numPostStore);
            } else {
                assertEquals(LifecycleListenerSpecification.EVENT_POST_CREATE, events[i++].intValue());
                assertEquals(LifecycleListenerSpecification.EVENT_PRE_STORE, events[i++].intValue());
                assertEquals(LifecycleListenerSpecification.EVENT_POST_STORE, events[i++].intValue());
                // Manager
                assertEquals(LifecycleListenerSpecification.EVENT_POST_CREATE, events[i++].intValue());
                // Manager
                assertEquals(LifecycleListenerSpecification.EVENT_PRE_STORE, events[i++].intValue());
                // Department 1
                assertEquals(LifecycleListenerSpecification.EVENT_POST_CREATE, events[i++].intValue());
                // Department 1
                assertEquals(LifecycleListenerSpecification.EVENT_PRE_STORE, events[i++].intValue());
                // Department 1
                assertEquals(LifecycleListenerSpecification.EVENT_POST_STORE, events[i++].intValue());
                // Department 2
                assertEquals(LifecycleListenerSpecification.EVENT_POST_CREATE, events[i++].intValue());
                // Department 2
                assertEquals(LifecycleListenerSpecification.EVENT_PRE_STORE, events[i++].intValue());
                // Department 2
                assertEquals(LifecycleListenerSpecification.EVENT_POST_STORE, events[i++].intValue());
                // Department 3
                assertEquals(LifecycleListenerSpecification.EVENT_POST_CREATE, events[i++].intValue());
                // Department 3
                assertEquals(LifecycleListenerSpecification.EVENT_PRE_STORE, events[i++].intValue());
                // Department 3
                assertEquals(LifecycleListenerSpecification.EVENT_POST_STORE, events[i++].intValue());
                // Manager
                assertEquals(LifecycleListenerSpecification.EVENT_POST_STORE, events[i++].intValue());
            }
            tx.rollback();
            listener.getRegisteredEvents().clear();
            PersistenceManager pm2 = pmf.getPersistenceManager();
            Transaction tx2 = pm.currentTransaction();
            try {
                tx2.begin();
                // Persist an object and check the events
                pm.makePersistent(person);
                pm.flush();
                events = listener.getRegisteredEventsAsArray();
                i = 0;
                assertEquals(LifecycleListenerSpecification.EVENT_POST_CREATE, events[i++].intValue());
                assertEquals(LifecycleListenerSpecification.EVENT_PRE_STORE, events[i++].intValue());
                assertEquals(LifecycleListenerSpecification.EVENT_POST_STORE, events[i++].intValue());
                tx2.rollback();
            } finally {
                if (tx2.isActive()) {
                    tx2.rollback();
                }
                pm2.close();
            }
        } catch (Exception e) {
            LOG.error("Exception while running lifecycle listener simple object test", e);
            fail("Exception thrown while running lifecycle listener simple object test : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
            listener.getRegisteredEvents().clear();
        }
    } finally {
        CompanyHelper.clearCompanyData(pmf);
        pmf.close();
    }
}
Also used : Department(org.jpox.samples.models.company.Department) BasicListener(org.datanucleus.samples.lifecyclelistener.BasicListener) Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) PersistenceManagerFactory(javax.jdo.PersistenceManagerFactory) Manager(org.jpox.samples.models.company.Manager) PersistenceManager(javax.jdo.PersistenceManager) Person(org.jpox.samples.models.company.Person)

Example 35 with Manager

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

the class PersistenceManagerTest method testLifecycleListenerRegisteredInPMFforAllClasses.

/**
 * Test of lifecycle listener registered for all classes
 */
public void testLifecycleListenerRegisteredInPMFforAllClasses() {
    BasicListener listener = new BasicListener(true);
    PersistenceManagerFactory pmf = getConfigurablePMF(1, null);
    pmf.addInstanceLifecycleListener(listener, null);
    freezePMF(pmf);
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    int i = 0;
    try {
        tx.begin();
        // Persist an object and check the events
        Person person = new Person(12345, "Fred", "Smith", "Fred.Smith@jpox.org");
        pm.makePersistent(person);
        // Persist related objects and check the events
        // Manager has a 1-N (FK) with Department
        Manager manager = new Manager(12346, "George", "Bush", "george.bush@thewhitehouse.com", 2000000, "ABC-DEF");
        Department dept1 = new Department("Invasions");
        Department dept2 = new Department("Propaganda");
        Department dept3 = new Department("Lies");
        manager.addDepartment(dept1);
        manager.addDepartment(dept2);
        manager.addDepartment(dept3);
        dept1.setManager(manager);
        dept2.setManager(manager);
        dept3.setManager(manager);
        pm.makePersistent(manager);
        pm.flush();
        Integer[] events = listener.getRegisteredEventsAsArray();
        assertEquals("Wrong number of lifecycle events", 15, events.length);
        if (tx.getOptimistic()) {
            assertEquals(LifecycleListenerSpecification.EVENT_POST_CREATE, events[i++].intValue());
            // Manager
            assertEquals(LifecycleListenerSpecification.EVENT_POST_CREATE, events[i++].intValue());
            // Department 1
            assertEquals(LifecycleListenerSpecification.EVENT_POST_CREATE, events[i++].intValue());
            // Department 2
            assertEquals(LifecycleListenerSpecification.EVENT_POST_CREATE, events[i++].intValue());
            // Department 3
            assertEquals(LifecycleListenerSpecification.EVENT_POST_CREATE, events[i++].intValue());
            // Expecting 5 PreStore and 5 PostStore; 1 for each of the objects
            int numPreStore = 0;
            int numPostStore = 0;
            int numOther = 0;
            for (int j = i; j < 15; j++) {
                if (events[j].intValue() == LifecycleListenerSpecification.EVENT_PRE_STORE) {
                    numPreStore++;
                } else if (events[j].intValue() == LifecycleListenerSpecification.EVENT_POST_STORE) {
                    numPostStore++;
                } else {
                    numOther++;
                }
                i++;
            }
            assertEquals("Number of PreStore events was incorrect", 5, numPreStore);
            assertEquals("Number of PostStore events was incorrect", 5, numPostStore);
            assertEquals("Number of other events was incorrect", 0, numOther);
        } else {
            assertEquals(LifecycleListenerSpecification.EVENT_POST_CREATE, events[i++].intValue());
            assertEquals(LifecycleListenerSpecification.EVENT_PRE_STORE, events[i++].intValue());
            assertEquals(LifecycleListenerSpecification.EVENT_POST_STORE, events[i++].intValue());
            // Manager
            assertEquals(LifecycleListenerSpecification.EVENT_POST_CREATE, events[i++].intValue());
            // Manager
            assertEquals(LifecycleListenerSpecification.EVENT_PRE_STORE, events[i++].intValue());
            // Department 1
            assertEquals(LifecycleListenerSpecification.EVENT_POST_CREATE, events[i++].intValue());
            // Department 1
            assertEquals(LifecycleListenerSpecification.EVENT_PRE_STORE, events[i++].intValue());
            // Department 1
            assertEquals(LifecycleListenerSpecification.EVENT_POST_STORE, events[i++].intValue());
            // Department 2
            assertEquals(LifecycleListenerSpecification.EVENT_POST_CREATE, events[i++].intValue());
            // Department 2
            assertEquals(LifecycleListenerSpecification.EVENT_PRE_STORE, events[i++].intValue());
            // Department 2
            assertEquals(LifecycleListenerSpecification.EVENT_POST_STORE, events[i++].intValue());
            // Department 3
            assertEquals(LifecycleListenerSpecification.EVENT_POST_CREATE, events[i++].intValue());
            // Department 3
            assertEquals(LifecycleListenerSpecification.EVENT_PRE_STORE, events[i++].intValue());
            // Department 3
            assertEquals(LifecycleListenerSpecification.EVENT_POST_STORE, events[i++].intValue());
            // Manager
            assertEquals(LifecycleListenerSpecification.EVENT_POST_STORE, events[i++].intValue());
        }
        tx.rollback();
        listener.getRegisteredEvents().clear();
        PersistenceManager pm2 = pmf.getPersistenceManager();
        Transaction tx2 = pm.currentTransaction();
        try {
            tx2.begin();
            // Persist an object and check the events
            pm.makePersistent(person);
            pm.flush();
            events = listener.getRegisteredEventsAsArray();
            i = 0;
            assertEquals(LifecycleListenerSpecification.EVENT_POST_CREATE, events[i++].intValue());
            assertEquals(LifecycleListenerSpecification.EVENT_PRE_STORE, events[i++].intValue());
            assertEquals(LifecycleListenerSpecification.EVENT_POST_STORE, events[i++].intValue());
            tx2.rollback();
        } finally {
            if (tx2.isActive()) {
                tx2.rollback();
            }
            pm2.close();
        }
    } catch (Exception e) {
        LOG.error(">> Exception thrown in test", e);
        fail("Exception thrown while running lifecycle listener simple object test : " + e.getMessage());
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
        listener.getRegisteredEvents().clear();
        pmf.close();
    }
}
Also used : BigInteger(java.math.BigInteger) Department(org.jpox.samples.models.company.Department) BasicListener(org.datanucleus.samples.lifecyclelistener.BasicListener) Transaction(javax.jdo.Transaction) JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager) PersistenceManagerFactory(javax.jdo.PersistenceManagerFactory) Manager(org.jpox.samples.models.company.Manager) StoreManager(org.datanucleus.store.StoreManager) JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager) Person(org.jpox.samples.models.company.Person) TransactionNotActiveException(org.datanucleus.api.jdo.exceptions.TransactionNotActiveException) JDOException(javax.jdo.JDOException) JDOUserException(javax.jdo.JDOUserException) TransactionNotReadableException(org.datanucleus.api.jdo.exceptions.TransactionNotReadableException) SQLException(java.sql.SQLException) JDOUserCallbackException(javax.jdo.JDOUserCallbackException) JDOObjectNotFoundException(javax.jdo.JDOObjectNotFoundException) TransactionNotWritableException(org.datanucleus.api.jdo.exceptions.TransactionNotWritableException) JDOUnsupportedOptionException(javax.jdo.JDOUnsupportedOptionException)

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