Source Code Cross Referenced for NotificationBroadcasterSupportTestCase.java in  » EJB-Server-JBoss-4.2.1 » jmx » test » compliance » notification » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » EJB Server JBoss 4.2.1 » jmx » test.compliance.notification 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * JBoss, Home of Professional Open Source.
003:         * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004:         * as indicated by the @author tags. See the copyright.txt file in the
005:         * distribution for a full listing of individual contributors.
006:         *
007:         * This is free software; you can redistribute it and/or modify it
008:         * under the terms of the GNU Lesser General Public License as
009:         * published by the Free Software Foundation; either version 2.1 of
010:         * the License, or (at your option) any later version.
011:         *
012:         * This software is distributed in the hope that it will be useful,
013:         * but WITHOUT ANY WARRANTY; without even the implied warranty of
014:         * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015:         * Lesser General Public License for more details.
016:         *
017:         * You should have received a copy of the GNU Lesser General Public
018:         * License along with this software; if not, write to the Free
019:         * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020:         * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021:         */
022:        package test.compliance.notification;
023:
024:        import java.util.ArrayList;
025:        import java.util.Iterator;
026:
027:        import javax.management.ListenerNotFoundException;
028:        import javax.management.Notification;
029:        import javax.management.NotificationBroadcasterSupport;
030:        import javax.management.NotificationFilterSupport;
031:
032:        import junit.framework.TestCase;
033:        import test.compliance.notification.support.Listener;
034:
035:        /**
036:         * Notification Broadcaster Support tests.
037:         *
038:         * @author  <a href="mailto:Adrian.Brock@HappeningTimes.com">Adrian Brock</a>.
039:         */
040:        public class NotificationBroadcasterSupportTestCase extends TestCase {
041:            // Attributes ----------------------------------------------------------------
042:
043:            /**
044:             * The sent notifications
045:             */
046:            private ArrayList sent = new ArrayList();
047:
048:            /**
049:             * The next notification sequence
050:             */
051:            private long sequence = 0;
052:
053:            /**
054:             * The default notification type
055:             */
056:            private static final String DEFAULT_TYPE = "DefaultType";
057:
058:            /**
059:             * A different notification type
060:             */
061:            private static final String ANOTHER_TYPE = "AnotherType";
062:
063:            /**
064:             * No notifications
065:             */
066:            private static final ArrayList EMPTY = new ArrayList();
067:
068:            // Constructor ---------------------------------------------------------------
069:
070:            /**
071:             * Construct the test
072:             */
073:            public NotificationBroadcasterSupportTestCase(String s) {
074:                super (s);
075:            }
076:
077:            // Tests ---------------------------------------------------------------------
078:
079:            public void testAddNotificationListener() throws Exception {
080:                NotificationBroadcasterSupport broadcaster = new NotificationBroadcasterSupport();
081:
082:                broadcaster.addNotificationListener(new Listener(), null, null);
083:
084:                broadcaster.addNotificationListener(new Listener(),
085:                        new NotificationFilterSupport(), null);
086:
087:                broadcaster.addNotificationListener(new Listener(), null,
088:                        new Object());
089:
090:                broadcaster.addNotificationListener(new Listener(),
091:                        new NotificationFilterSupport(), new Object());
092:            }
093:
094:            public void testAddNotificationListenerErrors() throws Exception {
095:                NotificationBroadcasterSupport broadcaster = new NotificationBroadcasterSupport();
096:
097:                boolean caught = false;
098:                try {
099:                    broadcaster.addNotificationListener(null, null, null);
100:                } catch (IllegalArgumentException e) {
101:                    caught = true;
102:                }
103:                assertTrue(
104:                        "Expected IllegalArgumentException for null listener",
105:                        caught);
106:
107:                caught = false;
108:                try {
109:                    broadcaster.addNotificationListener(null,
110:                            new NotificationFilterSupport(), null);
111:                } catch (IllegalArgumentException e) {
112:                    caught = true;
113:                }
114:                assertTrue(
115:                        "Expected IllegalArgumentException for null listener",
116:                        caught);
117:
118:                caught = false;
119:                try {
120:                    broadcaster.addNotificationListener(null, null,
121:                            new Object());
122:                } catch (IllegalArgumentException e) {
123:                    caught = true;
124:                }
125:                assertTrue(
126:                        "Expected IllegalArgumentException for null listener",
127:                        caught);
128:
129:                caught = false;
130:                try {
131:                    broadcaster.addNotificationListener(null,
132:                            new NotificationFilterSupport(), new Object());
133:                } catch (IllegalArgumentException e) {
134:                    caught = true;
135:                }
136:                assertTrue(
137:                        "Expected IllegalArgumentException for null listener",
138:                        caught);
139:            }
140:
141:            public void testNotificationWithNoListeners() throws Exception {
142:                NotificationBroadcasterSupport broadcaster = new NotificationBroadcasterSupport();
143:
144:                clear();
145:                createNotification(broadcaster);
146:            }
147:
148:            public void testSimpleNotification() throws Exception {
149:                NotificationBroadcasterSupport broadcaster = new NotificationBroadcasterSupport();
150:
151:                Listener listener = new Listener();
152:                broadcaster.addNotificationListener(listener, null, null);
153:
154:                clear();
155:                createNotification(broadcaster);
156:
157:                compare(sent, received(listener, null));
158:            }
159:
160:            public void testSimpleFilteredNotification() throws Exception {
161:                NotificationBroadcasterSupport broadcaster = new NotificationBroadcasterSupport();
162:
163:                Listener listener = new Listener();
164:                NotificationFilterSupport filter = new NotificationFilterSupport();
165:                filter.enableType(DEFAULT_TYPE);
166:                broadcaster.addNotificationListener(listener, filter, null);
167:
168:                clear();
169:                createNotification(broadcaster);
170:
171:                compare(apply(sent, filter), received(listener, null));
172:            }
173:
174:            public void testSimpleFilteredOutNotification() throws Exception {
175:                NotificationBroadcasterSupport broadcaster = new NotificationBroadcasterSupport();
176:
177:                Listener listener = new Listener();
178:                NotificationFilterSupport filter = new NotificationFilterSupport();
179:                filter.disableType(DEFAULT_TYPE);
180:                broadcaster.addNotificationListener(listener, filter, null);
181:
182:                clear();
183:                createNotification(broadcaster);
184:
185:                compare(apply(sent, filter), received(listener, null));
186:            }
187:
188:            public void testSimpleNotificationWithHandback() throws Exception {
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() throws Exception {
203:                NotificationBroadcasterSupport broadcaster = new NotificationBroadcasterSupport();
204:
205:                Listener listener = new Listener();
206:                broadcaster.addNotificationListener(listener, null, null);
207:
208:                clear();
209:                createNotification(broadcaster);
210:                createNotification(broadcaster);
211:
212:                compare(sent, received(listener, null));
213:            }
214:
215:            public void testTwoNotificationsWithDifferentTypes()
216:                    throws Exception {
217:                NotificationBroadcasterSupport broadcaster = new NotificationBroadcasterSupport();
218:
219:                Listener listener = new Listener();
220:                NotificationFilterSupport filter = new NotificationFilterSupport();
221:                filter.enableType(DEFAULT_TYPE);
222:                filter.enableType(ANOTHER_TYPE);
223:                broadcaster.addNotificationListener(listener, filter, null);
224:
225:                clear();
226:                createNotification(broadcaster);
227:                createNotification(broadcaster, ANOTHER_TYPE);
228:
229:                compare(apply(sent, filter), received(listener, null));
230:            }
231:
232:            public void testTwoNotificationsWithDifferentTypesOneFilteredOut()
233:                    throws Exception {
234:                NotificationBroadcasterSupport broadcaster = new NotificationBroadcasterSupport();
235:
236:                Listener listener = new Listener();
237:                NotificationFilterSupport filter = new NotificationFilterSupport();
238:                filter.enableType(DEFAULT_TYPE);
239:                broadcaster.addNotificationListener(listener, filter, null);
240:
241:                clear();
242:                createNotification(broadcaster);
243:                createNotification(broadcaster, ANOTHER_TYPE);
244:
245:                compare(apply(sent, filter), received(listener, null));
246:            }
247:
248:            public void testTwoListeners() throws Exception {
249:                NotificationBroadcasterSupport broadcaster = new NotificationBroadcasterSupport();
250:
251:                Listener listener1 = new Listener();
252:                broadcaster.addNotificationListener(listener1, null, null);
253:                Listener listener2 = new Listener();
254:                broadcaster.addNotificationListener(listener2, null, null);
255:
256:                clear();
257:                createNotification(broadcaster);
258:
259:                compare(sent, received(listener1, null));
260:                compare(sent, received(listener2, null));
261:            }
262:
263:            public void testOneListenerRegisteredTwice() throws Exception {
264:                NotificationBroadcasterSupport broadcaster = new NotificationBroadcasterSupport();
265:
266:                Listener listener = new Listener();
267:                broadcaster.addNotificationListener(listener, null, null);
268:                broadcaster.addNotificationListener(listener, null, null);
269:
270:                clear();
271:                createNotification(broadcaster);
272:
273:                ArrayList copySent = new ArrayList(sent);
274:                copySent.addAll(sent);
275:                compare(copySent, received(listener, null));
276:            }
277:
278:            public void testOneListenerRegisteredTwiceOneFilteredOut()
279:                    throws Exception {
280:                NotificationBroadcasterSupport broadcaster = new NotificationBroadcasterSupport();
281:
282:                Listener listener = new Listener();
283:                broadcaster.addNotificationListener(listener, null, null);
284:                NotificationFilterSupport filter = new NotificationFilterSupport();
285:                filter.disableType(DEFAULT_TYPE);
286:                broadcaster.addNotificationListener(listener, filter, null);
287:
288:                clear();
289:                createNotification(broadcaster);
290:
291:                compare(sent, received(listener, null));
292:            }
293:
294:            public void testOneListenerRegisteredWithDifferentHandbacks()
295:                    throws Exception {
296:                NotificationBroadcasterSupport broadcaster = new NotificationBroadcasterSupport();
297:
298:                Listener listener = new Listener();
299:                Object handback1 = new Object();
300:                broadcaster.addNotificationListener(listener, null, handback1);
301:                Object handback2 = new Object();
302:                broadcaster.addNotificationListener(listener, null, handback2);
303:
304:                clear();
305:                createNotification(broadcaster);
306:
307:                compare(sent, received(listener, handback1));
308:                compare(sent, received(listener, handback2));
309:                compare(EMPTY, received(listener, null));
310:            }
311:
312:            public void testOneListenerRegisteredWithDifferentHandbacksOneFilteredOut()
313:                    throws Exception {
314:                NotificationBroadcasterSupport broadcaster = new NotificationBroadcasterSupport();
315:
316:                Listener listener = new Listener();
317:                Object handback1 = new Object();
318:                broadcaster.addNotificationListener(listener, null, handback1);
319:                Object handback2 = new Object();
320:                NotificationFilterSupport filter = new NotificationFilterSupport();
321:                filter.disableType(DEFAULT_TYPE);
322:                broadcaster
323:                        .addNotificationListener(listener, filter, handback2);
324:
325:                clear();
326:                createNotification(broadcaster);
327:
328:                compare(sent, received(listener, handback1));
329:                compare(apply(sent, filter), received(listener, handback2));
330:                compare(EMPTY, received(listener, null));
331:            }
332:
333:            public void testOneListenerRegisteredWithTwoBroadcasters()
334:                    throws Exception {
335:                NotificationBroadcasterSupport broadcaster1 = new NotificationBroadcasterSupport();
336:                NotificationBroadcasterSupport broadcaster2 = new NotificationBroadcasterSupport();
337:
338:                Listener listener = new Listener();
339:                broadcaster1.addNotificationListener(listener, null, null);
340:                broadcaster2.addNotificationListener(listener, null, null);
341:
342:                createNotification(broadcaster1);
343:                createNotification(broadcaster2);
344:
345:                compare(sent, received(listener, null));
346:            }
347:
348:            public void testRemoveListener() throws Exception {
349:                NotificationBroadcasterSupport broadcaster = new NotificationBroadcasterSupport();
350:
351:                Listener listener = new Listener();
352:                broadcaster.addNotificationListener(listener, null, null);
353:                broadcaster.removeNotificationListener(listener);
354:
355:                createNotification(broadcaster);
356:
357:                compare(EMPTY, received(listener, null));
358:            }
359:
360:            public void testRegisteredTwiceRemoveListener() throws Exception {
361:                NotificationBroadcasterSupport broadcaster = new NotificationBroadcasterSupport();
362:
363:                Listener listener = new Listener();
364:                broadcaster.addNotificationListener(listener, null, null);
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 testRegisteredWithFilterRemoveListener()
374:                    throws Exception {
375:                NotificationBroadcasterSupport broadcaster = new NotificationBroadcasterSupport();
376:
377:                Listener listener = new Listener();
378:                NotificationFilterSupport filter = new NotificationFilterSupport();
379:                filter.enableType(DEFAULT_TYPE);
380:                broadcaster.addNotificationListener(listener, filter, null);
381:                broadcaster.removeNotificationListener(listener);
382:
383:                createNotification(broadcaster);
384:
385:                compare(EMPTY, received(listener, null));
386:            }
387:
388:            public void testRegisteredWithHandbackRemoveListener()
389:                    throws Exception {
390:                NotificationBroadcasterSupport broadcaster = new NotificationBroadcasterSupport();
391:
392:                Listener listener = new Listener();
393:                Object handback = new Object();
394:                broadcaster.addNotificationListener(listener, null, handback);
395:                broadcaster.removeNotificationListener(listener);
396:
397:                createNotification(broadcaster);
398:
399:                compare(EMPTY, received(listener, null));
400:                compare(EMPTY, received(listener, handback));
401:            }
402:
403:            public void testRegisteredWithFilterHandbackRemoveListener()
404:                    throws Exception {
405:                NotificationBroadcasterSupport broadcaster = new NotificationBroadcasterSupport();
406:
407:                Listener listener = new Listener();
408:                Object handback = new Object();
409:                NotificationFilterSupport filter = new NotificationFilterSupport();
410:                filter.enableType(DEFAULT_TYPE);
411:                broadcaster.addNotificationListener(listener, filter, 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 testRegisterRemoveListenerRegister() throws Exception {
421:                NotificationBroadcasterSupport broadcaster = new NotificationBroadcasterSupport();
422:
423:                Listener listener = new Listener();
424:                broadcaster.addNotificationListener(listener, null, null);
425:                broadcaster.removeNotificationListener(listener);
426:                broadcaster.addNotificationListener(listener, null, null);
427:
428:                createNotification(broadcaster);
429:
430:                compare(sent, received(listener, null));
431:            }
432:
433:            public void testRemoveListenerErrors() throws Exception {
434:                NotificationBroadcasterSupport broadcaster = new NotificationBroadcasterSupport();
435:
436:                Listener listener = new Listener();
437:
438:                boolean caught = false;
439:                try {
440:                    broadcaster.removeNotificationListener(null);
441:                } catch (ListenerNotFoundException e) {
442:                    caught = true;
443:                }
444:                assertTrue(
445:                        "Expected ListenerNotFoundException for null listener",
446:                        caught);
447:
448:                caught = false;
449:                try {
450:                    broadcaster.removeNotificationListener(listener);
451:                } catch (ListenerNotFoundException e) {
452:                    caught = true;
453:                }
454:                assertTrue(
455:                        "Expected ListenerNotFoundException for listener never added",
456:                        caught);
457:
458:                caught = false;
459:                try {
460:                    broadcaster.addNotificationListener(listener, null, null);
461:                    broadcaster.removeNotificationListener(listener);
462:                    broadcaster.removeNotificationListener(listener);
463:                } catch (ListenerNotFoundException e) {
464:                    caught = true;
465:                }
466:                assertTrue(
467:                        "Expected ListenerNotFoundException for listener remove twice",
468:                        caught);
469:            }
470:
471:            public void testRemoveTripletListenerOnly() throws Exception {
472:                NotificationBroadcasterSupport broadcaster = new NotificationBroadcasterSupport();
473:
474:                Listener listener = new Listener();
475:                broadcaster.addNotificationListener(listener, null, null);
476:                broadcaster.removeNotificationListener(listener, null, null);
477:
478:                createNotification(broadcaster);
479:
480:                compare(EMPTY, received(listener, null));
481:            }
482:
483:            public void testRemoveTripletListenerFilter() throws Exception {
484:                NotificationBroadcasterSupport broadcaster = new NotificationBroadcasterSupport();
485:
486:                Listener listener = new Listener();
487:                NotificationFilterSupport filter = new NotificationFilterSupport();
488:                filter.enableType(DEFAULT_TYPE);
489:                broadcaster.addNotificationListener(listener, filter, null);
490:                broadcaster.removeNotificationListener(listener, filter, null);
491:
492:                createNotification(broadcaster);
493:
494:                compare(EMPTY, received(listener, null));
495:            }
496:
497:            public void testRemoveTripletListenerHandback() throws Exception {
498:                NotificationBroadcasterSupport broadcaster = new NotificationBroadcasterSupport();
499:
500:                Listener listener = new Listener();
501:                Object handback = new Object();
502:                broadcaster.addNotificationListener(listener, null, handback);
503:                broadcaster
504:                        .removeNotificationListener(listener, null, handback);
505:
506:                createNotification(broadcaster);
507:
508:                compare(EMPTY, received(listener, null));
509:                compare(EMPTY, received(listener, handback));
510:            }
511:
512:            public void testRemoveTripletListenerFilterHandback()
513:                    throws Exception {
514:                NotificationBroadcasterSupport broadcaster = new NotificationBroadcasterSupport();
515:
516:                Listener listener = new Listener();
517:                Object handback = new Object();
518:                NotificationFilterSupport filter = new NotificationFilterSupport();
519:                filter.enableType(DEFAULT_TYPE);
520:                broadcaster.addNotificationListener(listener, filter, handback);
521:                broadcaster.removeNotificationListener(listener, filter,
522:                        handback);
523:
524:                createNotification(broadcaster);
525:
526:                compare(EMPTY, received(listener, null));
527:                compare(EMPTY, received(listener, handback));
528:            }
529:
530:            public void testRegisterTwiceRemoveOnceTriplet() throws Exception {
531:                NotificationBroadcasterSupport broadcaster = new NotificationBroadcasterSupport();
532:
533:                Listener listener = new Listener();
534:                Object handback = new Object();
535:                NotificationFilterSupport filter = new NotificationFilterSupport();
536:                filter.enableType(DEFAULT_TYPE);
537:                broadcaster.addNotificationListener(listener, filter, handback);
538:                broadcaster.addNotificationListener(listener, filter, handback);
539:                broadcaster.removeNotificationListener(listener, filter,
540:                        handback);
541:
542:                createNotification(broadcaster);
543:
544:                compare(EMPTY, received(listener, null));
545:                compare(sent, received(listener, handback));
546:            }
547:
548:            public void testRegisterTwiceRemoveTwiceTriplet() throws Exception {
549:                NotificationBroadcasterSupport broadcaster = new NotificationBroadcasterSupport();
550:
551:                Listener listener = new Listener();
552:                Object handback = new Object();
553:                NotificationFilterSupport filter = new NotificationFilterSupport();
554:                filter.enableType(DEFAULT_TYPE);
555:                broadcaster.addNotificationListener(listener, filter, handback);
556:                broadcaster.addNotificationListener(listener, filter, handback);
557:                broadcaster.removeNotificationListener(listener, filter,
558:                        handback);
559:                broadcaster.removeNotificationListener(listener, filter,
560:                        handback);
561:
562:                createNotification(broadcaster);
563:
564:                compare(EMPTY, received(listener, null));
565:                compare(EMPTY, received(listener, handback));
566:            }
567:
568:            public void testRegisterTwoRemoveOneTripletListener()
569:                    throws Exception {
570:                NotificationBroadcasterSupport broadcaster = new NotificationBroadcasterSupport();
571:
572:                Listener listener = new Listener();
573:                Object handback = new Object();
574:                NotificationFilterSupport filter = new NotificationFilterSupport();
575:                filter.enableType(DEFAULT_TYPE);
576:                broadcaster.addNotificationListener(listener, filter, handback);
577:                broadcaster.addNotificationListener(listener, null, null);
578:                broadcaster.removeNotificationListener(listener, null, null);
579:
580:                createNotification(broadcaster);
581:
582:                compare(EMPTY, received(listener, null));
583:                compare(sent, received(listener, handback));
584:            }
585:
586:            public void testRegisterTwoRemoveOneTripletListenerFilter()
587:                    throws Exception {
588:                NotificationBroadcasterSupport broadcaster = new NotificationBroadcasterSupport();
589:
590:                Listener listener = new Listener();
591:                Object handback = new Object();
592:                NotificationFilterSupport filter = new NotificationFilterSupport();
593:                filter.enableType(DEFAULT_TYPE);
594:                broadcaster.addNotificationListener(listener, filter, handback);
595:                broadcaster.addNotificationListener(listener, filter, null);
596:                broadcaster.removeNotificationListener(listener, filter, null);
597:
598:                createNotification(broadcaster);
599:
600:                compare(EMPTY, received(listener, null));
601:                compare(sent, received(listener, handback));
602:            }
603:
604:            public void testRegisterTwoRemoveOneTripletListenerHandback()
605:                    throws Exception {
606:                NotificationBroadcasterSupport broadcaster = new NotificationBroadcasterSupport();
607:
608:                Listener listener = new Listener();
609:                Object handback = new Object();
610:                NotificationFilterSupport filter = new NotificationFilterSupport();
611:                filter.enableType(DEFAULT_TYPE);
612:                broadcaster.addNotificationListener(listener, filter, handback);
613:                broadcaster.addNotificationListener(listener, null, handback);
614:                broadcaster
615:                        .removeNotificationListener(listener, null, handback);
616:
617:                createNotification(broadcaster);
618:
619:                compare(EMPTY, received(listener, null));
620:                compare(sent, received(listener, handback));
621:            }
622:
623:            public void testRegisterTwoRemoveOneTripletListenerFilterHandback()
624:                    throws Exception {
625:                NotificationBroadcasterSupport broadcaster = new NotificationBroadcasterSupport();
626:
627:                Listener listener = new Listener();
628:                Object handback = new Object();
629:                NotificationFilterSupport filter = new NotificationFilterSupport();
630:                filter.enableType(DEFAULT_TYPE);
631:                broadcaster.addNotificationListener(listener, null, null);
632:                broadcaster.addNotificationListener(listener, filter, handback);
633:                broadcaster.removeNotificationListener(listener, filter,
634:                        handback);
635:
636:                createNotification(broadcaster);
637:
638:                compare(sent, received(listener, null));
639:                compare(EMPTY, received(listener, handback));
640:            }
641:
642:            public void testRemoveTripletErrors() throws Exception {
643:                NotificationBroadcasterSupport broadcaster = new NotificationBroadcasterSupport();
644:                Object handback = new Object();
645:                NotificationFilterSupport filter = new NotificationFilterSupport();
646:                filter.enableType(DEFAULT_TYPE);
647:
648:                Listener listener = new Listener();
649:
650:                boolean caught = false;
651:                try {
652:                    broadcaster.removeNotificationListener(null, null, null);
653:                } catch (ListenerNotFoundException e) {
654:                    caught = true;
655:                }
656:                assertTrue(
657:                        "Expected ListenerNotFoundException for null listener",
658:                        caught);
659:
660:                caught = false;
661:                try {
662:                    broadcaster
663:                            .removeNotificationListener(listener, null, null);
664:                } catch (ListenerNotFoundException e) {
665:                    caught = true;
666:                }
667:                assertTrue(
668:                        "Expected ListenerNotFoundException for listener never added",
669:                        caught);
670:
671:                caught = false;
672:                try {
673:                    broadcaster.addNotificationListener(listener, null, null);
674:                    broadcaster
675:                            .removeNotificationListener(listener, null, null);
676:                    broadcaster
677:                            .removeNotificationListener(listener, null, null);
678:                } catch (ListenerNotFoundException e) {
679:                    caught = true;
680:                }
681:                assertTrue(
682:                        "Expected ListenerNotFoundException for listener remove twice",
683:                        caught);
684:
685:                caught = false;
686:                try {
687:                    broadcaster.addNotificationListener(listener, filter, null);
688:                    broadcaster.removeNotificationListener(listener,
689:                            new NotificationFilterSupport(), null);
690:                } catch (ListenerNotFoundException e) {
691:                    caught = true;
692:                    broadcaster.removeNotificationListener(listener, filter,
693:                            null);
694:                }
695:                assertTrue(
696:                        "Expected ListenerNotFoundException for wrong filter",
697:                        caught);
698:
699:                caught = false;
700:                try {
701:                    broadcaster.addNotificationListener(listener, null,
702:                            handback);
703:                    broadcaster.removeNotificationListener(listener, null,
704:                            new Object());
705:                } catch (ListenerNotFoundException e) {
706:                    caught = true;
707:                    broadcaster.removeNotificationListener(listener, null,
708:                            handback);
709:                }
710:                assertTrue(
711:                        "Expected ListenerNotFoundException for wrong handback",
712:                        caught);
713:
714:                caught = false;
715:                try {
716:                    broadcaster.addNotificationListener(listener, filter,
717:                            handback);
718:                    broadcaster.removeNotificationListener(listener,
719:                            new NotificationFilterSupport(), new Object());
720:                } catch (ListenerNotFoundException e) {
721:                    caught = true;
722:                    broadcaster.removeNotificationListener(listener, filter,
723:                            handback);
724:                }
725:                assertTrue(
726:                        "Expected ListenerNotFoundException for wrong filter and handback",
727:                        caught);
728:
729:                caught = false;
730:                try {
731:                    broadcaster.addNotificationListener(listener, filter,
732:                            handback);
733:                    broadcaster
734:                            .removeNotificationListener(listener, null, null);
735:                } catch (ListenerNotFoundException e) {
736:                    caught = true;
737:                    broadcaster.removeNotificationListener(listener, filter,
738:                            handback);
739:                }
740:                assertTrue(
741:                        "Expected ListenerNotFoundException for listener remove with wrong triplet 1",
742:                        caught);
743:
744:                caught = false;
745:                try {
746:                    broadcaster.addNotificationListener(listener, filter,
747:                            handback);
748:                    broadcaster.removeNotificationListener(listener, filter,
749:                            null);
750:                } catch (ListenerNotFoundException e) {
751:                    caught = true;
752:                    broadcaster.removeNotificationListener(listener, filter,
753:                            handback);
754:                }
755:                assertTrue(
756:                        "Expected ListenerNotFoundException for listener remove with wrong triplet 2",
757:                        caught);
758:
759:                caught = false;
760:                try {
761:                    broadcaster.addNotificationListener(listener, filter,
762:                            handback);
763:                    broadcaster.removeNotificationListener(listener, null,
764:                            handback);
765:                } catch (ListenerNotFoundException e) {
766:                    caught = true;
767:                    broadcaster.removeNotificationListener(listener, filter,
768:                            handback);
769:                }
770:                assertTrue(
771:                        "Expected ListenerNotFoundException for listener remove with wrong triplet 3",
772:                        caught);
773:
774:                caught = false;
775:                try {
776:                    broadcaster.addNotificationListener(listener, filter, null);
777:                    broadcaster
778:                            .removeNotificationListener(listener, null, null);
779:                } catch (ListenerNotFoundException e) {
780:                    caught = true;
781:                    broadcaster.removeNotificationListener(listener, filter,
782:                            null);
783:                }
784:                assertTrue(
785:                        "Expected ListenerNotFoundException for listener remove with wrong triplet 4",
786:                        caught);
787:
788:                caught = false;
789:                try {
790:                    broadcaster.addNotificationListener(listener, filter, null);
791:                    broadcaster.removeNotificationListener(listener, null,
792:                            handback);
793:                } catch (ListenerNotFoundException e) {
794:                    caught = true;
795:                    broadcaster.removeNotificationListener(listener, filter,
796:                            null);
797:                }
798:                assertTrue(
799:                        "Expected ListenerNotFoundException for listener remove with wrong triplet 5",
800:                        caught);
801:
802:                caught = false;
803:                try {
804:                    broadcaster.addNotificationListener(listener, filter, null);
805:                    broadcaster.removeNotificationListener(listener, filter,
806:                            handback);
807:                } catch (ListenerNotFoundException e) {
808:                    caught = true;
809:                    broadcaster.removeNotificationListener(listener, filter,
810:                            null);
811:                }
812:                assertTrue(
813:                        "Expected ListenerNotFoundException for listener remove with wrong triplet 6",
814:                        caught);
815:
816:                caught = false;
817:                try {
818:                    broadcaster.addNotificationListener(listener, null,
819:                            handback);
820:                    broadcaster
821:                            .removeNotificationListener(listener, null, null);
822:                } catch (ListenerNotFoundException e) {
823:                    caught = true;
824:                    broadcaster.removeNotificationListener(listener, null,
825:                            handback);
826:                }
827:                assertTrue(
828:                        "Expected ListenerNotFoundException for listener remove with wrong triplet 7",
829:                        caught);
830:
831:                caught = false;
832:                try {
833:                    broadcaster.addNotificationListener(listener, null,
834:                            handback);
835:                    broadcaster.removeNotificationListener(listener, filter,
836:                            null);
837:                } catch (ListenerNotFoundException e) {
838:                    caught = true;
839:                    broadcaster.removeNotificationListener(listener, null,
840:                            handback);
841:                }
842:                assertTrue(
843:                        "Expected ListenerNotFoundException for listener remove with wrong triplet 8",
844:                        caught);
845:
846:                caught = false;
847:                try {
848:                    broadcaster.addNotificationListener(listener, null,
849:                            handback);
850:                    broadcaster.removeNotificationListener(listener, filter,
851:                            handback);
852:                } catch (ListenerNotFoundException e) {
853:                    caught = true;
854:                    broadcaster.removeNotificationListener(listener, null,
855:                            handback);
856:                }
857:                assertTrue(
858:                        "Expected ListenerNotFoundException for listener remove with wrong triplet 9",
859:                        caught);
860:
861:                caught = false;
862:                try {
863:                    broadcaster.addNotificationListener(listener, null, null);
864:                    broadcaster.removeNotificationListener(listener, filter,
865:                            null);
866:                } catch (ListenerNotFoundException e) {
867:                    caught = true;
868:                    broadcaster
869:                            .removeNotificationListener(listener, null, null);
870:                }
871:                assertTrue(
872:                        "Expected ListenerNotFoundException for listener remove with wrong triplet 10",
873:                        caught);
874:
875:                caught = false;
876:                try {
877:                    broadcaster.addNotificationListener(listener, null, null);
878:                    broadcaster.removeNotificationListener(listener, null,
879:                            handback);
880:                } catch (ListenerNotFoundException e) {
881:                    caught = true;
882:                    broadcaster
883:                            .removeNotificationListener(listener, null, null);
884:                }
885:                assertTrue(
886:                        "Expected ListenerNotFoundException for listener remove with wrong triplet 11",
887:                        caught);
888:
889:                caught = false;
890:                try {
891:                    broadcaster.addNotificationListener(listener, null, null);
892:                    broadcaster.removeNotificationListener(listener, filter,
893:                            handback);
894:                } catch (ListenerNotFoundException e) {
895:                    caught = true;
896:                    broadcaster
897:                            .removeNotificationListener(listener, null, null);
898:                }
899:                assertTrue(
900:                        "Expected ListenerNotFoundException for listener remove with wrong triplet 12",
901:                        caught);
902:            }
903:
904:            // Support -------------------------------------------------------------------
905:
906:            private void createNotification(
907:                    NotificationBroadcasterSupport broadcaster) {
908:                createNotification(broadcaster, DEFAULT_TYPE);
909:            }
910:
911:            private void createNotification(
912:                    NotificationBroadcasterSupport broadcaster, String type) {
913:                synchronized (this ) {
914:                    sequence++;
915:                }
916:                Notification notification = new Notification(type, broadcaster,
917:                        sequence);
918:                sent.add(notification);
919:                broadcaster.sendNotification(notification);
920:            }
921:
922:            private void clear() {
923:                sent.clear();
924:            }
925:
926:            private ArrayList apply(ArrayList sent,
927:                    NotificationFilterSupport filter) {
928:                ArrayList result = new ArrayList();
929:                for (Iterator iterator = sent.iterator(); iterator.hasNext();) {
930:                    Notification notification = (Notification) iterator.next();
931:                    if (filter.isNotificationEnabled(notification))
932:                        result.add(notification);
933:                }
934:                return result;
935:            }
936:
937:            private ArrayList received(Listener listener, Object object) {
938:                ArrayList result = (ArrayList) listener.notifications
939:                        .get(object);
940:                if (result == null)
941:                    result = EMPTY;
942:                return result;
943:            }
944:
945:            private void compare(ArrayList passedSent, ArrayList passedReceived)
946:                    throws Exception {
947:                ArrayList sent = new ArrayList(passedSent);
948:                ArrayList received = new ArrayList(passedReceived);
949:
950:                for (Iterator iterator = sent.iterator(); iterator.hasNext();) {
951:                    Notification notification = (Notification) iterator.next();
952:                    boolean found = received.remove(notification);
953:                    assertTrue("Expected notification " + notification, found);
954:                }
955:
956:                assertTrue("Didn't expect notification(s) " + received,
957:                        received.isEmpty());
958:            }
959:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.