1 22 package org.objectweb.petals.jbi.management.service; 23 24 import static org.easymock.EasyMock.expect; 25 import static org.easymock.classextension.EasyMock.createMock; 26 import static org.easymock.classextension.EasyMock.replay; 27 import static org.easymock.classextension.EasyMock.reset; 28 import static org.easymock.classextension.EasyMock.verify; 29 30 import java.lang.reflect.Method ; 31 import java.util.HashMap ; 32 import java.util.Map ; 33 34 import javax.jbi.component.Component; 35 import javax.jbi.messaging.MessageExchange; 36 import javax.jbi.servicedesc.ServiceEndpoint; 37 import javax.management.ObjectName ; 38 39 import junit.framework.TestCase; 40 41 import org.objectweb.petals.jbi.component.lifecycle.ComponentLifeCycle; 42 import org.objectweb.petals.jbi.component.lifecycle.LifeCycleAbstract; 43 import org.objectweb.petals.jbi.messaging.MessageExchangeImpl; 44 import org.objectweb.petals.jbi.registry.InternalEndpoint; 45 import org.objectweb.petals.jbi.routing.mock.ComponentMock; 46 import org.objectweb.petals.util.LoggingUtil; 47 import org.w3c.dom.Document ; 48 49 54 public class AdminServiceTest extends TestCase { 55 56 57 public void testGetBindingComponentByName() { 58 61 AdminService adminService = new AdminService(); 62 LifeCycleManagerService lifeCycleManager = createMock(LifeCycleManagerImpl.class); 63 LoggingUtil log = createMock(LoggingUtil.class); 64 adminService.log = log; 65 ComponentLifeCycle lifecycle = createMock(ComponentLifeCycle.class); 66 ObjectName objectName = createMock(ObjectName .class); 67 70 Map <ObjectName , ComponentLifeCycle> bindings = new HashMap <ObjectName , ComponentLifeCycle>(); 71 bindings.put(objectName, lifecycle); 72 75 expect(lifeCycleManager.getBindingCompoLifeCycles()) 76 .andReturn(bindings); 77 expect(lifecycle.getName()).andReturn("bar"); 78 replay(lifecycle); 79 replay(lifeCycleManager); 80 adminService.manager = lifeCycleManager; 81 ObjectName result = null; 82 String name = "foo"; 83 86 result = adminService.getBindingComponentByName(name); 87 assertNull("The result of invokation should be null", result); 88 verify(lifeCycleManager); 89 verify(lifecycle); 90 93 reset(lifeCycleManager); 94 reset(lifecycle); 95 98 expect(lifeCycleManager.getBindingCompoLifeCycles()) 99 .andReturn(bindings); 100 expect(lifecycle.getName()).andReturn(name); 101 expect(lifecycle.getMBeanName()).andReturn(objectName); 102 replay(lifecycle); 103 replay(lifeCycleManager); 104 adminService.manager = lifeCycleManager; 105 108 result = adminService.getBindingComponentByName(name); 109 assertEquals("Wrong invokation response", result, objectName); 110 verify(lifecycle); 111 verify(lifeCycleManager); 112 } 113 114 public void testGetBindingComponents() { 115 118 AdminService adminService = new AdminService(); 119 LifeCycleManagerService lifeCycleManager = createMock(LifeCycleManagerImpl.class); 120 ComponentLifeCycle lifecycle = createMock(ComponentLifeCycle.class); 121 ObjectName objectName = createMock(ObjectName .class); 122 Map <ObjectName , ComponentLifeCycle> bindings = new HashMap <ObjectName , ComponentLifeCycle>(); 123 bindings.put(objectName, lifecycle); 124 127 expect(lifeCycleManager.getBindingCompoLifeCycles()) 128 .andReturn(bindings); 129 replay(lifeCycleManager); 130 adminService.manager = lifeCycleManager; 131 LoggingUtil log = createMock(LoggingUtil.class); 132 adminService.log = log; 133 136 ObjectName [] result = adminService.getBindingComponents(); 137 assertNotNull("The result of GetBindingComponents Must be non null", 138 result); 139 verify(lifeCycleManager); 140 assertTrue("Result does not have the right content", result[0] 141 .equals(objectName)); 142 } 143 144 public void testGetComponentByName() { 145 148 Method method1 = null; 149 Method method2 = null; 150 try { 151 method1 = AdminService.class.getDeclaredMethod( 152 "getBindingComponentByName", new Class [] {String .class}); 153 method2 = AdminService.class.getDeclaredMethod( 154 "getEngineComponentByName", new Class [] {String .class}); 155 156 } catch (Exception e) { 157 e.printStackTrace(); 158 fail("Could not retrieve mocked methods"); 159 } 160 163 Method [] mockedMethods = new Method [] {method1, method2}; 164 AdminService adminService = createMock(AdminService.class, 165 mockedMethods); 166 LifeCycleManagerService lifeCycleManager = new LifeCycleManagerImpl(); 167 adminService.manager = lifeCycleManager; 168 LoggingUtil log = createMock(LoggingUtil.class); 169 adminService.log = log; 170 String name = null; 171 174 try { 175 adminService.getComponentByName(name); 176 fail("No exception was raised"); 177 } catch (Exception e) { 178 } 180 181 184 name = ""; 185 188 try { 189 adminService.getComponentByName(name); 190 fail("No exception was raised"); 191 } catch (Exception e) { 192 } 194 197 name = "foo"; 198 expect(adminService.getBindingComponentByName(name)).andReturn(null); 199 expect(adminService.getEngineComponentByName(name)).andReturn(null); 200 replay(adminService); 201 204 ObjectName result = adminService.getComponentByName(name); 205 assertNull("The result is incorrect", result); 206 } 207 208 public void testGetEngineComponentByName() { 209 212 AdminService adminService = new AdminService(); 213 LifeCycleManagerService lifeCycleManager = createMock(LifeCycleManagerImpl.class); 214 LoggingUtil log = createMock(LoggingUtil.class); 215 adminService.log = log; 216 ComponentLifeCycle lifecycle = createMock(ComponentLifeCycle.class); 217 ObjectName objectName = createMock(ObjectName .class); 218 221 Map <ObjectName , ComponentLifeCycle> engines = new HashMap <ObjectName , ComponentLifeCycle>(); 222 engines.put(objectName, lifecycle); 223 226 expect(lifeCycleManager.getEngineCompoLifeCycles()).andReturn(engines); 227 expect(lifecycle.getName()).andReturn("bar"); 228 replay(lifecycle); 229 replay(lifeCycleManager); 230 adminService.manager = lifeCycleManager; 231 ObjectName result = null; 232 String name = "foo"; 233 236 result = adminService.getEngineComponentByName(name); 237 assertNull("The result of invokation should be null", result); 238 verify(lifeCycleManager); 239 verify(lifecycle); 240 243 reset(lifeCycleManager); 244 reset(lifecycle); 245 248 expect(lifeCycleManager.getEngineCompoLifeCycles()).andReturn(engines); 249 expect(lifecycle.getName()).andReturn(name); 250 expect(lifecycle.getMBeanName()).andReturn(objectName); 251 replay(lifecycle); 252 replay(lifeCycleManager); 253 adminService.manager = lifeCycleManager; 254 257 result = adminService.getEngineComponentByName(name); 258 assertEquals("Wrong invokation response", result, objectName); 259 verify(lifecycle); 260 verify(lifeCycleManager); 261 } 262 263 public void testGetEngineComponents() { 264 267 AdminService adminService = new AdminService(); 268 LifeCycleManagerService lifeCycleManager = createMock(LifeCycleManagerImpl.class); 269 ComponentLifeCycle lifecycle = createMock(ComponentLifeCycle.class); 270 ObjectName objectName = createMock(ObjectName .class); 271 Map <ObjectName , ComponentLifeCycle> engines = new HashMap <ObjectName , ComponentLifeCycle>(); 272 engines.put(objectName, lifecycle); 273 276 expect(lifeCycleManager.getEngineCompoLifeCycles()).andReturn(engines); 277 replay(lifeCycleManager); 278 adminService.manager = lifeCycleManager; 279 LoggingUtil log = createMock(LoggingUtil.class); 280 adminService.log = log; 281 284 ObjectName [] result = adminService.getEngineComponents(); 285 assertNotNull("The result of GetEngineComponents Must be non null", 286 result); 287 verify(lifeCycleManager); 288 assertTrue("Result does not have the right content", result[0] 289 .equals(objectName)); 290 } 291 292 public void testGetServiceDescription() { 293 297 AdminService adminService = new AdminService(); 298 LoggingUtil log = createMock(LoggingUtil.class); 299 adminService.log = log; 300 Document result = null; 301 String componentName = "fooComponent"; 302 ServiceEndpoint endpoint = createMock(InternalEndpoint.class); 303 ComponentLifeCycle lifeCycle = createMock(ComponentLifeCycle.class); 304 Component component = new ComponentMock(); 305 308 try { 309 result = adminService 310 .getServiceDescription(componentName, endpoint); 311 fail("No exception was raised"); 312 } catch (Exception e) { 313 } 315 319 LifeCycleManagerService lifeCycleManager = createMock(LifeCycleManagerImpl.class); 320 expect(lifeCycle.getComponent()).andReturn(component); 321 expect(lifeCycleManager.getComponentByName(componentName)).andReturn( 322 lifeCycle); 323 replay(lifeCycleManager); 324 replay(lifeCycle); 325 adminService.manager = lifeCycleManager; 326 329 try { 330 result = adminService 331 .getServiceDescription(componentName, endpoint); 332 } catch (Exception e) { 333 e.printStackTrace(); 334 fail("Error during method invokation"); 335 } 336 assertNull("The result should be null", result); 337 verify(lifeCycleManager); 338 verify(lifeCycle); 339 340 344 reset(lifeCycle); 345 reset(lifeCycleManager); 346 component = createMock(ComponentMock.class); 347 Document serviceDesc = createMock(Document .class); 348 expect(lifeCycleManager.getComponentByName(componentName)).andReturn( 349 lifeCycle); 350 expect(lifeCycle.getComponent()).andReturn(component); 351 expect(component.getServiceDescription(endpoint)) 352 .andReturn(serviceDesc); 353 replay(lifeCycleManager); 354 replay(lifeCycle); 355 replay(component); 356 359 try { 360 result = adminService 361 .getServiceDescription(componentName, endpoint); 362 } catch (Exception e) { 363 e.printStackTrace(); 364 fail("Error during method invokation"); 365 } 366 assertEquals("Wrong result for invokation", result, serviceDesc); 367 verify(lifeCycleManager); 368 verify(lifeCycle); 369 verify(component); 370 } 371 372 public void testGetSystemInfo() { 373 377 AdminService adminService = new AdminService(); 378 381 String result = adminService.getSystemInfo(); 382 assertNotNull("Result of getSystemInfo must not be null", result); 383 assertFalse("Result of getSystemInfo must not be empty", result 384 .length() == 0); 385 } 386 387 public void testGetSystemService() { 388 391 AdminService adminService = new AdminService(); 392 LoggingUtil log = createMock(LoggingUtil.class); 393 adminService.log = log; 394 397 400 String serviceName = null; 401 try { 402 adminService.getSystemService(serviceName); 403 fail("No exception was raised"); 404 } catch (Exception e) { 405 } 407 408 412 415 serviceName = ""; 416 419 try { 420 adminService.getSystemService(serviceName); 421 fail("No exception was raised"); 422 } catch (Exception e) { 423 } 425 426 430 433 serviceName = "fooService"; 434 LifeCycleManagerService lifeCycleManager = createMock(LifeCycleManagerImpl.class); 435 Map <ObjectName , LifeCycleAbstract> services = new HashMap <ObjectName , LifeCycleAbstract>(); 436 ObjectName objectName = createMock(ObjectName .class); 437 ComponentLifeCycle lifeCycle = createMock(ComponentLifeCycle.class); 438 services.put(objectName, lifeCycle); 439 expect(lifeCycleManager.getSystemServices()).andReturn(services); 440 expect(lifeCycle.getName()).andReturn(serviceName); 441 expect(lifeCycle.getMBeanName()).andReturn(objectName); 442 replay(lifeCycleManager); 443 replay(lifeCycle); 444 adminService.manager = lifeCycleManager; 445 ObjectName result = null; 446 449 try { 450 result = adminService.getSystemService(serviceName); 451 } catch (Exception e) { 452 fail("Error during invokation"); 453 } 454 assertEquals("Wrong result for invokation", result, objectName); 455 verify(lifeCycle); 456 verify(lifeCycleManager); 457 } 458 459 public void testGetSystemServices() { 460 464 AdminService adminService = new AdminService(); 465 LoggingUtil log = createMock(LoggingUtil.class); 466 adminService.log = log; 467 LifeCycleManagerService lifeCycleManager = createMock(LifeCycleManagerImpl.class); 468 Map <ObjectName , LifeCycleAbstract> services = new HashMap <ObjectName , LifeCycleAbstract>(); 469 ObjectName objectName = createMock(ObjectName .class); 470 ComponentLifeCycle lifeCycle = createMock(ComponentLifeCycle.class); 471 services.put(objectName, lifeCycle); 472 expect(lifeCycleManager.getSystemServices()).andReturn(services); 473 replay(lifeCycleManager); 474 adminService.manager = lifeCycleManager; 475 478 ObjectName [] result = adminService.getSystemServices(); 479 assertNotNull("The result of getSystemServices must not be null", 480 result); 481 assertTrue("The result is incorrect", result.length == 1); 482 verify(lifeCycleManager); 483 } 484 485 public void testIsBinding() { 486 490 AdminService adminService = new AdminService(); 491 LoggingUtil log = createMock(LoggingUtil.class); 492 adminService.log = log; 493 496 499 String componentName = null; 500 try { 501 adminService.isBinding(componentName); 502 fail("No exception was raised"); 503 } catch (Exception e) { 504 } 506 507 511 514 componentName = ""; 515 518 try { 519 adminService.isBinding(componentName); 520 fail("No exception was raised"); 521 } catch (Exception e) { 522 } 524 528 531 LifeCycleManagerService lifeCycleManager = createMock(LifeCycleManagerImpl.class); 532 ComponentLifeCycle lifecycle = createMock(ComponentLifeCycle.class); 533 ObjectName objectName = createMock(ObjectName .class); 534 537 Map <ObjectName , ComponentLifeCycle> bindings = new HashMap <ObjectName , ComponentLifeCycle>(); 538 bindings.put(objectName, lifecycle); 539 componentName = "fooCompo"; 540 543 expect(lifeCycleManager.getBindingCompoLifeCycles()) 544 .andReturn(bindings); 545 replay(lifeCycleManager); 546 adminService.manager = lifeCycleManager; 547 550 boolean result = adminService.isBinding(componentName); 551 assertFalse("Result of invokation should be false", result); 552 verify(lifeCycleManager); 553 } 554 555 public void testIsEngine() { 556 560 AdminService adminService = new AdminService(); 561 LoggingUtil log = createMock(LoggingUtil.class); 562 adminService.log = log; 563 566 569 String componentName = null; 570 try { 571 adminService.isEngine(componentName); 572 fail("No exception was raised"); 573 } catch (Exception e) { 574 } 576 577 581 584 componentName = ""; 585 588 try { 589 adminService.isEngine(componentName); 590 fail("No exception was raised"); 591 } catch (Exception e) { 592 } 594 598 601 LifeCycleManagerService lifeCycleManager = createMock(LifeCycleManagerImpl.class); 602 ComponentLifeCycle lifecycle = createMock(ComponentLifeCycle.class); 603 ObjectName objectName = createMock(ObjectName .class); 604 607 Map <ObjectName , ComponentLifeCycle> engines = new HashMap <ObjectName , ComponentLifeCycle>(); 608 engines.put(objectName, lifecycle); 609 componentName = "fooCompo"; 610 613 expect(lifeCycleManager.getEngineCompoLifeCycles()).andReturn(engines); 614 replay(lifeCycleManager); 615 adminService.manager = lifeCycleManager; 616 619 boolean result = adminService.isEngine(componentName); 620 assertFalse("Result of invokation should be false", result); 621 verify(lifeCycleManager); 622 } 623 624 public void testIsExchangeWithConsumerOkayForComponent() { 625 628 AdminService adminService = new AdminService(); 629 LifeCycleManagerService lifeCycleManager = createMock(LifeCycleManagerImpl.class); 630 adminService.manager = lifeCycleManager; 631 LoggingUtil log = createMock(LoggingUtil.class); 632 adminService.log = log; 633 636 String componentName = "foo"; 637 ServiceEndpoint internalEndpoint = createMock(InternalEndpoint.class); 638 MessageExchange exchange = createMock(MessageExchangeImpl.class); 639 642 try { 643 adminService.isExchangeWithConsumerOkayForComponent(componentName, 644 internalEndpoint, exchange); 645 fail("No exception was raised"); 646 } catch (Exception e) { 647 } 649 650 653 656 Component component = new ComponentMock(); 657 ComponentLifeCycle lifeCycle = createMock(ComponentLifeCycle.class); 658 lifeCycleManager = createMock(LifeCycleManagerImpl.class); 659 662 expect(lifeCycle.getComponent()).andReturn(component); 663 expect(lifeCycleManager.getComponentByName(componentName)).andReturn( 664 lifeCycle); 665 replay(lifeCycleManager); 666 replay(lifeCycle); 667 adminService.manager = lifeCycleManager; 668 boolean result = false; 669 672 try { 673 result = adminService.isExchangeWithConsumerOkayForComponent( 674 componentName, internalEndpoint, exchange); 675 } catch (Exception e) { 676 e.printStackTrace(); 677 fail("Error during invokation "); 678 } 679 assertTrue("the result of invokation should be true", result); 680 verify(lifeCycle); 681 verify(lifeCycleManager); 682 } 683 684 public void testIsExchangeWithProviderOkayForComponent() { 685 688 AdminService adminService = new AdminService(); 689 LifeCycleManagerService lifeCycleManager = createMock(LifeCycleManagerImpl.class); 690 adminService.manager = lifeCycleManager; 691 LoggingUtil log = createMock(LoggingUtil.class); 692 adminService.log = log; 693 696 String componentName = "foo"; 697 ServiceEndpoint internalEndpoint = createMock(InternalEndpoint.class); 698 MessageExchange exchange = createMock(MessageExchangeImpl.class); 699 702 try { 703 adminService.isExchangeWithProviderOkayForComponent(componentName, 704 internalEndpoint, exchange); 705 fail("No exception was raised"); 706 } catch (Exception e) { 707 } 709 710 713 716 Component component = new ComponentMock(); 717 ComponentLifeCycle lifeCycle = createMock(ComponentLifeCycle.class); 718 lifeCycleManager = createMock(LifeCycleManagerImpl.class); 719 722 expect(lifeCycle.getComponent()).andReturn(component); 723 expect(lifeCycleManager.getComponentByName(componentName)).andReturn( 724 lifeCycle); 725 replay(lifeCycleManager); 726 replay(lifeCycle); 727 adminService.manager = lifeCycleManager; 728 boolean result = false; 729 732 try { 733 result = adminService.isExchangeWithProviderOkayForComponent( 734 componentName, internalEndpoint, exchange); 735 } catch (Exception e) { 736 e.printStackTrace(); 737 fail("Error during invokation "); 738 } 739 assertTrue("the result of invokation should be true", result); 740 verify(lifeCycle); 741 verify(lifeCycleManager); 742 } 743 744 @Override 745 protected void setUp() { 746 747 } 748 } 749 | Popular Tags |