1 22 package test.implementation.util; 23 24 import java.util.Set ; 25 26 import java.lang.reflect.Method ; 27 28 import javax.management.Attribute ; 29 import javax.management.AttributeNotFoundException ; 30 import javax.management.DynamicMBean ; 31 import javax.management.InstanceNotFoundException ; 32 import javax.management.MBeanServer ; 33 import javax.management.MBeanServerFactory ; 34 import javax.management.ObjectInstance ; 35 import javax.management.ObjectName ; 36 37 import javax.management.modelmbean.ModelMBean ; 38 import javax.management.modelmbean.RequiredModelMBean ; 39 40 import test.implementation.util.support.Trivial; 41 import test.implementation.util.support.TrivialMBean; 42 import test.implementation.util.support.Trivial2; 43 import test.implementation.util.support.Trivial2MBean; 44 import test.implementation.util.support.ExtendedResource; 45 import test.implementation.util.support.MyInterface; 46 import test.implementation.util.support.MyInterface2; 47 import test.implementation.util.support.Resource; 48 import test.implementation.util.support.ResourceOverride; 49 import test.implementation.util.support.ResourceIncorrectInfo; 50 51 import junit.framework.TestCase; 52 53 import org.jboss.mx.util.AgentID; 54 import org.jboss.mx.util.DefaultExceptionHandler; 55 import org.jboss.mx.util.MBeanProxy; 56 import org.jboss.mx.util.ProxyContext; 57 58 import org.jboss.mx.modelmbean.XMBean; 59 60 61 67 public class MBeanProxyTEST extends TestCase 68 { 69 public MBeanProxyTEST(String s) 70 { 71 super(s); 72 } 73 74 public void testGetWithServer() throws Exception 75 { 76 MBeanServer server = MBeanServerFactory.createMBeanServer(); 77 ObjectName oname = new ObjectName ("test:name=test"); 78 79 server.registerMBean(new Trivial(), oname); 80 81 TrivialMBean mbean = (TrivialMBean)MBeanProxy.get( 82 TrivialMBean.class, oname, server); 83 84 mbean.doOperation(); 85 } 86 87 public void testGetWithAgentID() throws Exception 88 { 89 MBeanServer server = MBeanServerFactory.createMBeanServer(); 90 String agentID = AgentID.get(server); 91 ObjectName oname = new ObjectName ("test:name=test"); 92 93 server.registerMBean(new Trivial(), oname); 94 95 TrivialMBean mbean = (TrivialMBean)MBeanProxy.get( 96 TrivialMBean.class, oname, agentID); 97 98 mbean.doOperation(); 99 } 100 101 public void testCreateWithServer() throws Exception 102 { 103 MBeanServer server = MBeanServerFactory.createMBeanServer(); 104 ObjectName oname = new ObjectName ("test:test=test"); 105 106 TrivialMBean mbean = (TrivialMBean)MBeanProxy.create( 107 Trivial.class, TrivialMBean.class, oname, server); 108 109 mbean.doOperation(); 110 } 111 112 public void testCreateWithAgentID() throws Exception 113 { 114 MBeanServer server = MBeanServerFactory.createMBeanServer(); 115 ObjectName oname = new ObjectName ("test:test=test"); 116 String agentID = AgentID.get(server); 117 118 TrivialMBean mbean = (TrivialMBean)MBeanProxy.create( 119 Trivial.class, TrivialMBean.class, oname, agentID); 120 121 mbean.doOperation(); 122 } 123 124 public void testProxyInvocations() throws Exception 125 { 126 MBeanServer server = MBeanServerFactory.createMBeanServer(); 127 ObjectName oname = new ObjectName ("test:name=test"); 128 129 server.registerMBean(new Trivial(), oname); 130 131 TrivialMBean mbean = (TrivialMBean)MBeanProxy.get( 132 TrivialMBean.class, oname, AgentID.get(server)); 133 134 mbean.doOperation(); 135 mbean.setSomething("JBossMX"); 136 137 assertEquals("JBossMX", mbean.getSomething()); 138 } 139 140 public void testProxyInvocationWithConflictingMBeanAndContextMethods() throws Exception 141 { 142 MBeanServer server = MBeanServerFactory.createMBeanServer(); 143 ObjectName oname = new ObjectName ("test:test=test"); 144 145 server.registerMBean(new Trivial(), oname); 146 147 TrivialMBean mbean = (TrivialMBean)MBeanProxy.get( 148 TrivialMBean.class, oname, AgentID.get(server)); 149 150 mbean.getMBeanServer(); 151 assertTrue(mbean.isGMSInvoked()); 152 } 153 154 public void testContextAccess() throws Exception 155 { 156 MBeanServer server = MBeanServerFactory.createMBeanServer(); 157 ObjectName oname = new ObjectName ("test:test=test"); 158 159 Trivial2MBean mbean = (Trivial2MBean)MBeanProxy.create( 160 Trivial2.class, Trivial2MBean.class, oname, server 161 ); 162 163 ProxyContext ctx = (ProxyContext)mbean; 164 165 ctx.getMBeanServer(); 166 } 167 168 public void testProxyInvocationBetweenServers() throws Exception 169 { 170 MBeanServer server1 = MBeanServerFactory.createMBeanServer(); 171 MBeanServer server2 = MBeanServerFactory.createMBeanServer(); 172 ObjectName oname1 = new ObjectName ("test:name=target"); 173 ObjectName oname2 = new ObjectName ("test:name=proxy"); 174 175 Trivial2MBean mbean = (Trivial2MBean)MBeanProxy.create( 177 Trivial2.class, Trivial2MBean.class, oname1, server1 178 ); 179 180 server2.registerMBean(mbean, oname2); 182 183 server2.invoke(oname2, "doOperation", null, null); 185 186 assertTrue(((Boolean )server1.getAttribute(oname1, "OperationInvoked")).booleanValue()); 188 } 189 190 public void testSimultaneousTypedAndDetypedInvocations() throws Exception 191 { 192 MBeanServer server = MBeanServerFactory.createMBeanServer(); 193 ObjectName oname = new ObjectName ("test:test=test"); 194 195 Trivial2MBean mbean = (Trivial2MBean)MBeanProxy.create( 196 Trivial2.class, Trivial2MBean.class, oname,server 197 ); 198 199 mbean.setSomething("Kissa"); 201 assertTrue(mbean.getSomething().equals("Kissa")); 202 203 DynamicMBean mbean2 = (DynamicMBean )mbean; 205 mbean2.setAttribute(new Attribute ("Something", "Koira")); 206 assertTrue(mbean2.getAttribute("Something").equals("Koira")); 207 208 server.setAttribute(oname, new Attribute ("Something", "Kissa")); 210 assertTrue(server.getAttribute(oname, "Something").equals("Kissa")); 211 212 mbean.doOperation(); 214 assertTrue(mbean.isOperationInvoked()); 215 216 mbean.reset(); 217 218 mbean2.invoke("doOperation", null, null); 220 assertTrue(((Boolean )mbean2.getAttribute("OperationInvoked")).booleanValue()); 221 222 mbean2.invoke("reset", null, null); 223 224 server.invoke(oname, "doOperation", null, null); 226 assertTrue(((Boolean )server.getAttribute(oname, "OperationInvoked")).booleanValue()); 227 } 228 229 public void testContextAccessToMBeanServer() throws Exception 230 { 231 MBeanServer server = MBeanServerFactory.createMBeanServer(); 232 ObjectName oname = new ObjectName ("test:test=test"); 233 234 Trivial2MBean mbean = (Trivial2MBean)MBeanProxy.create( 235 Trivial2.class, Trivial2MBean.class, oname, server 236 ); 237 238 ProxyContext ctx = (ProxyContext)mbean; 240 MBeanServer srvr = ctx.getMBeanServer(); 241 242 Set mbeans = srvr.queryMBeans(new ObjectName ("test:*"), null); 243 ObjectInstance oi = (ObjectInstance )mbeans.iterator().next(); 244 245 assertTrue(oi.getObjectName().equals(oname)); 246 247 assertTrue(srvr.getAttribute( 248 new ObjectName ("JMImplementation:type=MBeanServerDelegate"), 249 "ImplementationName" 250 ).equals("JBossMX")); 251 252 } 253 254 public void testArbitraryInterfaceWithProxy() throws Exception 255 { 256 MBeanServer server = MBeanServerFactory.createMBeanServer(); 257 ObjectName oname = new ObjectName ("test:test=test"); 258 259 RequiredModelMBean rmm = new RequiredModelMBean (); 260 Resource resource = new Resource(); 261 262 rmm.setManagedResource(resource, "ObjectReference"); 263 rmm.setModelMBeanInfo(resource.getMBeanInfo()); 264 265 server.registerMBean(rmm, oname); 266 267 MyInterface mbean = (MyInterface)MBeanProxy.get( 268 MyInterface.class, oname, server 269 ); 270 271 mbean.setAttributeName("foo"); 272 mbean.setAttributeName2("bar"); 273 274 assertTrue(mbean.getAttributeName2().equals("bar")); 275 assertTrue(mbean.doOperation().equals("tamppi")); 276 } 277 278 287 public void testCustomExceptionHandler() throws Exception 288 { 289 MBeanServer server = MBeanServerFactory.createMBeanServer(); 290 ObjectName oname = new ObjectName ("test:test=test"); 291 ObjectName oname2 = new ObjectName ("test:test=test2"); 292 293 RequiredModelMBean rmm = new RequiredModelMBean (); 294 Resource resource = new Resource(); 295 296 rmm.setManagedResource(resource, "ObjectReference"); 297 rmm.setModelMBeanInfo(resource.getMBeanInfo()); 298 299 server.registerMBean(rmm, oname); 301 server.registerMBean(rmm, oname2); 302 303 ProxyContext ctx = (ProxyContext)MBeanProxy.get( 304 MyInterface.class, oname, server 305 ); 306 307 ctx.setExceptionHandler(new DefaultExceptionHandler() 310 { 311 public Object handleInstanceNotFound(ProxyContext proxyCtx, InstanceNotFoundException e, Method m, Object [] args) throws Exception 312 { 313 return proxyCtx.getMBeanServer().invoke(new ObjectName ("test:test=test2"), m.getName(), args, null); 314 } 315 }); 316 317 server.unregisterMBean(oname); 319 320 MyInterface mbean = (MyInterface)ctx; 322 assertTrue(mbean.doOperation().equals("tamppi")); 323 } 324 325 public void testObjectToStringOnProxy() throws Exception 326 { 327 MBeanServer server = MBeanServerFactory.createMBeanServer(); 328 ObjectName oname = new ObjectName ("test:test=test"); 329 330 RequiredModelMBean rmm = new RequiredModelMBean (); 331 Resource resource = new Resource(); 332 333 rmm.setManagedResource(resource, "ObjectReference"); 334 rmm.setModelMBeanInfo(resource.getMBeanInfo()); 335 336 server.registerMBean(rmm, oname); 337 338 MyInterface mbean = (MyInterface)MBeanProxy.get( 339 MyInterface.class, oname, server 340 ); 341 342 mbean.toString(); 343 344 Object o = (Object )mbean; 345 o.toString(); 346 } 347 348 public void testObjectToStringOverride() throws Exception 349 { 350 MBeanServer server = MBeanServerFactory.createMBeanServer(); 351 ObjectName oname = new ObjectName ("test:test=test"); 352 353 RequiredModelMBean rmm = new RequiredModelMBean (); 354 ResourceOverride resource = new ResourceOverride(); 355 356 rmm.setManagedResource(resource, "ObjectReference"); 357 rmm.setModelMBeanInfo(resource.getMBeanInfo()); 358 359 server.registerMBean(rmm, oname); 360 361 MyInterface mbean = (MyInterface)MBeanProxy.get( 362 MyInterface.class, oname, server 363 ); 364 365 assertTrue(mbean.toString().equals("Resource")); 366 Object o = (Object )mbean; 367 assertTrue(o.toString().equals("Resource")); 368 } 369 370 public void testObjectHashCodeOnProxy() throws Exception 371 { 372 MBeanServer server = MBeanServerFactory.createMBeanServer(); 373 ObjectName oname = new ObjectName ("test:test=test"); 374 375 RequiredModelMBean rmm = new RequiredModelMBean (); 376 Resource resource = new Resource(); 377 378 rmm.setManagedResource(resource, "ObjectReference"); 379 rmm.setModelMBeanInfo(resource.getMBeanInfo()); 380 381 server.registerMBean(rmm, oname); 382 383 MyInterface mbean = (MyInterface)MBeanProxy.get( 384 MyInterface.class, oname, server 385 ); 386 387 mbean.hashCode(); 388 389 Object o = (Object )mbean; 390 o.toString(); 391 } 392 393 public void testObjectHashCodeOverride() throws Exception 394 { 395 MBeanServer server = MBeanServerFactory.createMBeanServer(); 396 ObjectName oname = new ObjectName ("test:test=test"); 397 398 RequiredModelMBean rmm = new RequiredModelMBean (); 399 ResourceOverride resource = new ResourceOverride(); 400 401 rmm.setManagedResource(resource, "ObjectReference"); 402 rmm.setModelMBeanInfo(resource.getMBeanInfo()); 403 404 server.registerMBean(rmm, oname); 405 406 MyInterface mbean = (MyInterface)MBeanProxy.get( 407 MyInterface.class, oname, server 408 ); 409 410 assertTrue(mbean.hashCode() == 10); 411 Object o = (Object )mbean; 412 assertTrue(o.hashCode() == 10); 413 } 414 415 public void testObjectEqualsOnProxy() throws Exception 416 { 417 MBeanServer server = MBeanServerFactory.createMBeanServer(); 418 ObjectName oname = new ObjectName ("test:test=test"); 419 420 ModelMBean mmbean = new XMBean(); 421 Resource resource = new Resource(); 422 423 mmbean.setManagedResource(resource, "ObjectReference"); 424 mmbean.setModelMBeanInfo(resource.getMBeanInfo()); 425 426 server.registerMBean(mmbean, oname); 427 428 MyInterface mbean = (MyInterface)MBeanProxy.get( 429 MyInterface.class, oname, server 430 ); 431 432 MyInterface mbean2 = (MyInterface)MBeanProxy.get( 433 MyInterface.class, oname, server 434 ); 435 436 assertTrue(mbean.equals(mbean)); 437 assertTrue(!mbean.equals(mbean2)); 438 assertTrue(!mbean2.equals(mbean)); 439 } 440 441 public void testObjectEqualsOverride() throws Exception 442 { 443 MBeanServer server = MBeanServerFactory.createMBeanServer(); 444 ObjectName oname = new ObjectName ("test:test=test"); 445 446 ModelMBean rmm = new RequiredModelMBean (); 447 ResourceOverride resource = new ResourceOverride("state"); 448 449 rmm.setManagedResource(resource, "ObjectReference"); 450 rmm.setModelMBeanInfo(resource.getMBeanInfo()); 451 452 server.registerMBean(rmm, oname); 453 454 MyInterface mbean = (MyInterface)MBeanProxy.get( 455 MyInterface.class, oname, server 456 ); 457 458 assertTrue(mbean.equals(mbean)); 459 } 460 461 public void testAttributeNotFoundOnTypedProxy() throws Exception 462 { 463 MBeanServer server = MBeanServerFactory.createMBeanServer(); 464 ObjectName oname = new ObjectName ("test:test=test"); 465 466 ModelMBean mmbean = new XMBean(); 467 ResourceIncorrectInfo resource = new ResourceIncorrectInfo(); 468 469 mmbean.setManagedResource(resource, "ObjectReference"); 470 mmbean.setModelMBeanInfo(resource.getMBeanInfo()); 471 472 server.registerMBean(mmbean, oname); 473 474 MyInterface mbean = (MyInterface)MBeanProxy.get( 475 MyInterface.class, oname, server 476 ); 477 478 ProxyContext ctx = (ProxyContext)mbean; 479 ctx.setExceptionHandler(new DefaultExceptionHandler()); 480 481 try 482 { 483 mbean.setAttributeName2("some name"); 484 } 485 catch (IllegalArgumentException e) 486 { 487 489 } 494 } 495 496 public void testAttributeNotFoundOnDeTypedProxy() throws Exception 497 { 498 MBeanServer server = MBeanServerFactory.createMBeanServer(); 499 ObjectName oname = new ObjectName ("test:test=test"); 500 501 ModelMBean mmbean = new XMBean(); 502 ResourceIncorrectInfo resource = new ResourceIncorrectInfo(); 503 504 mmbean.setManagedResource(resource, "ObjectReference"); 505 mmbean.setModelMBeanInfo(resource.getMBeanInfo()); 506 507 server.registerMBean(mmbean, oname); 508 509 DynamicMBean mbean = (DynamicMBean )MBeanProxy.get(oname, server); 510 511 ProxyContext ctx = (ProxyContext)mbean; 512 ctx.setExceptionHandler(new DefaultExceptionHandler()); 513 514 try 515 { 516 mbean.setAttribute(new Attribute ("AttributeName2", "some name")); 517 } 518 catch (AttributeNotFoundException e) 519 { 520 } 522 } 523 524 public void testInheritanceInTypedProxyArgs() throws Exception 525 { 526 MBeanServer server = MBeanServerFactory.createMBeanServer(); 527 ObjectName oname = new ObjectName ("test:test=test"); 528 529 XMBean mmbean = new XMBean(); 530 ExtendedResource resource = new ExtendedResource(); 531 532 mmbean.setManagedResource(resource, "ObjectReference"); 533 mmbean.setModelMBeanInfo(resource.getMBeanInfo()); 534 535 server.registerMBean(mmbean, oname); 536 537 MyInterface2 mbean = (MyInterface2)MBeanProxy.get( 538 MyInterface2.class, oname, server); 539 540 assertTrue(mbean.doOperation().equals("doOperation")); 541 542 try 543 { 544 assertTrue(mbean.executeThis("executeThis").equals("executeThis")); 545 } 546 catch (ClassCastException e) { 547 fail("KNOWN ISSUE: proxy generates incorrect JMX invocation " + 548 "signature in case argument subclasses are used."); 549 } 550 } 551 552 public void testInheritanceInProxyAttribute() throws Exception 553 { 554 MBeanServer server = MBeanServerFactory.createMBeanServer(); 555 ObjectName oname = new ObjectName ("test:test=test"); 556 557 XMBean mmbean = new XMBean(); 558 ExtendedResource resource = new ExtendedResource(); 559 560 mmbean.setManagedResource(resource, "ObjectReference"); 561 mmbean.setModelMBeanInfo(resource.getMBeanInfo()); 562 563 server.registerMBean(mmbean, oname); 564 565 MyInterface2 mbean = (MyInterface2)MBeanProxy.get( 566 MyInterface2.class, oname, server); 567 568 mbean.setAttribute3("Attribute3"); 569 570 assertTrue(mbean.getAttribute3().equals("Attribute3")); 571 } 572 573 public void testInheritanceInProxyReturnType() throws Exception 574 { 575 MBeanServer server = MBeanServerFactory.createMBeanServer(); 576 ObjectName oname = new ObjectName ("test:test=test"); 577 578 XMBean mmbean = new XMBean(); 579 ExtendedResource resource = new ExtendedResource(); 580 581 mmbean.setManagedResource(resource, "ObjectReference"); 582 mmbean.setModelMBeanInfo(resource.getMBeanInfo()); 583 584 server.registerMBean(mmbean, oname); 585 586 MyInterface2 mbean = (MyInterface2)MBeanProxy.get( 587 MyInterface2.class, oname, server); 588 589 assertTrue(mbean.runMe("runMe").equals("runMe")); 590 } 591 592 } 593 | Popular Tags |