Search in sources :

Example 56 with JDOUserException

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

the class JDOQLBasicTest method testSerialiseQueryResult.

/**
 * Test for serialisation of the query results.
 */
public void testSerialiseQueryResult() {
    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");
    Employee bunny = new Employee(3, "Bugs", "Bunny", "bugs.bunny@warnerbros.com", 12, "serial 3");
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        pm.makePersistent(woody);
        pm.makePersistent(bart);
        pm.makePersistent(bunny);
        tx.commit();
        tx.begin();
        try {
            Query q = pm.newQuery(Employee.class);
            List results = (List) q.execute();
            assertEquals("received: " + results, 3, results.size());
            try {
                // serialise the results
                FileOutputStream fos = new FileOutputStream("query_results.serial");
                ObjectOutputStream oos = new ObjectOutputStream(fos);
                oos.writeObject(results);
                oos.flush();
                oos.close();
            } catch (Exception e) {
                LOG.error("Exception serialising results", e);
                fail("Exception serialising results : " + e.getMessage());
            }
            // Deserialise the results
            try {
                FileInputStream fis = new FileInputStream("query_results.serial");
                ObjectInputStream ois = new ObjectInputStream(fis);
                Object object2 = ois.readObject();
                ois.close();
                assertTrue("Deserialised form is not correct type", object2 instanceof List);
                List deserialisedResults = (List) object2;
                assertEquals("Invalid number of deserialised elements", 3, deserialisedResults.size());
            } catch (Exception e) {
                LOG.error("Exception deserialising results", e);
                fail("Exception deserialising results : " + e.getMessage());
            }
        } catch (JDOUserException e) {
            fail(e.getMessage());
        }
        tx.commit();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
        // Delete file
        File file = new File("query_results.serial");
        file.delete();
        // Clean out our data
        clean(Employee.class);
    }
}
Also used : Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) ObjectOutputStream(java.io.ObjectOutputStream) JDOUserException(javax.jdo.JDOUserException) JDOException(javax.jdo.JDOException) JDOUserException(javax.jdo.JDOUserException) NoSuchElementException(java.util.NoSuchElementException) FileInputStream(java.io.FileInputStream) Employee(org.jpox.samples.models.company.Employee) Transaction(javax.jdo.Transaction) FileOutputStream(java.io.FileOutputStream) List(java.util.List) ArrayList(java.util.ArrayList) File(java.io.File) ObjectInputStream(java.io.ObjectInputStream)

Example 57 with JDOUserException

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

the class JDOQLContainerTest method testQueryUsesContainsKeyOnceOnOneUnboundVariableImplicitVariables.

/**
 * Test for the Map.containsKey() method.
 */
