1 8 9 package test.javax.management; 10 11 import java.util.ArrayList ; 12 import java.util.List ; 13 14 import javax.management.ListenerNotFoundException ; 15 import javax.management.MBeanServer ; 16 import javax.management.Notification ; 17 import javax.management.NotificationBroadcasterSupport ; 18 import javax.management.NotificationFilter ; 19 import javax.management.NotificationListener ; 20 import javax.management.ObjectName ; 21 22 import test.MX4JTestCase; 23 import test.MutableInteger; 24 import test.MutableObject; 25 26 29 public class NotificationListenerTest extends MX4JTestCase 30 { 31 public NotificationListenerTest(String name) 32 { 33 super(name); 34 } 35 36 public void testAddRemoveOneListenerOnOneMBean() throws Exception 37 { 38 MBeanServer server = newMBeanServer(); 39 IdentityEmitter emitter = new IdentityEmitter(); 40 ObjectName objectName = ObjectName.getInstance("test:type=emitter"); 41 server.registerMBean(emitter, objectName); 42 43 final MutableObject source = new MutableObject(null); 44 NotificationListener listener = new NotificationListener () 45 { 46 public void handleNotification(Notification notification, Object handback) 47 { 48 source.set(notification.getSource()); 49 } 50 }; 51 52 server.addNotificationListener(objectName, listener, null, null); 53 assertEquals(emitter.getNotificationListeners().size(), 1); 54 55 Notification notification = new Notification ("type", emitter, 0); 56 emitter.sendNotification(notification); 57 assertEquals(objectName, source.get()); 58 59 server.removeNotificationListener(objectName, listener, null, null); 60 assertEquals(emitter.getNotificationListeners().size(), 0); 61 } 62 63 public void testAddRemoveOneListenerTwiceOnOneMBean() throws Exception 64 { 65 MBeanServer server = newMBeanServer(); 66 IdentityEmitter emitter = new IdentityEmitter(); 67 ObjectName objectName = ObjectName.getInstance("test:type=emitter"); 68 server.registerMBean(emitter, objectName); 69 70 final MutableInteger count = new MutableInteger(0); 71 final MutableObject source = new MutableObject(null); 72 NotificationListener listener = new NotificationListener () 73 { 74 public void handleNotification(Notification notification, Object handback) 75 { 76 source.set(notification.getSource()); 77 count.set(count.get() + 1); 78 } 79 }; 80 81 Object handback = new Object (); 83 server.addNotificationListener(objectName, listener, null, null); 84 server.addNotificationListener(objectName, listener, null, handback); 85 assertEquals(emitter.getNotificationListeners().size(), 2); 86 87 Notification notification = new Notification ("type", emitter, 0); 88 emitter.sendNotification(notification); 89 assertEquals(objectName, source.get()); 90 assertEquals(count.get(), 2); 91 92 server.removeNotificationListener(objectName, listener, null, null); 93 assertEquals(emitter.getNotificationListeners().size(), 1); 94 95 server.removeNotificationListener(objectName, listener, null, handback); 96 assertEquals(emitter.getNotificationListeners().size(), 0); 97 } 98 99 public void testAddRemoveTwoListenersOnOneMBean() throws Exception 100 { 101 MBeanServer server = newMBeanServer(); 102 IdentityEmitter emitter = new IdentityEmitter(); 103 ObjectName objectName = ObjectName.getInstance("test:type=emitter"); 104 server.registerMBean(emitter, objectName); 105 106 NotificationListener listener1 = new NotificationListener () 107 { 108 public void handleNotification(Notification notification, Object handback) 109 { 110 } 111 }; 112 113 NotificationListener listener2 = new NotificationListener () 114 { 115 public void handleNotification(Notification notification, Object handback) 116 { 117 } 118 }; 119 120 server.addNotificationListener(objectName, listener1, null, null); 121 server.addNotificationListener(objectName, listener2, null, null); 122 assertEquals(emitter.getNotificationListeners().size(), 2); 123 124 server.removeNotificationListener(objectName, listener1, null, null); 125 assertEquals(emitter.getNotificationListeners().size(), 1); 126 127 server.removeNotificationListener(objectName, listener2, null, null); 128 assertEquals(emitter.getNotificationListeners().size(), 0); 129 } 130 131 public void testAddRemoveOneListenerOnTwoMBeans() throws Exception 132 { 133 MBeanServer server = newMBeanServer(); 134 IdentityEmitter emitter1 = new IdentityEmitter(); 135 ObjectName objectName1 = ObjectName.getInstance("test:type=emitter1"); 136 server.registerMBean(emitter1, objectName1); 137 IdentityEmitter emitter2 = new IdentityEmitter(); 138 ObjectName objectName2 = ObjectName.getInstance("test:type=emitter2"); 139 server.registerMBean(emitter2, objectName2); 140 141 NotificationListener listener = new NotificationListener () 142 { 143 public void handleNotification(Notification notification, Object handback) 144 { 145 } 146 }; 147 148 server.addNotificationListener(objectName1, listener, null, null); 149 assertEquals(emitter1.getNotificationListeners().size(), 1); 150 assertEquals(emitter2.getNotificationListeners().size(), 0); 151 152 server.addNotificationListener(objectName2, listener, null, null); 153 assertEquals(emitter1.getNotificationListeners().size(), 1); 154 assertEquals(emitter2.getNotificationListeners().size(), 1); 155 156 server.removeNotificationListener(objectName1, listener, null, null); 157 assertEquals(emitter1.getNotificationListeners().size(), 0); 158 assertEquals(emitter2.getNotificationListeners().size(), 1); 159 160 server.removeNotificationListener(objectName2, listener, null, null); 161 assertEquals(emitter1.getNotificationListeners().size(), 0); 162 assertEquals(emitter2.getNotificationListeners().size(), 0); 163 } 164 165 public interface IdentityEmitterMBean 166 { 167 } 168 169 public static class IdentityEmitter extends NotificationBroadcasterSupport implements IdentityEmitterMBean 170 { 171 private List listeners = new ArrayList (); 172 173 public List getNotificationListeners() 174 { 175 return listeners; 176 } 177 178 public void addNotificationListener(NotificationListener listener, NotificationFilter filter, Object handback) 179 { 180 super.addNotificationListener(listener, filter, handback); 181 listeners.add(listener); 182 } 183 184 public void removeNotificationListener(NotificationListener listener, NotificationFilter filter, Object handback) throws ListenerNotFoundException 185 { 186 NotificationListener [] listens = (NotificationListener [])listeners.toArray(new NotificationListener [0]); 187 for (int i = 0; i < listens.length; i++) 188 { 189 NotificationListener listen = listens[i]; 190 if (listen == listener) 191 { 192 super.removeNotificationListener(listener, filter, handback); 193 listeners.remove(listener); 194 return; 195 } 196 } 197 throw new ListenerNotFoundException (); 198 } 199 } 200 } 201 | Popular Tags |