KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > deployment > DeploymentTestCase


1 /*
2  * JBoss, Home of Professional Open Source
3  * Copyright 2005, JBoss Inc., and individual contributors as indicated
4  * by the @authors tag. See the copyright.txt in the distribution for a
5  * full listing of individual contributors.
6  *
7  * This is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as
9  * published by the Free Software Foundation; either version 2.1 of
10  * the License, or (at your option) any later version.
11  *
12  * This software is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this software; if not, write to the Free
19  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21  */

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 JavaDoc;
32 import javax.enterprise.deploy.shared.ModuleType JavaDoc;
33 import javax.enterprise.deploy.shared.StateType JavaDoc;
34 import javax.enterprise.deploy.shared.factories.DeploymentFactoryManager JavaDoc;
35 import javax.enterprise.deploy.spi.DeploymentManager JavaDoc;
36 import javax.enterprise.deploy.spi.Target JavaDoc;
37 import javax.enterprise.deploy.spi.TargetModuleID JavaDoc;
38 import javax.enterprise.deploy.spi.factories.DeploymentFactory JavaDoc;
39 import javax.enterprise.deploy.spi.status.DeploymentStatus JavaDoc;
40 import javax.enterprise.deploy.spi.status.ProgressObject JavaDoc;
41 import javax.naming.InitialContext JavaDoc;
42 import javax.naming.NamingException JavaDoc;
43 import javax.rmi.PortableRemoteObject JavaDoc;
44
45 import java.io.BufferedReader JavaDoc;
46 import java.io.ByteArrayInputStream JavaDoc;
47 import java.io.File JavaDoc;
48 import java.io.FileInputStream JavaDoc;
49 import java.io.FileOutputStream JavaDoc;
50 import java.io.IOException JavaDoc;
51 import java.io.InputStreamReader JavaDoc;
52 import java.net.URI JavaDoc;
53 import java.net.URL JavaDoc;
54 import java.rmi.RemoteException JavaDoc;
55 import java.util.jar.JarOutputStream JavaDoc;
56
57 /**
58  * Deployment API JSR-88 tests
59  *
60  * @author Thomas.Diesler@jboss.org
61  * @author Fabiano C. de Oliveira (JBAS-1995)
62  * @version $Revision: 44451 $
63  */

64 public class DeploymentTestCase extends JBossTestCase
65 {
66    private static final String JavaDoc WAR_JBOSS_FILE = "WEB-INF/jboss-web.xml";
67
68    private static final String JavaDoc JAR_JBOSS_FILE = "META-INF/jboss.xml";
69
70    private static final String JavaDoc EAR_JBOSS_FILE = "META-INF/jboss-app.xml";
71
72    private DeploymentFactory JavaDoc factory;
73
74    public DeploymentTestCase(String JavaDoc name)
75    {
76       super(name);
77    }
78
79    /** Get the DeploymentManager
80     */

81    protected void setUp() throws Exception JavaDoc
82    {
83       super.setUp();
84       DeploymentFactoryManager JavaDoc dfManager = DeploymentFactoryManager.getInstance();
85       DeploymentFactory JavaDoc[] factories = dfManager.getDeploymentFactories();
86       assertTrue("No DeploymentFactory available", factories.length > 0);
87       factory = factories[0];
88    }
89
90    /** Check DeploymentManager
91     */

92    public void testDeploymentManager() throws Exception JavaDoc
93    {
94       String JavaDoc uri = DeploymentManagerImpl.DEPLOYER_URI;
95       DeploymentManager JavaDoc manager = factory.getDeploymentManager(uri, null, null);
96       assertNotNull("No deployment manager", manager);
97
98       Target JavaDoc target = manager.getTargets()[0];
99       assertEquals("JBoss JMX deployment target", target.getDescription());
100    }
101
102    /** Distribute a web app
103     */

104    public void testDistributeWebApp() throws Exception JavaDoc
105    {
106       ProgressObject JavaDoc 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 JavaDoc e)
121       {
122          // ignore
123
}
124    }
125
126    /**
127     * Distribute a EJB app
128     */

129    public void testDistributeEjbApp() throws Exception JavaDoc
130    {
131       ProgressObject JavaDoc 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 JavaDoc e)
146       {
147          // ignore
148
}
149    }
150
151    public void testDistributeEARApp() throws Exception JavaDoc
152    {
153       ProgressObject JavaDoc 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 JavaDoc e)
169       {
170          // ignore
171
}
172
173       try
174       {
175          assertEjbEchoAccess();
176          fail("Test deployment not undeployed");
177       }
178       catch (Exception JavaDoc e)
179       {
180          // ignore
181
}
182    }
183
184    /**
185     *
186     * @throws Exception
187     */