public void testQueryUsesContainsKeyOnceOnOneUnboundVariableImplicitVariables() {
    try {
        Gym gym1 = new Gym();
        gym1.setName("Cinema");
        gym1.setLocation("First floor");
        Wardrobe w1 = new Wardrobe();
        Wardrobe w2 = new Wardrobe();
        Wardrobe w3 = new Wardrobe();
        Wardrobe w4 = new Wardrobe();
        w1.setModel("2 door");
        w2.setModel("3 door");
        w3.setModel("4 door");
        w4.setModel("5 door");
        gym1.getWardrobes2().put(w1, w1.getModel());
        gym1.getWardrobes2().put(w2, w2.getModel());
        gym1.getWardrobes2().put(w3, w3.getModel());
        gym1.getWardrobes2().put(w4, w4.getModel());
        Gym gym2 = new Gym();
        gym2.setName("Shopping");
        gym2.setLocation("Second floor");
        gym2.getWardrobes2().put(w1, w1.getModel());
        gym2.getWardrobes2().put(w2, w2.getModel());
        Cloth c1 = new Cloth();
        c1.setName("green shirt");
        Cloth c2 = new Cloth();
        c2.setName("red shirt");
        Cloth c3 = new Cloth();
        c3.setName("blue shirt");
        GymEquipment ge1 = new GymEquipment();
        ge1.setName("Weight");
        GymEquipment ge2 = new GymEquipment();
        ge2.setName("Yoga");
        GymEquipment ge3 = new GymEquipment();
        ge3.setName("Pilates");
        GymEquipment ge4 = new GymEquipment();
        ge4.setName("Abdominal");
        gym1.getEquipments2().put(ge1, ge1.getName());
        gym1.getEquipments2().put(ge2, ge2.getName());
        gym2.getEquipments2().put(ge3, ge3.getName());
        gym2.getEquipments2().put(ge4, ge4.getName());
        gym1.getPartners2().put(gym2, gym2.getName());
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            pm.makePersistent(gym1);
            pm.makePersistent(gym2);
            tx.commit();
            tx.begin();
            Query q = pm.newQuery(Gym.class);
            q.setFilter("wardrobes2.containsKey(w1) && w1.model == \"4 door\"");
            Collection c = (Collection) q.execute();
            assertEquals(1, c.size());
            q = pm.newQuery(Gym.class);
            q.setFilter("wardrobes2.containsKey(w1) && (w1.model == \"2 door\" || w1.model == \"3 door\")");
            c = (Collection) q.execute();
            assertEquals(2, c.size());
            q = pm.newQuery(Gym.class);
            q.setFilter("wardrobes2.containsKey(w1) && (w1.model == \"4 door\" || w1.model == \"5 door\")");
            c = (Collection) q.execute();
            assertEquals(1, c.size());
            try {
                q = pm.newQuery(Gym.class);
                q.setFilter("equipments2.containsKey(e) && g.equipments2.containsKey(e) && e.name =='Yoga'");
                c = (Collection) q.execute();
                fail("expected JDOUserException");
            } catch (JDOUserException e) {
            // expected
            }
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        // Clean out our data
        FitnessHelper.cleanFitnessData(pmf);
    }
}
Also used : Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) Cloth(org.jpox.samples.models.fitness.Cloth) Collection(java.util.Collection) Gym(org.jpox.samples.models.fitness.Gym) Wardrobe(org.jpox.samples.models.fitness.Wardrobe) JDOUserException(javax.jdo.JDOUserException) GymEquipment(org.jpox.samples.models.fitness.GymEquipment)

Example 58 with JDOUserException

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

the class JDOQLContainerTest method testQueryUsesContainsValueOnceOnOneUnboundVariableImplicitVariables.

/**
 * Test for the Map.containsValue() method.
 */
public void testQueryUsesContainsValueOnceOnOneUnboundVariableImplicitVariables() {
    try {
        Gym gym1 = new Gym();
        gym1.setName("Cinema");
        gym1.setLocation("First floor");
        Wardrobe w1 = new Wardrobe();
        Wardrobe w2 = new Wardrobe();
        Wardrobe w3 = new Wardrobe();
        Wardrobe w4 = new Wardrobe();
        w1.setModel("2 door");
        w2.setModel("3 door");
        w3.setModel("4 door");
        w4.setModel("5 door");
        gym1.getWardrobes().put(w1.getModel(), w1);
        gym1.getWardrobes().put(w2.getModel(), w2);
        gym1.getWardrobes().put(w3.getModel(), w3);
        gym1.getWardrobes().put(w4.getModel(), w4);
        Gym gym2 = new Gym();
        gym2.setName("Shopping");
        gym2.setLocation("Second floor");
        gym2.getWardrobes().put(w1.getModel(), w1);
        gym2.getWardrobes().put(w2.getModel(), w2);
        Cloth c1 = new Cloth();
        c1.setName("green shirt");
        Cloth c2 = new Cloth();
        c2.setName("red shirt");
        Cloth c3 = new Cloth();
        c3.setName("blue shirt");
        GymEquipment ge1 = new GymEquipment();
        ge1.setName("Weight");
        GymEquipment ge2 = new GymEquipment();
        ge2.setName("Yoga");
        GymEquipment ge3 = new GymEquipment();
        ge3.setName("Pilates");
        GymEquipment ge4 = new GymEquipment();
        ge4.setName("Abdominal");
        gym1.getEquipments().put(ge1.getName(), ge1);
        gym1.getEquipments().put(ge2.getName(), ge2);
        gym2.getEquipments().put(ge3.getName(), ge3);
        gym2.getEquipments().put(ge4.getName(), ge4);
        gym1.getPartners().put(gym2.getName(), gym2);
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            pm.makePersistent(gym1);
            pm.makePersistent(gym2);
            tx.commit();
            tx.begin();
            Query q = pm.newQuery(Gym.class);
            q.setFilter("wardrobes.containsValue(w1) && w1.model == \"4 door\"");
            Collection c = (Collection) q.execute();
            assertEquals(1, c.size());
            q = pm.newQuery(Gym.class);
            q.setFilter("wardrobes.containsValue(w1) && (w1.model == \"2 door\" || w1.model == \"3 door\")");
            c = (Collection) q.execute();
            assertEquals(2, c.size());
            q = pm.newQuery(Gym.class);
            q.setFilter("wardrobes.containsValue(w1) && (w1.model == \"4 door\" || w1.model == \"5 door\")");
            c = (Collection) q.execute();
            assertEquals(1, c.size());
            try {
                q = pm.newQuery(Gym.class);
                q.setFilter("equipments.containsValue(e) && g.equipments.containsValue(e) && e.name =='Yoga'");
                c = (Collection) q.execute();
                fail("expected JDOUserException");
            } catch (JDOUserException e) {
            // expected
            }
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        // Clean out our data
        FitnessHelper.cleanFitnessData(pmf);
    }
}
Also used : Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) Cloth(org.jpox.samples.models.fitness.Cloth) Collection(java.util.Collection) Gym(org.jpox.samples.models.fitness.Gym) Wardrobe(org.jpox.samples.models.fitness.Wardrobe) JDOUserException(javax.jdo.JDOUserException) GymEquipment(org.jpox.samples.models.fitness.GymEquipment)

