Search in sources :

Example 16 with Person

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

the class CacheTest method clearEmployeeData.

protected void clearEmployeeData(PersistenceManagerFactory pmf) {
    Extent ext = null;
    java.util.Iterator it = null;
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        // disassociate all Employees and Departments from their Managers
        tx.begin();
        ext = pm.getExtent(Manager.class, false);
        it = ext.iterator();
        while (it.hasNext()) {
            Manager mgr = (Manager) it.next();
            mgr.clearSubordinates();
            mgr.clearDepartments();
        }
        tx.commit();
        // delete all Employee objects
        tx.begin();
        ext = pm.getExtent(Employee.class, false);
        it = ext.iterator();
        while (it.hasNext()) {
            Employee emp = (Employee) it.next();
            pm.deletePersistent(emp);
        }
        tx.commit();
        // delete all Qualification objects
        tx.begin();
        ext = pm.getExtent(Qualification.class, false);
        it = ext.iterator();
        while (it.hasNext()) {
            Qualification q = (Qualification) it.next();
            pm.deletePersistent(q);
        }
        tx.commit();
        // delete all Department objects
        tx.begin();
        ext = pm.getExtent(Department.class, false);
        it = ext.iterator();
        while (it.hasNext()) {
            Department d = (Department) it.next();
            pm.deletePersistent(d);
        }
        tx.commit();
        // delete all Manager objects
        tx.begin();
        ext = pm.getExtent(Manager.class, false);
        it = ext.iterator();
        while (it.hasNext()) {
            Manager mgr = (Manager) it.next();
            pm.deletePersistent(mgr);
        }
        tx.commit();
        // delete all Person objects
        tx.begin();
        ext = pm.getExtent(Person.class, true);
        it = ext.iterator();
        while (it.hasNext()) {
            Person person = (Person) it.next();
            pm.deletePersistent(person);
        }
        tx.commit();
    } finally {
        if (tx.isActive())
            tx.rollback();
        pm.close();
    }
}
Also used : Qualification(org.jpox.samples.models.company.Qualification) Department(org.jpox.samples.models.company.Department) Employee(org.jpox.samples.models.company.Employee) Iterator(java.util.Iterator) Transaction(javax.jdo.Transaction) Extent(javax.jdo.Extent) PersistenceManager(javax.jdo.PersistenceManager) Manager(org.jpox.samples.models.company.Manager) PersistenceManager(javax.jdo.PersistenceManager) Person(org.jpox.samples.models.company.Person)

Example 17 with Person

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

the class CacheTest method testL2MultiplePMSameObjectEvictionChange.

/**
 * Test to check the access of the same object in 2 PMs, with the first PM changing it then committing
 * and by the time the second PM commits (with no change) the object has been evicted.
 */
