Search in sources :

Example 1 with Extent

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

the class ViewTest method testViewOfSetWidgets.

public void testViewOfSetWidgets() throws Exception {
    /*
         * We can't run this test on Cloudscape because the view used by
         * SetWidgetCounts doesn't execute properly; some counts that should
         * be 0 come up 1.  This is presumably due to a bug in Cloudscape
         * (last tried on both 3.6 and 4.0).
         */
    if ("cloudscape".equals(vendorID)) {
        return;
    }
    try {
        LOG.info("Testing view derived from of " + StorageTester.TEST_OBJECT_COUNT + " " + SetWidget.class.getName() + " objects");
        tester.insertObjects(SetWidget.class);
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            Extent ext = pm.getExtent(SetWidgetCounts.class, true);
            Iterator exti = ext.iterator();
            int count = 0;
            while (exti.hasNext()) {
                SetWidgetCounts actual = (SetWidgetCounts) exti.next();
                SetWidgetCounts expected = new SetWidgetCounts(actual.getSetWidget());
                StorageTester.assertFieldsEqual(expected, actual);
                ++count;
            }
            tx.commit();
            assertEquals("Iteration over view extent returned wrong number of rows", StorageTester.TEST_OBJECT_COUNT, count);
            tx.begin();
            Query query = pm.newQuery(pm.getExtent(SetWidgetCounts.class, true));
            query.setFilter("normalSetSize != 0");
            query.setOrdering("sw.numElementWidgets descending");
            Collection results = (Collection) query.execute();
            TestObject[] objs = tester.getObjects();
            try {
                HashSet expected = new HashSet();
                for (int i = 0; i < objs.length; ++i) {
                    SetWidget sw = (SetWidget) objs[i];
                    if (sw.getNormalSet().size() != 0) {
                        expected.add(new SetWidgetCounts(sw));
                    }
                }
                assertTrue("Query has no expected results (test is broken)", !expected.isEmpty());
                assertTrue("Query returned no rows", !results.isEmpty());
                HashSet actual = new HashSet(results);
                assertEquals("Query returned duplicate rows", results.size(), actual.size());
                assertTrue("Query did not return expected results: expected " + expected + ", but was " + actual, TestObject.compareSet(expected, actual));
            } finally {
                query.closeAll();
            }
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } catch (JDOException e) {
        LOG.error(">> Exception during test", e);
        fail("Exception occurred during test : " + e.getMessage());
    } finally {
        tester.removeObjects();
        clean(Widget.class);
    }
}
Also used : SetWidget(org.jpox.samples.widget.SetWidget) HashSetWidget(org.jpox.samples.widget.HashSetWidget) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) Extent(javax.jdo.Extent) SetWidgetCounts(org.jpox.samples.rdbms.views.SetWidgetCounts) Transaction(javax.jdo.Transaction) Iterator(java.util.Iterator) Collection(java.util.Collection) HashSet(java.util.HashSet) JDOException(javax.jdo.JDOException)

Example 2 with Extent

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

the class AbstractClassesTest method perform1toNJoinTableSetRetrieval.

/**
 * Test of the retrieval of the abstract contained objects and of the
 * holder with its related abstract object.
 */
private void perform1toNJoinTableSetRetrieval(Class holderClass) throws Exception {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        // Find the holder
        Extent e = pm.getExtent(holderClass, true);
        int numberOfHolders = 0;
        Iterator iter = e.iterator();
        while (iter.hasNext()) {
            Object holder = iter.next();
            numberOfHolders++;
            // Extract the Set of elements
            Method getSetMethod = holderClass.getMethod("getAbstractSet1", new Class[] {});
            Object obj = getSetMethod.invoke(holder, new Object[] {});
            assertTrue("Elements for holder is NULL, but shouldn't be", obj != null);
            assertTrue("Elements type is not a Set", obj instanceof Set);
            Collection elements = (Collection) obj;
            assertEquals("Number of elements is incorrect", 2, elements.size());
            Iterator elementsIter = elements.iterator();
            while (elementsIter.hasNext()) {
                elementsIter.next();
            }
        }
        assertEquals("Number of container objects was incorrect.", 1, numberOfHolders);
        tx.commit();
    } catch (Exception e) {
        LOG.error("Exception thrown retrieving objects with abstract superclass", e);
        e.printStackTrace();
        fail("Exception thrown during persistence : " + e.getMessage());
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
}
Also used : Set(java.util.Set) Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) Extent(javax.jdo.Extent) Iterator(java.util.Iterator) Collection(java.util.Collection) Method(java.lang.reflect.Method) JDOUserException(javax.jdo.JDOUserException)

