PageRenderTime 37ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/testsuite/smoke/src/test/java/org/jboss/as/test/embedded/deployment/EnterpriseDeploymentTestCase.java

https://github.com/tomathome/jboss-as
Java | 339 lines | 258 code | 46 blank | 35 comment | 12 complexity | bc42853b4c592b375cb62256b6443319 MD5 | raw file
  1. /*
  2. * JBoss, Home of Professional Open Source.
  3. * Copyright 2008, Red Hat Middleware LLC, and individual contributors
  4. * as indicated by the @author tags. See the copyright.txt file in the
  5. * distribution for a 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.as.test.embedded.deployment;
  23. import static junit.framework.Assert.assertEquals;
  24. import static junit.framework.Assert.assertNotNull;
  25. import static junit.framework.Assert.assertTrue;
  26. import static junit.framework.Assert.fail;
  27. import static org.junit.Assert.assertNull;
  28. import java.io.BufferedReader;
  29. import java.io.ByteArrayInputStream;
  30. import java.io.ByteArrayOutputStream;
  31. import java.io.File;
  32. import java.io.FileInputStream;
  33. import java.io.IOException;
  34. import java.io.InputStream;
  35. import java.io.InputStreamReader;
  36. import java.net.URL;
  37. import java.util.concurrent.CountDownLatch;
  38. import java.util.concurrent.TimeUnit;
  39. import java.util.jar.JarOutputStream;
  40. import javax.enterprise.deploy.shared.ModuleType;
  41. import javax.enterprise.deploy.shared.StateType;
  42. import javax.enterprise.deploy.shared.factories.DeploymentFactoryManager;
  43. import javax.enterprise.deploy.spi.DeploymentManager;
  44. import javax.enterprise.deploy.spi.Target;
  45. import javax.enterprise.deploy.spi.TargetModuleID;
  46. import javax.enterprise.deploy.spi.factories.DeploymentFactory;
  47. import javax.enterprise.deploy.spi.status.DeploymentStatus;
  48. import javax.enterprise.deploy.spi.status.ProgressEvent;
  49. import javax.enterprise.deploy.spi.status.ProgressListener;
  50. import javax.enterprise.deploy.spi.status.ProgressObject;
  51. import org.jboss.arquillian.container.test.api.Deployment;
  52. import org.jboss.arquillian.container.test.api.RunAsClient;
  53. import org.jboss.arquillian.junit.Arquillian;
  54. import org.jboss.as.ee.deployment.spi.DeploymentManagerImpl;
  55. import org.jboss.as.ee.deployment.spi.DeploymentMetaData;
  56. import org.jboss.as.ee.deployment.spi.JarUtils;
  57. import org.jboss.as.ee.deployment.spi.factories.DeploymentFactoryImpl;
  58. import org.jboss.shrinkwrap.api.Archive;
  59. import org.jboss.shrinkwrap.api.ShrinkWrap;
  60. import org.jboss.shrinkwrap.api.exporter.ZipExporter;
  61. import org.jboss.shrinkwrap.api.spec.EnterpriseArchive;
  62. import org.jboss.shrinkwrap.api.spec.JavaArchive;
  63. import org.jboss.shrinkwrap.api.spec.WebArchive;
  64. import org.jboss.util.UnreachableStatementException;
  65. import org.junit.Ignore;
  66. import org.junit.Test;
  67. import org.junit.runner.RunWith;
  68. /**
  69. * Deployment API JSR-88 tests
  70. *
  71. * @author Thomas.Diesler@jboss.com
  72. * @since 02-Aug-2011
  73. */
  74. @RunWith(Arquillian.class)
  75. @RunAsClient
  76. @Ignore
  77. public class EnterpriseDeploymentTestCase {
  78. private static final String WAR_JBOSS_FILE = "WEB-INF/jboss-web.xml";
  79. private static final String JAR_JBOSS_FILE = "META-INF/jboss.xml";
  80. private static final String EAR_JBOSS_FILE = "META-INF/jboss-app.xml";
  81. private DeploymentManager deploymentManager;
  82. @Deployment
  83. public static Archive<?> createDeployment(){
  84. JavaArchive archive = ShrinkWrap.create(JavaArchive.class, "deployment-test.jar");
  85. return archive;
  86. }
  87. @Test
  88. public void testDeploymentManager() throws Exception {
  89. DeploymentManager manager = getDeploymentManager();
  90. assertNotNull("DeploymentManager not null", manager);
  91. Target target = manager.getTargets()[0];
  92. assertEquals("ServerDeploymentManager target", target.getDescription());
  93. }
  94. @Test
  95. public void testDistributeWebApp() throws Exception {
  96. ProgressObject progress = jsr88Deploy(getWebArchive());
  97. TargetModuleID[] targetModules = progress.getResultTargetModuleIDs();
  98. try {
  99. DeploymentStatus state = progress.getDeploymentStatus();
  100. assertEquals(StateType.COMPLETED, state.getState());
  101. assertServletAccess("custom-context");
  102. } finally {
  103. jsr88Undeploy(targetModules);
  104. }
  105. try {
  106. assertServletAccess("custom-context");
  107. fail("Test deployment not undeployed");
  108. } catch (IOException e) {
  109. // ignore
  110. }
  111. }
  112. @Test
  113. @Ignore
  114. public void testDistributeBadWar() throws Exception {
  115. ProgressObject progress = jsr88Deploy(getBadWebArchive());
  116. TargetModuleID[] targetModules = progress.getResultTargetModuleIDs();
  117. try {
  118. DeploymentStatus state = progress.getDeploymentStatus();
  119. assertEquals(StateType.FAILED, state.getState());
  120. } finally {
  121. jsr88Undeploy(targetModules);
  122. }
  123. }
  124. @Test
  125. public void testDistributeEjbApp() throws Exception {
  126. ProgressObject progress = jsr88Deploy(getEjbArchive());
  127. TargetModuleID[] targetModules = progress.getResultTargetModuleIDs();
  128. try {
  129. DeploymentStatus state = progress.getDeploymentStatus();
  130. assertEquals(StateType.COMPLETED, state.getState());
  131. } finally {
  132. jsr88Undeploy(targetModules);
  133. }
  134. }
  135. @Test
  136. public void testDistributeEARApp() throws Exception {
  137. ProgressObject progress = jsr88Deploy(getEarArchive());
  138. TargetModuleID[] targetModules = progress.getResultTargetModuleIDs();
  139. try {
  140. DeploymentStatus state = progress.getDeploymentStatus();
  141. assertEquals(StateType.COMPLETED, state.getState());
  142. assertServletAccess("custom-context");
  143. } finally {
  144. jsr88Undeploy(targetModules);
  145. }
  146. try {
  147. assertServletAccess("custom-context");
  148. fail("Test deployment not undeployed");
  149. } catch (Exception e) {
  150. // ignore
  151. }
  152. }
  153. @Test
  154. public void testListAvailableModules() throws Exception {
  155. DeploymentManager manager = getDeploymentManager();
  156. Target[] targets = manager.getTargets();
  157. TargetModuleID[] availableModules = manager.getAvailableModules(ModuleType.EAR, targets);
  158. assertNull(availableModules);
  159. ProgressObject progress = jsr88Deploy(getEarArchive());
  160. TargetModuleID[] targetModules = progress.getResultTargetModuleIDs();
  161. try {
  162. availableModules = manager.getAvailableModules(ModuleType.EAR, targets);
  163. assertNotNull(availableModules);
  164. assertEquals(1, availableModules.length);
  165. TargetModuleID targetModuleID = availableModules[0];
  166. String moduleID = targetModuleID.getModuleID();
  167. assertTrue("Ends with deployment-app.ear", moduleID.endsWith("deployment-app.ear"));
  168. // [TODO] verify child modules
  169. } finally {
  170. jsr88Undeploy(targetModules);
  171. }
  172. }
  173. private DeploymentManager getDeploymentManager() throws Exception {
  174. if (deploymentManager == null) {
  175. DeploymentFactoryImpl.register();
  176. DeploymentFactoryManager dfManager = DeploymentFactoryManager.getInstance();
  177. DeploymentFactory[] factories = dfManager.getDeploymentFactories();
  178. assertTrue("DeploymentFactory available", factories.length > 0);
  179. String mgrURI = DeploymentManagerImpl.DEPLOYER_URI + "?targetType=as7";
  180. deploymentManager = factories[0].getDeploymentManager(mgrURI, null, null);
  181. }
  182. return deploymentManager;
  183. }
  184. private ProgressObject jsr88Deploy(Archive<?> archive) throws Exception {
  185. // Get the deployment manager and the distribution targets
  186. DeploymentManager manager = getDeploymentManager();
  187. Target[] targets = manager.getTargets();
  188. assertEquals(1, targets.length);
  189. InputStream deploymentPlan = createDeploymentPlan(archive.getName());
  190. // Deploy the test archive
  191. InputStream inputStream = archive.as(ZipExporter.class).exportAsInputStream();
  192. ProgressObject progress = manager.distribute(targets, inputStream, deploymentPlan);
  193. awaitCompletion(progress, 5000);
  194. progress = manager.start(progress.getResultTargetModuleIDs());
  195. awaitCompletion(progress, 5000);
  196. return progress;
  197. }
  198. private ProgressObject jsr88Undeploy(TargetModuleID[] resultTargetModuleIDs) throws Exception {
  199. DeploymentManager manager = getDeploymentManager();
  200. Target[] targets = manager.getTargets();
  201. assertEquals(1, targets.length);
  202. ProgressObject progress = manager.stop(resultTargetModuleIDs);
  203. awaitCompletion(progress, 5000);
  204. progress = manager.undeploy(resultTargetModuleIDs);
  205. awaitCompletion(progress, 5000);
  206. return progress;
  207. }
  208. private void awaitCompletion(ProgressObject progress, long timeout) throws InterruptedException {
  209. final CountDownLatch latch = new CountDownLatch(1);
  210. progress.addProgressListener(new ProgressListener() {
  211. public void handleProgressEvent(ProgressEvent event) {
  212. DeploymentStatus status = event.getDeploymentStatus();
  213. if (status.isCompleted()) {
  214. latch.countDown();
  215. }
  216. }
  217. });
  218. if (latch.await(timeout, TimeUnit.MILLISECONDS) == false)
  219. throw new IllegalStateException("Deployment not completed: " + progress);
  220. }
  221. private void assertServletAccess(String context) throws IOException {
  222. // Check that we can access the servlet
  223. URL servletURL = new URL("http://localhost:8080/" + context);
  224. BufferedReader br = new BufferedReader(new InputStreamReader(servletURL.openStream()));
  225. String message = br.readLine();
  226. assertEquals("Hello World!", message);
  227. }
  228. private InputStream createDeploymentPlan(String deploymentFile) throws Exception {
  229. String[] strs = null;
  230. String jbossFile = getJBossFile(deploymentFile);
  231. File jbossDescriptor = getResourceFile("deployment/" + jbossFile);
  232. assertTrue(jbossDescriptor.exists());
  233. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  234. JarOutputStream jos = new JarOutputStream(baos);
  235. JarUtils.addJarEntry(jos, "!/" + jbossFile, new FileInputStream(jbossDescriptor));
  236. // Setup deployment plan meta data with propriatary descriptor
  237. DeploymentMetaData metaData = new DeploymentMetaData(deploymentFile);
  238. strs = jbossFile.split("/");
  239. metaData.addEntry(deploymentFile, strs[strs.length - 1]);
  240. // Add the meta data to the deployment plan
  241. String metaStr = metaData.toXMLString();
  242. JarUtils.addJarEntry(jos, DeploymentMetaData.ENTRY_NAME, new ByteArrayInputStream(metaStr.getBytes()));
  243. jos.flush();
  244. jos.close();
  245. return new ByteArrayInputStream(baos.toByteArray());
  246. }
  247. private String getJBossFile(String deploymentFile) {
  248. if (deploymentFile.endsWith(".war"))
  249. return WAR_JBOSS_FILE;
  250. else if (deploymentFile.endsWith(".jar"))
  251. return JAR_JBOSS_FILE;
  252. else if (deploymentFile.endsWith(".ear"))
  253. return EAR_JBOSS_FILE;
  254. else
  255. fail("Wrong J2EE Module found...");
  256. throw new UnreachableStatementException();
  257. }
  258. private File getResourceFile(String resource) {
  259. File file = new File(resource);
  260. if (file.exists())
  261. return file;
  262. String testResourcesDir = "target/test-classes";
  263. file = new File(testResourcesDir + "/" + resource);
  264. if (file.exists())
  265. return file;
  266. throw new IllegalArgumentException("Cannot obtain '" + testResourcesDir + "/" + resource + "'");
  267. }
  268. private Archive<?> getWebArchive() {
  269. WebArchive archive = ShrinkWrap.create(WebArchive.class, "deployment-web.war");
  270. archive.addClasses(SampleServlet.class);
  271. archive.setWebXML("deployment/WEB-INF/web.xml");
  272. return archive;
  273. }
  274. private Archive<?> getBadWebArchive() {
  275. WebArchive archive = ShrinkWrap.create(WebArchive.class, "deployment-bad-web.war");
  276. archive.addClasses(SampleServlet.class);
  277. archive.setWebXML("deployment/WEB-INF/badweb.xml");
  278. return archive;
  279. }
  280. private Archive<?> getEjbArchive() {
  281. JavaArchive archive = ShrinkWrap.create(JavaArchive.class, "deployment-ejb.jar");
  282. archive.addClasses(Echo.class, EchoHome.class, EchoBean.class);
  283. archive.addAsManifestResource("deployment/META-INF/ejb-jar.xml", "ejb-jar.xml");
  284. return archive;
  285. }
  286. private Archive<?> getEarArchive() {
  287. EnterpriseArchive archive = ShrinkWrap.create(EnterpriseArchive.class, "deployment-app.ear");
  288. archive.setApplicationXML("deployment/META-INF/application.xml");
  289. archive.add(getWebArchive(), "/", ZipExporter.class);
  290. archive.add(getEjbArchive(), "/", ZipExporter.class);
  291. return archive;
  292. }
  293. }