Search in sources :

Example 1 with Office

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

the class ReadUncommittedIsolationLevelTest method testReadUncommited.

public void testReadUncommited() {
    if (!pmf.supportedOptions().contains("javax.jdo.option.TransactionIsolationLevel.read-uncommitted")) {
        // Datastore doesn't support this isolation level
        return;
    } else if (vendorID != null && vendorID.equalsIgnoreCase("hsql")) {
        // HSQL 2.x can lock up on this
        return;
    }
    // Add some initial data
    pm = pmf.getPersistenceManager();
    pm.setProperty(PropertyNames.PROPERTY_CACHE_L2_TYPE, "none");
    Transaction tx = pm.currentTransaction();
    Object oid;
    try {
        tx.begin();
        // create sample data
        Office o = new Office(1L, ROOM, "desc");
        o = pm.makePersistent(o);
        oid = pm.getObjectId(o);
        tx.commit();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
    String finalDescription = null;
    PersistenceManager pm1 = pmf.getPersistenceManager();
    PersistenceManager pm2 = pmf.getPersistenceManager();
    Transaction tx1 = pm1.currentTransaction();
    Transaction tx2 = pm2.currentTransaction();
    try {
        pm1.setProperty(PropertyNames.PROPERTY_CACHE_L2_TYPE, "none");
        tx1.setIsolationLevel(Constants.TX_READ_UNCOMMITTED);
        tx1.begin();
        pm2.setProperty(PropertyNames.PROPERTY_CACHE_L2_TYPE, "none");
        tx2.setIsolationLevel(Constants.TX_READ_UNCOMMITTED);
        tx2.begin();
        Office o1 = (Office) pm1.getObjectById(oid);
        LOG.info("within tx1 after modifying:" + o1.asString());
        finalDescription = o1.getDescription() + o1.getRoomName();
        o1.setDescription(finalDescription);
        // send UPDATE to database
        pm1.flush();
        LOG.info("within tx1 after modifying:" + o1.asString());
        Office o2 = (Office) pm2.getObjectById(oid);
        LOG.info("within tx2: " + o2.asString());
        assertEquals("uncommited modification not seen", finalDescription, o2.getDescription());
    } catch (JDODataStoreException e) {
        assertFalse("Should be able to see description " + finalDescription + " but " + e.getMessage(), true);
    } finally {
        tx2.commit();
        pm2.close();
        tx1.commit();
        pm1.close();
        clean(Office.class);
        clean(Department.class);
    }
}
Also used : Office(org.jpox.samples.models.company.Office) Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) JDODataStoreException(javax.jdo.JDODataStoreException)

Example 2 with Office

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

the class JDOQLBasicTest method testOrderNullsFirstLast.

/**
 * Tests the extension for location of NULLS within an ORDER BY clause.
 */