public void testL2MultiplePMSameObjectEvictionChange() {
    Properties userProps = new Properties();
    userProps.setProperty(PropertyNames.PROPERTY_CACHE_L1_TYPE, "soft");
    userProps.setProperty(PropertyNames.PROPERTY_CACHE_L2_TYPE, "weak");
    PersistenceManagerFactory cachePMF = getPMF(1, userProps);
    try {
        // Create a PM and add an object
        Object id = null;
        PersistenceManager pm = cachePMF.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            Person p1 = new Person(102, "George", "Bush", "george.bush@whitehouse.gov");
            pm.makePersistent(p1);
            id = pm.getObjectId(p1);
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // Clear the L2 cache so we don't have this object
        Level2Cache l2Cache = ((JDODataStoreCache) cachePMF.getDataStoreCache()).getLevel2Cache();
        l2Cache.evictAll();
        // Pin all Person objects
        l2Cache.pinAll(Person.class, false);
        // Start 2 PMs, and their txns
        PersistenceManager pm1 = cachePMF.getPersistenceManager();
        Transaction tx1 = pm1.currentTransaction();
        tx1.begin();
        PersistenceManager pm2 = cachePMF.getPersistenceManager();
        Transaction tx2 = pm2.currentTransaction();
        tx2.begin();
        // Retrieve the object in both PMs
        Person per1 = (Person) pm1.getObjectById(id);
        /*Person per2 = (Person)*/
        pm2.getObjectById(id);
        // Change the object in PM1, and commit it
        per1.setEmailAddress("obama@whitehouse.gov");
        tx1.commit();
        cachePMF.getDataStoreCache().evict(id);
        // Commit PM2 txn
        tx2.commit();
        pm1.close();
        pm2.close();
        // Retrieve the object and check it
        PersistenceManager pm3 = cachePMF.getPersistenceManager();
        tx = pm3.currentTransaction();
        try {
            tx.begin();
            Person p1 = (Person) pm3.getObjectById(id);
            assertEquals("Email address found was not modified version!", "obama@whitehouse.gov", p1.getEmailAddress());
            tx.commit();
        } catch (Exception e) {
            e.printStackTrace();
            fail("Exception thrown while retrieving object from L2 cache : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm3.close();
        }
    } finally {
        // Clean out created data
        clean(cachePMF, Person.class);
        cachePMF.close();
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) Level2Cache(org.datanucleus.cache.Level2Cache) JDODataStoreCache(org.datanucleus.api.jdo.JDODataStoreCache) PersistenceManagerFactory(javax.jdo.PersistenceManagerFactory) JDOPersistenceManagerFactory(org.datanucleus.api.jdo.JDOPersistenceManagerFactory) Properties(java.util.Properties) Person(org.jpox.samples.models.company.Person) JDOObjectNotFoundException(javax.jdo.JDOObjectNotFoundException)

Example 18 with Person

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

the class SQLQueryTest method testWithCandidateClassWithoutResultClass.

/**
 * Test of a query with a candidate class, without a result class.
 * @throws Exception Thrown if an error occurs
 */
public void testWithCandidateClassWithoutResultClass() throws Exception {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        // Create some basic data to query
        tx.begin();
        Person p1 = new Person(1, "First", "Person", "first.person@jpox.org");
        pm.makePersistent(p1);
        Person p2 = new Person(2, "Second", "Person", "second.person@jpox.org");
        pm.makePersistent(p2);
        tx.commit();
        // Query for a basic object, including the PK field(s)
        tx = pm.currentTransaction();
        tx.begin();
        Query query = pm.newNamedQuery(Person.class, "PeopleWithEmail");
        List results = (List) query.execute("second.person@jpox.org");
        Iterator iter = results.iterator();
        while (iter.hasNext()) {
            Object obj = iter.next();
            assertTrue("Query with candidate class has returned the wrong type of object : was " + obj.getClass().getName() + " but should have been Person", obj.getClass().getName().equals(Person.class.getName()));
            Person p = (Person) obj;
            assertTrue("Query with candidate class has returned the wrong Person object.", p.getPersonNum() == 2);
            assertTrue("Query with candidate class has returned the wrong Person object.", p.getFirstName().equals("Second"));
        }
        tx.commit();
        // Create some inherited data to query
        tx = pm.currentTransaction();
        tx.begin();
        Developer p3 = new Developer(10, "James", "Java", "james@java.com", (float) 12.00, "1234567", new Integer(1), "jdo");
        pm.makePersistent(p3);
        tx.commit();
        // Query for an inherited object, including the PK field(s)
        tx = pm.currentTransaction();
        tx.begin();
        Query inhQuery = pm.newNamedQuery(Developer.class, "DeveloperWithSkill");
        results = (List) inhQuery.execute("jdo");
        iter = results.iterator();
        while (iter.hasNext()) {
            Object obj = iter.next();
            assertTrue("Query with candidate class has returned the wrong type of object : was " + obj.getClass().getName() + " but should have been Developer", obj.getClass().getName().equals(Developer.class.getName()));
            Developer p = (Developer) obj;
            assertTrue("Query with candidate class has returned the wrong Developer object.", p.getSKILL().equals("jdo"));
        }
        tx.commit();
        // Create some inherited data to query
        tx = pm.currentTransaction();
        tx.begin();
        Developer p4 = new Developer(11, "Paul", "Perl", "paul@perl.com", (float) 6.00, "1234568", new Integer(2), "perl");
        p4.setGlobalNum("GUID-p4");
        pm.makePersistent(p4);
        tx.commit();
        // Query for an inherited object, including the PK field(s)
        tx = pm.currentTransaction();
        tx.begin();
        Query inhQuery2 = pm.newNamedQuery(Developer.class, "DeveloperWithSkillUsingJoin");
        results = (List) inhQuery2.execute("perl");
        iter = results.iterator();
        while (iter.hasNext()) {
            Object obj = iter.next();
            assertTrue("Query with candidate class has returned the wrong type of object : was " + obj.getClass().getName() + " but should have been Developer", obj.getClass().getName().equals(Developer.class.getName()));
            Developer p = (Developer) obj;
            assertTrue("Query with candidate class has returned the wrong Developer object.", p.getSKILL().equals("perl"));
            assertTrue("Query with candidate class has returned the wrong Developer object.", p.getGlobalNum().equals("GUID-p4"));
        }
        tx.commit();
    } catch (Exception e) {
        e.printStackTrace();
        LOG.error(e);
        fail("Exception thrown while performing SQL query using candidate class : " + e.getMessage());
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
        clean(Developer.class);
        clean(Person.class);
    }
}
Also used : BigInteger(java.math.BigInteger) Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) Iterator(java.util.Iterator) Developer(org.jpox.samples.models.company.Developer) List(java.util.List) Person(org.jpox.samples.models.company.Person) JDOUserException(javax.jdo.JDOUserException) JDODataStoreException(javax.jdo.JDODataStoreException)