188    public void testListStartStopModules() throws Exception JavaDoc
189    {
190       TargetModuleIDImpl child = null;
191       TargetModuleIDImpl parent = null;
192
193       // Get the deployment manager and the distribution targets
194
DeploymentManager JavaDoc manager = factory.getDeploymentManager(DeploymentManagerImpl.DEPLOYER_URI, null, null);
195       Target JavaDoc[] targets = manager.getTargets();
196       assertEquals(1, targets.length);
197
198       TargetModuleID JavaDoc[] modules = manager.getRunningModules(ModuleType.EAR, manager.getTargets());
199       assertNull("no modules Available", modules);
200
201       ProgressObject JavaDoc 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 JavaDoc[] { 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 JavaDoc[]{ 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 JavaDoc[]{ 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 JavaDoc e)
259       {
260          // ignore
261
}
262
263       try
264       {
265          assertEjbEchoAccess();
266          fail("Test deployment not undeployed");
267       }
268       catch (Exception JavaDoc e)
269       {
270          // ignore
271
}
272    }
273
274    private void jsr88Undeploy(TargetModuleID JavaDoc[] resultTargetModuleIDs) throws Exception JavaDoc
275    {
276       // Get the deployment manager and the distribution targets
277
DeploymentManager JavaDoc manager = factory.getDeploymentManager(DeploymentManagerImpl.DEPLOYER_URI, null, null);
278       Target JavaDoc[] targets = manager.getTargets();
279       assertEquals(1, targets.length);
280
281       ProgressObject JavaDoc progress = manager.stop(resultTargetModuleIDs);
282       DeploymentStatus JavaDoc status = progress.getDeploymentStatus();
283       waitForCompletion(status);
284
285       // Check the webapp is undeployed
286
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 JavaDoc jsr88Deployment(String JavaDoc module) throws Exception JavaDoc
295    {
296       // Get the deployment manager and the distribution targets
297
DeploymentManager JavaDoc manager = factory.getDeploymentManager(DeploymentManagerImpl.DEPLOYER_URI, null, null);
298       Target JavaDoc[] targets = manager.getTargets();
299       assertEquals(1, targets.length);
300
301       File JavaDoc deploymentPlan = createDeploymentPlan(module);
302
303       // Get a pointer to the plain web archive
304
log.debug("module=" + module);
305       File JavaDoc moduleArchive = new File JavaDoc(new URI JavaDoc(getDeployURL(module).toString()));
306       assertTrue(moduleArchive.exists());
307
308       // Deploy the test war
309
ProgressObject JavaDoc progress = manager.distribute(targets, moduleArchive, deploymentPlan);
310       DeploymentStatus JavaDoc status = progress.getDeploymentStatus();
311       waitForCompletion(status);
312
313       assertEquals(status.getMessage(), StateType.COMPLETED, status.getState());
314
315       TargetModuleID JavaDoc[] 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 JavaDoc, RemoteException JavaDoc, CreateException JavaDoc
324    {
325       InitialContext JavaDoc initial = new InitialContext JavaDoc();
326       Object JavaDoc 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 JavaDoc status) throws InterruptedException JavaDoc
334    {
335       // wait for the deployment to finish
336
while (StateType.RUNNING == status.getState())
337          Thread.sleep(100);
338    }
339
340    private void assertServletAccess(String JavaDoc context) throws IOException JavaDoc
341    {
342       // Check that we can access the servlet
343
URL JavaDoc servletURL = new URL JavaDoc("http://" + getServerHost() + ":8080/" + context);
344       BufferedReader JavaDoc br = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(servletURL.openStream()));
345       String JavaDoc message = br.readLine();
346       assertEquals("Hello World!", message);
347    }
348
349    private File JavaDoc createDeploymentPlan(String JavaDoc deploymentFile) throws Exception JavaDoc
350    {
351       String JavaDoc[] strs = null;
352
353       // Create temp file for deployment plan
354
File JavaDoc deploymentPlan = File.createTempFile("deploymentplan", ".zip");
355       deploymentPlan.deleteOnExit();
356
357       String JavaDoc jbossFile = getJBossFile(deploymentFile);
358       String JavaDoc resourcedir = getResourceURL("deployment/" + jbossFile);
359       File JavaDoc jbossDescriptor = new File JavaDoc(new URI JavaDoc(resourcedir));
360       assertTrue(jbossDescriptor.exists());
361
362       JarOutputStream JavaDoc jos = new JarOutputStream JavaDoc(new FileOutputStream JavaDoc(deploymentPlan));
363       JarUtils.addJarEntry(jos, "!/" + jbossFile, new FileInputStream JavaDoc(jbossDescriptor));
364
365       // Setup deployment plan meta data with propriatary descriptor
366
DeploymentMetaData metaData = new DeploymentMetaData(deploymentFile);
367
368       strs = jbossFile.split("/");
369       metaData.addEntry(deploymentFile, strs[strs.length - 1]);
370
371       // Add the meta data to the deployment plan
372
String JavaDoc metaStr = metaData.toXMLString();
373
374       JarUtils.addJarEntry(jos, DeploymentMetaData.ENTRY_NAME, new ByteArrayInputStream JavaDoc(metaStr.getBytes()));
375       jos.flush();
376       jos.close();
377
378       return deploymentPlan;
379    }
380
381    private String JavaDoc getJBossFile(String JavaDoc 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