Search in sources :

Example 1 with BasicListener

use of org.datanucleus.samples.lifecyclelistener.BasicListener 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 2 with BasicListener

use of org.datanucleus.samples.lifecyclelistener.BasicListener 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)

Example 3 with BasicListener

use of org.datanucleus.samples.lifecyclelistener.BasicListener in project tests by datanucleus.

the class PersistenceManagerTest method testLifecycleListenerForSimpleObjects.

/**
 * Test of basic lifecycle listener behaviour, listeneing to the changes in the lifecycle
 * of a simple object (with no relationships). The object is persisted, then updated, then detached
 * then updated (whilst detached), then attached, and finally deleted. This exercises all listener
 * event types.
 */
public void testLifecycleListenerForSimpleObjects() {
    BasicListener listener = new BasicListener(true);
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    int i = 0;
    try {
        pm.addInstanceLifecycleListener(listener, new Class[] { Person.class });
        Object person_id;
        tx.begin();
        // Persist an object and check the events
        Person person = new Person(12345, "Fred", "Smith", "Fred.Smith@jpox.org");
        pm.makePersistent(person);
        pm.flush();
        Integer[] events = listener.getRegisteredEventsAsArray();
        assertEquals(LifecycleListenerSpecification.EVENT_POST_CREATE, events[i++].intValue());
        assertEquals(LifecycleListenerSpecification.EVENT_PRE_STORE, events[i++].intValue());
        assertEquals(LifecycleListenerSpecification.EVENT_POST_STORE, events[i++].intValue());
        // Commit the changes and check the events
        tx.commit();
        events = listener.getRegisteredEventsAsArray();
        assertEquals(LifecycleListenerSpecification.EVENT_PRE_CLEAR, events[i++].intValue());
        assertEquals(LifecycleListenerSpecification.EVENT_POST_CLEAR, events[i++].intValue());
        // Save the object id
        person_id = pm.getObjectId(person);
        // Clean the cache and retrieve the object from datastore
        tx.begin();
        pm.evictAll();
        // Update a field on the object and check the events
        person = (Person) pm.getObjectById(person_id);
        person.setEmailAddress("Fred.Smith@aol.com");
        pm.flush();
        events = listener.getRegisteredEventsAsArray();
        assertEquals(LifecycleListenerSpecification.EVENT_POST_LOAD, events[i++].intValue());
        assertEquals(LifecycleListenerSpecification.EVENT_PRE_DIRTY, events[i++].intValue());
        assertEquals(LifecycleListenerSpecification.EVENT_POST_DIRTY, events[i++].intValue());
        // Commit the changes and check the events
        tx.commit();
        events = listener.getRegisteredEventsAsArray();
        assertEquals(LifecycleListenerSpecification.EVENT_PRE_STORE, events[i++].intValue());
        assertEquals(LifecycleListenerSpecification.EVENT_POST_STORE, events[i++].intValue());
        assertEquals(LifecycleListenerSpecification.EVENT_PRE_CLEAR, events[i++].intValue());
        assertEquals(LifecycleListenerSpecification.EVENT_POST_CLEAR, events[i++].intValue());
        assertTrue("Total number of lifecycle events received was incorrect : should have been " + i + " but was " + events.length, events.length == i);
        tx.begin();
        pm.evictAll();
        // Retrieve the object and detach it
        person = (Person) pm.getObjectById(person_id);
        Person detachedPerson = (Person) pm.detachCopy(person);
        pm.flush();
        events = listener.getRegisteredEventsAsArray();
        assertEquals(LifecycleListenerSpecification.EVENT_POST_LOAD, events[i++].intValue());
        assertEquals(LifecycleListenerSpecification.EVENT_PRE_DETACH, events[i++].intValue());
        assertEquals(LifecycleListenerSpecification.EVENT_POST_DETACH, events[i++].intValue());
        // Commit the changes and check the events
        tx.commit();
        events = listener.getRegisteredEventsAsArray();
        if (tx.getOptimistic()) {
        // Nothing here. TODO Why are there no preClear/postClear for optimistic yet there are for pessimistic?
        } else {
            assertEquals(LifecycleListenerSpecification.EVENT_PRE_CLEAR, events[i++].intValue());
            assertEquals(LifecycleListenerSpecification.EVENT_POST_CLEAR, events[i++].intValue());
        }
        // Update the detached object
        detachedPerson.setLastName("Green");
        tx.begin();
        // Attach the detached object
        pm.makePersistent(detachedPerson);
        pm.flush();
        events = listener.getRegisteredEventsAsArray();
        assertEquals(LifecycleListenerSpecification.EVENT_PRE_ATTACH, events[i++].intValue());
        assertEquals(LifecycleListenerSpecification.EVENT_PRE_DIRTY, events[i++].intValue());
        assertEquals(LifecycleListenerSpecification.EVENT_POST_DIRTY, events[i++].intValue());
        assertEquals(LifecycleListenerSpecification.EVENT_POST_ATTACH, events[i++].intValue());
        // Commit the changes and check the events
        tx.commit();
        events = listener.getRegisteredEventsAsArray();
        assertEquals(LifecycleListenerSpecification.EVENT_PRE_STORE, events[i++].intValue());
        assertEquals(LifecycleListenerSpecification.EVENT_POST_STORE, events[i++].intValue());
        assertEquals(LifecycleListenerSpecification.EVENT_PRE_CLEAR, events[i++].intValue());
        assertEquals(LifecycleListenerSpecification.EVENT_POST_CLEAR, events[i++].intValue());
        assertTrue("Total number of lifecycle events received was incorrect : should have been " + i + " but was " + events.length, events.length == i);
        // Make sure the get goes to the DB
        pmf.getDataStoreCache().evictAll();
        // Make sure the get goes to the DB
        pm.evictAll();
        tx.begin();
        // Delete the object and check the events
        person = (Person) pm.getObjectById(person_id);
        pm.deletePersistent(person);
        pm.flush();
        events = listener.getRegisteredEventsAsArray();
        if (tx.getOptimistic()) {
            // TODO Why 2 preDelete here?
            assertEquals(LifecycleListenerSpecification.EVENT_POST_LOAD, events[i++].intValue());
            assertEquals(LifecycleListenerSpecification.EVENT_PRE_DELETE, events[i++].intValue());
            assertEquals(LifecycleListenerSpecification.EVENT_PRE_DELETE, events[i++].intValue());
            assertEquals(LifecycleListenerSpecification.EVENT_POST_DELETE, events[i++].intValue());
        } else {
            assertEquals(LifecycleListenerSpecification.EVENT_POST_LOAD, events[i++].intValue());
            assertEquals(LifecycleListenerSpecification.EVENT_PRE_DELETE, events[i++].intValue());
            if (vendorID == null) {
                // Not present for RDBMS
                assertEquals(LifecycleListenerSpecification.EVENT_POST_LOAD, events[i++].intValue());
            }
            assertEquals(LifecycleListenerSpecification.EVENT_POST_DELETE, events[i++].intValue());
        }
        // Commit the changes and check the events
        tx.commit();
        events = listener.getRegisteredEventsAsArray();
        assertEquals(LifecycleListenerSpecification.EVENT_PRE_CLEAR, events[i++].intValue());
        assertEquals(LifecycleListenerSpecification.EVENT_POST_CLEAR, events[i++].intValue());
        assertTrue("Total number of lifecycle events received was incorrect : should have been " + i + " but was " + events.length, events.length == i);
    } 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();
    }
}
Also used : BigInteger(java.math.BigInteger) BasicListener(org.datanucleus.samples.lifecyclelistener.BasicListener) Transaction(javax.jdo.Transaction) 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)

