Search in sources :

Example 6 with Office

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

the class JDOQLContainerTest method testNegateContains.

/**
 * Test of !contains in a JDOQL statement.
 */
public void testNegateContains() {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            // Persist some data
            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();
            tx.begin();
            Query q = pm.newQuery(Office.class);
            q.setFilter("!(departments.contains(dept) && dept.name.equals(\"Sales\"))");
            q.declareVariables("Department dept");
            q.declareImports("import org.jpox.samples.models.company.Department");
            Collection c = (Collection) q.execute();
            // Only Office 2 doesnt have "Sales"
            assertEquals(1, c.size());
            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) Collection(java.util.Collection)

Example 7 with Office

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

the class JDOQLResultTest method testSetResultJDOHelperGetObjectID.

/**
 * Tests the use of JDOHelper.getObjectId() as the result.
 */
public void testSetResultJDOHelperGetObjectID() {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        // Persist some simple objects
        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");
        pm.newQuery(Office.class).deletePersistentAll();
        pm.makePersistent(o1);
        pm.makePersistent(o2);
        pm.makePersistent(o3);
        tx.commit();
        Object[] officeIds = new Object[3];
        officeIds[0] = pm.getObjectId(o1);
        officeIds[1] = pm.getObjectId(o2);
        officeIds[2] = pm.getObjectId(o3);
        tx.begin();
        Query q = pm.newQuery("SELECT JDOHelper.getObjectId(this) FROM " + Office.class.getName());
        List results = (List) q.execute();
        assertEquals("Number of results is incorrect", 3, results.size());
        Iterator iter = results.iterator();
        boolean[] present = new boolean[] { false, false, false };
        while (iter.hasNext()) {
            Object id = iter.next();
            if (id.equals(officeIds[0])) {
                present[0] = true;
            }
            if (id.equals(officeIds[1])) {
                present[1] = true;
            }
            if (id.equals(officeIds[2])) {
                present[2] = true;
            }
        }
        for (int i = 0; i < 3; i++) {
            assertTrue("id " + i + " is not present", present[i]);
        }
        q.closeAll();
        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) ListIterator(java.util.ListIterator) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List)

Example 8 with Office

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

the class JDOQLBasicTest method testStringMatches.

/**
 * Tests the String.matches(pattern) expression
 */
public void testStringMatches() {
    try {
        Object[] ids = new Object[7];
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            Office[] offices = new Office[7];
            offices[0] = new Office(1, "Green", "downtown");
            offices[1] = new Office(1, "Red", "village");
            offices[2] = new Office(1, "Blue", "spring_field");
            offices[3] = new Office(1, "Orange", "percent%city");
            offices[4] = new Office(1, "Yellow", "slash\\city");
            offices[5] = new Office(1, "Grey", "Some name or other (Nr. 1507)");
            offices[6] = new Office(1, "Gold", "Some name or other (Nr# 1507)");
            pm.makePersistentAll(offices);
            tx.commit();
            ids[0] = JDOHelper.getObjectId(offices[0]);
            ids[1] = JDOHelper.getObjectId(offices[1]);
            ids[2] = JDOHelper.getObjectId(offices[2]);
            ids[3] = JDOHelper.getObjectId(offices[3]);
            ids[4] = JDOHelper.getObjectId(offices[4]);
            ids[5] = JDOHelper.getObjectId(offices[5]);
            ids[6] = JDOHelper.getObjectId(offices[6]);
            tx.begin();
            Query q = pm.newQuery(Office.class, "this.description.matches(\"village\")");
            Collection c = q.executeList();
            Assert.assertEquals(1, c.size());
            Assert.assertEquals(ids[1], JDOHelper.getObjectId(c.iterator().next()));
            q = pm.newQuery(Office.class, "this.description.matches(\".illag.\")");
            c = q.executeList();
            Assert.assertEquals(1, c.size());
            Assert.assertEquals(ids[1], JDOHelper.getObjectId(c.iterator().next()));
            q = pm.newQuery(Office.class, "this.description.matches(\".*illag.\")");
            c = q.executeList();
            Assert.assertEquals(1, c.size());
            Assert.assertEquals(ids[1], JDOHelper.getObjectId(c.iterator().next()));
            q = pm.newQuery(Office.class, "this.description.matches(\"(?i).*ILLAGE\")");
            c = q.executeList();
            Assert.assertEquals(1, c.size());
            Assert.assertEquals(ids[1], JDOHelper.getObjectId(c.iterator().next()));
            q = pm.newQuery(Office.class, "this.description.toUpperCase().matches(\".*ILLAGE\")");
            c = q.executeList();
            Assert.assertEquals(1, c.size());
            Assert.assertEquals(ids[1], JDOHelper.getObjectId(c.iterator().next()));
            q = pm.newQuery(Office.class, "this.description.toUpperCase().matches(\".*ILLAGA\")");
            c = q.executeList();
            Assert.assertEquals(0, c.size());
            q = pm.newQuery(Office.class, "this.description.toUpperCase().matches(\".*\")");
            c = q.executeList();
            Assert.assertEquals(offices.length, c.size());
            q = pm.newQuery(Office.class, "this.description.matches(\"spring_field\")");
            c = q.executeList();
            Assert.assertEquals(1, c.size());
            Assert.assertEquals(ids[2], JDOHelper.getObjectId(c.iterator().next()));
            q = pm.newQuery(Office.class, "this.description.matches(\"spring.field\")");
            c = q.executeList();
            Assert.assertEquals(1, c.size());
            Assert.assertEquals(ids[2], JDOHelper.getObjectId(c.iterator().next()));
            q = pm.newQuery(Office.class, "this.description.matches(\"percent%city\")");
            c = q.executeList();
            Assert.assertEquals(1, c.size());
            Assert.assertEquals(ids[3], JDOHelper.getObjectId(c.iterator().next()));
            q = pm.newQuery(Office.class, "this.description.matches(\"percent.city\")");
            c = q.executeList();
            Assert.assertEquals(1, c.size());
            Assert.assertEquals(ids[3], JDOHelper.getObjectId(c.iterator().next()));
            q = pm.newQuery(Office.class, "this.description.matches(\"Some name or other (Nr\\\\. 1507)\")");
            c = q.executeList();
            Assert.assertEquals(1, c.size());
            Assert.assertEquals(ids[5], JDOHelper.getObjectId(c.iterator().next()));
            String filter = "this.description.matches(\"slash\\\\city\")";
            q = pm.newQuery(Office.class, filter);
            c = q.executeList();
            Assert.assertEquals(1, c.size());
            Assert.assertEquals(ids[4], JDOHelper.getObjectId(c.iterator().next()));
            if (vendorID.equalsIgnoreCase("oracle") || vendorID.equalsIgnoreCase("derby")) {
                q = pm.newQuery(Office.class, "\"spring_field\".matches(this.description)");
                c = q.executeList();
                Assert.assertEquals(1, c.size());
            }
            if (vendorID.equalsIgnoreCase("derby")) {
                // only tested in derby 10.1, which correctly performs the match.
                // oracle 10 does not work, since it returns 7, ignoring the pattern
                Assert.assertEquals(ids[2], JDOHelper.getObjectId(c.iterator().next()));
                q = pm.newQuery(Office.class, "this.description.matches(this.description)");
                c = q.executeList();
                Assert.assertEquals(4, c.size());
            }
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        clean(Office.class);
    }
}
Also used : Office(org.jpox.samples.models.company.Office) Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) Collection(java.util.Collection)

