Search in sources :

Example 71 with Employee

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

the class JDOQLBasicTest method testNonstandardCharacters.

/**
 * Test the use of non-standard characters in parameter names etc.
 */
public void testNonstandardCharacters() {
    try {
        Employee woody = new Employee(1, "Woody", "Woodpecker", "woody@woodpecker.com", 13, "serial 1");
        Employee bart = new Employee(2, "Bart", "Simpson", "bart@simpson.com", 2, "serial 2");
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            // Persist some data to query
            tx.begin();
            pm.makePersistent(woody);
            pm.makePersistent(bart);
            tx.commit();
            // Query using $ in parameter names
            tx.begin();
            try {
                Query q = pm.newQuery(org.jpox.samples.models.company.Employee.class);
                q.declareImports("import java.lang.String");
                q.declareParameters("java.lang.String $theFirstName, java.lang.String $theLastName");
                q.setFilter("firstName == $theFirstName && lastName == $theLastName");
                Collection coll = (Collection) q.execute("Woody", "Woodpecker");
                assertTrue("received: " + coll, coll.size() == 1);
                q.closeAll();
            } catch (Exception e) {
                LOG.error("Exception during test", e);
                fail(e.getMessage());
            }
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        // Clean out our data
        CompanyHelper.clearCompanyData(pmf);
    }
}
Also used : Employee(org.jpox.samples.models.company.Employee) Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) Collection(java.util.Collection) JDOException(javax.jdo.JDOException) JDOUserException(javax.jdo.JDOUserException) NoSuchElementException(java.util.NoSuchElementException)

Example 72 with Employee

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

the class JDOQLContainerTest method testContainsInSetFields.

/**
 * Tests contains() in Set fields
 */
public void testContainsInSetFields() {
    try {
        Object managerId = null;
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            Manager manager = new Manager(1, "John", "Doe", "john.doe@acme.com", 10000f, "1");
            Department dept1 = new Department("accounting");
            Department dept2 = new Department("entertainment");
            Employee emp1 = new Employee(2, "Harvey", "Hacker", "harvey.hacker@acme.com", 500f, "2");
            Employee emp2 = new Employee(3, "Geoffrey", "Gimp", "geoffrey.gimp@acme.com", 500f, "3");
            pm.makePersistentAll(new Object[] { manager, dept1, dept2, emp1, emp2 });
            managerId = JDOHelper.getObjectId(manager);
            Set depts = manager.getDepartments();
            depts.add(dept1);
            depts.add(dept2);
            Set emps = manager.getSubordinates();
            emps.add(emp1);
            emps.add(emp2);
            pm.flush();
            Query q = pm.newQuery(Manager.class, "departments.contains(d) && subordinates.contains(e)");
            q.declareParameters(Department.class.getName() + " d, " + Employee.class.getName() + " e");
            q.compile();
            Collection c = (Collection) q.execute(dept1, emp1);
            assertEquals(1, c.size());
            assertEquals(managerId, JDOHelper.getObjectId(c.iterator().next()));
            tx.rollback();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        // Clean out our data
        CompanyHelper.clearCompanyData(pmf);
    }
}
Also used : Department(org.jpox.samples.models.company.Department) InsuranceDepartment(org.jpox.samples.models.company.InsuranceDepartment) Employee(org.jpox.samples.models.company.Employee) HashSet(java.util.HashSet) Set(java.util.Set) Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) Collection(java.util.Collection) Manager(org.jpox.samples.models.company.Manager) PersistenceManager(javax.jdo.PersistenceManager)

Example 73 with Employee

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

the class JDOQLResultTest method testResultSetInsensitive.

/**
 * Test for insensitive ResultSets. This tests a JPOX extension.
 */
