Search in sources :

Example 56 with Employee

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

the class JDOQLInMemoryTest method testInstanceOf.

/**
 * Test use of instanceof operator.
 */
public void testInstanceOf() {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        // Persist 2 Employees
        Employee emp1 = new Employee(101, "F1", "S1", "f1.s1@company.com", 100f, "10001");
        Employee emp2 = new Employee(102, "F2", "S2", "f2.s2@company.com", 200f, "10002");
        pm.makePersistent(emp1);
        pm.makePersistent(emp2);
        pm.flush();
        // Find all Employee objects
        Query q = pm.newQuery(getQueryLanguage(), "SELECT FROM " + Person.class.getName() + " WHERE this instanceof Employee");
        q.addExtension("datanucleus.query.evaluateInMemory", "true");
        List results = (List) q.execute();
        assertNotNull("No results from query!", results);
        assertEquals("Number of Employees was incorrect", 2, results.size());
        Iterator iter = results.iterator();
        while (iter.hasNext()) {
            Object obj = iter.next();
            assertTrue("Returned object is not an Employee!", obj instanceof Employee);
        }
        tx.commit();
    } catch (JDOException e) {
        LOG.error("Exception in execution of query", e);
        fail(e.getMessage());
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
        clean(Employee.class);
    }
}
Also used : Employee(org.jpox.samples.models.company.Employee) 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) JDOException(javax.jdo.JDOException)

Example 57 with Employee

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

the class JDOQLInMemoryTest method testCast.

/**
 * Test use of a cast operator.
 */
public void testCast() {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        // Persist 2 Employees
        Employee emp1 = new Employee(101, "F1", "S1", "f1.s1@company.com", 100f, "10001");
        Employee emp2 = new Employee(102, "F2", "S2", "f2.s2@company.com", 200f, "10002");
        pm.makePersistent(emp1);
        pm.makePersistent(emp2);
        pm.flush();
        // Find the Employee with the specified serial number
        Query q = pm.newQuery(getQueryLanguage(), "SELECT FROM " + Person.class.getName() + " WHERE this instanceof Employee && ((Employee)this).serialNo == \"10001\"");
        q.addExtension("datanucleus.query.evaluateInMemory", "true");
        List results = (List) q.execute();
        assertNotNull("No results from query!", results);
        assertEquals("Number of Employees with serial number was incorrect", 1, results.size());
        Object obj = results.get(0);
        assertTrue("Object is not Employee", obj instanceof Employee);
        Employee emp = (Employee) obj;
        assertEquals("Employee first name is incorrect", "F1", emp.getFirstName());
        assertEquals("Employee last name is incorrect", "S1", emp.getLastName());
        assertEquals("Employee serial number is incorrect", "10001", emp.getSerialNo());
        tx.commit();
    } catch (JDOException e) {
        LOG.error("Exception in execution of query", e);
        fail(e.getMessage());
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
        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) Person(org.jpox.samples.models.company.Person) JDOException(javax.jdo.JDOException)

Example 58 with Employee

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

the class MultithreadedTest method testQueryAndDetach.

/**
 * Test that populates the datastore, and then starts many threads querying and detaching the data
 * and trying to access the detached data (checking for undetached fields that should have been detached).
 */