Example 59 with JDOUserException

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

the class JDOQLResultTest method testSetIllegalAttributeInResult.

public void testSetIllegalAttributeInResult() {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            try {
                Query q = pm.newQuery(BasicTypeHolder.class);
                q.setResult("this.longFieldQQ");
                q.execute();
                fail("Expected JDOUserException");
            } catch (JDOUserException ex) {
            // expected
            }
            try {
                Query q = pm.newQuery(BasicTypeHolder.class);
                q.setResult("qngFieldQQ");
                q.execute();
                fail("Expected JDOUserException");
            } catch (JDOUserException ex) {
            // expected
            }
            try {
                Query q = pm.newQuery(BasicTypeHolder.class);
                q.setResult("this.longFieldQQ");
                q.compile();
                fail("Expected JDOUserException");
            } catch (JDOUserException ex) {
            // expected
            }
            try {
                Query q = pm.newQuery(BasicTypeHolder.class);
                q.setResult("qngFieldQQ");
                q.compile();
                fail("Expected JDOUserException");
            } catch (JDOUserException ex) {
            // expected
            }
            tx.rollback();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        clean(BasicTypeHolder.class);
    }
}
Also used : Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) JDOUserException(javax.jdo.JDOUserException)

Example 60 with JDOUserException

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

the class JDOQLResultTest method testSetResultClass.

/**
 * Test case to use the JDO 2.0 setResultClass().
 */
