-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor tests to reduce duplication
Add test to cover #736
- Loading branch information
Richard North
committed
May 15, 2019
1 parent
bfe10a6
commit 6f4791b
Showing
9 changed files
with
93 additions
and
136 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
modules/jdbc-test/src/test/java/org/testcontainers/junit/AbstractContainerDatabaseTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
modules/jdbc-test/src/test/java/org/testcontainers/junit/MultiVersionMySQLTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
15 changes: 2 additions & 13 deletions
15
modules/jdbc-test/src/test/java/org/testcontainers/junit/SimpleClickhouseTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 3 additions & 13 deletions
16
modules/jdbc-test/src/test/java/org/testcontainers/junit/SimpleOracleTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters