KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.apache.ojb.broker;
2
3 import org.apache.commons.lang.builder.ToStringBuilder;
4 import org.apache.ojb.junit.PBTestCase;
5 import org.apache.ojb.broker.query.Criteria;
6 import org.apache.ojb.broker.query.QueryByCriteria;
7 import org.apache.ojb.broker.query.QueryFactory;
8
9 import java.io.Serializable JavaDoc;
10 import java.util.List JavaDoc;
11 import java.util.ArrayList JavaDoc;
12 import java.util.Collection JavaDoc;
13 import java.util.Iterator JavaDoc;
14
15 /**
16  * This TestClass tests OJB facilities to work with persistence
17  * aware instances.
18  */

19 public class PBListenerTest extends PBTestCase
20 {
21     public static void main(String JavaDoc[] args)
22     {
23         String JavaDoc[] arr = {PBListenerTest.class.getName()};
24         junit.textui.TestRunner.main(arr);
25     }
26
27     public PBListenerTest(String JavaDoc name)
28     {
29         super(name);
30     }
31
32     /**
33      * Test for OJB-68
34      * tests the callbacks beforeStore() and afterStore().
35      */

36     public void testStoreAndQuery() throws Exception JavaDoc
37     {
38         String JavaDoc name = "testStoreAndQuery_" + System.currentTimeMillis();
39
40         PBAwareObject pba_1 = new PBAwareObject(name);
41         pba_1.setRefObject(new RefObj(name));
42         pba_1.addCollObject(new CollObj(name));
43         pba_1.addCollObject(new CollObj(name));
44         pba_1.addCollObject(new CollObj(name));
45
46         PBAwareObject pba_2 = new PBAwareObject(name);
47         pba_2.setRefObject(new RefObj(name));
48         pba_2.addCollObject(new CollObj(name));
49         pba_2.addCollObject(new CollObj(name));
50         pba_2.addCollObject(new CollObj(name));
51
52         PBAwareObject pba_3 = new PBAwareObject(name);
53         pba_3.setRefObject(new RefObj(name));
54         pba_3.addCollObject(new CollObj(name));
55         pba_3.addCollObject(new CollObj(name));
56         pba_3.addCollObject(new CollObj(name));
57
58         broker.beginTransaction();
59         broker.store(pba_1);
60         broker.store(pba_2);
61         broker.store(pba_3);
62         broker.commitTransaction();
63
64         Identity oid_1 = broker.serviceIdentity().buildIdentity(pba_1);
65         Identity oid_2 = broker.serviceIdentity().buildIdentity(pba_2);
66
67         broker.clearCache();
68
69         PBAwareObject pba_1_new = (PBAwareObject) broker.getObjectByIdentity(oid_1);
70         PBAwareObject pba_2_new = (PBAwareObject) broker.getObjectByIdentity(oid_2);
71
72         assertNotNull(pba_1_new);
73         assertNotNull(pba_2_new);
74         assertNotNull(pba_1_new.getRefObject());
75         assertNotNull(pba_2_new.getRefObject());
76         assertEquals(3, pba_1_new.getCollObjects().size());
77         assertEquals(3, pba_2_new.getCollObjects().size());
78         assertTrue(pba_1_new.isAfterLookupRefObjectPopulated());
79         assertTrue(pba_1_new.isAfterLookupCollObjectsPopulated());
80         assertTrue(pba_2_new.isAfterLookupRefObjectPopulated());
81         assertTrue(pba_2_new.isAfterLookupCollObjectsPopulated());
82
83         broker.clearCache();
84
85         Criteria criteria = new Criteria();
86         criteria.addEqualTo( "name", name);
87         QueryByCriteria query = QueryFactory.newQuery(PBAwareObject.class, criteria);
88         Collection JavaDoc result = broker.getCollectionByQuery(query);
89         assertEquals(3, result.size());
90         for(Iterator JavaDoc iterator = result.iterator(); iterator.hasNext();)
91         {
92             PBAwareObject pba = (PBAwareObject) iterator.next();
93             assertNotNull(pba);
94             assertNotNull(pba.getRefObject());
95             assertEquals(3, pba.getCollObjects().size());
96             assertTrue(pba.isAfterLookupRefObjectPopulated());
97             assertTrue(pba.isAfterLookupCollObjectsPopulated());
98         }
99     }
100
101     /**
102      * tests the callbacks beforeStore() and afterStore().
103      */

104     public void testStoreCallbacks() throws Exception JavaDoc
105     {
106         PBAwareObject obj = new PBAwareObject();
107         assertEquals(false, obj.getCalledBeforeUpdate());
108         assertEquals(false, obj.getCalledAfterUpdate());
109         assertEquals(false, obj.getCalledBeforeStore());
110         assertEquals(false, obj.getCalledAfterStore());
111         broker.beginTransaction();
112         broker.store(obj);
113         assertEquals(true, obj.getCalledBeforeStore());
114         assertEquals(true, obj.getCalledAfterStore());
115         assertEquals(false, obj.getCalledBeforeUpdate());
116         assertEquals(false, obj.getCalledAfterUpdate());
117         broker.commitTransaction();
118     }
119
120     /**
121      * tests the callbacks beforeStore() and afterStore().
122      */

123     public void testUpdateCallbacks() throws Exception JavaDoc
124     {
125         PBAwareObject obj = new PBAwareObject();
126         broker.beginTransaction();
127         broker.store(obj);
128         broker.commitTransaction();
129         Identity oid = new Identity(obj, broker);
130
131         PBAwareObject lookedUp = (PBAwareObject) broker.getObjectByIdentity(oid);
132         lookedUp.setName("testUpdateCallbacks");
133         assertEquals(false, obj.getCalledBeforeUpdate());
134         assertEquals(false, obj.getCalledAfterUpdate());
135         broker.beginTransaction();
136         broker.store(lookedUp);
137         broker.commitTransaction();
138         assertEquals(true, lookedUp.getCalledBeforeUpdate());
139         assertEquals(true, lookedUp.getCalledAfterUpdate());
140     }
141
142     /**
143      * tests the callbacks beforeDelete() and afterDelete().
144      */

145     public void testDeleteCallbacks() throws Exception JavaDoc
146     {
147         PBAwareObject obj = new PBAwareObject();
148         broker.beginTransaction();
149         broker.store(obj);
150         broker.commitTransaction();
151
152         assertEquals(false, obj.getCalledBeforeDelete());
153         assertEquals(false, obj.getCalledAfterDelete());
154         broker.beginTransaction();
155         broker.delete(obj);
156         assertEquals(true, obj.getCalledBeforeDelete());
157         assertEquals(true, obj.getCalledAfterDelete());
158         broker.commitTransaction();
159     }
160
161     /**
162      * tests the callback afterLookup()
163      */

164     public void testLookupCallback() throws Exception JavaDoc
165     {
166         PBAwareObject obj = new PBAwareObject();
167         Identity oid = new Identity(obj, broker);
168         broker.beginTransaction();
169         broker.store(obj);
170         broker.commitTransaction();
171         assertEquals(false, obj.getCalledAfterLookup());
172
173         PBAwareObject lookedUp = (PBAwareObject) broker.getObjectByIdentity(oid);
174
175         assertEquals(true, lookedUp.getCalledAfterLookup());
176     }
177
178     public void testLifeCycleListener()
179     {
180         PBAwareObject obj = new PBAwareObject();
181         obj.setName("testLifeCycleListener");
182         Identity oid = new Identity(obj, broker);
183
184         // now we add the listener
185
PBLifeCycleListenerObject listener = new PBLifeCycleListenerObject();
186         broker.addListener(listener);
187
188         broker.beginTransaction();
189         broker.store(obj);
190         assertEquals("insert listener call failed", 6, listener.evaluateTest());
191         broker.commitTransaction();
192
193         broker.clearCache();
194         PBAwareObject lookedUp = (PBAwareObject) broker.getObjectByIdentity(oid);
195         assertEquals("lookup listener call failed", 30, listener.evaluateTest());
196         lookedUp.setName("testLifeCycleListener_updated");
197         broker.beginTransaction();
198         broker.store(lookedUp);
199         assertEquals("update listener call failed", 2310, listener.evaluateTest());
200         broker.commitTransaction();
201
202         broker.beginTransaction();
203         broker.delete(obj);
204         assertEquals("delete listener call failed", 510510, listener.evaluateTest());
205         broker.commitTransaction();
206     }
207
208     public void testPBStateListener()
209     {
210         // This test need its own broker instance
211
PersistenceBroker pb = PersistenceBrokerFactory.defaultPersistenceBroker();
212         PBStateListenerObject listener;
213         PBStateListenerObject listener_2 = null;
214         try
215         {
216             listener = new PBStateListenerObject();
217             pb.addListener(listener);
218
219             // we could not check after open method
220
listener.afterOpen(new PBStateEvent(pb, PBStateEvent.Type.AFTER_OPEN));
221             assertEquals("afterOpen listener call failed", 2, listener.evaluateTest());
222
223             pb.beginTransaction();
224             assertEquals("beforeBegin/afterBegin listener call failed", 30, listener.evaluateTest());
225
226             pb.commitTransaction();
227             assertEquals("beforeCommit/afterCommit listener call failed", 2310, listener.evaluateTest());
228
229
230             listener_2 = new PBStateListenerObject();
231             pb.addListener(listener_2);
232
233             // we could not check after open method
234
listener_2.afterOpen(new PBStateEvent(pb, PBStateEvent.Type.AFTER_OPEN));
235             assertEquals("afterOpen listener call failed", 2, listener_2.evaluateTest());
236
237             pb.beginTransaction();
238             assertEquals("beforeBegin/afterBegin listener call failed", 30, listener_2.evaluateTest());
239
240             pb.abortTransaction();
241             assertEquals("beforeRollback/afterRollback listener call failed", 6630, listener_2.evaluateTest());
242         }
243         finally
244         {
245             pb.close();
246             if(listener_2 != null) assertEquals("beforeClose listener call failed", 125970, listener_2.evaluateTest());
247             else fail("Something wrong with test");
248         }
249     }
250
251
252
253     //************************************************************************
254
// test classes
255
//************************************************************************
256
public static class PBLifeCycleListenerObject implements PBLifeCycleListener
257     {
258         int beforeInsert = 1, afterInsert = 1, beforeUpdate = 1, afterUpdate = 1,
259         beforeDelete = 1, afterDelete = 1, afterLookup = 1;
260
261         public void beforeInsert(PBLifeCycleEvent event) throws PersistenceBrokerException
262         {
263             if(!(event.getTarget() instanceof PBAwareObject)) return;
264             beforeInsert*=2;
265         }
266
267         public void afterInsert(PBLifeCycleEvent event) throws PersistenceBrokerException
268         {
269             if(!(event.getTarget() instanceof PBAwareObject)) return;
270             afterInsert*=3;
271         }
272
273         public void afterLookup(PBLifeCycleEvent event) throws PersistenceBrokerException
274         {
275             if(!(event.getTarget() instanceof PBAwareObject)) return;
276             afterLookup*=5;
277         }
278
279         public void beforeUpdate(PBLifeCycleEvent event) throws PersistenceBrokerException
280         {
281             if(!(event.getTarget() instanceof PBAwareObject)) return;
282             beforeUpdate*=7;
283         }
284
285         public void afterUpdate(PBLifeCycleEvent event) throws PersistenceBrokerException
286         {
287             if(!(event.getTarget() instanceof PBAwareObject)) return;
288             afterUpdate*=11;
289         }
290
291         public void beforeDelete(PBLifeCycleEvent event) throws PersistenceBrokerException
292         {
293             if(!(event.getTarget() instanceof PBAwareObject)) return;
294             beforeDelete*=13;
295         }
296
297         public void afterDelete(PBLifeCycleEvent event) throws PersistenceBrokerException
298         {
299             if(!(event.getTarget() instanceof PBAwareObject)) return;
300             afterDelete*=17;
301         }
302
303         public int evaluateTest()
304         {
305             return beforeInsert * afterInsert * beforeUpdate * afterUpdate * beforeDelete *
306                     afterDelete * afterLookup;
307         }
308     }
309
310
311
312     public static class PBStateListenerObject implements PBStateListener
313     {
314         int afterOpen = 1, beforeBegin = 1, afterBegin = 1, beforeCommit = 1, afterCommit = 1,
315         beforeRollback = 1, afterRollback = 1, beforeClose = 1;
316
317         public void afterOpen(PBStateEvent event)
318         {
319             afterOpen*=2;
320         }
321
322         public void beforeBegin(PBStateEvent event)
323         {
324             beforeBegin*=3;
325         }
326
327         public void afterBegin(PBStateEvent event)
328         {
329             afterBegin*=5;
330         }
331
332         public void beforeCommit(PBStateEvent event)
333         {
334             beforeCommit*=7;
335         }
336
337         public void afterCommit(PBStateEvent event)
338         {
339             afterCommit*=11;
340         }
341
342         public void beforeRollback(PBStateEvent event)
343         {
344             beforeRollback*=13;
345         }
346
347         public void afterRollback(PBStateEvent event)
348         {
349             afterRollback*=17;
350         }
351
352         public void beforeClose(PBStateEvent event)
353         {
354             beforeClose*=19;
355         }
356
357         public int evaluateTest()
358         {
359             return afterOpen * beforeBegin * afterBegin * beforeCommit * afterCommit *
360                     beforeRollback * afterRollback * beforeClose;
361         }
362     }
363
364
365
366     /**
367      * persistence capable class
368      */

369     public static class PBAwareObject implements PersistenceBrokerAware, Serializable JavaDoc
370     {
371         private int id;
372         private String JavaDoc name;
373         private RefObj refObject;
374         private List JavaDoc collObjects;
375
376         private boolean calledBeforeInsert = false;
377         private boolean calledAfterInsert = false;
378         private boolean calledBeforeDelete = false;
379         private boolean calledAfterDelete = false;
380         private boolean calledAfterLookup = false;
381         private boolean calledAfterUpdate = false;
382         private boolean calledBeforeUpdate = false;
383         private boolean afterLookupRefObjectPopulated = false;
384         private boolean afterLookupCollObjectsPopulated = false;
385
386         public PBAwareObject()
387         {
388         }
389
390         public PBAwareObject(String JavaDoc name)
391         {
392             this.name = name;
393         }
394
395         public void beforeUpdate(PersistenceBroker broker) throws PersistenceBrokerException
396         {
397             calledBeforeUpdate = true;
398         }
399
400         public void afterUpdate(PersistenceBroker broker) throws PersistenceBrokerException
401         {
402             calledAfterUpdate = true;
403         }
404         public void beforeInsert(PersistenceBroker broker) throws PersistenceBrokerException
405         {
406             //System.out.println("beforeStore()");
407
calledBeforeInsert = true;
408         }
409         public void afterInsert(PersistenceBroker broker) throws PersistenceBrokerException
410         {
411             //System.out.println("afterStore()");
412
if (calledBeforeInsert)
413             {
414                 calledAfterInsert = true;
415             }
416         }
417         public void beforeDelete(PersistenceBroker broker) throws PersistenceBrokerException
418         {
419             //System.out.println("beforeDelete()");
420
calledBeforeDelete = true;
421         }
422         public void afterDelete(PersistenceBroker broker) throws PersistenceBrokerException
423         {
424             //System.out.println("afterDelete()");
425
if (calledBeforeDelete)
426             {
427                 calledAfterDelete = true;
428             }
429         }
430         public void afterLookup(PersistenceBroker broker) throws PersistenceBrokerException
431         {
432             //System.out.println("afterLookup()");
433
calledAfterLookup = true;
434             if(refObject != null) afterLookupRefObjectPopulated = true;
435             if(collObjects != null) afterLookupCollObjectsPopulated = true;
436 // System.out.println("## " + refObject);
437
// System.out.println("## " + collObjects);
438
// if(refObject == null)
439
// {
440
// try{throw new Exception();}catch(Exception e)
441
// {
442
// e.printStackTrace();
443
// }
444
// }
445
}
446
447
448         public boolean getCalledAfterUpdate()
449         {
450             return calledAfterUpdate;
451         }
452
453         public void setCalledAfterUpdate(boolean calledAfterUpdate)
454         {
455             this.calledAfterUpdate = calledAfterUpdate;
456         }
457
458         public boolean getCalledBeforeUpdate()
459         {
460             return calledBeforeUpdate;
461         }
462
463         public void setCalledBeforeUpdate(boolean calledBeforeUpdate)
464         {
465             this.calledBeforeUpdate = calledBeforeUpdate;
466         }
467
468         public boolean getCalledAfterDelete()
469         {
470             return calledAfterDelete;
471         }
472
473         public void setCalledAfterDelete(boolean calledAfterDelete)
474         {
475             this.calledAfterDelete = calledAfterDelete;
476         }
477
478         public boolean getCalledAfterLookup()
479         {
480             return calledAfterLookup;
481         }
482
483         public void setCalledAfterLookup(boolean calledAfterLookup)
484         {
485             this.calledAfterLookup = calledAfterLookup;
486         }
487
488         public boolean getCalledAfterStore()
489         {
490             return calledAfterInsert;
491         }
492
493         public void setCalledAfterStore(boolean calledAfterStore)
494         {
495             this.calledAfterInsert = calledAfterStore;
496         }
497
498         public boolean getCalledBeforeDelete()
499         {
500             return calledBeforeDelete;
501         }
502
503         public void setCalledBeforeDelete(boolean calledBeforeDelete)
504         {
505             this.calledBeforeDelete = calledBeforeDelete;
506         }
507
508         public boolean getCalledBeforeStore()
509         {
510             return calledBeforeInsert;
511         }
512
513         public void setCalledBeforeStore(boolean calledBeforeStore)
514         {
515             this.calledBeforeInsert = calledBeforeStore;
516         }
517
518         public boolean isCalledBeforeInsert()
519         {
520             return calledBeforeInsert;
521         }
522
523         public void setCalledBeforeInsert(boolean calledBeforeInsert)
524         {
525             this.calledBeforeInsert = calledBeforeInsert;
526         }
527
528         public boolean isCalledAfterInsert()
529         {
530             return calledAfterInsert;
531         }
532
533         public void setCalledAfterInsert(boolean calledAfterInsert)
534         {
535             this.calledAfterInsert = calledAfterInsert;
536         }
537
538         public boolean isAfterLookupRefObjectPopulated()
539         {
540             return afterLookupRefObjectPopulated;
541         }
542
543         public void setAfterLookupRefObjectPopulated(boolean afterLookupRefObjectPopulated)
544         {
545             this.afterLookupRefObjectPopulated = afterLookupRefObjectPopulated;
546         }
547
548         public boolean isAfterLookupCollObjectsPopulated()
549         {
550             return afterLookupCollObjectsPopulated;
551         }
552
553         public void setAfterLookupCollObjectsPopulated(boolean afterLookupCollObjectsPopulated)
554         {
555             this.afterLookupCollObjectsPopulated = afterLookupCollObjectsPopulated;
556         }
557
558         public String JavaDoc getName()
559         {
560             return name;
561         }
562
563         public void setName(String JavaDoc name)
564         {
565             this.name = name;
566         }
567
568         public int getId()
569         {
570             return id;
571         }
572
573         public void setId(int id)
574         {
575             this.id = id;
576         }
577
578         public RefObj getRefObject()
579         {
580             return refObject;
581         }
582
583         public void setRefObject(RefObj refObj)
584         {
585             this.refObject = refObj;
586         }
587
588         public List JavaDoc getCollObjects()
589         {
590             return collObjects;
591         }
592
593         public void addCollObject(CollObj obj)
594         {
595             if(collObjects == null)
596             {
597                 collObjects = new ArrayList JavaDoc();
598             }
599             collObjects.add(obj);
600         }
601
602         public void setCollObjects(List JavaDoc collObjects)
603         {
604             this.collObjects = collObjects;
605         }
606
607         public String JavaDoc toString()
608         {
609             return ToStringBuilder.reflectionToString(this);
610         }
611     }
612
613     public static class RefObj implements Serializable JavaDoc
614     {
615         private Integer JavaDoc id;
616         private String JavaDoc name;
617
618         public RefObj()
619         {
620         }
621
622         public RefObj(Integer JavaDoc id, String JavaDoc name)
623         {
624             this.id = id;
625             this.name = name;
626         }
627
628         public RefObj(String JavaDoc name)
629         {
630             this.name = name;
631         }
632
633         public Integer JavaDoc getId()
634         {
635             return id;
636         }
637
638         public void setId(Integer JavaDoc id)
639         {
640             this.id = id;
641         }
642
643         public String JavaDoc getName()
644         {
645             return name;
646         }
647
648         public void setName(String JavaDoc name)
649         {
650             this.name = name;
651         }
652     }
653
654     public static class CollObj extends RefObj
655     {
656         private Integer JavaDoc fkPBAwareObject;
657
658         public CollObj()
659         {
660         }
661
662         public CollObj(Integer JavaDoc id, String JavaDoc name)
663         {
664             super(id, name);
665         }
666
667         public CollObj(String JavaDoc name)
668         {
669             super(name);
670         }
671
672         public Integer JavaDoc getFkPBAwareObject()
673         {
674             return fkPBAwareObject;
675         }
676
677         public void setFkPBAwareObject(Integer JavaDoc fkPBAwareObject)
678         {
679             this.fkPBAwareObject = fkPBAwareObject;
680         }
681     }
682
683 }
684
Popular Tags