Skip to content

Commit

Permalink
Add a simple sanity and some simple test framework.
Browse files Browse the repository at this point in the history
Signed-off-by: James R. Perkins <[email protected]>
  • Loading branch information
jamezp committed Jun 28, 2024
1 parent 0879eb0 commit ce6fe18
Show file tree
Hide file tree
Showing 7 changed files with 191 additions and 15 deletions.
43 changes: 38 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
<!-- Dependency versions -->
<version.arquillian_core>1.8.0.Final</version.arquillian_core>
<version.testcontainers>1.19.8</version.testcontainers>
<version.org.junit>5.10.3</version.org.junit>

<!-- Plugin versions -->
<version.formatter.maven.plugin>2.23.0</version.formatter.maven.plugin>
Expand All @@ -95,18 +96,33 @@
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit-bom</artifactId>
<version>${version.org.junit}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers-bom</artifactId>
<version>${version.testcontainers}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>org.jboss.arquillian.container</groupId>
<artifactId>arquillian-container-test-impl-base</artifactId>
</dependency>
<dependency>
<groupId>org.jboss.arquillian.test</groupId>
<artifactId>arquillian-test-spi</artifactId>
</dependency>
<!-- Required for the OperatesOnDeploymentAwareProvider -->
<dependency>
<groupId>org.jboss.arquillian.container</groupId>
<artifactId>arquillian-container-test-impl-base</artifactId>
</dependency>
<!-- To avoid a CVE. This can be removed once TC is updated -->
<dependency>
<groupId>org.apache.commons</groupId>
Expand All @@ -116,7 +132,6 @@
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<version>${version.testcontainers}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
Expand All @@ -128,6 +143,24 @@
<artifactId>hamcrest</artifactId>
<scope>provided</scope>
</dependency>

<!-- Test dependencies -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.arquillian.junit5</groupId>
<artifactId>arquillian-junit5-container</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>mockserver</artifactId>
<scope>test</scope>
</dependency>

</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package org.jboss.arquillian.testcontainers;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

