KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > odmg > DSetTest


1 package org.apache.ojb.odmg;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.Collection JavaDoc;
5 import java.util.Iterator JavaDoc;
6
7 import org.apache.ojb.junit.ODMGTestCase;
8 import org.odmg.DSet;
9 import org.odmg.OQLQuery;
10 import org.odmg.Transaction;
11
12 /**
13  * Tests for OJB {@link org.odmg.DSet} implementation.
14  */

15 public class DSetTest extends ODMGTestCase
16 {
17     public static void main(String JavaDoc[] args)
18     {
19         String JavaDoc[] arr = {DSetTest.class.getName()};
20         junit.textui.TestRunner.main(arr);
21     }
22
23     public DSetTest(String JavaDoc name)
24
25     {
26         super(name);
27     }
28
29     protected DListTest.DObject createObject(String JavaDoc name) throws Exception JavaDoc
30     {
31         DListTest.DObject obj = new DListTest.DObject();
32         obj.setName(name);
33         obj.setRandomName("rnd_"+((int)(Math.random()*1000)));
34
35         return obj;
36     }
37
38     public void testAddingLockupWithTx() throws Exception JavaDoc
39     {
40         // create a unique name:
41
final String JavaDoc name = "testAdding_" + System.currentTimeMillis();
42
43         TransactionExt tx = (TransactionExt) odmg.newTransaction();
44         tx.begin();
45         // create DSet and bound by name
46
DSet list = odmg.newDSet();
47         database.bind(list, name);
48         tx.commit();
49
50         tx.begin();
51         tx.getBroker().clearCache();
52         Object JavaDoc obj = database.lookup(name);
53         tx.commit();
54         assertNotNull("binded DSet not found", obj);
55
56         tx.begin();
57         // add objects to list
58
for (int i = 0; i < 5; i++)
59         {
60             DListTest.DObject a = createObject(name);
61             list.add(a);
62         }
63         tx.commit();
64
65         // check current list
66
Iterator JavaDoc iter = list.iterator();
67         while (iter.hasNext())
68         {
69             DListTest.DObject a = (DListTest.DObject) iter.next();
70             assertNotNull(a);
71         }
72
73         tx.begin();
74         tx.getBroker().clearCache();
75
76         // lookup list and check entries
77
DSet lookedUp = (DSet) database.lookup(name);
78         assertNotNull("binded DSet not found", lookedUp);
79
80         //System.out.println("sequence of items in lookedup list:");
81
iter = lookedUp.iterator();
82         Iterator JavaDoc iter1 = list.iterator();
83         while (iter.hasNext())
84         {
85             DListTest.DObject a = (DListTest.DObject) iter.next();
86             DListTest.DObject b = (DListTest.DObject) iter1.next();
87             assertNotNull(a);
88             assertNotNull(b);
89             assertEquals(a.getId(), b.getId());
90         }
91         tx.commit();
92
93         // add new entries to list
94
tx.begin();
95         for (int i = 0; i < 3; i++)
96         {
97             DListTest.DObject a = createObject(name + "_new_entry");
98             list.add(a);
99         }
100         tx.commit();
101
102         tx.begin();
103         tx.getBroker().clearCache();
104         lookedUp = (DSet) database.lookup(name);
105         iter = lookedUp.iterator();
106         iter1 = list.iterator();
107         assertEquals("Wrong number of DListEntry found", 8, list.size());
108         while (iter.hasNext())
109         {
110             DListTest.DObject a = (DListTest.DObject) iter.next();
111             DListTest.DObject b = (DListTest.DObject) iter1.next();
112             assertNotNull(a);
113             assertNotNull(b);
114             assertEquals(a.getId(), b.getId());
115         }
116         tx.commit();
117         assertNotNull("binded DSet not found", lookedUp);
118     }
119
120     public void testReadAndStore() throws Exception JavaDoc
121     {
122         // create a unique name:
123
final String JavaDoc name = "testReadAndStore_" + System.currentTimeMillis();
124
125         // create test objects
126
Transaction tx = odmg.newTransaction();
127         tx.begin();
128         // add objects to list
129
for (int i = 0; i < 5; i++)
130         {
131             tx.lock(createObject(name), Transaction.WRITE);
132         }
133         tx.commit();
134
135         tx.begin();
136         // query test objects
137
OQLQuery q = odmg.newOQLQuery();
138         q.create("select all from "+DListTest.DObject.class.getName()+" where name=$1");
139         q.bind(name);
140         Collection JavaDoc ret = (Collection JavaDoc) q.execute();
141         // check result list size
142
assertEquals(5, ret.size());
143         // do read lock
144
for (Iterator JavaDoc it = ret.iterator(); it.hasNext(); )
145         {
146             tx.lock(it.next(), Transaction.READ);
147         }
148         // create new list for results
149
ArrayList JavaDoc result = new ArrayList JavaDoc();
150         result.addAll(ret);
151         tx.commit();
152     }
153
154     public void testIterateWithoutTx() throws Exception JavaDoc
155     {
156         // create a unique name:
157
final String JavaDoc name = "testAdding_" + System.currentTimeMillis();
158
159         // get DSet and fill with objects
160
DSet list = odmg.newDSet();
161         Transaction tx = odmg.newTransaction();
162         tx.begin();
163         for (int i = 0; i < 5; i++)
164         {
165             DListTest.DObject a = createObject(name);
166             list.add(a);
167         }
168         // bind the new list
169
database.bind(list, name);
170         tx.commit();
171
172         tx = odmg.newTransaction();
173         tx.begin();
174         Object JavaDoc obj = database.lookup(name);
175         tx.commit();
176         assertNotNull("binded DSet not found", obj);
177
178         // iterate list
179
Iterator JavaDoc iter = list.iterator();
180         while (iter.hasNext())
181         {
182             DListTest.DObject a = (DListTest.DObject) iter.next();
183             assertNotNull(a);
184         }
185         assertEquals(5, list.size());
186
187         tx = odmg.newTransaction();
188         tx.begin();
189         ((TransactionExt) odmg.currentTransaction()).getBroker().clearCache();
190         DSet lookedUp = (DSet) database.lookup(name);
191         tx.commit();
192         assertNotNull("binded DSet not found", lookedUp);
193
194         //System.out.println("sequence of items in lookedup list:");
195
iter = lookedUp.iterator();
196         Iterator JavaDoc iter1 = list.iterator();
197         while (iter.hasNext())
198         {
199             DListTest.DObject a = (DListTest.DObject) iter.next();
200             DListTest.DObject b = (DListTest.DObject) iter1.next();
201             assertNotNull(a);
202             assertNotNull(b);
203             assertEquals(a.getId(), b.getId());
204         }
205     }
206
207     /**
208      * this test checks if removing item from DSet works
209      */

210     public void testRemoving() throws Exception JavaDoc
211     {
212         // create a unique name:
213
String JavaDoc name = "testRemoving_" + System.currentTimeMillis();
214
215         TransactionExt tx = (TransactionExt) odmg.newTransaction();
216         tx.begin();
217         DSet list = odmg.newDSet();
218         // bind the list to the name:
219
database.bind(list, name);
220
221         Object JavaDoc first = null;
222         Object JavaDoc second = null;
223         for (int i = 0; i < 5; i++)
224         {
225             DListTest.DObject a = createObject(name);
226             list.add(a);
227             if(i==1) first = a;
228             if(i==2) second = a;
229         }
230         assertEquals(5, list.size());
231         tx.commit();
232
233         // delete two items
234
tx = (TransactionExt) odmg.newTransaction();
235         tx.begin();
236         ((HasBroker) odmg.currentTransaction()).getBroker().clearCache();
237         DSet lookedUp = (DSet) database.lookup(name);
238         assertNotNull("database lookup does not find the named DSet", lookedUp);
239         assertEquals("Wrong number of list entries", 5, lookedUp.size());
240         lookedUp.remove(first);
241         lookedUp.remove(second);
242         tx.commit();
243
244         // check if deletion was successful
245
tx = (TransactionExt) odmg.newTransaction();
246         tx.begin();
247         tx.getBroker().clearCache();
248         lookedUp = (DSet) database.lookup(name);
249         tx.commit();
250
251         assertEquals(3, lookedUp.size());
252     }
253
254
255     public void testAdding() throws Exception JavaDoc
256     {
257         // create a unique name:
258
String JavaDoc name = "testAdding_" + System.currentTimeMillis();
259
260         TransactionExt tx = (TransactionExt) odmg.newTransaction();
261         tx.begin();
262         DSet list = odmg.newDSet();
263         database.bind(list, name);
264         tx.commit();
265
266         tx = (TransactionExt) odmg.newTransaction();
267         tx.begin();
268         for (int i = 0; i < 5; i++)
269         {
270             DListTest.DObject a = createObject(name);
271             list.add(a);
272         }
273
274         list.add(createObject(name+"_posNew1"));
275         list.add(createObject(name+"_posNew2"));
276         list.add(createObject(name+"_posNew3"));
277         tx.commit();
278
279         tx.begin();
280         tx.getBroker().clearCache();
281         // System.out.println("list: " + list);
282
// System.out.println("lookup list: " + db.lookup(name));
283
tx.commit();
284
285         //System.out.println("sequence of items in list:");
286
Iterator JavaDoc iter = list.iterator();
287         DListTest.DObject a;
288         while (iter.hasNext())
289         {
290             a = (DListTest.DObject) iter.next();
291             assertNotNull(a);
292             //System.out.print(a.getArticleId() + ", ");
293
}
294         assertEquals(8, list.size());
295
296
297         tx = (TransactionExt) odmg.newTransaction();
298         tx.begin();
299         tx.getBroker().clearCache();
300         DSet lookedUp = (DSet) database.lookup(name);
301         // System.out.println("lookup list: " + lookedUp);
302
assertNotNull("database lookup does not find DSet", lookedUp);
303         assertEquals(8, lookedUp.size());
304         iter = lookedUp.iterator();
305         while (iter.hasNext())
306         {
307             a = (DListTest.DObject) iter.next();
308         }
309         tx.commit();
310     }
311
312     public void testDSet() throws Exception JavaDoc
313     {
314         String JavaDoc name = "testDSet_" + System.currentTimeMillis();
315         String JavaDoc set_1 = "set_1_" + System.currentTimeMillis();
316         String JavaDoc set_2 = "set_2_" + System.currentTimeMillis();
317
318         Transaction tx = odmg.newTransaction();
319         tx.begin();
320
321         DListTest.DObject a, b, c, d, e;
322         a = createObject(name);
323         b = createObject(name);
324         c = createObject(name);
325         d = createObject(name);
326         e = createObject(name);
327
328         DSet set1 = odmg.newDSet();
329         DSet set2 = odmg.newDSet();
330
331         set1.add(a);
332         set1.add(b);
333         set1.add(c);
334
335         set2.add(b);
336         set2.add(c);
337         set2.add(d);
338         set2.add(e);
339
340         database.bind(set1, set_1);
341         database.bind(set2, set_2);
342         tx.commit();
343
344         // low lookup both sets
345
tx = odmg.newTransaction();
346         tx.begin();
347         ((HasBroker) tx).getBroker().clearCache();
348         DSet set1a = (DSet) database.lookup(set_1);
349         DSet set2a = (DSet) database.lookup(set_2);
350
351         // check looked up sets
352
assertTrue(set1a.containsAll(set1));
353         assertTrue(set2a.containsAll(set2));
354
355         // now TestThreadsNLocks set operations:
356
DSet set3 = set1.difference(set2);
357         assertEquals(1, set3.size());
358
359         set3 = set1.intersection(set2);
360         assertEquals(2, set3.size());
361
362         set3 = set1.union(set2);
363         assertEquals(5, set3.size());
364
365         assertTrue(set1.properSubsetOf(set3));
366         assertTrue(set2.properSubsetOf(set3));
367
368         assertTrue(set3.properSupersetOf(set1));
369         assertTrue(set3.properSupersetOf(set2));
370
371         assertTrue(!set1.properSubsetOf(set2));
372
373         tx.commit();
374     }
375 }
376
Popular Tags