From 675fd1b62695397b6325eec34daf1d223afac63e Mon Sep 17 00:00:00 2001 From: emeroad Date: Fri, 24 Jun 2022 19:34:18 +0900 Subject: [PATCH] [#8965] Cleanup --- .../pinpoint/plugin/jdbc/MariaDBServer.java | 5 +-- .../pinpoint/plugin/jdbc/MariaDB_IT_Base.java | 3 +- .../pinpoint/plugin/jdbc/mssql/MSSqlIT.java | 7 ++-- .../plugin/jdbc/mssql/MsSqlServer.java | 7 +--- .../plugin/jdbc/mysql/MySqlServer.java | 5 +-- .../plugin/jdbc/mysql/MySql_IT_Base.java | 3 +- plugins-it/plugins-it-jdbc-test/pom.xml | 14 +++++++ .../pluginit/jdbc/DriverProperties.java | 1 + .../testcontainers/DatabaseContainers.java | 39 +++++++++++++++++++ .../jdbc/postgresql/PostgreSqlBase.java | 7 ++-- .../jdbc/postgresql/PostgreSqlServer.java | 7 +--- 11 files changed, 74 insertions(+), 24 deletions(-) create mode 100644 plugins-it/plugins-it-jdbc-test/src/main/java/com/navercorp/pinpoint/pluginit/jdbc/testcontainers/DatabaseContainers.java diff --git a/plugins-it/mariadb-jdbc-it/src/test/java/com/navercorp/pinpoint/plugin/jdbc/MariaDBServer.java b/plugins-it/mariadb-jdbc-it/src/test/java/com/navercorp/pinpoint/plugin/jdbc/MariaDBServer.java index ffbb970bb4db2..8671f358ffb41 100644 --- a/plugins-it/mariadb-jdbc-it/src/test/java/com/navercorp/pinpoint/plugin/jdbc/MariaDBServer.java +++ b/plugins-it/mariadb-jdbc-it/src/test/java/com/navercorp/pinpoint/plugin/jdbc/MariaDBServer.java @@ -1,5 +1,6 @@ package com.navercorp.pinpoint.plugin.jdbc; +import com.navercorp.pinpoint.pluginit.jdbc.testcontainers.DatabaseContainers; import com.navercorp.pinpoint.test.plugin.shared.SharedTestLifeCycle; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -38,10 +39,8 @@ public void accept(OutputFrame outputFrame) { mariaDB.start(); int port = mariaDB.getMappedPort(3306); - Properties properties = new Properties(); - properties.setProperty("JDBC_URL", mariaDB.getJdbcUrl()); + Properties properties = DatabaseContainers.toProperties(mariaDB); properties.setProperty("URL", mariaDB.getHost() + ":" + port); - return properties; } diff --git a/plugins-it/mariadb-jdbc-it/src/test/java/com/navercorp/pinpoint/plugin/jdbc/MariaDB_IT_Base.java b/plugins-it/mariadb-jdbc-it/src/test/java/com/navercorp/pinpoint/plugin/jdbc/MariaDB_IT_Base.java index 5a3768eb41724..e768cd490dc66 100644 --- a/plugins-it/mariadb-jdbc-it/src/test/java/com/navercorp/pinpoint/plugin/jdbc/MariaDB_IT_Base.java +++ b/plugins-it/mariadb-jdbc-it/src/test/java/com/navercorp/pinpoint/plugin/jdbc/MariaDB_IT_Base.java @@ -19,6 +19,7 @@ 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.pluginit.jdbc.testcontainers.DatabaseContainers; import com.navercorp.pinpoint.test.plugin.shared.SharedTestBeforeAllResult; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -80,7 +81,7 @@ public static String getURL() { @SharedTestBeforeAllResult public static void setBeforeAllResult(Properties beforeAllResult) { - JDBC_URL = beforeAllResult.getProperty("JDBC_URL"); + JDBC_URL = DatabaseContainers.getJdbcUrl(beforeAllResult); URL = beforeAllResult.getProperty("URL"); } diff --git a/plugins-it/mssql-jdbc-it/src/test/java/com/navercorp/pinpoint/plugin/jdbc/mssql/MSSqlIT.java b/plugins-it/mssql-jdbc-it/src/test/java/com/navercorp/pinpoint/plugin/jdbc/mssql/MSSqlIT.java index 07e29c935e85f..ca6b8807dff1a 100644 --- a/plugins-it/mssql-jdbc-it/src/test/java/com/navercorp/pinpoint/plugin/jdbc/mssql/MSSqlIT.java +++ b/plugins-it/mssql-jdbc-it/src/test/java/com/navercorp/pinpoint/plugin/jdbc/mssql/MSSqlIT.java @@ -22,6 +22,7 @@ import com.navercorp.pinpoint.pluginit.jdbc.JDBCApi; import com.navercorp.pinpoint.pluginit.jdbc.JDBCDriverClass; import com.navercorp.pinpoint.pluginit.jdbc.JDBCTestConstants; +import com.navercorp.pinpoint.pluginit.jdbc.testcontainers.DatabaseContainers; import com.navercorp.pinpoint.pluginit.utils.AgentPath; import com.navercorp.pinpoint.pluginit.utils.TestcontainersOption; import com.navercorp.pinpoint.test.plugin.Dependency; @@ -85,9 +86,9 @@ public static String getPassWord() { @SharedTestBeforeAllResult public static void setBeforeAllResult(Properties beforeAllResult) { - JDBC_URL = beforeAllResult.getProperty("JDBC_URL"); - USERNAME = beforeAllResult.getProperty("USERNAME"); - PASSWORD = beforeAllResult.getProperty("PASSWORD"); + JDBC_URL = DatabaseContainers.getJdbcUrl(beforeAllResult); + USERNAME = DatabaseContainers.getUsername(beforeAllResult); + PASSWORD = DatabaseContainers.getPassword(beforeAllResult); } @BeforeClass diff --git a/plugins-it/mssql-jdbc-it/src/test/java/com/navercorp/pinpoint/plugin/jdbc/mssql/MsSqlServer.java b/plugins-it/mssql-jdbc-it/src/test/java/com/navercorp/pinpoint/plugin/jdbc/mssql/MsSqlServer.java index deba1b2ef06ea..94509cbe89241 100644 --- a/plugins-it/mssql-jdbc-it/src/test/java/com/navercorp/pinpoint/plugin/jdbc/mssql/MsSqlServer.java +++ b/plugins-it/mssql-jdbc-it/src/test/java/com/navercorp/pinpoint/plugin/jdbc/mssql/MsSqlServer.java @@ -1,5 +1,6 @@ package com.navercorp.pinpoint.plugin.jdbc.mssql; +import com.navercorp.pinpoint.pluginit.jdbc.testcontainers.DatabaseContainers; import com.navercorp.pinpoint.test.plugin.shared.SharedTestLifeCycle; import org.junit.Assume; import org.slf4j.Logger; @@ -21,11 +22,7 @@ public Properties beforeAll() { mssqlserver = MSSQLServerContainerFactory.newMSSQLServerContainer(logger.getName()); mssqlserver.start(); - Properties properties = new Properties(); - properties.setProperty("JDBC_URL", mssqlserver.getJdbcUrl()); - properties.setProperty("USERNAME", mssqlserver.getUsername()); - properties.setProperty("PASSWORD", mssqlserver.getPassword()); - return properties; + return DatabaseContainers.toProperties(mssqlserver); } @Override diff --git a/plugins-it/mysql-jdbc-driver-plugin-it/src/test/java/com/navercorp/pinpoint/plugin/jdbc/mysql/MySqlServer.java b/plugins-it/mysql-jdbc-driver-plugin-it/src/test/java/com/navercorp/pinpoint/plugin/jdbc/mysql/MySqlServer.java index 2d3d0c9a4000b..0e97d4a2c98db 100644 --- a/plugins-it/mysql-jdbc-driver-plugin-it/src/test/java/com/navercorp/pinpoint/plugin/jdbc/mysql/MySqlServer.java +++ b/plugins-it/mysql-jdbc-driver-plugin-it/src/test/java/com/navercorp/pinpoint/plugin/jdbc/mysql/MySqlServer.java @@ -1,5 +1,6 @@ package com.navercorp.pinpoint.plugin.jdbc.mysql; +import com.navercorp.pinpoint.pluginit.jdbc.testcontainers.DatabaseContainers; import com.navercorp.pinpoint.test.plugin.shared.SharedTestLifeCycle; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -40,9 +41,7 @@ public void accept(OutputFrame outputFrame) { mysqlDB.withUrlParam("useSSL", "false"); mysqlDB.start(); - Properties properties = new Properties(); - properties.setProperty("JDBC_URL", mysqlDB.getJdbcUrl()); - return properties; + return DatabaseContainers.toProperties(mysqlDB); } @Override diff --git a/plugins-it/mysql-jdbc-driver-plugin-it/src/test/java/com/navercorp/pinpoint/plugin/jdbc/mysql/MySql_IT_Base.java b/plugins-it/mysql-jdbc-driver-plugin-it/src/test/java/com/navercorp/pinpoint/plugin/jdbc/mysql/MySql_IT_Base.java index bed42bda0e4fc..344e5436f0209 100644 --- a/plugins-it/mysql-jdbc-driver-plugin-it/src/test/java/com/navercorp/pinpoint/plugin/jdbc/mysql/MySql_IT_Base.java +++ b/plugins-it/mysql-jdbc-driver-plugin-it/src/test/java/com/navercorp/pinpoint/plugin/jdbc/mysql/MySql_IT_Base.java @@ -19,6 +19,7 @@ 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.pluginit.jdbc.testcontainers.DatabaseContainers; import com.navercorp.pinpoint.test.plugin.shared.SharedTestBeforeAllResult; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -47,7 +48,7 @@ public static String getJdbcUrl() { @SharedTestBeforeAllResult public static void setBeforeAllResult(Properties beforeAllResult) { - JDBC_URL = beforeAllResult.getProperty("JDBC_URL"); + JDBC_URL = DatabaseContainers.getJdbcUrl(beforeAllResult); } diff --git a/plugins-it/plugins-it-jdbc-test/pom.xml b/plugins-it/plugins-it-jdbc-test/pom.xml index a68d05af96181..092b8735e931c 100644 --- a/plugins-it/plugins-it-jdbc-test/pom.xml +++ b/plugins-it/plugins-it-jdbc-test/pom.xml @@ -49,6 +49,20 @@ junit provided + + org.testcontainers + testcontainers + ${testcontainers.version} + true + compile + + + org.testcontainers + jdbc + ${testcontainers.version} + true + compile + diff --git a/plugins-it/plugins-it-jdbc-test/src/main/java/com/navercorp/pinpoint/pluginit/jdbc/DriverProperties.java b/plugins-it/plugins-it-jdbc-test/src/main/java/com/navercorp/pinpoint/pluginit/jdbc/DriverProperties.java index 07ef084a6089e..9b739c529935b 100644 --- a/plugins-it/plugins-it-jdbc-test/src/main/java/com/navercorp/pinpoint/pluginit/jdbc/DriverProperties.java +++ b/plugins-it/plugins-it-jdbc-test/src/main/java/com/navercorp/pinpoint/pluginit/jdbc/DriverProperties.java @@ -17,6 +17,7 @@ package com.navercorp.pinpoint.pluginit.jdbc; import com.navercorp.pinpoint.common.util.PropertyUtils; +import org.testcontainers.containers.JdbcDatabaseContainer; import java.io.IOException; import java.util.Properties; diff --git a/plugins-it/plugins-it-jdbc-test/src/main/java/com/navercorp/pinpoint/pluginit/jdbc/testcontainers/DatabaseContainers.java b/plugins-it/plugins-it-jdbc-test/src/main/java/com/navercorp/pinpoint/pluginit/jdbc/testcontainers/DatabaseContainers.java new file mode 100644 index 0000000000000..75e46a37bc8f5 --- /dev/null +++ b/plugins-it/plugins-it-jdbc-test/src/main/java/com/navercorp/pinpoint/pluginit/jdbc/testcontainers/DatabaseContainers.java @@ -0,0 +1,39 @@ +package com.navercorp.pinpoint.pluginit.jdbc.testcontainers; + +import org.testcontainers.containers.JdbcDatabaseContainer; + +import java.util.Objects; +import java.util.Properties; + +public final class DatabaseContainers { + + public static final String JDBC_URL = "JDBC_URL"; + public static final String USERNAME = "USERNAME"; + public static final String PASSWORD = "PASSWORD"; + + private DatabaseContainers() { + } + + public static Properties toProperties(JdbcDatabaseContainer container) { + Objects.requireNonNull(container, "container"); + + Properties properties = new Properties(); + properties.setProperty(JDBC_URL, container.getJdbcUrl()); + properties.setProperty(USERNAME, container.getUsername()); + properties.setProperty(PASSWORD, container.getPassword()); + return properties; + } + + public static String getJdbcUrl(Properties p) { + return p.getProperty(JDBC_URL); + } + + public static String getUsername(Properties p) { + return p.getProperty(USERNAME); + } + + public static String getPassword(Properties p) { + return p.getProperty(PASSWORD); + } + +} 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 d0276c186f140..1660522729e5a 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,6 +19,7 @@ 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.pluginit.jdbc.testcontainers.DatabaseContainers; import com.navercorp.pinpoint.test.plugin.shared.SharedTestBeforeAllResult; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -55,9 +56,9 @@ public static String getPassWord() { @SharedTestBeforeAllResult public static void setBeforeAllResult(Properties beforeAllResult) { - JDBC_URL = beforeAllResult.getProperty("JDBC_URL"); - USERNAME = beforeAllResult.getProperty("USERNAME"); - PASSWORD = beforeAllResult.getProperty("PASSWORD"); + JDBC_URL = DatabaseContainers.getJdbcUrl(beforeAllResult); + USERNAME = DatabaseContainers.getUsername(beforeAllResult); + PASSWORD = DatabaseContainers.getPassword(beforeAllResult); } 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 index a54d85f17dd01..aa01bcbd97882 100644 --- 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 @@ -1,5 +1,6 @@ package com.navercorp.pinpoint.plugin.jdbc.postgresql; +import com.navercorp.pinpoint.pluginit.jdbc.testcontainers.DatabaseContainers; import com.navercorp.pinpoint.test.plugin.shared.SharedTestLifeCycle; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -22,11 +23,7 @@ public Properties beforeAll() { postgreSql.start(); - Properties properties = new Properties(); - properties.setProperty("JDBC_URL", postgreSql.getJdbcUrl()); - properties.setProperty("USERNAME", postgreSql.getUsername()); - properties.setProperty("PASSWORD", postgreSql.getPassword()); - return properties; + return DatabaseContainers.toProperties(postgreSql); } @Override