import org.jboss.arquillian.core.api.InstanceProducer;
import org.jboss.arquillian.core.api.annotation.Inject;
Expand Down Expand Up @@ -49,21 +50,35 @@ public void stopContainer(@Observes AfterClass afterClass) {

private void checkForDocker(boolean failIfNoDocker) {
final String detailMessage = "No Docker environment is available.";
if (!isDockerAvailable() && failIfNoDocker) {
throw new AssertionError(detailMessage);
} else {
try {
Class<?> clazz = Class.forName("org.junit.AssumptionViolatedException");
Constructor<?> ctor = clazz.getDeclaredConstructor(String.class);
throw (RuntimeException) ctor.newInstance(detailMessage);
} catch (Exception e) {
// TestNG?
if (!isDockerAvailable()) {
if (failIfNoDocker) {
throw new AssertionError(detailMessage);
} else if (!isDockerAvailable()) {
// First attempt to throw a JUnit 5 assumption
try {
Class<?> clazz = Class.forName("org.opentest4j.TestAbortedException");
Constructor<?> ctor = clazz.getConstructor(String.class);
throw (RuntimeException) ctor.newInstance(detailMessage);
} catch (ClassNotFoundException | NoSuchMethodException | InstantiationException | IllegalAccessException
| InvocationTargetException ignore) {
}
// Not found, attempt to throw a JUnit exception
try {
Class<?> clazz = Class.forName("org.junit.AssumptionViolatedException");
Constructor<?> ctor = clazz.getDeclaredConstructor(String.class);
throw (RuntimeException) ctor.newInstance(detailMessage);
} catch (ClassNotFoundException | NoSuchMethodException | InstantiationException | IllegalAccessException
| InvocationTargetException ignore) {
}
// No supported test platform found. Throw an AssertionError.
throw new AssertionError("Failed to find a support test platform and no Docker environment is available.");
}
}
}

private boolean isDockerAvailable() {
try (DockerClient client = DockerClientFactory.instance().client()) {
try {
DockerClient client = DockerClientFactory.instance().client();
return true;
} catch (Throwable ex) {
return false;
Expand Down
42 changes: 42 additions & 0 deletions src/test/java/org/jboss/arquillian/testcontainers/SanityTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright The Arquillian Authors
* SPDX-License-Identifier: Apache-2.0
*/

package org.jboss.arquillian.testcontainers;

import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.container.test.api.RunAsClient;
import org.jboss.arquillian.junit5.ArquillianExtension;
import org.jboss.arquillian.test.api.ArquillianResource;
import org.jboss.arquillian.testcontainers.test.common.SimpleTestContainer;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

/**
* @author <a href="mailto:[email protected]">James R. Perkins</a>
*/
@ExtendWith(ArquillianExtension.class)
@TestContainer(value = SimpleTestContainer.class, failIfNoDocker = false)
@RunAsClient
public class SanityTest {

@ArquillianResource
private SimpleTestContainer container;

@Deployment
public static JavaArchive createDeployment() {
return ShrinkWrap.create(JavaArchive.class)
.addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");
}

@Test
public void testContainerInjected() {
Assertions.assertNotNull(container, "Expected the container to be injected.");
Assertions.assertTrue(container.isRunning(), "Expected the container to be running");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright The Arquillian Authors
* SPDX-License-Identifier: Apache-2.0
*/

package org.jboss.arquillian.testcontainers.test.common;

import org.testcontainers.containers.MockServerContainer;
import org.testcontainers.utility.DockerImageName;

/**
* @author <a href="mailto:[email protected]">James R. Perkins</a>
*/
public class SimpleTestContainer extends MockServerContainer {

public SimpleTestContainer() {
super(DockerImageName
.parse("mockserver/mockserver")
.withTag("latest"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright The Arquillian Authors
* SPDX-License-Identifier: Apache-2.0
*/

package org.jboss.arquillian.testcontainers.test.common;

import org.jboss.arquillian.container.spi.ConfigurationException;
import org.jboss.arquillian.container.spi.client.container.ContainerConfiguration;
import org.jboss.arquillian.container.spi.client.container.DeployableContainer;
import org.jboss.arquillian.container.spi.client.protocol.ProtocolDescription;
import org.jboss.arquillian.container.spi.client.protocol.metadata.ProtocolMetaData;
import org.jboss.shrinkwrap.api.Archive;

/**
* @author <a href="mailto:[email protected]">James R. Perkins</a>
*/
public class TestDeployableContainer implements DeployableContainer<TestDeployableContainer.EmptyContainerConfiguration> {
@Override
public Class<EmptyContainerConfiguration> getConfigurationClass() {
return EmptyContainerConfiguration.class;
}

@Override
public ProtocolDescription getDefaultProtocol() {
return new ProtocolDescription("TestDeployableContainer");
}

@Override
public ProtocolMetaData deploy(final Archive<?> archive) {
return new ProtocolMetaData();
}

@Override
public void undeploy(final Archive<?> archive) {
}

public static class EmptyContainerConfiguration implements ContainerConfiguration {

@Override
public void validate() throws ConfigurationException {

}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* Copyright The Arquillian Authors
* SPDX-License-Identifier: Apache-2.0
*/

package org.jboss.arquillian.testcontainers.test.common;

import org.jboss.arquillian.container.spi.client.container.DeployableContainer;
import org.jboss.arquillian.core.spi.LoadableExtension;

/**
* @author <a href="mailto:[email protected]">James R. Perkins</a>
*/
public class TestLoadableExtension implements LoadableExtension {
@Override
public void register(final ExtensionBuilder builder) {
builder.service(DeployableContainer.class, TestDeployableContainer.class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.jboss.arquillian.testcontainers.test.common.TestLoadableExtension

0 comments on commit ce6fe18

Please sign in to comment.