Search in sources :

Example 1 with SetWidgetCounts

use of org.jpox.samples.rdbms.views.SetWidgetCounts 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)

Aggregations

Collection (java.util.Collection)1 HashSet (java.util.HashSet)1 Iterator (java.util.Iterator)1 Extent (javax.jdo.Extent)1 JDOException (javax.jdo.JDOException)1 PersistenceManager (javax.jdo.PersistenceManager)1 Query (javax.jdo.Query)1 Transaction (javax.jdo.Transaction)1 SetWidgetCounts (org.jpox.samples.rdbms.views.SetWidgetCounts)1 HashSetWidget (org.jpox.samples.widget.HashSetWidget)1 SetWidget (org.jpox.samples.widget.SetWidget)1