Skip to content

Commit

Permalink
[580] Add a manual mode test and do not use the Arquillian core paren…
Browse files Browse the repository at this point in the history
…t POM for tests.
  • Loading branch information
jamezp committed Jul 8, 2024
1 parent e68fa63 commit fbd394b
Show file tree
Hide file tree
Showing 18 changed files with 448 additions and 38 deletions.
2 changes: 1 addition & 1 deletion docs/introduction.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ _Do you really need to run the test in a real container when a Java SE
CDI environment would do?_

It's true, some tests can work without a full container. For instance,
you can run certain tests in a Java SE CDI environment with Arquillian.
you can run certain tests in a Java SE org.jboss.arquillian.integration.test.junit.common.CDI environment with Arquillian.
Let's call these "standalone" tests, whereas tests which do require a
full container are called "integration" tests. Every standalone test can
also be run as an integration test, but not the other way around. While
Expand Down
28 changes: 14 additions & 14 deletions docs/test-enrichers.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ endif::[]
* <<test-case-injection-points, Test Case Injection Points>>
* <<default-enrichers, Default Enrichers>>
** <<resource-injection, Resource Injection>>
** <<ejb-injection, EJB Injection (without CDI support)>>
** <<cdi-injection, CDI Injection>>
** <<ejb-injection, EJB Injection (without org.jboss.arquillian.integration.test.junit.common.CDI support)>>
** <<cdi-injection, org.jboss.arquillian.integration.test.junit.common.CDI Injection>>
* <<active-scopes, Active Scopes>>

When you use a unit testing framework like JUnit or TestNG, your test
Expand All @@ -28,7 +28,7 @@ this environment in the test falls on the developer's shoulders.

With Arquillian, you no longer have to worry about setting up the
execution environment because that is all handled for you. The test will
either be running in a container or a local CDI environment. But you
either be running in a container or a local org.jboss.arquillian.integration.test.junit.common.CDI environment. But you
still need some way to hook your test into this environment.

A key part of in-container integration testing is getting access to the
Expand Down Expand Up @@ -90,17 +90,17 @@ the box:
* `@Resource` - Java EE resource injections
* `@EJB` - EJB session bean reference injections
* `@Inject`, `@Resource`, `@EJB`, `@PersistenceContext` and
`@PersistenceUnit` - CDI-supported injections
`@PersistenceUnit` - org.jboss.arquillian.integration.test.junit.common.CDI-supported injections

The first two enrichers use JNDI to lookup the instance to inject. The
CDI injections are handled by treating the test class as a bean capable
of receiving standard CDI injections (non-contextual injection). Since
CDI requires containers to satisfy all Java EE injection points on a CDI
org.jboss.arquillian.integration.test.junit.common.CDI injections are handled by treating the test class as a bean capable
of receiving standard org.jboss.arquillian.integration.test.junit.common.CDI injections (non-contextual injection). Since
org.jboss.arquillian.integration.test.junit.common.CDI requires containers to satisfy all Java EE injection points on a org.jboss.arquillian.integration.test.junit.common.CDI
bean, the `@Resource`, `@EJB`, `@PersistenceContext` and
`@PersistenceUnit` injections are supported transitively by the CDI
enricher. In fact, because of CDI's tight integration with the
`@PersistenceUnit` injections are supported transitively by the org.jboss.arquillian.integration.test.junit.common.CDI
enricher. In fact, because of org.jboss.arquillian.integration.test.junit.common.CDI's tight integration with the
container, `@EJB` injections in the test class are satisfied according
to specification when the CDI enricher is available.
to specification when the org.jboss.arquillian.integration.test.junit.common.CDI enricher is available.

[[resource-injection]]
====== Resource Injection
Expand All @@ -111,7 +111,7 @@ defined in the Section 2.3 of the Common Annotations for the Java
Platform specification).

[[ejb-injection]]
====== EJB Injection (without CDI support)
====== EJB Injection (without org.jboss.arquillian.integration.test.junit.common.CDI support)

The `@EJB` annotation performs a JNDI lookup for the EJB session bean
reference using the following equation in the specified order:
Expand Down Expand Up @@ -139,9 +139,9 @@ common naming convention of EJB beans. In the future the lookup will
rely on the standard JNDI naming conventions established in Java EE 6.

