From 95467c28f478c2afa11b950b15a1183453410d90 Mon Sep 17 00:00:00 2001 From: nscuro Date: Tue, 13 Feb 2024 18:11:07 +0100 Subject: [PATCH] Consider reusability when stopping Postgres testcontainer Signed-off-by: nscuro --- .../java/org/dependencytrack/PersistenceCapableTest.java | 8 +++----- .../java/org/dependencytrack/PostgresTestContainer.java | 8 ++++++++ src/test/java/org/dependencytrack/ResourceTest.java | 2 +- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/test/java/org/dependencytrack/PersistenceCapableTest.java b/src/test/java/org/dependencytrack/PersistenceCapableTest.java index e0b73bca9..a78a20efc 100644 --- a/src/test/java/org/dependencytrack/PersistenceCapableTest.java +++ b/src/test/java/org/dependencytrack/PersistenceCapableTest.java @@ -32,7 +32,6 @@ import org.junit.Rule; import org.junit.contrib.java.lang.system.EnvironmentVariables; import org.testcontainers.containers.PostgreSQLContainer; -import org.testcontainers.utility.TestcontainersConfiguration; import javax.jdo.JDOHelper; import java.sql.Connection; @@ -49,7 +48,7 @@ public abstract class PersistenceCapableTest { protected QueryManager qm; @BeforeClass - public static void init() throws Exception { + public static void init() { Config.enableUnitTests(); postgresContainer = new PostgresTestContainer(); @@ -84,9 +83,8 @@ public void after() { @AfterClass public static void tearDownClass() { - final boolean canReuseContainers = TestcontainersConfiguration.getInstance().environmentSupportsReuse(); - if (postgresContainer != null && (!canReuseContainers || !postgresContainer.isShouldBeReused())) { - postgresContainer.stop(); + if (postgresContainer != null) { + postgresContainer.stopWhenNotReusing(); } } diff --git a/src/test/java/org/dependencytrack/PostgresTestContainer.java b/src/test/java/org/dependencytrack/PostgresTestContainer.java index 99f682f7a..967caa2d4 100644 --- a/src/test/java/org/dependencytrack/PostgresTestContainer.java +++ b/src/test/java/org/dependencytrack/PostgresTestContainer.java @@ -5,6 +5,7 @@ import org.postgresql.ds.PGSimpleDataSource; import org.testcontainers.containers.PostgreSQLContainer; import org.testcontainers.utility.DockerImageName; +import org.testcontainers.utility.TestcontainersConfiguration; public class PostgresTestContainer extends PostgreSQLContainer { @@ -42,4 +43,11 @@ protected void containerIsStarted(final InspectContainerResponse containerInfo, throw new RuntimeException(e); } } + + public void stopWhenNotReusing() { + if (!TestcontainersConfiguration.getInstance().environmentSupportsReuse() || !isShouldBeReused()) { + stop(); + } + } + } diff --git a/src/test/java/org/dependencytrack/ResourceTest.java b/src/test/java/org/dependencytrack/ResourceTest.java index 89b2e939c..ba34602ea 100644 --- a/src/test/java/org/dependencytrack/ResourceTest.java +++ b/src/test/java/org/dependencytrack/ResourceTest.java @@ -145,7 +145,7 @@ public void after() { @AfterClass public static void tearDownClass() { if (postgresContainer != null) { - postgresContainer.stop(); + postgresContainer.stopWhenNotReusing(); } }