Search in sources :

Example 36 with Person

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

the class JDOQLTest method testRetrieve.

/**
 * Query returning an object with relation fields, testing the contents of the relation fields.
 */
public void testRetrieve() {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        Query q = pm.newQuery("SELECT FROM " + Person.class.getName() + " EXCLUDE SUBCLASSES" + " WHERE firstName == 'Lami'");
        Collection c = (Collection) q.execute();
        assertEquals(1, c.size());
        Iterator it = c.iterator();
        Person lami = (Person) it.next();
        assertEquals("FirstName is incorrect", "Lami", lami.getFirstName());
        assertNotNull("Value of field storing relation is null but shouldnt be", lami.getBestFriend());
        assertEquals("FirstName of related object is incorrect", "Bugs", lami.getBestFriend().getFirstName());
        tx.commit();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
}
Also used : Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) Iterator(java.util.Iterator) Collection(java.util.Collection) Person(org.jpox.samples.models.company.Person)

Example 37 with Person

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

the class JDOQLTest method testFilter.

/**
 * result test
 */
public void testFilter() {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        Query q = pm.newQuery("SELECT FROM " + Person.class.getName() + " EXCLUDE SUBCLASSES WHERE firstName == 'Ana'");
        Collection c = (Collection) q.execute();
        assertEquals(1, c.size());
        Iterator it = c.iterator();
        assertEquals("Ana", ((Person) it.next()).getFirstName());
        tx.commit();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
}
Also used : Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) Iterator(java.util.Iterator) Collection(java.util.Collection) Person(org.jpox.samples.models.company.Person)

Example 38 with Person

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

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

the class PersistenceManagerTest method testDeletePersistentExceptions.

/**
 * Test attempts to delete a transient object.
 */
public void testDeletePersistentExceptions() throws Exception {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            Person p = new Person(0, FIRSTNAME[0], LASTNAME[0], EMAIL[0]);
            // test attempt to redelete transient object
            try {
                tx.begin();
                pm.deletePersistent(p);
                fail("calling deletePersistent on transient object should fail");
            } catch (JDOUserException e) {
            } finally {
                tx.rollback();
            }
            // test attempt to delete a P-new-del
            tx.begin();
            pm.makePersistent(p);
            pm.deletePersistent(p);
            try {
                pm.deletePersistent(p);
            } catch (JDOUserException e) {
                fail("calling deletePersistent on a P-del object should work");
            }
            tx.rollback();
            // test attempt to delete a P-del
            p = createNewPerson(pm, 0);
            tx.begin();
            pm.deletePersistent(p);
            try {
                pm.deletePersistent(p);
            } catch (JDOUserException e) {
                fail("calling deletePersistent on a P-del object should work");
            }
            tx.rollback();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        clean(Person.class);
    }
}
Also used : Transaction(javax.jdo.Transaction) JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager) Person(org.jpox.samples.models.company.Person) JDOUserException(javax.jdo.JDOUserException)

Example 40 with Person

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

Person (org.jpox.samples.models.company.Person)132 PersistenceManager (javax.jdo.PersistenceManager)130 Transaction (javax.jdo.Transaction)128 Query (javax.jdo.Query)75 List (java.util.List)60 JDOUserException (javax.jdo.JDOUserException)41 Iterator (java.util.Iterator)40 Collection (java.util.Collection)27 Employee (org.jpox.samples.models.company.Employee)22 HashMap (java.util.HashMap)21 JDODataStoreException (javax.jdo.JDODataStoreException)21 JDOException (javax.jdo.JDOException)16 JDOPersistenceManager (org.datanucleus.api.jdo.JDOPersistenceManager)16 ArrayList (java.util.ArrayList)15 BigInteger (java.math.BigInteger)14 JDOObjectNotFoundException (javax.jdo.JDOObjectNotFoundException)14 BigDecimal (java.math.BigDecimal)10 ClassLoaderResolver (org.datanucleus.ClassLoaderResolver)10 JDOQuery (org.datanucleus.api.jdo.JDOQuery)10 JDOQLCompiler (org.datanucleus.query.compiler.JDOQLCompiler)10