/* * 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.net.URI; import javax.ejb.EJB; import javax.ejb.Stateless; import junit.framework.Assert; import org.glassfish.embeddable.GlassFish; import org.glassfish.embeddable.GlassFishRuntime; 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.EnterpriseArchive; import org.jboss.shrinkwrap.api.spec.JavaArchive; import org.jboss.shrinkwrap.api.spec.WebArchive; import org.junit.Test; /** * UndeployIssueTestCase * * @author Aslak Knutsen * @version $Revision: $ */ public class UndeployIssueTestCase { @Stateless public static class MyEJB { public String getName() { return "test"; } } public static class MyClient { @EJB private MyEJB ejb; public String sayMyName() { return ejb.getName(); } } @Test public void shouldBeAbleToDeploy() throws Exception { GlassFish glassfish = GlassFishRuntime.bootstrap().newGlassFish(); try { glassfish.start(); WebArchive leakingDeployment = ShrinkWrap.create(WebArchive.class) .addClasses(MyEJB.class); EnterpriseArchive failingDeployment = ShrinkWrap.create(EnterpriseArchive.class) .addAsModule( ShrinkWrap.create(JavaArchive.class) .addClasses(MyEJB.class) ) .addAsModule( ShrinkWrap.create(WebArchive.class) .addClass(MyClient.class)); glassfish.getDeployer().deploy( toURI(leakingDeployment), "--name", createDeploymentName(leakingDeployment.getName())); Assert.assertEquals(1, glassfish.getDeployer().getDeployedApplications().size()); glassfish.getDeployer().undeploy(createDeploymentName(leakingDeployment.getName())); glassfish.getDeployer().deploy( toURI(failingDeployment), "--name", createDeploymentName(failingDeployment.getName())); Assert.assertEquals(1, glassfish.getDeployer().getDeployedApplications().size()); glassfish.getDeployer().undeploy(createDeploymentName(failingDeployment.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); } } }