Search in sources :

Example 51 with JDOUserException

use of javax.jdo.JDOUserException in project tests by datanucleus.

the class JDOQLBasicTest method testQueryOfInterface.

/**
 * Test for the querying of an interface (persistent).
 */
public void testQueryOfInterface() {
    Mouse mouse1 = new Mouse();
    mouse1.setId(101);
    mouse1.setManufacturer("Logitech");
    mouse1.setModel("M305");
    Keyboard kb1 = new Keyboard();
    kb1.setId(102);
    kb1.setManufacturer("Logitech");
    kb1.setModel("K304");
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        pm.makePersistent(mouse1);
        pm.makePersistent(kb1);
        tx.commit();
        tx.begin();
        try {
            Query q = pm.newQuery(ComputerPeripheral.class);
            List<ComputerPeripheral> results = (List<ComputerPeripheral>) q.execute();
            assertEquals("Number of results incorrect", 2, results.size());
            Iterator<ComputerPeripheral> resultsIter = results.iterator();
            boolean mousePresent = false;
            boolean kbPresent = false;
            while (resultsIter.hasNext()) {
                ComputerPeripheral peri = resultsIter.next();
                if (peri instanceof Mouse && peri.getId() == 101 && peri.getManufacturer().equals("Logitech") && peri.getModel().equals("M305")) {
                    mousePresent = true;
                }
                if (peri instanceof Keyboard && peri.getId() == 102 && peri.getManufacturer().equals("Logitech") && peri.getModel().equals("K304")) {
                    kbPresent = true;
                }
            }
            if (!mousePresent) {
                fail("Mouse not present in results");
            }
            if (!kbPresent) {
                fail("Keyboard not present in results");
            }
            q.closeAll();
        } catch (JDOUserException e) {
            fail(e.getMessage());
        }
        tx.commit();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
        // Clean out our data
        clean(Mouse.class);
        clean(Keyboard.class);
    }
}
Also used : Mouse(org.jpox.samples.persistentinterfaces.Mouse) Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) Keyboard(org.jpox.samples.persistentinterfaces.Keyboard) List(java.util.List) ArrayList(java.util.ArrayList) JDOUserException(javax.jdo.JDOUserException) ComputerPeripheral(org.jpox.samples.persistentinterfaces.ComputerPeripheral)

Example 52 with JDOUserException

use of javax.jdo.JDOUserException in project tests by datanucleus.

the class JDOQLBasicTest method testSingleString.

/**
 * Test case to use JDOQL single-string
 */
public void testSingleString() {
    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();
            try {
                Query q = pm.newQuery("SELECT UNIQUE FROM " + Employee.class.getName() + " EXCLUDE SUBCLASSES  WHERE firstName == \"Bugs\"");
                q.setClass(Employee.class);
                Object results = q.execute();
                assertTrue("UNIQUE query returned an object of an incorrect type : should have been Employee but was " + results, results instanceof Employee);
                q.closeAll();
                q = pm.newQuery("SELECT UNIQUE FROM " + Employee.class.getName() + " EXCLUDE SUBCLASSES  WHERE firstName == \"Bugs\" import " + Employee.class.getName());
                q.setClass(Employee.class);
                results = q.execute();
                assertTrue("UNIQUE query returned an object of an incorrect type : should have been Employee but was " + results, results instanceof Employee);
                q.closeAll();
            } catch (JDOUserException e) {
                fail(e.getMessage());
            }
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        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)

Example 53 with JDOUserException

use of javax.jdo.JDOUserException in project tests by datanucleus.

the class JDOQLBasicTest method testStaticFields.

/**
 * Test for the use of the static fields in query method.
 */
