use of org.jpox.samples.widget.Widget 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);
}
}
Aggregations