KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > compliance > notification > NotificationBroadcasterSupportTestCase


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package test.compliance.notification;
8
9 import java.util.ArrayList;
10 import java.util.Iterator;
11
12 import javax.management.ListenerNotFoundException;
13 import javax.management.Notification;
14 import javax.management.NotificationBroadcasterSupport;
15 import javax.management.NotificationFilterSupport;
16
17 import junit.framework.TestCase;
18 import test.compliance.notification.support.Listener;
19
20 /**
21  * Notification Broadcaster Support tests.
22  *
23  * @author <a HREF="mailto:Adrian.Brock@HappeningTimes.com">Adrian Brock</a>.
24  */

25 public class NotificationBroadcasterSupportTestCase
26   extends TestCase
27 {
28    // Attributes ----------------------------------------------------------------
29

30    /**
31     * The sent notifications
32     */

33    private ArrayList sent = new ArrayList();
34
35    /**
36     * The next notification sequence
37     */

38    private long sequence = 0;
39
40    /**
41     * The default notification type
42     */

43    private static final String DEFAULT_TYPE = "DefaultType";
44
45    /**
46     * A different notification type
47     */

48    private static final String ANOTHER_TYPE = "AnotherType";
49
50    /**
51     * No notifications
52     */

53    private static final ArrayList EMPTY = new ArrayList();
54
55    // Constructor ---------------------------------------------------------------
56

57    /**
58     * Construct the test
59     */

60    public NotificationBroadcasterSupportTestCase(String s)
61    {
62       super(s);
63    }
64
65    // Tests ---------------------------------------------------------------------
66

67    public void testAddNotificationListener()
68       throws Exception
69    {
70       NotificationBroadcasterSupport broadcaster = new NotificationBroadcasterSupport();
71
72       broadcaster.addNotificationListener(new Listener(), null, null);
73
74       broadcaster.addNotificationListener(new Listener(), new NotificationFilterSupport(), null);
75
76       broadcaster.addNotificationListener(new Listener(), null, new Object());
77
78       broadcaster.addNotificationListener(new Listener(), new NotificationFilterSupport(), new Object());
79    }
80
81    public void testAddNotificationListenerErrors()
82       throws Exception
83    {
84       NotificationBroadcasterSupport broadcaster = new NotificationBroadcasterSupport();
85
86       boolean caught = false;
87       try
88       {
89          broadcaster.addNotificationListener(null, null, null);
90       }
91       catch (IllegalArgumentException e)
92       {
93          caught = true;
94       }
95       assertTrue("Expected IllegalArgumentException for null listener", caught);
96
97       caught = false;
98       try
99       {
100          broadcaster.addNotificationListener(null, new NotificationFilterSupport(), null);
101       }
102       catch (IllegalArgumentException e)
103       {
104          caught = true;
105       }
106       assertTrue("Expected IllegalArgumentException for null listener", caught);
107
108       caught = false;
109       try
110       {
111          broadcaster.addNotificationListener(null, null, new Object());
112       }
113       catch (IllegalArgumentException e)
114       {
115          caught = true;
116       }
117       assertTrue("Expected IllegalArgumentException for null listener", caught);
118
119       caught = false;
120       try
121       {
122          broadcaster.addNotificationListener(null, new NotificationFilterSupport(), new Object());
123       }
124       catch (IllegalArgumentException e)
125       {
126          caught = true;
127       }
128       assertTrue("Expected IllegalArgumentException for null listener", caught);
129    }
130
131    public void testNotificationWithNoListeners()
132       throws Exception
133    {
134       NotificationBroadcasterSupport broadcaster = new NotificationBroadcasterSupport();
135
136       clear();
137       createNotification(broadcaster);
138    }
139
140    public void testSimpleNotification()
141       throws Exception
142    {
143       NotificationBroadcasterSupport broadcaster = new NotificationBroadcasterSupport();
144
145       Listener listener = new Listener();
146       broadcaster.addNotificationListener(listener, null, null);
147
148       clear();
149       createNotification(broadcaster);
150
151       compare(sent, received(listener, null));
152    }
153
154    public void testSimpleFilteredNotification()
155       throws Exception
156    {
157       NotificationBroadcasterSupport broadcaster = new NotificationBroadcasterSupport();
158
159       Listener listener = new Listener();
160       NotificationFilterSupport filter = new NotificationFilterSupport();
161       filter.enableType(DEFAULT_TYPE);
162       broadcaster.addNotificationListener(listener, filter, null);
163
164       clear();
165       createNotification(broadcaster);
166
167       compare(apply(sent, filter), received(listener, null));
168    }
169
170    public void testSimpleFilteredOutNotification()
171       throws Exception
172    {
173       NotificationBroadcasterSupport broadcaster = new NotificationBroadcasterSupport();
174
175       Listener listener = new Listener();
176       NotificationFilterSupport filter = new NotificationFilterSupport();
177       filter.disableType(DEFAULT_TYPE);
178       broadcaster.addNotificationListener(listener, filter, null);
179
180       clear();
181       createNotification(broadcaster);
182
183       compare(apply(sent, filter), received(listener, null));
184    }
185
186    public void testSimpleNotificationWithHandback()
187       throws Exception
188    {
189       NotificationBroadcasterSupport broadcaster = new NotificationBroadcasterSupport();
190
191       Listener listener = new Listener();
192       Object handback = new Object();
193       broadcaster.addNotificationListener(listener, null, handback);
194
195       clear();
196       createNotification(broadcaster);
197
198       compare(sent, received(listener, handback));
199       compare(EMPTY, received(listener, null));
200    }
201
202    public void testTwoNotifications()
203       throws Exception
204    {
205       NotificationBroadcasterSupport broadcaster = new NotificationBroadcasterSupport();
206
207       Listener listener = new Listener();
208       broadcaster.addNotificationListener(listener, null, null);
209
210       clear();
211       createNotification(broadcaster);
212       createNotification(broadcaster);
213
214       compare(sent, received(listener, null));
215    }
216
217    public void testTwoNotificationsWithDifferentTypes()
218       throws Exception
219    {
220       NotificationBroadcasterSupport broadcaster = new NotificationBroadcasterSupport();
221
222       Listener listener = new Listener();
223       NotificationFilterSupport filter = new NotificationFilterSupport();
224       filter.enableType(DEFAULT_TYPE);
225       filter.enableType(ANOTHER_TYPE);
226       broadcaster.addNotificationListener(listener, filter, null);
227
228       clear();
229       createNotification(broadcaster);
230       createNotification(broadcaster, ANOTHER_TYPE);
231
232       compare(apply(sent, filter), received(listener, null));
233    }
234
235    public void testTwoNotificationsWithDifferentTypesOneFilteredOut()
236       throws Exception
237    {
238       NotificationBroadcasterSupport broadcaster = new NotificationBroadcasterSupport();
239
240       Listener listener = new Listener();
241       NotificationFilterSupport filter = new NotificationFilterSupport();
242       filter.enableType(DEFAULT_TYPE);
243       broadcaster.addNotificationListener(listener, filter, null);
244
245       clear();
246       createNotification(broadcaster);
247       createNotification(broadcaster, ANOTHER_TYPE);
248
249       compare(apply(sent, filter), received(listener, null));
250    }
251
252    public void testTwoListeners()
253       throws Exception
254    {
255       NotificationBroadcasterSupport broadcaster = new NotificationBroadcasterSupport();
256
257       Listener listener1 = new Listener();
258       broadcaster.addNotificationListener(listener1, null, null);
259       Listener listener2 = new Listener();
260       broadcaster.addNotificationListener(listener2, null, null);
261
262       clear();
263       createNotification(broadcaster);
264
265       compare(sent, received(listener1, null));
266       compare(sent, received(listener2, null));
267    }
268
269    public void testOneListenerRegisteredTwice()
270       throws Exception
271    {
272       NotificationBroadcasterSupport broadcaster = new NotificationBroadcasterSupport();
273
274       Listener listener = new Listener();
275       broadcaster.addNotificationListener(listener, null, null);
276       broadcaster.addNotificationListener(listener, null, null);
277
278       clear();
279       createNotification(broadcaster);
280
281       ArrayList copySent = new ArrayList(sent);
282       copySent.addAll(sent);
283       compare(copySent, received(listener, null));
284    }
285
286    public void testOneListenerRegisteredTwiceOneFilteredOut()
287       throws Exception
288    {
289       NotificationBroadcasterSupport broadcaster = new NotificationBroadcasterSupport();
290
291       Listener listener = new Listener();
292       broadcaster.addNotificationListener(listener, null, null);
293       NotificationFilterSupport filter = new NotificationFilterSupport();
294       filter.disableType(DEFAULT_TYPE);
295       broadcaster.addNotificationListener(listener, filter, null);
296
297       clear();
298       createNotification(broadcaster);
299
300       compare(sent, received(listener, null));
301    }
302
303    public void testOneListenerRegisteredWithDifferentHandbacks()
304       throws Exception
305    {
306       NotificationBroadcasterSupport broadcaster = new NotificationBroadcasterSupport();
307
308       Listener listener = new Listener();
309       Object handback1 = new Object();
310       broadcaster.addNotificationListener(listener, null, handback1);
311       Object handback2 = new Object();
312       broadcaster.addNotificationListener(listener, null, handback2);
313
314       clear();
315       createNotification(broadcaster);
316
317       compare(sent, received(listener, handback1));
318       compare(sent, received(listener, handback2));
319       compare(EMPTY, received(listener, null));
320    }
321
322    public void testOneListenerRegisteredWithDifferentHandbacksOneFilteredOut()
323       throws Exception
324    {
325       NotificationBroadcasterSupport broadcaster = new NotificationBroadcasterSupport();
326
327       Listener listener = new Listener();
328       Object handback1 = new Object();
329       broadcaster.addNotificationListener(listener, null, handback1);
330       Object handback2 = new Object();
331       NotificationFilterSupport filter = new NotificationFilterSupport();
332       filter.disableType(DEFAULT_TYPE);
333       broadcaster.addNotificationListener(listener, filter, handback2);
334
335       clear();
336       createNotification(broadcaster);
337
338       compare(sent, received(listener, handback1));
339       compare(apply(sent, filter), received(listener, handback2));
340       compare(EMPTY, received(listener, null));
341    }
342
343    public void testOneListenerRegisteredWithTwoBroadcasters()
344       throws Exception
345    {
346       NotificationBroadcasterSupport broadcaster1 = new NotificationBroadcasterSupport();
347       NotificationBroadcasterSupport broadcaster2 = new NotificationBroadcasterSupport();
348
349       Listener listener = new Listener();
350       broadcaster1.addNotificationListener(listener, null, null);
351       broadcaster2.addNotificationListener(listener, null, null);
352
353       createNotification(broadcaster1);
354       createNotification(broadcaster2);
355
356       compare(sent, received(listener, null));
357    }
358
359    public void testRemoveListener()
360       throws Exception
361    {
362       NotificationBroadcasterSupport broadcaster = new NotificationBroadcasterSupport();
363
364       Listener listener = new Listener();
365       broadcaster.addNotificationListener(listener, null, null);
366       broadcaster.removeNotificationListener(listener);
367
368       createNotification(broadcaster);
369
370       compare(EMPTY, received(listener, null));
371    }
372
373    public void testRegisteredTwiceRemoveListener()
374       throws Exception
375    {
376       NotificationBroadcasterSupport broadcaster = new NotificationBroadcasterSupport();
377
378       Listener listener = new Listener();
379       broadcaster.addNotificationListener(listener, null, null);
380       broadcaster.addNotificationListener(listener, null, null);
381       broadcaster.removeNotificationListener(listener);
382
383       createNotification(broadcaster);
384
385       compare(EMPTY, received(listener, null));
386    }
387
388    public void testRegisteredWithFilterRemoveListener()
389       throws Exception
390    {
391       NotificationBroadcasterSupport broadcaster = new NotificationBroadcasterSupport();
392
393       Listener listener = new Listener();
394       NotificationFilterSupport filter = new NotificationFilterSupport();
395       filter.enableType(DEFAULT_TYPE);
396       broadcaster.addNotificationListener(listener, filter, null);
397       broadcaster.removeNotificationListener(listener);
398
399       createNotification(broadcaster);
400
401       compare(EMPTY, received(listener, null));
402    }
403
404    public void testRegisteredWithHandbackRemoveListener()
405       throws Exception
406    {
407       NotificationBroadcasterSupport broadcaster = new NotificationBroadcasterSupport();
408
409       Listener listener = new Listener();
410       Object handback = new Object();
411       broadcaster.addNotificationListener(listener, null, handback);
412       broadcaster.removeNotificationListener(listener);
413
414       createNotification(broadcaster);
415
416       compare(EMPTY, received(listener, null));
417       compare(EMPTY, received(listener, handback));
418    }
419
420    public void testRegisteredWithFilterHandbackRemoveListener()
421       throws Exception
422    {
423       NotificationBroadcasterSupport broadcaster = new NotificationBroadcasterSupport();
424
425       Listener listener = new Listener();
426       Object handback = new Object();
427       NotificationFilterSupport filter = new NotificationFilterSupport();
428       filter.enableType(DEFAULT_TYPE);
429       broadcaster.addNotificationListener(listener, filter, handback);
430       broadcaster.removeNotificationListener(listener);
431
432       createNotification(broadcaster);
433
434       compare(EMPTY, received(listener, null));
435       compare(EMPTY, received(listener, handback));
436    }
437
438    public void testRegisterRemoveListenerRegister()
439       throws Exception
440    {
441       NotificationBroadcasterSupport broadcaster = new NotificationBroadcasterSupport();
442
443       Listener listener = new Listener();
444       broadcaster.addNotificationListener(listener, null, null);
445       broadcaster.removeNotificationListener(listener);
446       broadcaster.addNotificationListener(listener, null, null);
447
448       createNotification(broadcaster);
449
450       compare(sent, received(listener, null));
451    }
452
453    public void testRemoveListenerErrors()
454       throws Exception
455    {
456       NotificationBroadcasterSupport broadcaster = new NotificationBroadcasterSupport();
457
458       Listener listener = new Listener();
459
460       boolean caught = false;
461       try
462       {
463          broadcaster.removeNotificationListener(null);
464       }
465       catch (ListenerNotFoundException e)
466       {
467          caught = true;
468       }
469       assertTrue("Expected ListenerNotFoundException for null listener", caught);
470
471       caught = false;
472       try
473       {
474          broadcaster.removeNotificationListener(listener);
475       }
476       catch (ListenerNotFoundException e)
477       {
478          caught = true;
479       }
480       assertTrue("Expected ListenerNotFoundException for listener never added", caught);
481
482       caught = false;
483       try
484       {
485          broadcaster.addNotificationListener(listener, null, null);
486          broadcaster.removeNotificationListener(listener);
487          broadcaster.removeNotificationListener(listener);
488       }
489       catch (ListenerNotFoundException e)
490       {
491          caught = true;
492       }
493       assertTrue("Expected ListenerNotFoundException for listener remove twice", caught);
494    }
495
496    public void testRemoveTripletListenerOnly()
497       throws Exception
498    {
499       NotificationBroadcasterSupport broadcaster = new NotificationBroadcasterSupport();
500
501       Listener listener = new Listener();
502       broadcaster.addNotificationListener(listener, null, null);
503       broadcaster.removeNotificationListener(listener, null, null);
504
505       createNotification(broadcaster);
506
507       compare(EMPTY, received(listener, null));
508    }
509
510    public void testRemoveTripletListenerFilter()
511       throws Exception
512    {
513       NotificationBroadcasterSupport broadcaster = new NotificationBroadcasterSupport();
514
515       Listener listener = new Listener();
516       NotificationFilterSupport filter = new NotificationFilterSupport();
517       filter.enableType(DEFAULT_TYPE);
518       broadcaster.addNotificationListener(listener, filter, null);
519       broadcaster.removeNotificationListener(listener, filter, null);
520
521       createNotification(broadcaster);
522
523       compare(EMPTY, received(listener, null));
524    }
525
526    public void testRemoveTripletListenerHandback()
527       throws Exception
528    {
529       NotificationBroadcasterSupport broadcaster = new NotificationBroadcasterSupport();
530
531       Listener listener = new Listener();
532       Object handback = new Object();
533       broadcaster.addNotificationListener(listener, null, handback);
534       broadcaster.removeNotificationListener(listener, null, handback);
535
536       createNotification(broadcaster);
537
538       compare(EMPTY, received(listener, null));
539       compare(EMPTY, received(listener, handback));
540    }
541
542    public void testRemoveTripletListenerFilterHandback()
543       throws Exception
544    {
545       NotificationBroadcasterSupport broadcaster = new NotificationBroadcasterSupport();
546
547       Listener listener = new Listener();
548       Object handback = new Object();
549       NotificationFilterSupport filter = new NotificationFilterSupport();
550       filter.enableType(DEFAULT_TYPE);
551       broadcaster.addNotificationListener(listener, filter, handback);
552       broadcaster.removeNotificationListener(listener, filter, handback);
553
554       createNotification(broadcaster);
555
556       compare(EMPTY, received(listener, null));
557       compare(EMPTY, received(listener, handback));
558    }
559
560    public void testRegisterTwiceRemoveOnceTriplet()
561       throws Exception
562    {
563       NotificationBroadcasterSupport broadcaster = new NotificationBroadcasterSupport();
564
565       Listener listener = new Listener();
566       Object handback = new Object();
567       NotificationFilterSupport filter = new NotificationFilterSupport();
568       filter.enableType(DEFAULT_TYPE);
569       broadcaster.addNotificationListener(listener, filter, handback);
570       broadcaster.addNotificationListener(listener, filter, handback);
571       broadcaster.removeNotificationListener(listener, filter, handback);
572
573       createNotification(broadcaster);
574
575       compare(EMPTY, received(listener, null));
576       compare(sent, received(listener, handback));
577    }
578
579    public void testRegisterTwiceRemoveTwiceTriplet()
580       throws Exception
581    {
582       NotificationBroadcasterSupport broadcaster = new NotificationBroadcasterSupport();
583
584       Listener listener = new Listener();
585       Object handback = new Object();
586       NotificationFilterSupport filter = new NotificationFilterSupport();
587       filter.enableType(DEFAULT_TYPE);
588       broadcaster.addNotificationListener(listener, filter, handback);
589       broadcaster.addNotificationListener(listener, filter, handback);
590       broadcaster.removeNotificationListener(listener, filter, handback);
591       broadcaster.removeNotificationListener(listener, filter, handback);
592
593       createNotification(broadcaster);
594
595       compare(EMPTY, received(listener, null));
596       compare(EMPTY, received(listener, handback));
597    }
598
599    public void testRegisterTwoRemoveOneTripletListener()
600       throws Exception
601    {
602       NotificationBroadcasterSupport broadcaster = new NotificationBroadcasterSupport();
603
604       Listener listener = new Listener();
605       Object handback = new Object();
606       NotificationFilterSupport filter = new NotificationFilterSupport();
607       filter.enableType(DEFAULT_TYPE);
608       broadcaster.addNotificationListener(listener, filter, handback);
609       broadcaster.addNotificationListener(listener, null, null);
610       broadcaster.removeNotificationListener(listener, null, null);
611
612       createNotification(broadcaster);
613
614       compare(EMPTY, received(listener, null));
615       compare(sent, received(listener, handback));
616    }
617
618    public void testRegisterTwoRemoveOneTripletListenerFilter()
619       throws Exception
620    {
621       NotificationBroadcasterSupport broadcaster = new NotificationBroadcasterSupport();
622
623       Listener listener = new Listener();
624       Object handback = new Object();
625       NotificationFilterSupport filter = new NotificationFilterSupport();
626       filter.enableType(DEFAULT_TYPE);
627       broadcaster.addNotificationListener(listener, filter, handback);
628       broadcaster.addNotificationListener(listener, filter, null);
629       broadcaster.removeNotificationListener(listener, filter, null);
630
631       createNotification(broadcaster);
632
633       compare(EMPTY, received(listener, null));
634       compare(sent, received(listener, handback));
635    }
636
637    public void testRegisterTwoRemoveOneTripletListenerHandback()
638       throws Exception
639    {
640       NotificationBroadcasterSupport broadcaster = new NotificationBroadcasterSupport();
641
642       Listener listener = new Listener();
643       Object handback = new Object();
644       NotificationFilterSupport filter = new NotificationFilterSupport();
645       filter.enableType(DEFAULT_TYPE);
646       broadcaster.addNotificationListener(listener, filter, handback);
647       broadcaster.addNotificationListener(listener, null, handback);
648       broadcaster.removeNotificationListener(listener, null, handback);
649
650       createNotification(broadcaster);
651
652       compare(EMPTY, received(listener, null));
653       compare(sent, received(listener, handback));
654    }
655
656    public void testRegisterTwoRemoveOneTripletListenerFilterHandback()
657       throws Exception
658    {
659       NotificationBroadcasterSupport broadcaster = new NotificationBroadcasterSupport();
660
661       Listener listener = new Listener();
662       Object handback = new Object();
663       NotificationFilterSupport filter = new NotificationFilterSupport();
664       filter.enableType(DEFAULT_TYPE);
665       broadcaster.addNotificationListener(listener, null, null);
666       broadcaster.addNotificationListener(listener, filter, handback);
667       broadcaster.removeNotificationListener(listener, filter, handback);
668
669       createNotification(broadcaster);
670
671       compare(sent, received(listener, null));
672       compare(EMPTY, received(listener, handback));
673    }
674
675    public void testRemoveTripletErrors()
676       throws Exception
677    {
678       NotificationBroadcasterSupport broadcaster = new NotificationBroadcasterSupport();
679       Object handback = new Object();
680       NotificationFilterSupport filter = new NotificationFilterSupport();
681       filter.enableType(DEFAULT_TYPE);
682
683       Listener listener = new Listener();
684
685       boolean caught = false;
686       try
687       {
688          broadcaster.removeNotificationListener(null, null, null);
689       }
690       catch (ListenerNotFoundException e)
691       {
692          caught = true;
693       }
694       assertTrue("Expected ListenerNotFoundException for null listener", caught);
695
696       caught = false;
697       try
698       {
699          broadcaster.removeNotificationListener(listener, null, null);
700       }
701       catch (ListenerNotFoundException e)
702       {
703          caught = true;
704       }
705       assertTrue("Expected ListenerNotFoundException for listener never added", caught);
706
707       caught = false;
708       try
709       {
710          broadcaster.addNotificationListener(listener, null, null);
711          broadcaster.removeNotificationListener(listener, null, null);
712          broadcaster.removeNotificationListener(listener, null, null);
713       }
714       catch (ListenerNotFoundException e)
715       {
716          caught = true;
717       }
718       assertTrue("Expected ListenerNotFoundException for listener remove twice", caught);
719
720       caught = false;
721       try
722       {
723          broadcaster.addNotificationListener(listener, filter, null);
724          broadcaster.removeNotificationListener(listener, new NotificationFilterSupport(), null);
725       }
726       catch (ListenerNotFoundException e)
727       {
728          caught = true;
729          broadcaster.removeNotificationListener(listener, filter, null);
730       }
731       assertTrue("Expected ListenerNotFoundException for wrong filter", caught);
732
733       caught = false;
734       try
735       {
736          broadcaster.addNotificationListener(listener, null, handback);
737          broadcaster.removeNotificationListener(listener, null, new Object());
738       }
739       catch (ListenerNotFoundException e)
740       {
741          caught = true;
742          broadcaster.removeNotificationListener(listener, null, handback);
743       }
744       assertTrue("Expected ListenerNotFoundException for wrong handback", caught);
745
746       caught = false;
747       try
748       {
749          broadcaster.addNotificationListener(listener, filter, handback);
750          broadcaster.removeNotificationListener(listener, new NotificationFilterSupport(), new Object());
751       }
752       catch (ListenerNotFoundException e)
753       {
754          caught = true;
755          broadcaster.removeNotificationListener(listener, filter, handback);
756       }
757       assertTrue("Expected ListenerNotFoundException for wrong filter and handback", caught);
758
759       caught = false;
760       try
761       {
762          broadcaster.addNotificationListener(listener, filter, handback);
763          broadcaster.removeNotificationListener(listener, null, null);
764       }
765       catch (ListenerNotFoundException e)
766       {
767          caught = true;
768          broadcaster.removeNotificationListener(listener, filter, handback);
769       }
770       assertTrue("Expected ListenerNotFoundException for listener remove with wrong triplet 1", caught);
771
772       caught = false;
773       try
774       {
775          broadcaster.addNotificationListener(listener, filter, handback);
776          broadcaster.removeNotificationListener(listener, filter, null);
777       }
778       catch (ListenerNotFoundException e)
779       {
780          caught = true;
781          broadcaster.removeNotificationListener(listener, filter, handback);
782       }
783       assertTrue("Expected ListenerNotFoundException for listener remove with wrong triplet 2", caught);
784
785       caught = false;
786       try
787       {
788          broadcaster.addNotificationListener(listener, filter, handback);
789          broadcaster.removeNotificationListener(listener, null, handback);
790       }
791       catch (ListenerNotFoundException e)
792       {
793          caught = true;
794          broadcaster.removeNotificationListener(listener, filter, handback);
795       }
796       assertTrue("Expected ListenerNotFoundException for listener remove with wrong triplet 3", caught);
797
798       caught = false;
799       try
800       {
801          broadcaster.addNotificationListener(listener, filter, null);
802          broadcaster.removeNotificationListener(listener, null, null);
803       }
804       catch (ListenerNotFoundException e)
805       {
806          caught = true;
807          broadcaster.removeNotificationListener(listener, filter, null);
808       }
809       assertTrue("Expected ListenerNotFoundException for listener remove with wrong triplet 4", caught);
810
811       caught = false;
812       try
813       {
814          broadcaster.addNotificationListener(listener, filter, null);
815          broadcaster.removeNotificationListener(listener, null, handback);
816       }
817       catch (ListenerNotFoundException e)
818       {
819          caught = true;
820          broadcaster.removeNotificationListener(listener, filter, null);
821       }
822       assertTrue("Expected ListenerNotFoundException for listener remove with wrong triplet 5", caught);
823
824       caught = false;
825       try
826       {
827          broadcaster.addNotificationListener(listener, filter, null);
828          broadcaster.removeNotificationListener(listener, filter, handback);
829       }
830       catch (ListenerNotFoundException e)
831       {
832          caught = true;
833          broadcaster.removeNotificationListener(listener, filter, null);
834       }
835       assertTrue("Expected ListenerNotFoundException for listener remove with wrong triplet 6", caught);
836
837       caught = false;
838       try
839       {
840          broadcaster.addNotificationListener(listener, null, handback);
841          broadcaster.removeNotificationListener(listener, null, null);
842       }
843       catch (ListenerNotFoundException e)
844       {
845          caught = true;
846          broadcaster.removeNotificationListener(listener, null, handback);
847       }
848       assertTrue("Expected ListenerNotFoundException for listener remove with wrong triplet 7", caught);
849
850       caught = false;
851       try
852       {
853          broadcaster.addNotificationListener(listener, null, handback);
854          broadcaster.removeNotificationListener(listener, filter, null);
855       }
856       catch (ListenerNotFoundException e)
857       {
858          caught = true;
859          broadcaster.removeNotificationListener(listener, null, handback);
860       }
861       assertTrue("Expected ListenerNotFoundException for listener remove with wrong triplet 8", caught);
862
863       caught = false;
864       try
865       {
866          broadcaster.addNotificationListener(listener, null, handback);
867          broadcaster.removeNotificationListener(listener, filter, handback);
868       }
869       catch (ListenerNotFoundException e)
870       {
871          caught = true;
872          broadcaster.removeNotificationListener(listener, null, handback);
873       }
874       assertTrue("Expected ListenerNotFoundException for listener remove with wrong triplet 9", caught);
875
876       caught = false;
877       try
878       {
879          broadcaster.addNotificationListener(listener, null, null);
880          broadcaster.removeNotificationListener(listener, filter, null);
881       }
882       catch (ListenerNotFoundException e)
883       {
884          caught = true;
885          broadcaster.removeNotificationListener(listener, null, null);
886       }
887       assertTrue("Expected ListenerNotFoundException for listener remove with wrong triplet 10", caught);
888
889       caught = false;
890       try
891       {
892          broadcaster.addNotificationListener(listener, null, null);
893          broadcaster.removeNotificationListener(listener, null, handback);
894       }
895       catch (ListenerNotFoundException e)
896       {
897          caught = true;
898          broadcaster.removeNotificationListener(listener, null, null);
899       }
900       assertTrue("Expected ListenerNotFoundException for listener remove with wrong triplet 11", caught);
901
902       caught = false;
903       try
904       {
905          broadcaster.addNotificationListener(listener, null, null);
906          broadcaster.removeNotificationListener(listener, filter, handback);
907       }
908       catch (ListenerNotFoundException e)
909       {
910          caught = true;
911          broadcaster.removeNotificationListener(listener, null, null);
912       }
913       assertTrue("Expected ListenerNotFoundException for listener remove with wrong triplet 12", caught);
914    }
915
916    // Support -------------------------------------------------------------------
917

918    private void createNotification(NotificationBroadcasterSupport broadcaster)
919    {
920       createNotification(broadcaster, DEFAULT_TYPE);
921    }
922
923    private void createNotification(NotificationBroadcasterSupport broadcaster, String type)
924    {
925       synchronized(this)
926       {
927          sequence++;
928       }
929       Notification notification = new Notification(type, broadcaster, sequence);
930       sent.add(notification);
931       broadcaster.sendNotification(notification);
932    }
933
934    private void clear()
935    {
936       sent.clear();
937    }
938
939    private ArrayList apply(ArrayList sent, NotificationFilterSupport filter)
940    {
941       ArrayList result = new ArrayList();
942       for (Iterator iterator = sent.iterator(); iterator.hasNext();)
943       {
944          Notification notification = (Notification) iterator.next();
945          if (filter.isNotificationEnabled(notification))
946             result.add(notification);
947       }
948       return result;
949    }
950
951    private ArrayList received(Listener listener, Object object)
952    {
953       ArrayList result = (ArrayList) listener.notifications.get(object);
954       if (result == null)
955          result = EMPTY;
956       return result;
957    }
958
959    private void compare(ArrayList passedSent, ArrayList passedReceived)
960       throws Exception
961    {
962       ArrayList sent = new ArrayList(passedSent);
963       ArrayList received = new ArrayList(passedReceived);
964
965       for (Iterator iterator = sent.iterator(); iterator.hasNext();)
966       {
967           Notification notification = (Notification) iterator.next();
968           boolean found = received.remove(notification);
969           assertTrue("Expected notification " + notification, found);
970       }
971
972       assertTrue("Didn't expect notification(s) " + received, received.isEmpty());
973    }
974 }
Popular Tags