Example 3 with Extent

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

the class SCOMapTests method checkQuery.

/**
 * Utility for checking the use of queries with JDOQL on a Map.
 * Uses containsKey(),containsValue(),containsEntry() methods.
 * @param pmf The PersistenceManager factory
 * @param container_class The container class e.g HashMapNormal
 * @param item_class_parent The parent element class
 * @param db_vendor_id Id of datastore e.g mysql (TODO : remove this)
 */
public static void checkQuery(PersistenceManagerFactory pmf, Class container_class, Class item_class_parent, String db_vendor_id) throws Exception {
    int NO_OF_ITEMS = 5;
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        // Create a container
        MapHolder container = null;
        try {
            container = (MapHolder) container_class.newInstance();
        } catch (Exception e1) {
            LOG.error(e1);
            Assert.fail("Failed to find Container class " + container_class.getName());
        }
        // Create a few items and add them to a Map
        java.util.Map m = new java.util.HashMap();
        for (int i = 0; i < NO_OF_ITEMS; i++) {
            // Create an item
            Object item = SCOHolderUtilities.createItemParent(item_class_parent, "Item " + i, 0.00 + (10.00 * i), i);
            m.put(new String("Key" + (i + 1)), item);
        }
        // Add the items to the container
        SCOHolderUtilities.addItemsToMap(container, m);
        pm.makePersistent(container);
        JDOHelper.getObjectId(container);
        tx.commit();
    } catch (JDOUserException e) {
        LOG.error(e);
        Assert.assertTrue("Exception thrown while creating " + container_class.getName() + " " + e.getMessage(), false);
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
    // Query the Map
    pm = pmf.getPersistenceManager();
    tx = pm.currentTransaction();
    try {
        tx.begin();
        // Get all Maps that contain the key "Key1" - should return 1
        Extent e1 = pm.getExtent(container_class, true);
        Query q1 = pm.newQuery(e1, "items.containsKey(\"Key1\")");
        java.util.Collection c1 = (java.util.Collection) q1.execute();
        Assert.assertTrue("containsKey : No of containers with a key \"Key1\" is incorrect (" + c1.size() + ") : should have been 1", c1.size() == 1);
        // Get all Maps that contain the key "Key7" - should return 0
        Extent e2 = pm.getExtent(container_class, true);
        Query q2 = pm.newQuery(e2, "items.containsKey(\"Key7\")");
        java.util.Collection c2 = (java.util.Collection) q2.execute();
        Assert.assertTrue("containsKey : No of containers with a key \"Key7\" is incorrect (" + c2.size() + ") : should have been 0", c2.size() == 0);
        // TODO : remove the MySQL omittal when it supports subqueries
        if (db_vendor_id == null || !db_vendor_id.equals("mysql")) {
            Extent e3 = pm.getExtent(container_class, true);
            Query q3 = pm.newQuery(e3, "items.isEmpty()");
            java.util.Collection c3 = (java.util.Collection) q3.execute();
            Assert.assertTrue("No of containers that are empty is incorrect (" + c3.size() + ") : should have been 0", c3.size() == 0);
        }
        // Get all Maps containing a particular value
        Extent e4 = pm.getExtent(container_class, true);
        Query q4 = pm.newQuery(e4, "items.containsValue(the_value) && the_value.name==\"Item 1\"");
        q4.declareImports("import " + item_class_parent.getName());
        q4.declareVariables(item_class_parent.getName() + " the_value");
        java.util.Collection c4 = (java.util.Collection) q4.execute();
        Assert.assertTrue("containsValue : No of containers with the specified value is incorrect (" + c4.size() + ") : should have been 1", c4.size() == 1);
        // Get all Maps containing a particular value
        Extent e5 = pm.getExtent(container_class, true);
        Query q5 = pm.newQuery(e5, "items.containsEntry(\"Key1\",the_value) && the_value.name==\"Item 0\"");
        q5.declareImports("import " + item_class_parent.getName());
        q5.declareVariables(item_class_parent.getName() + " the_value");
        java.util.Collection c5 = (java.util.Collection) q5.execute();
        Assert.assertTrue("containsEntry : No of containers with the specified value is incorrect (" + c5.size() + ") : should have been 1", c5.size() == 1);
        tx.commit();
    } catch (JDOUserException e) {
        LOG.error(e);
        e.printStackTrace();
        Assert.assertTrue("Exception thrown while querying container objects " + e.getMessage(), false);
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
}
Also used : Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) MapHolder(org.jpox.samples.types.container.MapHolder) Extent(javax.jdo.Extent) JDOUserException(javax.jdo.JDOUserException) JDOUserException(javax.jdo.JDOUserException) Transaction(javax.jdo.Transaction) Map(java.util.Map)

