Source Code Cross Referenced for NotificationListenerTest.java in  » JMX » mx4j » test » javax » management » 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 » JMX » mx4j » test.javax.management 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright (C) The MX4J Contributors.
003:         * All rights reserved.
004:         *
005:         * This software is distributed under the terms of the MX4J License version 1.0.
006:         * See the terms of the MX4J License in the documentation provided with this software.
007:         */
008:
009:        package test.javax.management;
010:
011:        import java.util.ArrayList;
012:        import java.util.List;
013:
014:        import javax.management.ListenerNotFoundException;
015:        import javax.management.MBeanServer;
016:        import javax.management.Notification;
017:        import javax.management.NotificationBroadcasterSupport;
018:        import javax.management.NotificationFilter;
019:        import javax.management.NotificationListener;
020:        import javax.management.ObjectName;
021:
022:        import test.MX4JTestCase;
023:        import test.MutableInteger;
024:        import test.MutableObject;
025:
026:        /**
027:         * @version $Revision: 1.1 $
028:         */
029:        public class NotificationListenerTest extends MX4JTestCase {
030:            public NotificationListenerTest(String name) {
031:                super (name);
032:            }
033:
034:            public void testAddRemoveOneListenerOnOneMBean() throws Exception {
035:                MBeanServer server = newMBeanServer();
036:                IdentityEmitter emitter = new IdentityEmitter();
037:                ObjectName objectName = ObjectName
038:                        .getInstance("test:type=emitter");
039:                server.registerMBean(emitter, objectName);
040:
041:                final MutableObject source = new MutableObject(null);
042:                NotificationListener listener = new NotificationListener() {
043:                    public void handleNotification(Notification notification,
044:                            Object handback) {
045:                        source.set(notification.getSource());
046:                    }
047:                };
048:
049:                server
050:                        .addNotificationListener(objectName, listener, null,
051:                                null);
052:                assertEquals(emitter.getNotificationListeners().size(), 1);
053:
054:                Notification notification = new Notification("type", emitter, 0);
055:                emitter.sendNotification(notification);
056:                assertEquals(objectName, source.get());
057:
058:                server.removeNotificationListener(objectName, listener, null,
059:                        null);
060:                assertEquals(emitter.getNotificationListeners().size(), 0);
061:            }
062:
063:            public void testAddRemoveOneListenerTwiceOnOneMBean()
064:                    throws Exception {
065:                MBeanServer server = newMBeanServer();
066:                IdentityEmitter emitter = new IdentityEmitter();
067:                ObjectName objectName = ObjectName
068:                        .getInstance("test:type=emitter");
069:                server.registerMBean(emitter, objectName);
070:
071:                final MutableInteger count = new MutableInteger(0);
072:                final MutableObject source = new MutableObject(null);
073:                NotificationListener listener = new NotificationListener() {
074:                    public void handleNotification(Notification notification,
075:                            Object handback) {
076:                        source.set(notification.getSource());
077:                        count.set(count.get() + 1);
078:                    }
079:                };
080:
081:                // Add same listener twice, with different handbacks
082:                Object handback = new Object();
083:                server
084:                        .addNotificationListener(objectName, listener, null,
085:                                null);
086:                server.addNotificationListener(objectName, listener, null,
087:                        handback);
088:                assertEquals(emitter.getNotificationListeners().size(), 2);
089:
090:                Notification notification = new Notification("type", emitter, 0);
091:                emitter.sendNotification(notification);
092:                assertEquals(objectName, source.get());
093:                assertEquals(count.get(), 2);
094:
095:                server.removeNotificationListener(objectName, listener, null,
096:                        null);
097:                assertEquals(emitter.getNotificationListeners().size(), 1);
098:
099:                server.removeNotificationListener(objectName, listener, null,
100:                        handback);
101:                assertEquals(emitter.getNotificationListeners().size(), 0);
102:            }
103:
104:            public void testAddRemoveTwoListenersOnOneMBean() throws Exception {
105:                MBeanServer server = newMBeanServer();
106:                IdentityEmitter emitter = new IdentityEmitter();
107:                ObjectName objectName = ObjectName
108:                        .getInstance("test:type=emitter");
109:                server.registerMBean(emitter, objectName);
110:
111:                NotificationListener listener1 = new NotificationListener() {
112:                    public void handleNotification(Notification notification,
113:                            Object handback) {
114:                    }
115:                };
116:
117:                NotificationListener listener2 = new NotificationListener() {
118:                    public void handleNotification(Notification notification,
119:                            Object handback) {
120:                    }
121:                };
122:
123:                server.addNotificationListener(objectName, listener1, null,
124:                        null);
125:                server.addNotificationListener(objectName, listener2, null,
126:                        null);
127:                assertEquals(emitter.getNotificationListeners().size(), 2);
128:
129:                server.removeNotificationListener(objectName, listener1, null,
130:                        null);
131:                assertEquals(emitter.getNotificationListeners().size(), 1);
132:
133:                server.removeNotificationListener(objectName, listener2, null,
134:                        null);
135:                assertEquals(emitter.getNotificationListeners().size(), 0);
136:            }
137:
138:            public void testAddRemoveOneListenerOnTwoMBeans() throws Exception {
139:                MBeanServer server = newMBeanServer();
140:                IdentityEmitter emitter1 = new IdentityEmitter();
141:                ObjectName objectName1 = ObjectName
142:                        .getInstance("test:type=emitter1");
143:                server.registerMBean(emitter1, objectName1);
144:                IdentityEmitter emitter2 = new IdentityEmitter();
145:                ObjectName objectName2 = ObjectName
146:                        .getInstance("test:type=emitter2");
147:                server.registerMBean(emitter2, objectName2);
148:
149:                NotificationListener listener = new NotificationListener() {
150:                    public void handleNotification(Notification notification,
151:                            Object handback) {
152:                    }
153:                };
154:
155:                server.addNotificationListener(objectName1, listener, null,
156:                        null);
157:                assertEquals(emitter1.getNotificationListeners().size(), 1);
158:                assertEquals(emitter2.getNotificationListeners().size(), 0);
159:
160:                server.addNotificationListener(objectName2, listener, null,
161:                        null);
162:                assertEquals(emitter1.getNotificationListeners().size(), 1);
163:                assertEquals(emitter2.getNotificationListeners().size(), 1);
164:
165:                server.removeNotificationListener(objectName1, listener, null,
166:                        null);
167:                assertEquals(emitter1.getNotificationListeners().size(), 0);
168:                assertEquals(emitter2.getNotificationListeners().size(), 1);
169:
170:                server.removeNotificationListener(objectName2, listener, null,
171:                        null);
172:                assertEquals(emitter1.getNotificationListeners().size(), 0);
173:                assertEquals(emitter2.getNotificationListeners().size(), 0);
174:            }
175:
176:            public interface IdentityEmitterMBean {
177:            }
178:
179:            public static class IdentityEmitter extends
180:                    NotificationBroadcasterSupport implements 
181:                    IdentityEmitterMBean {
182:                private List listeners = new ArrayList();
183:
184:                public List getNotificationListeners() {
185:                    return listeners;
186:                }
187:
188:                public void addNotificationListener(
189:                        NotificationListener listener,
190:                        NotificationFilter filter, Object handback) {
191:                    super .addNotificationListener(listener, filter, handback);
192:                    listeners.add(listener);
193:                }
194:
195:                public void removeNotificationListener(
196:                        NotificationListener listener,
197:                        NotificationFilter filter, Object handback)
198:                        throws ListenerNotFoundException {
199:                    NotificationListener[] listens = (NotificationListener[]) listeners
200:                            .toArray(new NotificationListener[0]);
201:                    for (int i = 0; i < listens.length; i++) {
202:                        NotificationListener listen = listens[i];
203:                        if (listen == listener) {
204:                            super .removeNotificationListener(listener, filter,
205:                                    handback);
206:                            listeners.remove(listener);
207:                            return;
208:                        }
209:                    }
210:                    throw new ListenerNotFoundException();
211:                }
212:            }
213:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.