/* * JBoss, Home of Professional Open Source * Copyright 2011 Red Hat Inc. and/or its affiliates and other contributors * as indicated by the @authors tag. All rights reserved. * See the copyright.txt in the distribution for a * full listing of individual contributors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * http://www.apache.org/licenses/LICENSE-2.0 * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.jboss.arquillian.container.glassfish.embedded_3_1.app; import java.io.File; import java.io.IOException; import java.net.URI; import java.util.Map; import javax.servlet.ServletException; import javax.servlet.ServletRegistration; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import junit.framework.Assert; import org.apache.catalina.Container; import org.glassfish.embeddable.GlassFish; import org.glassfish.embeddable.GlassFishRuntime; import org.glassfish.embeddable.web.Context; import org.glassfish.embeddable.web.VirtualServer; import org.glassfish.embeddable.web.WebContainer; import org.jboss.shrinkwrap.api.Archive; import org.jboss.shrinkwrap.api.ShrinkWrap; import org.jboss.shrinkwrap.api.exporter.ZipExporter; import org.jboss.shrinkwrap.api.spec.WebArchive; import org.junit.Test; import com.sun.enterprise.web.WebModule; /** * UndeployIssueTestCase * * @author Aslak Knutsen * @version $Revision: $ */ public class ContextIssueTestCase { @WebServlet(urlPatterns = "/*") public static class MyServlet extends HttpServlet { private static final long serialVersionUID = 1L; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.getWriter().write("_test_"); } } @Test public void shouldBeAbleToDeploy() throws Exception { GlassFish glassfish = GlassFishRuntime.bootstrap().newGlassFish(); try { glassfish.start(); WebArchive leakingDeployment = ShrinkWrap.create(WebArchive.class) .addClasses(MyServlet.class); glassfish.getDeployer().deploy( toURI(leakingDeployment), "--name", createDeploymentName(leakingDeployment.getName())); Assert.assertEquals(1, glassfish.getDeployer().getDeployedApplications().size()); WebContainer webContainer = glassfish.getService(WebContainer.class); Assert.assertEquals(1, webContainer.getVirtualServers().size()); for(VirtualServer virtualServer : webContainer.getVirtualServers()) { Assert.assertTrue(virtualServer instanceof com.sun.enterprise.web.VirtualServer); if(virtualServer instanceof com.sun.enterprise.web.VirtualServer) { Container container = ((com.sun.enterprise.web.VirtualServer)virtualServer).findChild("/" + createDeploymentName(leakingDeployment.getName())); Assert.assertNotNull(container); System.out.println(container); } // No Contexts found on virtual server Assert.assertEquals(1, virtualServer.getContexts().size()); for(Context context : virtualServer.getContexts()) { if(context instanceof WebModule) { WebModule webModule = (WebModule)context; for(Map.Entry servletRegistration : webModule.getServletRegistrations().entrySet()) { System.out.println(webModule.getContextPath() + ":" + servletRegistration.getKey()); } } } } glassfish.getDeployer().undeploy(createDeploymentName(leakingDeployment.getName())); } finally { glassfish.stop(); } } private String createDeploymentName(String archiveName) { String correctedName = archiveName; if(correctedName.startsWith("/")) { correctedName = correctedName.substring(1); } if(correctedName.indexOf(".") != -1) { correctedName = correctedName.substring(0, archiveName.lastIndexOf(".")-1); } return correctedName; } public static URI toURI(final Archive archive) { // create a random named temp file, then delete and use it as a directory for export try { File root = File.createTempFile("arquillian", archive.getName()); root.delete(); root.mkdirs(); File deployment = new File(root, archive.getName()); deployment.deleteOnExit(); archive.as(ZipExporter.class).exportTo(deployment, true); return deployment.toURI(); } catch (Exception e) { throw new RuntimeException("Could not export deployment to temp", e); } } }