public void testSetResultClass() {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            Person p1 = new Person(101, "Fred", "Flintstone", "fred.flintstone@jpox.com");
            p1.setAge(34);
            Person p2 = new Person(102, "Barney", "Rubble", "barney.rubble@jpox.com");
            p1.setAge(37);
            pm.makePersistent(p1);
            pm.makePersistent(p2);
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
        }
        try {
            tx.begin();
            try {
                Query q = pm.newQuery(pm.getExtent(Person.class, false));
                q.setFilter("age > 25");
                q.setResultClass(Object[].class);
                List results = (List) q.execute();
                Iterator resultsIter = results.iterator();
                while (resultsIter.hasNext()) {
                    Object obj = resultsIter.next();
                    assertEquals("ResultClass of query is incorrect.", Object[].class.getName(), obj.getClass().getName());
                }
                q.closeAll();
            } catch (JDOUserException e) {
                fail(e.getMessage());
            }
            // Query using user-defined result output : user class has constructor of right type
            try {
                Query q = pm.newQuery(pm.getExtent(Person.class, false));
                q.setResult("firstName, lastName, age");
                q.setResultClass(Identity1.class);
                List results = (List) q.execute();
                Iterator resultsIter = results.iterator();
                while (resultsIter.hasNext()) {
                    Object obj = resultsIter.next();
                    assertEquals("ResultClass of query is incorrect.", Identity1.class.getName(), obj.getClass().getName());
                }
                q.closeAll();
            } catch (JDOUserException e) {
                fail(e.getMessage());
            }
            // Query using user-defined result output : user class has public fields
            try {
                Query q = pm.newQuery(pm.getExtent(Person.class, false));
                q.setResult("firstName, lastName, age");
                q.setResultClass(Identity2.class);
                List results = (List) q.execute();
                Iterator resultsIter = results.iterator();
                while (resultsIter.hasNext()) {
                    Object obj = resultsIter.next();
                    assertEquals("ResultClass of query is incorrect.", Identity2.class.getName(), obj.getClass().getName());
                }
                q.closeAll();
            } catch (JDOUserException e) {
                fail(e.getMessage());
            }
            // Query using user-defined result output : user class has setters
            try {
                Query q = pm.newQuery(pm.getExtent(Person.class, false));
                q.setResult("firstName, lastName, age");
                q.setResultClass(Identity3.class);
                List results = (List) q.execute();
                Iterator resultsIter = results.iterator();
                while (resultsIter.hasNext()) {
                    Object obj = resultsIter.next();
                    assertEquals("ResultClass of query is incorrect.", Identity3.class.getName(), obj.getClass().getName());
                }
                q.closeAll();
            } catch (JDOUserException e) {
                fail(e.getMessage());
            }
            // Query using user-defined result output : user class has put method
            try {
                Query q = pm.newQuery(pm.getExtent(Person.class, false));
                q.setResult("firstName, lastName, age");
                q.setResultClass(Identity4.class);
                List results = (List) q.execute();
                Iterator resultsIter = results.iterator();
                while (resultsIter.hasNext()) {
                    Object obj = resultsIter.next();
                    assertEquals("ResultClass of query is incorrect.", Identity4.class.getName(), obj.getClass().getName());
                }
                q.closeAll();
            } catch (JDOUserException e) {
                fail(e.getMessage());
            }
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // Query using user-defined result output : invalid result class
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        Query q2 = null;
        try {
            tx.begin();
            q2 = pm.newQuery(pm.getExtent(Person.class, false));
            q2.setResult("firstName, lastName, age");
            q2.setResultClass(Identity5.class);
            List results = (List) q2.execute();
            Iterator resultsIter = results.iterator();
            while (resultsIter.hasNext()) {
                resultsIter.next();
            }
            fail("Query was executed but should have thrown an exception in the result extraction due to invalid ResultClass");
            tx.commit();
        } catch (JDOUserException e) {
            LOG.info("Exception thrown by query", e);
            // Success!
            q2.closeAll();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // Query using simple type result output
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            Query q = pm.newQuery(pm.getExtent(Person.class, false));
            // Field is of type String
            q.setResult("firstName");
            q.setResultClass(String.class);
            List results = (List) q.execute();
            Iterator resultsIter = results.iterator();
            while (resultsIter.hasNext()) {
                Object obj = resultsIter.next();
                assertEquals("ResultClass of query is incorrect.", String.class.getName(), obj.getClass().getName());
            }
            q.closeAll();
            tx.commit();
        } catch (JDOUserException e) {
            fail(e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
        }
        // Query using simple type result output but with too many fields
        try {
            tx.begin();
            Query q = pm.newQuery(pm.getExtent(Person.class, false));
            // Fields of type String, int
            q.setResult("firstName, age");
            q.setResultClass(String.class);
            q.execute();
            fail("ResultClass was specified as String yet 2 fields were returned. An exception should have been thrown");
            q.closeAll();
            tx.commit();
        } catch (JDOUserException e) {
        // Success. JPOX detected the erroneous resultClass spec.
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        // Clean out our data
        clean(Person.class);
    }
}
Also used : Identity4(org.jpox.samples.models.company.Identity4) Identity3(org.jpox.samples.models.company.Identity3) Identity2(org.jpox.samples.models.company.Identity2) Query(javax.jdo.Query) Identity1(org.jpox.samples.models.company.Identity1) PersistenceManager(javax.jdo.PersistenceManager) JDOUserException(javax.jdo.JDOUserException) Transaction(javax.jdo.Transaction) ListIterator(java.util.ListIterator) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) Person(org.jpox.samples.models.company.Person)

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