Skip to content

Commit

Permalink
[pinpoint-apm#8965] Apply SharedTestLifeCycle to MySql
Browse files Browse the repository at this point in the history
  • Loading branch information
emeroad committed Jun 24, 2022
1 parent 43e96f3 commit e0ea4e8
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
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.SharedTestLifeCycleClass;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.junit.Assert;
Expand All @@ -49,6 +50,7 @@
@ImportPlugin("com.navercorp.pinpoint:pinpoint-mysql-jdbc-driver-plugin")
@Dependency({"mysql:mysql-connector-java:[5.1.0],[5.1.34],[5.1.36,5.max]", "log4j:log4j:1.2.16",
"org.slf4j:slf4j-log4j12:1.7.5", JDBCTestConstants.VERSION, TestcontainersOption.MYSQLDB})
@SharedTestLifeCycleClass(MySqlServer.class)
public class MySqlConnection_5_X_IT extends MySql_IT_Base {

private final Logger logger = LogManager.getLogger(this.getClass());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
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.SharedTestLifeCycleClass;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand All @@ -49,6 +50,7 @@
@ImportPlugin("com.navercorp.pinpoint:pinpoint-mysql-jdbc-driver-plugin")
@Dependency({"mysql:mysql-connector-java:[6.min,6.max]", "log4j:log4j:1.2.16",
"org.slf4j:slf4j-log4j12:1.7.5", JDBCTestConstants.VERSION, TestcontainersOption.MYSQLDB})
@SharedTestLifeCycleClass(MySqlServer.class)
public class MySqlConnection_6_X_IT extends MySql_IT_Base {

private final Logger logger = LogManager.getLogger(this.getClass());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ protected JDBCDriverClass getJDBCDriverClass() {

public static DriverProperties getDriverProperties() {
String loadbalance = UrlUtils.getLoadbalanceUrl(JDBC_URL);
return new DriverProperties(loadbalance, USERNAME, PASSWORD, new Properties());
return new DriverProperties(loadbalance, MySqlServer.USERNAME, MySqlServer.PASSWORD, new Properties());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
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.SharedTestLifeCycleClass;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand All @@ -54,6 +55,7 @@
@Dependency({"mysql:mysql-connector-java:[6.min,6.max]",
"log4j:log4j:1.2.16", "org.slf4j:slf4j-log4j12:1.7.5",
JDBCTestConstants.VERSION, TestcontainersOption.MYSQLDB})
@SharedTestLifeCycleClass(MySqlServer.class)
public class MySqlLoadBalance_6_X_IT extends MySql_IT_Base {

private final Logger logger = LogManager.getLogger(this.getClass());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.navercorp.pinpoint.plugin.jdbc.mysql;

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.MySQLContainer;
import org.testcontainers.containers.output.OutputFrame;

import java.util.Properties;
import java.util.function.Consumer;

public class MySqlServer implements SharedTestLifeCycle {
private final Logger logger = LogManager.getLogger(getClass());

public static final String DATABASE_NAME = "test";
public static final String USERNAME = "root";
public static final String PASSWORD = "";

private MySQLContainer mysqlDB = new MySQLContainer();

@Override
public Properties beforeAll() {
Assume.assumeTrue("Docker not enabled", DockerClientFactory.instance().isDockerAvailable());

mysqlDB = new MySQLContainer();
mysqlDB.withLogConsumer(new Consumer<OutputFrame>() {
@Override
public void accept(OutputFrame outputFrame) {
logger.info(outputFrame.getUtf8String());
}
});
mysqlDB.withDatabaseName(DATABASE_NAME);
mysqlDB.withUsername(USERNAME);
mysqlDB.withPassword(PASSWORD);
mysqlDB.withInitScript("init.sql");
// mysqlDB.
mysqlDB.withUrlParam("serverTimezone", "UTC");
mysqlDB.withUrlParam("useSSL", "false");
mysqlDB.start();

Properties properties = new Properties();
properties.setProperty("JDBC_URL", mysqlDB.getJdbcUrl());
return properties;
}

@Override
public void afterAll() {
if (mysqlDB != null) {
mysqlDB.stop();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
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.SharedTestLifeCycleClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand All @@ -41,6 +42,7 @@
@ImportPlugin("com.navercorp.pinpoint:pinpoint-mysql-jdbc-driver-plugin")
@Dependency({"mysql:mysql-connector-java:[5.min,5.1.9),[5.1.10,5.max]", "log4j:log4j:1.2.16",
"org.slf4j:slf4j-log4j12:1.7.5", JDBCTestConstants.VERSION, TestcontainersOption.MYSQLDB})
@SharedTestLifeCycleClass(MySqlServer.class)
public class MySql_5_X_IT extends MySql_IT_Base {

private static MySqlItHelper HELPER;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
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.SharedTestLifeCycleClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand All @@ -45,6 +46,7 @@
@Dependency({"mysql:mysql-connector-java:[6.min,6.max]",
"org.slf4j:slf4j-log4j12:1.7.5",
JDBCTestConstants.VERSION, TestcontainersOption.TEST_CONTAINER, TestcontainersOption.MYSQLDB})
@SharedTestLifeCycleClass(MySqlServer.class)
public class MySql_6_X_IT extends MySql_IT_Base {

private static MySqlItHelper HELPER;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
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.SharedTestLifeCycleClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand All @@ -41,6 +42,7 @@
@ImportPlugin("com.navercorp.pinpoint:pinpoint-mysql-jdbc-driver-plugin")
@Dependency({"mysql:mysql-connector-java:[8.0.11,)", "log4j:log4j:1.2.16",
"org.slf4j:slf4j-log4j12:1.7.5", JDBCTestConstants.VERSION, TestcontainersOption.MYSQLDB})
@SharedTestLifeCycleClass(MySqlServer.class)
public class MySql_8_X_IT extends MySql_IT_Base {

private static MySqlItHelper HELPER;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,88 +19,37 @@
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.BeforeSharedClass;
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.Assume;
import org.junit.Before;
import org.testcontainers.DockerClientFactory;
import org.testcontainers.containers.MySQLContainer;
import org.testcontainers.containers.output.OutputFrame;

import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
import java.util.function.Consumer;

/**
* @author Taejin Koo
*/
public abstract class MySql_IT_Base {
private static final Logger logger = LogManager.getLogger(MySql_IT_Base.class);
public static MySQLContainer mysqlDB = new MySQLContainer();
private final Logger logger = LogManager.getLogger(MySql_IT_Base.class);

protected static final String DATABASE_NAME = "test";
protected static final String USERNAME = "root";
protected static final String PASSWORD = "";


// ---------- For @BeforeSharedClass, @AfterSharedClass //
// for shared test'
protected static String JDBC_URL;
protected static String URL;
protected static final String USERNAME = MySqlServer.USERNAME;
protected static final String PASSWORD = MySqlServer.PASSWORD;

public static String getJdbcUrl() {
return JDBC_URL;
}

public static void setJdbcUrl(String jdbcUrl) {
JDBC_URL = jdbcUrl;
}

public static String getURL() {
return URL;
}

public static void setURL(String URL) {
MySql_IT_Base.URL = URL;
@SharedTestBeforeAllResult
public static void setBeforeAllResult(Properties beforeAllResult) {
JDBC_URL = beforeAllResult.getProperty("JDBC_URL");
}
// ---------- //

@BeforeSharedClass
public static void sharedSetUp() throws Exception {
Assume.assumeTrue("Docker not enabled", DockerClientFactory.instance().isDockerAvailable());

mysqlDB.withLogConsumer(new Consumer<OutputFrame>() {
@Override
public void accept(OutputFrame outputFrame) {
logger.info(outputFrame.getUtf8String());
}
});
mysqlDB.withDatabaseName(DATABASE_NAME);
mysqlDB.withUsername(USERNAME);
mysqlDB.withPassword(PASSWORD);
mysqlDB.withInitScript("init.sql");
// mysqlDB.
mysqlDB.withUrlParam("serverTimezone", "UTC");
mysqlDB.withUrlParam("useSSL", "false");
mysqlDB.start();

setJdbcUrl(mysqlDB.getJdbcUrl());
int port = mysqlDB.getMappedPort(3306);
setURL(mysqlDB.getHost() + ":" + port);
}

@AfterSharedClass
public static void sharedTearDown() throws Exception {
if (mysqlDB != null) {
mysqlDB.stop();
}
}

abstract JDBCDriverClass getJDBCDriverClass();

Expand All @@ -121,7 +70,7 @@ public void after() throws Exception {
}

public static DriverProperties getDriverProperties() {
return new DriverProperties(JDBC_URL, USERNAME, PASSWORD, new Properties());
return new DriverProperties(JDBC_URL, MySqlServer.USERNAME, MySqlServer.PASSWORD, new Properties());
}

}

0 comments on commit e0ea4e8

Please sign in to comment.