Skip to content

Commit

Permalink
Refactor tests to reduce duplication
Browse files Browse the repository at this point in the history
Add test to cover #736
  • Loading branch information
Richard North committed May 15, 2019
1 parent bfe10a6 commit 6f4791b
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 136 deletions.
2 changes: 1 addition & 1 deletion modules/jdbc-test/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ dependencies {

testCompile 'com.google.guava:guava:18.0'
testCompile 'org.postgresql:postgresql:42.0.0'
testCompile 'mysql:mysql-connector-java:5.1.45'
testCompile 'mysql:mysql-connector-java:8.0.14'
testCompile 'org.mariadb.jdbc:mariadb-java-client:1.4.6'
testCompile 'com.oracle:ojdbc6:12.1.0.1-atlassian-hosted'
testCompile 'com.microsoft.sqlserver:mssql-jdbc:6.1.0.jre8'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package org.testcontainers.junit;

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.testcontainers.containers.JdbcDatabaseContainer;

import javax.sql.DataSource;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
* TODO: Javadocs
*/
abstract class AbstractContainerDatabaseTest {

ResultSet performQuery(JdbcDatabaseContainer container, String sql) throws SQLException {
DataSource ds = getDataSource(container);
Statement statement = ds.getConnection().createStatement();
statement.execute(sql);
ResultSet resultSet = statement.getResultSet();

resultSet.next();
return resultSet;
}

DataSource getDataSource(JdbcDatabaseContainer container) {
HikariConfig hikariConfig = new HikariConfig();
hikariConfig.setJdbcUrl(container.getJdbcUrl());
hikariConfig.setUsername(container.getUsername());
hikariConfig.setPassword(container.getPassword());

return new HikariDataSource(hikariConfig);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package org.testcontainers.junit;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.testcontainers.containers.MySQLContainer;

import java.sql.ResultSet;
import java.sql.SQLException;

import static org.rnorth.visibleassertions.VisibleAssertions.assertEquals;

@RunWith(Parameterized.class)
public class MultiVersionMySQLTest extends AbstractContainerDatabaseTest {

@Parameterized.Parameters(name = "{0}")
public static String[] params() {
return new String[]{"5.5.62", "5.6.42", "5.7.26", "8.0.16"};
}

private final String version;

public MultiVersionMySQLTest(String version) {
this.version = version;
}

@Test
public void versionCheckTest() throws SQLException {
try (final MySQLContainer container = new MySQLContainer<>("mysql:" + version)) {
container.start();
final ResultSet resultSet = performQuery(container, "SELECT VERSION()");
final String resultSetString = resultSet.getString(1);

assertEquals("The database version can be set using a container rule parameter", version, resultSetString);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,33 +1,22 @@
package org.testcontainers.junit;

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.junit.Rule;
import org.junit.Test;
import org.testcontainers.containers.ClickHouseContainer;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import static org.rnorth.visibleassertions.VisibleAssertions.assertEquals;

public class SimpleClickhouseTest {
public class SimpleClickhouseTest extends AbstractContainerDatabaseTest {

@Rule
public ClickHouseContainer clickhouse = new ClickHouseContainer();

@Test
public void testSimple() throws SQLException {
HikariConfig hikariConfig = new HikariConfig();
hikariConfig.setJdbcUrl(clickhouse.getJdbcUrl());
hikariConfig.setUsername(clickhouse.getUsername());
hikariConfig.setPassword(clickhouse.getPassword());

HikariDataSource ds = new HikariDataSource(hikariConfig);
Statement statement = ds.getConnection().createStatement();
statement.execute("SELECT 1");
ResultSet resultSet = statement.getResultSet();
ResultSet resultSet = performQuery(clickhouse, "SELECT 1");

resultSet.next();
int resultSetInt = resultSet.getInt(1);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package org.testcontainers.junit;

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.junit.Rule;
import org.junit.Test;
import org.testcontainers.containers.MSSQLServerContainer;

import javax.sql.DataSource;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
Expand All @@ -15,22 +14,14 @@
/**
* @author Stefan Hufschmidt
*/
public class SimpleMSSQLServerTest {
public class SimpleMSSQLServerTest extends AbstractContainerDatabaseTest {

@Rule
public MSSQLServerContainer mssqlServer = new MSSQLServerContainer();

@Test
public void testSimple() throws SQLException {
HikariConfig hikariConfig = new HikariConfig();
hikariConfig.setJdbcUrl(mssqlServer.getJdbcUrl());
hikariConfig.setUsername(mssqlServer.getUsername());
hikariConfig.setPassword(mssqlServer.getPassword());

HikariDataSource ds = new HikariDataSource(hikariConfig);
Statement statement = ds.getConnection().createStatement();
statement.execute("SELECT 1");
ResultSet resultSet = statement.getResultSet();
ResultSet resultSet = performQuery(mssqlServer, "SELECT 1");

resultSet.next();
int resultSetInt = resultSet.getInt(1);
Expand All @@ -39,12 +30,7 @@ public void testSimple() throws SQLException {

@Test
public void testSetupDatabase() throws SQLException {
HikariConfig hikariConfig = new HikariConfig();
hikariConfig.setJdbcUrl(mssqlServer.getJdbcUrl());
hikariConfig.setUsername(mssqlServer.getUsername());
hikariConfig.setPassword(mssqlServer.getPassword());

HikariDataSource ds = new HikariDataSource(hikariConfig);
DataSource ds = getDataSource(mssqlServer);
Statement statement = ds.getConnection().createStatement();
statement.executeUpdate("CREATE DATABASE [test];");
statement = ds.getConnection().createStatement();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,21 @@
package org.testcontainers.junit;

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import lombok.NonNull;

import org.apache.commons.lang.SystemUtils;
import org.junit.Test;
import org.testcontainers.containers.MariaDBContainer;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import static org.junit.Assume.assumeFalse;
import static org.rnorth.visibleassertions.VisibleAssertions.assertEquals;
import static org.rnorth.visibleassertions.VisibleAssertions.assertTrue;
import static org.junit.Assume.assumeFalse;


/**
* @author Miguel Gonzalez Sanchez
*/
public class SimpleMariaDBTest {
public class SimpleMariaDBTest extends AbstractContainerDatabaseTest {

@Test
public void testSimple() throws SQLException {
Expand Down Expand Up @@ -85,20 +80,4 @@ public void testMariaDBWithCommandOverride() throws SQLException {
mariadbCustomConfig.stop();
}
}

@NonNull
protected ResultSet performQuery(MariaDBContainer containerRule, String sql) throws SQLException {
HikariConfig hikariConfig = new HikariConfig();
hikariConfig.setJdbcUrl(containerRule.getJdbcUrl());
hikariConfig.setUsername(containerRule.getUsername());
hikariConfig.setPassword(containerRule.getPassword());

HikariDataSource ds = new HikariDataSource(hikariConfig);
Statement statement = ds.getConnection().createStatement();
statement.execute(sql);
ResultSet resultSet = statement.getResultSet();

resultSet.next();
return resultSet;
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,5 @@
package org.testcontainers.junit;

import static org.junit.Assert.fail;
import static org.junit.Assume.assumeFalse;
import static org.rnorth.visibleassertions.VisibleAssertions.assertEquals;
import static org.rnorth.visibleassertions.VisibleAssertions.assertTrue;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;

import org.apache.commons.lang.SystemUtils;
import org.junit.Test;
import org.slf4j.Logger;
Expand All @@ -20,13 +8,19 @@
import org.testcontainers.containers.MySQLContainer;
import org.testcontainers.containers.output.Slf4jLogConsumer;

import lombok.NonNull;
import java.sql.ResultSet;
import java.sql.SQLException;

import static org.junit.Assert.fail;
import static org.junit.Assume.assumeFalse;
import static org.rnorth.visibleassertions.VisibleAssertions.assertEquals;
import static org.rnorth.visibleassertions.VisibleAssertions.assertTrue;


/**
* @author richardnorth
*/
public class SimpleMySQLTest {
public class SimpleMySQLTest extends AbstractContainerDatabaseTest {

private static final Logger logger = LoggerFactory.getLogger(SimpleMySQLTest.class);

Expand Down Expand Up @@ -114,24 +108,6 @@ public void testCommandOverride() throws SQLException {

}

@Test
public void testMySQL8() throws SQLException {
assumeFalse(SystemUtils.IS_OS_WINDOWS);
MySQLContainer container = new MySQLContainer<>("mysql:8.0.11")
.withCommand("mysqld --default-authentication-plugin=mysql_native_password");
container.start();

try {
ResultSet resultSet = performQuery(container, "SELECT VERSION()");
String resultSetString = resultSet.getString(1);

assertTrue("The database version can be set using a container rule parameter", "8.0.11".equals(resultSetString));
}
finally {
container.stop();
}
}

@Test
public void testExplicitInitScript() throws SQLException {
try (MySQLContainer container = (MySQLContainer) new MySQLContainer()
Expand Down Expand Up @@ -160,21 +136,4 @@ public void testEmptyPasswordWithNonRootUser() {
container.stop();
}
}

@NonNull
protected ResultSet performQuery(MySQLContainer containerRule, String sql) throws SQLException {
HikariConfig hikariConfig = new HikariConfig();
hikariConfig.setDriverClassName(containerRule.getDriverClassName());
hikariConfig.setJdbcUrl(containerRule.getJdbcUrl());
hikariConfig.setUsername(containerRule.getUsername());
hikariConfig.setPassword(containerRule.getPassword());

HikariDataSource ds = new HikariDataSource(hikariConfig);
Statement statement = ds.getConnection().createStatement();
statement.execute(sql);
ResultSet resultSet = statement.getResultSet();

resultSet.next();
return resultSet;
}
}
Original file line number Diff line number Diff line change
@@ -1,41 +1,31 @@
package org.testcontainers.junit;

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.testcontainers.containers.OracleContainer;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import static org.rnorth.visibleassertions.VisibleAssertions.assertEquals;

/**
* @author gusohal
*/
@Ignore
public class SimpleOracleTest {
public class SimpleOracleTest extends AbstractContainerDatabaseTest {

@Rule
public OracleContainer oracle = new OracleContainer();

@Test
public void testSimple() throws SQLException {
HikariConfig hikariConfig = new HikariConfig();
hikariConfig.setJdbcUrl(oracle.getJdbcUrl());
hikariConfig.setUsername(oracle.getUsername());
hikariConfig.setPassword(oracle.getPassword());

HikariDataSource ds = new HikariDataSource(hikariConfig);
Statement statement = ds.getConnection().createStatement();
statement.execute("SELECT 1 FROM dual");
ResultSet resultSet = statement.getResultSet();
ResultSet resultSet = performQuery(oracle, "SELECT 1 FROM dual");

resultSet.next();
int resultSetInt = resultSet.getInt(1);

assertEquals("A basic SELECT query succeeds", 1, resultSetInt);
}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
package org.testcontainers.junit;

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.junit.Test;
import org.testcontainers.containers.JdbcDatabaseContainer;
import org.testcontainers.containers.PostgreSQLContainer;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import static org.rnorth.visibleassertions.VisibleAssertions.assertEquals;

/**
* @author richardnorth
*/
public class SimplePostgreSQLTest {
public class SimplePostgreSQLTest extends AbstractContainerDatabaseTest {

@Test
public void testSimple() throws SQLException {
Expand All @@ -41,18 +37,4 @@ public void testExplicitInitScript() throws SQLException {
assertEquals("Value from init script should equal real value", "hello world", firstColumnValue);
}
}

private ResultSet performQuery(JdbcDatabaseContainer container, String sql) throws SQLException {
HikariConfig hikariConfig = new HikariConfig();
hikariConfig.setJdbcUrl(container.getJdbcUrl());
hikariConfig.setUsername(container.getUsername());
hikariConfig.setPassword(container.getPassword());

HikariDataSource ds = new HikariDataSource(hikariConfig);
Statement statement = ds.getConnection().createStatement();
statement.execute(sql);
ResultSet resultSet = statement.getResultSet();
resultSet.next();
return resultSet;
}
}

0 comments on commit 6f4791b

Please sign in to comment.