Example 4 with Extent

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

the class SCOMapTests method checkQueryPrimitive.

/**
 * Utility for checking the use of queries with JDOQL on a Map with
 * primitive key/value objects. This is separate from the checkQuery
 * because with primitive (String) value a different table is created.
 * Uses containsKey(),containsValue(),containsEntry() methods.
 * @param pmf The PersistenceManager factory
 * @param container_class The container class e.g HashMapNormal
 * @param db_vendor_id Id of datastore e.g mysql (TODO : remove this)
 */
public static void checkQueryPrimitive(PersistenceManagerFactory pmf, Class container_class, String db_vendor_id) throws Exception {
    int NO_OF_ITEMS = 5;
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        // Create a container
        MapHolder container = null;
        try {
            container = (MapHolder) container_class.newInstance();
        } catch (Exception e1) {
            LOG.error(e1);
            Assert.fail("Failed to find Container class " + container_class.getName());
        }
        // Create a few items and add them to a Map
        java.util.Map m = new java.util.HashMap();
        for (int i = 0; i < NO_OF_ITEMS; i++) {
            m.put(new String("Key" + (i + 1)), new String("Value " + (i + 1)));
        }
        // Add the items to the container
        SCOHolderUtilities.addItemsToMap(container, m);
        pm.makePersistent(container);
        JDOHelper.getObjectId(container);
        tx.commit();
    } catch (JDOUserException e) {
        LOG.error(e);
        Assert.assertTrue("Exception thrown while creating " + container_class.getName() + " " + e.getMessage(), false);
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
    // Query the Map
    pm = pmf.getPersistenceManager();
    tx = pm.currentTransaction();
    try {
        tx.begin();
        // Get all Maps that contain the key "Key1" - should return 1
        Extent e1 = pm.getExtent(container_class, true);
        Query q1 = pm.newQuery(e1, "items.containsKey(\"Key1\")");
        java.util.Collection c1 = (java.util.Collection) q1.execute();
        Assert.assertTrue("containsKey : No of containers with a key \"Key1\" is incorrect (" + c1.size() + ") : should have been 1", c1.size() == 1);
        // Get all Maps that contain the key "Key7" - should return 0
        Extent e2 = pm.getExtent(container_class, true);
        Query q2 = pm.newQuery(e2, "items.containsKey(\"Key7\")");
        java.util.Collection c2 = (java.util.Collection) q2.execute();
        Assert.assertTrue("containsKey : No of containers with a key \"Key7\" is incorrect (" + c2.size() + ") : should have been 0", c2.size() == 0);
        // TODO : remove the MySQL omittal when it supports subqueries
        if (db_vendor_id == null || !db_vendor_id.equals("mysql")) {
            Extent e3 = pm.getExtent(container_class, true);
            Query q3 = pm.newQuery(e3, "items.isEmpty()");
            java.util.Collection c3 = (java.util.Collection) q3.execute();
            Assert.assertTrue("No of containers that are empty is incorrect (" + c3.size() + ") : should have been 0", c3.size() == 0);
        }
        // Get all Maps containing a particular value
        Extent e4 = pm.getExtent(container_class, true);
        Query q4 = pm.newQuery(e4, "items.containsValue(the_value) && the_value==\"Value 1\"");
        q4.declareImports("import java.lang.String");
        q4.declareVariables("java.lang.String the_value");
        java.util.Collection c4 = (java.util.Collection) q4.execute();
        Assert.assertTrue("containsValue : No of containers with the specified value is incorrect (" + c4.size() + ") : should have been 1", c4.size() == 1);
        // Get all Maps containing a particular entry
        Extent e5 = pm.getExtent(container_class, true);
        Query q5 = pm.newQuery(e5, "items.containsEntry(\"Key1\",the_value) && the_value==\"Value 1\"");
        q5.declareImports("import java.lang.String");
        q5.declareVariables("java.lang.String the_value");
        java.util.Collection c5 = (java.util.Collection) q5.execute();
        Assert.assertTrue("containsEntry : No of containers with the specified value is incorrect (" + c5.size() + ") : should have been 1", c5.size() == 1);
        tx.commit();
    } catch (JDOUserException e) {
        LOG.error(e);
        e.printStackTrace();
        Assert.assertTrue("Exception thrown while querying container objects " + e.getMessage(), false);
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
}
Also used : Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) MapHolder(org.jpox.samples.types.container.MapHolder) Extent(javax.jdo.Extent) JDOUserException(javax.jdo.JDOUserException) JDOUserException(javax.jdo.JDOUserException) Transaction(javax.jdo.Transaction) Map(java.util.Map)

