1 17 18 package org.apache.geronimo.kernel; 19 20 import java.net.URL ; 21 import java.net.URLClassLoader ; 22 import java.util.Set ; 23 24 import junit.extensions.TestSetup; 25 import junit.framework.Test; 26 import junit.framework.TestCase; 27 import junit.framework.TestSuite; 28 29 import org.apache.geronimo.gbean.AbstractName; 30 import org.apache.geronimo.gbean.AbstractNameQuery; 31 import org.apache.geronimo.gbean.GBeanData; 32 import org.apache.geronimo.gbean.GBeanInfo; 33 import org.apache.geronimo.gbean.GBeanInfoBuilder; 34 import org.apache.geronimo.kernel.management.State; 35 import org.apache.geronimo.kernel.proxy.ProxyFactory; 36 import org.apache.geronimo.kernel.proxy.ProxyManager; 37 import org.apache.geronimo.kernel.repository.Artifact; 38 39 42 public class GBeanTest extends TestCase { 43 private static KernelWraper kernelWraper = new KernelWraper(); 44 private Kernel realKernel; 45 private Kernel kernel; 46 47 50 private static class KernelWraper { 51 public Kernel wrap(Kernel kernel) { 52 return kernel; 53 } 54 } 55 56 59 private static class GBeanKernelLifecycle extends KernelWraper { 60 public Kernel wrap(Kernel kernel) { 61 return new KernelGBean(kernel); 62 } 63 } 64 65 public static Test suite() throws Exception { 66 TestSuite suite = new TestSuite(); 67 TestSuite inner = new TestSuite(GBeanTest.class); 68 69 suite.addTest(new TestSetup(inner) { 70 protected void setUp() throws Exception { 71 super.setUp(); 72 kernelWraper = new KernelWraper(); 73 } 74 }); 75 76 suite.addTest(new TestSetup(inner) { 77 protected void setUp() throws Exception { 78 super.setUp(); 79 kernelWraper = new GBeanKernelLifecycle(); 80 } 81 }); 82 83 return suite; 84 } 85 86 public void testListGBeans() throws Exception { 87 GBeanData gbean = buildGBeanData("name", "test", MockGBean.getGBeanInfo()); 88 89 kernel.loadGBean(gbean, getClass().getClassLoader()); 90 kernel.startGBean(gbean.getAbstractName()); 91 assertEquals(State.RUNNING_INDEX, kernel.getGBeanState(gbean.getAbstractName())); 92 93 Set gbeans = kernel.listGBeans(new AbstractNameQuery(gbean.getAbstractName(), gbean.getGBeanInfo().getInterfaces())); 94 assertEquals(1, gbeans.size()); 95 assertEquals(gbean.getAbstractName(), gbeans.iterator().next()); 96 } 97 98 public void testLifeCycle() throws Exception { 99 GBeanData gbean = buildGBeanData("name", "test", MockGBean.getGBeanInfo()); 100 gbean.setAttribute("finalInt", new Integer (123)); 101 102 assertFalse(kernel.isLoaded(gbean.getAbstractName())); 104 assertFalse(kernel.isRunning(gbean.getAbstractName())); 105 106 kernel.loadGBean(gbean, getClass().getClassLoader()); 107 assertTrue(kernel.isLoaded(gbean.getAbstractName())); 108 assertFalse(kernel.isRunning(gbean.getAbstractName())); 109 110 kernel.startGBean(gbean.getAbstractName()); 111 assertTrue(kernel.isLoaded(gbean.getAbstractName())); 112 assertTrue(kernel.isRunning(gbean.getAbstractName())); 113 114 kernel.stopGBean(gbean.getAbstractName()); 115 assertTrue(kernel.isLoaded(gbean.getAbstractName())); 116 assertFalse(kernel.isRunning(gbean.getAbstractName())); 117 118 kernel.unloadGBean(gbean.getAbstractName()); 119 assertFalse(kernel.isLoaded(gbean.getAbstractName())); 120 assertFalse(kernel.isRunning(gbean.getAbstractName())); 121 122 123 assertFalse(kernel.isLoaded("name")); 125 assertFalse(kernel.isRunning("name")); 126 127 kernel.loadGBean(gbean, getClass().getClassLoader()); 128 assertTrue(kernel.isLoaded("name")); 129 assertFalse(kernel.isRunning("name")); 130 131 kernel.startGBean("name"); 132 assertTrue(kernel.isLoaded("name")); 133 assertTrue(kernel.isRunning("name")); 134 135 kernel.stopGBean("name"); 136 assertTrue(kernel.isLoaded("name")); 137 assertFalse(kernel.isRunning("name")); 138 139 kernel.unloadGBean("name"); 140 assertFalse(kernel.isLoaded("name")); 141 assertFalse(kernel.isRunning("name")); 142 143 144 assertFalse(kernel.isLoaded(MockGBean.class)); 146 assertFalse(kernel.isRunning(MockGBean.class)); 147 148 kernel.loadGBean(gbean, getClass().getClassLoader()); 149 assertTrue(kernel.isLoaded(MockGBean.class)); 150 assertFalse(kernel.isRunning(MockGBean.class)); 151 152 kernel.startGBean(MockGBean.class); 153 assertTrue(kernel.isLoaded(MockGBean.class)); 154 assertTrue(kernel.isRunning(MockGBean.class)); 155 156 kernel.stopGBean(MockGBean.class); 157 assertTrue(kernel.isLoaded(MockGBean.class)); 158 assertFalse(kernel.isRunning(MockGBean.class)); 159 160 kernel.unloadGBean(MockGBean.class); 161 assertFalse(kernel.isLoaded(MockGBean.class)); 162 assertFalse(kernel.isRunning(MockGBean.class)); 163 164 165 assertFalse(kernel.isLoaded("name", MockGBean.class)); 167 assertFalse(kernel.isRunning("name", MockGBean.class)); 168 169 kernel.loadGBean(gbean, getClass().getClassLoader()); 170 assertTrue(kernel.isLoaded("name", MockGBean.class)); 171 assertFalse(kernel.isRunning("name", MockGBean.class)); 172 173 kernel.startGBean("name", MockGBean.class); 174 assertTrue(kernel.isLoaded("name", MockGBean.class)); 175 assertTrue(kernel.isRunning("name", MockGBean.class)); 176 177 kernel.stopGBean("name", MockGBean.class); 178 assertTrue(kernel.isLoaded("name", MockGBean.class)); 179 assertFalse(kernel.isRunning("name", MockGBean.class)); 180 181 kernel.unloadGBean("name", MockGBean.class); 182 assertFalse(kernel.isLoaded("name", MockGBean.class)); 183 assertFalse(kernel.isRunning("name", MockGBean.class)); 184 } 185 186 public void testGetGBean() throws Exception { 187 GBeanData gbean = buildGBeanData("name", "test", MockGBean.getGBeanInfo()); 188 gbean.setAttribute("finalInt", new Integer (123)); 189 190 kernel.loadGBean(gbean, getClass().getClassLoader()); 191 kernel.startGBean(gbean.getAbstractName()); 192 assertTrue(kernel.isRunning(gbean.getAbstractName())); 193 194 MockGBean mockGBean = (MockGBean) kernel.getGBean(gbean.getAbstractName()); 196 assertEquals(123, mockGBean.getFinalInt()); 197 assertEquals(gbean.getAbstractName(), kernel.getAbstractNameFor(mockGBean)); 198 assertEquals("name", kernel.getShortNameFor(mockGBean)); 199 200 mockGBean = (MockGBean) kernel.getGBean("name"); 202 assertEquals(123, mockGBean.getFinalInt()); 203 assertEquals(gbean.getAbstractName(), kernel.getAbstractNameFor(mockGBean)); 204 assertEquals("name", kernel.getShortNameFor(mockGBean)); 205 206 mockGBean = (MockGBean) kernel.getGBean(MockGBean.class); 208 assertEquals(123, mockGBean.getFinalInt()); 209 assertEquals(gbean.getAbstractName(), kernel.getAbstractNameFor(mockGBean)); 210 assertEquals("name", kernel.getShortNameFor(mockGBean)); 211 212 mockGBean = (MockGBean) kernel.getGBean("name", MockGBean.class); 214 assertEquals(123, mockGBean.getFinalInt()); 215 assertEquals(gbean.getAbstractName(), kernel.getAbstractNameFor(mockGBean)); 216 assertEquals("name", kernel.getShortNameFor(mockGBean)); 217 } 218 219 public void testInvoke() throws Exception { 220 GBeanData gbean = buildGBeanData("name", "test", MockGBean.getGBeanInfo()); 221 String testValue = "test-value"; 222 String otherValue = "other-value"; 223 gbean.setAttribute("value", testValue); 224 225 kernel.loadGBean(gbean, getClass().getClassLoader()); 226 kernel.startGBean(gbean.getAbstractName()); 227 assertTrue(kernel.isRunning(gbean.getAbstractName())); 228 MockGBean mockGBean = (MockGBean) kernel.getGBean(gbean.getAbstractName()); 229 230 234 assertEquals(testValue, mockGBean.getValue()); 236 mockGBean.setValue(otherValue); 237 assertEquals(otherValue, mockGBean.getValue()); 238 mockGBean.setValue(testValue); 239 240 assertEquals(testValue, kernel.getAttribute(gbean.getAbstractName(), "value")); 242 kernel.setAttribute(gbean.getAbstractName(), "value", otherValue); 243 assertEquals(otherValue, kernel.getAttribute(gbean.getAbstractName(), "value")); 244 kernel.setAttribute(gbean.getAbstractName(), "value", testValue); 245 assertEquals(testValue, kernel.getAttribute(gbean.getAbstractName(), "value")); 246 247 assertEquals(testValue, mockGBean.fetchValue()); 249 assertEquals(testValue, kernel.invoke(gbean.getAbstractName(), "fetchValue")); 250 assertEquals(testValue, mockGBean.doSomething(testValue)); 251 assertEquals(testValue, kernel.invoke(gbean.getAbstractName(), "doSomething", new Object [] {testValue}, new String [] {String .class.getName()})); 252 assertEquals(testValue, mockGBean.getValue()); 253 254 258 assertEquals(testValue, mockGBean.getValue()); 260 mockGBean.setValue(otherValue); 261 assertEquals(otherValue, mockGBean.getValue()); 262 mockGBean.setValue(testValue); 263 264 assertEquals(testValue, kernel.getAttribute("name", "value")); 266 kernel.setAttribute("name", "value", otherValue); 267 assertEquals(otherValue, kernel.getAttribute("name", "value")); 268 kernel.setAttribute("name", "value", testValue); 269 assertEquals(testValue, kernel.getAttribute("name", "value")); 270 271 assertEquals(testValue, mockGBean.fetchValue()); 273 assertEquals(testValue, kernel.invoke("name", "fetchValue")); 274 assertEquals(testValue, mockGBean.doSomething(testValue)); 275 assertEquals(testValue, kernel.invoke("name", "doSomething", new Object [] {testValue}, new String [] {String .class.getName()})); 276 assertEquals(testValue, mockGBean.getValue()); 277 278 282 assertEquals(testValue, mockGBean.getValue()); 284 mockGBean.setValue(otherValue); 285 assertEquals(otherValue, mockGBean.getValue()); 286 mockGBean.setValue(testValue); 287 288 assertEquals(testValue, kernel.getAttribute(MockGBean.class, "value")); 290 kernel.setAttribute(MockGBean.class, "value", otherValue); 291 assertEquals(otherValue, kernel.getAttribute(MockGBean.class, "value")); 292 kernel.setAttribute(MockGBean.class, "value", testValue); 293 assertEquals(testValue, kernel.getAttribute(MockGBean.class, "value")); 294 295 assertEquals(testValue, mockGBean.fetchValue()); 297 assertEquals(testValue, kernel.invoke(MockGBean.class, "fetchValue")); 298 assertEquals(testValue, mockGBean.doSomething(testValue)); 299 assertEquals(testValue, kernel.invoke(MockGBean.class, "doSomething", new Object [] {testValue}, new String [] {String .class.getName()})); 300 assertEquals(testValue, mockGBean.getValue()); 301 302 306 assertEquals(testValue, mockGBean.getValue()); 308 mockGBean.setValue(otherValue); 309 assertEquals(otherValue, mockGBean.getValue()); 310 mockGBean.setValue(testValue); 311 312 assertEquals(testValue, kernel.getAttribute("name", MockGBean.class, "value")); 314 kernel.setAttribute("name", MockGBean.class, "value", otherValue); 315 assertEquals(otherValue, kernel.getAttribute("name", MockGBean.class, "value")); 316 kernel.setAttribute("name", MockGBean.class, "value", testValue); 317 assertEquals(testValue, kernel.getAttribute("name", MockGBean.class, "value")); 318 319 assertEquals(testValue, mockGBean.fetchValue()); 321 assertEquals(testValue, kernel.invoke("name", MockGBean.class, "fetchValue")); 322 assertEquals(testValue, mockGBean.doSomething(testValue)); 323 assertEquals(testValue, kernel.invoke("name", MockGBean.class, "doSomething", new Object [] {testValue}, new String [] {String .class.getName()})); 324 assertEquals(testValue, mockGBean.getValue()); 325 326 } 327 328 public void testLoad() throws Exception { 329 ClassLoader cl = getClass().getClassLoader(); 330 ClassLoader myCl = new URLClassLoader (new URL [0], cl); 331 GBeanData gbean = buildGBeanData("name", "test", MockGBean.getGBeanInfo()); 332 gbean.setAttribute("name", "Test"); 333 gbean.setAttribute("finalInt", new Integer (123)); 334 335 kernel.loadGBean(gbean, myCl); 336 kernel.startGBean(gbean.getAbstractName()); 337 assertEquals(State.RUNNING_INDEX, kernel.getGBeanState(gbean.getAbstractName())); 338 assertEquals("Hello", kernel.invoke(gbean.getAbstractName(), "doSomething", new Object []{"Hello"}, new String []{String .class.getName()})); 339 340 assertEquals(gbean.getAbstractName().getObjectName().getCanonicalName(), kernel.getAttribute(gbean.getAbstractName(), "objectName")); 341 assertEquals(gbean.getAbstractName().getObjectName().getCanonicalName(), kernel.getAttribute(gbean.getAbstractName(), "actualObjectName")); 342 343 assertSame(myCl, kernel.getAttribute(gbean.getAbstractName(), "actualClassLoader")); 344 345 kernel.getAttribute(gbean.getAbstractName(), "classLoader"); 348 349 assertSame(realKernel, kernel.getAttribute(gbean.getAbstractName(), "kernel")); 350 assertSame(realKernel, kernel.getAttribute(gbean.getAbstractName(), "actualKernel")); 351 352 kernel.stopGBean(gbean.getAbstractName()); 353 kernel.unloadGBean(gbean.getAbstractName()); 354 } 355 356 public void testEndpoint() throws Exception { 357 ClassLoader cl = MockGBean.class.getClassLoader(); 358 GBeanData gbean1 = buildGBeanData("name", "test", MockGBean.getGBeanInfo()); 359 gbean1.setAttribute("finalInt", new Integer (123)); 360 kernel.loadGBean(gbean1, cl); 361 kernel.startGBean(gbean1.getAbstractName()); 362 363 GBeanData gbean2 = buildGBeanData("name", "test2", MockGBean.getGBeanInfo()); 364 gbean2.setAttribute("finalInt", new Integer (123)); 365 gbean2.setReferencePattern("MockEndpoint", gbean1.getAbstractName()); 366 kernel.loadGBean(gbean2, cl); 367 kernel.startGBean(gbean2.getAbstractName()); 368 369 assertEquals("endpointCheck", kernel.invoke(gbean2.getAbstractName(), "checkEndpoint", null, null)); 370 } 371 372 public void testNoProxyEndpoint() throws Exception { 373 ClassLoader cl = MockGBean.class.getClassLoader(); 374 GBeanData gbean1 = buildGBeanData("name", "test", MockGBean.getGBeanInfo()); 375 gbean1.setAttribute("finalInt", new Integer (123)); 376 kernel.loadGBean(gbean1, cl); 377 kernel.startGBean(gbean1.getAbstractName()); 378 379 GBeanData gbean2 = buildGBeanData("name", "test2", MockGBean.getGBeanInfo()); 380 gbean2.setAttribute("finalInt", new Integer (123)); 381 gbean2.setReferencePattern("MockEndpoint", gbean1.getAbstractName()); 382 kernel.loadGBean(gbean2, cl); 383 kernel.startGBean(gbean2.getAbstractName()); 384 385 386 MockGBean mockGBean1 = (MockGBean) kernel.getGBean(gbean1.getAbstractName()); 387 MockGBean mockGBean2 = (MockGBean) kernel.getGBean(gbean2.getAbstractName()); 388 } 391 392 public void testProxiesInterfaces() throws Exception { 393 ClassLoader cl = getClass().getClassLoader(); 394 ClassLoader myCl = new URLClassLoader (new URL [0], cl); 395 GBeanData gbean = buildGBeanData("name", "test", MockGBean.getGBeanInfo()); 396 gbean.setAttribute("name", "Test"); 397 gbean.setAttribute("finalInt", new Integer (123)); 398 kernel.loadGBean(gbean, myCl); 399 kernel.startGBean(gbean.getAbstractName()); 400 ProxyManager mgr = kernel.getProxyManager(); 401 402 Object test = mgr.createProxy(gbean.getAbstractName(), myCl); 403 assertTrue(test instanceof MockEndpoint); 404 assertTrue(test instanceof MockParentInterface1); 405 assertTrue(test instanceof MockParentInterface2); 406 assertTrue(test instanceof MockChildInterface1); 407 assertTrue(test instanceof MockChildInterface2); 408 assertFalse(test instanceof Comparable ); 409 ((MockEndpoint)test).doNothing(); 410 assertEquals("Foo", ((MockEndpoint)test).echo("Foo")); 411 ((MockParentInterface1)test).setValue("Foo"); 412 assertEquals("Foo", ((MockParentInterface1)test).getValue()); 413 ((MockParentInterface1)test).setMutableInt(6); 414 assertEquals(6, ((MockParentInterface1)test).getMutableInt()); 415 ((MockParentInterface2)test).doNothing(); 416 assertEquals("Foo", ((MockParentInterface2)test).echo("Foo")); 417 ((MockParentInterface2)test).setValue("Foo"); 418 assertEquals("Foo", ((MockParentInterface2)test).getValue()); 419 ((MockChildInterface1)test).getFinalInt(); 420 ((MockChildInterface2)test).doNothing(); 421 assertEquals("Foo", ((MockChildInterface2)test).doSomething("Foo")); 422 423 test = mgr.createProxy(gbean.getAbstractName(), MockEndpoint.class); 424 assertTrue(test instanceof MockEndpoint); 425 assertTrue(test instanceof MockParentInterface1); 426 assertTrue(test instanceof MockParentInterface2); 427 assertTrue(test instanceof MockChildInterface1); 428 assertTrue(test instanceof MockChildInterface2); 429 assertFalse(test instanceof Comparable ); 430 431 ProxyFactory proxyFactory; 432 proxyFactory = mgr.createProxyFactory(new Class []{MockEndpoint.class, MockParentInterface2.class, MockChildInterface2.class}, myCl); 433 test = proxyFactory.createProxy(gbean.getAbstractName()); 434 assertTrue(test instanceof MockEndpoint); 435 assertTrue(test instanceof MockParentInterface1); 436 assertTrue(test instanceof MockParentInterface2); 437 assertTrue(test instanceof MockChildInterface1); 438 assertTrue(test instanceof MockChildInterface2); 439 assertFalse(test instanceof Comparable ); 440 441 proxyFactory = mgr.createProxyFactory(new Class []{MockEndpoint.class, MockParentInterface1.class, MockChildInterface1.class}, myCl); 442 test = proxyFactory.createProxy(gbean.getAbstractName()); 443 assertTrue(test instanceof MockEndpoint); 444 assertTrue(test instanceof MockParentInterface1); 445 assertFalse(test instanceof MockParentInterface2); 446 assertTrue(test instanceof MockChildInterface1); 447 assertFalse(test instanceof MockChildInterface2); 448 assertFalse(test instanceof Comparable ); 449 450 proxyFactory = mgr.createProxyFactory(new Class []{MockEndpoint.class, MockParentInterface1.class, MockChildInterface1.class, Comparable .class}, myCl); 451 test = proxyFactory.createProxy(gbean.getAbstractName()); 452 assertTrue(test instanceof MockEndpoint); 453 assertTrue(test instanceof MockParentInterface1); 454 assertFalse(test instanceof MockParentInterface2); 455 assertTrue(test instanceof MockChildInterface1); 456 assertFalse(test instanceof MockChildInterface2); 457 458 proxyFactory = mgr.createProxyFactory(new Class []{MockParentInterface1.class, MockChildInterface1.class, Comparable .class}, myCl); 459 test = proxyFactory.createProxy(gbean.getAbstractName()); 460 assertFalse(test instanceof MockEndpoint); 461 assertTrue(test instanceof MockParentInterface1); 462 assertFalse(test instanceof MockParentInterface2); 463 assertTrue(test instanceof MockChildInterface1); 464 assertFalse(test instanceof MockChildInterface2); 465 466 proxyFactory = mgr.createProxyFactory(new Class []{MockEndpoint.class, Comparable .class}, myCl); 467 test = proxyFactory.createProxy(gbean.getAbstractName()); 468 assertTrue(test instanceof MockEndpoint); 469 assertFalse(test instanceof MockParentInterface1); 470 assertFalse(test instanceof MockParentInterface2); 471 assertFalse(test instanceof MockChildInterface1); 472 assertFalse(test instanceof MockChildInterface2); 473 474 proxyFactory = mgr.createProxyFactory(new Class []{Comparable .class}, myCl); 475 test = proxyFactory.createProxy(gbean.getAbstractName()); 476 477 try { 478 proxyFactory = mgr.createProxyFactory(null, myCl); 479 fail(); 480 } catch (NullPointerException e) { 481 } 482 483 try { 484 proxyFactory = mgr.createProxyFactory(new Class [0], myCl); 485 fail(); 486 } catch (IllegalArgumentException e) { 487 } 488 489 try { 490 test = mgr.createProxyFactory(new Class []{MockGBean.class, String .class}, cl).createProxy(gbean.getAbstractName()); 492 fail(); 493 } catch (IllegalArgumentException e) { 494 } 495 } 496 497 public void testProxyClass() throws Exception { 498 ClassLoader cl = getClass().getClassLoader(); 499 ClassLoader myCl = new URLClassLoader (new URL [0], cl); 500 501 GBeanData gbean = buildGBeanData("name", "test", ClassGBean.getGBeanInfo()); 502 gbean.setAttribute("name", "Test"); 503 kernel.loadGBean(gbean, myCl); 504 kernel.startGBean(gbean.getAbstractName()); 505 ProxyManager mgr = kernel.getProxyManager(); 506 507 Object test = mgr.createProxy(gbean.getAbstractName(), Named.class); 508 assertTrue(test instanceof Named); 509 assertFalse(test instanceof ClassGBean); 510 assertEquals("Test", ((Named)test).getName()); 511 512 test = mgr.createProxy(gbean.getAbstractName(), myCl); 513 assertTrue(test instanceof Named); 514 assertFalse(test instanceof ClassGBean); 515 assertEquals("Test", ((Named)test).getName()); 516 517 try { 518 mgr.createProxy(gbean.getAbstractName(), ClassGBean.class); 519 fail("expected an IllegalArgumentException"); 520 } catch (IllegalArgumentException ignored) { 521 } 523 } 524 525 protected void setUp() throws Exception { 526 super.setUp(); 527 realKernel = KernelFactory.newInstance().createKernel("test"); 528 realKernel.boot(); 529 kernel = kernelWraper.wrap(realKernel); 530 } 531 532 protected void tearDown() throws Exception { 533 realKernel.shutdown(); 534 realKernel = null; 535 kernel = null; 536 super.tearDown(); 537 } 538 539 private GBeanData buildGBeanData(String name, String type, GBeanInfo info) { 540 AbstractName abstractName = kernel.getNaming().createRootName(new Artifact("test", "foo", "1", "car"), name, type); 541 return new GBeanData(abstractName, info); 542 } 543 544 public static interface Named { 545 String getName(); 546 } 547 548 public static class ClassGBean implements Named { 549 private final String name; 550 551 public ClassGBean(String name) { 552 this.name = name; 553 } 554 555 public String getName() { 556 return name; 557 } 558 559 private static final GBeanInfo GBEAN_INFO; 560 561 static { 562 GBeanInfoBuilder infoFactory = GBeanInfoBuilder.createStatic(ClassGBean.class); 563 infoFactory.setConstructor(new String []{"name"}); 564 565 GBEAN_INFO = infoFactory.getBeanInfo(); 566 } 567 568 public static GBeanInfo getGBeanInfo() { 569 return GBEAN_INFO; 570 } 571 } 572 } 573 | Popular Tags |