Example 9 with Office

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

the class JDOQLBasicTest method testJDOHelperGetObjectID2.

/**
 * Tests the JDOHelper.getObjectId() expression
 */
public void testJDOHelperGetObjectID2() {
    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();
        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");
        pm.newQuery(Office.class).deletePersistentAll();
        pm.makePersistent(o1);
        pm.makePersistent(o2);
        pm.makePersistent(o3);
        tx.commit();
        Object[] officeIds = new Object[3];
        officeIds[0] = pm.getObjectId(o1);
        officeIds[1] = pm.getObjectId(o2);
        officeIds[2] = pm.getObjectId(o3);
        tx.begin();
        Query q = pm.newQuery(Office.class, "JDOHelper.getObjectId(this) == oid");
        q.declareParameters("Object oid");
        Collection c = null;
        for (int i = 0; i < officeIds.length; i++) {
            c = (Collection) q.execute(officeIds[i]);
            assertEquals(1, c.size());
            assertEquals(officeIds[i], JDOHelper.getObjectId(c.iterator().next()));
        }
        tx.commit();
        tx.begin();
        q = pm.newQuery(Office.class, "javax.jdo.JDOHelper.getObjectId(this) == oid");
        q.declareParameters("Object oid");
        c = null;
        for (int i = 0; i < officeIds.length; i++) {
            c = (Collection) q.execute(officeIds[i]);
            assertEquals(1, c.size());
            assertEquals(officeIds[i], JDOHelper.getObjectId(c.iterator().next()));
        }
        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) Collection(java.util.Collection)

Example 10 with Office

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

the class JDOQLResultTest method testSetResultWithAggregation2.

public void testSetResultWithAggregation2() {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            Office o1 = new Office(1, "Turquoise", "Small office at the end of the long corridor");
            Office o2 = new Office(2, "Green", "Large fully-featured modern office");
            Office o3 = new Office(1, "Yellow", "Medium sized office");
            pm.makePersistent(o1);
            pm.makePersistent(o2);
            pm.makePersistent(o3);
            tx.commit();
            tx.begin();
            // -----------
            // COUNT
            // -----------
            Query q = pm.newQuery(pm.getExtent(Office.class, false));
            q.setResult("count(this)");
            Object result = q.execute();
            assertEquals("Type of result object is incorrect", result.getClass().getName(), "java.lang.Long");
            assertEquals("Result is incorrect", 3, ((Long) result).longValue());
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception in query", e);
            fail("Exception thrown by query " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        // 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) JDOException(javax.jdo.JDOException) JDOUserException(javax.jdo.JDOUserException)

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