Example 5 with Extent

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

the class ApplicationIdentityTest method testQueryUsingPrimaryKeyFields.

/**
 * Test for querying of PK fields of a composite PK.
 * Query of candidate class as well as 1-1 relation FK fields.
 */
public void testQueryUsingPrimaryKeyFields() {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            Car audi = new Car("A6", "O6");
            Car mercedes = new Car("Class E", "OE");
            Car volks = new Car("Beetle", "OB");
            FourByFour c1 = new FourByFour("C1", "O1");
            FourByFour c2 = new FourByFour("C2", "O2");
            audi.setTowedCar(c1);
            mercedes.setTowedCar(c2);
            pm.makePersistentAll(new Car[] { audi, mercedes, volks });
            pm.flush();
            Query q = pm.newQuery(Car.class);
            q.declareParameters("String pOwnerID, String pCarID");
            q.setFilter("ownerID == pOwnerID && carID == pCarID");
            Collection c = (Collection) q.execute("O6", "A6");
            assertEquals(1, c.size());
            assertEquals("A6", ((Car) c.iterator().next()).getCarID());
            assertEquals("O6", ((Car) c.iterator().next()).getOwnerID());
            q = pm.newQuery(Car.class);
            q.declareVariables("Car tow");
            q.declareParameters("String pOwnerID, String pCarID");
            q.setFilter("towedCar == tow && tow.ownerID == pOwnerID && tow.carID == pCarID");
            c = (Collection) q.execute("O1", "C1");
            assertEquals(1, c.size());
            assertEquals("A6", ((Car) c.iterator().next()).getCarID());
            assertEquals("O6", ((Car) c.iterator().next()).getOwnerID());
            q = pm.newQuery(Car.class);
            q.declareParameters("String pOwnerID, String pCarID");
            q.setFilter("towedCar.ownerID == pOwnerID && towedCar.carID == pCarID");
            c = (Collection) q.execute("O1", "C1");
            assertEquals(1, c.size());
            assertEquals("A6", ((Car) c.iterator().next()).getCarID());
            assertEquals("O6", ((Car) c.iterator().next()).getOwnerID());
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.commit();
            }
            pm.close();
        }
    } finally {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            Extent ex = pm.getExtent(Car.class, true);
            Iterator iter = ex.iterator();
            while (iter.hasNext()) {
                Car car = (Car) iter.next();
                car.setTowedCar(null);
            }
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        clean(Car.class);
        clean(FourByFour.class);
    }
}
Also used : Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) Car(org.jpox.samples.identity.application.Car) Extent(javax.jdo.Extent) FourByFour(org.jpox.samples.identity.application.FourByFour) Iterator(java.util.Iterator) Collection(java.util.Collection)

Aggregations

Extent (javax.jdo.Extent)72 PersistenceManager (javax.jdo.PersistenceManager)72 Transaction (javax.jdo.Transaction)70 Iterator (java.util.Iterator)62 JDOUserException (javax.jdo.JDOUserException)35 Collection (java.util.Collection)22 JDOPersistenceManager (org.datanucleus.api.jdo.JDOPersistenceManager)22 Query (javax.jdo.Query)15 Manager (org.jpox.samples.models.company.Manager)13 StoreManager (org.datanucleus.store.StoreManager)11 Method (java.lang.reflect.Method)10 JDOObjectNotFoundException (javax.jdo.JDOObjectNotFoundException)10 PersistenceManagerFactory (javax.jdo.PersistenceManagerFactory)10 Employee (org.jpox.samples.models.company.Employee)10 JDOException (javax.jdo.JDOException)9 SQLException (java.sql.SQLException)8 Department (org.jpox.samples.models.company.Department)8 InversePrimitive (org.datanucleus.samples.widget.InversePrimitive)7 Primitive (org.datanucleus.samples.widget.Primitive)6 List (java.util.List)5