001: /*
002: * Copyright 2003 (C) TJDO.
003: * All rights reserved.
004: *
005: * This software is distributed under the terms of the TJDO License version 1.0.
006: * See the terms of the TJDO License in the documentation provided with this software.
007: *
008: * $Id: ViewTest.java,v 1.3 2004/02/07 22:44:15 jackknifebarber Exp $
009: */
010:
011: package com.triactive.jdo.test.views;
012:
013: import com.triactive.jdo.store.ViewNotSupportedException;
014: import com.triactive.jdo.test.*;
015: import java.util.Collection;
016: import java.util.HashSet;
017: import java.util.Iterator;
018: import javax.jdo.Extent;
019: import javax.jdo.PersistenceManager;
020: import javax.jdo.Query;
021: import javax.jdo.Transaction;
022: import org.apache.log4j.Category;
023:
024: /**
025: * Tests the functionality of view objects.
026: *
027: * @author <a href="mailto:mmartin5@austin.rr.com">Mike Martin</a>
028: * @version $Revision: 1.3 $
029: */
030:
031: public class ViewTest extends StorageTestCase {
032: private static final Category LOG = Category
033: .getInstance(ViewTest.class);
034:
035: private boolean schemaInitialized = false;
036:
037: /**
038: * Used by the JUnit framework to construct tests. Normally, programmers
039: * would never explicitly use this constructor.
040: *
041: * @param name Name of the <tt>TestCase</tt>.
042: */
043:
044: public ViewTest(String name) {
045: super (name);
046: }
047:
048: protected void setUp() throws Exception {
049: super .setUp();
050:
051: if (!schemaInitialized) {
052: addClassesToSchema(new Class[] { DateWidget.class,
053: ElementWidget.class, FloatWidget.class,
054: SetWidget.class, SetWidgetCounts.class,
055: StringWidget.class, Widget.class });
056:
057: /*
058: * Can't create this view on SQL Server because it doesn't allow you
059: * to GROUP BY a bit column.
060: */
061: if (!"sqlserver".equals(vendorID))
062: addClassesToSchema(new Class[] { MinMaxWidgetValues.class });
063:
064: schemaInitialized = true;
065: }
066: }
067:
068: public void testViewOfWidgets() throws Exception {
069: /*
070: * Can't run this test on SQL Server because it doesn't allow you to
071: * GROUP BY a bit column.
072: */
073: if ("sqlserver".equals(vendorID))
074: return;
075:
076: LOG.info("Testing view derived from of " + TEST_OBJECT_COUNT
077: + " " + Widget.class.getName() + " objects");
078: insertObjects(Widget.class);
079:
080: MinMaxWidgetValues trueValues = new MinMaxWidgetValues(true);
081: MinMaxWidgetValues falseValues = new MinMaxWidgetValues(false);
082:
083: for (int i = 0; i < objs.length; ++i) {
084: Widget w = (Widget) objs[i];
085: MinMaxWidgetValues tfv = w.getBooleanField() ? trueValues
086: : falseValues;
087:
088: if (tfv.getMinByteValue() > w.getByteField())
089: tfv.setMinByteValue(w.getByteField());
090:
091: if (tfv.getMinShortValue() > w.getShortField())
092: tfv.setMinShortValue(w.getShortField());
093:
094: if (tfv.getMaxIntValue() < w.getIntField())
095: tfv.setMaxIntValue(w.getIntField());
096:
097: if (tfv.getMaxLongValue() < w.getLongField())
098: tfv.setMaxLongValue(w.getLongField());
099: }
100:
101: PersistenceManager pm = pmf.getPersistenceManager();
102: Transaction tx = pm.currentTransaction();
103:
104: try {
105: tx.begin();
106:
107: Extent ext = pm.getExtent(MinMaxWidgetValues.class, false);
108: Iterator exti = ext.iterator();
109: int count = 0;
110:
111: while (exti.hasNext()) {
112: MinMaxWidgetValues wv = (MinMaxWidgetValues) exti
113: .next();
114: MinMaxWidgetValues tfv;
115:
116: if (wv.getBooleanValue()) {
117: tfv = trueValues;
118: trueValues = null;
119: } else {
120: tfv = falseValues;
121: falseValues = null;
122: }
123:
124: assertFieldsEqual(tfv, wv);
125: ++count;
126: }
127:
128: assertEquals(
129: "Iteration over view extent returned wrong number of rows",
130: 2, count);
131:
132: tx.commit();
133:
134: /*
135: * Negative test #1. Ensure that an attempt to write a field
136: * throws the proper exception.
137: */
138:
139: try {
140: tx.begin();
141:
142: MinMaxWidgetValues wv = (MinMaxWidgetValues) ext
143: .iterator().next();
144: wv.fillRandom();
145:
146: tx.commit();
147:
148: fail("Writing to a persistent view object succeeded");
149: } catch (ViewNotSupportedException e) {
150: if (tx.isActive())
151: tx.rollback();
152: }
153:
154: /*
155: * Negative test #2. Ensure that an attempt to make a view object
156: * persistent throws the proper exception.
157: */
158:
159: try {
160: tx.begin();
161:
162: MinMaxWidgetValues wv = new MinMaxWidgetValues(true);
163: pm.makePersistent(wv);
164:
165: tx.commit();
166:
167: fail("Making a view object persistent succeeded");
168: } catch (ViewNotSupportedException e) {
169: if (tx.isActive())
170: tx.rollback();
171: }
172:
173: /*
174: * Negative test #3. Ensure that an attempt to delete a view object
175: * throws the proper exception.
176: */
177:
178: try {
179: tx.begin();
180:
181: MinMaxWidgetValues wv = (MinMaxWidgetValues) ext
182: .iterator().next();
183: pm.deletePersistent(wv);
184:
185: tx.commit();
186:
187: fail("Deleting a persistent view object succeeded");
188: } catch (ViewNotSupportedException e) {
189: if (tx.isActive())
190: tx.rollback();
191: }
192: } finally {
193: if (tx.isActive())
194: tx.rollback();
195:
196: pm.close();
197: }
198:
199: removeObjects();
200: }
201:
202: public void testViewOfSetWidgets() throws Exception {
203: /*
204: * We can't run this test on Cloudscape because the view used by
205: * SetWidgetCounts doesn't execute properly; some counts that should
206: * be 0 come up 1. This is presumably due to a bug in Cloudscape
207: * (last tried on both 3.6 and 4.0).
208: */
209: if ("cloudscape".equals(vendorID))
210: return;
211:
212: LOG.info("Testing view derived from of " + TEST_OBJECT_COUNT
213: + " " + SetWidget.class.getName() + " objects");
214: insertObjects(SetWidget.class);
215:
216: PersistenceManager pm = pmf.getPersistenceManager();
217: Transaction tx = pm.currentTransaction();
218:
219: try {
220: tx.begin();
221:
222: Extent ext = pm.getExtent(SetWidgetCounts.class, true);
223: Iterator exti = ext.iterator();
224: int count = 0;
225:
226: while (exti.hasNext()) {
227: SetWidgetCounts actual = (SetWidgetCounts) exti.next();
228: SetWidgetCounts expected = new SetWidgetCounts(actual
229: .getSetWidget());
230:
231: assertFieldsEqual(expected, actual);
232: ++count;
233: }
234:
235: tx.commit();
236:
237: assertEquals(
238: "Iteration over view extent returned wrong number of rows",
239: TEST_OBJECT_COUNT, count);
240:
241: tx.begin();
242:
243: Query query = pm.newQuery(pm.getExtent(
244: SetWidgetCounts.class, true));
245: query.setFilter("normalSetSize != 0");
246: query.setOrdering("sw.numElementWidgets descending");
247: Collection results = (Collection) query.execute();
248:
249: try {
250: HashSet expected = new HashSet();
251:
252: for (int i = 0; i < objs.length; ++i) {
253: SetWidget sw = (SetWidget) objs[i];
254:
255: if (sw.getNormalSet().size() != 0)
256: expected.add(new SetWidgetCounts(sw));
257: }
258:
259: assertTrue(
260: "Query has no expected results (test is broken)",
261: !expected.isEmpty());
262: assertTrue("Query returned no rows", !results.isEmpty());
263:
264: HashSet actual = new HashSet(results);
265:
266: assertEquals("Query returned duplicate rows", results
267: .size(), actual.size());
268: assertTrue(
269: "Query did not return expected results: expected "
270: + expected + ", but was " + actual,
271: TestObject.compareCollection(expected, actual));
272: } finally {
273: query.closeAll();
274: }
275:
276: tx.commit();
277: } finally {
278: if (tx.isActive())
279: tx.rollback();
280:
281: pm.close();
282: }
283:
284: removeObjects();
285: }
286: }
|