-
-
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.
Jdbc containers with user, passwd, and db customizations (#400)
- Loading branch information
1 parent
d2906fd
commit 56513a4
Showing
4 changed files
with
91 additions
and
7 deletions.
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
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,47 @@ | ||
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"; | ||
|
||
// Add MYSQL_ROOT_HOST environment so that we can root login from anywhere for testing purposes | ||
@Rule | ||
public MySQLContainer mysql = (MySQLContainer) new MySQLContainer("mysql:5.5") | ||
.withDatabaseName(DB_NAME) | ||
.withUsername(USER) | ||
.withPassword(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(); | ||
|
||
assertEquals("There is a result", resultSet.next(), true); | ||
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
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