1 7 8 package test.compliance.loading; 9 10 import java.net.URL; 11 import java.util.Set; 12 13 import javax.management.Attribute; 14 import javax.management.MBeanException; 15 import javax.management.MBeanServer; 16 import javax.management.MBeanServerFactory; 17 import javax.management.ObjectName; 18 import javax.management.ServiceNotFoundException; 19 import javax.management.loading.MLet; 20 21 import junit.framework.AssertionFailedError; 22 import junit.framework.TestCase; 23 24 29 public class MLetTEST extends TestCase 30 { 31 private MBeanServer server; 32 33 public MLetTEST(String s) 34 { 35 super(s); 36 } 37 38 protected void setUp() throws Exception 39 { 40 super.setUp(); 41 server = MBeanServerFactory.createMBeanServer(); 42 } 43 44 public void testCreateAndRegister() throws Exception 45 { 46 47 MLet mlet = new MLet(); 48 ObjectName name = new ObjectName("test:name=mlet"); 49 50 try 51 { 52 server.registerMBean(mlet, name); 53 } 54 finally 55 { 56 server.unregisterMBean(name); 57 } 58 } 59 60 public void testMLetLoadClassFromURLInConstructor() throws Exception 61 { 62 65 final String MBEANS_URL = "file:./output/etc/test/compliance/loading/MyMBeans.jar"; 66 67 try 69 { 70 server.getClassLoaderRepository().loadClass("test.compliance.loading.support.Trivial"); 71 72 fail("class test.compliance.loading.support.Trivial was already found in CL repository."); 73 } 74 catch (ClassNotFoundException e) 75 { 76 } 78 79 MBeanServer server = MBeanServerFactory.createMBeanServer(); 80 ObjectName name = new ObjectName("test:name=mlet"); 81 MLet mlet = new MLet(new URL[] { new URL(MBEANS_URL)} ); 82 83 try 85 { 86 server.getClassLoaderRepository().loadClass("test.compliance.loading.support.Trivial"); 87 88 fail("class test.compliance.loading.support.Trivial found in CL repository after MLet construction."); 89 } 90 catch (ClassNotFoundException e) 91 { 92 } 94 95 try 96 { 97 server.registerMBean(mlet, name); 98 server.getClassLoaderRepository().loadClass("test.compliance.loading.support.Trivial"); 99 } 100 finally 101 { 102 server.unregisterMBean(name); 103 } 104 try 106 { 107 server.getClassLoaderRepository().loadClass("test.compliance.loading.support.Trivial"); 108 109 fail("class test.compliance.loading.support.Trivial was still found in CL repository."); 110 } 111 catch (ClassNotFoundException e) 112 { 113 } 115 } 116 117 public void testBasicMLetFileLoad() throws Exception 118 { 119 122 final String MLET_URL = "file:./output/etc/test/compliance/loading/BasicConfig.mlet"; 123 124 try 126 { 127 server.getClassLoaderRepository().loadClass("test.compliance.loading.support.Trivial"); 128 129 fail("class test.compliance.loading.support.Trivial was already found in CL repository."); 130 } 131 catch (ClassNotFoundException e) 132 { 133 } 135 try 137 { 138 server.getClassLoaderRepository().loadClass("test.compliance.loading.support.Trivial2"); 139 140 fail("class test.compliance.loading.support.Trivial2 was already found in CL repository."); 141 } 142 catch (ClassNotFoundException e) 143 { 144 } 146 147 MBeanServer server = MBeanServerFactory.createMBeanServer(); 148 MLet mlet = new MLet(); 149 ObjectName name = new ObjectName("test:name=mlet"); 150 server.registerMBean(mlet, name); 151 152 Set mbeans = (Set)server.invoke(name, "getMBeansFromURL", 153 new Object[] { MLET_URL }, 154 new String[] { String.class.getName() } 155 ); 156 157 try 158 { 159 assertTrue(server.isRegistered(new ObjectName("test:name=Trivial"))); 160 assertTrue(server.isRegistered(new ObjectName("test:name=Trivial2"))); 161 } 162 catch (AssertionFailedError e) 163 { 164 URL[] urls = mlet.getURLs(); 165 URL url = null; 166 167 if (urls != null && urls.length > 0) 168 url = urls[0]; 169 170 fail("FAILS IN RI: SUN JMX RI builds a malformed URL from an MLet text file URL '" + 171 MLET_URL + "' resulting into MLET codebase URL '" + url + "' and therefore fails " + 172 "to load the required classes from the Java archive (MyMBeans.jar)"); 173 } 174 175 assertTrue(server.getMBeanInfo(new ObjectName("test:name=Trivial")) != null); 176 assertTrue(server.getMBeanInfo(new ObjectName("test:name=Trivial2")) != null); 177 178 assertTrue(server.getAttribute(new ObjectName("test:name=Trivial2"), "Something").equals("foo")); 179 180 server.invoke(new ObjectName("test:name=Trivial"), "doOperation", 181 new Object[] { "Test" }, 182 new String[] { String.class.getName() } 183 ); 184 185 server.invoke(new ObjectName("test:name=Trivial2"), "doOperation", 186 new Object[] { "Test" }, 187 new String[] { String.class.getName() } 188 ); 189 } 190 191 195 public void testConflictingMLetFileLoad() throws Exception 196 { 197 200 final String MLET_URL1 = "file:./output/etc/test/compliance/loading/BasicConfig2.mlet"; 201 final String MLET_URL2 = "file:./output/etc/test/compliance/loading/BasicConfig2.mlet"; 202 203 try 205 { 206 server.getClassLoaderRepository().loadClass("test.compliance.loading.support.Trivial3"); 207 208 fail("class test.compliance.loading.support.Trivial was already found in CL repository."); 209 } 210 catch (ClassNotFoundException e) 211 { 212 } 214 try 216 { 217 server.getClassLoaderRepository().loadClass("test.compliance.loading.support.Trivial4"); 218 219 fail("class test.compliance.loading.support.Trivial2 was already found in CL repository."); 220 } 221 catch (ClassNotFoundException e) 222 { 223 } 225 226 MBeanServer server = MBeanServerFactory.createMBeanServer(); 227 MLet mlet = new MLet(); 228 ObjectName name = new ObjectName("test:name=mlet"); 229 server.registerMBean(mlet, name); 230 231 Set mbeans = (Set)server.invoke(name, "getMBeansFromURL", 232 new Object[] { MLET_URL1 }, 233 new String[] { String.class.getName() } 234 ); 235 236 ObjectName oname = new ObjectName("test:name=Trivial2"); 237 server.setAttribute(oname, new Attribute("Something", "Something")); 238 239 mlet = new MLet(); 240 name = new ObjectName("test:name=mlet2"); 241 server.registerMBean(mlet, name); 242 243 mbeans = (Set)server.invoke(name, "getMBeansFromURL", 244 new Object[] { MLET_URL2 }, 245 new String[] { String.class.getName() } 246 ); 247 248 oname = new ObjectName("test:name=Trivial2"); 249 String value = (String)server.getAttribute(oname, "Something"); 250 251 assertTrue(value.equals("Something")); 252 } 253 254 255 public void testMalformedURLLoad() throws Exception 256 { 257 260 MBeanServer server = MBeanServerFactory.createMBeanServer(); 261 MLet mlet = new MLet(); 262 ObjectName name = new ObjectName("test:name=mlet"); 263 try 264 { 265 server.registerMBean(mlet, name); 266 267 server.invoke(name, "getMBeansFromURL", 268 new Object[] { "output/etc/test/compliance/loading/BasicConfig.mlet" }, 269 new String[] { String.class.getName() } 270 ); 271 272 fail("FAILS IN RI: Malformed URL in getMBeansURL() should result in ServiceNotFoundException thrown."); 274 } 275 catch (AssertionFailedError e) 276 { 277 throw e; 279 } 280 catch (MBeanException e) 281 { 282 assertTrue(e.getTargetException() instanceof ServiceNotFoundException); 283 } 284 finally 285 { 286 try 287 { 288 server.unregisterMBean(name); 289 } 290 catch (Exception ignored) {} 291 } 292 } 293 294 public void testMissingMLetTagInLoad() throws Exception 295 { 296 299 MBeanServer server = MBeanServerFactory.createMBeanServer(); 300 MLet mlet = new MLet(); 301 ObjectName name = new ObjectName("test:name=mlet"); 302 try 303 { 304 server.registerMBean(mlet, name); 305 306 server.invoke(name, "getMBeansFromURL", 307 new Object[] { "file:./output/etc/test/compliance/loading/MissingMLET.mlet" }, 308 new String[] { String.class.getName() } 309 ); 310 311 fail("MLet text file missing the MLET tag should result in ServiceNotFoundException thrown."); 313 } 314 catch (AssertionFailedError e) 315 { 316 throw e; 318 } 319 catch (MBeanException e) 320 { 321 assertTrue(e.getTargetException() instanceof ServiceNotFoundException); 322 } 323 finally 324 { 325 try 326 { 327 server.unregisterMBean(name); 328 } 329 catch (Exception ignored) {} 330 } 331 } 332 333 public void testMissingMandatoryArchiveTagInLoad() throws Exception 334 { 335 338 MBeanServer server = MBeanServerFactory.createMBeanServer(); 339 MLet mlet = new MLet(); 340 ObjectName name = new ObjectName("test:name=mlet"); 341 try 342 { 343 server.registerMBean(mlet, name); 344 345 server.invoke(name, "getMBeansFromURL", 346 new Object[] { "file:./output/etc/test/compliance/loading/MissingMandatoryArchive.mlet" }, 347 new String[] { String.class.getName() } 348 ); 349 350 fail("MLet text file missing mandatory ARCHIVE attribute should result in ServiceNotFoundException thrown."); 352 } 353 catch (AssertionFailedError e) 354 { 355 throw e; 357 } 358 catch (MBeanException e) 359 { 360 assertTrue(e.getTargetException() instanceof ServiceNotFoundException); 361 } 362 finally 363 { 364 try 365 { 366 server.unregisterMBean(name); 367 } 368 catch (Exception ignored) {} 369 } 370 } 371 372 public void testMissingMandatoryCodeTagInLoad() throws Exception 373 { 374 377 MBeanServer server = MBeanServerFactory.createMBeanServer(); 378 MLet mlet = new MLet(); 379 ObjectName name = new ObjectName("test:name=mlet"); 380 try 381 { 382 server.registerMBean(mlet, name); 383 384 server.invoke(name, "getMBeansFromURL", 385 new Object[] { "file:./output/etc/test/compliance/loading/MissingMandatoryCode.mlet" }, 386 new String[] { String.class.getName() } 387 ); 388 389 fail("MLet text file missing mandatory CODE attribute should result in ServiceNotFoundException thrown."); 391 } 392 catch (AssertionFailedError e) 393 { 394 throw e; 396 } 397 catch (MBeanException e) 398 { 399 assertTrue(e.getTargetException() instanceof ServiceNotFoundException); 400 } 401 finally 402 { 403 try 404 { 405 server.unregisterMBean(name); 406 } 407 catch (Exception ignored) {} 408 } 409 } 410 411 public void testArchiveListInMLet() throws Exception 412 { 413 416 final String MLET_URL = "file:./output/etc/test/compliance/loading/ArchiveList.mlet"; 417 418 MBeanServer server = MBeanServerFactory.createMBeanServer(); 419 MLet mlet = new MLet(); 420 ObjectName name = new ObjectName("test:name=mlet"); 421 422 try 423 { 424 server.registerMBean(mlet, name); 425 426 server.invoke(name, "getMBeansFromURL", 427 new Object[] { MLET_URL }, 428 new String[] { String.class.getName() } 429 ); 430 431 Class c = null; 432 433 try 434 { 435 c = mlet.loadClass("test.compliance.loading.support.AClass"); 436 } 437 catch (ClassNotFoundException e) 438 { 439 URL[] urls = mlet.getURLs(); 440 fail("FAILS IN RI: SUN JMX RI builds a malformed URL from an MLet text file URL '" + 441 MLET_URL + "' resulting into MLET codebase URL '" + urls[0] + "' and therefore fails " + 442 "to load the required classes from the Java archive."); 443 } 444 445 Object o = c.newInstance(); 446 447 server.setAttribute(new ObjectName("test:name=AnotherTrivial"), new Attribute("Something", o)); 448 o = server.getAttribute(new ObjectName("test:name=AnotherTrivial"), "Something"); 449 450 assertTrue(o.getClass().isAssignableFrom(c)); 451 } 452 finally 453 { 454 try 455 { 456 server.unregisterMBean(name); 457 } 458 catch (Exception ignored) {} 459 } 460 } 461 462 public void testUnexpectedEndOfFile() throws Exception 463 { 464 467 MBeanServer server = MBeanServerFactory.createMBeanServer(); 468 MLet mlet = new MLet(); 469 ObjectName name = new ObjectName("test:name=mlet"); 470 try 471 { 472 server.registerMBean(mlet, name); 473 474 server.invoke(name, "getMBeansFromURL", 475 new Object[] { "file:./output/etc/test/compliance/loading/UnexpectedEnd.mlet" }, 476 new String[] { String.class.getName() } 477 ); 478 479 fail("Unexpected end of file from mlet text file did not cause an exception."); 481 } 482 catch (AssertionFailedError e) 483 { 484 throw e; 485 } 486 catch (MBeanException e) 487 { 488 assertTrue(e.getTargetException() instanceof ServiceNotFoundException); 489 } 490 finally 491 { 492 try 493 { 494 server.unregisterMBean(name); 495 } 496 catch (Exception ignored) {} 497 } 498 } 499 500 public void testMissingEndMLetTag() throws Exception 501 { 502 505 MBeanServer server = MBeanServerFactory.createMBeanServer(); 506 MLet mlet = new MLet(); 507 ObjectName name = new ObjectName("test:name=mlet"); 508 try 509 { 510 server.registerMBean(mlet, name); 511 512 server.invoke(name, "getMBeansFromURL", 513 new Object[] { "file:./output/etc/test/compliance/loading/MissingEndTag.mlet" }, 514 new String[] { String.class.getName() } 515 ); 516 517 assertTrue(!server.isRegistered(new ObjectName("test:name=Trivial"))); 518 } 519 catch (AssertionFailedError e) 520 { 521 throw e; 522 } 523 catch (MBeanException e) 524 { 525 assertTrue(e.getTargetException() instanceof ServiceNotFoundException); 526 } 527 finally 528 { 529 try 530 { 531 server.unregisterMBean(name); 532 } 533 catch (Exception ignored) {} 534 } 535 } 536 537 } 538 | Popular Tags |