public void testOrderNullsFirstLast() {
    RDBMSStoreManager srm = (RDBMSStoreManager) storeMgr;
    if (!srm.getDatastoreAdapter().supportsOption(DatastoreAdapter.ORDERBY_NULLS_DIRECTIVES)) {
        // Not supported so it passed :-)
        return;
    }
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        // Persist some simple objects (uses datastore id, or composite application id depending on suite)
        tx.begin();
        for (int i = 0; i < 3; i++) {
            Office off = new Office(i, "Colour" + i, null);
            pm.makePersistent(off);
        }
        Office o1 = new Office(3, "Green", "Big spacious office");
        Office o2 = new Office(4, "Blue", "Pokey office at the back of the building");
        Office o3 = new Office(5, "Yellow", "Massive open plan office");
        pm.makePersistent(o1);
        pm.makePersistent(o2);
        pm.makePersistent(o3);
        tx.commit();
        tx.begin();
        Query q = pm.newQuery("SELECT FROM " + Office.class.getName() + " ORDER BY description NULLS FIRST");
        List<Office> offices = (List<Office>) q.execute();
        assertEquals(6, offices.size());
        Iterator<Office> it = offices.iterator();
        assertNull("First result (NULLS FIRST) should have null description", it.next().getDescription());
        assertNull("Second result (NULLS FIRST) should have null description", it.next().getDescription());
        assertNull("Third result (NULLS FIRST) should have null description", it.next().getDescription());
        assertNotNull("Fourth result (NULLS FIRST) should have not null description", it.next().getDescription());
        assertNotNull("Fifth result (NULLS FIRST) should have not null description", it.next().getDescription());
        assertNotNull("Sixth result (NULLS FIRST) should have not null description", it.next().getDescription());
        q = pm.newQuery("SELECT FROM " + Office.class.getName() + " ORDER BY description NULLS LAST");
        offices = (List<Office>) q.execute();
        assertEquals(6, offices.size());
        it = offices.iterator();
        assertNotNull("First result (NULLS FIRST) should have not null description", it.next().getDescription());
        assertNotNull("Second result (NULLS FIRST) should have not null description", it.next().getDescription());
        assertNotNull("Third result (NULLS FIRST) should have not null description", it.next().getDescription());
        assertNull("Fourth result (NULLS FIRST) should have null description", it.next().getDescription());
        assertNull("Fifth result (NULLS FIRST) should have null description", it.next().getDescription());
        assertNull("Sixth result (NULLS FIRST) should have null description", it.next().getDescription());
        tx.commit();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
        // Clean out our data
        clean(Office.class);
    }
}
Also used : Office(org.jpox.samples.models.company.Office) Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) List(java.util.List) RDBMSStoreManager(org.datanucleus.store.rdbms.RDBMSStoreManager)

Example 3 with Office

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

the class QueryTimeoutTest method setUp.

protected void setUp() throws Exception {
    super.setUp();
    LOG.info("starting QueryTimeoutTest setUp");
    PersistenceManager pm = pmf.getPersistenceManager();
    pm.setProperty(PropertyNames.PROPERTY_CACHE_L2_TYPE, "none");
    final Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        Office r;
        r = new Office(0, "-x", DESC_PFX + "foo");
        pm.makePersistent(r);
        r = new Office(1, "-y", DESC_PFX + "bar");
        pm.makePersistent(r);
        tx.commit();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
    LOG.info("ended setUp");
}
Also used : Office(org.jpox.samples.models.company.Office)

Example 4 with Office

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

the class JDOQLBasicTest method testQueryUnboundVariables.

public void testQueryUnboundVariables() {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            Office o1 = new Office(1, "Green", "Big spacious office");
            Office o2 = new Office(2, "Blue", "Pokey office at the back of the building");
            Office o3 = new Office(1, "Yellow", "Massive open plan office");
            Department d1 = new Department("Finance");
            Department d2 = new Department("Customer Support");
            Department d3 = new Department("Sales");
            Department d4 = new Department("IT");
            o1.addDepartment(d1);
            o1.addDepartment(d3);
            o2.addDepartment(d2);
            o3.addDepartment(d4);
            o3.addDepartment(d3);
            pm.makePersistent(o1);
            pm.makePersistent(o2);
            pm.makePersistent(o3);
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception during test", e);
            fail("Error persisting sample data : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            Query q = pm.newQuery(Office.class);
            q.setFilter("dept.name.equals(\"Finance\") && departments.contains(dept)");
            q.declareVariables("Department dept");
            q.declareImports("import org.jpox.samples.models.company.Department");
            List l = new ArrayList((Collection) q.execute());
            assertEquals(1, l.size());
            q = pm.newQuery(Office.class);
            q.setFilter("dept.name.equals(\"Sales\") && departments.contains(dept)");
            q.declareVariables("Department dept");
            q.declareImports("import org.jpox.samples.models.company.Department");
            q.setOrdering("roomName ascending");
            l = new ArrayList((Collection) q.execute());
            assertEquals(2, l.size());
            assertEquals(((Office) l.get(0)).getRoomName(), "Green");
            assertEquals(((Office) l.get(1)).getRoomName(), "Yellow");
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        // Clean out our data
        CompanyHelper.clearCompanyData(pmf);
    }
}
Also used : Office(org.jpox.samples.models.company.Office) Department(org.jpox.samples.models.company.Department) InsuranceDepartment(org.jpox.samples.models.company.InsuranceDepartment) Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) ArrayList(java.util.ArrayList) Collection(java.util.Collection) List(java.util.List) ArrayList(java.util.ArrayList) JDOException(javax.jdo.JDOException) JDOUserException(javax.jdo.JDOUserException) NoSuchElementException(java.util.NoSuchElementException)

