<commit_before>= 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_.

[source, java]
----
@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:

[source, java]
----
@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 _DD_ interface and register it. For this reason +Arquillian-Container-Undertow+ provides a +Shrinkwrap+ resolver named _sss_.

[source, java]
----
@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:

[source, java]
----
@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_.

[source, xml]
----
<?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.

[source, xml]
----
<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.<commit_msg>Fixed some typo errors and add how to add as Maven dependency.<commit_after>= 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_.

[source, java]
----
@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:

[source, java]
----
@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_.

[source, java]
----
@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:

[source, java]
----
@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_.

[source, xml]
----
<?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.

[source, xml]
----
<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.

[source, xml]
----
<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:

[source, xml]
----
<dependency>
  <groupId>org.jboss.arquillian.container</groupId>
  <artifactId>undertow-embedded</artifactId>
  <version>1.0.0.Alpha1</version>
</dependency>
----
{'fraction_non_alphanumeric': 0.11011391094235416, 'fraction_numerical': 0.00782418593947762, 'mean_word_length': 4.392059553349876, 'pattern_counts': {'":': 0, '<': 57, '<?xml version=': 2, '>': 57, 'https://': 1, 'lorem ipsum': 0, 'www.': 2, 'xml': 12}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '702623', 'n_tokens_mistral': 2761, 'n_tokens_neox': 2484, 'n_words': 812}