1 22 package org.jboss.test.deployment; 23 24 import org.jboss.deployment.spi.DeploymentManagerImpl; 25 import org.jboss.deployment.spi.DeploymentMetaData; 26 import org.jboss.deployment.spi.JarUtils; 27 import org.jboss.deployment.spi.TargetModuleIDImpl; 28 import org.jboss.test.JBossTestCase; 29 import org.jboss.util.UnreachableStatementException; 30 31 import javax.ejb.CreateException ; 32 import javax.enterprise.deploy.shared.ModuleType ; 33 import javax.enterprise.deploy.shared.StateType ; 34 import javax.enterprise.deploy.shared.factories.DeploymentFactoryManager ; 35 import javax.enterprise.deploy.spi.DeploymentManager ; 36 import javax.enterprise.deploy.spi.Target ; 37 import javax.enterprise.deploy.spi.TargetModuleID ; 38 import javax.enterprise.deploy.spi.factories.DeploymentFactory ; 39 import javax.enterprise.deploy.spi.status.DeploymentStatus ; 40 import javax.enterprise.deploy.spi.status.ProgressObject ; 41 import javax.naming.InitialContext ; 42 import javax.naming.NamingException ; 43 import javax.rmi.PortableRemoteObject ; 44 45 import java.io.BufferedReader ; 46 import java.io.ByteArrayInputStream ; 47 import java.io.File ; 48 import java.io.FileInputStream ; 49 import java.io.FileOutputStream ; 50 import java.io.IOException ; 51 import java.io.InputStreamReader ; 52 import java.net.URI ; 53 import java.net.URL ; 54 import java.rmi.RemoteException ; 55 import java.util.jar.JarOutputStream ; 56 57 64 public class DeploymentTestCase extends JBossTestCase 65 { 66 private static final String WAR_JBOSS_FILE = "WEB-INF/jboss-web.xml"; 67 68 private static final String JAR_JBOSS_FILE = "META-INF/jboss.xml"; 69 70 private static final String EAR_JBOSS_FILE = "META-INF/jboss-app.xml"; 71 72 private DeploymentFactory factory; 73 74 public DeploymentTestCase(String name) 75 { 76 super(name); 77 } 78 79 81 protected void setUp() throws Exception 82 { 83 super.setUp(); 84 DeploymentFactoryManager dfManager = DeploymentFactoryManager.getInstance(); 85 DeploymentFactory [] factories = dfManager.getDeploymentFactories(); 86 assertTrue("No DeploymentFactory available", factories.length > 0); 87 factory = factories[0]; 88 } 89 90 92 public void testDeploymentManager() throws Exception 93 { 94 String uri = DeploymentManagerImpl.DEPLOYER_URI; 95 DeploymentManager manager = factory.getDeploymentManager(uri, null, null); 96 assertNotNull("No deployment manager", manager); 97 98 Target target = manager.getTargets()[0]; 99 assertEquals("JBoss JMX deployment target", target.getDescription()); 100 } 101 102 104 public void testDistributeWebApp() throws Exception 105 { 106 ProgressObject progress = jsr88Deployment("deployment-web.war"); 107 try 108 { 109 assertServletAccess("custom-context"); 110 } 111 finally 112 { 113 jsr88Undeploy(progress.getResultTargetModuleIDs()); 114 } 115 try 116 { 117 assertServletAccess("custom-context"); 118 fail("Test deployment not undeployed"); 119 } 120 catch (IOException e) 121 { 122 } 124 } 125 126 129 public void testDistributeEjbApp() throws Exception 130 { 131 ProgressObject progress = jsr88Deployment("deployment-ejb.jar"); 132 try 133 { 134 assertEjbEchoAccess(); 135 } 136 finally 137 { 138 jsr88Undeploy(progress.getResultTargetModuleIDs()); 139 } 140 try 141 { 142 assertEjbEchoAccess(); 143 fail("Test deployment not undeployed"); 144 } 145 catch (Exception e) 146 { 147 } 149 } 150 151 public void testDistributeEARApp() throws Exception 152 { 153 ProgressObject progress = jsr88Deployment("deployment-ear.ear"); 154 try 155 { 156 assertServletAccess("custom-context"); 157 assertEjbEchoAccess(); 158 } 159 finally 160 { 161 jsr88Undeploy(progress.getResultTargetModuleIDs()); 162 } 163 try 164 { 165 assertServletAccess("custom-context"); 166 fail("Test deployment not undeployed"); 167 } 168 catch (Exception e) 169 { 170 } 172 173 try 174 { 175 assertEjbEchoAccess(); 176 fail("Test deployment not undeployed"); 177 } 178 catch (Exception e) 179 { 180 } 182 } 183 184 188 public void testListStartStopModules() throws Exception 189 { 190 TargetModuleIDImpl child = null; 191 TargetModuleIDImpl parent = null; 192 193 DeploymentManager manager = factory.getDeploymentManager(DeploymentManagerImpl.DEPLOYER_URI, null, null); 195 Target [] targets = manager.getTargets(); 196 assertEquals(1, targets.length); 197 198 TargetModuleID [] modules = manager.getRunningModules(ModuleType.EAR, manager.getTargets()); 199 assertNull("no modules Available", modules); 200 201 ProgressObject parentProgress = jsr88Deployment("deployment-ear.ear"); 202 assertServletAccess("custom-context"); 203 assertEjbEchoAccess(); 204 205 modules = manager.getRunningModules(ModuleType.EAR, manager.getTargets()); 206 assertNotNull(modules); 207 assertEquals("one EAR module in the server", modules.length, 1); 208 209 parent = (TargetModuleIDImpl) modules[0]; 210 assertTrue("wrong state", parent.isRunning()); 211 assertEquals("wrong type", parent.getModuleType(), ModuleType.EAR); 212 assertEquals("EAR module have a jar and a war", parent.getChildTargetModuleID().length, 2); 213 214 child = (TargetModuleIDImpl) parent.getChildTargetModuleID()[0]; 215 assertTrue("wrong state", child.isRunning()); 216 assertTrue("wrong type", child.getModuleType().equals(ModuleType.EJB) || child.getModuleType().equals(ModuleType.WAR)); 217 assertEquals("child have no child", child.getChildTargetModuleID().length, 0); 218 219 child = (TargetModuleIDImpl) parent.getChildTargetModuleID()[1]; 220 assertTrue("wrong state", child.isRunning()); 221 assertTrue("wrong type " + child.getModuleType(), child.getModuleType().equals(ModuleType.EJB) || child.getModuleType().equals(ModuleType.WAR)); 222 assertEquals("child have no child", child.getChildTargetModuleID().length, 0); 223 224 parentProgress = manager.stop(new TargetModuleID [] { parent }); 225 waitForCompletion(parentProgress.getDeploymentStatus()); 226 227 modules = manager.getNonRunningModules(ModuleType.EAR, manager.getTargets()); 228 assertNotNull(modules); 229 assertEquals("one EAR module in the server", modules.length, 1); 230 231 parent = (TargetModuleIDImpl) modules[0]; 232 assertFalse("wrong state", parent.isRunning()); 233 assertEquals("wrong type", parent.getModuleType(), ModuleType.EAR); 234 assertEquals("EAR module have a jar and a war", parent.getChildTargetModuleID().length, 2); 235 236 parentProgress = manager.start(new TargetModuleID []{ parent }); 237 waitForCompletion(parentProgress.getDeploymentStatus()); 238 239 modules = manager.getRunningModules(ModuleType.EAR, manager.getTargets()); 240 assertNotNull(modules); 241 assertEquals("one EAR module in the server", modules.length, 1); 242 243 parent = (TargetModuleIDImpl) modules[0]; 244 assertTrue("wrong state", parent.isRunning()); 245 assertEquals("wrong type", parent.getModuleType(), ModuleType.EAR); 246 assertEquals("EAR module have a jar and a war", parent.getChildTargetModuleID().length, 2); 247 parentProgress = manager.undeploy(new TargetModuleID []{ parent }); 248 waitForCompletion(parentProgress.getDeploymentStatus()); 249 250 modules = manager.getAvailableModules(ModuleType.EAR, manager.getTargets()); 251 assertNull("EAR must not be available", modules); 252 253 try 254 { 255 assertServletAccess("custom-context"); 256 fail("Test deployment not undeployed"); 257 } 258 catch (Exception e) 259 { 260 } 262 263 try 264 { 265 assertEjbEchoAccess(); 266 fail("Test deployment not undeployed"); 267 } 268 catch (Exception e) 269 { 270 } 272 } 273 274 private void jsr88Undeploy(TargetModuleID [] resultTargetModuleIDs) throws Exception 275 { 276 DeploymentManager manager = factory.getDeploymentManager(DeploymentManagerImpl.DEPLOYER_URI, null, null); 278 Target [] targets = manager.getTargets(); 279 assertEquals(1, targets.length); 280 281 ProgressObject progress = manager.stop(resultTargetModuleIDs); 282 DeploymentStatus status = progress.getDeploymentStatus(); 283 waitForCompletion(status); 284 285 assertEquals(status.getMessage(), StateType.COMPLETED, status.getState()); 287 288 progress = manager.undeploy(resultTargetModuleIDs); 289 status = progress.getDeploymentStatus(); 290 waitForCompletion(status); 291 assertEquals(status.getMessage(), StateType.COMPLETED, status.getState()); 292 } 293 294 private ProgressObject jsr88Deployment(String module) throws Exception 295 { 296 DeploymentManager manager = factory.getDeploymentManager(DeploymentManagerImpl.DEPLOYER_URI, null, null); 298 Target [] targets = manager.getTargets(); 299 assertEquals(1, targets.length); 300 301 File deploymentPlan = createDeploymentPlan(module); 302 303 log.debug("module=" + module); 305 File moduleArchive = new File (new URI (getDeployURL(module).toString())); 306 assertTrue(moduleArchive.exists()); 307 308 ProgressObject progress = manager.distribute(targets, moduleArchive, deploymentPlan); 310 DeploymentStatus status = progress.getDeploymentStatus(); 311 waitForCompletion(status); 312 313 assertEquals(status.getMessage(), StateType.COMPLETED, status.getState()); 314 315 TargetModuleID [] moduleIDs = progress.getResultTargetModuleIDs(); 316 progress = manager.start(moduleIDs); 317 status = progress.getDeploymentStatus(); 318 waitForCompletion(status); 319 320 return progress; 321 } 322 323 private void assertEjbEchoAccess() throws NamingException , RemoteException , CreateException 324 { 325 InitialContext initial = new InitialContext (); 326 Object obj = initial.lookup("deployment/test/Echo"); 327 EchoHome home = (EchoHome) PortableRemoteObject.narrow(obj, EchoHome.class); 328 Echo echo = home.create(); 329 330 assertEquals("Wrong EJB return", "Hello!", echo.echo("Hello!")); 331 } 332 333 private void waitForCompletion(DeploymentStatus status) throws InterruptedException 334 { 335 while (StateType.RUNNING == status.getState()) 337 Thread.sleep(100); 338 } 339 340 private void assertServletAccess(String context) throws IOException 341 { 342 URL servletURL = new URL ("http://" + getServerHost() + ":8080/" + context); 344 BufferedReader br = new BufferedReader (new InputStreamReader (servletURL.openStream())); 345 String message = br.readLine(); 346 assertEquals("Hello World!", message); 347 } 348 349 private File createDeploymentPlan(String deploymentFile) throws Exception 350 { 351 String [] strs = null; 352 353 File deploymentPlan = File.createTempFile("deploymentplan", ".zip"); 355 deploymentPlan.deleteOnExit(); 356 357 String jbossFile = getJBossFile(deploymentFile); 358 String resourcedir = getResourceURL("deployment/" + jbossFile); 359 File jbossDescriptor = new File (new URI (resourcedir)); 360 assertTrue(jbossDescriptor.exists()); 361 362 JarOutputStream jos = new JarOutputStream (new FileOutputStream (deploymentPlan)); 363 JarUtils.addJarEntry(jos, "!/" + jbossFile, new FileInputStream (jbossDescriptor)); 364 365 DeploymentMetaData metaData = new DeploymentMetaData(deploymentFile); 367 368 strs = jbossFile.split("/"); 369 metaData.addEntry(deploymentFile, strs[strs.length - 1]); 370 371 String metaStr = metaData.toXMLString(); 373 374 JarUtils.addJarEntry(jos, DeploymentMetaData.ENTRY_NAME, new ByteArrayInputStream (metaStr.getBytes())); 375 jos.flush(); 376 jos.close(); 377 378 return deploymentPlan; 379 } 380 381 private String getJBossFile(String deploymentFile) 382 { 383 if (deploymentFile.endsWith(".war")) 384 return WAR_JBOSS_FILE; 385 else if (deploymentFile.endsWith(".jar")) 386 return JAR_JBOSS_FILE; 387 else if (deploymentFile.endsWith(".ear")) 388 return EAR_JBOSS_FILE; 389 else 390 fail("Wrong J2EE Module found..."); 391 throw new UnreachableStatementException(); 392 } 393 } 394 | Popular Tags |