KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > triactive > jdo > test > views > ViewTest


1 /*
2  * Copyright 2003 (C) TJDO.
3  * All rights reserved.
4  *
5  * This software is distributed under the terms of the TJDO License version 1.0.
6  * See the terms of the TJDO License in the documentation provided with this software.
7  *
8  * $Id: ViewTest.java,v 1.3 2004/02/07 22:44:15 jackknifebarber Exp $
9  */

10
11 package com.triactive.jdo.test.views;
12
13 import com.triactive.jdo.store.ViewNotSupportedException;
14 import com.triactive.jdo.test.*;
15 import java.util.Collection JavaDoc;
16 import java.util.HashSet JavaDoc;
17 import java.util.Iterator JavaDoc;
18 import javax.jdo.Extent;
19 import javax.jdo.PersistenceManager;
20 import javax.jdo.Query;
21 import javax.jdo.Transaction;
22 import org.apache.log4j.Category;
23
24
25 /**
26  * Tests the functionality of view objects.
27  *
28  * @author <a HREF="mailto:mmartin5@austin.rr.com">Mike Martin</a>
29  * @version $Revision: 1.3 $
30  */

31
32 public class ViewTest extends StorageTestCase
33 {
34     private static final Category LOG = Category.getInstance(ViewTest.class);
35
36     private boolean schemaInitialized = false;
37
38
39     /**
40      * Used by the JUnit framework to construct tests. Normally, programmers
41      * would never explicitly use this constructor.
42      *
43      * @param name Name of the <tt>TestCase</tt>.
44      */

45
46     public ViewTest(String JavaDoc name)
47     {
48         super(name);
49     }
50
51
52     protected void setUp() throws Exception JavaDoc
53     {
54         super.setUp();
55
56         if (!schemaInitialized)
57         {
58             addClassesToSchema(new Class JavaDoc[]
59                 {
60                     DateWidget.class,
61                     ElementWidget.class,
62                     FloatWidget.class,
63                     SetWidget.class,
64                     SetWidgetCounts.class,
65                     StringWidget.class,
66                     Widget.class
67                 }
68             );
69
70             /*
71              * Can't create this view on SQL Server because it doesn't allow you
72              * to GROUP BY a bit column.
73              */

74             if (!"sqlserver".equals(vendorID))
75                 addClassesToSchema(new Class JavaDoc[] { MinMaxWidgetValues.class });
76
77             schemaInitialized = true;
78         }
79     }
80
81
82     public void testViewOfWidgets() throws Exception JavaDoc
83     {
84         /*
85          * Can't run this test on SQL Server because it doesn't allow you to
86          * GROUP BY a bit column.
87          */

88         if ("sqlserver".equals(vendorID))
89             return;
90
91         LOG.info("Testing view derived from of " + TEST_OBJECT_COUNT + " " + Widget.class.getName() + " objects");
92         insertObjects(Widget.class);
93
94         MinMaxWidgetValues trueValues = new MinMaxWidgetValues(true);
95         MinMaxWidgetValues falseValues = new MinMaxWidgetValues(false);
96
97         for (int i = 0; i < objs.length; ++i)
98         {
99             Widget w = (Widget)objs[i];
100             MinMaxWidgetValues tfv = w.getBooleanField() ? trueValues : falseValues;
101
102             if (tfv.getMinByteValue() > w.getByteField())
103                 tfv.setMinByteValue(w.getByteField());
104
105             if (tfv.getMinShortValue() > w.getShortField())
106                 tfv.setMinShortValue(w.getShortField());
107
108             if (tfv.getMaxIntValue() < w.getIntField())
109                 tfv.setMaxIntValue(w.getIntField());
110
111             if (tfv.getMaxLongValue() < w.getLongField())
112                 tfv.setMaxLongValue(w.getLongField());
113         }
114
115         PersistenceManager pm = pmf.getPersistenceManager();
116         Transaction tx = pm.currentTransaction();
117
118         try
119         {
120             tx.begin();
121
122             Extent ext = pm.getExtent(MinMaxWidgetValues.class, false);
123             Iterator JavaDoc exti = ext.iterator();
124             int count = 0;
125
126             while (exti.hasNext())
127             {
128                 MinMaxWidgetValues wv = (MinMaxWidgetValues)exti.next();
129                 MinMaxWidgetValues tfv;
130                 
131                 if (wv.getBooleanValue())
132                 {
133                     tfv = trueValues;
134                     trueValues = null;
135                 }
136                 else
137                 {
138                     tfv = falseValues;
139                     falseValues = null;
140                 }
141
142                 assertFieldsEqual(tfv, wv);
143                 ++count;
144             }
145
146             assertEquals("Iteration over view extent returned wrong number of rows", 2, count);
147
148             tx.commit();
149
150             /*
151              * Negative test #1. Ensure that an attempt to write a field
152              * throws the proper exception.
153              */

154
155             try
156             {
157                 tx.begin();
158
159                 MinMaxWidgetValues wv = (MinMaxWidgetValues)ext.iterator().next();
160                 wv.fillRandom();
161
162                 tx.commit();
163
164                 fail("Writing to a persistent view object succeeded");
165             }
166             catch (ViewNotSupportedException e)
167             {
168                 if (tx.isActive())
169                     tx.rollback();
170             }
171
172             /*
173              * Negative test #2. Ensure that an attempt to make a view object
174              * persistent throws the proper exception.
175              */

176
177             try
178             {
179                 tx.begin();
180
181                 MinMaxWidgetValues wv = new MinMaxWidgetValues(true);
182                 pm.makePersistent(wv);
183
184                 tx.commit();
185
186                 fail("Making a view object persistent succeeded");
187             }
188             catch (ViewNotSupportedException e)
189             {
190                 if (tx.isActive())
191                     tx.rollback();
192             }
193
194             /*
195              * Negative test #3. Ensure that an attempt to delete a view object
196              * throws the proper exception.
197              */

198
199             try
200             {
201                 tx.begin();
202
203                 MinMaxWidgetValues wv = (MinMaxWidgetValues)ext.iterator().next();
204                 pm.deletePersistent(wv);
205
206                 tx.commit();
207
208                 fail("Deleting a persistent view object succeeded");
209             }
210             catch (ViewNotSupportedException e)
211             {
212                 if (tx.isActive())
213                     tx.rollback();
214             }
215         }
216         finally
217         {
218             if (tx.isActive())
219                 tx.rollback();
220
221             pm.close();
222         }
223
224         removeObjects();
225     }
226
227
228     public void testViewOfSetWidgets() throws Exception JavaDoc
229     {
230         /*
231          * We can't run this test on Cloudscape because the view used by
232          * SetWidgetCounts doesn't execute properly; some counts that should
233          * be 0 come up 1. This is presumably due to a bug in Cloudscape
234          * (last tried on both 3.6 and 4.0).
235          */

236         if ("cloudscape".equals(vendorID))
237             return;
238
239         LOG.info("Testing view derived from of " + TEST_OBJECT_COUNT + " " + SetWidget.class.getName() + " objects");
240         insertObjects(SetWidget.class);
241
242         PersistenceManager pm = pmf.getPersistenceManager();
243         Transaction tx = pm.currentTransaction();
244
245         try
246         {
247             tx.begin();
248
249             Extent ext = pm.getExtent(SetWidgetCounts.class, true);
250             Iterator JavaDoc exti = ext.iterator();
251             int count = 0;
252
253             while (exti.hasNext())
254             {
255                 SetWidgetCounts actual = (SetWidgetCounts)exti.next();
256                 SetWidgetCounts expected = new SetWidgetCounts(actual.getSetWidget());
257                 
258                 assertFieldsEqual(expected, actual);
259                 ++count;
260             }
261
262             tx.commit();
263
264             assertEquals("Iteration over view extent returned wrong number of rows", TEST_OBJECT_COUNT, count);
265
266             tx.begin();
267
268             Query query = pm.newQuery(pm.getExtent(SetWidgetCounts.class, true));
269             query.setFilter("normalSetSize != 0");
270             query.setOrdering("sw.numElementWidgets descending");
271             Collection JavaDoc results = (Collection JavaDoc)query.execute();
272
273             try
274             {
275                 HashSet JavaDoc expected = new HashSet JavaDoc();
276
277                 for (int i = 0; i < objs.length; ++i)
278                 {
279                     SetWidget sw = (SetWidget)objs[i];
280
281                     if (sw.getNormalSet().size() != 0)
282                         expected.add(new SetWidgetCounts(sw));
283                 }
284
285                 assertTrue("Query has no expected results (test is broken)", !expected.isEmpty());
286                 assertTrue("Query returned no rows", !results.isEmpty());
287
288                 HashSet JavaDoc actual = new HashSet JavaDoc(results);
289
290                 assertEquals("Query returned duplicate rows", results.size(), actual.size());
291                 assertTrue("Query did not return expected results: expected " + expected + ", but was " + actual, TestObject.compareCollection(expected, actual));
292             }
293             finally
294             {
295                 query.closeAll();
296             }
297
298             tx.commit();
299         }
300         finally
301         {
302             if (tx.isActive())
303                 tx.rollback();
304
305             pm.close();
306         }
307
308         removeObjects();
309     }
310 }
311
Popular Tags