Search in sources :

Example 1 with MinMaxWidgetValues

use of org.jpox.samples.rdbms.views.MinMaxWidgetValues in project tests by datanucleus.

the class ViewTest method testViewOfWidgets.

public void testViewOfWidgets() throws Exception {
    if ("sqlserver".equals(vendorID)) {
        // Can't run this test on SQL Server because it doesn't allow you to GROUP BY a bit column.
        return;
    }
    try {
        LOG.info("Testing view derived from of " + StorageTester.TEST_OBJECT_COUNT + " " + Widget.class.getName() + " objects");
        tester.insertObjects(Widget.class);
        MinMaxWidgetValues trueValues = new MinMaxWidgetValues(true);
        MinMaxWidgetValues falseValues = new MinMaxWidgetValues(false);
        TestObject[] objs = tester.getObjects();
        for (int i = 0; i < objs.length; ++i) {
            Widget w = (Widget) objs[i];
            MinMaxWidgetValues tfv = w.getBooleanField() ? trueValues : falseValues;
            if (tfv.getMinByteValue() > w.getByteField()) {
                tfv.setMinByteValue(w.getByteField());
            }
            if (tfv.getMinShortValue() > w.getShortField()) {
                tfv.setMinShortValue(w.getShortField());
            }
            if (tfv.getMaxIntValue() < w.getIntField()) {
                tfv.setMaxIntValue(w.getIntField());
            }
            if (tfv.getMaxLongValue() < w.getLongField()) {
                tfv.setMaxLongValue(w.getLongField());
            }
        }
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            Extent ext = pm.getExtent(MinMaxWidgetValues.class, false);
            Iterator exti = ext.iterator();
            int count = 0;
            while (exti.hasNext()) {
                MinMaxWidgetValues wv = (MinMaxWidgetValues) exti.next();
                MinMaxWidgetValues tfv;
                if (wv.getBooleanValue()) {
                    tfv = trueValues;
                    trueValues = null;
                } else {
                    tfv = falseValues;
                    falseValues = null;
                }
                StorageTester.assertFieldsEqual(tfv, wv);
                ++count;
            }
            assertEquals("Iteration over view extent returned wrong number of rows", 2, count);
            tx.commit();
            /*
                 * Negative test #1.  Ensure that an attempt to write a field
                 * throws the proper exception.
                 */
            try {
                tx.begin();
                MinMaxWidgetValues wv = (MinMaxWidgetValues) ext.iterator().next();
                wv.fillRandom();
                tx.commit();
                fail("Writing to a persistent view object succeeded");
            } catch (Exception e) {
                if (tx.isActive()) {
                    tx.rollback();
                }
            }
            /*
                 * Negative test #2.  Ensure that an attempt to make a view object
                 * persistent throws the proper exception.
                 */
            try {
                tx.begin();
                MinMaxWidgetValues wv = new MinMaxWidgetValues(true);
                pm.makePersistent(wv);
                tx.commit();
                fail("Making a view object persistent succeeded");
            } catch (Exception e) {
                if (tx.isActive()) {
                    tx.rollback();
                }
            }
            /*
                 * Negative test #3.  Ensure that an attempt to delete a view object
                 * throws the proper exception.
                 */
            try {
                tx.begin();
                MinMaxWidgetValues wv = (MinMaxWidgetValues) ext.iterator().next();
                pm.deletePersistent(wv);
                tx.commit();
                fail("Deleting a persistent view object succeeded");
            } catch (Exception e) {
                if (tx.isActive()) {
                    tx.rollback();
                }
            }
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        tester.removeObjects();
        clean(Widget.class);
        clean(MinMaxWidgetValues.class);
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) Extent(javax.jdo.Extent) DateWidget(org.jpox.samples.widget.DateWidget) Widget(org.jpox.samples.widget.Widget) DecimalWidget(org.jpox.samples.widget.DecimalWidget) StringWidget(org.jpox.samples.widget.StringWidget) ElementWidget(org.jpox.samples.widget.ElementWidget) FloatWidget(org.jpox.samples.widget.FloatWidget) SetWidget(org.jpox.samples.widget.SetWidget) HashSetWidget(org.jpox.samples.widget.HashSetWidget) Iterator(java.util.Iterator) JDOException(javax.jdo.JDOException) NucleusDataStoreException(org.datanucleus.exceptions.NucleusDataStoreException) MinMaxWidgetValues(org.jpox.samples.rdbms.views.MinMaxWidgetValues)

Aggregations

Iterator (java.util.Iterator)1 Extent (javax.jdo.Extent)1 JDOException (javax.jdo.JDOException)1 PersistenceManager (javax.jdo.PersistenceManager)1 Transaction (javax.jdo.Transaction)1 NucleusDataStoreException (org.datanucleus.exceptions.NucleusDataStoreException)1 MinMaxWidgetValues (org.jpox.samples.rdbms.views.MinMaxWidgetValues)1 DateWidget (org.jpox.samples.widget.DateWidget)1 DecimalWidget (org.jpox.samples.widget.DecimalWidget)1 ElementWidget (org.jpox.samples.widget.ElementWidget)1 FloatWidget (org.jpox.samples.widget.FloatWidget)1 HashSetWidget (org.jpox.samples.widget.HashSetWidget)1 SetWidget (org.jpox.samples.widget.SetWidget)1 StringWidget (org.jpox.samples.widget.StringWidget)1 Widget (org.jpox.samples.widget.Widget)1