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