KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.util.List JavaDoc;
7 import java.util.Collections JavaDoc;
8 import java.util.Comparator JavaDoc;
9
10 import org.apache.commons.lang.builder.ToStringBuilder;
11 import org.apache.commons.lang.builder.HashCodeBuilder;
12 import org.apache.commons.lang.builder.EqualsBuilder;
13 import org.apache.ojb.junit.ODMGTestCase;
14 import org.odmg.DBag;
15 import org.odmg.DList;
16 import org.odmg.OQLQuery;
17 import org.odmg.Transaction;
18
19 /**
20  * Tests for OJB {@link org.odmg.DList} implementation.
21  */

22 public class DListTest extends ODMGTestCase
23 {
24     public static void main(String JavaDoc[] args)
25     {
26         String JavaDoc[] arr = {DListTest.class.getName()};
27         junit.textui.TestRunner.main(arr);
28     }
29
30     public DListTest(String JavaDoc name)
31
32     {
33         super(name);
34     }
35
36     protected DObject createObject(String JavaDoc name) throws Exception JavaDoc
37     {
38         DObject obj = new DObject();
39         obj.setName(name);
40         obj.setRandomName("rnd_"+((int)(Math.random()*1000)));
41
42         return obj;
43     }
44
45     public void testAddingLockupWithTx() throws Exception JavaDoc
46     {
47         // create a unique name:
48
final String JavaDoc name = "testAdding_" + System.currentTimeMillis();
49
50         TransactionExt tx = (TransactionExt) odmg.newTransaction();
51         tx.begin();
52         // create DList and bound by name
53
DList list = odmg.newDList();
54         database.bind(list, name);
55         tx.commit();
56
57         tx.begin();
58         tx.getBroker().clearCache();
59         Object JavaDoc obj = database.lookup(name);
60         tx.commit();
61         assertNotNull("binded DList not found", obj);
62
63         tx.begin();
64         // add objects to list
65
for (int i = 0; i < 5; i++)
66         {
67             DObject a = createObject(name);
68             list.add(a);
69         }
70         tx.commit();
71
72         // check current list
73
Iterator JavaDoc iter = list.iterator();
74         while (iter.hasNext())
75         {
76             DObject a = (DObject) iter.next();
77             assertNotNull(a);
78         }
79
80         tx.begin();
81         tx.getBroker().clearCache();
82
83         // lookup list and check entries
84
DList lookedUp = (DList) database.lookup(name);
85         assertNotNull("binded DList not found", lookedUp);
86
87         //System.out.println("sequence of items in lookedup list:");
88
iter = lookedUp.iterator();
89         Iterator JavaDoc iter1 = list.iterator();
90         while (iter.hasNext())
91         {
92             DObject a = (DObject) iter.next();
93             DObject b = (DObject) iter1.next();
94             assertNotNull(a);
95             assertNotNull(b);
96             assertEquals(a.getId(), b.getId());
97         }
98         tx.commit();
99
100         // add new entries to list
101
tx.begin();
102         for (int i = 0; i < 3; i++)
103         {
104             DObject a = createObject(name + "_new_entry");
105             list.add(a);
106         }
107         tx.commit();
108
109         tx.begin();
110         tx.getBroker().clearCache();
111         lookedUp = (DList) database.lookup(name);
112         iter = lookedUp.iterator();
113         iter1 = list.iterator();
114         assertEquals("Wrong number of DListEntry found", 8, list.size());
115         while (iter.hasNext())
116         {
117             DObject a = (DObject) iter.next();
118             DObject b = (DObject) iter1.next();
119             assertNotNull(a);
120             assertNotNull(b);
121             assertEquals(a.getId(), b.getId());
122         }
123         tx.commit();
124         assertNotNull("binded DList not found", lookedUp);
125     }
126
127     public void testRemoveAdd() throws Exception JavaDoc
128     {
129         // create a unique name:
130
final String JavaDoc name = "testRemoveAdd_" + System.currentTimeMillis();
131
132         TransactionExt tx = (TransactionExt) odmg.newTransaction();
133         tx.begin();
134         // create DList and bound by name
135
DList list = odmg.newDList();
136         database.bind(list, name);
137
138         // add object to list
139
for (int i = 0; i < 5; i++)
140         {
141             DObject a = createObject(name);
142             list.add(a);
143         }
144         tx.commit();
145
146         // check current list
147
Iterator JavaDoc iter = list.iterator();
148         while (iter.hasNext())
149         {
150             DObject a = (DObject) iter.next();
151             assertNotNull(a);
152         }
153
154         tx.begin();
155         tx.getBroker().clearCache();
156
157         // lookup list and check entries
158
DList lookedUp = (DList) database.lookup(name);
159         assertNotNull("binded DList not found", lookedUp);
160
161         //System.out.println("sequence of items in lookedup list:");
162
iter = lookedUp.iterator();
163         Iterator JavaDoc iter1 = list.iterator();
164         while (iter.hasNext())
165         {
166             DObject a = (DObject) iter.next();
167             DObject b = (DObject) iter1.next();
168             assertNotNull(a);
169             assertNotNull(b);
170             assertEquals(a.getId(), b.getId());
171         }
172         tx.commit();
173
174         // add and remove new entries
175
tx.begin();
176         for (int i = 0; i < 3; i++)
177         {
178             DObject a = createObject(name + "_new_entry_NOT_PERSIST");
179             list.add(a);
180             list.remove(list.size()-1);
181         }
182         tx.commit();
183
184
185         tx.begin();
186         tx.getBroker().clearCache();
187         lookedUp = (DList) database.lookup(name);
188         iter = lookedUp.iterator();
189         iter1 = list.iterator();
190         assertEquals("Wrong number of DListEntry found", 5, list.size());
191         while (iter.hasNext())
192         {
193             DObject a = (DObject) iter.next();
194             DObject b = (DObject) iter1.next();
195             assertNotNull(a);
196             assertNotNull(b);
197             assertEquals(a.getId(), b.getId());
198         }
199         tx.commit();
200         assertNotNull("binded DList not found", lookedUp);
201
202
203         tx.begin();
204         for (int i = 0; i < 3; i++)
205         {
206             DObject a = createObject(name + "_new_entry_new_persist");
207             list.add(a);
208             list.remove(0);
209         }
210         tx.commit();
211
212         tx.begin();
213         tx.getBroker().clearCache();
214         lookedUp = (DList) database.lookup(name);
215         iter = lookedUp.iterator();
216         iter1 = list.iterator();
217         assertEquals("Wrong number of DListEntry found", 5, list.size());
218         while (iter.hasNext())
219         {
220             DObject a = (DObject) iter.next();
221             DObject b = (DObject) iter1.next();
222             assertNotNull(a);
223             assertNotNull(b);
224             assertEquals(a.getId(), b.getId());
225         }
226         tx.commit();
227         assertNotNull("binded DList not found", lookedUp);
228     }
229
230     public void testReadAndStore() throws Exception JavaDoc
231     {
232         // create a unique name:
233
final String JavaDoc name = "testReadAndStore_" + System.currentTimeMillis();
234
235         // create test objects
236
Transaction tx = odmg.newTransaction();
237         tx.begin();
238         // add objects to list
239
for (int i = 0; i < 5; i++)
240         {
241             tx.lock(createObject(name), Transaction.WRITE);
242         }
243         tx.commit();
244
245         tx.begin();
246         // query test objects
247
OQLQuery q = odmg.newOQLQuery();
248         q.create("select all from "+DObject.class.getName()+" where name=$1");
249         q.bind(name);
250         Collection JavaDoc ret = (Collection JavaDoc) q.execute();
251         // check result list size
252
assertEquals(5, ret.size());
253         // do read lock
254
for (Iterator JavaDoc it = ret.iterator(); it.hasNext(); )
255         {
256             tx.lock(it.next(), Transaction.READ);
257         }
258         // create new list for results
259
ArrayList JavaDoc result = new ArrayList JavaDoc();
260         result.addAll(ret);
261         tx.commit();
262     }
263
264     public void testIterateWithoutTx() throws Exception JavaDoc
265     {
266         // create a unique name:
267
final String JavaDoc name = "testAdding_" + System.currentTimeMillis();
268
269         // get DList and fill with objects
270
List JavaDoc list = odmg.newDList();
271         TransactionExt tx = (TransactionExt) odmg.newTransaction();
272         tx.begin();
273         for (int i = 0; i < 5; i++)
274         {
275             DObject a = createObject(name);
276             list.add(a);
277         }
278         // bind the new list
279
database.bind(list, name);
280         tx.commit();
281
282         tx = (TransactionExt) odmg.newTransaction();
283         tx.begin();
284         Object JavaDoc obj = database.lookup(name);
285         tx.commit();
286         assertNotNull("binded DList not found", obj);
287
288         // iterate list
289
Iterator JavaDoc iter = list.iterator();
290         while (iter.hasNext())
291         {
292             DObject a = (DObject) iter.next();
293             assertNotNull(a);
294         }
295         assertEquals(5, list.size());
296
297         tx = (TransactionExt) odmg.newTransaction();
298         tx.begin();
299         tx.getBroker().clearCache();
300
301         List JavaDoc lookedUp = (List JavaDoc) database.lookup(name);
302         tx.commit();
303         assertNotNull("binded DList not found", lookedUp);
304
305         // DList doesn't support #set(...) method, so
306
list = new ArrayList JavaDoc(list);
307         lookedUp = new ArrayList JavaDoc(lookedUp);
308
309         Collections.sort(list, new Comparator JavaDoc(){
310             public int compare(Object JavaDoc o1, Object JavaDoc o2)
311             {
312                 DObject d1 = (DObject) o1;
313                 DObject d2 = (DObject) o2;
314                 return d1.getId().compareTo(d2.getId());
315             }
316         });
317
318         Collections.sort(lookedUp, new Comparator JavaDoc(){
319             public int compare(Object JavaDoc o1, Object JavaDoc o2)
320             {
321                 DObject d1 = (DObject) o1;
322                 DObject d2 = (DObject) o2;
323                 return d1.getId().compareTo(d2.getId());
324             }
325         });
326
327         assertEquals(list.size(), lookedUp.size());
328
329         for(int i = 0; i < lookedUp.size(); i++)
330         {
331             DObject a = (DObject) lookedUp.get(i);
332             DObject aa = (DObject) list.get(i);
333             assertNotNull(a);
334             assertNotNull(aa);
335             assertEquals(a.getId(), aa.getId());
336         }
337     }
338
339     /**
340      * this test checks if removing item from DList works
341      */

342     public void testRemoving() throws Exception JavaDoc
343     {
344         // create a unique name:
345
String JavaDoc name = "testRemoving_" + System.currentTimeMillis();
346
347         Transaction tx = odmg.newTransaction();
348         tx.begin();
349         DList list = odmg.newDList();
350         // bind the list to the name:
351
database.bind(list, name);
352
353         for (int i = 0; i < 5; i++)
354         {
355             DObject a = createObject(name);
356             list.add(a);
357         }
358         assertEquals(5, list.size());
359         tx.commit();
360
361         // delete two items
362
tx = odmg.newTransaction();
363         tx.begin();
364         ((HasBroker) odmg.currentTransaction()).getBroker().clearCache();
365         DList lookedUp = (DList) database.lookup(name);
366         assertNotNull("database lookup does not find the named DList", lookedUp);
367         assertEquals("Wrong number of list entries", 5, lookedUp.size());
368         lookedUp.remove(2);
369         lookedUp.remove(1);
370         tx.commit();
371
372         // check if deletion was successful
373
tx = odmg.newTransaction();
374         tx.begin();
375         ((HasBroker) odmg.currentTransaction()).getBroker().clearCache();
376         lookedUp = (DList) database.lookup(name);
377         tx.commit();
378
379         assertEquals(3, lookedUp.size());
380     }
381
382
383     public void testAddingWithIndex() throws Exception JavaDoc
384     {
385         // create a unique name:
386
String JavaDoc name = "testAddingWithIndex_" + System.currentTimeMillis();
387
388         Transaction tx = odmg.newTransaction();
389         tx.begin();
390         DList list = odmg.newDList();
391         database.bind(list, name);
392         tx.commit();
393
394         tx = odmg.newTransaction();
395         tx.begin();
396         for (int i = 0; i < 5; i++)
397         {
398             DObject a = createObject(name);
399             list.add(a);
400         }
401
402         list.add(2, createObject(name+"_pos2"));
403         list.add(0, createObject(name+"_pos0"));
404         list.add(7, createObject(name+"_pos7"));
405         tx.commit();
406
407         tx.begin();
408         ((TransactionImpl) tx).getBroker().clearCache();
409         // System.out.println("list: " + list);
410
// System.out.println("lookup list: " + db.lookup(name));
411
tx.commit();
412
413         //System.out.println("sequence of items in list:");
414
Iterator JavaDoc iter = list.iterator();
415         DObject a;
416         while (iter.hasNext())
417         {
418             a = (DObject) iter.next();
419             assertNotNull(a);
420             //System.out.print(a.getArticleId() + ", ");
421
}
422
423
424         tx = odmg.newTransaction();
425         tx.begin();
426         ((TransactionImpl) tx).getBroker().clearCache();
427         DList lookedUp = (DList) database.lookup(name);
428         // System.out.println("lookup list: " + lookedUp);
429
assertNotNull("database lookup does not find DList", lookedUp);
430         assertEquals(8, lookedUp.size());
431         iter = lookedUp.iterator();
432         while (iter.hasNext())
433         {
434             a = (DObject) iter.next();
435         }
436         tx.commit();
437     }
438
439     public void testDBag() throws Exception JavaDoc
440     {
441         String JavaDoc name = "testDBag_" + System.currentTimeMillis();
442
443         Transaction tx = odmg.newTransaction();
444         tx.begin();
445         DBag bag1 = odmg.newDBag();
446         DBag bag2 = odmg.newDBag();
447         DObject a, b, c, d, e;
448         a = createObject(name);
449         b = createObject(name);
450         c = createObject(name);
451         d = createObject(name);
452         e = createObject(name);
453         bag1.add(a);
454         bag1.add(b);
455         bag1.add(c);
456         bag2.add(b);
457         bag2.add(c);
458         bag2.add(d);
459         bag2.add(e);
460         DBag bag3 = bag1.difference(bag2);
461         assertEquals("should contain only 1 element", 1, bag3.size());
462
463         bag3 = bag1.intersection(bag2);
464         assertEquals("should contain two elements", 2, bag3.size());
465
466         tx.commit();
467     }
468
469     public static class DObject
470     {
471         Integer JavaDoc id;
472         String JavaDoc name;
473         String JavaDoc randomName;
474
475         public DObject()
476         {
477         }
478
479         public boolean equals(Object JavaDoc obj)
480         {
481             if(obj instanceof DObject)
482             {
483                 DObject target = ((DObject)obj);
484                 return new EqualsBuilder()
485                         .append(id, target.getId())
486                         .append(name, target.getName())
487                         .append(randomName, target.getRandomName())
488                         .isEquals();
489             }
490             else
491             {
492                 return false;
493             }
494         }
495
496         public int hashCode()
497         {
498             return new HashCodeBuilder().append(id).append(name).append(randomName).hashCode();
499         }
500
501         public String JavaDoc toString()
502         {
503             ToStringBuilder buf = new ToStringBuilder(this);
504             buf.append("id", id);
505             buf.append("name", name);
506             buf.append("randonName", randomName);
507             return buf.toString();
508         }
509
510         public Integer JavaDoc getId()
511         {
512             return id;
513         }
514
515         public void setId(Integer JavaDoc id)
516         {
517             this.id = id;
518         }
519
520         public String JavaDoc getName()
521         {
522             return name;
523         }
524
525         public void setName(String JavaDoc name)
526         {
527             this.name = name;
528         }
529
530         public String JavaDoc getRandomName()
531         {
532             return randomName;
533         }
534
535         public void setRandomName(String JavaDoc randomName)
536         {
537             this.randomName = randomName;
538         }
539     }
540 }
541
Popular Tags