From 853ee310f728e49e13ffc55327e65c9af26faba1 Mon Sep 17 00:00:00 2001 From: emeroad Date: Fri, 24 Jun 2022 10:13:46 +0900 Subject: [PATCH] [#8965] Apply SharedTestLifeCycle to PostSql --- .../PostgreSQLContainerFactory.java | 3 +- .../jdbc/postgresql/PostgreSqlBase.java | 44 ++++++------------- .../jdbc/postgresql/PostgreSqlServer.java | 38 ++++++++++++++++ .../postgresql/PostgreSql_9_4_1207_IT.java | 22 +++------- .../PostgreSql_9_x_to_9_4_1207_IT.java | 22 +++------- .../PostgreSql_Post_9_4_1208_IT.java | 24 ++-------- 6 files changed, 65 insertions(+), 88 deletions(-) create mode 100644 plugins-it/postgresql-jdbc-it/src/test/java/com/navercorp/pinpoint/plugin/jdbc/postgresql/PostgreSqlServer.java diff --git a/plugins-it/postgresql-jdbc-it/src/test/java/com/navercorp/pinpoint/plugin/jdbc/postgresql/PostgreSQLContainerFactory.java b/plugins-it/postgresql-jdbc-it/src/test/java/com/navercorp/pinpoint/plugin/jdbc/postgresql/PostgreSQLContainerFactory.java index a787d80ebf97..81d332e65f83 100644 --- a/plugins-it/postgresql-jdbc-it/src/test/java/com/navercorp/pinpoint/plugin/jdbc/postgresql/PostgreSQLContainerFactory.java +++ b/plugins-it/postgresql-jdbc-it/src/test/java/com/navercorp/pinpoint/plugin/jdbc/postgresql/PostgreSQLContainerFactory.java @@ -18,7 +18,6 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; -import org.testcontainers.containers.JdbcDatabaseContainer; import org.testcontainers.containers.PostgreSQLContainer; import org.testcontainers.containers.output.OutputFrame; @@ -29,7 +28,7 @@ */ public class PostgreSQLContainerFactory { - public static JdbcDatabaseContainer newContainer(String loggerName) { + public static PostgreSQLContainer newContainer(String loggerName) { PostgreSQLContainer container = new PostgreSQLContainer(); container.withInitScript("init_postgresql.sql"); diff --git a/plugins-it/postgresql-jdbc-it/src/test/java/com/navercorp/pinpoint/plugin/jdbc/postgresql/PostgreSqlBase.java b/plugins-it/postgresql-jdbc-it/src/test/java/com/navercorp/pinpoint/plugin/jdbc/postgresql/PostgreSqlBase.java index d1a0e70a7f6e..d0276c186f14 100644 --- a/plugins-it/postgresql-jdbc-it/src/test/java/com/navercorp/pinpoint/plugin/jdbc/postgresql/PostgreSqlBase.java +++ b/plugins-it/postgresql-jdbc-it/src/test/java/com/navercorp/pinpoint/plugin/jdbc/postgresql/PostgreSqlBase.java @@ -19,13 +19,11 @@ import com.navercorp.pinpoint.pluginit.jdbc.DriverManagerUtils; import com.navercorp.pinpoint.pluginit.jdbc.DriverProperties; import com.navercorp.pinpoint.pluginit.jdbc.JDBCDriverClass; -import com.navercorp.pinpoint.test.plugin.shared.AfterSharedClass; - +import com.navercorp.pinpoint.test.plugin.shared.SharedTestBeforeAllResult; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import org.junit.After; import org.junit.Before; -import org.apache.logging.log4j.Logger; -import org.apache.logging.log4j.LogManager; -import org.testcontainers.containers.JdbcDatabaseContainer; import java.sql.Driver; import java.sql.DriverManager; @@ -37,45 +35,29 @@ public abstract class PostgreSqlBase { protected abstract JDBCDriverClass getJDBCDriverClass(); - private static final Logger logger = LogManager.getLogger(PostgreSqlBase.class); - - protected static JdbcDatabaseContainer container; + private final Logger logger = LogManager.getLogger(getClass()); - // ---------- For @BeforeSharedClass, @AfterSharedClass // private static String JDBC_URL; - private static String USER_NAME; - private static String PASS_WORD; + private static String USERNAME; + private static String PASSWORD; public static String getJdbcUrl() { return JDBC_URL; } - public static void setJdbcUrl(String jdbcUrl) { - JDBC_URL = jdbcUrl; - } - public static String getUserName() { - return USER_NAME; - } - - public static void setUserName(String userName) { - USER_NAME = userName; + return USERNAME; } public static String getPassWord() { - return PASS_WORD; - } - - public static void setPassWord(String passWord) { - PASS_WORD = passWord; + return PASSWORD; } - // ---------- // - @AfterSharedClass - public static void sharedTeardown() throws Exception { - if (container != null) { - container.stop(); - } + @SharedTestBeforeAllResult + public static void setBeforeAllResult(Properties beforeAllResult) { + JDBC_URL = beforeAllResult.getProperty("JDBC_URL"); + USERNAME = beforeAllResult.getProperty("USERNAME"); + PASSWORD = beforeAllResult.getProperty("PASSWORD"); } public static DriverProperties getDriverProperties() { diff --git a/plugins-it/postgresql-jdbc-it/src/test/java/com/navercorp/pinpoint/plugin/jdbc/postgresql/PostgreSqlServer.java b/plugins-it/postgresql-jdbc-it/src/test/java/com/navercorp/pinpoint/plugin/jdbc/postgresql/PostgreSqlServer.java new file mode 100644 index 000000000000..a54d85f17dd0 --- /dev/null +++ b/plugins-it/postgresql-jdbc-it/src/test/java/com/navercorp/pinpoint/plugin/jdbc/postgresql/PostgreSqlServer.java @@ -0,0 +1,38 @@ +package com.navercorp.pinpoint.plugin.jdbc.postgresql; + +import com.navercorp.pinpoint.test.plugin.shared.SharedTestLifeCycle; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.junit.Assume; +import org.testcontainers.DockerClientFactory; +import org.testcontainers.containers.PostgreSQLContainer; + +import java.util.Properties; + +public class PostgreSqlServer implements SharedTestLifeCycle { + private final Logger logger = LogManager.getLogger(getClass()); + + private PostgreSQLContainer postgreSql; + + @Override + public Properties beforeAll() { + Assume.assumeTrue("Docker not enabled", DockerClientFactory.instance().isDockerAvailable()); + + postgreSql = PostgreSQLContainerFactory.newContainer(logger.getName()); + + postgreSql.start(); + + Properties properties = new Properties(); + properties.setProperty("JDBC_URL", postgreSql.getJdbcUrl()); + properties.setProperty("USERNAME", postgreSql.getUsername()); + properties.setProperty("PASSWORD", postgreSql.getPassword()); + return properties; + } + + @Override + public void afterAll() { + if (postgreSql != null) { + postgreSql.stop(); + } + } +} diff --git a/plugins-it/postgresql-jdbc-it/src/test/java/com/navercorp/pinpoint/plugin/jdbc/postgresql/PostgreSql_9_4_1207_IT.java b/plugins-it/postgresql-jdbc-it/src/test/java/com/navercorp/pinpoint/plugin/jdbc/postgresql/PostgreSql_9_4_1207_IT.java index 4739cc55a471..76364b56dfe5 100644 --- a/plugins-it/postgresql-jdbc-it/src/test/java/com/navercorp/pinpoint/plugin/jdbc/postgresql/PostgreSql_9_4_1207_IT.java +++ b/plugins-it/postgresql-jdbc-it/src/test/java/com/navercorp/pinpoint/plugin/jdbc/postgresql/PostgreSql_9_4_1207_IT.java @@ -25,15 +25,12 @@ import com.navercorp.pinpoint.test.plugin.JvmVersion; import com.navercorp.pinpoint.test.plugin.PinpointAgent; import com.navercorp.pinpoint.test.plugin.PinpointPluginTestSuite; -import com.navercorp.pinpoint.test.plugin.shared.BeforeSharedClass; - -import org.junit.Assume; +import com.navercorp.pinpoint.test.plugin.shared.SharedTestLifeCycleClass; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import org.junit.BeforeClass; import org.junit.Test; import org.junit.runner.RunWith; -import org.apache.logging.log4j.Logger; -import org.apache.logging.log4j.LogManager; -import org.testcontainers.DockerClientFactory; /** * @author HyunGil Jeong @@ -44,25 +41,16 @@ @Dependency({"org.postgresql:postgresql:[9.4.1207,9.4.1208)", "log4j:log4j:1.2.16", "org.slf4j:slf4j-log4j12:1.7.5", JDBCTestConstants.VERSION, TestcontainersOption.TEST_CONTAINER, TestcontainersOption.POSTGRESQL}) +@SharedTestLifeCycleClass(PostgreSqlServer.class) public class PostgreSql_9_4_1207_IT extends PostgreSqlBase { - private static final Logger logger = LogManager.getLogger(PostgreSql_9_4_1207_IT.class); + private final Logger logger = LogManager.getLogger(getClass()); private static PostgreSqlItHelper HELPER; private static PostgreSqlJDBCDriverClass driverClass; private static PostgreSqlJDBCApi jdbcApi; - @BeforeSharedClass - public static void sharedSetup() throws Exception { - Assume.assumeTrue("Docker not enabled", DockerClientFactory.instance().isDockerAvailable()); - container = PostgreSQLContainerFactory.newContainer(logger.getName()); - - container.start(); - setJdbcUrl(container.getJdbcUrl()); - setUserName(container.getUsername()); - setPassWord(container.getPassword()); - } @BeforeClass public static void beforeClass() throws Exception { diff --git a/plugins-it/postgresql-jdbc-it/src/test/java/com/navercorp/pinpoint/plugin/jdbc/postgresql/PostgreSql_9_x_to_9_4_1207_IT.java b/plugins-it/postgresql-jdbc-it/src/test/java/com/navercorp/pinpoint/plugin/jdbc/postgresql/PostgreSql_9_x_to_9_4_1207_IT.java index abaac806b956..4bb51ee07d43 100644 --- a/plugins-it/postgresql-jdbc-it/src/test/java/com/navercorp/pinpoint/plugin/jdbc/postgresql/PostgreSql_9_x_to_9_4_1207_IT.java +++ b/plugins-it/postgresql-jdbc-it/src/test/java/com/navercorp/pinpoint/plugin/jdbc/postgresql/PostgreSql_9_x_to_9_4_1207_IT.java @@ -25,15 +25,12 @@ import com.navercorp.pinpoint.test.plugin.JvmVersion; import com.navercorp.pinpoint.test.plugin.PinpointAgent; import com.navercorp.pinpoint.test.plugin.PinpointPluginTestSuite; -import com.navercorp.pinpoint.test.plugin.shared.BeforeSharedClass; - -import org.junit.Assume; +import com.navercorp.pinpoint.test.plugin.shared.SharedTestLifeCycleClass; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import org.junit.BeforeClass; import org.junit.Test; import org.junit.runner.RunWith; -import org.apache.logging.log4j.Logger; -import org.apache.logging.log4j.LogManager; -import org.testcontainers.DockerClientFactory; /** * @author HyunGil Jeong @@ -44,24 +41,15 @@ @Dependency({"org.postgresql:postgresql:[9.min,9.4.1207)", "log4j:log4j:1.2.16", "org.slf4j:slf4j-log4j12:1.7.5", JDBCTestConstants.VERSION, TestcontainersOption.TEST_CONTAINER, TestcontainersOption.POSTGRESQL}) +@SharedTestLifeCycleClass(PostgreSqlServer.class) public class PostgreSql_9_x_to_9_4_1207_IT extends PostgreSqlBase { - private static final Logger logger = LogManager.getLogger(PostgreSql_9_x_to_9_4_1207_IT.class); + private final Logger logger = LogManager.getLogger(getClass()); private static PostgreSqlItHelper HELPER; private static PostgreSqlJDBCDriverClass driverClass; private static PostgreSqlJDBCApi jdbcApi; - @BeforeSharedClass - public static void sharedSetup() { - Assume.assumeTrue("Docker not enabled", DockerClientFactory.instance().isDockerAvailable()); - container = PostgreSQLContainerFactory.newContainer(logger.getName()); - - container.start(); - setJdbcUrl(container.getJdbcUrl()); - setUserName(container.getUsername()); - setPassWord(container.getPassword()); - } @BeforeClass public static void beforeClass() { diff --git a/plugins-it/postgresql-jdbc-it/src/test/java/com/navercorp/pinpoint/plugin/jdbc/postgresql/PostgreSql_Post_9_4_1208_IT.java b/plugins-it/postgresql-jdbc-it/src/test/java/com/navercorp/pinpoint/plugin/jdbc/postgresql/PostgreSql_Post_9_4_1208_IT.java index 7544343ffbef..501667288694 100644 --- a/plugins-it/postgresql-jdbc-it/src/test/java/com/navercorp/pinpoint/plugin/jdbc/postgresql/PostgreSql_Post_9_4_1208_IT.java +++ b/plugins-it/postgresql-jdbc-it/src/test/java/com/navercorp/pinpoint/plugin/jdbc/postgresql/PostgreSql_Post_9_4_1208_IT.java @@ -25,16 +25,14 @@ import com.navercorp.pinpoint.test.plugin.JvmVersion; import com.navercorp.pinpoint.test.plugin.PinpointAgent; import com.navercorp.pinpoint.test.plugin.PinpointPluginTestSuite; -import com.navercorp.pinpoint.test.plugin.shared.BeforeSharedClass; -import org.junit.AfterClass; +import com.navercorp.pinpoint.test.plugin.shared.SharedTestLifeCycleClass; import org.junit.Assume; import org.junit.BeforeClass; import org.junit.Test; import org.junit.runner.RunWith; import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.LogManager; -import org.testcontainers.DockerClientFactory; import java.net.URL; @@ -48,25 +46,16 @@ // JDBCTestConstants.VERSION, TestcontainersOption.TEST_CONTAINER, TestcontainersOption.POSTGRESQL}) @Dependency({"org.postgresql:postgresql:[9.4.1208,)", JDBCTestConstants.VERSION, TestcontainersOption.TEST_CONTAINER, TestcontainersOption.POSTGRESQL}) +@SharedTestLifeCycleClass(PostgreSqlServer.class) public class PostgreSql_Post_9_4_1208_IT extends PostgreSqlBase { - private static final Logger logger = LogManager.getLogger(PostgreSql_Post_9_4_1208_IT.class); + private final Logger logger = LogManager.getLogger(getClass()); private static PostgreSqlItHelper HELPER; private static PostgreSqlJDBCDriverClass driverClass; private static PostgreSqlJDBCApi jdbcApi; - @BeforeSharedClass - public static void sharedSetup() throws Exception { - Assume.assumeTrue("Docker not enabled", DockerClientFactory.instance().isDockerAvailable()); - container = PostgreSQLContainerFactory.newContainer(logger.getName()); - - container.start(); - setJdbcUrl(container.getJdbcUrl()); - setUserName(container.getUsername()); - setPassWord(container.getPassword()); - } @BeforeClass public static void beforeClass() throws Exception { @@ -88,13 +77,6 @@ private static void invalidJarCheck() { Assume.assumeTrue("test skip : invalid jar ", jar != null); } - @AfterClass - public static void afterClass() throws Exception { - if (container != null) { - container.stop(); - } - } - @Override protected JDBCDriverClass getJDBCDriverClass() { return driverClass;