1 22 package org.objectweb.petals.jbi.component.lifecycle; 23 24 import java.io.File ; 25 import java.lang.reflect.Method ; 26 import java.net.URI ; 27 28 import javax.jbi.JBIException; 29 import javax.jbi.component.Bootstrap; 30 import javax.jbi.management.LifeCycleMBean; 31 import javax.management.MBeanNotificationInfo ; 32 import javax.management.Notification ; 33 import javax.management.NotificationBroadcasterSupport ; 34 import javax.management.NotificationListener ; 35 import javax.management.ObjectName ; 36 37 import junit.framework.TestCase; 38 39 import org.objectweb.petals.classloader.LoaderManager; 40 import org.objectweb.petals.classloader.PetalsClassLoader; 41 import org.objectweb.petals.jbi.component.context.ComponentContextImpl; 42 import org.objectweb.petals.jbi.component.event.StateChangeFailedEvent; 43 import org.objectweb.petals.jbi.component.event.StateChangedEvent; 44 import org.objectweb.petals.jbi.component.thread.BootstrapThread; 45 import org.objectweb.petals.jbi.management.service.LifeCycleManagerService; 46 import org.objectweb.petals.jbi.management.systemstate.SystemState; 47 import org.objectweb.petals.jbi.mock.MockInstaller; 48 import org.objectweb.petals.jbi.mock.MockSystemState; 49 import org.objectweb.petals.tools.jbicommon.descriptor.ComponentDescription; 50 import org.objectweb.petals.util.LoggingUtil; 51 import org.objectweb.util.monolog.api.Logger; 52 53 import static org.easymock.EasyMock.expect; 54 import static org.easymock.EasyMock.isA; 55 import static org.easymock.classextension.EasyMock.createMock; 56 import static org.easymock.classextension.EasyMock.replay; 57 import static org.easymock.classextension.EasyMock.reset; 58 import static org.easymock.classextension.EasyMock.verify; 59 60 65 public class InstallerTest extends TestCase { 66 67 private File testDataDir; 68 69 public void testAddNotificationListener() { 70 73 Installer installer = createMock(Installer.class,new Method []{}); 74 NotificationBroadcasterSupport notifSupport = createMock(NotificationBroadcasterSupport .class); 75 78 notifSupport.addNotificationListener(null, null, null); 79 replay(notifSupport); 80 installer.notifSupport = notifSupport; 81 84 installer.addNotificationListener(null, null, null); 85 verify(notifSupport); 86 } 87 88 public void testGetComponentName() { 89 92 Installer installer = createMock(Installer.class,new Method []{}); 93 ComponentContextImpl componentCtx = createMock(ComponentContextImpl.class); 94 expect(componentCtx.getComponentName()).andReturn(null); 95 replay(componentCtx); 96 installer.componentContext = componentCtx; 97 100 installer.getComponentName(); 101 verify(componentCtx); 102 } 103 104 public void testGetCurrentState() { 105 108 Installer installer = createMock(Installer.class,new Method []{}); 109 112 113 assertEquals("Wrong invokation result",LifeCycleMBean.UNKNOWN,installer.getCurrentState()); 114 115 } 116 117 public void testGetInstallerConfigurationMBean() { 118 124 Method method1 = null; 125 try { 126 method1 = Installer.class.getDeclaredMethod("loadBootstrap", new Class []{PetalsClassLoader.class}); 127 } catch (Exception e ) { 128 e.printStackTrace(); 129 fail("Could not retrieve mocked methods"); 130 } 131 Installer installer = createMock(Installer.class,new Method []{method1}); 132 133 BootstrapThread installerThread = createMock(BootstrapThread.class); 134 ObjectName oName = createMock(ObjectName .class); 135 installer.componentJmxName = oName; 136 137 expect(installerThread.getExtensionMBeanName()).andReturn(oName); 138 replay(installerThread); 139 installer.bootstrapThread = installerThread; 140 141 LifeCycleManagerService lifeCycleManager = createMock(LifeCycleManagerService.class); 142 installer.jmxAdmin = lifeCycleManager; 143 ComponentContextImpl componentCtx = createMock(ComponentContextImpl.class); 144 installer.componentContext = componentCtx; 145 146 149 try { 150 installer.getInstallerConfigurationMBean(); 151 fail("No exception was raised"); 152 } catch (Exception e) { 153 } 155 156 160 reset(installer); 161 reset(installerThread); 162 reset(lifeCycleManager); 163 reset(componentCtx); 164 167 Bootstrap bootstrap = createMock(Bootstrap.class); 168 ObjectName objectName = createMock(ObjectName .class); 169 PetalsClassLoader classloader = createMock(PetalsClassLoader.class); 170 installer.state = Installer.INSTALLED; 171 installer.bootstrapThread = installerThread; 172 String componentName = "componentName"; 173 ComponentLifeCycle lifeCycle = createMock(ComponentLifeCycle.class); 174 installer.componentJmxName = objectName; 175 178 try { 179 expect(bootstrap.getExtensionMBeanName()).andReturn(objectName); 180 expect(installer.loadBootstrap(classloader)).andReturn(bootstrap); 181 expect(installerThread.getExtensionMBeanName()).andReturn(objectName); 182 expect(lifeCycleManager.getComponentByName(componentName)).andReturn(lifeCycle); 183 expect(lifeCycle.getCurrentState()).andReturn(LifeCycleMBean.SHUTDOWN); 184 expect(componentCtx.getComponentName()).andReturn(componentName); 185 } catch (Exception e) { 186 e.printStackTrace(); 187 fail("Could not initialize Mocks"); 188 } 189 replay(installer); 190 replay(installerThread); 191 replay(bootstrap); 192 replay(lifeCycle); 193 replay(lifeCycleManager); 194 replay(componentCtx); 195 installer.jmxAdmin = lifeCycleManager; 196 installer.componentContext = componentCtx; 197 installer.jbiBootstrap = bootstrap; 198 installer.bootstrapThread = installerThread; 199 ObjectName result = null; 200 201 204 try { 205 result = installer.getInstallerConfigurationMBean(); 206 } catch (Exception e) { 207 e.printStackTrace(); 208 fail("Error during invokation"); 209 } 210 verify(installerThread); 211 verify(lifeCycle); 212 verify(lifeCycleManager); 213 verify(componentCtx); 214 assertEquals("Wrong invokation result",result, objectName); 215 } 216 217 public void testGetInstallRoot() { 218 221 Installer installer = createMock(Installer.class,new Method []{}); 222 225 String installationRoot = "installation/root"; 226 ComponentContextImpl componentCtx = createMock(ComponentContextImpl.class); 227 expect(componentCtx.getInstallRoot()).andReturn(installationRoot); 228 replay(componentCtx); 229 installer.componentContext = componentCtx; 230 233 String result = installer.getInstallRoot(); 234 assertNotNull("Result of getInstallRoot must not be null",result); 235 assertFalse("Result of getInstallRoot must not be empty",result.length() == 0); 236 verify(componentCtx); 237 } 238 239 public void testGetMBeanName() { 240 243 Installer installer = createMock(Installer.class,new Method []{}); 244 247 installer.mBeanName = createMock(ObjectName .class); 248 251 assertEquals("Wroong invokation result",installer.getMBeanName(),installer.mBeanName); 252 } 253 254 public void testGetNotificationInfo() { 255 258 Installer installer = createMock(Installer.class,new Method []{}); 259 262 MBeanNotificationInfo [] result= null; 263 266 result = installer.getNotificationInfo(); 267 assertTrue("Wrong invokation result",result.length == 1); 268 } 269 270 public void testInstall() { 271 275 Method method1 = null; 276 Method method2 = null; 277 Method method3 = null; 278 try { 279 method1 = Installer.class.getDeclaredMethod("stateChangeFailed", new Class []{StateChangeFailedEvent.class}); 280 method2 = Installer.class.getDeclaredMethod("setState", new Class []{String .class}); 281 method3 = Installer.class.getDeclaredMethod("doInstall", new Class []{}); 282 } catch (Exception e) { 283 e.printStackTrace(); 284 fail("Could not retrieve mocked methods"); 285 } 286 Method [] mockedMethods = new Method []{method1, method2, method3}; 287 Installer installer = createMock(Installer.class,mockedMethods); 288 LoggingUtil log = createMock(LoggingUtil.class); 289 installer.log = log; 290 293 296 installer.stateChangeFailed(isA(StateChangeFailedEvent.class)); 297 replay(installer); 298 installer.activitySynchronizer = new Object (); 299 302 try { 303 installer.install(); 304 fail("No exception was raised"); 305 } catch (Exception e) { 306 verify(installer); 307 } 308 309 312 reset(installer); 313 316 installer.state = Installer.UNINSTALLED; 317 ObjectName objectName = createMock(ObjectName .class); 318 installer.componentJmxName = objectName; 319 ObjectName result = null; 320 323 try { 324 installer.setState(Installer.INSTALLING); 325 installer.doInstall(); 326 installer.setState(Installer.INSTALLED); 327 } catch (Exception e) { 328 e.printStackTrace(); 329 fail("Could not initialize Mocks"); 330 } 331 replay(installer); 332 335 try { 336 result = installer.install(); 337 } catch (Exception e) { 338 e.printStackTrace(); 339 fail("Error during invokation"); 340 } 341 verify(installer); 342 assertEquals("Wrong invokation result",result,objectName); 343 344 347 351 installer = createMock(MockInstaller.class,new Method []{method1, method2}); 352 installer.state = Installer.UNINSTALLED; 353 installer.componentJmxName = objectName; 354 installer.log = log; 355 installer.activitySynchronizer = new Object (); 356 result = null; 357 try { 358 installer.setState(Installer.INSTALLING); 360 installer.setState(Installer.UNKNOWN); 361 installer.stateChangeFailed(isA(StateChangeFailedEvent.class)); 362 } catch (Exception e) { 363 e.printStackTrace(); 364 fail("Could not initialize Mocks"); 365 } 366 replay(installer); 367 370 try { 371 result = installer.install(); 372 fail("No exception was raised"); 373 } catch (Exception e) { 374 verify(installer); 375 } 376 } 377 378 public void testStateChanged() { 379 382 Installer installer = createMock(Installer.class,new Method []{}); 383 386 LoggingUtil log = createMock(LoggingUtil.class); 387 installer.log = log; 388 installer.mBeanName = createMock(ObjectName .class); 389 StateChangedEvent stateChange = new StateChangedEvent("source","oldState","newState"); 390 NotificationBroadcasterSupport notificationBroacaster = createMock(NotificationBroadcasterSupport .class); 391 394 notificationBroacaster.sendNotification(isA(Notification .class)); 395 replay(notificationBroacaster); 396 installer.notifSupport = notificationBroacaster; 397 400 installer.stateChanged(stateChange); 401 verify(notificationBroacaster); 402 403 } 404 405 public void testStateChangeFailed() { 406 409 Installer installer = createMock(Installer.class,new Method []{}); 410 413 LoggingUtil log = createMock(LoggingUtil.class); 414 installer.log = log; 415 installer.mBeanName = createMock(ObjectName .class); 416 StateChangeFailedEvent stateChangeFailed = new StateChangeFailedEvent("source",new JBIException("testStateChangeFailed testing exception")); 417 NotificationBroadcasterSupport notificationBroacaster = createMock(NotificationBroadcasterSupport .class); 418 421 notificationBroacaster.sendNotification(isA(Notification .class)); 422 replay(notificationBroacaster); 423 installer.notifSupport = notificationBroacaster; 424 427 installer.stateChangeFailed(stateChangeFailed); 428 verify(notificationBroacaster); 429 } 430 431 public void testInstaller() { 432 435 438 Installer result = null; 439 URI installationURI = new File (testDataDir, "fakearchive" + File.separator + "archive").toURI(); 440 LoaderManager loaderManager = createMock(LoaderManager.class); 441 ComponentContextImpl componentCtx = createMock(ComponentContextImpl.class); 442 ComponentDescription componentDescription = createMock(ComponentDescription.class); 443 LifeCycleManagerService lifeCycleManager = createMock(LifeCycleManagerService.class); 444 SystemState systemState = createMock(SystemState.class); 445 Logger logger = null; 446 ObjectName objectName = createMock(ObjectName .class); 447 450 try { 451 result = new Installer(installationURI, loaderManager, 452 componentCtx, componentDescription, lifeCycleManager, 453 systemState, objectName, logger); 454 } catch (Exception e) { 455 e.printStackTrace(); 456 fail("Error during invokation"); 457 } 458 assertNotNull("",result); 459 } 460 461 public void testIsInstalled() { 462 465 Installer installer = createMock(Installer.class,new Method []{}); 466 469 installer.state = Installer.INSTALLED; 470 473 assertTrue("Wrong invokation result",installer.isInstalled()); 474 } 475 476 public void testRemoveNotificationListener() { 477 480 Installer installer = createMock(Installer.class,new Method []{}); 481 484 installer.notifSupport = createMock(NotificationBroadcasterSupport .class); 485 NotificationListener notificationListener = createMock(NotificationListener .class); 486 489 try { 490 installer.removeNotificationListener(notificationListener); 491 } catch (Exception e) { 492 e.printStackTrace(); 493 fail("Error during invokation"); 494 } 495 } 496 497 public void testSetState() { 498 502 Method method1 = null; 503 Method method2 = null; 504 try { 505 method1 = Installer.class.getDeclaredMethod("stateChanged", new Class []{StateChangedEvent.class}); 506 method2 = Installer.class.getDeclaredMethod("getComponentName", new Class []{}); 507 } catch (Exception e) { 508 e.printStackTrace(); 509 fail("Could not retrieve mocked methods"); 510 } 511 Method [] mockedMethods = new Method []{method1,method2}; 512 Installer installer = createMock(Installer.class,mockedMethods); 513 516 LoggingUtil log = createMock(LoggingUtil.class); 517 installer.log = log; 518 installer.recoverySrv = createMock(MockSystemState.class, new Method []{}); 519 String componentName = "componentName"; 520 523 installer.stateChanged(isA(StateChangedEvent.class)); 524 expect(installer.getComponentName()).andReturn(componentName); 525 528 try { 529 installer.setState("state"); 530 fail("No exception was raised"); 531 } catch (Exception e) { 532 } 534 } 535 536 public void testUninstall() { 537 541 Method method1 = null; 542 Method method2 = null; 543 Method method3 = null; 544 try { 545 method1 = Installer.class.getDeclaredMethod("stateChangeFailed", new Class []{StateChangeFailedEvent.class}); 546 method2 = Installer.class.getDeclaredMethod("getComponentLifeCycle", new Class []{}); 547 method3 = Installer.class.getDeclaredMethod("doUninstall", new Class []{}); 548 } catch (Exception e) { 549 e.printStackTrace(); 550 fail("Could not retrieve mocked methods"); 551 } 552 Method [] mockedMethods = new Method []{method1, method2, method3}; 553 Installer installer = createMock(Installer.class,mockedMethods); 554 installer.state = Installer.UNINSTALLED; 555 LoggingUtil log = createMock(LoggingUtil.class); 556 installer.log = log; 557 560 563 installer.stateChangeFailed(isA(StateChangeFailedEvent.class)); 564 replay(installer); 565 568 try { 569 installer.uninstall(); 570 fail("No exception was raised"); 571 } catch (Exception e) { 572 verify(installer); 573 } 574 } 575 576 @Override 577 protected void setUp() throws Exception { 578 testDataDir = new File ("src" + File.separator + "test-data"); 579 if (!testDataDir.exists()) { 580 testDataDir = new File ("container" + File.separator + "src" 581 + File.separator + "test-data"); 582 if (!testDataDir.exists()) { 583 testDataDir = new File ("platform" + File.separator 584 + "container" + File.separator + "src" + File.separator 585 + "test-data"); 586 } 587 } 588 } 589 590 591 } 592 | Popular Tags |