001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.test.deployment;
023:
024: import org.jboss.deployment.spi.DeploymentManagerImpl;
025: import org.jboss.deployment.spi.DeploymentMetaData;
026: import org.jboss.deployment.spi.JarUtils;
027: import org.jboss.deployment.spi.TargetModuleIDImpl;
028: import org.jboss.test.JBossTestCase;
029: import org.jboss.util.UnreachableStatementException;
030:
031: import javax.ejb.CreateException;
032: import javax.enterprise.deploy.shared.ModuleType;
033: import javax.enterprise.deploy.shared.StateType;
034: import javax.enterprise.deploy.shared.factories.DeploymentFactoryManager;
035: import javax.enterprise.deploy.spi.DeploymentManager;
036: import javax.enterprise.deploy.spi.Target;
037: import javax.enterprise.deploy.spi.TargetModuleID;
038: import javax.enterprise.deploy.spi.factories.DeploymentFactory;
039: import javax.enterprise.deploy.spi.status.DeploymentStatus;
040: import javax.enterprise.deploy.spi.status.ProgressObject;
041: import javax.naming.InitialContext;
042: import javax.naming.NamingException;
043: import javax.rmi.PortableRemoteObject;
044:
045: import java.io.BufferedReader;
046: import java.io.ByteArrayInputStream;
047: import java.io.File;
048: import java.io.FileInputStream;
049: import java.io.FileOutputStream;
050: import java.io.IOException;
051: import java.io.InputStreamReader;
052: import java.net.URI;
053: import java.net.URL;
054: import java.rmi.RemoteException;
055: import java.util.jar.JarOutputStream;
056:
057: /**
058: * Deployment API JSR-88 tests
059: *
060: * @author Thomas.Diesler@jboss.org
061: * @author Fabiano C. de Oliveira (JBAS-1995)
062: * @version $Revision: 57211 $
063: */
064: public class DeploymentTestCase extends JBossTestCase {
065: private static final String WAR_JBOSS_FILE = "WEB-INF/jboss-web.xml";
066:
067: private static final String JAR_JBOSS_FILE = "META-INF/jboss.xml";
068:
069: private static final String EAR_JBOSS_FILE = "META-INF/jboss-app.xml";
070:
071: private DeploymentFactory factory;
072:
073: public DeploymentTestCase(String name) {
074: super (name);
075: }
076:
077: /** Get the DeploymentManager
078: */
079: protected void setUp() throws Exception {
080: super .setUp();
081: DeploymentFactoryManager dfManager = DeploymentFactoryManager
082: .getInstance();
083: DeploymentFactory[] factories = dfManager
084: .getDeploymentFactories();
085: assertTrue("No DeploymentFactory available",
086: factories.length > 0);
087: factory = factories[0];
088: }
089:
090: /** Check DeploymentManager
091: */
092: public void testDeploymentManager() throws Exception {
093: String uri = DeploymentManagerImpl.DEPLOYER_URI;
094: DeploymentManager manager = factory.getDeploymentManager(uri,
095: null, null);
096: assertNotNull("No deployment manager", manager);
097:
098: Target target = manager.getTargets()[0];
099: assertEquals("JBoss JMX deployment target", target
100: .getDescription());
101: }
102:
103: /** Distribute a web app
104: */
105: public void testDistributeWebApp() throws Exception {
106: ProgressObject progress = jsr88Deployment("deployment-web.war");
107: try {
108: assertServletAccess("custom-context");
109: } finally {
110: jsr88Undeploy(progress.getResultTargetModuleIDs());
111: }
112: try {
113: assertServletAccess("custom-context");
114: fail("Test deployment not undeployed");
115: } catch (IOException e) {
116: // ignore
117: }
118: }
119:
120: /**
121: * Distribute a EJB app
122: */
123: public void testDistributeEjbApp() throws Exception {
124: ProgressObject progress = jsr88Deployment("deployment-ejb.jar");
125: try {
126: assertEjbEchoAccess();
127: } finally {
128: jsr88Undeploy(progress.getResultTargetModuleIDs());
129: }
130: try {
131: assertEjbEchoAccess();
132: fail("Test deployment not undeployed");
133: } catch (Exception e) {
134: // ignore
135: }
136: }
137:
138: public void testDistributeEARApp() throws Exception {
139: ProgressObject progress = jsr88Deployment("deployment-ear.ear");
140: try {
141: assertServletAccess("custom-context");
142: assertEjbEchoAccess();
143: } finally {
144: jsr88Undeploy(progress.getResultTargetModuleIDs());
145: }
146: try {
147: assertServletAccess("custom-context");
148: fail("Test deployment not undeployed");
149: } catch (Exception e) {
150: // ignore
151: }
152:
153: try {
154: assertEjbEchoAccess();
155: fail("Test deployment not undeployed");
156: } catch (Exception e) {
157: // ignore
158: }
159: }
160:
161: /**
162: *
163: * @throws Exception
164: */
165: public void testListStartStopModules() throws Exception {
166: TargetModuleIDImpl child = null;
167: TargetModuleIDImpl parent = null;
168:
169: // Get the deployment manager and the distribution targets
170: DeploymentManager manager = factory.getDeploymentManager(
171: DeploymentManagerImpl.DEPLOYER_URI, null, null);
172: Target[] targets = manager.getTargets();
173: assertEquals(1, targets.length);
174:
175: TargetModuleID[] modules = manager.getRunningModules(
176: ModuleType.EAR, manager.getTargets());
177: assertNull("no modules Available", modules);
178:
179: ProgressObject parentProgress = jsr88Deployment("deployment-ear.ear");
180: assertServletAccess("custom-context");
181: assertEjbEchoAccess();
182:
183: modules = manager.getRunningModules(ModuleType.EAR, manager
184: .getTargets());
185: assertNotNull(modules);
186: assertEquals("one EAR module in the server", modules.length, 1);
187:
188: parent = (TargetModuleIDImpl) modules[0];
189: assertTrue("wrong state", parent.isRunning());
190: assertEquals("wrong type", parent.getModuleType(),
191: ModuleType.EAR);
192: assertEquals("EAR module have a jar and a war", parent
193: .getChildTargetModuleID().length, 2);
194:
195: child = (TargetModuleIDImpl) parent.getChildTargetModuleID()[0];
196: assertTrue("wrong state", child.isRunning());
197: assertTrue("wrong type", child.getModuleType().equals(
198: ModuleType.EJB)
199: || child.getModuleType().equals(ModuleType.WAR));
200: assertEquals("child have no child", child
201: .getChildTargetModuleID().length, 0);
202:
203: child = (TargetModuleIDImpl) parent.getChildTargetModuleID()[1];
204: assertTrue("wrong state", child.isRunning());
205: assertTrue("wrong type " + child.getModuleType(), child
206: .getModuleType().equals(ModuleType.EJB)
207: || child.getModuleType().equals(ModuleType.WAR));
208: assertEquals("child have no child", child
209: .getChildTargetModuleID().length, 0);
210:
211: parentProgress = manager.stop(new TargetModuleID[] { parent });
212: waitForCompletion(parentProgress.getDeploymentStatus());
213:
214: modules = manager.getNonRunningModules(ModuleType.EAR, manager
215: .getTargets());
216: assertNotNull(modules);
217: assertEquals("one EAR module in the server", modules.length, 1);
218:
219: parent = (TargetModuleIDImpl) modules[0];
220: assertFalse("wrong state", parent.isRunning());
221: assertEquals("wrong type", parent.getModuleType(),
222: ModuleType.EAR);
223: assertEquals("EAR module have a jar and a war", parent
224: .getChildTargetModuleID().length, 2);
225:
226: parentProgress = manager.start(new TargetModuleID[] { parent });
227: waitForCompletion(parentProgress.getDeploymentStatus());
228:
229: modules = manager.getRunningModules(ModuleType.EAR, manager
230: .getTargets());
231: assertNotNull(modules);
232: assertEquals("one EAR module in the server", modules.length, 1);
233:
234: parent = (TargetModuleIDImpl) modules[0];
235: assertTrue("wrong state", parent.isRunning());
236: assertEquals("wrong type", parent.getModuleType(),
237: ModuleType.EAR);
238: assertEquals("EAR module have a jar and a war", parent
239: .getChildTargetModuleID().length, 2);
240: parentProgress = manager
241: .undeploy(new TargetModuleID[] { parent });
242: waitForCompletion(parentProgress.getDeploymentStatus());
243:
244: modules = manager.getAvailableModules(ModuleType.EAR, manager
245: .getTargets());
246: assertNull("EAR must not be available", modules);
247:
248: try {
249: assertServletAccess("custom-context");
250: fail("Test deployment not undeployed");
251: } catch (Exception e) {
252: // ignore
253: }
254:
255: try {
256: assertEjbEchoAccess();
257: fail("Test deployment not undeployed");
258: } catch (Exception e) {
259: // ignore
260: }
261: }
262:
263: private void jsr88Undeploy(TargetModuleID[] resultTargetModuleIDs)
264: throws Exception {
265: // Get the deployment manager and the distribution targets
266: DeploymentManager manager = factory.getDeploymentManager(
267: DeploymentManagerImpl.DEPLOYER_URI, null, null);
268: Target[] targets = manager.getTargets();
269: assertEquals(1, targets.length);
270:
271: ProgressObject progress = manager.stop(resultTargetModuleIDs);
272: DeploymentStatus status = progress.getDeploymentStatus();
273: waitForCompletion(status);
274:
275: // Check the webapp is undeployed
276: assertEquals(status.getMessage(), StateType.COMPLETED, status
277: .getState());
278:
279: progress = manager.undeploy(resultTargetModuleIDs);
280: status = progress.getDeploymentStatus();
281: waitForCompletion(status);
282: assertEquals(status.getMessage(), StateType.COMPLETED, status
283: .getState());
284: }
285:
286: private ProgressObject jsr88Deployment(String module)
287: throws Exception {
288: // Get the deployment manager and the distribution targets
289: DeploymentManager manager = factory.getDeploymentManager(
290: DeploymentManagerImpl.DEPLOYER_URI, null, null);
291: Target[] targets = manager.getTargets();
292: assertEquals(1, targets.length);
293:
294: File deploymentPlan = createDeploymentPlan(module);
295:
296: // Get a pointer to the plain web archive
297: log.debug("module=" + module);
298: File moduleArchive = new File(new URI(getDeployURL(module)
299: .toString()));
300: assertTrue(moduleArchive.exists());
301:
302: // Deploy the test war
303: ProgressObject progress = manager.distribute(targets,
304: moduleArchive, deploymentPlan);
305: DeploymentStatus status = progress.getDeploymentStatus();
306: waitForCompletion(status);
307:
308: assertEquals(status.getMessage(), StateType.COMPLETED, status
309: .getState());
310:
311: TargetModuleID[] moduleIDs = progress
312: .getResultTargetModuleIDs();
313: progress = manager.start(moduleIDs);
314: status = progress.getDeploymentStatus();
315: waitForCompletion(status);
316:
317: return progress;
318: }
319:
320: private void assertEjbEchoAccess() throws NamingException,
321: RemoteException, CreateException {
322: InitialContext initial = new InitialContext();
323: Object obj = initial.lookup("deployment/test/Echo");
324: EchoHome home = (EchoHome) PortableRemoteObject.narrow(obj,
325: EchoHome.class);
326: Echo echo = home.create();
327:
328: assertEquals("Wrong EJB return", "Hello!", echo.echo("Hello!"));
329: }
330:
331: private void waitForCompletion(DeploymentStatus status)
332: throws InterruptedException {
333: // wait for the deployment to finish
334: while (StateType.RUNNING == status.getState())
335: Thread.sleep(100);
336: }
337:
338: private void assertServletAccess(String context) throws IOException {
339: // Check that we can access the servlet
340: URL servletURL = new URL("http://" + getServerHost() + ":8080/"
341: + context);
342: BufferedReader br = new BufferedReader(new InputStreamReader(
343: servletURL.openStream()));
344: String message = br.readLine();
345: assertEquals("Hello World!", message);
346: }
347:
348: private File createDeploymentPlan(String deploymentFile)
349: throws Exception {
350: String[] strs = null;
351:
352: // Create temp file for deployment plan
353: File deploymentPlan = File.createTempFile("deploymentplan",
354: ".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(
363: deploymentPlan));
364: JarUtils.addJarEntry(jos, "!/" + jbossFile,
365: new FileInputStream(jbossDescriptor));
366:
367: // Setup deployment plan meta data with propriatary descriptor
368: DeploymentMetaData metaData = new DeploymentMetaData(
369: deploymentFile);
370:
371: strs = jbossFile.split("/");
372: metaData.addEntry(deploymentFile, strs[strs.length - 1]);
373:
374: // Add the meta data to the deployment plan
375: String metaStr = metaData.toXMLString();
376:
377: JarUtils.addJarEntry(jos, DeploymentMetaData.ENTRY_NAME,
378: new ByteArrayInputStream(metaStr.getBytes()));
379: jos.flush();
380: jos.close();
381:
382: return deploymentPlan;
383: }
384:
385: private String getJBossFile(String deploymentFile) {
386: if (deploymentFile.endsWith(".war"))
387: return WAR_JBOSS_FILE;
388: else if (deploymentFile.endsWith(".jar"))
389: return JAR_JBOSS_FILE;
390: else if (deploymentFile.endsWith(".ear"))
391: return EAR_JBOSS_FILE;
392: else
393: fail("Wrong J2EE Module found...");
394: throw new UnreachableStatementException();
395: }
396: }
|