KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * BatchModeTest.java
3  * JUnit based test
4  *
5  * Created on February 15, 2003, 12:47 AM
6  */

7
8 package org.apache.ojb.broker;
9
10 import java.util.ArrayList JavaDoc;
11 import java.util.Collection JavaDoc;
12 import java.util.Iterator JavaDoc;
13
14 import org.apache.ojb.broker.accesslayer.ConnectionManagerIF;
15 import org.apache.ojb.broker.query.Criteria;
16 import org.apache.ojb.broker.query.Query;
17 import org.apache.ojb.broker.query.QueryFactory;
18 import org.apache.ojb.junit.PBTestCase;
19
20 /**
21  * @author Oleg Nitz
22  */

23 public class BatchModeTest extends PBTestCase
24 {
25     private static long timestamp = System.currentTimeMillis();
26
27     public BatchModeTest(String JavaDoc testName)
28     {
29         super(testName);
30     }
31
32     private long getNewId()
33     {
34         return timestamp++;
35     }
36
37     public static void main(String JavaDoc[] args)
38     {
39         String JavaDoc[] arr = {BatchModeTest.class.getName()};
40         junit.textui.TestRunner.main(arr);
41     }
42
43     public void setUp() throws Exception JavaDoc
44     {
45         super.setUp();
46         broker.serviceConnectionManager().setBatchMode(true);
47         // lookup connection to enable batch mode
48
broker.serviceConnectionManager().getConnection();
49     }
50
51     boolean batchModeDisabled()
52     {
53         if(broker.serviceConnectionManager().isBatchMode())
54         {
55             return false;
56         }
57         else
58         {
59             System.out.println("[" + BatchModeTest.class.getName() + "] Skip test, batch mode was not enabled or supported");
60             return true;
61         }
62     }
63
64
65     /**
66      * A common (no specific batch) test
67      */

68     public void testDelete()
69     {
70         if(batchModeDisabled()) return;
71
72         long pk = getNewId();
73         String JavaDoc nameMain = "testDelete_Main_" + pk;
74         String JavaDoc nameSub = "testDelete_Sub_" + pk;
75
76         MainObject main1 = new MainObject(pk, nameMain);
77         SubObject sub = new SubObject(nameSub);
78         main1.add(sub);
79         main1.add(new SubObject(nameSub));
80
81         broker.beginTransaction();
82         broker.store(main1);
83         Identity oid_main = broker.serviceIdentity().buildIdentity(main1);
84         Identity oid_sub = broker.serviceIdentity().buildIdentity(sub);
85         broker.delete(main1);
86         broker.commitTransaction();
87
88
89         MainObject newMain = (MainObject) broker.getObjectByIdentity(oid_main);
90         assertNull(newMain);
91         SubObject newSub = (SubObject) broker.getObjectByIdentity(oid_sub);
92         assertNull(newSub);
93
94         Criteria crit = new Criteria();
95         crit.addLike("name", nameSub);
96         Query q = QueryFactory.newQuery(SubObject.class, crit);
97         Collection JavaDoc result = broker.getCollectionByQuery(q);
98         assertEquals(0, result.size());
99     }
100
101     /**
102      * A common (no specific batch) test
103      */

104     public void testEquals() throws Exception JavaDoc
105     {
106         if(batchModeDisabled()) return;
107
108         long pk = getNewId();
109         String JavaDoc nameMain = "testEquals_Main_" + pk;
110         String JavaDoc nameSub = "testEquals_Sub_" + pk;
111
112         MainObject main1 = new MainObject(pk, nameMain);
113         main1.add(new SubObject(nameSub));
114         MainObject main2 = new MainObject(pk, nameMain);
115         main2.add(new SubObject(nameSub));
116
117         broker.beginTransaction();
118         broker.store(main1);
119         // delete object before add new instance with same PK
120
broker.delete(main1);
121         broker.store(main2);
122         broker.commitTransaction();
123
124         // new PB instance
125
super.tearDown();
126         super.setUp();
127
128         MainObject main3 = new MainObject(pk, nameMain);
129         main3.add(new SubObject(nameSub));
130
131         Identity oid = broker.serviceIdentity().buildIdentity(main1);
132         broker.clearCache();
133         MainObject main4 = (MainObject) broker.getObjectByIdentity(oid);
134
135         assertEquals(main3, main4);
136     }
137
138     public void testDeleteInsert()
139     {
140         if(batchModeDisabled()) return;
141
142         long pk = getNewId();
143         String JavaDoc nameMain = "testDeleteInsert_Main_" + pk;
144         String JavaDoc nameSub = "testDeleteInsert_Sub_" + pk;
145
146         MainObject main1 = new MainObject(pk, nameMain);
147         main1.add(new SubObject("normal_" + nameSub));
148         broker.beginTransaction();
149         broker.store(main1);
150         broker.commitTransaction();
151
152         // enable batch mode before start tx
153
broker.serviceConnectionManager().setBatchMode(true);
154         Identity oid = broker.serviceIdentity().buildIdentity(main1);
155         broker.beginTransaction();
156         broker.delete(main1);
157         MainObject main2 = new MainObject(pk, nameMain);
158         main2.add(new SubObject("updated_" + nameSub));
159         broker.store(main2);
160
161         broker.commitTransaction();
162
163         broker.clearCache();
164         MainObject newMain = (MainObject) broker.getObjectByIdentity(oid);
165         assertNotNull(newMain);
166         assertNotNull(newMain.getSubObjects());
167         assertEquals(1, newMain.getSubObjects().size());
168     }
169
170     public void testBatchStatementsOrder()
171     {
172         if(batchModeDisabled()) return;
173
174         String JavaDoc name = "testBatchStatementsOrder_" + System.currentTimeMillis();
175         ConnectionManagerIF conMan = broker.serviceConnectionManager();
176         // try to enable batch mode
177
conMan.setBatchMode(true);
178         broker.beginTransaction();
179
180         ProductGroup pg1 = new ProductGroup();
181         pg1.setName("ProductGroup#1_" + name);
182         broker.store(pg1);
183
184         conMan.executeBatch();
185
186         Article a1 = new Article();
187         a1.setArticleName(name);
188         a1.setProductGroup(pg1);
189         pg1.add(a1);
190         broker.store(pg1);
191         broker.store(a1);
192
193         ProductGroup pg2 = new ProductGroup();
194         pg2.setName("ProductGroup #2_" + name);
195         broker.store(pg2);
196
197         Article a2 = new Article();
198         a2.setArticleName(name);
199         a2.setProductGroup(pg2);
200         pg2.add(a2);
201         broker.store(a2);
202
203         ProductGroup pg3 = new ProductGroup();
204         pg3.setName("ProductGroup #3_" + name);
205         broker.store(pg3);
206
207         Article a3 = new Article();
208         a3.setArticleName(name);
209         a3.setProductGroup(pg3);
210         pg3.add(a3);
211         broker.store(a3);
212
213         conMan.executeBatch();
214
215         broker.delete(a1);
216
217         conMan.executeBatch();
218
219         broker.delete(pg1);
220         broker.delete(a2);
221         broker.delete(pg2);
222         broker.delete(a3);
223         broker.delete(pg3);
224         broker.commitTransaction();
225
226
227         broker.beginTransaction();
228         pg3.getAllArticles().clear();
229         broker.store(pg3);
230         broker.delete(pg3);
231         broker.store(pg3);
232         broker.delete(pg3);
233         conMan.executeBatch();
234         broker.commitTransaction();
235     }
236
237     /**
238      * collection-descriptor without inverse reference-descriptor
239      */

240     public void testBatchStatementsOrder2()
241     {
242         if(batchModeDisabled()) return;
243
244         ConnectionManagerIF conMan = broker.serviceConnectionManager();
245         broker.beginTransaction();
246
247         Zoo zoo1 = new Zoo();
248         zoo1.setName("BatchModeTest Zoo #1");
249         broker.store(zoo1);
250
251         conMan.executeBatch();
252
253         Mammal m1 = new Mammal();
254         m1.setName("BatchModeTest Mammal #1");
255         m1.setAge(5);
256         m1.setNumLegs(4);
257         m1.setZooId(zoo1.getZooId());
258         zoo1.getAnimals().add(m1);
259         broker.store(m1);
260
261         Zoo zoo2 = new Zoo();
262         zoo2.setName("BatchModeTest Zoo #2");
263         broker.store(zoo2);
264
265         Mammal m2 = new Mammal();
266         m2.setName("BatchModeTest Mammal #2");
267         m2.setAge(5);
268         m2.setNumLegs(4);
269         m2.setZooId(zoo2.getZooId());
270         zoo2.getAnimals().add(m2);
271         broker.store(m2);
272
273         Zoo zoo3 = new Zoo();
274         zoo3.setName("BatchModeTest Zoo #3");
275         broker.store(zoo3);
276
277         Mammal m3 = new Mammal();
278         m3.setName("BatchModeTest Mammal #3");
279         m3.setAge(5);
280         m3.setNumLegs(4);
281         m3.setZooId(zoo3.getZooId());
282         zoo3.getAnimals().add(m3);
283         broker.store(m3);
284
285         conMan.executeBatch();
286
287         broker.delete(m1);
288
289         conMan.executeBatch();
290
291         broker.delete(zoo1);
292         broker.delete(m2);
293         broker.delete(zoo2);
294         broker.delete(m3);
295         broker.delete(zoo3);
296
297         conMan.executeBatch();
298         broker.commitTransaction();
299     }
300
301     public void testMassInsertDelete()
302     {
303         if(batchModeDisabled()) return;
304
305         String JavaDoc name = "testMassInsert_" + System.currentTimeMillis();
306
307         broker.serviceConnectionManager().setBatchMode(true);
308         broker.beginTransaction();
309         for(int i = 200 - 1; i >= 0; i--)
310         {
311             Person p = new Person();
312             p.setFirstname("a mass test_" + i);
313             p.setLastname(name);
314             broker.store(p);
315         }
316         broker.commitTransaction();
317
318         Criteria crit = new Criteria();
319         crit.addLike("lastname", name);
320         Query q = QueryFactory.newQuery(Person.class, crit);
321         Collection JavaDoc result = broker.getCollectionByQuery(q);
322         assertEquals(200, result.size());
323
324         broker.beginTransaction();
325         for(Iterator JavaDoc iterator = result.iterator(); iterator.hasNext();)
326         {
327             broker.delete(iterator.next());
328         }
329         broker.commitTransaction();
330
331         crit = new Criteria();
332         crit.addLike("lastname", name);
333         q = QueryFactory.newQuery(Person.class, crit);
334         result = broker.getCollectionByQuery(q);
335         assertEquals(0, result.size());
336     }
337
338     public void testBatchModeDeclaration() throws Exception JavaDoc
339     {
340         if(batchModeDisabled()) return;
341
342         String JavaDoc name = "testBatchModeDeclaration_" + System.currentTimeMillis();
343
344         broker.serviceConnectionManager().setBatchMode(true);
345         broker.beginTransaction();
346         Person p = new Person();
347         p.setFirstname("a mass test");
348         p.setLastname(name);
349         broker.store(p);
350         broker.commitTransaction();
351
352         // new PB instance
353
tearDown();
354         setUp();
355
356         broker.beginTransaction();
357         broker.serviceConnectionManager().setBatchMode(true);
358         p = new Person();
359         p.setFirstname("a mass test");
360         p.setLastname(name);
361         broker.store(p);
362         broker.commitTransaction();
363
364         // new PB instance
365
tearDown();
366         setUp();
367         broker.serviceConnectionManager().setBatchMode(true);
368         broker.serviceConnectionManager().getConnection();
369         broker.beginTransaction();
370         broker.commitTransaction();
371
372         // new PB instance
373
tearDown();
374         setUp();
375         broker.serviceConnectionManager().setBatchMode(true);
376         broker.serviceConnectionManager().getConnection();
377         broker.beginTransaction();
378         broker.abortTransaction();
379
380         // new PB instance
381
tearDown();
382         setUp();
383         broker.beginTransaction();
384         broker.serviceConnectionManager().setBatchMode(true);
385         broker.serviceConnectionManager().getConnection();
386         broker.commitTransaction();
387
388         // new PB instance
389
tearDown();
390         setUp();
391         broker.beginTransaction();
392         broker.serviceConnectionManager().setBatchMode(true);
393         broker.serviceConnectionManager().getConnection();
394         broker.abortTransaction();
395     }
396
397     /**
398      * A common (no specific batch) test
399      */

400     public void testStoreDeleteStore()
401     {
402         if(batchModeDisabled()) return;
403
404         long pk = getNewId();
405         String JavaDoc nameMain = "testDelete_Main_" + pk;
406         String JavaDoc nameSub = "testDelete_Sub_" + pk;
407
408         MainObject main1 = new MainObject(pk, nameMain);
409         main1.add(new SubObject(nameSub));
410         main1.add(new SubObject(nameSub));
411
412         broker.beginTransaction();
413         broker.store(main1);
414         broker.delete(main1);
415         broker.store(main1);
416         broker.delete(main1);
417         broker.store(main1);
418         broker.commitTransaction();
419
420         Identity oid = broker.serviceIdentity().buildIdentity(main1);
421         broker.clearCache();
422         MainObject newMain = (MainObject) broker.getObjectByIdentity(oid);
423         assertNotNull(newMain);
424         assertEquals(2, newMain.getSubObjects().size());
425         broker.clearCache();
426         Criteria crit = new Criteria();
427         crit.addLike("name", nameSub);
428         Query q = QueryFactory.newQuery(SubObject.class, crit);
429         Collection JavaDoc result = broker.getCollectionByQuery(q);
430         assertEquals(2, result.size());
431     }
432
433
434     //==========================================================
435
// inner classes used for testing
436
//==========================================================
437
public static class MainObject
438     {
439         private Long JavaDoc id;
440         private String JavaDoc name;
441         private Collection JavaDoc subObjects;
442
443         public MainObject()
444         {
445         }
446
447         public MainObject(long id, String JavaDoc name)
448         {
449             setId(new Long JavaDoc(id));
450             setName(name);
451         }
452
453         public Long JavaDoc getId()
454         {
455             return id;
456         }
457
458         public void setId(Long JavaDoc id)
459         {
460             this.id = id;
461         }
462
463         public String JavaDoc getName()
464         {
465             return name;
466         }
467
468         public void setName(String JavaDoc name)
469         {
470             this.name = name;
471         }
472
473         public Collection JavaDoc getSubObjects()
474         {
475             return subObjects;
476         }
477
478         public void setSubObjects(Collection JavaDoc subObjects)
479         {
480             this.subObjects = subObjects;
481         }
482
483         public void add(SubObject obj)
484         {
485             if(subObjects == null)
486             {
487                 subObjects = new ArrayList JavaDoc();
488             }
489             subObjects.add(obj);
490         }
491
492         public boolean equals(Object JavaDoc other)
493         {
494             if(other instanceof MainObject)
495             {
496                 MainObject main = (MainObject) other;
497                 return ((name == null) ? main.name == null : name.equals(main.name))
498                         && ((subObjects == null || subObjects.isEmpty())
499                         ? (main.subObjects == null || main.subObjects.isEmpty())
500                         : subObjects.equals(main.subObjects));
501             }
502             else
503             {
504                 return false;
505             }
506         }
507     }
508
509     public static class SubObject
510     {
511         private Long JavaDoc id;
512         private String JavaDoc name;
513         private Long JavaDoc mainId;
514
515         public SubObject()
516         {
517         }
518
519         public SubObject(String JavaDoc name)
520         {
521             setName(name);
522         }
523
524         public Long JavaDoc getId()
525         {
526             return id;
527         }
528
529         public void setId(Long JavaDoc id)
530         {
531             this.id = id;
532         }
533
534         public Long JavaDoc getMainId()
535         {
536             return mainId;
537         }
538
539         public void setMainId(Long JavaDoc mainId)
540         {
541             this.mainId = mainId;
542         }
543
544         public String JavaDoc getName()
545         {
546             return name;
547         }
548
549         public void setName(String JavaDoc name)
550         {
551             this.name = name;
552         }
553
554         public boolean equals(Object JavaDoc other)
555         {
556             if(other instanceof SubObject)
557             {
558                 SubObject sub = (SubObject) other;
559                 return (name == null) ? sub.name == null : name.equals(sub.name);
560             }
561             else
562             {
563                 return false;
564             }
565         }
566
567     }
568 }
569
Popular Tags