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: }
|