Search in sources :

Example 6 with Developer

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

the class SQLQueryTest method testNamedQueriesDescoped.

/**
 * Test using Named Queries that aren't scoped by a candidate class.
 */
public void testNamedQueriesDescoped() throws Exception {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        // 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();
        // Named query with no candidate class without result class
        tx.begin();
        Query query = pm.newNamedQuery(null, "DeveloperWithSkillForResultDescoped");
        List results = (List) query.execute("jdo");
        Iterator resultsIter = results.iterator();
        assertTrue("Incorrect number of results from NamedQuery with no candidate class : was " + results.size() + " but should be 1", results.size() == 1);
        while (resultsIter.hasNext()) {
            Object obj = resultsIter.next();
            assertTrue("Query with no 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 no candidate class has returned the wrong Developer object.", p.getSKILL().equals("jdo"));
        }
        tx.commit();
    } catch (Exception e) {
        e.printStackTrace();
        LOG.error(e);
        fail("Exception thrown while executing Named Query with no class scope : " + e.getMessage());
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
        clean(Developer.class);
    }
}
Also used : BigInteger(java.math.BigInteger) Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) DeveloperRC(org.jpox.samples.models.company.DeveloperRC) PersistenceManager(javax.jdo.PersistenceManager) Iterator(java.util.Iterator) Developer(org.jpox.samples.models.company.Developer) List(java.util.List) JDOUserException(javax.jdo.JDOUserException) JDODataStoreException(javax.jdo.JDODataStoreException)

Example 7 with Developer

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

the class SQLQueryTest method testQueryFromJdoqueryFile.

/**
 * Test of a query specified in a jdo query file.
 */
public void testQueryFromJdoqueryFile() throws Exception {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        // 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();
        // Run the query (specified in a jdoquery file)
        tx.begin();
        Query query = pm.newNamedQuery(null, "DeveloperSkills");
        List results = (List) query.execute("jdo");
        Iterator resultsIter = results.iterator();
        while (resultsIter.hasNext()) {
            Object skill = resultsIter.next();
            LOG.debug(">> Skill : " + skill);
        }
    } catch (JDOUserException ue) {
        ue.printStackTrace();
        LOG.error(ue);
        fail("Exception thrown while persisting object and performing query : " + ue.getMessage());
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
        clean(Developer.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) JDOUserException(javax.jdo.JDOUserException)

Example 8 with Developer

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

the class SQLQueryTest method testNamedQueriesDescoped.

/**
 * Test using Named Queries that aren't scoped by a candidate class.
 */
public void testNamedQueriesDescoped() throws Exception {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        // 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();
        // Named query with no candidate class without result class
        tx.begin();
        Query query = pm.newNamedQuery(null, "DeveloperWithSkillForResultDescoped");
        List results = (List) query.execute("jdo");
        Iterator resultsIter = results.iterator();
        assertTrue("Incorrect number of results from NamedQuery with no candidate class : was " + results.size() + " but should be 1", results.size() == 1);
        while (resultsIter.hasNext()) {
            Object obj = resultsIter.next();
            assertTrue("Query with no 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 no candidate class has returned the wrong Developer object.", p.getSKILL().equals("jdo"));
        }
        tx.commit();
    } catch (Exception e) {
        e.printStackTrace();
        LOG.error(e);
        fail("Exception thrown while executing Named Query with no class scope : " + e.getMessage());
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
        clean(Developer.class);
    }
}
Also used : BigInteger(java.math.BigInteger) Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) DeveloperRC(org.jpox.samples.models.company.DeveloperRC) PersistenceManager(javax.jdo.PersistenceManager) Iterator(java.util.Iterator) Developer(org.jpox.samples.models.company.Developer) List(java.util.List) JDOUserException(javax.jdo.JDOUserException) JDODataStoreException(javax.jdo.JDODataStoreException)

Example 9 with Developer

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

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

the class SQLQueryTest method testWithoutCandidateClassWithResultClass.

/**
 * Test of SQL queries when no candidate class is defined, and a result class is given.
 */
public void testWithoutCandidateClassWithResultClass() throws Exception {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        // Add a couple of payments
        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();
        // Test using ResultClass that is constructor-based
        tx.begin();
        try {
            String sqlText = "SELECT FIRSTNAME, LASTNAME, AGE FROM PERSON";
            Query query = pm.newQuery("javax.jdo.query.SQL", sqlText);
            query.setResultClass(PersonalDetails.class);
            List results = (List) query.execute();
            Iterator resultsIter = results.iterator();
            while (resultsIter.hasNext()) {
                Object obj = resultsIter.next();
                assertEquals("ResultClass of query is incorrect.", PersonalDetails.class.getName(), obj.getClass().getName());
            }
            query.closeAll();
        } catch (JDOUserException e) {
            fail(e.getMessage());
        }
        tx.commit();
        tx.begin();
        Developer p3 = new Developer();
        p3.setFirstName("John");
        p3.setSKILL("jdo");
        p3.setGlobalNum("GUID-p3");
        p3.setSerialNo("" + new Random(System.currentTimeMillis()).nextInt());
        pm.makePersistent(p3);
        Query inhQuery = pm.newNamedQuery(Developer.class, "DeveloperWithSkillForResult");
        inhQuery.setClass(null);
        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();
        tx.begin();
        Developer p4 = new Developer();
        p4.setFirstName("John");
        p4.setSKILL("ejb");
        p4.setGlobalNum("GUID-p4");
        p4.setSalary(10);
        p4.setPersonNum(100);
        p4.setSerialNo("" + new Random(System.currentTimeMillis()).nextInt());
        pm.makePersistent(p4);
        Query inhQuery2 = pm.newNamedQuery(Developer.class, "DeveloperWithSkillUsingJoinForResult");
        inhQuery2.setClass(null);
        results = (List) inhQuery2.execute("ejb");
        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("ejb"));
            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();
        tx.begin();
        Developer p5 = new Developer();
        p5.setFirstName("John");
        p5.setSKILL("sap");
        p5.setGlobalNum("GUID-p5");
        p5.setSalary(10);
        p5.setPersonNum(100);
        p5.setSerialNo("" + new Random(System.currentTimeMillis()).nextInt());
        pm.makePersistent(p5);
        Query inhQuery3 = pm.newNamedQuery(Developer.class, "DeveloperWithSkillUsingJoinForResultHashMap");
        inhQuery3.setClass(null);
        results = (List) inhQuery3.execute("sap");
        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("sap"));
            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();
    } 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) Transaction(javax.jdo.Transaction) Random(java.util.Random) Iterator(java.util.Iterator) List(java.util.List) Person(org.jpox.samples.models.company.Person)

Aggregations

Iterator (java.util.Iterator)10 List (java.util.List)10 JDOUserException (javax.jdo.JDOUserException)10 PersistenceManager (javax.jdo.PersistenceManager)10 Query (javax.jdo.Query)10 Transaction (javax.jdo.Transaction)10 Developer (org.jpox.samples.models.company.Developer)10 BigInteger (java.math.BigInteger)8 JDODataStoreException (javax.jdo.JDODataStoreException)6 DeveloperRC (org.jpox.samples.models.company.DeveloperRC)6 Person (org.jpox.samples.models.company.Person)6 BigDecimal (java.math.BigDecimal)4 HashMap (java.util.HashMap)4 Random (java.util.Random)4 PersonalDetails (org.jpox.samples.models.company.PersonalDetails)4