Example 4 with BasicListener

use of org.datanucleus.samples.lifecyclelistener.BasicListener in project tests by datanucleus.

the class PersistenceManagerTest method testLifecycleListenerForCollections.

/**
 * Test of basic lifecycle listener behaviour, listeneing to the changes in the lifecycle
 * of an object with a collection of other objects.
 */
public void testLifecycleListenerForCollections() {
    BasicListener listener = new BasicListener(true);
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    int i = 0;
    try {
        pm.addInstanceLifecycleListener(listener, new Class[] { Manager.class, Department.class });
        Object managerId;
        Object dept2Id;
        tx.begin();
        // 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();
        if (tx.getOptimistic()) {
            // 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 + 8;
            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++;
            }
            assertEquals("Number of PreStore events is wrong", 4, numPreStore);
            assertEquals("Number of PostStore events is wrong", 4, numPostStore);
        } else {
            // 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());
        }
        // Commit the changes and check the events
        tx.commit();
        events = listener.getRegisteredEventsAsArray();
        // Manager
        assertEquals(LifecycleListenerSpecification.EVENT_PRE_CLEAR, events[i++].intValue());
        // Manager
        assertEquals(LifecycleListenerSpecification.EVENT_POST_CLEAR, events[i++].intValue());
        // Department 1
        assertEquals(LifecycleListenerSpecification.EVENT_PRE_CLEAR, events[i++].intValue());
        // Department 1
        assertEquals(LifecycleListenerSpecification.EVENT_POST_CLEAR, events[i++].intValue());
        // Department 2
        assertEquals(LifecycleListenerSpecification.EVENT_PRE_CLEAR, events[i++].intValue());
        // Department 2
        assertEquals(LifecycleListenerSpecification.EVENT_POST_CLEAR, events[i++].intValue());
        // Department 3
        assertEquals(LifecycleListenerSpecification.EVENT_PRE_CLEAR, events[i++].intValue());
        // Department 3
        assertEquals(LifecycleListenerSpecification.EVENT_POST_CLEAR, events[i++].intValue());
        assertTrue("Total number of lifecycle events received was incorrect : should have been " + i + " but was " + events.length, events.length == i);
        // Evict anything in the L2 cache so we know we are going to the
        // datastore, and hence get predictable callback ordering
        pmf.getDataStoreCache().evictAll();
        // Save the object ids
        managerId = pm.getObjectId(manager);
        dept2Id = pm.getObjectId(dept2);
        // Make sure the get goes to the DB
        pmf.getDataStoreCache().evictAll();
        // Make sure the get goes to the DB
        pm.evictAll();
        tx.begin();
        dept2 = (Department) pm.getObjectById(dept2Id);
        manager = (Manager) pm.getObjectById(managerId);
        // Remove manager of dept2 and check the events
        dept2.setManager(null);
        manager.removeDepartment(dept2);
        pm.flush();
        events = listener.getRegisteredEventsAsArray();
        if (tx.getOptimistic()) {
            int numPostLoad = 0;
            int numPreDirty = 0;
            int numPostDirty = 0;
            int numEventsToProcess = i + 8;
            for (int j = i; j < numEventsToProcess; j++) {
                if (events[j].intValue() == LifecycleListenerSpecification.EVENT_POST_LOAD) {
                    numPostLoad++;
                } else if (events[j].intValue() == LifecycleListenerSpecification.EVENT_PRE_DIRTY) {
                    numPreDirty++;
                } else if (events[j].intValue() == LifecycleListenerSpecification.EVENT_POST_DIRTY) {
                    numPostDirty++;
                }
                i++;
            }
            assertEquals("Number of PostLoad is wrong", 4, numPostLoad);
            // 1 for Department2 and 1 for Manager
            assertEquals("Number of PreDirty is wrong", 2, numPreDirty);
            // 1 for Department2 and 1 for Manager
            assertEquals("Number of PostDirty is wrong", 2, numPostDirty);
        } else {
            // Department 2
            assertEquals(LifecycleListenerSpecification.EVENT_POST_LOAD, events[i++].intValue());
            // Manager
            assertEquals(LifecycleListenerSpecification.EVENT_POST_LOAD, events[i++].intValue());
            // Department 2
            assertEquals(LifecycleListenerSpecification.EVENT_PRE_DIRTY, events[i++].intValue());
            // Department 2
            assertEquals(LifecycleListenerSpecification.EVENT_POST_DIRTY, events[i++].intValue());
            if (vendorID == null) {
                // Not needed on RDBMS for some reason
                // Department 1
                assertEquals(LifecycleListenerSpecification.EVENT_POST_LOAD, events[i++].intValue());
                // Department 3
                assertEquals(LifecycleListenerSpecification.EVENT_POST_LOAD, events[i++].intValue());
            }
            // Manager
            assertEquals(LifecycleListenerSpecification.EVENT_PRE_DIRTY, events[i++].intValue());
            // Manager
            assertEquals(LifecycleListenerSpecification.EVENT_POST_DIRTY, events[i++].intValue());
        }
        // Commit the changes and check the events
        tx.commit();
        events = listener.getRegisteredEventsAsArray();
        if (tx.getOptimistic()) {
            // Department 2
            assertEquals(LifecycleListenerSpecification.EVENT_PRE_STORE, events[i++].intValue());
            // Department 2
            assertEquals(LifecycleListenerSpecification.EVENT_POST_STORE, events[i++].intValue());
            // Manager
            assertEquals(LifecycleListenerSpecification.EVENT_PRE_STORE, events[i++].intValue());
            // Manager
            assertEquals(LifecycleListenerSpecification.EVENT_POST_STORE, events[i++].intValue());
            // Manager
            assertEquals(LifecycleListenerSpecification.EVENT_PRE_CLEAR, events[i++].intValue());
            // Manager
            assertEquals(LifecycleListenerSpecification.EVENT_POST_CLEAR, events[i++].intValue());
            // Department 2
            assertEquals(LifecycleListenerSpecification.EVENT_PRE_CLEAR, events[i++].intValue());
            // Department 2
            assertEquals(LifecycleListenerSpecification.EVENT_POST_CLEAR, events[i++].intValue());
        } else {
            // Department 2
            assertEquals(LifecycleListenerSpecification.EVENT_PRE_STORE, events[i++].intValue());
            // Department 2
            assertEquals(LifecycleListenerSpecification.EVENT_POST_STORE, events[i++].intValue());
            if (vendorID != null) {
                // RDBMS loads here
                assertEquals(LifecycleListenerSpecification.EVENT_POST_LOAD, events[i++].intValue());
                assertEquals(LifecycleListenerSpecification.EVENT_POST_LOAD, events[i++].intValue());
            }
            // Manager
            assertEquals(LifecycleListenerSpecification.EVENT_PRE_STORE, events[i++].intValue());
            // Manager
            assertEquals(LifecycleListenerSpecification.EVENT_POST_STORE, events[i++].intValue());
            // Manager
            assertEquals(LifecycleListenerSpecification.EVENT_PRE_CLEAR, events[i++].intValue());
            // Manager
            assertEquals(LifecycleListenerSpecification.EVENT_POST_CLEAR, events[i++].intValue());
            // Department 2
            assertEquals(LifecycleListenerSpecification.EVENT_PRE_CLEAR, events[i++].intValue());
            // Department 2
            assertEquals(LifecycleListenerSpecification.EVENT_POST_CLEAR, events[i++].intValue());
            if (vendorID == null) {
                // Clear the other 2 departments
                // Department 1
                assertEquals(LifecycleListenerSpecification.EVENT_PRE_CLEAR, events[i++].intValue());
                // Department 1
                assertEquals(LifecycleListenerSpecification.EVENT_POST_CLEAR, events[i++].intValue());
                // Department 3
                assertEquals(LifecycleListenerSpecification.EVENT_PRE_CLEAR, events[i++].intValue());
                // Department 3
                assertEquals(LifecycleListenerSpecification.EVENT_POST_CLEAR, events[i++].intValue());
            }
        }
        assertTrue("Total number of lifecycle events received was incorrect : should have been " + i + " but was " + events.length, events.length == i);
    // TODO Add attach/detach of the Manager and its Departments.
    } catch (Exception e) {
        LOG.error("Exception thrown in test", e);
        fail("Exception thrown while running lifecycle listener collection test : " + e.getMessage());
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
        listener.getRegisteredEvents().clear();
    }
}
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) Manager(org.jpox.samples.models.company.Manager) StoreManager(org.datanucleus.store.StoreManager) JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager) 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)4 Transaction (javax.jdo.Transaction)4 BasicListener (org.datanucleus.samples.lifecyclelistener.BasicListener)4 BigInteger (java.math.BigInteger)3 SQLException (java.sql.SQLException)3 JDOException (javax.jdo.JDOException)3 JDOObjectNotFoundException (javax.jdo.JDOObjectNotFoundException)3 JDOUnsupportedOptionException (javax.jdo.JDOUnsupportedOptionException)3 JDOUserCallbackException (javax.jdo.JDOUserCallbackException)3 JDOUserException (javax.jdo.JDOUserException)3 JDOPersistenceManager (org.datanucleus.api.jdo.JDOPersistenceManager)3 TransactionNotActiveException (org.datanucleus.api.jdo.exceptions.TransactionNotActiveException)3 TransactionNotReadableException (org.datanucleus.api.jdo.exceptions.TransactionNotReadableException)3 TransactionNotWritableException (org.datanucleus.api.jdo.exceptions.TransactionNotWritableException)3 Department (org.jpox.samples.models.company.Department)3 Manager (org.jpox.samples.models.company.Manager)3 Person (org.jpox.samples.models.company.Person)3 PersistenceManagerFactory (javax.jdo.PersistenceManagerFactory)2 StoreManager (org.datanucleus.store.StoreManager)2