Arquillian Deployable Container for Undertow
Java
Switch branches/tags
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
.github
shrinkwrap-container-undertow
undertow-embedded
.editorconfig
.gitignore
LICENSE
README.adoc
pom.xml

README.adoc

Arquillian-Container-Undertow

Undertow

Undertow is a flexible performant web server written in java, providing both blocking and non-blocking API’s based on NIO.

Undertow has a composition based architecture that allows you to build a web server by combining small single purpose handlers. The gives you the flexibility to choose between a full Java EE servlet 3.1 container, or a low level non-blocking handler, to anything in between.

Undertow is sponsored by JBoss and is the default web server in the Wildfly Application Server.

Writing tests for Undertow

Arquillian-Container-Undertow as every Arquillian Container Extension it take cares of you of starting, stopping and deploying the application. Also provides an Shrinkwrap resolver for creating the Undertow deployment file.

To simplify the development of tests on Undertow we have created two Shrinkwrap resolvers:

  • Embedded Servlet Deployment

  • Non-blocking handler

Embedded Servlet Deployment

When you want to deploy a servlet/s inside Undertow, you must create a DeploymentInfo class and provide all the required information. For this reason Arquillian-Container-Undertow provides a Shrinkwrap resolver named UndertowWebArchive.

@Deployment(testable = false)
public static Archive<WebArchive> createDeployment() {
	return ShrinkWrap.create(UndertowWebArchive.class).from(
			deployment()
			.setContextPath("/myapp")
			.setDeploymentName("test.war")
			.setClassLoader(
					EmbeddedUndertowClientWebContainerTest.class
							.getClassLoader())
			.addServlet(
					servlet("MessageServlet", MessageServlet.class)
						.addInitParam("message", "Hello World")
						.addMapping(SERVLET_MAPPING)));
}

And then we can write our test:

@Test
public void shouldBeAbleToInvokeServletInDeployedWebApp(
		@ArquillianResource URL url) throws Exception {

	String body = readAllAndClose(new URL(url, "myservlet").openStream());

	assertThat(body, is("Hello World"));
}

Non-blocking handler

But with Undertow you can also write non-blocking handlers. For creating a non-blocking handler in Undertow you simply must create a class that implements HttpHandler interface and register it. For this reason Arquillian-Container-Undertow provides a Shrinkwrap resolver named UndertowHttpHandlerArchive.

@Deployment(testable = false)
public static Archive<JavaArchive> createDeployment() {
	return ShrinkWrap.create(UndertowHttpHandlerArchive.class).from(new HttpHandler() {
           @Override
           public void handleRequest(final HttpServerExchange exchange) throws Exception {
               exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, "text/plain");
               exchange.getResponseSender().send("Hello World");
           }
       });
}

And then we can write our test:

@Test
public void shouldBeAbleToInvokeHandlers(@ArquillianResource URL url) throws Exception {

	String body = readAllAndClose(url.openStream());

	assertThat(body, is("Hello World"));

}

Configuration

You can configure the bind address and port of Undertow. By default Undertow is opened at localhost:8080.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<arquillian xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://jboss.org/schema/arquillian"
    xsi:schemaLocation="http://jboss.org/schema/arquillian http://jboss.org/schema/arquillian/arquillian_1_0.xsd">

    <container qualifier="undertow" default="true">
    </container>
</arquillian>

But you can also set the bind address and the listening port.

<container qualifier="undertow" default="true">
	<configuration>
		<property name="bindAddress">localhost</property>
		<property name="bindHttpPort">9090</property> <!-- <1> -->
	</configuration>
</container>
  1. If port is set to -1, a random port between 1024 and 49151 is used.

Maven

Arquillian-Container-Undertow is released on JBoss Maven Repositories so you need to set it on your pom file.

<repositories>
	<repository>
    	<id>jboss-public-repository-group</id>
        <name>JBoss Public Maven Repository Group</name>
        <url>https://repository.jboss.org/nexus/content/groups/public</url>
	</repository>
</repositories>

And add dependency as:

<dependency>
  <groupId>org.jboss.arquillian.container</groupId>
  <artifactId>undertow-embedded</artifactId>
  <version>1.0.0.Alpha1</version>
</dependency>