Search in sources :

Example 66 with Employee

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

the class JDOQLBasicTest method testNamedQueries.

/**
 * Test case to use the JDO 2.0 named queries defined in MetaData.
 */
public void testNamedQueries() {
    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");
        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();
            // Simple named query with all in the query tag
            try {
                Query q = pm.newNamedQuery(org.jpox.samples.models.company.Employee.class, "SalaryBelow12");
                Collection results = (Collection) q.execute();
                assertTrue("Named Query \"SalaryBelow12\" returned an incorrect number of Employees : expected 2 but returned " + results.size(), results.size() == 2);
                q.closeAll();
            } catch (JDOUserException e) {
                fail(e.getMessage());
            }
            // Named query with filter and declare tags
            try {
                Query q = pm.newNamedQuery(org.jpox.samples.models.company.Employee.class, "SerialNoStartsWith");
                Collection results = (Collection) q.execute("serial");
                assertTrue("Named Query \"SerialNoStartsWith\" returned an incorrect number of Employees : expected 4 but returned " + results.size(), results.size() == 4);
                q.closeAll();
            } catch (JDOUserException 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) JDOUserException(javax.jdo.JDOUserException)

Example 67 with Employee

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

the class JDOQLBasicTest method testCharLiteralInvalidComparison.

/**
 * Test case to see what happens when we compare a CharacterLiteral with an invalid value.
 * It is arguable that this test is pointless because the user has fscked up the query
 * but it currently gives a NullPointerException which should be avoided.
 */
public void testCharLiteralInvalidComparison() {
    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");
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            // Persist some data to play with
            tx.begin();
            pm.makePersistent(woody);
            pm.makePersistent(bart);
            pm.makePersistent(bunny);
            pm.makePersistent(roadrunner);
            tx.commit();
            // Query using a CharacterLiteral == int
            tx.begin();
            try {
                Query q = pm.newQuery(pm.getExtent(Employee.class, false));
                q.setFilter("'A' == 65");
                List results = (List) q.execute();
                assertTrue("Test for a CharLiteral with invalid clause returned objects!", results != null && results.size() == 4);
                q.closeAll();
                q = pm.newQuery(pm.getExtent(Employee.class, false));
                q.setFilter("'A' != 65");
                results = (List) q.execute();
                assertTrue("Test for a CharLiteral with invalid clause returned objects!", results != null && results.size() == 0);
                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) List(java.util.List) ArrayList(java.util.ArrayList) JDOException(javax.jdo.JDOException) JDOUserException(javax.jdo.JDOUserException) NoSuchElementException(java.util.NoSuchElementException)

Example 68 with Employee

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

the class JDOQLBasicTest method testSingleQuotes.

/**
 * Test case to use single quoted strings.
 */
public void testSingleQuotes() {
    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");
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            // Persist some data to play with
            tx.begin();
            pm.makePersistent(woody);
            pm.makePersistent(bart);
            pm.makePersistent(bunny);
            pm.makePersistent(roadrunner);
            tx.commit();
            // Query using a single-quoted StringLiteral
            tx.begin();
            try {
                Query q = pm.newQuery(pm.getExtent(Employee.class, false));
                q.setFilter("firstName == 'Bugs'");
                List results = (List) q.execute();
                assertTrue("Test for a singly-quoted string returned no objects!", results != null && results.size() > 0);
                assertTrue("Test for a singly-quoted string returned incorrect number of objects - returned " + results.size() + " but should have been 1", results.size() == 1);
                q.closeAll();
            } catch (JDOUserException e) {
                LOG.error(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) List(java.util.List) ArrayList(java.util.ArrayList) JDOUserException(javax.jdo.JDOUserException)

Example 69 with Employee

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

the class JDOQLBasicTest method testStringEndsWith.

/**
 * Test the use of the String.endsWith() method.
 */
public void testStringEndsWith() {
    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 {
            tx.begin();
            pm.makePersistent(woody);
            pm.makePersistent(bart);
            tx.commit();
            tx.begin();
            try {
                // test 1
                Query q = pm.newQuery(pm.getExtent(org.jpox.samples.models.company.Employee.class, false));
                q.setFilter("lastName.endsWith(\"pecker\")");
                HashSet results = new HashSet((Collection) q.execute());
                assertEquals("Received incorrect number of results to String.endsWith()", results.size(), 1);
                Employee emp = (Employee) results.iterator().next();
                assertEquals("First name of retrieved object is incorrect", emp.getFirstName(), "Woody");
                assertEquals("Email of retrieved object is incorrect", emp.getEmailAddress(), "Woody@woodpecker.com");
                q.closeAll();
                // test 2
                q = pm.newQuery(pm.getExtent(org.jpox.samples.models.company.Employee.class, false));
                q.setFilter("\"Woody Woodpecker\".endsWith(lastName)");
                results = new HashSet((Collection) q.execute());
                assertTrue("received: " + results, results.size() == 1);
                q.closeAll();
                // test 3
                q = pm.newQuery(pm.getExtent(org.jpox.samples.models.company.Employee.class, false));
                q.declareParameters("java.lang.String var1");
                q.setFilter("var1.endsWith(lastName)");
                results = new HashSet((Collection) q.execute("Woody Woodpecker"));
                assertTrue("received: " + results, results.size() == 1);
                q.closeAll();
            } catch (JDOUserException 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
        clean(Employee.class);
    }
}
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) JDOUserException(javax.jdo.JDOUserException) HashSet(java.util.HashSet)

Example 70 with Employee

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

the class JDOQLBasicTest method testStringSubstring.

/**
 * Test the use of the String.substring() method.
 */
public void testStringSubstring() {
    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 {
            tx.begin();
            pm.makePersistent(woody);
            pm.makePersistent(bart);
            tx.commit();
            tx.begin();
            try {
                // test 1
                Query q = pm.newQuery(pm.getExtent(org.jpox.samples.models.company.Employee.class, false));
                q.setFilter("firstName.substring(2) == \"ody\"");
                HashSet results = new HashSet((Collection) q.execute());
                assertEquals("Received incorrect number of results to String.substring()", results.size(), 1);
                Employee emp = (Employee) results.iterator().next();
                assertEquals("First name of retrieved object is incorrect", emp.getFirstName(), "Woody");
                assertEquals("Email of retrieved object is incorrect", emp.getEmailAddress(), "Woody@woodpecker.com");
                q.closeAll();
            } catch (JDOUserException 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
        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) HashSet(java.util.HashSet)

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