Example 19 with Person

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

the class SQLQueryTest method testWithCandidateClassWithResultClass.

/**
 * Test of SQL queries with a candidate class AND a result class.
 */
public void testWithCandidateClassWithResultClass() throws Exception {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        // Create some basic data to query, and query using a ResultClass that is constructor based
        tx = pm.currentTransaction();
        tx.begin();
        Person pers1 = new Person(234568, "Homer", "Simpson", "homer.simpson@fox.com");
        pers1.setAge(45);
        Person pers2 = new Person(234578, "Marge", "Simpson", "marge.simpson@fox.com");
        pers1.setAge(42);
        pm.makePersistent(pers1);
        pm.makePersistent(pers2);
        tx.commit();
        tx = pm.currentTransaction();
        tx.begin();
        try {
            Query amountQuery = pm.newNamedQuery(Person.class, "PersonDetails");
            List results = (List) amountQuery.execute();
            Iterator resultsIter = results.iterator();
            while (resultsIter.hasNext()) {
                Object obj = resultsIter.next();
                assertEquals("ResultClass of query is incorrect.", PersonalDetails.class.getName(), obj.getClass().getName());
            }
            amountQuery.closeAll();
        } catch (JDOUserException e) {
            fail(e.getMessage());
        }
        tx.commit();
        // Create some inherited data to query and query using a ResultClass without constructor
        tx = pm.currentTransaction();
        tx.begin();
        Developer p3 = new Developer(13, "James", "Java", "james@java.com", (float) 15.00, "1234569", new Integer(3), "jdo");
        pm.makePersistent(p3);
        tx.commit();
        tx = pm.currentTransaction();
        tx.begin();
        Query inhQuery = pm.newNamedQuery(Developer.class, "DeveloperWithSkillForResult");
        List results = (List) inhQuery.execute("jdo");
        Iterator iter = results.iterator();
        while (iter.hasNext()) {
            Object obj = iter.next();
            assertTrue("Query with candidate class has returned the wrong type of object : was " + obj.getClass().getName() + " but should have been DeveloperRC", obj.getClass().getName().equals(DeveloperRC.class.getName()));
            DeveloperRC p = (DeveloperRC) obj;
            assertTrue("Query with candidate class has returned the wrong Developer object.", p.getSKILL().equals("jdo"));
        }
        tx.commit();
        // Create some inherited data to query and query using INNER JOIN and users class
        tx = pm.currentTransaction();
        tx.begin();
        Developer p4 = new Developer(100, "Mike", "Microsoft", "mike@microsoft.com", 10, "1234570", new Integer(3), ".net");
        p4.setGlobalNum("GUID-p4");
        pm.makePersistent(p4);
        tx.commit();
        tx = pm.currentTransaction();
        tx.begin();
        Query inhQuery2 = pm.newNamedQuery(Developer.class, "DeveloperWithSkillUsingJoinForResult");
        results = (List) inhQuery2.execute(".net");
        iter = results.iterator();
        while (iter.hasNext()) {
            Object obj = iter.next();
            assertTrue("Query with candidate class has returned the wrong type of object : was " + obj.getClass().getName() + " but should have been DeveloperRC", obj.getClass().getName().equals(DeveloperRC.class.getName()));
            DeveloperRC p = (DeveloperRC) obj;
            assertTrue("Query with candidate class has returned the wrong DeveloperRC object.", p.getSKILL().equals(".net"));
            assertTrue("Query with candidate class has returned the wrong DeveloperRC object.", p.getGlobalNum().equals("GUID-p4"));
            assertEquals("Query with candidate class has returned the wrong DeveloperRC object.", 100, p.personNum);
            assertEquals("Query with candidate class has returned the wrong DeveloperRC object.", 10, (int) p.salary);
        }
        tx.commit();
        // Create some inherited data to query and query using HashMap.
        tx = pm.currentTransaction();
        tx.begin();
        Developer p5 = new Developer();
        p5.setFirstName("John");
        p5.setSKILL("uml");
        p5.setGlobalNum("GUID-p5");
        p5.setSalary(10);
        p5.setPersonNum(100);
        p5.setSerialNo("" + new Random(System.currentTimeMillis()).nextInt());
        pm.makePersistent(p5);
        tx.commit();
        // ResultClass = java.util.HashMap
        tx = pm.currentTransaction();
        tx.begin();
        Query inhQuery3 = pm.newNamedQuery(Developer.class, "DeveloperWithSkillUsingJoinForResultHashMap");
        results = (List) inhQuery3.execute("uml");
        iter = results.iterator();
        while (iter.hasNext()) {
            Object obj = iter.next();
            assertTrue("Query with candidate class has returned the wrong type of object : was " + obj.getClass().getName() + " but should have been HashMap", obj.getClass().getName().equals(HashMap.class.getName()));
            HashMap p = (HashMap) obj;
            assertTrue("Query with candidate class has returned the wrong DeveloperRC object.", getValueForKeyInMapCaseInsensitive(p, "SKILL").equals("uml"));
            assertTrue("Query with candidate class has returned the wrong DeveloperRC object.", getValueForKeyInMapCaseInsensitive(p, "GLOBALNUM").equals("GUID-p5"));
            Number salary = (Number) getValueForKeyInMapCaseInsensitive(p, "salary");
            assertEquals("Query with candidate class has returned the wrong DeveloperRC object.", 10, salary.intValue());
            Object personNumObj = getValueForKeyInMapCaseInsensitive(p, "PERSONNUM");
            if (personNumObj instanceof Long) {
                assertEquals("Query with candidate class has returned the wrong DeveloperRC object.", 100, ((Long) personNumObj).intValue());
            } else if (personNumObj instanceof BigDecimal) {
                assertEquals("Query with candidate class has returned the wrong DeveloperRC object.", 100, ((BigDecimal) personNumObj).intValue());
            } else {
                fail("Test doest support keys in map of type " + personNumObj.getClass());
            }
        }
        tx.commit();
        // ResultClass = java.util.Map
        tx = pm.currentTransaction();
        tx.begin();
        Query inhQuery4 = pm.newNamedQuery(Developer.class, "DeveloperWithSkillUsingJoinForResultMap");
        results = (List) inhQuery4.execute("uml");
        iter = results.iterator();
        while (iter.hasNext()) {
            Object obj = iter.next();
            assertTrue("Query with candidate class has returned the wrong type of object : was " + obj.getClass().getName() + " but should have been HashMap", obj.getClass().getName().equals(HashMap.class.getName()));
            HashMap p = (HashMap) obj;
            assertTrue("Query with candidate class has returned the wrong DeveloperRC object.", getValueForKeyInMapCaseInsensitive(p, "SKILL").equals("uml"));
            assertTrue("Query with candidate class has returned the wrong DeveloperRC object.", getValueForKeyInMapCaseInsensitive(p, "GLOBALNUM").equals("GUID-p5"));
            Number salary = (Number) getValueForKeyInMapCaseInsensitive(p, "salary");
            assertEquals("Query with candidate class has returned the wrong DeveloperRC object.", 10, salary.intValue());
            Object personNumObj = getValueForKeyInMapCaseInsensitive(p, "PERSONNUM");
            if (personNumObj instanceof Long) {
                assertEquals("Query with candidate class has returned the wrong DeveloperRC object.", 100, ((Long) personNumObj).intValue());
            } else if (personNumObj instanceof BigDecimal) {
                assertEquals("Query with candidate class has returned the wrong DeveloperRC object.", 100, ((BigDecimal) personNumObj).intValue());
            } else {
                fail("Test doest support keys in map of type " + personNumObj.getClass());
            }
        }
        tx.commit();
    } catch (Exception e) {
        e.printStackTrace();
        LOG.error(e);
        fail("Exception thrown while querying with candidate class and result class : " + e.getMessage());
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
        clean(Developer.class);
        clean(Person.class);
    }
}
Also used : Query(javax.jdo.Query) DeveloperRC(org.jpox.samples.models.company.DeveloperRC) PersistenceManager(javax.jdo.PersistenceManager) HashMap(java.util.HashMap) Developer(org.jpox.samples.models.company.Developer) PersonalDetails(org.jpox.samples.models.company.PersonalDetails) JDOUserException(javax.jdo.JDOUserException) BigDecimal(java.math.BigDecimal) JDOUserException(javax.jdo.JDOUserException) JDODataStoreException(javax.jdo.JDODataStoreException) BigInteger(java.math.BigInteger) Transaction(javax.jdo.Transaction) Random(java.util.Random) Iterator(java.util.Iterator) List(java.util.List) Person(org.jpox.samples.models.company.Person)

