1- package org .testcontainers .junit . mysql ;
1+ package org .testcontainers .mysql ;
22
33import org .junit .jupiter .api .Test ;
44import org .slf4j .Logger ;
55import org .slf4j .LoggerFactory ;
66import org .testcontainers .MySQLTestImages ;
77import org .testcontainers .containers .ContainerLaunchException ;
8- import org .testcontainers .containers .MySQLContainer ;
98import org .testcontainers .containers .output .Slf4jLogConsumer ;
109import org .testcontainers .db .AbstractContainerDatabaseTest ;
1110
3029import static org .assertj .core .api .Assertions .assertThatThrownBy ;
3130import static org .assertj .core .api .Assumptions .assumeThat ;
3231
33- class SimpleMySQLTest extends AbstractContainerDatabaseTest {
32+ class MySQLContainerTest extends AbstractContainerDatabaseTest {
3433
35- private static final Logger logger = LoggerFactory .getLogger (SimpleMySQLTest .class );
34+ private static final Logger logger = LoggerFactory .getLogger (MySQLContainerTest .class );
3635
3736 @ Test
3837 void testSimple () throws SQLException {
3938 try ( // container {
40- MySQLContainer <?> mysql = new MySQLContainer <> ("mysql:8.0.36" )
39+ MySQLContainer mysql = new MySQLContainer ("mysql:8.0.36" )
4140 // }
4241 ) {
4342 mysql .start ();
@@ -53,7 +52,7 @@ void testSimple() throws SQLException {
5352 @ Test
5453 void testSpecificVersion () throws SQLException {
5554 try (
56- MySQLContainer <?> mysqlOldVersion = new MySQLContainer <> (MySQLTestImages .MYSQL_80_IMAGE )
55+ MySQLContainer mysqlOldVersion = new MySQLContainer (MySQLTestImages .MYSQL_80_IMAGE )
5756 .withConfigurationOverride ("somepath/mysql_conf_override" )
5857 .withLogConsumer (new Slf4jLogConsumer (logger ))
5958 ) {
@@ -71,7 +70,7 @@ void testSpecificVersion() throws SQLException {
7170 @ Test
7271 void testMySQLWithCustomIniFile () throws SQLException {
7372 try (
74- MySQLContainer <?> mysqlCustomConfig = new MySQLContainer <> (MySQLTestImages .MYSQL_80_IMAGE )
73+ MySQLContainer mysqlCustomConfig = new MySQLContainer (MySQLTestImages .MYSQL_80_IMAGE )
7574 .withConfigurationOverride ("somepath/mysql_conf_override" )
7675 ) {
7776 mysqlCustomConfig .start ();
@@ -83,7 +82,7 @@ void testMySQLWithCustomIniFile() throws SQLException {
8382 @ Test
8483 void testCommandOverride () throws SQLException {
8584 try (
86- MySQLContainer <?> mysqlCustomConfig = new MySQLContainer <> (MySQLTestImages .MYSQL_80_IMAGE )
85+ MySQLContainer mysqlCustomConfig = new MySQLContainer (MySQLTestImages .MYSQL_80_IMAGE )
8786 .withCommand ("mysqld --auto_increment_increment=42" )
8887 ) {
8988 mysqlCustomConfig .start ();
@@ -98,7 +97,7 @@ void testCommandOverride() throws SQLException {
9897 @ Test
9998 void testExplicitInitScript () throws SQLException {
10099 try (
101- MySQLContainer <?> container = new MySQLContainer <> (MySQLTestImages .MYSQL_80_IMAGE )
100+ MySQLContainer container = new MySQLContainer (MySQLTestImages .MYSQL_80_IMAGE )
102101 .withInitScript ("somepath/init_mysql.sql" )
103102 .withLogConsumer (new Slf4jLogConsumer (logger ))
104103 ) {
@@ -114,7 +113,7 @@ void testExplicitInitScript() throws SQLException {
114113 @ Test
115114 void testEmptyPasswordWithNonRootUser () {
116115 try (
117- MySQLContainer <?> container = new MySQLContainer <> (MySQLTestImages .MYSQL_80_IMAGE )
116+ MySQLContainer container = new MySQLContainer (MySQLTestImages .MYSQL_80_IMAGE )
118117 .withDatabaseName ("TEST" )
119118 .withUsername ("test" )
120119 .withPassword ("" )
@@ -130,7 +129,7 @@ void testEmptyPasswordWithNonRootUser() {
130129 void testEmptyPasswordWithRootUser () throws SQLException {
131130 // Add MYSQL_ROOT_HOST environment so that we can root login from anywhere for testing purposes
132131 try (
133- MySQLContainer <?> mysql = new MySQLContainer <> (MySQLTestImages .MYSQL_80_IMAGE )
132+ MySQLContainer mysql = new MySQLContainer (MySQLTestImages .MYSQL_80_IMAGE )
134133 .withDatabaseName ("foo" )
135134 .withUsername ("root" )
136135 .withPassword ("" )
@@ -147,7 +146,7 @@ void testEmptyPasswordWithRootUser() throws SQLException {
147146
148147 @ Test
149148 void testWithAdditionalUrlParamTimeZone () throws SQLException {
150- MySQLContainer <?> mysql = new MySQLContainer <> (MySQLTestImages .MYSQL_80_IMAGE )
149+ MySQLContainer mysql = new MySQLContainer (MySQLTestImages .MYSQL_80_IMAGE )
151150 .withUrlParam ("serverTimezone" , "Europe/Zurich" )
152151 .withEnv ("TZ" , "Europe/Zurich" )
153152 .withLogConsumer (new Slf4jLogConsumer (logger ));
@@ -182,7 +181,7 @@ void testWithAdditionalUrlParamTimeZone() throws SQLException {
182181
183182 @ Test
184183 void testWithAdditionalUrlParamMultiQueries () throws SQLException {
185- MySQLContainer <?> mysql = new MySQLContainer <> (MySQLTestImages .MYSQL_80_IMAGE )
184+ MySQLContainer mysql = new MySQLContainer (MySQLTestImages .MYSQL_80_IMAGE )
186185 .withUrlParam ("allowMultiQueries" , "true" )
187186 .withLogConsumer (new Slf4jLogConsumer (logger ));
188187 mysql .start ();
@@ -207,7 +206,7 @@ void testWithAdditionalUrlParamMultiQueries() throws SQLException {
207206
208207 @ Test
209208 void testWithAdditionalUrlParamInJdbcUrl () {
210- MySQLContainer <?> mysql = new MySQLContainer <> (MySQLTestImages .MYSQL_80_IMAGE )
209+ MySQLContainer mysql = new MySQLContainer (MySQLTestImages .MYSQL_80_IMAGE )
211210 .withUrlParam ("allowMultiQueries" , "true" )
212211 .withUrlParam ("rewriteBatchedStatements" , "true" )
213212 .withLogConsumer (new Slf4jLogConsumer (logger ));
@@ -228,7 +227,7 @@ void testWithAdditionalUrlParamInJdbcUrl() {
228227 void testWithOnlyUserReadableCustomIniFile () throws Exception {
229228 assumeThat (FileSystems .getDefault ().supportedFileAttributeViews ().contains ("posix" )).isTrue ();
230229 try (
231- MySQLContainer <?> mysql = new MySQLContainer <> (MySQLTestImages .MYSQL_80_IMAGE )
230+ MySQLContainer mysql = new MySQLContainer (MySQLTestImages .MYSQL_80_IMAGE )
232231 .withConfigurationOverride ("somepath/mysql_conf_override" )
233232 .withLogConsumer (new Slf4jLogConsumer (logger ))
234233 ) {
@@ -252,12 +251,31 @@ void testWithOnlyUserReadableCustomIniFile() throws Exception {
252251 }
253252 }
254253
255- private void assertHasCorrectExposedAndLivenessCheckPorts (MySQLContainer <?> mysql ) {
254+ @ Test
255+ void testCustom () throws SQLException {
256+ // Add MYSQL_ROOT_HOST environment so that we can root login from anywhere for testing purposes
257+ try (
258+ MySQLContainer mysql = new MySQLContainer (MySQLTestImages .MYSQL_80_IMAGE )
259+ .withDatabaseName ("foo" )
260+ .withUsername ("bar" )
261+ .withPassword ("baz" )
262+ .withEnv ("MYSQL_ROOT_HOST" , "%" )
263+ ) {
264+ mysql .start ();
265+
266+ ResultSet resultSet = performQuery (mysql , "SELECT 1" );
267+
268+ int resultSetInt = resultSet .getInt (1 );
269+ assertThat (resultSetInt ).as ("A basic SELECT query succeeds" ).isEqualTo (1 );
270+ }
271+ }
272+
273+ private void assertHasCorrectExposedAndLivenessCheckPorts (MySQLContainer mysql ) {
256274 assertThat (mysql .getExposedPorts ()).containsExactly (MySQLContainer .MYSQL_PORT );
257275 assertThat (mysql .getLivenessCheckPortNumbers ()).containsExactly (mysql .getMappedPort (MySQLContainer .MYSQL_PORT ));
258276 }
259277
260- private void assertThatCustomIniFileWasUsed (MySQLContainer <?> mysql ) throws SQLException {
278+ private void assertThatCustomIniFileWasUsed (MySQLContainer mysql ) throws SQLException {
261279 try (ResultSet resultSet = performQuery (mysql , "SELECT @@GLOBAL.innodb_max_undo_log_size" )) {
262280 long result = resultSet .getLong (1 );
263281 assertThat (result )
0 commit comments