diff --git a/pom.xml b/pom.xml index e7d95d5..fb67c32 100644 --- a/pom.xml +++ b/pom.xml @@ -76,6 +76,7 @@ 1.8.0.Final 1.19.8 + 5.10.3 2.23.0 @@ -95,18 +96,33 @@ pom import + + org.junit + junit-bom + ${version.org.junit} + pom + import + + + org.testcontainers + testcontainers-bom + ${version.testcontainers} + pom + import + - - org.jboss.arquillian.container - arquillian-container-test-impl-base - org.jboss.arquillian.test arquillian-test-spi + + + org.jboss.arquillian.container + arquillian-container-test-impl-base + org.apache.commons @@ -116,7 +132,6 @@ org.testcontainers testcontainers - ${version.testcontainers} junit @@ -128,6 +143,24 @@ hamcrest provided + + + + org.junit.jupiter + junit-jupiter + test + + + org.jboss.arquillian.junit5 + arquillian-junit5-container + test + + + org.testcontainers + mockserver + test + + diff --git a/src/main/java/org/jboss/arquillian/testcontainers/TestContainersObserver.java b/src/main/java/org/jboss/arquillian/testcontainers/TestContainersObserver.java index 941bf5d..228c941 100644 --- a/src/main/java/org/jboss/arquillian/testcontainers/TestContainersObserver.java +++ b/src/main/java/org/jboss/arquillian/testcontainers/TestContainersObserver.java @@ -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; @@ -16,8 +17,7 @@ import org.testcontainers.DockerClientFactory; import org.testcontainers.containers.GenericContainer; -import com.github.dockerjava.api.DockerClient; - +@SuppressWarnings("unused") public class TestContainersObserver { @Inject @ClassScoped @@ -49,21 +49,34 @@ 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 { + // First attempt to throw a JUnit 5 assumption + throwAssumption("org.opentest4j.TestAbortedException", detailMessage); + // Not found, attempt to throw a JUnit exception + throwAssumption("org.junit.AssumptionViolatedException", detailMessage); + // 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 void throwAssumption(final String type, final String detailMessage) { + try { + Class clazz = Class.forName(type); + Constructor ctor = clazz.getConstructor(String.class); + throw (RuntimeException) ctor.newInstance(detailMessage); + } catch (ClassNotFoundException | NoSuchMethodException | InstantiationException | IllegalAccessException + | InvocationTargetException ignore) { + } + } + + @SuppressWarnings({ "resource", "BooleanMethodIsAlwaysInverted" }) private boolean isDockerAvailable() { - try (DockerClient client = DockerClientFactory.instance().client()) { + try { + DockerClientFactory.instance().client(); return true; } catch (Throwable ex) { return false; diff --git a/src/test/java/org/jboss/arquillian/testcontainers/SanityTest.java b/src/test/java/org/jboss/arquillian/testcontainers/SanityTest.java new file mode 100644 index 0000000..7684fef --- /dev/null +++ b/src/test/java/org/jboss/arquillian/testcontainers/SanityTest.java @@ -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 James R. Perkins + */ +@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"); + } +} diff --git a/src/test/java/org/jboss/arquillian/testcontainers/test/common/SimpleTestContainer.java b/src/test/java/org/jboss/arquillian/testcontainers/test/common/SimpleTestContainer.java new file mode 100644 index 0000000..6527648 --- /dev/null +++ b/src/test/java/org/jboss/arquillian/testcontainers/test/common/SimpleTestContainer.java @@ -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 James R. Perkins + */ +public class SimpleTestContainer extends MockServerContainer { + + public SimpleTestContainer() { + super(DockerImageName + .parse("mockserver/mockserver") + .withTag("latest")); + } +} diff --git a/src/test/java/org/jboss/arquillian/testcontainers/test/common/TestDeployableContainer.java b/src/test/java/org/jboss/arquillian/testcontainers/test/common/TestDeployableContainer.java new file mode 100644 index 0000000..5aec1f9 --- /dev/null +++ b/src/test/java/org/jboss/arquillian/testcontainers/test/common/TestDeployableContainer.java @@ -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 James R. Perkins + */ +public class TestDeployableContainer implements DeployableContainer { + @Override + public Class 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 { + + } + } +} diff --git a/src/test/java/org/jboss/arquillian/testcontainers/test/common/TestLoadableExtension.java b/src/test/java/org/jboss/arquillian/testcontainers/test/common/TestLoadableExtension.java new file mode 100644 index 0000000..60c0f9b --- /dev/null +++ b/src/test/java/org/jboss/arquillian/testcontainers/test/common/TestLoadableExtension.java @@ -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 James R. Perkins + */ +public class TestLoadableExtension implements LoadableExtension { + @Override + public void register(final ExtensionBuilder builder) { + builder.service(DeployableContainer.class, TestDeployableContainer.class); + } +} diff --git a/src/test/resources/META-INF/services/org.jboss.arquillian.core.spi.LoadableExtension b/src/test/resources/META-INF/services/org.jboss.arquillian.core.spi.LoadableExtension new file mode 100644 index 0000000..e8a541a --- /dev/null +++ b/src/test/resources/META-INF/services/org.jboss.arquillian.core.spi.LoadableExtension @@ -0,0 +1 @@ +org.jboss.arquillian.testcontainers.test.common.TestLoadableExtension \ No newline at end of file