From 4cddf4d211c6f20e426306689eaea0daea617021 Mon Sep 17 00:00:00 2001 From: Gabriel Roldan Date: Sun, 11 Aug 2024 13:57:11 -0300 Subject: [PATCH] Support running the GeoServerAclContainer with JUnit 4 and Java 11 --- src/artifacts/testcontainer/pom.xml | 3 + .../testcontainer/GeoServerAclContainer.java | 58 +++++++++++++++++++ 2 files changed, 61 insertions(+) diff --git a/src/artifacts/testcontainer/pom.xml b/src/artifacts/testcontainer/pom.xml index d507065..917d915 100644 --- a/src/artifacts/testcontainer/pom.xml +++ b/src/artifacts/testcontainer/pom.xml @@ -15,6 +15,9 @@ gs-acl-testcontainer jar acl-testcontainer + + 11 + org.testcontainers diff --git a/src/artifacts/testcontainer/src/main/java/org/geoserver/acl/testcontainer/GeoServerAclContainer.java b/src/artifacts/testcontainer/src/main/java/org/geoserver/acl/testcontainer/GeoServerAclContainer.java index bf2ef0f..2ca6ed2 100644 --- a/src/artifacts/testcontainer/src/main/java/org/geoserver/acl/testcontainer/GeoServerAclContainer.java +++ b/src/artifacts/testcontainer/src/main/java/org/geoserver/acl/testcontainer/GeoServerAclContainer.java @@ -4,6 +4,12 @@ */ package org.geoserver.acl.testcontainer; +import static org.junit.Assume.assumeTrue; + +import org.junit.Assume; +import org.junit.runner.Description; +import org.junit.runners.model.Statement; +import org.testcontainers.DockerClientFactory; import org.testcontainers.containers.GenericContainer; import org.testcontainers.containers.wait.strategy.Wait; import org.testcontainers.containers.wait.strategy.WaitStrategy; @@ -11,6 +17,7 @@ import java.time.Duration; import java.util.Objects; +import java.util.logging.Logger; public class GeoServerAclContainer extends GenericContainer { @@ -37,6 +44,9 @@ public class GeoServerAclContainer extends GenericContainerSame effect as JUnit 5's {@code + * org.testcontainers.junit.jupiter.@Testcontainers(disabledWithoutDocker = true)} + */ + public GeoServerAclContainer disabledWithoutDocker() { + this.disabledWithoutDocker = true; + return this; + } + + /** + * Support for JUnit 4 to have the same effect as JUnit 5's {@code + * org.testcontainers.junit.jupiter.@Testcontainers(disabledWithoutDocker = true)} when {@link + * #disabledWithoutDocker()}. + * + *

Overrides to apply the {@link Assume assumption} checking the Docker environment is + * available if {@link #disabledWithoutDocker() enabled}, so this test container can be used as + * a {@code ClassRule @ClassRule} and hence avoid running a container for each test case. + */ + @Override + @SuppressWarnings("deprecation") + public Statement apply(Statement base, Description description) { + if (disabledWithoutDocker) { + assumeTrue( + "Docker environment unavailable, ignoring tests", + DockerClientFactory.instance().isDockerAvailable()); + } + return super.apply(base, description); + } + + @Override + protected void doStart() { + Logger.getLogger(getClass().getName()) + .info("Starting " + getDockerImageName() + " test container"); + super.doStart(); + } + public String devAdminUser() { return "admin"; } @@ -73,4 +121,14 @@ public String apiUrl() { int port = apiPort(); return String.format("http://%s:%d/acl/api", host, port); } + + @Override + public boolean equals(Object o) { + return this == o; + } + + @Override + public int hashCode() { + return System.identityHashCode(this); + } }