-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
SimpleMySQLTest.java
226 lines (184 loc) · 8.81 KB
/
SimpleMySQLTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package org.testcontainers.junit.mysql;
import org.apache.commons.lang.SystemUtils;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testcontainers.containers.ContainerLaunchException;
import org.testcontainers.containers.MySQLContainer;
import org.testcontainers.containers.output.Slf4jLogConsumer;
import org.testcontainers.db.AbstractContainerDatabaseTest;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoUnit;
import static org.hamcrest.CoreMatchers.containsString;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
import static org.junit.Assume.assumeFalse;
import static org.rnorth.visibleassertions.VisibleAssertions.assertEquals;
import static org.rnorth.visibleassertions.VisibleAssertions.assertTrue;
public class SimpleMySQLTest extends AbstractContainerDatabaseTest {
private static final Logger logger = LoggerFactory.getLogger(SimpleMySQLTest.class);
/*
* Ordinarily you wouldn't try and run multiple containers simultaneously - this is just used for testing.
* To avoid memory issues with the default, low memory, docker machine setup, we instantiate only one container
* at a time, inside the test methods themselves.
*/
/*
@ClassRule
public static MySQLContainer<?> mysql = new MySQLContainer<>();
@ClassRule
public static MySQLContainer<?> mysqlOldVersion = new MySQLContainer<>("mysql:5.5");
@ClassRule
public static MySQLContainer<?> mysqlCustomConfig = new MySQLContainer<>("mysql:5.6")
.withConfigurationOverride("somepath/mysql_conf_override");
*/
@Test
public void testSimple() throws SQLException {
try (MySQLContainer<?> mysql = new MySQLContainer<>()
.withConfigurationOverride("somepath/mysql_conf_override")
.withLogConsumer(new Slf4jLogConsumer(logger))) {
mysql.start();
ResultSet resultSet = performQuery(mysql, "SELECT 1");
int resultSetInt = resultSet.getInt(1);
assertEquals("A basic SELECT query succeeds", 1, resultSetInt);
}
}
@Test
public void testSpecificVersion() throws SQLException {
try (MySQLContainer<?> mysqlOldVersion = new MySQLContainer<>("mysql:5.5")
.withConfigurationOverride("somepath/mysql_conf_override")
.withLogConsumer(new Slf4jLogConsumer(logger))) {
mysqlOldVersion.start();
ResultSet resultSet = performQuery(mysqlOldVersion, "SELECT VERSION()");
String resultSetString = resultSet.getString(1);
assertTrue("The database version can be set using a container rule parameter", resultSetString.startsWith("5.5"));
}
}
@Test
public void testMySQLWithCustomIniFile() throws SQLException {
assumeFalse(SystemUtils.IS_OS_WINDOWS);
try (MySQLContainer<?> mysqlCustomConfig = new MySQLContainer<>("mysql:5.6")
.withConfigurationOverride("somepath/mysql_conf_override")) {
mysqlCustomConfig.start();
ResultSet resultSet = performQuery(mysqlCustomConfig, "SELECT @@GLOBAL.innodb_file_format");
String result = resultSet.getString(1);
assertEquals("The InnoDB file format has been set by the ini file content", "Barracuda", result);
}
}
@Test
public void testCommandOverride() throws SQLException {
try (MySQLContainer<?> mysqlCustomConfig = new MySQLContainer<>()
.withCommand("mysqld --auto_increment_increment=42")) {
mysqlCustomConfig.start();
ResultSet resultSet = performQuery(mysqlCustomConfig, "show variables like 'auto_increment_increment'");
String result = resultSet.getString("Value");
assertEquals("Auto increment increment should be overriden by command line", "42", result);
}
}
@Test
public void testExplicitInitScript() throws SQLException {
try (MySQLContainer<?> container = new MySQLContainer<>()
.withInitScript("somepath/init_mysql.sql")
.withLogConsumer(new Slf4jLogConsumer(logger))) {
container.start();
ResultSet resultSet = performQuery(container, "SELECT foo FROM bar");
String firstColumnValue = resultSet.getString(1);
assertEquals("Value from init script should equal real value", "hello world", firstColumnValue);
}
}
@Test(expected = ContainerLaunchException.class)
public void testEmptyPasswordWithNonRootUser() {
try (MySQLContainer<?> container = new MySQLContainer<>("mysql:5.5")
.withDatabaseName("TEST")
.withUsername("test")
.withPassword("")
.withEnv("MYSQL_ROOT_HOST", "%")){
container.start();
fail("ContainerLaunchException expected to be thrown");
}
}
@Test
public void testEmptyPasswordWithRootUser() throws SQLException {
// Add MYSQL_ROOT_HOST environment so that we can root login from anywhere for testing purposes
try (MySQLContainer<?> mysql = new MySQLContainer<>("mysql:5.5")
.withDatabaseName("foo")
.withUsername("root")
.withPassword("")
.withEnv("MYSQL_ROOT_HOST", "%")) {
mysql.start();
ResultSet resultSet = performQuery(mysql, "SELECT 1");
int resultSetInt = resultSet.getInt(1);
assertEquals("A basic SELECT query succeeds", 1, resultSetInt);
}
}
@Test
public void testWithAdditionalUrlParamTimeZone() throws SQLException {
MySQLContainer mysql = (MySQLContainer) new MySQLContainer()
.withUrlParam("serverTimezone", "Europe/Zurich")
.withEnv("TZ", "Europe/Zurich")
.withLogConsumer(new Slf4jLogConsumer(logger));
mysql.start();
try(Connection connection = mysql.createConnection("")) {
Statement statement = connection.createStatement();
statement.execute("SELECT NOW();");
try (ResultSet resultSet = statement.getResultSet()) {
resultSet.next();
// checking that the time_zone MySQL is Europe/Zurich
LocalDateTime localDateTime = resultSet.getObject(1, LocalDateTime.class);
ZonedDateTime actualDateTime = localDateTime.atZone(ZoneId.of("Europe/Zurich"))
.truncatedTo(ChronoUnit.MINUTES);
ZonedDateTime expectedDateTime = ZonedDateTime.now(ZoneId.of("Europe/Zurich"))
.truncatedTo(ChronoUnit.MINUTES);
String message = String.format("MySQL time zone is not Europe/Zurich. MySQL date:%s, current date:%s",
actualDateTime, expectedDateTime);
assertTrue(message, actualDateTime.equals(expectedDateTime));
}
} finally {
mysql.stop();
}
}
@Test
public void testWithAdditionalUrlParamMultiQueries() throws SQLException {
MySQLContainer mysql = (MySQLContainer) new MySQLContainer()
.withUrlParam("allowMultiQueries", "true")
.withLogConsumer(new Slf4jLogConsumer(logger));
mysql.start();
try(Connection connection = mysql.createConnection("")) {
Statement statement = connection.createStatement();
String multiQuery = "DROP TABLE IF EXISTS bar; " +
"CREATE TABLE bar (foo VARCHAR(20)); " +
"INSERT INTO bar (foo) VALUES ('hello world');";
statement.execute(multiQuery);
statement.execute("SELECT foo FROM bar;");
try(ResultSet resultSet = statement.getResultSet()) {
resultSet.next();
String firstColumnValue = resultSet.getString(1);
assertEquals("Value from bar should equal real value", "hello world", firstColumnValue);
}
} finally {
mysql.stop();
}
}
@Test
public void testWithAdditionalUrlParamInJdbcUrl() {
MySQLContainer mysql = (MySQLContainer) new MySQLContainer()
.withUrlParam("allowMultiQueries", "true")
.withUrlParam("rewriteBatchedStatements", "true")
.withLogConsumer(new Slf4jLogConsumer(logger));
try {
mysql.start();
String jdbcUrl = mysql.getJdbcUrl();
assertThat(jdbcUrl, containsString("?"));
assertThat(jdbcUrl, containsString("&"));
assertThat(jdbcUrl, containsString("rewriteBatchedStatements=true"));
assertThat(jdbcUrl, containsString("allowMultiQueries=true"));
} finally {
mysql.stop();
}
}
}