public void testQueryAndDetach() {
    addClassesToSchema(new Class[] { Employee.class, Manager.class, Developer.class });
    try {
        // Persist some data
        LOG.debug(">> Persisting data");
        PersistenceManager pm = pmf.getPersistenceManager();
        pm.setProperty(PropertyNames.PROPERTY_DETACH_ON_CLOSE, "true");
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            Manager mgr = new Manager(1, "The", "Boss", "the.boss@datanucleus.com", 200000, "100000");
            pm.makePersistent(mgr);
            for (int i = 0; i < 100; i++) {
                Employee emp = new Employee(i + 2, "FirstName" + i, "LastName" + i, "first.last." + i + "@datanucleus.com", 100000 + i, "12345" + i);
                emp.setManager(mgr);
                mgr.addSubordinate(emp);
                pm.makePersistent(emp);
            }
            tx.commit();
        } catch (Throwable thr) {
            LOG.error("Exception persisting objects", thr);
            fail("Exception persisting data : " + thr.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        LOG.debug(">> Persisted data");
        // Verify the persistence
        pm = pmf.getPersistenceManager();
        pm.setProperty(PropertyNames.PROPERTY_DETACH_ON_CLOSE, "true");
        tx = pm.currentTransaction();
        try {
            tx.begin();
            LOG.debug(">> Querying Employees");
            Query<Employee> q = pm.newQuery(Employee.class);
            List<Employee> emps = q.executeList();
            for (Employee e : emps) {
                LOG.debug(">> emp=" + e + " e.mgr=" + e.getManager());
            }
            LOG.debug(">> Queried Employees");
            tx.commit();
        } catch (Throwable thr) {
            LOG.error("Exception checking objects", thr);
            fail("Exception checking data : " + thr.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // Create the Threads
        int THREAD_SIZE = 500;
        final String[] threadErrors = new String[THREAD_SIZE];
        Thread[] threads = new Thread[THREAD_SIZE];
        for (int i = 0; i < THREAD_SIZE; i++) {
            final int threadNo = i;
            threads[i] = new Thread(new Runnable() {

                public void run() {
                    String errorMsg = processQueryAndDetach(true);
                    threadErrors[threadNo] = errorMsg;
                }
            });
        }
        // Run the Threads
        LOG.debug(">> Starting threads");
        for (int i = 0; i < THREAD_SIZE; i++) {
            threads[i].start();
        }
        for (int i = 0; i < THREAD_SIZE; i++) {
            try {
                threads[i].join();
            } catch (InterruptedException e) {
                fail(e.getMessage());
            }
        }
        LOG.debug(">> Completed threads");
        // Process any errors and fail the test if any threads failed present
        for (String error : threadErrors) {
            if (error != null) {
                fail(error);
            }
        }
    } finally {
        CompanyHelper.clearCompanyData(pmf);
    }
}
Also used : PersistenceManager(javax.jdo.PersistenceManager) Manager(org.jpox.samples.models.company.Manager) PersistenceManager(javax.jdo.PersistenceManager) Employee(org.jpox.samples.models.company.Employee) Transaction(javax.jdo.Transaction)

Example 59 with Employee

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

the class MultithreadPMTest method testMultipleTransitionWrite.

/**
 * Test changing the state
 */
public void testMultipleTransitionWrite() {
    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];
        PersistenceManager pm = myPMF.getPersistenceManager();
        pm.currentTransaction().begin();
        final 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");
        final Manager boss = new Manager(3, "Boss", "WakesUp", "boss@wakes.up", 4, "serial 3");
        woody.setManager(bart);
        pm.makePersistent(woody);
        pm.currentTransaction().commit();
        pm.currentTransaction().begin();
        try {
            for (int i = 0; i < THREAD_SIZE; i++) {
                threads[i] = new Thread(new Runnable() {

                    public void run() {
                        woody.setLastName("name");
                        woody.setManager(boss);
                    }
                });
            }
            for (int i = 0; i < THREAD_SIZE; i++) {
                threads[i].start();
            }
            for (int i = 0; i < THREAD_SIZE; i++) {
                try {
                    threads[i].join();
                } catch (InterruptedException 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 60 with Employee

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

the class MultithreadPMTest method testMultipleNonTransitionWrite.

/**
 * Test changing the state
 */
public void testMultipleNonTransitionWrite() {
    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];
        PersistenceManager pm = myPMF.getPersistenceManager();
        pm.currentTransaction().begin();
        final 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");
        final Manager boss = new Manager(3, "Boss", "WakesUp", "boss@wakes.up", 4, "serial 3");
        woody.setManager(bart);
        pm.makePersistent(woody);
        pm.currentTransaction().commit();
        pm.currentTransaction().setNontransactionalWrite(true);
        try {
            for (int i = 0; i < THREAD_SIZE; i++) {
                threads[i] = new Thread(new Runnable() {

                    public void run() {
                        woody.setLastName("name");
                        woody.setManager(boss);
                    }
                });
            }
            for (int i = 0; i < THREAD_SIZE; i++) {
                threads[i].start();
            }
            for (int i = 0; i < THREAD_SIZE; i++) {
                try {
                    threads[i].join();
                } catch (InterruptedException 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)

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