Search in sources :

Example 61 with Employee

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

the class MultithreadPMTest method testMultipleDetachCopy.

public void testMultipleDetachCopy() {
    Properties multiProps = new Properties();
    multiProps.setProperty(PropertyNames.PROPERTY_MULTITHREADED, "true");
    PersistenceManagerFactory myPMF = getPMF(1, multiProps);
    try {
        int THREAD_SIZE = 1000;
        Thread[] threads = new Thread[THREAD_SIZE];
        MultithreadDetachRunner[] runner = new MultithreadDetachRunner[THREAD_SIZE];
        PersistenceManager pm = myPMF.getPersistenceManager();
        pm.currentTransaction().begin();
        Employee woody = new Employee(1, "Woody", "Woodpecker", "woody@woodpecker.com", 13, "serial 1", new Integer(10));
        Manager bart = new Manager(2, "Bart", "Simpson", "bart@simpson.com", 2, "serial 2");
        woody.setManager(bart);
        pm.makePersistent(woody);
        pm.currentTransaction().commit();
        pm.currentTransaction().begin();
        try {
            for (int i = 0; i < THREAD_SIZE; i++) {
                runner[i] = new MultithreadDetachRunner(pm, woody);
                threads[i] = new Thread(runner[i]);
                threads[i].start();
            }
            for (int i = 0; i < THREAD_SIZE; i++) {
                threads[i].join();
                if (runner[i].getException() != null) {
                    LOG.error("Exception during test", runner[i].getException());
                    fail("Exception thrown during test : " + runner[i].getException());
                }
            }
        } catch (Exception e) {
            fail(e.getMessage());
        } finally {
            if (pm.currentTransaction().isActive()) {
                pm.currentTransaction().rollback();
            }
            pm.close();
        }
    } finally {
        CompanyHelper.clearCompanyData(myPMF);
        myPMF.close();
    }
}
Also used : Employee(org.jpox.samples.models.company.Employee) PersistenceManager(javax.jdo.PersistenceManager) PersistenceManagerFactory(javax.jdo.PersistenceManagerFactory) Properties(java.util.Properties) Manager(org.jpox.samples.models.company.Manager) PersistenceManager(javax.jdo.PersistenceManager)

Example 62 with Employee

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

the class JDOQLBasicTest method testStringToUpperCase.

/**
 * Test the use of the String.toUpperCase() method.
 */
public void testStringToUpperCase() {
    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.toUpperCase() == \"WOODPECKER\"");
                HashSet results = new HashSet((Collection) q.execute());
                assertEquals("Received incorrect number of results to String.toUpperCase()", 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\" == firstName.toUpperCase()");
                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.toUpperCase() == lastName.toUpperCase()");
                results = new HashSet((Collection) q.execute("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 63 with Employee

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

the class JDOQLBasicTest method testInstanceof.

/**
 * Test case to use JDOQL "instanceof"
 */
public void testInstanceof() {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            // 1. instanceof where the candidate class uses "new-table" (union)
            tx.begin();
            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!
            Manager roadrunner = new Manager(4, "Road", "Runner", "road.runner@warnerbros.com", 11, "serial 4");
            pm.makePersistent(woody);
            pm.makePersistent(bart);
            pm.makePersistent(bunny);
            pm.makePersistent(roadrunner);
            tx.commit();
            tx.begin();
            try {
                Query q = pm.newQuery(Employee.class, "this instanceof " + Manager.class.getName());
                List results = (List) q.execute();
                assertEquals("Number of objects returned from instanceof was incorrect", results.size(), 1);
                Object obj = results.get(0);
                assertTrue("Type of object returned should have been Manager but wasnt", obj instanceof Manager);
                q.closeAll();
            } catch (JDOUserException e) {
                fail(e.getMessage());
            }
            tx.commit();
            // 2. instanceof where the candidate class using "superclass-table" (discriminator)
            tx.begin();
            ElectronicJournal elecJournal = new ElectronicJournal(1, "Electronics Weekly");
            PrintJournal printJournal = new PrintJournal(2, "Glossy magazine");
            pm.makePersistent(elecJournal);
            pm.makePersistent(printJournal);
            tx.commit();
            tx.begin();
            try {
                Query q = pm.newQuery(AbstractJournal.class, "this instanceof " + PrintJournal.class.getName());
                List results = (List) q.execute();
                assertEquals("Number of objects returned from instanceof was incorrect", results.size(), 1);
                Object obj = results.get(0);
                assertTrue("Type of object returned should have been PrintJournal but wasnt", obj instanceof PrintJournal);
                q.closeAll();
            } catch (JDOUserException e) {
                fail(e.getMessage());
            }
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        // Clean out our data
        clean(PrintJournal.class);
        clean(ElectronicJournal.class);
        CompanyHelper.clearCompanyData(pmf);
    }
}
Also used : Employee(org.jpox.samples.models.company.Employee) Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) ElectronicJournal(org.jpox.samples.one_one.bidir_3.ElectronicJournal) List(java.util.List) ArrayList(java.util.ArrayList) Manager(org.jpox.samples.models.company.Manager) StoreManager(org.datanucleus.store.StoreManager) RDBMSStoreManager(org.datanucleus.store.rdbms.RDBMSStoreManager) PersistenceManager(javax.jdo.PersistenceManager) JDOUserException(javax.jdo.JDOUserException) PrintJournal(org.jpox.samples.one_one.bidir_3.PrintJournal)

Example 64 with Employee

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

the class JDOQLBasicTest method testAvgWithHaving.

public void testAvgWithHaving() {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        Employee woody = new Employee(1, "Woody", "Woodpecker", "woody@woodpecker.com", 10, "serial 1");
        Employee bunny = new Employee(3, "Bugs", "Bunny", "bugs.bunny@warnerbros.com", 5, "serial 3");
        Employee bart = new Employee(2, "Bart", "Simpson", "bart@simpson.com", 5, "serial 2");
        woody.setAge(30);
        bunny.setAge(30);
        bart.setAge(15);
        pm.makePersistent(woody);
        pm.makePersistent(bart);
        pm.makePersistent(bunny);
        tx.commit();
        tx.begin();
        try {
            LOG.info(">> QUERY WITH GROUPBY AVG");
            Query q = pm.newQuery("SELECT age, avg(salary) FROM " + Employee.class.getName() + " GROUP BY age HAVING avg(salary) > 6");
            List results = (List) q.execute();
            // Create a nice representation of the result for error reporting
            StringBuffer resultString = new StringBuffer();
            for (Object object : results) {
                resultString.append(Arrays.toString((Object[]) object));
            }
            assertEquals("Did not return the correct number of age groups: " + resultString, 1, results.size());
            Object[] ageGroup = (Object[]) results.get(0);
            assertEquals("Did not return the correct age group: " + resultString, 30, ageGroup[0]);
            assertEquals("Did not return the correct avg salary: " + resultString, 7.5, ageGroup[1]);
            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) List(java.util.List) ArrayList(java.util.ArrayList) JDOUserException(javax.jdo.JDOUserException)

Example 65 with Employee

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

the class JDOQLBasicTest method testStringToLowerCase.

/**
 * Test the use of the String.toLowerCase() method.
 */
public void testStringToLowerCase() {
    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.toLowerCase() == \"woodpecker\"");
                HashSet results = new HashSet((Collection) q.execute());
                assertEquals("Received incorrect number of results to String.toUpperCase()", 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\" == firstName.toLowerCase()");
                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.toLowerCase() == lastName.toLowerCase()");
                results = new HashSet((Collection) q.execute("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)

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