1 17 package org.apache.servicemix.jbi.installation; 18 19 import java.io.File ; 20 21 import javax.jbi.component.Bootstrap; 22 import javax.jbi.component.Component; 23 import javax.jbi.component.ComponentLifeCycle; 24 import javax.jbi.component.ServiceUnitManager; 25 import javax.jbi.management.DeploymentServiceMBean; 26 import javax.jbi.management.InstallerMBean; 27 import javax.jbi.management.LifeCycleMBean; 28 import javax.management.MBeanServerInvocationHandler ; 29 import javax.management.ObjectName ; 30 31 import org.easymock.MockControl; 32 33 public class DeploymentTest extends AbstractManagementTest { 34 35 protected ExtMockControl bootstrapMock; 37 protected Bootstrap bootstrap; 38 protected ExtMockControl componentMock; 39 protected Component component; 40 protected ExtMockControl lifecycleMock; 41 protected ComponentLifeCycle lifecycle; 42 protected ExtMockControl managerMock; 43 protected ServiceUnitManager manager; 44 45 protected void setUp() throws Exception { 46 super.setUp(); 47 bootstrapMock = ExtMockControl.createControl(Bootstrap.class); 49 bootstrap = (Bootstrap) bootstrapMock.getMock(); 50 Bootstrap1.setDelegate(bootstrap); 51 componentMock = ExtMockControl.createControl(Component.class); 52 component = (Component) componentMock.getMock(); 53 Component1.setDelegate(component); 54 lifecycleMock = ExtMockControl.createControl(ComponentLifeCycle.class); 55 lifecycle = (ComponentLifeCycle) lifecycleMock.getMock(); 56 managerMock = ExtMockControl.createControl(ServiceUnitManager.class); 57 manager = (ServiceUnitManager) managerMock.getMock(); 58 } 59 60 protected void reset() { 61 bootstrapMock.reset(); 62 componentMock.reset(); 63 lifecycleMock.reset(); 64 managerMock.reset(); 65 } 66 67 protected void replay() { 68 bootstrapMock.replay(); 69 componentMock.replay(); 70 lifecycleMock.replay(); 71 managerMock.replay(); 72 } 73 74 protected void verify() { 75 bootstrapMock.verify(); 76 componentMock.verify(); 77 lifecycleMock.verify(); 78 managerMock.verify(); 79 } 80 81 85 public void testDeployAndStart() throws Exception { 86 reset(); 88 bootstrap.init(null); 89 bootstrapMock.setMatcher(MockControl.ALWAYS_MATCHER); 90 bootstrap.onInstall(); 91 bootstrap.getExtensionMBeanName(); 92 bootstrapMock.setReturnValue(null); 93 bootstrap.cleanUp(); 94 replay(); 95 startContainer(true); 97 String installJarUrl = createInstallerArchive("component1").getAbsolutePath(); 98 ObjectName installerName = getInstallationService().loadNewInstaller(installJarUrl); 99 InstallerMBean installer = (InstallerMBean) MBeanServerInvocationHandler.newProxyInstance(container.getMBeanServer(), installerName, InstallerMBean.class, false); 100 assertFalse(installer.isInstalled()); 101 ObjectName lifecycleName = installer.install(); 102 LifeCycleMBean lifecycleMBean = (LifeCycleMBean) MBeanServerInvocationHandler.newProxyInstance(container.getMBeanServer(), lifecycleName, LifeCycleMBean.class, false); 103 assertEquals(LifeCycleMBean.SHUTDOWN, lifecycleMBean.getCurrentState()); 104 verify(); 106 107 reset(); 109 component.getLifeCycle(); 110 componentMock.setReturnValue(lifecycle, MockControl.ONE_OR_MORE); 111 lifecycle.init(null); 112 lifecycleMock.setMatcher(MockControl.ALWAYS_MATCHER); 113 lifecycle.start(); 114 replay(); 115 lifecycleMBean.start(); 117 assertEquals(LifeCycleMBean.STARTED, lifecycleMBean.getCurrentState()); 118 verify(); 120 121 reset(); 123 component.getServiceUnitManager(); 124 componentMock.setReturnValue(manager, MockControl.ONE_OR_MORE); 125 manager.deploy(null, null); 126 managerMock.setMatcher(MockControl.ALWAYS_MATCHER); 127 managerMock.setReturnValue(null); 128 replay(); 129 assertTrue(getDeploymentService().canDeployToComponent("component1")); 131 File installSaUrl = createServiceAssemblyArchive("sa", "su", "component1"); 132 String result = getDeploymentService().deploy(installSaUrl.getAbsolutePath()); 133 System.err.println(result); 134 String [] sas = getDeploymentService().getDeployedServiceAssemblies(); 135 assertNotNull(sas); 136 assertEquals(1, sas.length); 137 assertEquals("sa", sas[0]); 138 sas = getDeploymentService().getDeployedServiceAssembliesForComponent("component1"); 139 assertNotNull(sas); 140 assertEquals(1, sas.length); 141 assertEquals("sa", sas[0]); 142 assertEquals(DeploymentServiceMBean.SHUTDOWN, getDeploymentService().getState("sa")); 143 verify(); 145 146 reset(); 148 component.getServiceUnitManager(); 149 componentMock.setReturnValue(manager, MockControl.ZERO_OR_MORE); 150 manager.init(null, null); 151 managerMock.setMatcher(MockControl.ALWAYS_MATCHER); 152 manager.start("su"); 153 replay(); 154 getDeploymentService().start("sa"); 156 assertEquals(DeploymentServiceMBean.STARTED, getDeploymentService().getState("sa")); 157 verify(); 159 160 reset(); 162 component.getLifeCycle(); 163 componentMock.setReturnValue(lifecycle, MockControl.ONE_OR_MORE); 164 component.getServiceUnitManager(); 165 componentMock.setReturnValue(manager, MockControl.ONE_OR_MORE); 166 lifecycle.stop(); 167 lifecycle.shutDown(); 168 manager.shutDown("su"); 170 replay(); 171 shutdownContainer(); 172 } 173 174 178 public void testDeployAndRestart() throws Exception { 179 reset(); 181 bootstrap.init(null); 182 bootstrapMock.setMatcher(MockControl.ALWAYS_MATCHER); 183 bootstrap.onInstall(); 184 bootstrap.getExtensionMBeanName(); 185 bootstrapMock.setReturnValue(null); 186 bootstrap.cleanUp(); 187 replay(); 188 startContainer(true); 190 String installJarUrl = createInstallerArchive("component1").getAbsolutePath(); 191 ObjectName installerName = getInstallationService().loadNewInstaller(installJarUrl); 192 InstallerMBean installer = (InstallerMBean) MBeanServerInvocationHandler.newProxyInstance(container.getMBeanServer(), installerName, InstallerMBean.class, false); 193 assertFalse(installer.isInstalled()); 194 ObjectName lifecycleName = installer.install(); 195 LifeCycleMBean lifecycleMBean = (LifeCycleMBean) MBeanServerInvocationHandler.newProxyInstance(container.getMBeanServer(), lifecycleName, LifeCycleMBean.class, false); 196 assertEquals(LifeCycleMBean.SHUTDOWN, lifecycleMBean.getCurrentState()); 197 verify(); 199 200 reset(); 202 component.getLifeCycle(); 203 componentMock.setReturnValue(lifecycle, MockControl.ONE_OR_MORE); 204 lifecycle.init(null); 205 lifecycleMock.setMatcher(MockControl.ALWAYS_MATCHER); 206 lifecycle.start(); 207 replay(); 208 lifecycleMBean.start(); 210 assertEquals(LifeCycleMBean.STARTED, lifecycleMBean.getCurrentState()); 211 verify(); 213 214 reset(); 216 component.getServiceUnitManager(); 217 componentMock.setReturnValue(manager, MockControl.ONE_OR_MORE); 218 manager.deploy(null, null); 219 managerMock.setMatcher(MockControl.ALWAYS_MATCHER); 220 managerMock.setReturnValue(null); 221 replay(); 222 assertTrue(getDeploymentService().canDeployToComponent("component1")); 224 File installSaUrl = createServiceAssemblyArchive("sa", "su", "component1"); 225 getDeploymentService().deploy(installSaUrl.getAbsolutePath()); 226 String [] sas = getDeploymentService().getDeployedServiceAssemblies(); 227 assertNotNull(sas); 228 assertEquals(1, sas.length); 229 assertEquals("sa", sas[0]); 230 sas = getDeploymentService().getDeployedServiceAssembliesForComponent("component1"); 231 assertNotNull(sas); 232 assertEquals(1, sas.length); 233 assertEquals("sa", sas[0]); 234 assertEquals(DeploymentServiceMBean.SHUTDOWN, getDeploymentService().getState("sa")); 235 verify(); 237 238 reset(); 240 lifecycle.stop(); 241 lifecycle.shutDown(); 242 replay(); 243 shutdownContainer(); 245 verify(); 247 248 reset(); 250 bootstrap.init(null); 252 bootstrapMock.setMatcher(MockControl.ALWAYS_MATCHER); 253 bootstrap.getExtensionMBeanName(); 254 bootstrapMock.setReturnValue(null); 255 component.getLifeCycle(); 256 componentMock.setReturnValue(lifecycle, MockControl.ONE_OR_MORE); 257 lifecycle.init(null); 258 lifecycleMock.setMatcher(MockControl.ALWAYS_MATCHER); 259 lifecycle.start(); 260 component.getServiceUnitManager(); 261 componentMock.setReturnValue(manager, MockControl.ONE_OR_MORE); 262 manager.init(null, null); 263 managerMock.setMatcher(MockControl.ALWAYS_MATCHER); 264 manager.shutDown("su"); 265 replay(); 266 startContainer(false); 268 verify(); 270 271 reset(); 273 component.getLifeCycle(); 274 componentMock.setReturnValue(lifecycle, MockControl.ONE_OR_MORE); 275 component.getServiceUnitManager(); 276 componentMock.setReturnValue(manager, MockControl.ONE_OR_MORE); 277 lifecycle.stop(); 278 lifecycle.shutDown(); 279 manager.shutDown("su"); 281 replay(); 282 shutdownContainer(); 283 } 284 285 289 public void testDeployStartAndRestart() throws Exception { 290 reset(); 292 bootstrap.init(null); 293 bootstrapMock.setMatcher(MockControl.ALWAYS_MATCHER); 294 bootstrap.onInstall(); 295 bootstrap.getExtensionMBeanName(); 296 bootstrapMock.setReturnValue(null); 297 bootstrap.cleanUp(); 298 replay(); 299 startContainer(true); 301 String installJarUrl = createInstallerArchive("component1").getAbsolutePath(); 302 ObjectName installerName = getInstallationService().loadNewInstaller(installJarUrl); 303 InstallerMBean installer = (InstallerMBean) MBeanServerInvocationHandler.newProxyInstance(container.getMBeanServer(), installerName, InstallerMBean.class, false); 304 assertFalse(installer.isInstalled()); 305 ObjectName lifecycleName = installer.install(); 306 LifeCycleMBean lifecycleMBean = (LifeCycleMBean) MBeanServerInvocationHandler.newProxyInstance(container.getMBeanServer(), lifecycleName, LifeCycleMBean.class, false); 307 assertEquals(LifeCycleMBean.SHUTDOWN, lifecycleMBean.getCurrentState()); 308 verify(); 310 311 reset(); 313 component.getLifeCycle(); 314 componentMock.setReturnValue(lifecycle, MockControl.ONE_OR_MORE); 315 lifecycle.init(null); 316 lifecycleMock.setMatcher(MockControl.ALWAYS_MATCHER); 317 lifecycle.start(); 318 replay(); 319 lifecycleMBean.start(); 321 assertEquals(LifeCycleMBean.STARTED, lifecycleMBean.getCurrentState()); 322 verify(); 324 325 reset(); 327 component.getServiceUnitManager(); 328 componentMock.setReturnValue(manager, MockControl.ONE_OR_MORE); 329 manager.deploy(null, null); 330 managerMock.setMatcher(MockControl.ALWAYS_MATCHER); 331 managerMock.setReturnValue(null); 332 replay(); 333 assertTrue(getDeploymentService().canDeployToComponent("component1")); 335 File installSaUrl = createServiceAssemblyArchive("sa", "su", "component1"); 336 getDeploymentService().deploy(installSaUrl.getAbsolutePath()); 337 String [] sas = getDeploymentService().getDeployedServiceAssemblies(); 338 assertNotNull(sas); 339 assertEquals(1, sas.length); 340 assertEquals("sa", sas[0]); 341 sas = getDeploymentService().getDeployedServiceAssembliesForComponent("component1"); 342 assertNotNull(sas); 343 assertEquals(1, sas.length); 344 assertEquals("sa", sas[0]); 345 assertEquals(DeploymentServiceMBean.SHUTDOWN, getDeploymentService().getState("sa")); 346 verify(); 348 349 reset(); 351 manager.init(null, null); 352 managerMock.setMatcher(MockControl.ALWAYS_MATCHER); 353 manager.start("su"); 354 replay(); 355 getDeploymentService().start("sa"); 357 assertEquals(DeploymentServiceMBean.STARTED, getDeploymentService().getState("sa")); 358 verify(); 360 361 reset(); 363 lifecycle.stop(); 364 lifecycle.shutDown(); 365 manager.stop("su"); 366 manager.shutDown("su"); 367 replay(); 368 shutdownContainer(); 370 verify(); 372 373 reset(); 375 bootstrap.init(null); 377 bootstrapMock.setMatcher(MockControl.ALWAYS_MATCHER); 378 bootstrap.getExtensionMBeanName(); 379 bootstrapMock.setReturnValue(null); 380 component.getLifeCycle(); 381 componentMock.setReturnValue(lifecycle, MockControl.ONE_OR_MORE); 382 lifecycle.init(null); 383 lifecycleMock.setMatcher(MockControl.ALWAYS_MATCHER); 384 lifecycle.start(); 385 component.getServiceUnitManager(); 386 componentMock.setReturnValue(manager, MockControl.ONE_OR_MORE); 387 manager.init(null, null); 388 managerMock.setMatcher(MockControl.ALWAYS_MATCHER); 389 manager.start("su"); 390 managerMock.setMatcher(MockControl.ALWAYS_MATCHER); 391 replay(); 392 startContainer(false); 394 verify(); 396 397 reset(); 399 component.getLifeCycle(); 400 componentMock.setReturnValue(lifecycle, MockControl.ONE_OR_MORE); 401 component.getServiceUnitManager(); 402 componentMock.setReturnValue(manager, MockControl.ONE_OR_MORE); 403 lifecycle.stop(); 404 lifecycle.shutDown(); 405 manager.shutDown("su"); 407 replay(); 408 shutdownContainer(); 409 } 410 411 } 412 | Popular Tags |