1 22 package org.objectweb.petals.jbi.management.service; 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.reset; 29 import static org.easymock.classextension.EasyMock.verify; 30 31 import java.io.File ; 32 import java.lang.reflect.Method ; 33 import java.net.URI ; 34 import java.util.ArrayList ; 35 import java.util.HashMap ; 36 import java.util.List ; 37 import java.util.Map ; 38 import java.util.zip.ZipFile ; 39 40 import javax.management.AttributeNotFoundException ; 41 import javax.management.InstanceNotFoundException ; 42 import javax.management.MBeanException ; 43 import javax.management.MBeanServer ; 44 import javax.management.ObjectName ; 45 import javax.management.ReflectionException ; 46 47 import junit.framework.TestCase; 48 49 import org.objectweb.petals.PetalsException; 50 import org.objectweb.petals.classloader.LoaderManager; 51 import org.objectweb.petals.classloader.LoaderManagerImpl; 52 import org.objectweb.petals.jbi.component.context.ComponentContextImpl; 53 import org.objectweb.petals.jbi.component.lifecycle.ComponentLifeCycle; 54 import org.objectweb.petals.jbi.component.lifecycle.Installer; 55 import org.objectweb.petals.jbi.component.lifecycle.LifeCycleMBean; 56 import org.objectweb.petals.jbi.management.systemstate.ComponentState; 57 import org.objectweb.petals.jbi.management.systemstate.SharedLibraryState; 58 import org.objectweb.petals.jbi.management.systemstate.SystemState; 59 import org.objectweb.petals.jbi.management.systemstate.SystemStateImpl; 60 import org.objectweb.petals.jbi.mock.MockLifeCycleManagerService; 61 import org.objectweb.petals.repository.RepositoryImpl; 62 import org.objectweb.petals.repository.RepositoryService; 63 import org.objectweb.petals.tools.jbicommon.descriptor.ComponentDescription; 64 import org.objectweb.petals.tools.jbicommon.descriptor.Identification; 65 import org.objectweb.petals.tools.jbicommon.descriptor.JBIDescriptor; 66 import org.objectweb.petals.tools.jbicommon.descriptor.SharedLibrary; 67 import org.objectweb.petals.tools.jbicommon.descriptor.SharedLibraryList; 68 import org.objectweb.petals.util.LoggingUtil; 69 70 75 public class InstallationServiceTest extends TestCase { 76 77 private File testDataDir = null; 78 79 public void testCheckClassPathElements() { 80 83 86 Method method1 = null; 87 try { 88 method1 = InstallationServiceImpl.class.getDeclaredMethod( 89 "hasEntry", new Class [] {ZipFile .class, String .class}); 90 } catch (Exception e) { 91 e.printStackTrace(); 92 fail("Could not retrieve mocked methods"); 93 } 94 Method [] mockedMethods = new Method [] {method1}; 95 InstallationServiceImpl installationService = createMock( 96 InstallationServiceImpl.class, mockedMethods); 97 100 List <String > classPathElements = new ArrayList <String >(); 101 classPathElements.add(""); 102 ZipFile archive = createMock(ZipFile .class); 103 106 try { 107 installationService.checkClassPathElements(archive, 108 classPathElements); 109 fail("No exception raised"); 110 } catch (Exception e) { 111 } 113 114 117 120 classPathElements = new ArrayList <String >(); 121 String element = "someElement"; 122 classPathElements.add(element); 123 126 expect(installationService.hasEntry(archive, element)).andReturn(false); 127 replay(installationService); 128 131 try { 132 installationService.checkClassPathElements(archive, 133 classPathElements); 134 fail("No exception raised"); 135 } catch (Exception e) { 136 verify(installationService); 137 } 138 } 139 140 275 public void testCheckComponentShutdownStateForSL() { 276 280 InstallationServiceImpl installationService = new InstallationServiceImpl(); 281 284 LifeCycleManagerService lcms = createMock(LifeCycleManagerImpl.class); 285 Map <ObjectName , ComponentLifeCycle> bindingComponentLifecycles = new HashMap <ObjectName , ComponentLifeCycle>(); 286 Map <ObjectName , ComponentLifeCycle> engineLifecycles = new HashMap <ObjectName , ComponentLifeCycle>(); 287 ObjectName componentName = null; 288 ObjectName engineName = null; 289 try { 290 componentName = new ObjectName ("component", "component", 291 "component"); 292 engineName = new ObjectName ("engine", "engine", "engine"); 293 } catch (Exception e) { 294 e.printStackTrace(); 295 fail("Could not create test parameters"); 296 } 297 ComponentLifeCycle componentLifeCycle = createMock(ComponentLifeCycle.class); 298 LoggingUtil log = createMock(LoggingUtil.class); 299 bindingComponentLifecycles.put(componentName, componentLifeCycle); 300 engineLifecycles.put(engineName, componentLifeCycle); 301 ComponentDescription componentDescription = createMock(ComponentDescription.class); 302 Identification identification = createMock(Identification.class); 303 SharedLibraryList sharedLibrayList = createMock(SharedLibraryList.class); 304 List <SharedLibraryList> slList = new ArrayList <SharedLibraryList>(); 305 slList.add(sharedLibrayList); 306 String slName = "sharedLibraryName"; 307 String compName = "compName"; 308 installationService.log = log; 309 312 try { 313 expect(lcms.getBindingCompoLifeCycles()).andReturn( 314 bindingComponentLifecycles); 315 expect(lcms.getEngineCompoLifeCycles()).andReturn(engineLifecycles); 316 expect(componentLifeCycle.getComponentDescription()).andReturn( 317 componentDescription); 318 expect(componentLifeCycle.getCurrentState()).andReturn( 319 LifeCycleMBean.STOPPED); 320 expect(componentDescription.getSharedLibraryList()).andReturn( 321 slList); 322 expect(componentDescription.getIdentification()).andReturn( 323 identification).anyTimes(); 324 expect(identification.getName()).andReturn(compName); 325 expect(sharedLibrayList.getName()).andReturn(slName); 326 } catch (Exception e) { 327 e.printStackTrace(); 328 fail("Could not initialize Mocks"); 329 } 330 replay(sharedLibrayList); 331 replay(componentDescription); 332 replay(componentLifeCycle); 333 replay(lcms); 334 installationService.jmxAdmin = lcms; 335 boolean result = false; 336 339 try { 340 result = installationService 341 .checkComponentShutdownStateForSL(slName); 342 } catch (Exception e) { 343 e.printStackTrace(); 344 fail("Error during invokation"); 345 } 346 verify(sharedLibrayList); 347 verify(componentDescription); 348 verify(componentLifeCycle); 349 verify(lcms); 350 assertFalse("Wrong invokation result", result); 351 352 356 reset(sharedLibrayList); 357 reset(componentDescription); 358 reset(componentLifeCycle); 359 reset(lcms); 360 reset(identification); 361 364 367 try { 368 expect(lcms.getBindingCompoLifeCycles()).andReturn( 369 bindingComponentLifecycles); 370 expect(lcms.getEngineCompoLifeCycles()).andReturn(engineLifecycles); 371 expect(componentLifeCycle.getComponentDescription()).andReturn( 372 componentDescription).anyTimes(); 373 expect(componentLifeCycle.getCurrentState()).andReturn( 374 LifeCycleMBean.SHUTDOWN); 375 expect(componentLifeCycle.getCurrentState()).andReturn( 376 LifeCycleMBean.STOPPED); 377 expect(componentDescription.getSharedLibraryList()).andReturn( 378 slList).anyTimes(); 379 expect(componentDescription.getIdentification()).andReturn( 380 identification).anyTimes(); 381 expect(identification.getName()).andReturn(compName); 382 expect(sharedLibrayList.getName()).andReturn(slName).anyTimes(); 383 } catch (Exception e) { 384 e.printStackTrace(); 385 fail("Could not initialize Mocks"); 386 } 387 replay(sharedLibrayList); 388 replay(componentDescription); 389 replay(componentLifeCycle); 390 replay(lcms); 391 replay(identification); 392 installationService.jmxAdmin = lcms; 393 result = false; 394 397 try { 398 result = installationService 399 .checkComponentShutdownStateForSL(slName); 400 } catch (Exception e) { 401 e.printStackTrace(); 402 fail("Error during invokation"); 403 } 404 verify(sharedLibrayList); 405 verify(componentDescription); 406 verify(componentLifeCycle); 407 verify(lcms); 408 assertFalse("Wrong invokation result", result); 409 } 410 411 public void testGetComponentNameFromJBIDescriptor() { 412 415 InstallationServiceImpl installationService = new InstallationServiceImpl(); 416 419 String componentName = "componentName"; 420 JBIDescriptor descriptor = createMock(JBIDescriptor.class); 421 ComponentDescription component = createMock(ComponentDescription.class); 422 Identification id = createMock(Identification.class); 423 426 expect(descriptor.getComponent()).andReturn(component); 427 expect(component.getIdentification()).andReturn(id); 428 expect(id.getName()).andReturn(componentName); 429 replay(descriptor); 430 replay(id); 431 replay(component); 432 435 assertEquals("Wrong invokation result", componentName, 436 installationService 437 .getComponentNameFromJBIDescriptor(descriptor)); 438 verify(descriptor); 439 verify(id); 440 verify(component); 441 } 442 443 public void testGetSharedLibraryName() { 444 447 InstallationServiceImpl installationService = new InstallationServiceImpl(); 448 451 String sharedLibraryName = "sharedLibraryName"; 452 JBIDescriptor descriptor = createMock(JBIDescriptor.class); 453 SharedLibrary sharedLibrary = createMock(SharedLibrary.class); 454 Identification id = createMock(Identification.class); 455 458 expect(descriptor.getSharedLibrary()).andReturn(sharedLibrary); 459 expect(sharedLibrary.getIdentification()).andReturn(id); 460 expect(id.getName()).andReturn(sharedLibraryName); 461 replay(descriptor); 462 replay(id); 463 replay(sharedLibrary); 464 467 assertEquals("Wrong invokation result", sharedLibraryName, 468 installationService.getSharedLibraryName(descriptor)); 469 verify(descriptor); 470 verify(id); 471 verify(sharedLibrary); 472 } 473 474 public void testStart() { 475 478 InstallationServiceImpl installationService = new InstallationServiceImpl(); 479 482 485 installationService.start(); 486 assertNotNull("Initialization incorectly done", 487 installationService.slLoadedCache); 488 assertNotNull("Initialization incorectly done", 489 installationService.componentLoadedCache); 490 assertNotNull("Initialization incorectly done", installationService.log); 491 assertNotNull("Initialization incorectly done", 492 installationService.packageHandler); 493 } 494 495 830 public void testInstallationService() { 831 834 InstallationServiceImpl installationService = new InstallationServiceImpl(); 835 assertNotNull(installationService); 836 } 837 838 public void testLoadInstaller() { 839 843 InstallationServiceImpl installationService = new InstallationServiceImpl(); 844 String componentName = null; 845 LoggingUtil log = createMock(LoggingUtil.class); 846 installationService.log = log; 847 850 try { 851 installationService.loadInstaller(componentName); 852 fail("No exception was thrown"); 853 } catch (Exception e) { 854 } 856 857 861 componentName = ""; 862 865 try { 866 installationService.loadInstaller(componentName); 867 fail("No exception was thrown"); 868 } catch (Exception e) { 869 } 871 872 875 componentName = "componentName"; 876 LifeCycleManagerService lcms = createMock(LifeCycleManagerImpl.class); 877 installationService.jmxAdmin = lcms; 878 881 ObjectName result = installationService.loadInstaller(componentName); 882 assertNull("Wrong invokation result", result); 883 } 884 885 public void testLoadNewInstaller() { 886 890 893 Method method1 = null; 894 Method method2 = null; 895 Method method3 = null; 896 Method method4 = null; 897 Method method5 = null; 898 Method method6 = null; 899 Method method7 = null; 900 Method method8 = null; 901 Method method9 = null; 902 try { 903 method1 = InstallationServiceImpl.class.getDeclaredMethod( 904 "checkComponent", new Class [] {JBIDescriptor.class, 905 URI .class}); 906 method2 = InstallationServiceImpl.class.getDeclaredMethod( 907 "checkInstalledSLForComp", 908 new Class [] {JBIDescriptor.class}); 909 method3 = InstallationServiceImpl.class.getDeclaredMethod( 910 "getComponentNameFromJBIDescriptor", 911 new Class [] {JBIDescriptor.class}); 912 method4 = InstallationServiceImpl.class.getDeclaredMethod( 913 "loadNewInstaller", new Class [] {URI .class, 914 JBIDescriptor.class}); 915 method5 = InstallationServiceImpl.class.getDeclaredMethod( 916 "createFileFromURI", new Class [] {URI .class}); 917 method6 = InstallationServiceImpl.class.getDeclaredMethod( 918 "getInstalledArchive", new Class [] {File .class}); 919 method7 = InstallationServiceImpl.class.getDeclaredMethod( 920 "copyFile", new Class [] {File .class, File .class}); 921 method8 = InstallationServiceImpl.class.getDeclaredMethod( 922 "createSuccessFile", new Class [] {File .class}); 923 method9 = InstallationServiceImpl.class.getDeclaredMethod( 924 "explodeComponent", new Class [] {String .class, URI .class}); 925 } catch (Exception e) { 926 e.printStackTrace(); 927 fail("Could not retrieve mockedMethods"); 928 } 929 Method [] mockedMethods = new Method [] {method1, method2, method3, 930 method4, method5, method6, method7, method8, method9}; 931 InstallationServiceImpl installationService = createMock( 932 InstallationServiceImpl.class, mockedMethods); 933 String installZipUrl = null; 934 LoggingUtil log = createMock(LoggingUtil.class); 935 installationService.log = log; 936 939 try { 940 installationService.loadNewInstaller(installZipUrl); 941 fail("No exception was thrown"); 942 } catch (Exception e) { 943 } 945 946 950 installZipUrl = ""; 951 954 try { 955 installationService.loadNewInstaller(installZipUrl); 956 fail("No exception was thrown"); 957 } catch (Exception e) { 958 } 960 961 965 installZipUrl = "illegalURL"; 966 969 try { 970 installationService.loadNewInstaller(installZipUrl); 971 fail("No exception was thrown"); 972 } catch (Exception e) { 973 } 975 976 979 installZipUrl = "file://zip/URL"; 980 PackageHandler packageHandler = createMock(PackageHandler.class); 981 JBIDescriptor descriptor = createMock(JBIDescriptor.class); 982 URI archiveURI = null; 983 try { 984 archiveURI = new URI ("file://archive/uri"); 985 } catch (Exception e) { 986 e.printStackTrace(); 987 fail("Could not create test parameters"); 988 } 989 990 reset(installationService); 991 reset(packageHandler); 992 995 String componentName = "componentName"; 996 URI installRootURI = null; 997 try { 998 installRootURI = new URI ("file://install/root"); 999 } catch (Exception e) { 1000 e.printStackTrace(); 1001 fail("Could not create test parameters"); 1002 } 1003 File file = new File (""); 1004 File installedArchive = new File (testDataDir, "installedArchive"); 1005 SystemState recoverySrv = createMock(SystemStateImpl.class); 1006 ObjectName invokationResult = createMock(ObjectName .class); 1007 1010 try { 1011 expect(packageHandler.processAndGetPackageURI(installZipUrl, true)) 1012 .andReturn(archiveURI); 1013 expect(packageHandler.loadDescriptor(archiveURI)).andReturn( 1014 descriptor); 1015 installationService.checkComponent(descriptor, archiveURI); 1016 expect( 1017 installationService.loadNewInstaller(installRootURI, 1018 descriptor)).andReturn(invokationResult); 1019 expect( 1020 installationService 1021 .getComponentNameFromJBIDescriptor(descriptor)) 1022 .andReturn(componentName); 1023 expect( 1024 installationService.explodeComponent(componentName, 1025 archiveURI)).andReturn(installRootURI); 1026 expect(installationService.createFileFromURI(archiveURI)) 1027 .andReturn(file); 1028 expect(installationService.getInstalledArchive(file)).andReturn( 1029 installedArchive); 1030 installationService.createSuccessFile(installedArchive); 1031 installationService.copyFile(file, installedArchive); 1032 } catch (Exception e) { 1033 e.printStackTrace(); 1034 fail("Could not initialize Mocks"); 1035 } 1036 replay(installationService); 1037 replay(packageHandler); 1038 installationService.packageHandler = packageHandler; 1039 installationService.recoverySrv = recoverySrv; 1040 1043 ObjectName result = null; 1044 try { 1045 result = installationService.loadNewInstaller(installZipUrl); 1046 } catch (Exception e) { 1047 fail("Error during invokation"); 1048 } 1049 assertNotNull("Result of loadNewInstaller must not be null", result); 1050 assertEquals("Wrong invokation result ", result, invokationResult); 1051 verify(installationService); 1052 verify(packageHandler); 1053 } 1054 1055 public void testCheckInstalledSLForComp() { 1056 1059 InstallationServiceImpl installationService = new InstallationServiceImpl(); 1060 1063 JBIDescriptor descriptor = createMock(JBIDescriptor.class); 1064 ComponentDescription componentDescription = createMock(ComponentDescription.class); 1065 SharedLibraryList sharedLibrayList = createMock(SharedLibraryList.class); 1066 LoggingUtil log = createMock(LoggingUtil.class); 1067 List <SharedLibraryList> slList = new ArrayList <SharedLibraryList>(); 1068 slList.add(sharedLibrayList); 1069 installationService.log = log; 1070 1073 expect(descriptor.getComponent()).andReturn(componentDescription); 1074 expect(componentDescription.getSharedLibraryList()).andReturn(slList); 1075 expect(sharedLibrayList.getName()).andReturn("a SL name").anyTimes(); 1076 replay(descriptor); 1077 replay(sharedLibrayList); 1078 replay(componentDescription); 1079 installationService.slLoadedCache = new ArrayList <String >(); 1080 1083 try { 1084 installationService.checkInstalledSLForComp(descriptor); 1085 fail("Must throw an exception if SL is missing"); 1086 } catch (PetalsException e) { 1087 } 1089 verify(descriptor); 1090 verify(componentDescription); 1091 } 1092 1093 1225 public void testCreateAndRegisterInstallerMBean() { 1226 1229 InstallationServiceImpl installationService = new InstallationServiceImpl(); 1230 1233 LoggingUtil log = createMock(LoggingUtil.class); 1234 installationService.log = log; 1235 ComponentContextImpl componentCtx = createMock(ComponentContextImpl.class); 1236 JBIDescriptor descriptor = createMock(JBIDescriptor.class); 1238 URI installationRoot = new File (testDataDir, "fakearchive" 1239 + File.separator + "archive").toURI(); 1240 ComponentDescription componentDescription = createMock(ComponentDescription.class); 1241 ObjectName result = null; 1242 LifeCycleManagerService lcms = createMock(LifeCycleManagerImpl.class); 1243 MBeanNamesImpl beanNames = createMock(MBeanNamesImpl.class); 1244 Identification id = createMock(Identification.class); 1245 String componentName = "componentName"; 1246 ObjectName objectName = createMock(ObjectName .class); 1247 1250 try { 1251 expect(descriptor.getComponent()).andReturn(componentDescription); 1252 expect(lcms.getMBeanNames()).andReturn(beanNames); 1253 expect(lcms.registerInstaller(isA(Installer.class))) 1254 .andReturn(null); 1255 expect(componentDescription.getIdentification()).andReturn(id); 1256 expect(id.getName()).andReturn(componentName); 1257 expect(beanNames.createInstallerMBeanName(componentName)) 1258 .andReturn(objectName); 1259 } catch (Exception e) { 1260 e.printStackTrace(); 1261 fail("Could not initialize Mocks"); 1262 } 1263 replay(lcms); 1264 replay(descriptor); 1265 replay(id); 1266 replay(beanNames); 1267 replay(componentDescription); 1268 installationService.jmxAdmin = lcms; 1269 1272 try { 1273 result = installationService 1274 .createAndRegisterInstallerMBean(componentCtx, 1275 descriptor, installationRoot); 1276 } catch (PetalsException e) { 1277 e.printStackTrace(); 1278 } 1279 verify(lcms); 1280 verify(descriptor); 1281 verify(id); 1282 verify(beanNames); 1283 verify(componentDescription); 1284 assertEquals("Wrong invokation result", result, objectName); 1285 } 1286 1287 public void testCreateFileFromURI() { 1288 1291 InstallationServiceImpl installationService = new InstallationServiceImpl(); 1292 1295 URI fileURI = new File (testDataDir, "fakearchive" + File.separator 1296 + "archive").toURI(); 1297 File result = null; 1298 1301 result = installationService.createFileFromURI(fileURI); 1302 assertEquals("Wrong invokation result", result.toURI(), fileURI); 1303 } 1304 1305 public void testLoadNewInstaller2() { 1306 1310 1313 Method createComponentContextMethod = null; 1314 Method createAndRegisterInstallerMBeanMethod = null; 1316 Method getComponentNameFromJBIDescriptormethod = null; 1317 Method checkInstalledSLForCompMethod = null; 1318 try { 1319 createComponentContextMethod = InstallationServiceImpl.class.getDeclaredMethod( 1320 "createComponentContext", new Class [] {JBIDescriptor.class, 1321 URI .class}); 1322 createAndRegisterInstallerMBeanMethod = InstallationServiceImpl.class 1326 .getDeclaredMethod("createAndRegisterInstallerMBean", 1327 new Class [] {ComponentContextImpl.class, 1328 JBIDescriptor.class, URI .class}); 1329 getComponentNameFromJBIDescriptormethod = InstallationServiceImpl.class.getDeclaredMethod( 1330 "getComponentNameFromJBIDescriptor", 1331 new Class [] {JBIDescriptor.class}); 1332 checkInstalledSLForCompMethod = InstallationServiceImpl.class.getDeclaredMethod( 1333 "checkInstalledSLForComp", 1334 new Class [] {JBIDescriptor.class}); 1335 } catch (Exception e) { 1336 e.printStackTrace(); 1337 fail("Could not retrieve mockedMethods"); 1338 } 1339 Method [] mockedMethods = new Method [] {createComponentContextMethod, createAndRegisterInstallerMBeanMethod, 1342 getComponentNameFromJBIDescriptormethod, checkInstalledSLForCompMethod}; 1343 1344 InstallationServiceImpl installationService = createMock( 1345 InstallationServiceImpl.class, mockedMethods); 1346 LoggingUtil log = createMock(LoggingUtil.class); 1347 installationService.log = log; 1348 1351 URI installationRoot = null; 1352 JBIDescriptor descriptor = null; 1353 1356 try { 1357 installationService.loadNewInstaller(installationRoot, descriptor); 1358 fail("No exception was raised"); 1359 } catch (Exception e) { 1360 } 1362 1363 1366 1369 try { 1370 installationRoot = new URI ("file://installation/root"); 1371 } catch (Exception e) { 1372 e.printStackTrace(); 1373 fail("Could not create test parameters"); 1374 } 1375 1378 try { 1379 installationService.loadNewInstaller(installationRoot, descriptor); 1380 fail("No exception was raised"); 1381 } catch (Exception e) { 1382 } 1384 1385 1389 1392 descriptor = createMock(JBIDescriptor.class); 1393 ComponentContextImpl componentCtx = createMock(ComponentContextImpl.class); 1394 ObjectName objectName = createMock(ObjectName .class); 1396 String componentName = "componentName"; 1397 1400 try { 1401 expect( 1402 installationService.createComponentContext(descriptor, 1403 installationRoot)).andReturn(componentCtx); 1404 } catch (PetalsException e2) { 1405 e2.printStackTrace(); 1406 } 1407 try { 1411 expect( 1412 installationService.createAndRegisterInstallerMBean( 1413 componentCtx, descriptor, 1414 installationRoot)).andReturn(objectName); 1415 } catch (PetalsException e1) { 1416 e1.printStackTrace(); 1417 } 1418 try { 1419 installationService.checkInstalledSLForComp(descriptor); 1420 } catch (PetalsException e1) { 1421 e1.printStackTrace(); 1422 } 1423 expect( 1424 installationService 1425 .getComponentNameFromJBIDescriptor(descriptor)) 1426 .andReturn(componentName); 1427 replay(installationService); 1428 installationService.componentLoadedCache = new ArrayList <String >(); 1429 ObjectName result = null; 1430 1433 try { 1434 result = installationService.loadNewInstaller(installationRoot, 1435 descriptor); 1436 } catch (Exception e) { 1437 e.printStackTrace(); 1438 fail("Error during invokation"); 1439 } 1440 verify(installationService); 1441 assertNotNull("Result of loadNewInstaller", result); 1442 assertEquals("Wrong invokation result", objectName, result); 1443 } 1444 1445 public void testStop() { 1446 1449 InstallationServiceImpl installationService = new InstallationServiceImpl(); 1450 1453 installationService.start(); 1454 1457 installationService.stop(); 1458 assertNull("Shutdown process incomplete", installationService.logger); 1459 assertNull("Shutdown process incomplete", 1460 installationService.slLoadedCache); 1461 assertNull("Shutdown process incomplete", 1462 installationService.componentLoadedCache); 1463 } 1464 1465 public void testUninstallSharedLibrary() { 1466 1469 1472 Method method1 = null; 1473 Method method2 = null; 1474 Method method3 = null; 1475 Method method4 = null; 1476 try { 1477 method1 = InstallationServiceImpl.class.getDeclaredMethod( 1478 "checkComponentShutdownStateForSL", 1479 new Class [] {String .class}); 1480 method2 = InstallationServiceImpl.class.getDeclaredMethod( 1481 "createFileFromURI", new Class [] {URI .class}); 1482 method3 = InstallationServiceImpl.class.getDeclaredMethod( 1483 "getUninstalledArchive", new Class [] {File .class}); 1484 1489 } catch (Exception e) { 1490 e.printStackTrace(); 1491 fail("Could not retrieve mockedMethods"); 1492 } 1493 Method [] mockedMethods = new Method [] {method1, method2, method3, 1494 method4}; 1495 InstallationServiceImpl installationService = createMock( 1496 InstallationServiceImpl.class, mockedMethods); 1497 LoggingUtil log = createMock(LoggingUtil.class); 1498 installationService.log = log; 1499 LoaderManager loaderSrv = createMock(LoaderManagerImpl.class); 1500 installationService.loaderSrv = loaderSrv; 1501 installationService.slLoadedCache = new ArrayList <String >(); 1502 SystemState recoverySrv = createMock(SystemStateImpl.class); 1503 installationService.recoverySrv = recoverySrv; 1504 RepositoryService repositorySrv = createMock(RepositoryImpl.class); 1505 1508 String slName = null; 1509 1512 try { 1513 installationService.uninstallSharedLibrary(slName); 1514 fail("No exception was raised"); 1515 } catch (Exception e) { 1516 } 1518 1519 1522 1525 slName = ""; 1526 1529 try { 1530 installationService.uninstallSharedLibrary(slName); 1531 fail("No exception was raised"); 1532 } catch (Exception e) { 1533 } 1535 1536 1539 1542 slName = "sharedLibraryName"; 1543 PackageHandler packageHandler = createMock(PackageHandler.class); 1544 SharedLibraryState slState = createMock(SharedLibraryState.class); 1545 String archiveZipURL = "file://achive.zip.url"; 1546 URI archiveURI = null; 1547 try { 1548 archiveURI = new URI (archiveZipURL); 1549 } catch (Exception e) { 1550 e.printStackTrace(); 1551 fail("Could not create test parameters"); 1552 } 1553 File installedArchive = new File (testDataDir, "fakearchive" 1554 + File.separator + "archive"); 1555 File uninstalledArchive = installedArchive; 1556 1559 try { 1560 expect(installationService.checkComponentShutdownStateForSL(slName)) 1561 .andReturn(true); 1562 expect(installationService.createFileFromURI(archiveURI)) 1563 .andReturn(installedArchive); 1564 expect(installationService.getUninstalledArchive(installedArchive)) 1565 .andReturn(uninstalledArchive); 1566 expect(repositorySrv.removeSharedLibPackage(slName)) 1567 .andReturn(true); 1568 expect(recoverySrv.deleteSharedLibraryStateHolder(slName)) 1569 .andReturn(slState); 1570 expect(slState.getArchiveURL()).andReturn(archiveZipURL); 1571 expect(packageHandler.processAndGetPackageURI(archiveZipURL, false)) 1572 .andReturn(archiveURI); 1573 } catch (Exception e) { 1574 e.printStackTrace(); 1575 fail("Could not initialize Mocks"); 1576 } 1577 replay(installationService); 1578 replay(repositorySrv); 1579 replay(slState); 1580 replay(packageHandler); 1581 replay(recoverySrv); 1582 installationService.repositorySrv = repositorySrv; 1583 installationService.packageHandler = packageHandler; 1584 installationService.recoverySrv = recoverySrv; 1585 boolean result = false; 1586 1589 try { 1590 result = installationService.uninstallSharedLibrary(slName); 1591 } catch (Exception e) { 1592 fail("Error during invokation"); 1593 } 1594 verify(installationService); 1595 verify(repositorySrv); 1596 verify(slState); 1597 verify(packageHandler); 1598 verify(recoverySrv); 1599 assertTrue("Wrong invokation result", result); 1600 1601 1604 1607 reset(installationService); 1608 reset(repositorySrv); 1609 reset(slState); 1610 reset(packageHandler); 1611 reset(recoverySrv); 1612 1615 try { 1616 expect(installationService.checkComponentShutdownStateForSL(slName)) 1617 .andReturn(true); 1618 expect(recoverySrv.deleteSharedLibraryStateHolder(slName)) 1620 .andThrow( 1621 new Exception ( 1622 "deleteSharedLibraryStateHolder testing exception")); 1623 } catch (Exception e) { 1628 e.printStackTrace(); 1629 fail("Could not initialize Mocks"); 1630 } 1631 replay(installationService); 1632 replay(recoverySrv); 1636 installationService.repositorySrv = repositorySrv; 1637 installationService.packageHandler = packageHandler; 1638 installationService.recoverySrv = recoverySrv; 1639 result = false; 1640 1643 try { 1644 result = installationService.uninstallSharedLibrary(slName); 1645 } catch (Exception e) { 1646 fail("Error during invokation"); 1647 } 1648 verify(installationService); 1649 verify(recoverySrv); 1650 assertFalse("Wrong invokation result", result); 1654 } 1655 1656 public void testUnloadInstaller() { 1657 1661 1664 Method method1 = null; 1665 Method method2 = null; 1666 Method method3 = null; 1667 Method method4 = null; 1668 Method method5 = null; 1669 try { 1670 method1 = MockLifeCycleManagerService.class.getDeclaredMethod( 1671 "getInstallerByName", new Class [] {String .class}); 1672 method2 = MockLifeCycleManagerService.class.getDeclaredMethod( 1673 "getMBeanServer", new Class [] {}); 1674 method3 = MockLifeCycleManagerService.class.getDeclaredMethod( 1675 "unregisterInstaller", new Class [] {ObjectName .class}); 1676 method4 = InstallationServiceImpl.class.getDeclaredMethod( 1677 "createFileFromURI", new Class [] {URI .class}); 1678 method5 = InstallationServiceImpl.class.getDeclaredMethod( 1679 "getUninstalledArchive", new Class [] {File .class}); 1680 } catch (Exception e) { 1681 e.printStackTrace(); 1682 fail("Could not retrieve mockedMethods"); 1683 } 1684 Method [] mockedMethods = new Method [] {method4, method5}; 1685 InstallationServiceImpl installationService = createMock( 1686 InstallationServiceImpl.class, mockedMethods); 1687 mockedMethods = new Method [] {method1, method2}; 1688 LoggingUtil log = createMock(LoggingUtil.class); 1689 installationService.log = log; 1690 1698 1701 String componentName = null; 1702 1705 try { 1706 installationService.unloadInstaller(componentName, true); 1707 fail("No exception was raised"); 1708 } catch (Exception e) { 1709 } 1711 1712 1716 1719 componentName = ""; 1720 1723 try { 1724 installationService.unloadInstaller(componentName, true); 1725 fail("No exception was raised"); 1726 } catch (Exception e) { 1727 } 1729 1730 1733 1736 componentName = "ComponentName"; 1737 LifeCycleManagerService managerSrv = createMock( 1738 MockLifeCycleManagerService.class, mockedMethods); 1739 ObjectName objectName = createMock(ObjectName .class); 1740 MBeanServer beanServer = createMock(MBeanServer .class); 1741 1744 try { 1745 expect(managerSrv.getInstallerByName(componentName)).andReturn( 1746 objectName); 1747 expect(managerSrv.getMBeanServer()).andReturn(beanServer); 1748 expect(beanServer.getAttribute(objectName, "Installed")).andThrow( 1749 new InstanceNotFoundException ( 1750 "getAttribute testing exception")); 1751 } catch (Exception e) { 1752 e.printStackTrace(); 1753 fail("Could not initialize Mocks"); 1754 } 1755 replay(managerSrv); 1756 replay(beanServer); 1757 installationService.jmxAdmin = managerSrv; 1758 boolean result = false; 1759 1762 try { 1763 result = installationService.unloadInstaller(componentName, true); 1764 } catch (Exception e) { 1765 fail("Error during invokation"); 1766 } 1767 verify(managerSrv); 1768 verify(beanServer); 1769 assertFalse("Wrong invokation result", result); 1770 1771 1774 reset(managerSrv); 1775 reset(beanServer); 1776 1779 1782 try { 1783 expect(managerSrv.getInstallerByName(componentName)).andReturn( 1784 objectName); 1785 expect(managerSrv.getMBeanServer()).andReturn(beanServer); 1786 expect(beanServer.getAttribute(objectName, "Installed")).andThrow( 1787 new MBeanException (new Exception ( 1788 "getAttribute testing exception"))); 1789 } catch (Exception e) { 1790 e.printStackTrace(); 1791 fail("Could not initialize Mocks"); 1792 } 1793 replay(managerSrv); 1794 replay(beanServer); 1795 installationService.jmxAdmin = managerSrv; 1796 result = false; 1797 1800 try { 1801 result = installationService.unloadInstaller(componentName, true); 1802 } catch (Exception e) { 1803 fail("Error during invokation"); 1804 } 1805 verify(managerSrv); 1806 verify(beanServer); 1807 assertFalse("Wrong invokation result", result); 1808 1809 1812 reset(managerSrv); 1813 reset(beanServer); 1814 1817 1820 try { 1821 expect(managerSrv.getInstallerByName(componentName)).andReturn( 1822 objectName); 1823 expect(managerSrv.getMBeanServer()).andReturn(beanServer); 1824 expect(beanServer.getAttribute(objectName, "Installed")).andThrow( 1825 new ReflectionException (new Exception ( 1826 "getAttribute testing exception"))); 1827 } catch (Exception e) { 1828 e.printStackTrace(); 1829 fail("Could not initialize Mocks"); 1830 } 1831 replay(managerSrv); 1832 replay(beanServer); 1833 installationService.jmxAdmin = managerSrv; 1834 result = false; 1835 1838 try { 1839 result = installationService.unloadInstaller(componentName, true); 1840 } catch (Exception e) { 1841 fail("Error during invokation"); 1842 } 1843 verify(managerSrv); 1844 verify(beanServer); 1845 assertFalse("Wrong invokation result", result); 1846 1847 1850 reset(managerSrv); 1851 reset(beanServer); 1852 1855 1858 try { 1859 expect(managerSrv.getInstallerByName(componentName)).andReturn( 1860 objectName); 1861 expect(managerSrv.getMBeanServer()).andReturn(beanServer); 1862 expect(beanServer.getAttribute(objectName, "Installed")).andThrow( 1863 new AttributeNotFoundException ( 1864 "getAttribute testing exception")); 1865 } catch (Exception e) { 1866 e.printStackTrace(); 1867 fail("Could not initialize Mocks"); 1868 } 1869 replay(managerSrv); 1870 replay(beanServer); 1871 installationService.jmxAdmin = managerSrv; 1872 result = false; 1873 1876 try { 1877 result = installationService.unloadInstaller(componentName, true); 1878 } catch (Exception e) { 1879 fail("Error during invokation"); 1880 } 1881 verify(managerSrv); 1882 verify(beanServer); 1883 assertFalse("Wrong invokation result", result); 1884 1885 1889 reset(managerSrv); 1890 reset(beanServer); 1891 1894 Boolean getAttribute = new Boolean (true); 1895 1898 try { 1899 expect(managerSrv.getInstallerByName(componentName)).andReturn( 1900 objectName); 1901 expect(managerSrv.getMBeanServer()).andReturn(beanServer); 1902 expect(beanServer.getAttribute(objectName, "Installed")).andReturn( 1903 getAttribute); 1904 expect( 1905 beanServer.invoke(isA(ObjectName .class), isA(String .class), 1906 isA(Object [].class), isA(String [].class))) 1907 .andReturn(null); 1908 } catch (Exception e) { 1909 e.printStackTrace(); 1910 fail("Could not initialize Mocks"); 1911 } 1912 replay(managerSrv); 1913 replay(beanServer); 1914 installationService.jmxAdmin = managerSrv; 1915 result = false; 1916 1919 try { 1920 result = installationService.unloadInstaller(componentName, true); 1921 } catch (Exception e) { 1922 fail("Error during invokation"); 1923 } 1924 verify(managerSrv); 1925 verify(beanServer); 1926 assertFalse("Wrong invokation result", result); 1927 1928 1932 reset(managerSrv); 1933 reset(beanServer); 1934 1937 mockedMethods = new Method [] {method1, method2, method3}; 1938 managerSrv = createMock(MockLifeCycleManagerService.class, 1939 mockedMethods); 1940 SystemState sysState = createMock(SystemStateImpl.class); 1941 ComponentState componentState = createMock(ComponentState.class); 1942 RepositoryService repositorySrv = createMock(RepositoryImpl.class); 1943 1946 try { 1947 expect(managerSrv.getInstallerByName(componentName)).andReturn( 1948 objectName); 1949 expect(managerSrv.getMBeanServer()).andReturn(beanServer); 1950 managerSrv.unregisterInstaller(objectName); 1951 expect(beanServer.getAttribute(objectName, "Installed")).andReturn( 1952 getAttribute); 1953 expect( 1954 beanServer.invoke(isA(ObjectName .class), isA(String .class), 1955 isA(Object [].class), isA(String [].class))) 1956 .andReturn(null); 1957 expect(sysState.deleteComponentStateHolder(componentName)) 1958 .andThrow( 1959 new Exception ( 1960 "deleteComponentStateHolder testing exception")); 1961 } catch (Exception e) { 1962 e.printStackTrace(); 1963 fail("Could not initialize Mocks"); 1964 } 1965 replay(managerSrv); 1966 replay(beanServer); 1967 replay(sysState); 1968 replay(repositorySrv); 1969 installationService.jmxAdmin = managerSrv; 1970 installationService.componentLoadedCache = new ArrayList <String >(); 1971 installationService.recoverySrv = sysState; 1972 installationService.repositorySrv = repositorySrv; 1973 result = false; 1974 1977 try { 1978 result = installationService.unloadInstaller(componentName, true); 1979 } catch (Exception e) { 1980 fail("Error during invokation"); 1981 } 1982 verify(managerSrv); 1983 verify(beanServer); 1984 verify(sysState); 1985 verify(repositorySrv); 1986 assertFalse("Wrong invokation result", result); 1987 1988 1992 reset(managerSrv); 1993 reset(beanServer); 1994 reset(repositorySrv); 1995 reset(sysState); 1996 1999 String archiveUrl = "archive/url"; 2000 URI archiveURI = null; 2001 try { 2002 archiveURI = new URI ("file://arvhice.uri"); 2003 } catch (Exception e) { 2004 e.printStackTrace(); 2005 fail("Could not create Test parameters"); 2006 } 2007 File installedArchive = new File (testDataDir, "fakearchive" 2008 + File.separator + "archive"); 2009 File uninstalledArchive = installedArchive; 2010 PackageHandler packageHandler = createMock(PackageHandler.class); 2011 2014 try { 2015 expect(managerSrv.getInstallerByName(componentName)).andReturn( 2016 objectName); 2017 expect(managerSrv.getMBeanServer()).andReturn(beanServer); 2018 managerSrv.unregisterInstaller(objectName); 2019 expect(beanServer.getAttribute(objectName, "Installed")).andReturn( 2020 getAttribute); 2021 expect( 2022 beanServer.invoke(isA(ObjectName .class), isA(String .class), 2023 isA(Object [].class), isA(String [].class))) 2024 .andReturn(null); 2025 expect(sysState.deleteComponentStateHolder(componentName)) 2026 .andReturn(componentState); 2027 expect(repositorySrv.removeComponentPackage(componentName)) 2028 .andReturn(true); 2029 expect(componentState.getArchiveURL()).andReturn(archiveUrl); 2030 expect(installationService.createFileFromURI(archiveURI)) 2031 .andReturn(installedArchive); 2032 expect(installationService.getUninstalledArchive(installedArchive)) 2033 .andReturn(uninstalledArchive); 2034 expect(packageHandler.processAndGetPackageURI(archiveUrl, false)) 2035 .andReturn(archiveURI); 2036 } catch (Exception e) { 2037 e.printStackTrace(); 2038 fail("Could not initialize Mocks"); 2039 } 2040 replay(managerSrv); 2041 replay(beanServer); 2042 replay(sysState); 2043 replay(repositorySrv); 2044 replay(componentState); 2045 replay(installationService); 2046 replay(packageHandler); 2047 installationService.jmxAdmin = managerSrv; 2048 installationService.componentLoadedCache = new ArrayList <String >(); 2049 installationService.recoverySrv = sysState; 2050 installationService.repositorySrv = repositorySrv; 2051 installationService.packageHandler = packageHandler; 2052 result = false; 2053 2056 try { 2057 result = installationService.unloadInstaller(componentName, true); 2058 } catch (Exception e) { 2059 e.printStackTrace(); 2060 fail("Error during invokation"); 2061 } 2062 verify(managerSrv); 2063 verify(beanServer); 2064 verify(sysState); 2065 verify(componentState); 2066 verify(repositorySrv); 2067 verify(installationService); 2068 verify(packageHandler); 2069 assertTrue("Wrong invokation result", result); 2070 2071 } 2072 2073 @Override 2074 protected void setUp() { 2075 String baseDir = this.getClass().getResource(".").toString(); 2076 baseDir = baseDir.substring(0, baseDir.indexOf("target")); 2077 baseDir = baseDir.substring(baseDir.indexOf(":") + 1); 2078 2079 testDataDir = new File (baseDir + "src" + File.separator + "test-data"); 2080 } 2081} 2082 | Popular Tags |