-
-
Save dansiviter/fbef914311b7fecf9a32f3b418725fea to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package acme; | |
import static org.jboss.shrinkwrap.api.ShrinkWrap.create; | |
import static org.junit.Assert.assertEquals; | |
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
import java.net.HttpURLConnection; | |
import java.net.URI; | |
import java.util.Set; | |
import javax.annotation.Resource; | |
import javax.enterprise.concurrent.ManagedExecutorService; | |
import javax.servlet.ServletContainerInitializer; | |
import javax.servlet.ServletContext; | |
import javax.servlet.ServletException; | |
import javax.servlet.annotation.WebServlet; | |
import javax.servlet.http.HttpServlet; | |
import javax.servlet.http.HttpServletRequest; | |
import javax.servlet.http.HttpServletResponse; | |
import org.jboss.arquillian.container.test.api.Deployment; | |
import org.jboss.arquillian.container.test.api.RunAsClient; | |
import org.jboss.arquillian.junit.Arquillian; | |
import org.jboss.arquillian.test.api.ArquillianResource; | |
import org.jboss.shrinkwrap.api.spec.WebArchive; | |
import org.junit.Test; | |
import org.junit.runner.RunWith; | |
import org.wildfly.swarm.spi.api.JARArchive; | |
@RunWith(Arquillian.class) | |
public class ServletTest { | |
@ArquillianResource | |
public URI deploymentUri; | |
private HttpURLConnection openConnection(String path) throws IOException { | |
final URI uri = URI.create(deploymentUri.toString() + path); | |
final HttpURLConnection conn = (HttpURLConnection) uri.toURL().openConnection(); | |
conn.setRequestMethod("GET"); | |
return conn; | |
} | |
@Test | |
@RunAsClient | |
public void testDynamic() throws IOException { | |
final HttpURLConnection conn = openConnection("dynamic"); | |
conn.setRequestMethod("GET"); | |
try (BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()))) { | |
assertEquals("Yay!", reader.readLine()); | |
} | |
} | |
@Test | |
@RunAsClient | |
public void testStatic() throws IOException { | |
final HttpURLConnection conn = openConnection("static"); | |
conn.setRequestMethod("GET"); | |
try (BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()))) { | |
assertEquals("Yay!", reader.readLine()); | |
} | |
} | |
@Deployment | |
public static WebArchive createWebArchive() { | |
return create(WebArchive.class) | |
.addClasses(StaticServlet.class) | |
.addAsLibrary(create(JARArchive.class) | |
.addAsServiceProvider(ServletContainerInitializer.class, Initialiser.class) | |
.addClasses(DynamicServlet.class)); | |
} | |
public static class Initialiser implements ServletContainerInitializer { | |
@Override | |
public void onStartup(Set<Class<?>> c, ServletContext ctx) throws ServletException { | |
ctx.addServlet("acme", DynamicServlet.class).addMapping("/dynamic"); | |
} | |
} | |
public static class DynamicServlet extends HttpServlet { | |
private static final long serialVersionUID = 1L; | |
@Resource | |
private ManagedExecutorService executor; | |
@Override | |
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { | |
resp.setContentType("text/plain"); | |
resp.getWriter().println(this.executor != null ? "Yay!" : "Oh no!"); | |
} | |
} | |
@WebServlet("/static") | |
public static class StaticServlet extends DynamicServlet { | |
private static final long serialVersionUID = 1L; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment