KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > jmx > compliance > notification > NotificationBroadcasterSupportTestCase


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.test.jmx.compliance.notification;
23
24 import java.util.ArrayList JavaDoc;
25 import java.util.Iterator JavaDoc;
26
27 import javax.management.ListenerNotFoundException JavaDoc;
28 import javax.management.Notification JavaDoc;
29 import javax.management.NotificationBroadcasterSupport JavaDoc;
30 import javax.management.NotificationFilterSupport JavaDoc;
31
32 import org.jboss.test.jmx.compliance.notification.support.Listener;
33
34 import junit.framework.TestCase;
35
36 /**
37  * Notification Broadcaster Support tests.
38  *
39  * @author <a HREF="mailto:Adrian.Brock@HappeningTimes.com">Adrian Brock</a>.
40  */

41 public class NotificationBroadcasterSupportTestCase
42   extends TestCase
43 {
44    // Attributes ----------------------------------------------------------------
45

46    /**
47     * The sent notifications
48     */

49    private ArrayList JavaDoc sent = new ArrayList JavaDoc();
50
51    /**
52     * The next notification sequence
53     */

54    private long sequence = 0;
55
56    /**
57     * The default notification type
58     */

59    private static final String JavaDoc DEFAULT_TYPE = "DefaultType";
60
61    /**
62     * A different notification type
63     */

64    private static final String JavaDoc ANOTHER_TYPE = "AnotherType";
65
66    /**
67     * No notifications
68     */

69    private static final ArrayList JavaDoc EMPTY = new ArrayList JavaDoc();
70
71    // Constructor ---------------------------------------------------------------
72

73    /**
74     * Construct the test
75     */

76    public NotificationBroadcasterSupportTestCase(String JavaDoc s)
77    {
78       super(s);
79    }
80
81    // Tests ---------------------------------------------------------------------
82

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

934    private void createNotification(NotificationBroadcasterSupport JavaDoc broadcaster)
935    {
936       createNotification(broadcaster, DEFAULT_TYPE);
937    }
938
939    private void createNotification(NotificationBroadcasterSupport JavaDoc broadcaster, String JavaDoc type)
940    {
941       synchronized(this)
942       {
943          sequence++;
944       }
945       Notification JavaDoc notification = new Notification JavaDoc(type, broadcaster, sequence);
946       sent.add(notification);
947       broadcaster.sendNotification(notification);
948    }
949
950    private void clear()
951    {
952       sent.clear();
953    }
954
955    private ArrayList JavaDoc apply(ArrayList JavaDoc sent, NotificationFilterSupport JavaDoc filter)
956    {
957       ArrayList JavaDoc result = new ArrayList JavaDoc();
958       for (Iterator JavaDoc iterator = sent.iterator(); iterator.hasNext();)
959       {
960          Notification JavaDoc notification = (Notification JavaDoc) iterator.next();
961          if (filter.isNotificationEnabled(notification))
962             result.add(notification);
963       }
964       return result;
965    }
966
967    private ArrayList JavaDoc received(Listener listener, Object JavaDoc object)
968    {
969       ArrayList JavaDoc result = (ArrayList JavaDoc) listener.notifications.get(object);
970       if (result == null)
971          result = EMPTY;
972       return result;
973    }
974
975    private void compare(ArrayList JavaDoc passedSent, ArrayList JavaDoc passedReceived)
976       throws Exception JavaDoc
977    {
978       ArrayList JavaDoc sent = new ArrayList JavaDoc(passedSent);
979       ArrayList JavaDoc received = new ArrayList JavaDoc(passedReceived);
980
981       for (Iterator JavaDoc iterator = sent.iterator(); iterator.hasNext();)
982       {
983           Notification JavaDoc notification = (Notification JavaDoc) iterator.next();
984           boolean found = received.remove(notification);
985           assertTrue("Expected notification " + notification, found);
986       }
987
988       assertTrue("Didn't expect notification(s) " + received, received.isEmpty());
989    }
990 }
Popular Tags