1 22 package org.jboss.test.jmx.compliance.loading; 23 24 import java.net.URL ; 25 import java.util.Iterator ; 26 import java.util.Set ; 27 28 import javax.management.Attribute ; 29 import javax.management.MBeanException ; 30 import javax.management.MBeanServer ; 31 import javax.management.MBeanServerFactory ; 32 import javax.management.ObjectName ; 33 import javax.management.ServiceNotFoundException ; 34 import javax.management.loading.MLet ; 35 36 import junit.framework.AssertionFailedError; 37 import junit.framework.TestCase; 38 39 44 public class MLetTEST extends TestCase 45 { 46 private MBeanServer server; 47 48 private URL location; 49 50 public MLetTEST(String s) throws Exception 51 { 52 super(s); 53 54 location = getClass().getResource("/org/jboss/test/jmx/compliance/loading/MLetTEST.class"); 56 String jarPath = location.getPath(); 57 int i = jarPath.indexOf('!'); 58 if (i != -1) 59 { 60 jarPath = jarPath.substring(0, i); 61 location = new URL (jarPath); 62 location = new URL (location, "file:../"); 63 } 64 } 65 66 protected void setUp() throws Exception 67 { 68 super.setUp(); 69 server = MBeanServerFactory.createMBeanServer(); 70 } 71 72 public void testCreateAndRegister() throws Exception 73 { 74 75 MLet mlet = new MLet (); 76 ObjectName name = new ObjectName ("test:name=mlet"); 77 78 try 79 { 80 server.registerMBean(mlet, name); 81 } 82 finally 83 { 84 server.unregisterMBean(name); 85 } 86 } 87 88 public void testMLetLoadClassFromURLInConstructor() throws Exception 89 { 90 93 final URL MBEANS_URL = new URL (location, "lib/jmxcompliance-MyMBeans.jar"); 94 95 try 97 { 98 server.getClassLoaderRepository().loadClass("org.jboss.test.jmx.compliance.loading.support.Trivial"); 99 100 fail("class org.jboss.test.jmx.compliance.loading.support.Trivial was already found in CL repository."); 101 } 102 catch (ClassNotFoundException e) 103 { 104 } 106 107 MBeanServer server = MBeanServerFactory.createMBeanServer(); 108 ObjectName name = new ObjectName ("test:name=mlet"); 109 MLet mlet = new MLet (new URL [] { MBEANS_URL }); 110 111 try 113 { 114 server.getClassLoaderRepository().loadClass("org.jboss.test.jmx.compliance.loading.support.Trivial"); 115 116 fail("class org.jboss.test.jmx.compliance.loading.support.Trivial found in CL repository after MLet construction."); 117 } 118 catch (ClassNotFoundException e) 119 { 120 } 122 123 try 124 { 125 server.registerMBean(mlet, name); 126 server.getClassLoaderRepository().loadClass("org.jboss.test.jmx.compliance.loading.support.Trivial"); 127 } 128 finally 129 { 130 server.unregisterMBean(name); 131 } 132 try 134 { 135 server.getClassLoaderRepository().loadClass("org.jboss.test.jmx.compliance.loading.support.Trivial"); 136 137 fail("class org.jboss.test.jmx.compliance.loading.support.Trivial was still found in CL repository."); 138 } 139 catch (ClassNotFoundException e) 140 { 141 } 143 } 144 145 public void testBasicMLetFileLoad() throws Exception 146 { 147 150 final URL MLET_URL = new URL (location, "etc/tests/BasicConfig.mlet"); 151 152 try 154 { 155 server.getClassLoaderRepository().loadClass("org.jboss.test.jmx.compliance.loading.support.Trivial"); 156 157 fail("class org.jboss.test.jmx.compliance.loading.support.Trivial was already found in CL repository."); 158 } 159 catch (ClassNotFoundException e) 160 { 161 } 163 try 165 { 166 server.getClassLoaderRepository().loadClass("org.jboss.test.jmx.compliance.loading.support.Trivial2"); 167 168 fail("class org.jboss.test.jmx.compliance.loading.support.Trivial2 was already found in CL repository."); 169 } 170 catch (ClassNotFoundException e) 171 { 172 } 174 175 MBeanServer server = MBeanServerFactory.createMBeanServer(); 176 MLet mlet = new MLet (); 177 ObjectName name = new ObjectName ("test:name=mlet"); 178 server.registerMBean(mlet, name); 179 180 server.invoke(name, "getMBeansFromURL", 181 new Object [] { MLET_URL.toString() }, 182 new String [] { String .class.getName() } 183 ); 184 185 try 186 { 187 assertTrue(server.isRegistered(new ObjectName ("test:name=Trivial"))); 188 assertTrue(server.isRegistered(new ObjectName ("test:name=Trivial2"))); 189 } 190 catch (AssertionFailedError e) 191 { 192 URL [] urls = mlet.getURLs(); 193 URL url = null; 194 195 if (urls != null && urls.length > 0) 196 url = urls[0]; 197 198 fail("FAILS IN RI: SUN JMX RI builds a malformed URL from an MLet text file URL '" + 199 MLET_URL + "' resulting into MLET codebase URL '" + url + "' and therefore fails " + 200 "to load the required classes from the Java archive (MyMBeans.jar)"); 201 } 202 203 assertTrue(server.getMBeanInfo(new ObjectName ("test:name=Trivial")) != null); 204 assertTrue(server.getMBeanInfo(new ObjectName ("test:name=Trivial2")) != null); 205 206 assertTrue(server.getAttribute(new ObjectName ("test:name=Trivial2"), "Something").equals("foo")); 207 208 server.invoke(new ObjectName ("test:name=Trivial"), "doOperation", 209 new Object [] { "Test" }, 210 new String [] { String .class.getName() } 211 ); 212 213 server.invoke(new ObjectName ("test:name=Trivial2"), "doOperation", 214 new Object [] { "Test" }, 215 new String [] { String .class.getName() } 216 ); 217 } 218 219 223 public void testConflictingMLetFileLoad() throws Exception 224 { 225 228 final URL MLET_URL1 = new URL (location, "etc/tests/BasicConfig2.mlet"); 229 final URL MLET_URL2 = new URL (location, "etc/tests/BasicConfig2.mlet"); 230 231 try 233 { 234 server.getClassLoaderRepository().loadClass("org.jboss.test.jmx.compliance.loading.support.Trivial3"); 235 236 fail("class org.jboss.test.jmx.compliance.loading.support.Trivial was already found in CL repository."); 237 } 238 catch (ClassNotFoundException e) 239 { 240 } 242 try 244 { 245 server.getClassLoaderRepository().loadClass("org.jboss.test.jmx.compliance.loading.support.Trivial4"); 246 247 fail("class org.jboss.test.jmx.compliance.loading.support.Trivial2 was already found in CL repository."); 248 } 249 catch (ClassNotFoundException e) 250 { 251 } 253 254 MBeanServer server = MBeanServerFactory.createMBeanServer(); 255 MLet mlet = new MLet (); 256 ObjectName name = new ObjectName ("test:name=mlet"); 257 server.registerMBean(mlet, name); 258 259 Set result = (Set ) server.invoke(name, "getMBeansFromURL", 260 new Object [] { MLET_URL1.toString() }, 261 new String [] { String .class.getName() } 262 ); 263 checkResult(result); 264 265 ObjectName oname = new ObjectName ("test:name=Trivial2"); 266 server.setAttribute(oname, new Attribute ("Something", "Something")); 267 268 mlet = new MLet (); 269 name = new ObjectName ("test:name=mlet2"); 270 server.registerMBean(mlet, name); 271 272 server.invoke(name, "getMBeansFromURL", 273 new Object [] { MLET_URL2.toString() }, 274 new String [] { String .class.getName() } 275 ); 276 277 oname = new ObjectName ("test:name=Trivial2"); 278 String value = (String )server.getAttribute(oname, "Something"); 279 280 assertTrue(value.equals("Something")); 281 } 282 283 284 public void testMalformedURLLoad() throws Exception 285 { 286 289 final URL MLET_URL = new URL (location, "etc/tests/BasicConfig.mlet"); 290 291 MBeanServer server = MBeanServerFactory.createMBeanServer(); 292 MLet mlet = new MLet (); 293 ObjectName name = new ObjectName ("test:name=mlet"); 294 try 295 { 296 server.registerMBean(mlet, name); 297 298 server.invoke(name, "getMBeansFromURL", 299 new Object [] { MLET_URL.getPath() }, 300 new String [] { String .class.getName() } 301 ); 302 303 fail("FAILS IN RI: Malformed URL in getMBeansURL() should result in ServiceNotFoundException thrown."); 305 } 306 catch (AssertionFailedError e) 307 { 308 throw e; 310 } 311 catch (MBeanException e) 312 { 313 assertTrue(e.getTargetException() instanceof ServiceNotFoundException ); 314 } 315 finally 316 { 317 try 318 { 319 server.unregisterMBean(name); 320 } 321 catch (Exception ignored) {} 322 } 323 } 324 325 public void testMissingMLetTagInLoad() throws Exception 326 { 327 330 final URL MLET_URL = new URL (location, "etc/tests/MissingMLET.mlet"); 331 332 MBeanServer server = MBeanServerFactory.createMBeanServer(); 333 MLet mlet = new MLet (); 334 ObjectName name = new ObjectName ("test:name=mlet"); 335 try 336 { 337 server.registerMBean(mlet, name); 338 339 server.invoke(name, "getMBeansFromURL", 340 new Object [] { MLET_URL.toString() }, 341 new String [] { String .class.getName() } 342 ); 343 344 fail("MLet text file missing the MLET tag should result in ServiceNotFoundException thrown."); 346 } 347 catch (AssertionFailedError e) 348 { 349 throw e; 351 } 352 catch (MBeanException e) 353 { 354 assertTrue(e.getTargetException() instanceof ServiceNotFoundException ); 355 } 356 finally 357 { 358 try 359 { 360 server.unregisterMBean(name); 361 } 362 catch (Exception ignored) {} 363 } 364 } 365 366 public void testMissingMandatoryArchiveTagInLoad() throws Exception 367 { 368 371 final URL MLET_URL = new URL (location, "etc/tests/MissingMandatoryArchive.mlet"); 372 373 MBeanServer server = MBeanServerFactory.createMBeanServer(); 374 MLet mlet = new MLet (); 375 ObjectName name = new ObjectName ("test:name=mlet"); 376 try 377 { 378 server.registerMBean(mlet, name); 379 380 server.invoke(name, "getMBeansFromURL", 381 new Object [] { MLET_URL.toString() }, 382 new String [] { String .class.getName() } 383 ); 384 385 fail("MLet text file missing mandatory ARCHIVE attribute should result in ServiceNotFoundException thrown."); 387 } 388 catch (AssertionFailedError e) 389 { 390 throw e; 392 } 393 catch (MBeanException e) 394 { 395 assertTrue(e.getTargetException() instanceof ServiceNotFoundException ); 396 } 397 finally 398 { 399 try 400 { 401 server.unregisterMBean(name); 402 } 403 catch (Exception ignored) {} 404 } 405 } 406 407 public void testMissingMandatoryCodeTagInLoad() throws Exception 408 { 409 412 final URL MLET_URL = new URL (location, "etc/tests/MissingMandatoryCode.mlet"); 413 414 MBeanServer server = MBeanServerFactory.createMBeanServer(); 415 MLet mlet = new MLet (); 416 ObjectName name = new ObjectName ("test:name=mlet"); 417 try 418 { 419 server.registerMBean(mlet, name); 420 421 server.invoke(name, "getMBeansFromURL", 422 new Object [] { MLET_URL.toString() }, 423 new String [] { String .class.getName() } 424 ); 425 426 fail("MLet text file missing mandatory CODE attribute should result in ServiceNotFoundException thrown."); 428 } 429 catch (AssertionFailedError e) 430 { 431 throw e; 433 } 434 catch (MBeanException e) 435 { 436 assertTrue(e.getTargetException() instanceof ServiceNotFoundException ); 437 } 438 finally 439 { 440 try 441 { 442 server.unregisterMBean(name); 443 } 444 catch (Exception ignored) {} 445 } 446 } 447 448 public void testArchiveListInMLet() throws Exception 449 { 450 453 final URL MLET_URL = new URL (location, "etc/tests/ArchiveList.mlet"); 454 455 MBeanServer server = MBeanServerFactory.createMBeanServer(); 456 MLet mlet = new MLet (); 457 ObjectName name = new ObjectName ("test:name=mlet"); 458 459 try 460 { 461 server.registerMBean(mlet, name); 462 463 server.invoke(name, "getMBeansFromURL", 464 new Object [] { MLET_URL.toString() }, 465 new String [] { String .class.getName() } 466 ); 467 468 Class c = null; 469 470 try 471 { 472 c = mlet.loadClass("org.jboss.test.jmx.compliance.loading.support.AClass"); 473 } 474 catch (ClassNotFoundException e) 475 { 476 URL [] urls = mlet.getURLs(); 477 fail("FAILS IN RI: SUN JMX RI builds a malformed URL from an MLet text file URL '" + 478 MLET_URL + "' resulting into MLET codebase URL '" + urls[0] + "' and therefore fails " + 479 "to load the required classes from the Java archive."); 480 } 481 482 Object o = c.newInstance(); 483 484 server.setAttribute(new ObjectName ("test:name=AnotherTrivial"), new Attribute ("Something", o)); 485 o = server.getAttribute(new ObjectName ("test:name=AnotherTrivial"), "Something"); 486 487 assertTrue(o.getClass().isAssignableFrom(c)); 488 } 489 finally 490 { 491 try 492 { 493 server.unregisterMBean(name); 494 } 495 catch (Exception ignored) {} 496 } 497 } 498 499 public void testUnexpectedEndOfFile() throws Exception 500 { 501 504 final URL MLET_URL = new URL (location, "etc/tests/UnexpectedEnd.mlet"); 505 506 MBeanServer server = MBeanServerFactory.createMBeanServer(); 507 MLet mlet = new MLet (); 508 ObjectName name = new ObjectName ("test:name=mlet"); 509 try 510 { 511 server.registerMBean(mlet, name); 512 513 server.invoke(name, "getMBeansFromURL", 514 new Object [] { MLET_URL.toString() }, 515 new String [] { String .class.getName() } 516 ); 517 518 fail("Unexpected end of file from mlet text file did not cause an exception."); 520 } 521 catch (AssertionFailedError e) 522 { 523 throw e; 524 } 525 catch (MBeanException e) 526 { 527 assertTrue(e.getTargetException() instanceof ServiceNotFoundException ); 528 } 529 finally 530 { 531 try 532 { 533 server.unregisterMBean(name); 534 } 535 catch (Exception ignored) {} 536 } 537 } 538 539 public void testMissingEndMLetTag() throws Exception 540 { 541 544 final URL MLET_URL = new URL (location, "etc/tests/MissingEndTag.mlet"); 545 546 MBeanServer server = MBeanServerFactory.createMBeanServer(); 547 MLet mlet = new MLet (); 548 ObjectName name = new ObjectName ("test:name=mlet"); 549 try 550 { 551 server.registerMBean(mlet, name); 552 553 server.invoke(name, "getMBeansFromURL", 554 new Object [] { MLET_URL.toString() }, 555 new String [] { String .class.getName() } 556 ); 557 558 assertTrue(!server.isRegistered(new ObjectName ("test:name=Trivial"))); 559 } 560 catch (AssertionFailedError e) 561 { 562 throw e; 563 } 564 catch (MBeanException e) 565 { 566 assertTrue(e.getTargetException() instanceof ServiceNotFoundException ); 567 } 568 finally 569 { 570 try 571 { 572 server.unregisterMBean(name); 573 } 574 catch (Exception ignored) {} 575 } 576 } 577 578 579 protected void checkResult(Set result) 580 { 581 for (Iterator i = result.iterator(); i.hasNext();) 582 { 583 Object mbean = i.next(); 584 if (mbean instanceof Throwable ) 585 throw new RuntimeException ((Throwable ) mbean); 586 } 587 } 588 } 589 | Popular Tags |