public void testResultSetInsensitive() {
    try {
        Employee woody = new Employee(1, "Woody", "Woodpecker", "woody@woodpecker.com", 13, "serial 1");
        Employee bart = new Employee(2, "Bart", "Simpson", "bart@simpson.com", 2, "serial 2");
        // Eh, what's up, doc?
        Employee bunny = new Employee(3, "Bugs", "Bunny", "bugs.bunny@warnerbros.com", 12, "serial 3");
        // Meep! Meep!
        Employee roadrunner = new Employee(4, "Road", "Runner", "road.runner@warnerbros.com", 11, "serial 4");
        getConfigurationForPMF(pmf).setProperty(RDBMSPropertyNames.PROPERTY_RDBMS_QUERY_RESULT_SET_TYPE, "scroll-insensitive");
        getConfigurationForPMF(pmf).setProperty(RDBMSPropertyNames.PROPERTY_RDBMS_QUERY_FETCH_DIRECTION, "forward");
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            // Create some data
            tx.begin();
            pm.makePersistent(woody);
            pm.makePersistent(bart);
            pm.makePersistent(bunny);
            pm.makePersistent(roadrunner);
            tx.commit();
            // Query the data
            tx.begin();
            try {
                Query q = pm.newQuery(pm.getExtent(org.jpox.samples.models.company.Employee.class, false));
                List results = (List) q.execute();
                assertEquals("Number of objects retrieved using insensitive ResultSet is incorrect", 4, results.size());
                Object[] objects = new Object[results.size()];
                ListIterator resultsIter = results.listIterator();
                // Navigate forwards through the list
                assertEquals("hasNext() returns incorrect value", true, resultsIter.hasNext());
                assertEquals("hasPrevious() returns incorrect value", false, resultsIter.hasPrevious());
                assertEquals("nextIndex() in listIterator is incorrect", 0, resultsIter.nextIndex());
                assertEquals("previousIndex() in listIterator is incorrect", -1, resultsIter.previousIndex());
                objects[0] = resultsIter.next();
                assertEquals("previous() returns an incorrect object", objects[0], resultsIter.previous());
                resultsIter.next();
                assertEquals("hasNext() returns incorrect value", true, resultsIter.hasNext());
                assertEquals("hasPrevious() returns incorrect value", true, resultsIter.hasPrevious());
                assertEquals("nextIndex() in listIterator is incorrect", 1, resultsIter.nextIndex());
                assertEquals("previousIndex() in listIterator is incorrect", 0, resultsIter.previousIndex());
                objects[1] = resultsIter.next();
                assertEquals("previous() returns an incorrect object", objects[1], resultsIter.previous());
                resultsIter.next();
                assertEquals("hasNext() returns incorrect value", true, resultsIter.hasNext());
                assertEquals("hasPrevious() returns incorrect value", true, resultsIter.hasPrevious());
                assertEquals("nextIndex() in listIterator is incorrect", 2, resultsIter.nextIndex());
                assertEquals("previousIndex() in listIterator is incorrect", 1, resultsIter.previousIndex());
                objects[2] = resultsIter.next();
                assertEquals("previous() returns an incorrect object", objects[2], resultsIter.previous());
                resultsIter.next();
                assertEquals("hasNext() returns incorrect value", true, resultsIter.hasNext());
                assertEquals("hasPrevious() returns incorrect value", true, resultsIter.hasPrevious());
                assertEquals("nextIndex() in listIterator is incorrect", 3, resultsIter.nextIndex());
                assertEquals("previousIndex() in listIterator is incorrect", 2, resultsIter.previousIndex());
                objects[3] = resultsIter.next();
                assertEquals("previous() returns an incorrect object", objects[3], resultsIter.previous());
                resultsIter.next();
                assertEquals("hasNext() returns incorrect value", false, resultsIter.hasNext());
                assertEquals("hasPrevious() returns incorrect value", true, resultsIter.hasPrevious());
                assertEquals("previousIndex() in listIterator is incorrect", 3, resultsIter.previousIndex());
                assertEquals("nextIndex() in listIterator is incorrect", 4, resultsIter.nextIndex());
                // Navigate backwards through the list
                assertEquals("previous() returns an incorrect object", objects[3], resultsIter.previous());
                assertEquals("hasNext() returns incorrect value", true, resultsIter.hasNext());
                assertEquals("hasPrevious() returns incorrect value", true, resultsIter.hasPrevious());
                assertEquals("previousIndex() returns incorrect value", 2, resultsIter.previousIndex());
                assertEquals("nextIndex() returns incorrect value", 3, resultsIter.nextIndex());
                assertEquals("previous() returns an incorrect object", objects[2], resultsIter.previous());
                assertEquals("hasNext() returns incorrect value", true, resultsIter.hasNext());
                assertEquals("hasPrevious() returns incorrect value", true, resultsIter.hasPrevious());
                assertEquals("previousIndex() returns incorrect value", 1, resultsIter.previousIndex());
                assertEquals("nextIndex() returns incorrect value", 2, resultsIter.nextIndex());
                assertEquals("previous() returns an incorrect object", objects[1], resultsIter.previous());
                assertEquals("hasNext() returns incorrect value", true, resultsIter.hasNext());
                assertEquals("hasPrevious() returns incorrect value", true, resultsIter.hasPrevious());
                assertEquals("previousIndex() returns incorrect value", 0, resultsIter.previousIndex());
                assertEquals("nextIndex() returns incorrect value", 1, resultsIter.nextIndex());
                assertEquals("previous() returns an incorrect object", objects[0], resultsIter.previous());
                assertEquals("hasNext() returns incorrect value", true, resultsIter.hasNext());
                assertEquals("hasPrevious() returns incorrect value", false, resultsIter.hasPrevious());
                assertEquals("previousIndex() returns incorrect value", -1, resultsIter.previousIndex());
                assertEquals("nextIndex() returns incorrect value", 0, resultsIter.nextIndex());
                q.closeAll();
            } catch (JDOUserException e) {
                fail(e.getMessage());
            }
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        // Clean out our data
        clean(Employee.class);
    }
}
Also used : Employee(org.jpox.samples.models.company.Employee) Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) ArrayList(java.util.ArrayList) List(java.util.List) ListIterator(java.util.ListIterator) JDOUserException(javax.jdo.JDOUserException)

Example 74 with Employee

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

the class JDOQLResultTest method testUniqueResultAggregates.