public void testStaticFields() {
    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");
        // Beep! Beep!
        Employee roadrunner = new Employee(4, "Road", "Runner", "road.runner@warnerbros.com", 11, "serial 4");
        // I hate the gravity
        Employee coyote = new Employee(5, "Wile", "E. Coyote", "wile.coyote@acme.com", 9, "serial 5");
        // paranoid, and neurotic
        Employee duck = new Employee(6, "Daffy", "Duck", "daffy.duck@warnerbros.com", 7, "serial 6");
        // You are my peanut.
        Employee pepe = new Employee(7, "Pepe", "le Pew", "pepe.lepew@warnerbros.com", 8, "serial 7");
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            pm.makePersistent(woody);
            pm.makePersistent(bart);
            pm.makePersistent(bunny);
            pm.makePersistent(roadrunner);
            pm.makePersistent(coyote);
            pm.makePersistent(duck);
            pm.makePersistent(pepe);
            tx.commit();
            tx.begin();
            try {
                Query q = pm.newQuery(pm.getExtent(org.jpox.samples.models.company.Employee.class, false));
                q.setFilter("firstName == org.jpox.samples.models.company.Employee.FIRSTNAME");
                HashSet results = new HashSet((Collection) q.execute());
                assertEquals("received: " + results, 1, results.size());
                assertEquals(org.jpox.samples.models.company.Employee.FIRSTNAME, ((Person) results.iterator().next()).getFirstName());
                q.closeAll();
                q = pm.newQuery(pm.getExtent(org.jpox.samples.models.company.Employee.class, false));
                q.setFilter("firstName == org.jpox.samples.models.company.Person.FIRSTNAME");
                results = new HashSet((Collection) q.execute());
                assertEquals("received: " + results, 1, results.size());
                assertEquals(org.jpox.samples.models.company.Person.FIRSTNAME, ((Person) results.iterator().next()).getFirstName());
                q.closeAll();
                q = pm.newQuery(pm.getExtent(org.jpox.samples.models.company.Employee.class, false));
                q.setFilter("firstName == Person.FIRSTNAME");
                results = new HashSet((Collection) q.execute());
                assertEquals("received: " + results, 1, results.size());
                assertEquals(org.jpox.samples.models.company.Person.FIRSTNAME, ((Person) results.iterator().next()).getFirstName());
                q.closeAll();
                q = pm.newQuery(pm.getExtent(org.jpox.samples.models.company.Employee.class, false));
                q.setFilter("firstName == Employee.FIRSTNAME");
                results = new HashSet((Collection) q.execute());
                assertEquals("received: " + results, 1, results.size());
                assertEquals(org.jpox.samples.models.company.Employee.FIRSTNAME, ((Person) results.iterator().next()).getFirstName());
                q.closeAll();
                q = pm.newQuery(pm.getExtent(org.jpox.samples.models.company.Employee.class, false));
                q.setFilter("firstName == FIRSTNAME");
                results = new HashSet((Collection) q.execute());
                assertEquals("received: " + results, 1, results.size());
                assertEquals(org.jpox.samples.models.company.Employee.FIRSTNAME, ((Person) results.iterator().next()).getFirstName());
                q.closeAll();
            } catch (JDOUserException e) {
                fail(e.getMessage());
            }
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        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 54 with JDOUserException

use of javax.jdo.JDOUserException in project tests by datanucleus.

the class JDOQLBasicTest method runTestCandidateCollection.

protected void runTestCandidateCollection(PersistenceManager pm) {
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        try {
            Query q = pm.newQuery(pm.getExtent(org.jpox.samples.models.company.Employee.class, false));
            HashSet results = new HashSet((Collection) q.execute());
            Collection resultIds = new HashSet();
            Iterator iter = results.iterator();
            while (iter.hasNext()) {
                resultIds.add(JDOHelper.getObjectId(iter.next()));
            }
            assertTrue("Expected result was all candidates elements, but received: " + StringUtils.collectionToString(resultIds), resultIds.equals(allEmployeeIds));
            q.closeAll();
        } catch (JDOUserException e) {
            LOG.error("Exception thrown in querying ", e);
            fail(e.getMessage());
        }
        try {
            Collection candidates = new HashSet();
            candidates.addAll(pm.getObjectsById(warnerEmployeeIds));
            Query q = pm.newQuery(org.jpox.samples.models.company.Employee.class, candidates);
            HashSet results = new HashSet((Collection) q.execute());
            Collection resultIds = new HashSet();
            Iterator iter = results.iterator();
            while (iter.hasNext()) {
                resultIds.add(JDOHelper.getObjectId(iter.next()));
            }
            assertTrue("Expected result was warner candidate elements, but received: " + StringUtils.collectionToString(resultIds), resultIds.equals(warnerEmployeeIds));
            q.closeAll();
        } catch (JDOUserException e) {
            fail(e.getMessage());
        }
        try {
            Query q = pm.newQuery(org.jpox.samples.models.company.Employee.class, (Collection) null);
            HashSet results = new HashSet((Collection) q.execute());
            Collection resultIds = new HashSet();
            Iterator iter = results.iterator();
            while (iter.hasNext()) {
                resultIds.add(JDOHelper.getObjectId(iter.next()));
            }
            assertTrue("Expected result was all candidate elements, but received: " + StringUtils.collectionToString(resultIds), resultIds.equals(allEmployeeIds));
            q.closeAll();
        } catch (JDOUserException e) {
            fail(e.getMessage());
        }
        try {
            Query q = pm.newQuery(org.jpox.samples.models.company.Employee.class);
            q.setCandidates((Collection) null);
            HashSet results = new HashSet((Collection) q.execute());
            Collection resultIds = new HashSet();
            Iterator iter = results.iterator();
            while (iter.hasNext()) {
                resultIds.add(JDOHelper.getObjectId(iter.next()));
            }
            assertTrue("Expected result was warner candidate elements, but received: " + StringUtils.collectionToString(resultIds), resultIds.equals(allEmployeeIds));
            q.closeAll();
        } catch (JDOUserException e) {
            fail(e.getMessage());
        }
        try {
            Query q = pm.newQuery(org.jpox.samples.models.company.Employee.class, new HashSet());
            HashSet results = new HashSet((Collection) q.execute());
            assertEquals("Number of elements is wrong", 0, results.size());
            q.closeAll();
        } catch (JDOUserException e) {
            fail(e.getMessage());
        }
        try {
            Query q = pm.newQuery(org.jpox.samples.models.company.Employee.class);
            q.setCandidates(new HashSet());
            HashSet results = new HashSet((Collection) q.execute());
            assertEquals("Number of elements is wrong", 0, results.size());
            q.closeAll();
        } catch (JDOUserException e) {
            fail(e.getMessage());
        }
        try {
            Query q = pm.newQuery(org.jpox.samples.models.company.Employee.class);
            q.setCandidates(new HashSet());
            // Replaces collection with null, so all candidates
            q.setCandidates((Collection) null);
            HashSet results = new HashSet((Collection) q.execute());
            Collection resultIds = new HashSet();
            Iterator iter = results.iterator();
            while (iter.hasNext()) {
                resultIds.add(JDOHelper.getObjectId(iter.next()));
            }
            assertTrue("Expected result was all candidate elements, but received: " + StringUtils.collectionToString(resultIds), resultIds.equals(allEmployeeIds));
            q.closeAll();
        } catch (JDOUserException e) {
            fail(e.getMessage());
        }
        try {
            Query q = pm.newQuery(org.jpox.samples.models.company.Employee.class);
            q.setCandidates((Extent) null);
            HashSet results = new HashSet((Collection) q.execute());
            Collection resultIds = new HashSet();
            Iterator iter = results.iterator();
            while (iter.hasNext()) {
                resultIds.add(JDOHelper.getObjectId(iter.next()));
            }
            assertTrue("Expected result was all candidate elements, but received: " + StringUtils.collectionToString(resultIds), resultIds.equals(allEmployeeIds));
            q.closeAll();
        } catch (JDOUserException e) {
            fail(e.getMessage());
        }
        try {
            Collection candidates = new HashSet();
            candidates.addAll(pm.getObjectsById(warnerEmployeeIds));
            Query q = pm.newQuery(org.jpox.samples.models.company.Employee.class, candidates, "salary > 10");
            HashSet results = new HashSet((Collection) q.execute());
            Collection resultIds = new HashSet();
            Iterator iter = results.iterator();
            while (iter.hasNext()) {
                resultIds.add(JDOHelper.getObjectId(iter.next()));
            }
            assertTrue("Expected result was all expected elements, but received: " + StringUtils.collectionToString(resultIds), resultIds.equals(expectedEmployeeIds));
            q.closeAll();
        } catch (JDOUserException e) {
            fail(e.getMessage());
        }
        tx.commit();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
    }
}
Also used : Employee(org.jpox.samples.models.company.Employee) Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) Iterator(java.util.Iterator) Collection(java.util.Collection) JDOUserException(javax.jdo.JDOUserException) HashSet(java.util.HashSet)

