1 7 8 package test.compliance.server; 9 10 import java.util.ArrayList; 11 12 import javax.management.MBeanNotificationInfo; 13 import javax.management.MBeanServer; 14 import javax.management.MBeanServerFactory; 15 import javax.management.MBeanServerInvocationHandler; 16 import javax.management.Notification; 17 import javax.management.NotificationEmitter; 18 import javax.management.NotificationFilterSupport; 19 import javax.management.NotificationListener; 20 import javax.management.ObjectName; 21 22 import junit.framework.TestCase; 23 import test.compliance.server.support.BroadcasterInvocationHandlerTest; 24 import test.compliance.server.support.EmitterInvocationHandlerTest; 25 import test.compliance.server.support.InvocationHandlerTest; 26 import test.compliance.server.support.InvocationHandlerTestMBean; 27 import test.compliance.server.support.ObjectInvocationHandlerTest; 28 29 34 public class MBeanServerInvocationHandlerTestCase 35 extends TestCase 36 implements NotificationListener 37 { 38 40 private ObjectName invocationHandlerTestName; 41 42 private ArrayList messages = new ArrayList(); 43 44 46 49 public MBeanServerInvocationHandlerTestCase(String s) 50 { 51 super(s); 52 53 try 54 { 55 invocationHandlerTestName = new ObjectName("MBeanServerInvocationHandlerTestCase:type=InvocationHandlerTest"); 56 } 57 catch (Exception e) 58 { 59 throw new RuntimeException(e.toString()); 60 } 61 } 62 63 65 public void testConstructor() 66 throws Exception 67 { 68 MBeanServer server = MBeanServerFactory.newMBeanServer(); 69 } 70 71 public void testGetter() 72 throws Exception 73 { 74 MBeanServer server = MBeanServerFactory.newMBeanServer(); 75 InvocationHandlerTest test = new InvocationHandlerTest(); 76 server.registerMBean(test, invocationHandlerTestName); 77 78 InvocationHandlerTestMBean proxy = (InvocationHandlerTestMBean) MBeanServerInvocationHandler.newProxyInstance( 79 server, invocationHandlerTestName, InvocationHandlerTestMBean.class, false); 80 assertEquals("Attribute", proxy.getAttribute()); 81 } 82 83 public void testSetter() 84 throws Exception 85 { 86 MBeanServer server = MBeanServerFactory.newMBeanServer(); 87 InvocationHandlerTest test = new InvocationHandlerTest(); 88 server.registerMBean(test, invocationHandlerTestName); 89 InvocationHandlerTestMBean proxy = (InvocationHandlerTestMBean) MBeanServerInvocationHandler.newProxyInstance( 90 server, invocationHandlerTestName, InvocationHandlerTestMBean.class, false); 91 92 proxy.setAttribute("Changed"); 93 assertEquals("Changed", test.getAttribute()); 94 } 95 96 public void testGetterPrimitiveBoolean() 97 throws Exception 98 { 99 MBeanServer server = MBeanServerFactory.newMBeanServer(); 100 InvocationHandlerTest test = new InvocationHandlerTest(); 101 server.registerMBean(test, invocationHandlerTestName); 102 103 InvocationHandlerTestMBean proxy = (InvocationHandlerTestMBean) MBeanServerInvocationHandler.newProxyInstance( 104 server, invocationHandlerTestName, InvocationHandlerTestMBean.class, false); 105 106 assertEquals(false, test.isIsPrimitive()); 107 test.setIsPrimitive(true); 108 109 assertEquals(true, proxy.isIsPrimitive()); 110 } 111 112 public void testSetterPrimitiveBoolean() 113 throws Exception 114 { 115 MBeanServer server = MBeanServerFactory.newMBeanServer(); 116 InvocationHandlerTest test = new InvocationHandlerTest(); 117 server.registerMBean(test, invocationHandlerTestName); 118 InvocationHandlerTestMBean proxy = (InvocationHandlerTestMBean) MBeanServerInvocationHandler.newProxyInstance( 119 server, invocationHandlerTestName, InvocationHandlerTestMBean.class, false); 120 121 assertEquals(false, test.isIsPrimitive()); 122 proxy.setIsPrimitive(true); 123 124 assertEquals(true, test.isIsPrimitive()); 125 } 126 127 public void testGetterTypeBoolean() 128 throws Exception 129 { 130 MBeanServer server = MBeanServerFactory.newMBeanServer(); 131 InvocationHandlerTest test = new InvocationHandlerTest(); 132 server.registerMBean(test, invocationHandlerTestName); 133 134 InvocationHandlerTestMBean proxy = (InvocationHandlerTestMBean) MBeanServerInvocationHandler.newProxyInstance( 135 server, invocationHandlerTestName, InvocationHandlerTestMBean.class, false); 136 137 assertEquals(null, test.isIsType()); 138 test.setIsType(new Boolean(true)); 139 140 assertEquals(true, proxy.isIsType().booleanValue()); 141 } 142 143 public void testSetterTypeBoolean() 144 throws Exception 145 { 146 MBeanServer server = MBeanServerFactory.newMBeanServer(); 147 InvocationHandlerTest test = new InvocationHandlerTest(); 148 server.registerMBean(test, invocationHandlerTestName); 149 InvocationHandlerTestMBean proxy = (InvocationHandlerTestMBean) MBeanServerInvocationHandler.newProxyInstance( 150 server, invocationHandlerTestName, InvocationHandlerTestMBean.class, false); 151 152 assertEquals(null, test.isIsType()); 153 proxy.setIsType(new Boolean(true)); 154 155 assertEquals(true, test.isIsType().booleanValue()); 156 } 157 158 public void testInvokeNoArgsNoReturn() 159 throws Exception 160 { 161 MBeanServer server = MBeanServerFactory.newMBeanServer(); 162 InvocationHandlerTest test = new InvocationHandlerTest(); 163 server.registerMBean(test, invocationHandlerTestName); 164 InvocationHandlerTestMBean proxy = (InvocationHandlerTestMBean) MBeanServerInvocationHandler.newProxyInstance( 165 server, invocationHandlerTestName, InvocationHandlerTestMBean.class, false); 166 167 proxy.invokeNoArgsNoReturn(); 168 assertTrue(test.invokeNoArgsNoReturnInvoked); 169 } 170 171 public void testInvokeNoArgs() 172 throws Exception 173 { 174 MBeanServer server = MBeanServerFactory.newMBeanServer(); 175 InvocationHandlerTest test = new InvocationHandlerTest(); 176 server.registerMBean(test, invocationHandlerTestName); 177 InvocationHandlerTestMBean proxy = (InvocationHandlerTestMBean) MBeanServerInvocationHandler.newProxyInstance( 178 server, invocationHandlerTestName, InvocationHandlerTestMBean.class, false); 179 180 assertEquals("invokeNoArgs", proxy.invokeNoArgs()); 181 } 182 183 public void testInvoke() 184 throws Exception 185 { 186 MBeanServer server = MBeanServerFactory.newMBeanServer(); 187 InvocationHandlerTest test = new InvocationHandlerTest(); 188 server.registerMBean(test, invocationHandlerTestName); 189 InvocationHandlerTestMBean proxy = (InvocationHandlerTestMBean) MBeanServerInvocationHandler.newProxyInstance( 190 server, invocationHandlerTestName, InvocationHandlerTestMBean.class, false); 191 192 assertEquals("parameter", proxy.invoke("parameter")); 193 } 194 195 public void testInvokeMixedParameters() 196 throws Exception 197 { 198 MBeanServer server = MBeanServerFactory.newMBeanServer(); 199 InvocationHandlerTest test = new InvocationHandlerTest(); 200 server.registerMBean(test, invocationHandlerTestName); 201 InvocationHandlerTestMBean proxy = (InvocationHandlerTestMBean) MBeanServerInvocationHandler.newProxyInstance( 202 server, invocationHandlerTestName, InvocationHandlerTestMBean.class, false); 203 204 Integer parameter = new Integer(20); 205 assertEquals(parameter, proxy.invokeMixedParameters("parameter", 10, parameter)); 206 } 207 208 public void testNotificationEmitterAdd() 209 throws Exception 210 { 211 MBeanServer server = MBeanServerFactory.newMBeanServer(); 212 EmitterInvocationHandlerTest test = new EmitterInvocationHandlerTest(); 213 server.registerMBean(test, invocationHandlerTestName); 214 NotificationEmitter proxy = (NotificationEmitter) MBeanServerInvocationHandler.newProxyInstance( 215 server, invocationHandlerTestName, InvocationHandlerTestMBean.class, true); 216 217 proxy.addNotificationListener(this, null, null); 218 219 messages.clear(); 220 test.sendNotification(); 221 assertTrue(messages.size() == 1); 222 } 223 224 public void testNotificationEmitterRemove() 225 throws Exception 226 { 227 MBeanServer server = MBeanServerFactory.newMBeanServer(); 228 EmitterInvocationHandlerTest test = new EmitterInvocationHandlerTest(); 229 server.registerMBean(test, invocationHandlerTestName); 230 NotificationEmitter proxy = (NotificationEmitter) MBeanServerInvocationHandler.newProxyInstance( 231 server, invocationHandlerTestName, InvocationHandlerTestMBean.class, true); 232 233 proxy.addNotificationListener(this, null, null); 234 235 messages.clear(); 236 test.sendNotification(); 237 assertTrue(messages.size() == 1); 238 239 proxy.removeNotificationListener(this); 240 241 messages.clear(); 242 test.sendNotification(); 243 assertTrue(messages.size() == 0); 244 } 245 246 public void testNotificationEmitterRemoveTriplet() 247 throws Exception 248 { 249 MBeanServer server = MBeanServerFactory.newMBeanServer(); 250 EmitterInvocationHandlerTest test = new EmitterInvocationHandlerTest(); 251 server.registerMBean(test, invocationHandlerTestName); 252 NotificationEmitter proxy = (NotificationEmitter) MBeanServerInvocationHandler.newProxyInstance( 253 server, invocationHandlerTestName, InvocationHandlerTestMBean.class, true); 254 255 NotificationFilterSupport filter = new NotificationFilterSupport(); 256 filter.enableType("test"); 257 Object handback = new Object(); 258 proxy.addNotificationListener(this, filter, handback); 259 260 messages.clear(); 261 test.sendNotification(); 262 assertTrue(messages.size() == 1); 263 264 proxy.removeNotificationListener(this, filter, handback); 265 266 messages.clear(); 267 test.sendNotification(); 268 assertTrue(messages.size() == 0); 269 } 270 271 public void testNotificationEmitterRemoveTripletFailsOnBroadcaster() 272 throws Exception 273 { 274 MBeanServer server = MBeanServerFactory.newMBeanServer(); 275 BroadcasterInvocationHandlerTest test = new BroadcasterInvocationHandlerTest(); 276 server.registerMBean(test, invocationHandlerTestName); 277 NotificationEmitter proxy = (NotificationEmitter) MBeanServerInvocationHandler.newProxyInstance( 278 server, invocationHandlerTestName, InvocationHandlerTestMBean.class, true); 279 280 NotificationFilterSupport filter = new NotificationFilterSupport(); 281 filter.enableType("test"); 282 Object handback = new Object(); 283 proxy.addNotificationListener(this, filter, handback); 284 285 messages.clear(); 286 test.sendNotification(); 287 assertTrue(messages.size() == 1); 288 289 try 290 { 291 proxy.removeNotificationListener(this, filter, handback); 292 fail("removeNotificationListener(NotificationListener, NotificationFilter, Object) " + 293 "should not work for a broadcaster"); 294 } 295 catch (Exception ignored) 296 { 297 } 298 } 299 300 public void testGetNotificationInfo() 301 throws Exception 302 { 303 MBeanServer server = MBeanServerFactory.newMBeanServer(); 304 EmitterInvocationHandlerTest test = new EmitterInvocationHandlerTest(); 305 server.registerMBean(test, invocationHandlerTestName); 306 NotificationEmitter proxy = (NotificationEmitter) MBeanServerInvocationHandler.newProxyInstance( 307 server, invocationHandlerTestName, InvocationHandlerTestMBean.class, true); 308 309 MBeanNotificationInfo[] info = proxy.getNotificationInfo(); 310 assertEquals("test", info[0].getNotifTypes()[0]); 311 } 312 313 public void testToString() 314 throws Exception 315 { 316 MBeanServer server = MBeanServerFactory.newMBeanServer(); 317 ObjectInvocationHandlerTest test = new ObjectInvocationHandlerTest(); 318 server.registerMBean(test, invocationHandlerTestName); 319 InvocationHandlerTestMBean proxy = (InvocationHandlerTestMBean) MBeanServerInvocationHandler.newProxyInstance( 320 server, invocationHandlerTestName, InvocationHandlerTestMBean.class, true); 321 322 assertEquals("TOSTRING", proxy.toString()); 323 } 324 325 public void testToStringFailsWhenNotExposed() 326 throws Exception 327 { 328 MBeanServer server = MBeanServerFactory.newMBeanServer(); 329 InvocationHandlerTest test = new InvocationHandlerTest(); 330 server.registerMBean(test, invocationHandlerTestName); 331 InvocationHandlerTestMBean proxy = (InvocationHandlerTestMBean) MBeanServerInvocationHandler.newProxyInstance( 332 server, invocationHandlerTestName, InvocationHandlerTestMBean.class, true); 333 334 try 335 { 336 proxy.toString(); 337 fail("toString() should not work when it is not exposed for management"); 338 } 339 catch (Exception ignored) 340 { 341 } 342 } 343 344 public void testEquals() 345 throws Exception 346 { 347 MBeanServer server = MBeanServerFactory.newMBeanServer(); 348 ObjectInvocationHandlerTest test = new ObjectInvocationHandlerTest(); 349 server.registerMBean(test, invocationHandlerTestName); 350 InvocationHandlerTestMBean proxy = (InvocationHandlerTestMBean) MBeanServerInvocationHandler.newProxyInstance( 351 server, invocationHandlerTestName, InvocationHandlerTestMBean.class, true); 352 353 assertTrue(proxy.equals(new Object())); 354 } 355 356 public void testEqualsFailsWhenNotExposed() 357 throws Exception 358 { 359 MBeanServer server = MBeanServerFactory.newMBeanServer(); 360 InvocationHandlerTest test = new InvocationHandlerTest(); 361 server.registerMBean(test, invocationHandlerTestName); 362 InvocationHandlerTestMBean proxy = (InvocationHandlerTestMBean) MBeanServerInvocationHandler.newProxyInstance( 363 server, invocationHandlerTestName, InvocationHandlerTestMBean.class, true); 364 365 try 366 { 367 proxy.equals(new Object()); 368 fail("equals(Object) should not work when it is not exposed for management"); 369 } 370 catch (Exception ignored) 371 { 372 } 373 } 374 375 public void testHashCode() 376 throws Exception 377 { 378 MBeanServer server = MBeanServerFactory.newMBeanServer(); 379 ObjectInvocationHandlerTest test = new ObjectInvocationHandlerTest(); 380 server.registerMBean(test, invocationHandlerTestName); 381 InvocationHandlerTestMBean proxy = (InvocationHandlerTestMBean) MBeanServerInvocationHandler.newProxyInstance( 382 server, invocationHandlerTestName, InvocationHandlerTestMBean.class, true); 383 384 assertEquals(1234, proxy.hashCode()); 385 } 386 387 public void testHashCodeFailsWhenNotExposed() 388 throws Exception 389 { 390 MBeanServer server = MBeanServerFactory.newMBeanServer(); 391 InvocationHandlerTest test = new InvocationHandlerTest(); 392 server.registerMBean(test, invocationHandlerTestName); 393 InvocationHandlerTestMBean proxy = (InvocationHandlerTestMBean) MBeanServerInvocationHandler.newProxyInstance( 394 server, invocationHandlerTestName, InvocationHandlerTestMBean.class, true); 395 396 try 397 { 398 proxy.hashCode(); 399 fail("hashCode() should not work when it is not exposed for management"); 400 } 401 catch (Exception ignored) 402 { 403 } 404 } 405 406 408 public void handleNotification(Notification notification, Object handback) 409 { 410 messages.add(notification); 411 } 412 } 413 | Popular Tags |