Skip to content

Commit

Permalink
[#8965] Apply SharedTestLifeCycle to PostSql
Browse files Browse the repository at this point in the history
  • Loading branch information
emeroad committed Jun 24, 2022
1 parent 2c0bbac commit d66ac86
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 88 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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 {
Expand All @@ -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;
Expand Down

0 comments on commit d66ac86

Please sign in to comment.