Example 55 with JDOUserException

use of javax.jdo.JDOUserException in project tests by datanucleus.

the class JDOQLBasicTest method testStringStartsWith.

/**
 * Test the use of the String.startsWith() method.
 */
public void testStringStartsWith() {
    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");
    // Beep! Beep!
    Employee roadrunner = new Employee(4, "Road", "Runner", "Road.runner@warnerbros.com", 11, "serial 4");
    // I hate the gravity
    Employee coyote = new Employee(5, "Wile", "E. Coyote", "Wile.coyote@acme.com", 9, "serial 5");
    // paranoid, and neurotic
    Employee duck = new Employee(6, "Daffy", "Duck", "Daffy.duck@warnerbros.com", 7, "serial 6");
    // You are my peanut.
    Employee pepe = new Employee(7, "Pepe Changed", "le Pew", "Pepe.lepew@warnerbros.com", 8, "serial 7");
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        pm.makePersistent(woody);
        pm.makePersistent(bart);
        pm.makePersistent(bunny);
        pm.makePersistent(roadrunner);
        pm.makePersistent(coyote);
        pm.makePersistent(duck);
        pm.makePersistent(pepe);
        tx.commit();
        tx.begin();
        try {
            // test 1
            Query q = pm.newQuery(pm.getExtent(org.jpox.samples.models.company.Employee.class, false));
            q.setFilter("emailAddress.startsWith(\"Road\")");
            HashSet results = new HashSet((Collection) q.execute());
            assertTrue("received: " + results, results.size() == 1);
            q.closeAll();
            // test 2
            q = pm.newQuery(pm.getExtent(org.jpox.samples.models.company.Employee.class, false));
            q.setFilter("emailAddress.startsWith(firstName)");
            results = new HashSet((Collection) q.execute());
            assertTrue("received: " + results, results.size() == 6);
            q.closeAll();
            // test 3
            q = pm.newQuery(pm.getExtent(org.jpox.samples.models.company.Employee.class, false));
            q.setFilter("\"Road Runner\".startsWith(firstName)");
            results = new HashSet((Collection) q.execute());
            assertTrue("received: " + results, results.size() == 1);
            q.closeAll();
            // test 4
            q = pm.newQuery(pm.getExtent(org.jpox.samples.models.company.Employee.class, false));
            q.declareParameters("java.lang.String var1");
            q.setFilter("var1.startsWith(firstName)");
            results = new HashSet((Collection) q.execute("Road Runner"));
            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();
        // 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

JDOUserException (javax.jdo.JDOUserException)191 PersistenceManager (javax.jdo.PersistenceManager)163 Transaction (javax.jdo.Transaction)161 Query (javax.jdo.Query)94 Iterator (java.util.Iterator)54 Collection (java.util.Collection)45 Employee (org.jpox.samples.models.company.Employee)35 List (java.util.List)33 HashSet (java.util.HashSet)30 ArrayList (java.util.ArrayList)22 Extent (javax.jdo.Extent)22 JDOException (javax.jdo.JDOException)15 JDOPersistenceManager (org.datanucleus.api.jdo.JDOPersistenceManager)15 CollectionHolder (org.jpox.samples.types.container.CollectionHolder)15 Map (java.util.Map)13 MapHolder (org.jpox.samples.types.container.MapHolder)13 JDOFatalUserException (javax.jdo.JDOFatalUserException)12 NucleusException (org.datanucleus.exceptions.NucleusException)10 TransactionActiveOnCloseException (org.datanucleus.exceptions.TransactionActiveOnCloseException)10 Person (org.jpox.samples.models.company.Person)10