1 22 package org.objectweb.petals.jbi.component.context; 23 24 import java.io.IOException ; 25 import java.lang.reflect.Method ; 26 import java.net.URL ; 27 import java.util.HashMap ; 28 import java.util.HashSet ; 29 import java.util.Map ; 30 import java.util.MissingResourceException ; 31 import java.util.ResourceBundle ; 32 import java.util.Set ; 33 import java.util.logging.Logger ; 34 35 import javax.jbi.JBIException; 36 import javax.jbi.component.Component; 37 import javax.jbi.management.MBeanNames; 38 import javax.jbi.messaging.DeliveryChannel; 39 import javax.jbi.messaging.MessagingException; 40 import javax.jbi.servicedesc.ServiceEndpoint; 41 import javax.management.MBeanServer ; 42 import javax.management.ObjectName ; 43 import javax.xml.namespace.QName ; 44 45 import junit.framework.TestCase; 46 47 import org.objectweb.petals.jbi.component.context.ComponentContextImpl; 48 import org.objectweb.petals.jbi.component.lifecycle.ComponentLifeCycle; 49 import org.objectweb.petals.jbi.component.mock.MockEndpointService; 50 import org.objectweb.petals.jbi.management.service.EndpointService; 51 import org.objectweb.petals.jbi.management.service.EndpointServiceImpl; 52 import org.objectweb.petals.jbi.management.service.LifeCycleManagerImpl; 53 import org.objectweb.petals.jbi.management.service.MBeanNamesImpl; 54 import org.objectweb.petals.jbi.messaging.DeliveryChannelImpl; 55 import org.objectweb.petals.jbi.registry.AbstractEndpoint; 56 import org.objectweb.petals.jbi.registry.ConsumerEndpoint; 57 import org.objectweb.petals.jbi.registry.ExternalEndpoint; 58 import org.objectweb.petals.jbi.registry.InternalEndpoint; 59 import org.objectweb.petals.tools.jbicommon.descriptor.ComponentDescription; 60 import org.objectweb.petals.tools.jbicommon.descriptor.Identification; 61 import org.objectweb.petals.tools.jbicommon.descriptor.JBIDescriptor; 62 import org.w3c.dom.DocumentFragment ; 63 64 import static org.easymock.EasyMock.expect; 65 import static org.easymock.classextension.EasyMock.createMock; 66 import static org.easymock.classextension.EasyMock.replay; 67 import static org.easymock.classextension.EasyMock.reset; 68 import static org.easymock.classextension.EasyMock.verify; 69 70 import com.sun.org.apache.xerces.internal.dom.DocumentFragmentImpl; 71 72 78 public class ComponentContextImplTest extends TestCase { 79 80 private ComponentContextImpl componentContextImpl; 81 82 private MockEndpointService endpointService; 83 84 @Override 85 protected void setUp() { 86 componentContextImpl = new ComponentContextImpl(); 87 Logger logger = Logger.getLogger("test"); 88 componentContextImpl.setRootLogger(logger); 89 endpointService = new MockEndpointService(); 90 componentContextImpl.endpointService = endpointService; 91 } 92 93 public void testGetLogger() throws MissingResourceException , JBIException { 94 98 Logger rootLogger = Logger.getLogger("rootLogger"); 99 componentContextImpl.rootLogger = rootLogger; 100 103 String suffix = null; 104 try{ 105 componentContextImpl.getLogger(suffix, null); 106 fail("No Exception is raised"); 107 } catch (Exception e) { 108 } 110 114 117 suffix = ""; 118 Logger result = componentContextImpl.getLogger(suffix, null); 119 assertNotNull(result); 120 assertEquals("Returned logger does not have the name of base componentn logger",result.getName(),rootLogger.getName()); 121 125 128 suffix = "test"; 129 result = componentContextImpl.getLogger(suffix, null); 130 assertNotNull(result); 131 assertEquals("Returned logger does not have the name of base componentn logger",result.getName(),rootLogger.getName()+"."+suffix); 132 } 133 134 public void testGetLoggerSameSuffix() throws MissingResourceException , 135 JBIException { 136 Logger logger = componentContextImpl.getLogger("suffix", null); 137 Logger logger1 = componentContextImpl.getLogger("suffix", null); 138 assertEquals(logger, logger1); 139 } 140 141 public void testGetLoggerResourceFile() throws MissingResourceException , 142 JBIException, IOException { 143 URL url = this.getClass().getClassLoader().getResource( 144 "myresources_fr.properties"); 145 assertNotNull(url); 146 Logger logger = componentContextImpl.getLogger("resource", 147 "myresources"); 148 ResourceBundle resourceBundle = logger.getResourceBundle(); 149 assertEquals(resourceBundle.getString("Error"), "Erreur"); 150 } 151 152 public void testActivateEndpoint() { 153 157 160 Method method1 = null; 161 try { 162 method1 = EndpointServiceImpl.class.getDeclaredMethod("activateEndpoint", new Class []{QName .class,String .class,ConsumerEndpoint.class}); 163 } catch (Exception e) { 164 e.printStackTrace(); 165 fail("Could not retrieve mocks methods"); 166 } 167 Method [] mockedMethods = new Method []{method1}; 168 EndpointService epService = createMock(EndpointServiceImpl.class,mockedMethods); 169 String service = "fooService"; 170 String namespace = "namespace"; 171 QName serviceName = new QName (namespace,service); 172 String endpointName = "fooServiceEndpoint"; 173 ConsumerEndpoint consumerEndpoint = createMock(ConsumerEndpoint.class); 174 InternalEndpoint internalEP = createMock(InternalEndpoint.class); 175 178 try { 179 expect(epService.activateEndpoint(serviceName, endpointName, consumerEndpoint)).andReturn(internalEP); 180 } catch (JBIException e){ 181 e.printStackTrace(); 182 fail("Could not initialize mocks"); 183 } 184 replay(epService); 185 componentContextImpl.endpointService = epService; 186 componentContextImpl.internalEndpoints = new HashSet <AbstractEndpoint>(); 187 componentContextImpl.address = consumerEndpoint; 188 191 ServiceEndpoint result = null; 192 try { 193 result = componentContextImpl.activateEndpoint(serviceName,endpointName); 194 } catch (JBIException e){ 195 e.printStackTrace(); 196 fail("Error during method invokation"); 197 } 198 assertNotNull("The result of activateEndpoint must not be null",result); 199 assertEquals("Wrong endpoint created",internalEP, result); 200 assertTrue("ComponentContext does not contain the newly created ", componentContextImpl.internalEndpoints.contains(internalEP)); 201 verify(epService); 202 203 207 210 try{ 211 componentContextImpl.activateEndpoint(null,endpointName); 212 fail("No exception was raised"); 213 } catch (Exception e) { 214 } 215 216 220 223 try{ 224 componentContextImpl.activateEndpoint(serviceName,null); 225 fail("No exception was raised"); 226 } catch (Exception e) { 227 } 228 229 } 230 231 public void testDeactivateEndpoint() { 232 236 EndpointService epService = createMock(EndpointServiceImpl.class); 237 AbstractEndpoint serviceEP = createMock(InternalEndpoint.class); 238 Set <AbstractEndpoint> endpoints = new HashSet <AbstractEndpoint>(); 239 endpoints.add(serviceEP); 240 componentContextImpl.endpointService = epService; 241 componentContextImpl.internalEndpoints = endpoints; 242 245 try { 246 componentContextImpl.deactivateEndpoint(serviceEP); 247 } catch (JBIException e){ 248 e.printStackTrace(); 249 fail("Error during method invokation"); 250 } 251 assertFalse("The service endpoint was not properly deactivated", componentContextImpl.internalEndpoints.contains(serviceEP)); 252 253 257 260 try { 261 componentContextImpl.deactivateEndpoint(null); 262 fail("No exception was thrown"); 263 } catch (Exception e) { 264 } 265 } 266 267 public void testDeregisterAllEndpoints() { 268 componentContextImpl.internalEndpoints = new HashSet <AbstractEndpoint>(); 269 componentContextImpl.externalEndpoints = new HashSet <ServiceEndpoint>(); 270 InternalEndpoint internalEndpoint = new InternalEndpoint(new QName ( 271 "service"), "endpoint", "compo", "0", null); 272 componentContextImpl.internalEndpoints.add(internalEndpoint); 273 ExternalEndpoint externalEndpoint = new ExternalEndpoint( 274 new ConsumerEndpoint("compo", "0"), "compo", "0"); 275 componentContextImpl.externalEndpoints.add(externalEndpoint); 276 try { 277 componentContextImpl.deregisterAllEndpoints(); 278 } catch (JBIException e){ 279 e.printStackTrace(); 280 fail("Error during method invokation"); 281 } 282 assertEquals(componentContextImpl.internalEndpoints.size(), 0); 283 assertEquals(componentContextImpl.externalEndpoints.size(), 0); 284 assertTrue(endpointService.isDeregisterExternalEndpoint()); 285 assertTrue(endpointService.isDeactivateEndpoint()); 286 } 287 288 public void testDeregisterExternalEndpoint() { 289 ExternalEndpoint externalEndpoint = new ExternalEndpoint( 290 new ConsumerEndpoint("compo", "0"), "compo", "0"); 291 componentContextImpl.externalEndpoints = new HashSet <ServiceEndpoint>(); 292 componentContextImpl.externalEndpoints.add(externalEndpoint); 293 try { 294 componentContextImpl.deregisterExternalEndpoint(externalEndpoint); 295 } catch (JBIException e){ 296 e.printStackTrace(); 297 fail("Error during method invokation"); 298 } 299 assertEquals(componentContextImpl.externalEndpoints.size(), 0); 300 assertTrue(endpointService.isDeregisterExternalEndpoint()); 301 } 302 303 public void testGetComponentName() { 304 307 310 JBIDescriptor descriptor = createMock(JBIDescriptor.class); 311 ComponentDescription componentDescription = createMock(ComponentDescription.class); 312 Identification identification = createMock(Identification.class); 313 String componentName = "fooComponent"; 314 expect(identification.getName()).andReturn(componentName); 315 expect(componentDescription.getIdentification()).andReturn(identification); 316 expect(descriptor.getComponent()).andReturn(componentDescription); 317 replay(identification); 318 replay(componentDescription); 319 replay(descriptor); 320 323 componentContextImpl.descriptor = descriptor; 324 327 String result = componentContextImpl.getComponentName(); 328 assertNotNull("The result can not be null",result); 329 assertFalse("The result can not be an empty String",result.length()==0); 330 } 331 332 public void testGetDeliveryChannel() throws MessagingException { 333 337 Method method1 = null; 338 try { 339 method1 = ComponentContextImpl.class.getDeclaredMethod("createDeliveryChannel", new Class []{}); 340 } catch (Exception e) { 341 e.printStackTrace(); 342 fail("Could not retrieve mocked methods"); 343 } 344 Method [] mockedMethods = new Method []{method1}; 345 ComponentContextImpl componentContextImpl = createMock(ComponentContextImpl.class,mockedMethods); 346 DeliveryChannelImpl oldDeliveryChannel = createMock(DeliveryChannelImpl.class); 347 DeliveryChannelImpl newDeliveryChannel = createMock(DeliveryChannelImpl.class); 348 expect(oldDeliveryChannel.isOpened()).andReturn(false); 349 expect(componentContextImpl.createDeliveryChannel()).andReturn(newDeliveryChannel); 350 replay(oldDeliveryChannel); 351 replay(componentContextImpl); 352 componentContextImpl.deliveryChannel = oldDeliveryChannel; 353 356 DeliveryChannel result = componentContextImpl.getDeliveryChannel(); 357 assertNotNull("The result of GetDeliveryChannel can not be null",result); 358 assertEquals("Wrong DeliveryChannel returned",newDeliveryChannel, result); 359 verify(oldDeliveryChannel); 360 verify(componentContextImpl); 361 362 367 reset(oldDeliveryChannel); 368 componentContextImpl = createMock(ComponentContextImpl.class,mockedMethods); 369 expect(oldDeliveryChannel.isOpened()).andReturn(true); 370 replay(oldDeliveryChannel); 371 componentContextImpl.deliveryChannel = oldDeliveryChannel; 372 375 try { 376 componentContextImpl.getDeliveryChannel(); 377 fail("No exception was raised"); 378 } catch (Exception e) { 379 verify(oldDeliveryChannel); 380 } 381 382 386 componentContextImpl = createMock(ComponentContextImpl.class,mockedMethods); 387 expect(componentContextImpl.createDeliveryChannel()).andReturn(newDeliveryChannel); 388 replay(componentContextImpl); 389 392 try { 393 result = componentContextImpl.getDeliveryChannel(); 394 } catch (Exception e) { 395 fail("Exception during method invokation"); 396 } 397 assertNotNull("The result of GetDeliveryChannel can not be null",result); 398 assertEquals("Wrong DeliveryChannel returned",newDeliveryChannel, result); 399 verify(componentContextImpl); 400 } 401 402 public void testGetEndpoint() { 403 407 410 QName serviceName = new QName ("fooService"); 411 String name = "fooService"; 412 415 try { 416 componentContextImpl.getEndpoint(null, name); 417 fail("No exception was raised"); 418 } catch (Exception e) { 419 } 420 421 425 428 try { 429 componentContextImpl.getEndpoint(serviceName, null); 430 fail("No exception was raised"); 431 } catch (Exception e) { 432 } 433 437 440 componentContextImpl.getEndpoint(serviceName, name); 441 assertTrue(endpointService.isGetEndpoint()); 442 } 443 444 public void testGetEndpointDescriptor() { 445 449 452 try { 453 componentContextImpl.getEndpointDescriptor(null); 454 fail("no exception was raised"); 455 } catch (Exception e) { 456 } 458 462 465 Method method1 = null; 466 try { 467 method1 = ComponentContextImpl.class.getDeclaredMethod("getEndpoint", new Class []{QName .class,String .class}); 468 } catch (Exception e) { 469 e.printStackTrace(); 470 fail("Could not retrieve mocked methods"); 471 } 472 Method [] mockedMethods = new Method []{method1}; 473 componentContextImpl = createMock(ComponentContextImpl.class,mockedMethods); 474 InternalEndpoint internalEndpoint = new InternalEndpoint(new QName ( 475 "test"), "endpoint", "compo", "0", null); 476 internalEndpoint.setDescription("<test></test>"); 477 480 expect(componentContextImpl.getEndpoint(internalEndpoint.getServiceName(), internalEndpoint.getEndpointName())).andReturn(internalEndpoint); 481 replay(componentContextImpl); 482 485 try { 486 assertNotNull(componentContextImpl.getEndpointDescriptor(internalEndpoint)); 487 } catch (JBIException e){ 488 e.printStackTrace(); 489 fail("Error during method invokation"); 490 } 491 verify(componentContextImpl); 492 } 493 494 public void testGetEndpoints() { 495 ServiceEndpoint[] result = componentContextImpl.getEndpoints(null); 496 assertTrue(endpointService.isGetInternalEndpointsForInterface()); 497 assertNotNull("The result can not be null",result); 498 } 499 500 public void testGetEndpointsForService() { 501 505 508 try{ 509 componentContextImpl.getEndpointsForService(null); 510 fail("No exception was raised"); 511 } catch (Exception e) { 512 } 514 518 521 ServiceEndpoint[] endpoints = componentContextImpl.getEndpointsForService(new QName ("fooService")); 522 assertTrue(endpointService.isGetInternalEndpointsForService()); 523 assertNotNull("Result must not be null",endpoints); 524 } 525 526 public void testGetExternalEndpoints() { 527 531 534 try { 535 componentContextImpl.getExternalEndpoints(null); 536 fail("No exception was raised"); 537 } catch (Exception e) { 538 } 540 544 547 ServiceEndpoint[] endpoints = componentContextImpl.getExternalEndpoints(new QName ("fooService")); 548 assertTrue(endpointService.isGetExternalEndpointsForInterface()); 549 assertNotNull("Result must not be null",endpoints); 550 } 551 552 public void testGetExternalEndpointsForService() { 553 557 560 try { 561 componentContextImpl.getExternalEndpointsForService(null); 562 fail("No exception was raised"); 563 } catch (Exception e) { 564 } 566 570 573 ServiceEndpoint[] endpoints = componentContextImpl.getExternalEndpointsForService(new QName ("fooService")); 574 assertNotNull("Result must not be null",endpoints); 575 assertTrue(endpointService.isGetExternalEndpointsForService()); 576 } 577 578 public void testGetInstallRoot() { 579 componentContextImpl.installationRoot = "root"; 580 String result = componentContextImpl.getInstallRoot(); 581 assertNotNull("The result must not be null",result); 582 assertFalse("The result string must not be empty",result.length() == 0); 583 assertEquals(result, "root"); 584 } 585 586 public void testGetMBeanNames() { 587 MBeanNames beanNames = new MBeanNamesImpl("test"); 588 componentContextImpl.beanNames = beanNames; 589 MBeanNames result = componentContextImpl.getMBeanNames(); 590 assertNotNull("Result of getMBeanNames must not be null",result); 591 assertEquals(result, beanNames); 592 } 593 594 public void testGetMBeanServer() { 595 598 componentContextImpl.beanServer = createMock(MBeanServer .class); 599 602 MBeanServer result = componentContextImpl.getMBeanServer(); 603 assertNotNull("Result of getMBeanServer must not be null",result); 604 } 605 606 public void testGetNamingContext() { 607 try { 608 componentContextImpl.getNamingContext(); 609 fail(); 610 } catch (Error e) { 611 } 613 } 614 615 public void testGetTransactionManager() { 616 try { 617 componentContextImpl.getTransactionManager(); 618 fail(); 619 } catch (Error e) { 620 } 622 } 623 624 public void testGetWorkspaceRoot() { 625 628 631 componentContextImpl.workspaceRoot = "work"; 632 String result = componentContextImpl.getWorkspaceRoot(); 633 assertNotNull("The result of getWorkspaceRoot must be non null",result); 634 assertFalse("The result of getWorkspaceRoot must be non empty",result.length() == 0); 635 assertEquals(result, "work"); 636 } 637 638 public void testRegisterExternalEndpoint() { 639 643 646 try{ 647 componentContextImpl.registerExternalEndpoint(null); 648 fail("No Exception was raised"); 649 } catch (Exception e) { 650 } 652 656 ExternalEndpoint externalEndpoint = new ExternalEndpoint( 657 new InternalEndpoint(new QName ("service"), "endpoint", "compo", 658 "0", null), "compo", "0"); 659 componentContextImpl.externalEndpoints = new HashSet <ServiceEndpoint>(); 660 663 try { 664 componentContextImpl.registerExternalEndpoint(externalEndpoint); 665 } catch (JBIException e){ 666 e.printStackTrace(); 667 fail("Error during method invokation"); 668 } 669 assertEquals(componentContextImpl.externalEndpoints.size(), 1); 670 assertTrue(endpointService.isRegisterExternalEndpoint()); 671 } 672 673 public void testResolveEndpointReference() { 674 679 682 Method method1 = null; 683 Method method2 = null; 684 try { 685 method1 = LifeCycleManagerImpl.class.getDeclaredMethod("getEngineCompoLifeCycles", new Class []{}); 686 method2 = LifeCycleManagerImpl.class.getDeclaredMethod("getBindingCompoLifeCycles", new Class []{}); 687 } catch (Exception e) { 688 e.printStackTrace(); 689 fail("Could not retrieve mocked methods"); 690 } 691 Method [] mockedMethods = new Method []{method1, method2}; 692 LifeCycleManagerImpl lyfeCycleManager = createMock(LifeCycleManagerImpl.class,mockedMethods); 693 696 DocumentFragment epr = createMock(DocumentFragmentImpl.class); 697 ComponentLifeCycle compoLifeCycle = createMock(ComponentLifeCycle.class); 698 ComponentLifeCycle engineLifeCycle = createMock(ComponentLifeCycle.class); 699 Component bindingComponent = createMock(Component.class); 700 Component engineComponent = createMock(Component.class); 701 ObjectName engineName = createMock(ObjectName .class); 702 ObjectName bindingName = createMock(ObjectName .class); 703 Map <ObjectName ,ComponentLifeCycle> engines = new HashMap <ObjectName , ComponentLifeCycle>(); 704 Map <ObjectName ,ComponentLifeCycle> bindings = new HashMap <ObjectName , ComponentLifeCycle>(); 705 engines.put(engineName, engineLifeCycle); 706 bindings.put(bindingName, compoLifeCycle); 707 try { 708 expect(lyfeCycleManager.getEngineCompoLifeCycles()).andReturn(engines); 709 expect(lyfeCycleManager.getBindingCompoLifeCycles()).andReturn(bindings); 710 expect(compoLifeCycle.getComponent()).andReturn(bindingComponent); 711 expect(engineLifeCycle.getComponent()).andReturn(engineComponent); 712 } catch (Exception e) { 713 e.printStackTrace(); 714 fail("Could not register testing componentLyfeCycle"); 715 } 716 replay(lyfeCycleManager); 717 replay(compoLifeCycle); 718 replay(engineLifeCycle); 719 componentContextImpl.manager = lyfeCycleManager; 720 723 ServiceEndpoint result = null; 724 try { 725 result = componentContextImpl.resolveEndpointReference(epr); 726 } catch (Exception e) { 727 e.printStackTrace(); 728 fail("Exception during method invokation"); 729 } 730 assertNull("the result of resolveEndpointReference should be null",result); 731 verify(lyfeCycleManager); 732 verify(compoLifeCycle); 733 verify(engineLifeCycle); 734 738 741 try{ 742 componentContextImpl.resolveEndpointReference(null); 743 fail("No exception was raised"); 744 } catch (Exception e) { 745 } 747 } 748 749 } 750 | Popular Tags |