[[cdi-injection]]
====== CDI Injection
====== org.jboss.arquillian.integration.test.junit.common.CDI Injection

In order for CDI injections to work, the test archive defined with
In order for org.jboss.arquillian.integration.test.junit.common.CDI injections to work, the test archive defined with
ShrinkWrap must be in a bean archive. That means adding beans.xml to the
META-INF directory. Here's a `@Deployment` method that shows one way to
add a beans.xml to the archive:
Expand All @@ -155,7 +155,7 @@ public static JavaArchive createTestArchive() {
.addAsManifestResource(EmptyAsset.INSTANCE, Paths.create("beans.xml"))
----

In an application that takes full advantage of CDI, you can likely get
In an application that takes full advantage of org.jboss.arquillian.integration.test.junit.common.CDI, you can likely get
by only using injections defined with the `@Inject` annotation.
Regardless, the other two types of injection come in handy from
time-to-time.
Expand Down
24 changes: 23 additions & 1 deletion integration-tests/common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,22 @@
<name>Arquillian Core: Common integration tests</name>
<description/>

<properties/>
<properties>
<version.org.junit>5.10.3</version.org.junit>
</properties>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit-bom</artifactId>
<version>${version.org.junit}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>jakarta.annotation</groupId>
Expand All @@ -40,6 +55,13 @@
<groupId>org.jboss.arquillian.test</groupId>
<artifactId>arquillian-test-api</artifactId>
</dependency>

<!-- Test Dependencies -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build/>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package org.jboss.arquillian.integration.test.common;

import java.net.URI;

/**
* The test environment setup.
*
* @author <a href="mailto:[email protected]">James R. Perkins</a>
*/
public class TestEnvironment {

private static final String PROTOCOL;
private static final String HOST;
private static final int PORT;
public static final String REST_PATH = "/rest";

static {
PROTOCOL = System.getProperty("arq.protocol", "http");
HOST = System.getProperty("arq.host", "localhost");
PORT = Integer.parseInt(System.getProperty("arq.port", "8080"));
}

private TestEnvironment() {
}

/**
* Returns the defined protocol to use for HTTP connections. The default is {@code http} and can be overridden
* with the {@code arq.protocol} system property.
*
* @return the HTTP protocol
*/
public static String protocol() {
return PROTOCOL;
}

/**
* Returns the defined host to use for HTTP connections. The default is {@code localhost} and can be overridden
* with the {@code arq.host} system property.
*
* @return the HTTP host
*/
public static String host() {
return HOST;
}

/**
* Returns the defined port to use for HTTP connections. The default is {@code 8080} and can be overridden
* with the {@code arq.port} system property.
*
* @return the HTTP port
*/
public static int port() {
return PORT;
}

/**
* Creates a URI with the given paths appended to the {@linkplain #protocol() protocol}, {@linkplain #host() host}
* and {@linkplain #port() port}.
*
* @param paths the paths to append
*
* @return a new URI for an HTTP connection
*/
public static URI uri(final String... paths) {
final StringBuilder uri = new StringBuilder()
.append(protocol())
.append("://")
.append(host())
.append(':')
.append(port());
for (String path : paths) {
if (!path.isEmpty()) {
if (path.charAt(0) == '/') {
uri.append(path);
} else {
uri.append('/').append(path);
}
}
}
return URI.create(uri.toString());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.jboss.arquillian.integration.test.common.app;

import jakarta.enterprise.context.RequestScoped;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.Path;

/**
* @author <a href="mailto:[email protected]">James R. Perkins</a>
*/
@Path("echo")
@RequestScoped
public class EchoResource {

@POST
public String echo(final String msg) {
return msg;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.jboss.arquillian.integration.test.common.app;

import jakarta.ws.rs.ApplicationPath;
import jakarta.ws.rs.core.Application;
import org.jboss.arquillian.integration.test.common.TestEnvironment;

/**
* @author <a href="mailto:[email protected]">James R. Perkins</a>
*/
@ApplicationPath(TestEnvironment.REST_PATH)
public class RestActivator extends Application {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright The WildFly Authors
~ SPDX-License-Identifier: Apache-2.0
-->

<arquillian xmlns="http://jboss.org/schema/arquillian" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://jboss.org/schema/arquillian http://jboss.org/schema/arquillian/arquillian_1_0.xsd">
<container qualifier="default" default="true" mode="manual"/>
</arquillian>
14 changes: 13 additions & 1 deletion integration-tests/junit4-tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<groupId>org.jboss.arquillian</groupId>
<artifactId>integration-tests</artifactId>
<version>1.8.2.Final-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

<artifactId>junit4-tests</artifactId>
Expand Down Expand Up @@ -37,7 +38,18 @@
</dependency>
</dependencies>

<build/>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>11</source>
<target>11</target>
</configuration>
</plugin>
</plugins>
</build>

<profiles>
<profile>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.jboss.arquillian.integration.test.junit;
package org.jboss.arquillian.integration.test.cdi;

import java.net.URI;

Expand All @@ -20,7 +20,7 @@
*/
@RunWith(Arquillian.class)
@ApplicationScoped
public class CdiInjectionTestCase {
public class CdiInjectionTest {

@Deployment
public static WebArchive createDeployment() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package org.jboss.arquillian.integration.test.manual;

import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;

import org.jboss.arquillian.container.test.api.ContainerController;
import org.jboss.arquillian.container.test.api.Deployer;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.container.test.api.RunAsClient;
import org.jboss.arquillian.integration.test.common.TestEnvironment;
import org.jboss.arquillian.integration.test.common.app.EchoResource;
import org.jboss.arquillian.integration.test.common.app.RestActivator;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.arquillian.test.api.ArquillianResource;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;

/**
* @author <a href="mailto:[email protected]">James R. Perkins</a>
*/
@RunWith(Arquillian.class)
@RunAsClient
public class ManualModeTest {
private static final String CONTAINER_NAME = "default";
private static final String DEPLOYMENT_NAME = "manual-mode";

@Deployment(name = DEPLOYMENT_NAME, managed = false)
public static WebArchive createDeployment() {
return ShrinkWrap.create(WebArchive.class, DEPLOYMENT_NAME + ".war")
.addClasses(RestActivator.class, EchoResource.class)
.addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");
}

@ArquillianResource
private static ContainerController controller;

@ArquillianResource
private static Deployer deployer;

@Test
public void startConnectAndStop() throws Exception {
Assert.assertNotNull(controller);
Assert.assertFalse("This is a manual mode test and the server should not have been started.", controller.isStarted(CONTAINER_NAME));
// Check that we can start the server and the server is running
controller.start(CONTAINER_NAME);
Assert.assertTrue("The server should have been started", controller.isStarted(CONTAINER_NAME));

// Deploy the application and make a REST request
deployer.deploy(DEPLOYMENT_NAME);
assertAppDeployed();

// Undeploy the application and stop the server
deployer.undeploy(DEPLOYMENT_NAME);
controller.stop(CONTAINER_NAME);
}

private void assertAppDeployed() throws Exception {
final String msg = "Test message";
final HttpClient client = HttpClient.newHttpClient();
final HttpRequest request = HttpRequest.newBuilder()
.uri(TestEnvironment.uri(DEPLOYMENT_NAME, TestEnvironment.REST_PATH, "echo"))
.POST(HttpRequest.BodyPublishers.ofString(msg))
.build();
final HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
Assert.assertEquals(200, response.statusCode());
Assert.assertEquals(msg, response.body());
}
}
1 change: 1 addition & 0 deletions integration-tests/junit5-tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<groupId>org.jboss.arquillian</groupId>
<artifactId>integration-tests</artifactId>
<version>1.8.2.Final-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

<artifactId>junit5-tests</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.jboss.arquillian.integration.test.junit5;
package org.jboss.arquillian.integration.test.cdi;

import java.net.URI;

Expand All @@ -20,7 +20,7 @@
*/
@ExtendWith(ArquillianExtension.class)
@ApplicationScoped
public class CdiInjectionTestCase {
public class CdiInjectionTest {

@Deployment
public static WebArchive createDeployment() {
Expand Down
Loading

0 comments on commit fbd394b

Please sign in to comment.