public void testUniqueResultAggregates() {
    Employee woody = new Employee(1, "Woody", "Woodpecker", "woody@woodpecker.com", 13, "serial 1");
    Employee bart = new Employee(2, "Bart", "Simpson", "bart@simpson.com", 2, "serial 2");
    // Eh, what's up, doc?
    Employee bunny = new Employee(3, "Bugs", "Bunny", "bugs.bunny@warnerbros.com", 12, "serial 3");
    // Meep! Meep!
    Employee roadrunner = new Employee(4, "Road", "Runner", "road.runner@warnerbros.com", 11, "serial 4");
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        pm.makePersistent(woody);
        pm.makePersistent(bart);
        pm.makePersistent(bunny);
        pm.makePersistent(roadrunner);
        tx.commit();
        tx.begin();
        try {
            Query q = pm.newQuery(pm.getExtent(org.jpox.samples.models.company.Employee.class, false));
            q.setResult("max(salary)");
            Object results = q.execute();
            assertTrue("test returned an object of an incorrect type : type is " + results.getClass(), results instanceof Float);
            q.closeAll();
        } catch (JDOUserException e) {
            fail(e.getMessage());
        }
        try {
            Query q = pm.newQuery(pm.getExtent(org.jpox.samples.models.company.Employee.class, false));
            q.setResult("max (salary)");
            Object results = q.execute();
            assertTrue("test returned an object of an incorrect type : type is " + results.getClass(), results instanceof Float);
            q.closeAll();
        } catch (JDOUserException e) {
            fail(e.getMessage());
        }
        tx.commit();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
        // Clean out our data
        clean(Employee.class);
    }
}
Also used : Employee(org.jpox.samples.models.company.Employee) Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) JDOUserException(javax.jdo.JDOUserException)

Example 75 with Employee

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

the class JDOQLSubqueryTest method testSingleStringSubqueryWithParameter.

/**
 * Test a simple subquery using single-string form and a parameter in the subquery.
 */
public void testSingleStringSubqueryWithParameter() {
    if (storeMgr instanceof RDBMSStoreManager) {
        DatastoreAdapter dba = ((RDBMSStoreManager) storeMgr).getDatastoreAdapter();
        if (!dba.supportsOption(DatastoreAdapter.ACCESS_PARENTQUERY_IN_SUBQUERY_JOINED)) {
            // Access of outer query cols not supported by this datastore so dont test it
            LOG.warn("Database doesnt support use of parameters with subqueries so omitting the test");
            return;
        }
    }
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        // Persist Employees
        Employee emp1 = new Employee(101, "Fred", "Smith", "fred.smith@company.com", 100f, "10001");
        Employee emp2 = new Employee(102, "John", "Smith", "john.smith@company.com", 80f, "10002");
        Employee emp3 = new Employee(103, "Jim", "Smith", "jim.smith@company.com", 80f, "10003");
        Employee emp4 = new Employee(104, "Geoff", "Jones", "f2.s2@company.com", 200f, "10004");
        pm.makePersistent(emp1);
        pm.makePersistent(emp2);
        pm.makePersistent(emp3);
        pm.makePersistent(emp4);
        pm.flush();
        // Find the Employees earning more than the average salary of people with the same surname
        Query q = pm.newQuery("SELECT FROM " + Employee.class.getName() + " WHERE salary > " + " (SELECT avg(e.salary) FROM " + Employee.class.getName() + " e " + " WHERE e.lastName == this.lastName)");
        // NOTE : HSQL <= 1.8 doesnt seem to support and conditions back to the outer query
        List results = (List) q.execute();
        assertNotNull("No results from query!", results);
        assertEquals("Number of Employees with more than average salary was wrong", 1, results.size());
        // Don't commit the data
        tx.rollback();
    } catch (JDOUserException e) {
        e.printStackTrace();
        fail(e.getMessage());
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
}
Also used : Employee(org.jpox.samples.models.company.Employee) Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) DatastoreAdapter(org.datanucleus.store.rdbms.adapter.DatastoreAdapter) List(java.util.List) JDOUserException(javax.jdo.JDOUserException) RDBMSStoreManager(org.datanucleus.store.rdbms.RDBMSStoreManager)

Aggregations

Employee (org.jpox.samples.models.company.Employee)129 PersistenceManager (javax.jdo.PersistenceManager)126 Transaction (javax.jdo.Transaction)119 Query (javax.jdo.Query)68 JDOUserException (javax.jdo.JDOUserException)62 Manager (org.jpox.samples.models.company.Manager)46 List (java.util.List)44 JDOPersistenceManager (org.datanucleus.api.jdo.JDOPersistenceManager)37 JDOObjectNotFoundException (javax.jdo.JDOObjectNotFoundException)24 Person (org.jpox.samples.models.company.Person)24 Collection (java.util.Collection)21 PersistenceManagerFactory (javax.jdo.PersistenceManagerFactory)18 Iterator (java.util.Iterator)17 Properties (java.util.Properties)17 JDODetachedFieldAccessException (javax.jdo.JDODetachedFieldAccessException)16 SQLException (java.sql.SQLException)12 StoreManager (org.datanucleus.store.StoreManager)12 Department (org.jpox.samples.models.company.Department)12 ArrayList (java.util.ArrayList)11 HashSet (java.util.HashSet)11