1 17 package org.apache.servicemix.jbi.installation; 18 19 import java.io.File ; 20 import java.io.FileInputStream ; 21 import java.io.FileOutputStream ; 22 23 import javax.jbi.JBIException; 24 import javax.jbi.component.Bootstrap; 25 import javax.jbi.component.Component; 26 import javax.jbi.component.ComponentLifeCycle; 27 import javax.jbi.component.ServiceUnitManager; 28 import javax.jbi.management.LifeCycleMBean; 29 import javax.management.MBeanServerInvocationHandler ; 30 import javax.management.ObjectName ; 31 32 import org.apache.servicemix.jbi.util.FileUtil; 33 import org.easymock.MockControl; 34 35 public class HotDeployTest extends AbstractManagementTest { 36 37 protected ExtMockControl bootstrapMock; 39 protected Bootstrap bootstrap; 40 protected ExtMockControl componentMock; 41 protected Component component; 42 protected ExtMockControl lifecycleMock; 43 protected ComponentLifeCycle lifecycle; 44 protected ExtMockControl managerMock; 45 protected ServiceUnitManager manager; 46 47 protected void setUp() throws Exception { 48 super.setUp(); 49 bootstrapMock = ExtMockControl.createControl(Bootstrap.class); 51 bootstrap = (Bootstrap) bootstrapMock.getMock(); 52 Bootstrap1.setDelegate(bootstrap); 53 componentMock = ExtMockControl.createControl(Component.class); 54 component = (Component) componentMock.getMock(); 55 Component1.setDelegate(component); 56 lifecycleMock = ExtMockControl.createControl(ComponentLifeCycle.class); 57 lifecycle = (ComponentLifeCycle) lifecycleMock.getMock(); 58 managerMock = ExtMockControl.createControl(ServiceUnitManager.class); 59 manager = (ServiceUnitManager) managerMock.getMock(); 60 } 61 62 protected void reset() { 63 bootstrapMock.reset(); 64 componentMock.reset(); 65 lifecycleMock.reset(); 66 managerMock.reset(); 67 } 68 69 protected void replay() { 70 bootstrapMock.replay(); 71 componentMock.replay(); 72 lifecycleMock.replay(); 73 managerMock.replay(); 74 } 75 76 protected void verify() { 77 bootstrapMock.verify(); 78 componentMock.verify(); 79 lifecycleMock.verify(); 80 managerMock.verify(); 81 } 82 83 protected void initContainer() { 84 container.setCreateMBeanServer(true); 85 container.setMonitorInstallationDirectory(true); 86 container.setMonitorDeploymentDirectory(true); 87 container.setMonitorInterval(1); 88 } 89 90 91 92 public void testHotDeployComponent() throws Exception { 93 final Object lock = new Object (); 94 Bootstrap1.setDelegate(new BootstrapDelegate(bootstrap) { 96 public void cleanUp() throws JBIException { 97 super.cleanUp(); 98 synchronized (lock) { 99 lock.notify(); 100 } 101 } 102 }); 103 reset(); 104 bootstrap.init(null); 105 bootstrapMock.setMatcher(MockControl.ALWAYS_MATCHER); 106 bootstrap.getExtensionMBeanName(); 107 bootstrapMock.setReturnValue(null); 108 bootstrap.onInstall(); 109 bootstrap.cleanUp(); 110 component.getLifeCycle(); 111 componentMock.setReturnValue(lifecycle); 112 lifecycle.init(null); 113 lifecycleMock.setMatcher(MockControl.ALWAYS_MATCHER); 114 lifecycle.start(); 115 replay(); 116 startContainer(true); 118 String installJarUrl = createInstallerArchive("component1").getAbsolutePath(); 119 File hdInstaller = new File (container.getEnvironmentContext().getInstallationDir(), new File (installJarUrl).getName()); 120 synchronized (lock) { 121 FileUtil.copyInputStream(new FileInputStream (installJarUrl), 122 new FileOutputStream (hdInstaller)); 123 lock.wait(5000); 124 } 125 Thread.sleep(50); 126 ObjectName lifecycleName = container.getComponent("component1").getMBeanName(); 127 assertNotNull(lifecycleName); 128 LifeCycleMBean lifecycleMBean = (LifeCycleMBean) MBeanServerInvocationHandler.newProxyInstance(container.getMBeanServer(), lifecycleName, LifeCycleMBean.class, false); 129 assertEquals(LifeCycleMBean.STARTED, lifecycleMBean.getCurrentState()); 130 verify(); 132 133 reset(); 135 component.getLifeCycle(); 136 componentMock.setReturnValue(lifecycle); 137 lifecycle.stop(); 138 lifecycle.shutDown(); 139 replay(); 140 shutdownContainer(); 141 } 142 143 public void testHotDeployUndeployComponent() throws Exception { 144 final Object lock = new Object (); 145 Bootstrap1.setDelegate(new BootstrapDelegate(bootstrap) { 147 public void cleanUp() throws JBIException { 148 super.cleanUp(); 149 synchronized (lock) { 150 lock.notify(); 151 } 152 } 153 }); 154 reset(); 155 bootstrap.init(null); 156 bootstrapMock.setMatcher(MockControl.ALWAYS_MATCHER); 157 bootstrap.getExtensionMBeanName(); 158 bootstrapMock.setReturnValue(null); 159 bootstrap.onInstall(); 160 bootstrap.cleanUp(); 161 component.getLifeCycle(); 162 componentMock.setReturnValue(lifecycle); 163 lifecycle.init(null); 164 lifecycleMock.setMatcher(MockControl.ALWAYS_MATCHER); 165 lifecycle.start(); 166 replay(); 167 startContainer(true); 169 String installJarUrl = createInstallerArchive("component1").getAbsolutePath(); 170 File hdInstaller = new File (container.getEnvironmentContext().getInstallationDir(), new File (installJarUrl).getName()); 171 synchronized (lock) { 172 FileUtil.copyInputStream(new FileInputStream (installJarUrl), 173 new FileOutputStream (hdInstaller)); 174 lock.wait(5000); 175 } 176 Thread.sleep(50); 177 ObjectName lifecycleName = container.getComponent("component1").getMBeanName(); 178 assertNotNull(lifecycleName); 179 LifeCycleMBean lifecycleMBean = (LifeCycleMBean) MBeanServerInvocationHandler.newProxyInstance(container.getMBeanServer(), lifecycleName, LifeCycleMBean.class, false); 180 assertEquals(LifeCycleMBean.STARTED, lifecycleMBean.getCurrentState()); 181 verify(); 183 184 reset(); 186 bootstrap.onUninstall(); 187 bootstrap.cleanUp(); 188 lifecycle.stop(); 189 lifecycle.shutDown(); 190 replay(); 192 synchronized (lock) { 194 assertTrue(hdInstaller.delete()); 195 lock.wait(5000); 196 } 197 Thread.sleep(50); 198 assertNull(container.getComponent("component1")); 199 verify(); 201 202 reset(); 204 replay(); 205 shutdownContainer(); 206 } 207 208 public void testDeploySAThenComponent() throws Exception { 209 final Object lock = new Object (); 210 Bootstrap1.setDelegate(new BootstrapDelegate(bootstrap) { 212 public void cleanUp() throws JBIException { 213 super.cleanUp(); 214 synchronized (lock) { 215 lock.notify(); 216 } 217 } 218 }); 219 reset(); 220 bootstrap.init(null); 221 bootstrapMock.setMatcher(MockControl.ALWAYS_MATCHER); 222 bootstrap.getExtensionMBeanName(); 223 bootstrapMock.setReturnValue(null); 224 bootstrap.onInstall(); 225 bootstrap.cleanUp(); 226 component.getLifeCycle(); 227 componentMock.setReturnValue(lifecycle); 228 lifecycle.init(null); 229 lifecycleMock.setMatcher(MockControl.ALWAYS_MATCHER); 230 lifecycle.start(); 231 component.getServiceUnitManager(); 232 componentMock.setReturnValue(manager, MockControl.ONE_OR_MORE); 233 manager.deploy(null, null); 234 managerMock.setMatcher(MockControl.ALWAYS_MATCHER); 235 managerMock.setReturnValue(null); 236 manager.init(null, null); 237 managerMock.setMatcher(MockControl.ALWAYS_MATCHER); 238 manager.start("su"); 239 replay(); 240 startContainer(true); 242 File saJarUrl = createServiceAssemblyArchive("sa", "su", "component1"); 243 File hdSa = new File (container.getEnvironmentContext().getDeploymentDir(), saJarUrl.getName()); 244 FileUtil.copyInputStream(new FileInputStream (saJarUrl), 245 new FileOutputStream (hdSa)); 246 Thread.sleep(2000); 247 248 String installJarUrl = createInstallerArchive("component1").getAbsolutePath(); 249 File hdInstaller = new File (container.getEnvironmentContext().getInstallationDir(), new File (installJarUrl).getName()); 250 synchronized (lock) { 251 FileUtil.copyInputStream(new FileInputStream (installJarUrl), 252 new FileOutputStream (hdInstaller)); 253 lock.wait(5000); 254 } 255 Thread.sleep(2000); 256 ObjectName lifecycleName = container.getComponent("component1").getMBeanName(); 257 assertNotNull(lifecycleName); 258 LifeCycleMBean lifecycleMBean = (LifeCycleMBean) MBeanServerInvocationHandler.newProxyInstance(container.getMBeanServer(), lifecycleName, LifeCycleMBean.class, false); 259 assertEquals(LifeCycleMBean.STARTED, lifecycleMBean.getCurrentState()); 260 verify(); 262 263 reset(); 265 component.getLifeCycle(); 266 componentMock.setReturnValue(lifecycle); 267 component.getServiceUnitManager(); 268 componentMock.setReturnValue(manager, MockControl.ONE_OR_MORE); 269 lifecycle.stop(); 270 manager.shutDown("su"); 271 lifecycle.shutDown(); 272 replay(); 273 shutdownContainer(); 274 } 275 } 276 | Popular Tags |