Example 20 with Person

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

the class SQLQueryTest method testWithoutCandidatesClassWithParameters.

/**
 * Basic test of SQL without a candidate class but with parameters.
 */
public void testWithoutCandidatesClassWithParameters() throws Exception {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        // insert a new element for table person
        tx.begin();
        Person p = new Person(1, "Nobody", "Nobody", "nobody@jpox.org");
        pm.makePersistent(p);
        tx.commit();
        tx.begin();
        String sqlText = "SELECT count(*) FROM PERSON WHERE EMAIL_ADDRESS = ?";
        Query query = pm.newQuery("javax.jdo.query.SQL", sqlText);
        List results = (List) query.execute("nobody@jpox.org");
        Iterator iter = results.iterator();
        assertEquals(1, results.size());
        while (iter.hasNext()) {
            Object obj = iter.next();
            if (obj.getClass().isArray()) {
                fail("SQL Query selecting count(*) has returned an Object[] yet should have been Object");
            }
            assertTrue("SQL Query selecting count(*) has returned an object of the wrong type : was " + obj.getClass().getName() + " but should have been Number or subclass", obj instanceof Number);
            Number value = (Number) obj;
            assertEquals("SQL Query selecting count(*) returned the wrong value : was " + value.longValue() + " but should have been 1", value.longValue(), 1);
        }
        // test more than one parameter
        sqlText = "SELECT count(*) FROM PERSON WHERE EMAIL_ADDRESS = ? AND FIRSTNAME = ?";
        query = pm.newQuery("javax.jdo.query.SQL", sqlText);
        results = (List) query.execute("nobody@jpox.org", "Nobody");
        iter = results.iterator();
        assertEquals(1, results.size());
        while (iter.hasNext()) {
            Object obj = iter.next();
            if (obj.getClass().isArray()) {
                fail("SQL Query selecting count(*) has returned an Object[] yet should have been Object");
            }
            assertTrue("SQL Query selecting count(*) has returned an object of the wrong type : was " + obj.getClass().getName() + " but should have been Number or subclass", obj instanceof Number);
            Number value = (Number) obj;
            assertEquals("SQL Query selecting count(*) returned the wrong value : was " + value.longValue() + " but should have been 1", value.longValue(), 1);
        }
        // test more than one parameter
        sqlText = "SELECT count(*) FROM PERSON WHERE EMAIL_ADDRESS = ? AND FIRSTNAME = ?";
        query = pm.newQuery("javax.jdo.query.SQL", sqlText);
        results = (List) query.execute("nobody@jpox.org", "Noboda");
        assertEquals(1, results.size());
        // test more than one parameter
        sqlText = "SELECT * FROM PERSON WHERE EMAIL_ADDRESS = ? AND FIRSTNAME = ?";
        query = pm.newQuery("javax.jdo.query.SQL", sqlText);
        results = (List) query.execute("nobody@jpox.org", "Nobody");
        assertEquals(1, results.size());
        // test more than one parameter
        sqlText = "SELECT * FROM PERSON WHERE EMAIL_ADDRESS = ? AND FIRSTNAME = ?";
        query = pm.newQuery("javax.jdo.query.SQL", sqlText);
        results = (List) query.execute("nobody@jpox.org", "Noboda");
        assertEquals(0, results.size());
        tx.commit();
    } catch (Exception e) {
        e.printStackTrace();
        LOG.error(e);
        fail("Exception thrown while running SQL query with parameters : " + e.getMessage());
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
        clean(Person.class);
    }
}
Also used : Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) Iterator(java.util.Iterator) List(java.util.List) Person(org.jpox.samples.models.company.Person) JDOUserException(javax.jdo.JDOUserException) JDODataStoreException(javax.jdo.JDODataStoreException)

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