forked from testcontainers/testcontainers-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
578 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,4 +7,5 @@ Krystian Nowak <[email protected]> | |
Viktor Schulz <[email protected]> | ||
Asaf Mesika <[email protected]> | ||
Sergei Egorov <[email protected]> | ||
Pete Cornish <[email protected]> | ||
Pete Cornish <[email protected]> | ||
Miguel Gonzalez Sanchez <[email protected]> |
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,59 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<parent> | ||
<groupId>org.testcontainers</groupId> | ||
<artifactId>testcontainers-parent</artifactId> | ||
<version>1.1.6-SNAPSHOT</version> | ||
<relativePath>../../pom.xml</relativePath> | ||
</parent> | ||
|
||
<artifactId>mariadb</artifactId> | ||
<name>TestContainers :: JDBC :: MariaDB</name> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>${project.groupId}</groupId> | ||
<artifactId>testcontainers</artifactId> | ||
<version>${project.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>${project.groupId}</groupId> | ||
<artifactId>jdbc</artifactId> | ||
<version>${project.version}</version> | ||
</dependency> | ||
|
||
<!-- Database driver for testing --> | ||
<dependency> | ||
<groupId>org.mariadb.jdbc</groupId> | ||
<artifactId>mariadb-java-client</artifactId> | ||
<version>1.4.6</version> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
<!-- Database connection pool for testing --> | ||
<dependency> | ||
<groupId>com.zaxxer</groupId> | ||
<artifactId>HikariCP-java6</artifactId> | ||
<version>2.3.8</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>commons-dbutils</groupId> | ||
<artifactId>commons-dbutils</artifactId> | ||
<version>1.6</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.apache.tomcat</groupId> | ||
<artifactId>tomcat-jdbc</artifactId> | ||
<version>8.5.4</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.vibur</groupId> | ||
<artifactId>vibur-dbcp</artifactId> | ||
<version>9.0</version> | ||
</dependency> | ||
</dependencies> | ||
</project> |
73 changes: 73 additions & 0 deletions
73
modules/mariadb/src/main/java/org/testcontainers/containers/MariaDBContainer.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,73 @@ | ||
package org.testcontainers.containers; | ||
|
||
/** | ||
* Container implementation for the MariaDB project. | ||
* | ||
* @author Miguel Gonzalez Sanchez | ||
*/ | ||
public class MariaDBContainer<SELF extends MariaDBContainer<SELF>> extends JdbcDatabaseContainer<SELF> { | ||
|
||
public static final String NAME = "mariadb"; | ||
public static final String IMAGE = "mariadb"; | ||
private static final Integer MARIADB_PORT = 3306; | ||
private static final String MARIADB_USER = "test"; | ||
private static final String MARIADB_PASSWORD = "test"; | ||
private static final String MARIADB_DATABASE = "test"; | ||
private static final String MY_CNF_CONFIG_OVERRIDE_PARAM_NAME = "TC_MY_CNF"; | ||
|
||
public MariaDBContainer() { | ||
super(IMAGE + ":latest"); | ||
} | ||
|
||
public MariaDBContainer(String dockerImageName) { | ||
super(dockerImageName); | ||
} | ||
|
||
@Override | ||
protected Integer getLivenessCheckPort() { | ||
return getMappedPort(MARIADB_PORT); | ||
} | ||
|
||
@Override | ||
protected void configure() { | ||
optionallyMapResourceParameterAsVolume(MY_CNF_CONFIG_OVERRIDE_PARAM_NAME, "/etc/mysql/conf.d"); | ||
|
||
addExposedPort(MARIADB_PORT); | ||
addEnv("MYSQL_DATABASE", MARIADB_DATABASE); | ||
addEnv("MYSQL_USER", MARIADB_USER); | ||
addEnv("MYSQL_PASSWORD", MARIADB_PASSWORD); | ||
addEnv("MYSQL_ROOT_PASSWORD", MARIADB_PASSWORD); | ||
setCommand("mysqld"); | ||
setStartupAttempts(3); | ||
} | ||
|
||
@Override | ||
public String getDriverClassName() { | ||
return "org.mariadb.jdbc.Driver"; | ||
} | ||
|
||
@Override | ||
public String getJdbcUrl() { | ||
return "jdbc:mariadb://" + getContainerIpAddress() + ":" + getMappedPort(MARIADB_PORT) + "/test"; | ||
} | ||
|
||
@Override | ||
public String getUsername() { | ||
return MARIADB_USER; | ||
} | ||
|
||
@Override | ||
public String getPassword() { | ||
return MARIADB_PASSWORD; | ||
} | ||
|
||
@Override | ||
public String getTestQueryString() { | ||
return "SELECT 1"; | ||
} | ||
|
||
public SELF withConfigurationOverride(String s) { | ||
parameters.put(MY_CNF_CONFIG_OVERRIDE_PARAM_NAME, s); | ||
return self(); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
modules/mariadb/src/main/java/org/testcontainers/containers/MariaDBContainerProvider.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,16 @@ | ||
package org.testcontainers.containers; | ||
|
||
/** | ||
* Factory for MariaDB containers. | ||
*/ | ||
public class MariaDBContainerProvider extends JdbcDatabaseContainerProvider { | ||
@Override | ||
public boolean supports(String databaseType) { | ||
return databaseType.equals(MariaDBContainer.NAME); | ||
} | ||
|
||
@Override | ||
public JdbcDatabaseContainer newInstance(String tag) { | ||
return new MariaDBContainer(MariaDBContainer.IMAGE + ":" + tag); | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
...n/resources/META-INF/services/org.testcontainers.containers.JdbcDatabaseContainerProvider
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 @@ | ||
org.testcontainers.containers.MariaDBContainerProvider |
133 changes: 133 additions & 0 deletions
133
modules/mariadb/src/test/java/org/testcontainers/jdbc/JDBCDriverTest.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,133 @@ | ||
package org.testcontainers.jdbc; | ||
|
||
import com.zaxxer.hikari.HikariConfig; | ||
import com.zaxxer.hikari.HikariDataSource; | ||
import org.apache.commons.dbutils.QueryRunner; | ||
import org.apache.commons.dbutils.ResultSetHandler; | ||
import org.apache.commons.lang.SystemUtils; | ||
import org.junit.After; | ||
import org.junit.Test; | ||
|
||
import java.sql.Connection; | ||
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; | ||
|
||
/** | ||
* | ||
*/ | ||
public class JDBCDriverTest { | ||
|
||
@After | ||
public void testCleanup() { | ||
ContainerDatabaseDriver.killContainers(); | ||
} | ||
|
||
@Test | ||
public void testMariaDBWithVersion() throws SQLException { | ||
performSimpleTest("jdbc:tc:mariadb:10.1.16://hostname/databasename"); | ||
} | ||
|
||
@Test | ||
public void testMariaDBWithNoSpecifiedVersion() throws SQLException { | ||
performSimpleTest("jdbc:tc:mariadb://hostname/databasename"); | ||
} | ||
|
||
@Test | ||
public void testMariaDBWithCustomIniFile() throws SQLException { | ||
assumeFalse(SystemUtils.IS_OS_WINDOWS); | ||
HikariDataSource ds = getDataSource("jdbc:tc:mariadb:10.1.16://hostname/databasename?TC_MY_CNF=somepath/mariadb_conf_override", 1); | ||
Statement statement = ds.getConnection().createStatement(); | ||
statement.execute("SELECT @@GLOBAL.innodb_file_format"); | ||
ResultSet resultSet = statement.getResultSet(); | ||
|
||
resultSet.next(); | ||
String result = resultSet.getString(1); | ||
|
||
assertEquals("The InnoDB file format has been set by the ini file content", "Barracuda", result); | ||
} | ||
|
||
@Test | ||
public void testMariaDBWithClasspathInitScript() throws SQLException { | ||
performSimpleTest("jdbc:tc:mariadb://hostname/databasename?TC_INITSCRIPT=somepath/init_mariadb.sql"); | ||
|
||
performTestForScriptedSchema("jdbc:tc:mariadb://hostname/databasename?TC_INITSCRIPT=somepath/init_mariadb.sql"); | ||
} | ||
|
||
@Test | ||
public void testMariaDBWithClasspathInitFunction() throws SQLException { | ||
performSimpleTest("jdbc:tc:mariadb://hostname/databasename?TC_INITFUNCTION=org.testcontainers.jdbc.JDBCDriverTest::sampleInitFunction"); | ||
|
||
performTestForScriptedSchema("jdbc:tc:mariadb://hostname/databasename?TC_INITFUNCTION=org.testcontainers.jdbc.JDBCDriverTest::sampleInitFunction"); | ||
} | ||
|
||
@Test | ||
public void testMariaDBWithQueryParams() throws SQLException { | ||
performSimpleTestWithCharacterSet("jdbc:tc:mariadb://hostname/databasename?useUnicode=yes&characterEncoding=utf8"); | ||
} | ||
|
||
private void performSimpleTest(String jdbcUrl) throws SQLException { | ||
HikariDataSource dataSource = getDataSource(jdbcUrl, 1); | ||
new QueryRunner(dataSource).query("SELECT 1", new ResultSetHandler<Object>() { | ||
@Override | ||
public Object handle(ResultSet rs) throws SQLException { | ||
rs.next(); | ||
int resultSetInt = rs.getInt(1); | ||
assertEquals("A basic SELECT query succeeds", 1, resultSetInt); | ||
return true; | ||
} | ||
}); | ||
dataSource.close(); | ||
} | ||
|
||
private void performTestForScriptedSchema(String jdbcUrl) throws SQLException { | ||
HikariDataSource dataSource = getDataSource(jdbcUrl, 1); | ||
new QueryRunner(dataSource).query("SELECT foo FROM bar WHERE foo LIKE '%world'", new ResultSetHandler<Object>() { | ||
@Override | ||
public Object handle(ResultSet rs) throws SQLException { | ||
rs.next(); | ||
String resultSetString = rs.getString(1); | ||
assertEquals("A basic SELECT query succeeds where the schema has been applied from a script", "hello world", resultSetString); | ||
return true; | ||
} | ||
}); | ||
dataSource.close(); | ||
} | ||
|
||
private void performSimpleTestWithCharacterSet(String jdbcUrl) throws SQLException { | ||
HikariDataSource dataSource = getDataSource(jdbcUrl, 1); | ||
new QueryRunner(dataSource).query("SHOW VARIABLES LIKE 'character\\_set\\_connection'", new ResultSetHandler<Object>() { | ||
@Override | ||
public Object handle(ResultSet rs) throws SQLException { | ||
rs.next(); | ||
String resultSetInt = rs.getString(2); | ||
assertEquals("Passing query parameters to set DB connection encoding is successful", "utf8", resultSetInt); | ||
return true; | ||
} | ||
}); | ||
dataSource.close(); | ||
} | ||
|
||
private HikariDataSource getDataSource(String jdbcUrl, int poolSize) { | ||
HikariConfig hikariConfig = new HikariConfig(); | ||
hikariConfig.setJdbcUrl(jdbcUrl); | ||
hikariConfig.setConnectionTestQuery("SELECT 1"); | ||
hikariConfig.setMinimumIdle(1); | ||
hikariConfig.setMaximumPoolSize(poolSize); | ||
|
||
return new HikariDataSource(hikariConfig); | ||
} | ||
|
||
public static void sampleInitFunction(Connection connection) throws SQLException { | ||
connection.createStatement().execute("CREATE TABLE bar (\n" + | ||
" foo VARCHAR(255)\n" + | ||
");"); | ||
connection.createStatement().execute("INSERT INTO bar (foo) VALUES ('hello world');"); | ||
connection.createStatement().execute("CREATE TABLE my_counter (\n" + | ||
" n INT\n" + | ||
");"); | ||
} | ||
} |
Oops, something went wrong.