Skip to content

Commit

Permalink
Support running the GeoServerAclContainer with JUnit 4 and Java 11
Browse files Browse the repository at this point in the history
  • Loading branch information
groldan committed Aug 11, 2024
1 parent 2a59212 commit 4cddf4d
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/artifacts/testcontainer/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
<artifactId>gs-acl-testcontainer</artifactId>
<packaging>jar</packaging>
<name>acl-testcontainer</name>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.testcontainers</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,20 @@
*/
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;
import org.testcontainers.utility.DockerImageName;

import java.time.Duration;
import java.util.Objects;
import java.util.logging.Logger;

public class GeoServerAclContainer extends GenericContainer<GeoServerAclContainer> {

Expand All @@ -37,6 +44,9 @@ public class GeoServerAclContainer extends GenericContainer<GeoServerAclContaine
public static final DockerImageName CURRENT =
DockerImageName.parse(DEFAULT_IMAGE_REPOSITORY + ":" + CURRENT_VERSION);

/** flag for {@link #disabledWithoutDocker()} */
private boolean disabledWithoutDocker;

public GeoServerAclContainer() {
this(CURRENT);
}
Expand All @@ -56,6 +66,44 @@ public GeoServerAclContainer withDevMode() {
return this;
}

/**
* Disables the tests using this testcontainer if there's no Docker environment available.
*
* <p>Same 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()}.
*
* <p>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";
}
Expand All @@ -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);
}
}

0 comments on commit 4cddf4d

Please sign in to comment.