Example 5 with Office

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

the class JDOQLBasicTest method testOrderingAndFunction2Parms.

public void testOrderingAndFunction2Parms() {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            Office o1 = new Office(1, "Green", "Big spacious office");
            Office o2 = new Office(2, "Blue", "Pokey office at the back of the building");
            Office o3 = new Office(1, "Yellow", "Massive open plan office");
            Department d1 = new Department("Finance");
            Department d2 = new Department("Customer Support");
            Department d3 = new Department("Sales");
            Department d4 = new Department("IT");
            o1.addDepartment(d1);
            o1.addDepartment(d3);
            o2.addDepartment(d2);
            o3.addDepartment(d4);
            o3.addDepartment(d3);
            pm.makePersistent(o1);
            pm.makePersistent(o2);
            pm.makePersistent(o3);
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception during test", e);
            fail("Error persisting sample data : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            Query q = pm.newQuery(Office.class);
            q.setFilter("dept.name.equals(\"Finance\") && departments.contains(dept)");
            q.declareVariables("Department dept");
            q.declareImports("import org.jpox.samples.models.company.Department");
            List l = new ArrayList((Collection) q.execute());
            assertEquals(1, l.size());
            q = pm.newQuery(Office.class);
            q.setFilter("dept.name.equals(\"Sales\") && departments.contains(dept)");
            q.declareVariables("Department dept");
            q.declareImports("import org.jpox.samples.models.company.Department");
            q.setOrdering("roomName.substring(0,10) ascending, description ascending");
            l = new ArrayList((Collection) q.execute());
            assertEquals(2, l.size());
            assertEquals(((Office) l.get(0)).getRoomName(), "Green");
            assertEquals(((Office) l.get(1)).getRoomName(), "Yellow");
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        // Clean out our data
        CompanyHelper.clearCompanyData(pmf);
    }
}
Also used : Office(org.jpox.samples.models.company.Office) Department(org.jpox.samples.models.company.Department) InsuranceDepartment(org.jpox.samples.models.company.InsuranceDepartment) Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) ArrayList(java.util.ArrayList) Collection(java.util.Collection) List(java.util.List) ArrayList(java.util.ArrayList) JDOException(javax.jdo.JDOException) JDOUserException(javax.jdo.JDOUserException) NoSuchElementException(java.util.NoSuchElementException)

Aggregations

Office (org.jpox.samples.models.company.Office)16 PersistenceManager (javax.jdo.PersistenceManager)15 Transaction (javax.jdo.Transaction)15 Query (javax.jdo.Query)14 Collection (java.util.Collection)8 JDOUserException (javax.jdo.JDOUserException)8 List (java.util.List)6 JDOException (javax.jdo.JDOException)6 Department (org.jpox.samples.models.company.Department)6 InsuranceDepartment (org.jpox.samples.models.company.InsuranceDepartment)6 ArrayList (java.util.ArrayList)5 Iterator (java.util.Iterator)4 ListIterator (java.util.ListIterator)3 Manager (org.jpox.samples.models.company.Manager)3 NoSuchElementException (java.util.NoSuchElementException)2 RDBMSStoreManager (org.datanucleus.store.rdbms.RDBMSStoreManager)2 Calendar (java.util.Calendar)1 JDODataStoreException (javax.jdo.JDODataStoreException)1