1 22 package org.objectweb.petals.jbi.management.deployment; 23 24 import static org.easymock.EasyMock.expect; 25 import static org.easymock.EasyMock.isA; 26 import static org.easymock.classextension.EasyMock.createMock; 27 import static org.easymock.classextension.EasyMock.replay; 28 import static org.easymock.classextension.EasyMock.verify; 29 30 import java.lang.reflect.Method ; 31 import java.net.URI ; 32 import java.util.ArrayList ; 33 import java.util.HashMap ; 34 import java.util.List ; 35 import java.util.Map ; 36 37 import javax.jbi.component.Component; 38 import javax.jbi.component.ServiceUnitManager; 39 import javax.jbi.management.DeploymentException; 40 41 import junit.framework.TestCase; 42 43 import org.objectweb.petals.jbi.component.lifecycle.ComponentLifeCycle; 44 import org.objectweb.petals.jbi.component.lifecycle.LifeCycleAbstract; 45 import org.objectweb.petals.jbi.component.lifecycle.ServiceAssemblyLifeCycle; 46 import org.objectweb.petals.jbi.management.service.LifeCycleManagerImpl; 47 import org.objectweb.petals.jbi.management.service.LifeCycleManagerService; 48 import org.objectweb.petals.jbi.routing.mock.ComponentMock; 49 import org.objectweb.petals.tools.jbicommon.descriptor.Identification; 50 import org.objectweb.petals.tools.jbicommon.descriptor.ServiceAssembly; 51 import org.objectweb.petals.tools.jbicommon.descriptor.ServiceUnit; 52 import org.objectweb.petals.util.LoggingUtil; 53 import org.objectweb.util.monolog.api.Logger; 54 import org.objectweb.util.monolog.wrapper.printwriter.LoggerImpl; 55 56 61 public class DeploymentServiceTest extends TestCase { 62 63 public void testCanDeployToComponent() { 64 68 DeploymentServiceImpl deploymentService = new DeploymentServiceImpl(); 69 72 String componentName = null; 73 LoggingUtil log = createMock(LoggingUtil.class); 74 deploymentService.log = log; 75 78 try { 79 deploymentService.canDeployToComponent(componentName); 80 fail("No exception was raised"); 81 } catch (Exception e) { 82 assertTrue("Wrong exception thrown", e.getMessage().contains( 83 "must not be null")); 84 } 85 86 90 93 componentName = ""; 94 97 try { 98 deploymentService.canDeployToComponent(componentName); 99 fail("No exception was raised"); 100 } catch (Exception e) { 101 assertTrue("Wrong exception thrown", e.getMessage().contains( 102 "must not be empty")); 103 } 104 105 109 112 componentName = "fooCompo"; 113 LifeCycleManagerService lifeCycleManager = createMock(LifeCycleManagerImpl.class); 114 ComponentLifeCycle componentLifeCycle = createMock(ComponentLifeCycle.class); 115 Component component = new ComponentMock() { 116 public javax.jbi.component.ServiceUnitManager getServiceUnitManager() { 117 return new ServiceUnitManager() { 118 119 public String deploy(String serviceUnitName, 120 String serviceUnitRootPath) 121 throws DeploymentException { 122 return null; 123 } 124 125 public void init(String serviceUnitName, 126 String serviceUnitRootPath) 127 throws DeploymentException { 128 129 } 130 131 public void shutDown(String serviceUnitName) 132 throws DeploymentException { 133 134 } 135 136 public void start(String serviceUnitName) 137 throws DeploymentException { 138 139 } 140 141 public void stop(String serviceUnitName) 142 throws DeploymentException { 143 144 } 145 146 public String undeploy(String serviceUnitName, 147 String serviceUnitRootPath) 148 throws DeploymentException { 149 return null; 150 } 151 152 }; 153 } 154 }; 155 156 159 expect(lifeCycleManager.getComponentByName(componentName)).andReturn( 160 componentLifeCycle); 161 expect(componentLifeCycle.getCurrentState()).andReturn( 162 LifeCycleAbstract.STARTED); 163 expect(componentLifeCycle.getComponent()).andReturn(component); 164 replay(lifeCycleManager); 165 replay(componentLifeCycle); 166 deploymentService.managerService = lifeCycleManager; 167 boolean result = false; 168 171 try { 172 result = deploymentService.canDeployToComponent(componentName); 173 } catch (Exception e) { 174 fail("Error during invokation"); 175 } 176 assertTrue("Wrong invokation result", result); 177 } 178 179 public void testGetComponentsForDeployedServiceAssembly() { 180 184 DeploymentServiceImpl deploymentService = new DeploymentServiceImpl(); 185 188 String serviceAssemblyName = null; 189 LoggingUtil log = createMock(LoggingUtil.class); 190 deploymentService.log = log; 191 194 try { 195 deploymentService 196 .getComponentsForDeployedServiceAssembly(serviceAssemblyName); 197 fail("No exception was raised"); 198 } catch (Exception e) { 199 assertTrue("Wrong exception thrown", e.getMessage().contains( 200 "must not be null")); 201 } 202 203 207 210 serviceAssemblyName = ""; 211 214 try { 215 deploymentService 216 .getComponentsForDeployedServiceAssembly(serviceAssemblyName); 217 fail("No exception was raised"); 218 } catch (Exception e) { 219 assertTrue("Wrong exception thrown", e.getMessage().contains( 220 "must not be empty")); 221 } 222 226 229 LifeCycleManagerService lifeCycleManager = createMock(LifeCycleManagerImpl.class); 230 deploymentService.managerService = lifeCycleManager; 231 serviceAssemblyName = "fooSA"; 232 235 try { 236 deploymentService 237 .getComponentsForDeployedServiceAssembly(serviceAssemblyName); 238 fail("No exception was raised"); 239 } catch (Exception e) { 240 assertTrue( 241 "Wrong exception thrown", 242 e 243 .getMessage() 244 .contains( 245 "Cannot retrieve service assembly with the specified name : ")); 246 } 247 248 251 254 lifeCycleManager = createMock(LifeCycleManagerImpl.class); 255 serviceAssemblyName = "fooSA"; 256 ServiceAssemblyLifeCycle saLifeCycle = createMock(ServiceAssemblyLifeCycle.class); 257 ServiceAssembly sa = createMock(ServiceAssembly.class); 258 List <ServiceUnit> sus = new ArrayList <ServiceUnit>(); 259 ServiceUnit su = createMock(ServiceUnit.class); 260 sus.add(su); 261 String componentName = "componentName"; 262 265 expect(lifeCycleManager.getServiceAssemblyByName(serviceAssemblyName)) 266 .andReturn(saLifeCycle); 267 expect(saLifeCycle.getServiceAssembly()).andReturn(sa); 268 expect(sa.getServiceUnits()).andReturn(sus); 269 expect(su.getTargetComponentName()).andReturn(componentName); 270 replay(lifeCycleManager); 271 replay(sa); 272 replay(saLifeCycle); 273 replay(su); 274 deploymentService.managerService = lifeCycleManager; 275 String [] result = null; 276 279 try { 280 result = deploymentService 281 .getComponentsForDeployedServiceAssembly(serviceAssemblyName); 282 } catch (Exception e) { 283 e.printStackTrace(); 284 fail("Error during invokation"); 285 } 286 assertNotNull( 287 "The result of getComponentsForDeployedServiceAssembly must not be null", 288 result); 289 assertEquals("Wrong invokation result", result[0], componentName); 290 assertEquals("Wrong invokation result", result.length, 1); 291 verify(lifeCycleManager); 292 verify(sa); 293 verify(saLifeCycle); 294 verify(su); 295 } 296 297 public void testGetDeployedServiceAssemblies() { 298 302 DeploymentServiceImpl deploymentService = new DeploymentServiceImpl(); 303 306 LifeCycleManagerImpl lifeCycleManager = createMock(LifeCycleManagerImpl.class); 307 Map <String , ServiceAssemblyLifeCycle> serviceAssemblies = new HashMap <String , ServiceAssemblyLifeCycle>(); 308 String saName = "service assembly"; 309 ServiceAssemblyLifeCycle saLifeCycle = createMock(ServiceAssemblyLifeCycle.class); 310 serviceAssemblies.put(saName, saLifeCycle); 311 314 expect(lifeCycleManager.getServiceAssemblyLifeCycles()).andReturn( 315 serviceAssemblies); 316 replay(lifeCycleManager); 317 deploymentService.managerService = lifeCycleManager; 318 String [] result = null; 319 322 try { 323 result = deploymentService.getDeployedServiceAssemblies(); 324 } catch (Exception e) { 325 e.printStackTrace(); 326 fail("Error during invokation"); 327 } 328 assertNotNull( 329 "Result of getDeployedServiceAssemblies must not be null", 330 result); 331 assertEquals("Wrong invokation result", result[0], saName); 332 assertEquals("Wrong invokation result", result.length, 1); 333 verify(lifeCycleManager); 334 } 335 336 public void testGetDeployedServiceAssembliesForComponent() { 337 341 DeploymentServiceImpl deploymentService = new DeploymentServiceImpl(); 342 345 String componentName = null; 346 LoggingUtil log = createMock(LoggingUtil.class); 347 deploymentService.log = log; 348 351 try { 352 deploymentService 353 .getDeployedServiceAssembliesForComponent(componentName); 354 fail("No exception raised"); 355 } catch (Exception e) { 356 assertTrue("Wrong exception thrown", e.getMessage().contains( 357 "must not be null")); 358 } 359 363 366 componentName = ""; 367 370 try { 371 deploymentService 372 .getDeployedServiceAssembliesForComponent(componentName); 373 fail("No exception raised"); 374 } catch (Exception e) { 375 assertTrue("Wrong exception thrown", e.getMessage().contains( 376 "must not be empty")); 377 } 378 379 382 385 componentName = "fooComponent"; 386 LifeCycleManagerImpl lifeCycleManager = createMock(LifeCycleManagerImpl.class); 387 Map <String , ServiceAssemblyLifeCycle> serviceAssemblies = new HashMap <String , ServiceAssemblyLifeCycle>(); 388 String saName = "service assembly"; 389 ServiceAssemblyLifeCycle saLifeCycle = createMock(ServiceAssemblyLifeCycle.class); 390 serviceAssemblies.put(saName, saLifeCycle); 391 ServiceAssembly sa = createMock(ServiceAssembly.class); 392 List <ServiceUnit> sus = new ArrayList <ServiceUnit>(); 393 ServiceUnit su = createMock(ServiceUnit.class); 394 sus.add(su); 395 Identification identification = createMock(Identification.class); 396 399 expect(lifeCycleManager.getServiceAssemblyLifeCycles()).andReturn( 400 serviceAssemblies); 401 expect(saLifeCycle.getServiceAssembly()).andReturn(sa).times(2); 402 expect(sa.getServiceUnits()).andReturn(sus); 403 expect(sa.getIdentification()).andReturn(identification); 404 expect(identification.getName()).andReturn(saName); 405 expect(su.getTargetComponentName()).andReturn(componentName); 406 replay(sa); 407 replay(su); 408 replay(lifeCycleManager); 409 replay(saLifeCycle); 410 replay(identification); 411 deploymentService.managerService = lifeCycleManager; 412 String [] result = null; 413 416 try { 417 result = deploymentService 418 .getDeployedServiceAssembliesForComponent(componentName); 419 } catch (Exception e) { 420 fail("Error during invokation"); 421 } 422 assertNotNull( 423 "Result of getDeployedServiceAssembliesForComponent must not be null", 424 result); 425 assertEquals("Wrong invokation result", result[0], saName); 426 verify(saLifeCycle); 427 verify(lifeCycleManager); 428 verify(sa); 429 verify(su); 430 verify(identification); 431 } 432 433 public void testGetDeployedServiceUnitList() { 434 438 DeploymentServiceImpl deploymentService = new DeploymentServiceImpl(); 439 442 String componentName = null; 443 LoggingUtil log = createMock(LoggingUtil.class); 444 deploymentService.log = log; 445 448 try { 449 deploymentService.getDeployedServiceUnitList(componentName); 450 fail("No exception raised"); 451 } catch (Exception e) { 452 assertTrue("Wrong exception thrown", e.getMessage().contains( 453 "must not be null")); 454 } 455 459 462 componentName = ""; 463 466 try { 467 deploymentService.getDeployedServiceUnitList(componentName); 468 fail("No exception raised"); 469 } catch (Exception e) { 470 assertTrue("Wrong exception thrown", e.getMessage().contains( 471 "must not be empty")); 472 } 473 474 477 480 componentName = "fooComponent"; 481 LifeCycleManagerImpl lifeCycleManager = createMock(LifeCycleManagerImpl.class); 482 Map <String , ServiceAssemblyLifeCycle> serviceAssemblies = new HashMap <String , ServiceAssemblyLifeCycle>(); 483 String suName = "service unit"; 484 ServiceAssemblyLifeCycle saLifeCycle = createMock(ServiceAssemblyLifeCycle.class); 485 serviceAssemblies.put(suName, saLifeCycle); 486 ServiceAssembly sa = createMock(ServiceAssembly.class); 487 List <ServiceUnit> sus = new ArrayList <ServiceUnit>(); 488 ServiceUnit su = createMock(ServiceUnit.class); 489 sus.add(su); 490 Identification identification = createMock(Identification.class); 491 494 expect(lifeCycleManager.getServiceAssemblyLifeCycles()).andReturn( 495 serviceAssemblies); 496 expect(saLifeCycle.getServiceAssembly()).andReturn(sa); 497 expect(sa.getServiceUnits()).andReturn(sus); 498 expect(identification.getName()).andReturn(suName); 499 expect(su.getTargetComponentName()).andReturn(componentName); 500 expect(su.getIdentification()).andReturn(identification); 501 replay(sa); 502 replay(su); 503 replay(lifeCycleManager); 504 replay(saLifeCycle); 505 replay(identification); 506 deploymentService.managerService = lifeCycleManager; 507 String [] result = null; 508 511 try { 512 result = deploymentService 513 .getDeployedServiceUnitList(componentName); 514 } catch (Exception e) { 515 fail("Error during invokation"); 516 } 517 assertNotNull( 518 "Result of getDeployedServiceAssembliesForComponent must not be null", 519 result); 520 assertEquals("Wrong invokation result", result[0], suName); 521 verify(saLifeCycle); 522 verify(lifeCycleManager); 523 verify(sa); 524 verify(su); 525 verify(identification); 526 } 527 528 public void testGetServiceAssemblyDescriptor() { 529 533 536 Method method1 = null; 537 try { 538 method1 = DeploymentServiceImpl.class.getDeclaredMethod( 539 "getJbiDescriptorAsString", new Class [] {URI .class}); 540 } catch (Exception e) { 541 e.printStackTrace(); 542 fail("Could not retrieve mocked methods"); 543 } 544 Method [] mockedMethods = new Method [] {method1}; 545 DeploymentServiceImpl deploymentService = createMock( 546 DeploymentServiceImpl.class, mockedMethods); 547 550 String serviceAssemblyName = null; 551 LoggingUtil log = createMock(LoggingUtil.class); 552 deploymentService.log = log; 553 556 try { 557 deploymentService.getServiceAssemblyDescriptor(serviceAssemblyName); 558 fail("No exception raised"); 559 } catch (Exception e) { 560 assertTrue("Wrong exception thrown", e.getMessage().contains( 561 " must not be null")); 562 } 563 567 570 serviceAssemblyName = ""; 571 574 try { 575 deploymentService.getServiceAssemblyDescriptor(serviceAssemblyName); 576 fail("No exception raised"); 577 } catch (Exception e) { 578 assertTrue("Wrong exception thrown", e.getMessage().contains( 579 "must not be empty")); 580 } 581 582 585 588 LifeCycleManagerImpl lifeCycleManager = createMock(LifeCycleManagerImpl.class); 589 serviceAssemblyName = "service assembly"; 590 ServiceAssemblyLifeCycle saLifeCycle = createMock(ServiceAssemblyLifeCycle.class); 591 URI installationURI = null; 592 try { 593 installationURI = new URI ("file://installation"); 594 } catch (Exception e) { 595 e.printStackTrace(); 596 fail("Could not create test parameters"); 597 } 598 org.objectweb.petals.util.monolog.wrapper.javalog.Logger logger = createMock(org.objectweb.petals.util.monolog.wrapper.javalog.Logger.class); 599 deploymentService.logger = logger; 600 String resultDescriptor = "result"; 601 String result = null; 602 605 expect(lifeCycleManager.getServiceAssemblyByName(serviceAssemblyName)) 606 .andReturn(saLifeCycle); 607 expect(saLifeCycle.getInstallationRoot()).andReturn(installationURI); 608 expect(deploymentService.getJbiDescriptorAsString(isA(URI .class))) 609 .andReturn(resultDescriptor); 610 replay(lifeCycleManager); 611 replay(saLifeCycle); 612 replay(deploymentService); 613 deploymentService.managerService = lifeCycleManager; 614 617 try { 618 result = deploymentService 619 .getServiceAssemblyDescriptor(serviceAssemblyName); 620 } catch (Exception e) { 621 fail("Error during invokation"); 622 } 623 verify(saLifeCycle); 624 verify(lifeCycleManager); 625 verify(deploymentService); 626 assertNotNull( 627 "Result of getServiceAssemblyDescriptor must be non null", 628 result); 629 assertEquals("Wrong invokation result", result, resultDescriptor); 630 } 631 632 public void testIsDeployedServiceUnit() { 633 637 DeploymentServiceImpl deploymentService = new DeploymentServiceImpl(); 638 642 LoggingUtil log = createMock(LoggingUtil.class); 643 deploymentService.log = log; 644 String serviceUnitName = null; 645 String componentName = null; 646 649 try { 650 deploymentService.isDeployedServiceUnit(componentName, 651 serviceUnitName); 652 fail("No exception raised"); 653 } catch (Exception e) { 654 assertTrue("Wrong exception thrown", e.getMessage().contains( 655 "must not be null")); 656 } 657 658 662 665 serviceUnitName = ""; 666 669 try { 670 deploymentService.isDeployedServiceUnit(componentName, 671 serviceUnitName); 672 fail("No exception raised"); 673 } catch (Exception e) { 674 assertTrue("Wrong exception thrown", e.getMessage().contains( 675 "must not be empty")); 676 } 677 678 682 685 serviceUnitName = "service unit name"; 686 689 try { 690 deploymentService.isDeployedServiceUnit(componentName, 691 serviceUnitName); 692 fail("No exception raised"); 693 } catch (Exception e) { 694 assertTrue("Wrong exception thrown", e.getMessage().contains( 695 "must not be null")); 696 } 697 698 702 705 componentName = ""; 706 709 try { 710 deploymentService.isDeployedServiceUnit(componentName, 711 serviceUnitName); 712 fail("No exception raised"); 713 } catch (Exception e) { 714 assertTrue("Wrong exception thrown", e.getMessage().contains( 715 "must not be empty")); 716 } 717 718 722 725 componentName = "fooComponent"; 726 String saName = "service assembly name"; 727 LifeCycleManagerImpl lifeCycleManager = createMock(LifeCycleManagerImpl.class); 728 Map <String , ServiceAssemblyLifeCycle> serviceAssemblies = new HashMap <String , ServiceAssemblyLifeCycle>(); 729 ServiceAssemblyLifeCycle saLifeCycle = createMock(ServiceAssemblyLifeCycle.class); 730 serviceAssemblies.put(saName, saLifeCycle); 731 ServiceAssembly sa = createMock(ServiceAssembly.class); 732 List <ServiceUnit> sus = new ArrayList <ServiceUnit>(); 733 ServiceUnit su = createMock(ServiceUnit.class); 734 sus.add(su); 735 Identification identification = createMock(Identification.class); 736 739 expect(lifeCycleManager.getServiceAssemblyLifeCycles()).andReturn( 740 serviceAssemblies); 741 expect(saLifeCycle.getServiceAssembly()).andReturn(sa); 742 expect(sa.getServiceUnits()).andReturn(sus); 743 expect(identification.getName()).andReturn(serviceUnitName); 744 expect(su.getTargetComponentName()).andReturn(componentName); 745 expect(su.getIdentification()).andReturn(identification); 746 replay(sa); 747 replay(su); 748 replay(lifeCycleManager); 749 replay(saLifeCycle); 750 replay(identification); 751 deploymentService.managerService = lifeCycleManager; 752 boolean result = false; 753 756 try { 757 result = deploymentService.isDeployedServiceUnit(componentName, 758 serviceUnitName); 759 } catch (Exception e) { 760 e.printStackTrace(); 761 fail("Error during invokation"); 762 } 763 assertTrue("Wrong invokation result", result); 764 verify(sa); 765 verify(su); 766 verify(lifeCycleManager); 767 verify(saLifeCycle); 768 verify(identification); 769 } 770 771 public void testShutdown() { 772 776 DeploymentServiceImpl deploymentService = new DeploymentServiceImpl(); 777 780 LoggingUtil log = createMock(LoggingUtil.class); 781 deploymentService.log = log; 782 String serviceAssemblyName = null; 783 786 try { 787 deploymentService.shutDown(serviceAssemblyName); 788 fail("No exception was raised"); 789 } catch (Exception e) { 790 assertTrue("Wrong exception thrown", e.getMessage().contains( 791 "must not be null")); 792 } 793 797 800 serviceAssemblyName = ""; 801 804 try { 805 deploymentService.shutDown(serviceAssemblyName); 806 fail("No exception was raised"); 807 } catch (Exception e) { 808 assertTrue("Wrong exception thrown", e.getMessage().contains( 809 "must not be empty")); 810 } 811 } 812 813 public void testStop() { 814 817 DeploymentServiceImpl deploymentService = new DeploymentServiceImpl(); 818 821 LoggingUtil log = createMock(LoggingUtil.class); 822 deploymentService.log = log; 823 824 Logger logger = createMock(LoggerImpl.class); 825 deploymentService.logger = logger; 826 829 deploymentService.stop(); 830 assertNull("ShutDown action was not properly realized",deploymentService.logger); 831 } 832 833 public void testStart() { 834 838 DeploymentServiceImpl deploymentService = new DeploymentServiceImpl(); 839 842 LoggingUtil log = createMock(LoggingUtil.class); 843 deploymentService.log = log; 844 String serviceAssemblyName = null; 845 848 try { 849 deploymentService.start(serviceAssemblyName); 850 fail("No exception was raised"); 851 } catch (Exception e) { 852 assertTrue("Wrong exception thrown", e.getMessage().contains( 853 "must not be null")); 854 } 855 859 862 serviceAssemblyName = ""; 863 866 try { 867 deploymentService.start(serviceAssemblyName); 868 fail("No exception was raised"); 869 } catch (Exception e) { 870 assertTrue("Wrong exception thrown", e.getMessage().contains( 871 "must not be empty")); 872 } 873 } 874 875 public void testStopSA() { 876 880 DeploymentServiceImpl deploymentService = new DeploymentServiceImpl(); 881 884 LoggingUtil log = createMock(LoggingUtil.class); 885 deploymentService.log = log; 886 String serviceAssemblyName = null; 887 890 try { 891 deploymentService.stop(serviceAssemblyName); 892 fail("No exception was raised"); 893 } catch (Exception e) { 894 assertTrue("Wrong exception thrown", e.getMessage().contains( 895 "must not be null")); 896 } 897 901 904 serviceAssemblyName = ""; 905 908 try { 909 deploymentService.stop(serviceAssemblyName); 910 fail("No exception was raised"); 911 } catch (Exception e) { 912 assertTrue("Wrong exception thrown", e.getMessage().contains( 913 "must not be empty")); 914 } 915 } 916 917 public void testUndeploy() { 918 919 } 920 921 @Override 922 protected void setUp() { 923 924 } 925 } 926 | Popular Tags |