KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > broker > RsIteratorTest


1 package org.apache.ojb.broker;
2
3 import java.util.Iterator JavaDoc;
4
5 import junit.framework.TestCase;
6 import org.apache.ojb.broker.query.*;
7 import org.apache.ojb.broker.accesslayer.RsIterator;
8
9 /**
10  * Test case for the RsIterator
11  *
12  * @author <a HREF="mailto:rongallagher@bellsouth.net">Ron Gallagher<a>
13  * @version $Id: $
14  */

15 public class RsIteratorTest extends TestCase
16 {
17     private PersistenceBroker broker;
18
19     public static void main(String JavaDoc[] args)
20     {
21         String JavaDoc[] arr = {RsIteratorTest.class.getName()};
22         junit.textui.TestRunner.main(arr);
23     }
24
25     public RsIteratorTest(String JavaDoc name)
26     {
27         super(name);
28     }
29
30     public void setUp()
31     {
32         broker = PersistenceBrokerFactory.defaultPersistenceBroker();
33     }
34
35     public void tearDown()
36     {
37         if(broker != null)
38         {
39             broker.close();
40         }
41     }
42
43     public void testRsIterator() throws Exception JavaDoc
44     {
45         String JavaDoc name = "testRsIterator_" + System.currentTimeMillis();
46         prepareTest(name);
47
48         Criteria criteria = new Criteria();
49         criteria.addLike("name", name+"*");
50         Query query = new QueryByCriteria(ObjectRepository.Component.class, criteria);
51
52         Iterator JavaDoc it = broker.getIteratorByQuery(query);
53         int k = 0;
54         while(it.hasNext())
55         {
56             it.next();
57             k++;
58         }
59         assertEquals("Wrong number of items found", 2, k);
60     }
61
62     /**
63      * Test RsIterator cleanup on PB.commitTransaction()
64      */

65     public void testRsIteratorAutomaticCleanupCheck_1() throws Exception JavaDoc
66     {
67         String JavaDoc name = "testRsIteratorAutomaticCleanupCheck_1_" + System.currentTimeMillis();
68         prepareTest(name);
69
70         Criteria criteria = new Criteria();
71         criteria.addLike("name", name+"*");
72         Query query = new QueryByCriteria(ObjectRepository.Component.class, criteria);
73
74         Iterator JavaDoc it = broker.getIteratorByQuery(query);
75         it.hasNext();
76         broker.beginTransaction();
77         broker.commitTransaction();
78         /*
79         if tx was commited we invalidate RsIterator instance
80         */

81         try
82         {
83             it.next();
84             fail("We expect RsIterator has released resources on pb.commit..");
85         }
86         catch (RsIterator.ResourceClosedException e)
87         {
88             assertTrue(true);
89         }
90
91
92         it = broker.getIteratorByQuery(query);
93         it.hasNext();
94         it.next();
95         broker.beginTransaction();
96         broker.commitTransaction();
97         /*
98         if tx was commited we invalidate RsIterator instance
99         */

100         try
101         {
102             it.hasNext();
103             it.next();
104             fail("We expect RsIterator has released resources on pb.commit..");
105         }
106         catch (RsIterator.ResourceClosedException e)
107         {
108             assertTrue(true);
109         }
110     }
111
112     /**
113      * Test RsIterator cleanup on PB.abortTransaction()
114      */

115     public void testRsIteratorAutomaticCleanupCheck_2() throws Exception JavaDoc
116     {
117         String JavaDoc name = "testRsIteratorAutomaticCleanupCheck_" + System.currentTimeMillis();
118         prepareTest(name);
119
120         Criteria criteria = new Criteria();
121         criteria.addLike("name", name+"*");
122         Query query = new QueryByCriteria(ObjectRepository.Component.class, criteria);
123
124         Iterator JavaDoc it = broker.getIteratorByQuery(query);
125         it.hasNext();
126         broker.beginTransaction();
127         broker.abortTransaction();
128         /*
129         if tx was aborted we invalidate RsIterator instance
130         */

131         try
132         {
133             it.next();
134             fail("We expect RsIterator has released resources on pb.commit..");
135         }
136         catch (RsIterator.ResourceClosedException e)
137         {
138             assertTrue(true);
139         }
140
141
142         it = broker.getIteratorByQuery(query);
143         it.hasNext();
144         it.next();
145         broker.beginTransaction();
146         broker.abortTransaction();
147         /*
148         if tx was aborted we invalidate RsIterator instance
149         */

150         try
151         {
152             it.hasNext();
153             it.next();
154             fail("We expect RsIterator has released resources on pb.commit..");
155         }
156         catch (RsIterator.ResourceClosedException e)
157         {
158             assertTrue(true);
159         }
160     }
161
162     /**
163      * Test RsIterator cleanup on PB.close()
164      */

165     public void testRsIteratorAutomaticCleanupCheck_3() throws Exception JavaDoc
166     {
167         String JavaDoc name = "testRsIteratorAutomaticCleanupCheck_" + System.currentTimeMillis();
168         prepareTest(name);
169
170         Criteria criteria = new Criteria();
171         criteria.addLike("name", name+"*");
172         Query query = new QueryByCriteria(ObjectRepository.Component.class, criteria);
173
174         Iterator JavaDoc it = broker.getIteratorByQuery(query);
175         broker.close();
176         /*
177         if was closed we invalidate RsIterator instance
178         */

179         try
180         {
181             if(it.hasNext()) it.next();
182             fail("We expect RsIterator has released resources on pb.commit..");
183         }
184         catch (RsIterator.ResourceClosedException e)
185         {
186             assertTrue(true);
187         }
188     }
189
190     /**
191      * Test RsIterator cleanup on PB.abortTransaction()
192      */

193     public void testRsIteratorUserCleanup_1() throws Exception JavaDoc
194     {
195         String JavaDoc name = "testRsIteratorAutomaticCleanupCheck_" + System.currentTimeMillis();
196         prepareTest(name);
197
198         Criteria criteria = new Criteria();
199         criteria.addLike("name", name+"*");
200         Query query = new QueryByCriteria(ObjectRepository.Component.class, criteria);
201
202         Iterator JavaDoc it = broker.getIteratorByQuery(query);
203
204         /*
205         TODO: After integration of setAutoRelease into OJBIterator and changes
206         in PB interface getIteratorXXX methods we don't need these casts any longer
207         */

208         if(!(it instanceof RsIterator))
209         {
210             // skip test
211
return;
212         }
213
214         // TODO: Remove this cast one day
215
((RsIterator) it).setAutoRelease(false);
216
217         it.hasNext();
218         broker.beginTransaction();
219         broker.abortTransaction();
220         /*
221         if tx was aborted we invalidate RsIterator instance
222         */

223         try
224         {
225             it.next();
226             fail("We expect RsIterator has released resources on pb.commit..");
227         }
228         catch (RsIterator.ResourceClosedException e)
229         {
230             assertTrue(true);
231         }
232
233
234         it = broker.getIteratorByQuery(query);
235         // TODO: Remove this cast one day
236
((RsIterator) it).setAutoRelease(false);
237
238         it.hasNext();
239         it.next();
240         broker.beginTransaction();
241         broker.abortTransaction();
242         /*
243         if tx was aborted we invalidate RsIterator instance
244         */

245         try
246         {
247             it.hasNext();
248             it.next();
249             fail("We expect RsIterator has released resources on pb.commit..");
250         }
251         catch (RsIterator.ResourceClosedException e)
252         {
253             assertTrue(true);
254         }
255     }
256
257     /**
258      * Test RsIterator cleanup on PB.abortTransaction()
259      */

260     public void testRsIteratorUserCleanup_2() throws Exception JavaDoc
261     {
262         String JavaDoc name = "testRsIteratorAutomaticCleanupCheck_" + System.currentTimeMillis();
263         prepareTest(name);
264
265         Criteria criteria = new Criteria();
266         criteria.addLike("name", name+"*");
267         Query query = new QueryByCriteria(ObjectRepository.Component.class, criteria);
268
269         Iterator JavaDoc it = broker.getIteratorByQuery(query);
270
271         /*
272         TODO: After integration of setAutoRelease into OJBIterator and changes
273         in PB interface getIteratorXXX methods we don't need these casts any longer
274         */

275         if(!(it instanceof RsIterator))
276         {
277             // skip test
278
return;
279         }
280
281         // TODO: Remove this cast one day
282
((RsIterator) it).setAutoRelease(false);
283
284         while(it.hasNext())
285         {
286             ObjectRepository.Component c = (ObjectRepository.Component) it.next();
287             assertNotNull(c.getId());
288         }
289
290         try
291         {
292             ((RsIterator) it).relative(1);
293         }
294         catch(RsIterator.ResourceClosedException e)
295         {
296             fail("RsIterator should not close resources by itself");
297         }
298         catch (PersistenceBrokerException ignore)
299         {
300         }
301
302         // TODO: Remove this cast one day
303
((RsIterator) it).releaseDbResources();
304     }
305
306
307     private void prepareTest(String JavaDoc objectName)
308     {
309         ObjectRepository.Component c1 = new ObjectRepository.Component();
310         c1.setName(objectName + "_1");
311         ObjectRepository.Component c2 = new ObjectRepository.Component();
312         c2.setName(objectName + "_2");
313
314         broker.beginTransaction();
315         broker.store(c1);
316         broker.store(c2);
317         broker.commitTransaction();
318     }
319
320     /**
321      * Test retrieving data via the rsIterator
322      * test by Ron Gallagher
323      */

324     public void testInternUsedRsIterator() throws Exception JavaDoc
325     {
326         // Build the query
327
Criteria criteria = new Criteria();
328         criteria.addEqualTo("id", new Integer JavaDoc(1));
329         Query query = new QueryByCriteria(Person.class, criteria);
330         // Run the query.
331
Person person = (Person) broker.getObjectByQuery(query);
332         assertNotNull("Person with id 1 was not found", person);
333     }
334
335 }
336
Popular Tags