-
-
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.
Allow for mysql connection customizations
- Loading branch information
1 parent
e7c6ed2
commit 48658a3
Showing
2 changed files
with
101 additions
and
8 deletions.
There are no files selected for viewing
69 changes: 69 additions & 0 deletions
69
modules/jdbc-test/src/test/java/org/testcontainers/junit/CustomizableMySQLTest.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,69 @@ | ||
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.MySQLContainer; | ||
|
||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
import java.sql.Statement; | ||
|
||
import static org.rnorth.visibleassertions.VisibleAssertions.assertEquals; | ||
|
||
public class CustomizableMySQLTest { | ||
private static final String DB_NAME = "foo"; | ||
private static final String USER = "bar"; | ||
private static final String PWD = "baz"; | ||
private static final String ROOT_PWD = "qux"; | ||
|
||
// Add MYSQL_ROOT_HOST environment so that we can root login from anywhere for testing purposes | ||
@Rule | ||
public MySQLContainer mysql = (MySQLContainer) new MySQLContainer("mysql:latest") | ||
.withDatabaseName(DB_NAME) | ||
.withUsername(USER) | ||
.withPassword(PWD) | ||
.withRootPassword(ROOT_PWD) | ||
.withEnv("MYSQL_ROOT_HOST", "%"); | ||
|
||
@Test | ||
public void testSimple() throws SQLException { | ||
HikariConfig hikariConfig = new HikariConfig(); | ||
hikariConfig.setJdbcUrl("jdbc:mysql://" | ||
+ mysql.getContainerIpAddress() | ||
+ ":" + mysql.getMappedPort(MySQLContainer.MYSQL_PORT) | ||
+ "/" + DB_NAME); | ||
hikariConfig.setUsername(USER); | ||
hikariConfig.setPassword(PWD); | ||
|
||
HikariDataSource ds = new HikariDataSource(hikariConfig); | ||
Statement statement = ds.getConnection().createStatement(); | ||
statement.execute("SELECT 1"); | ||
ResultSet resultSet = statement.getResultSet(); | ||
|
||
resultSet.next(); | ||
int resultSetInt = resultSet.getInt(1); | ||
assertEquals("A basic SELECT query succeeds", 1, resultSetInt); | ||
} | ||
|
||
@Test | ||
public void testRootSimple() throws SQLException { | ||
HikariConfig hikariConfig = new HikariConfig(); | ||
hikariConfig.setJdbcUrl("jdbc:mysql://" | ||
+ mysql.getContainerIpAddress() | ||
+ ":" + mysql.getMappedPort(MySQLContainer.MYSQL_PORT) | ||
+ "/" + DB_NAME); | ||
hikariConfig.setUsername("root"); | ||
hikariConfig.setPassword(ROOT_PWD); | ||
|
||
HikariDataSource ds = new HikariDataSource(hikariConfig); | ||
Statement statement = ds.getConnection().createStatement(); | ||
statement.execute("SELECT 1"); | ||
ResultSet resultSet = statement.getResultSet(); | ||
|
||
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