-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
SimpleMySQLTest.java
147 lines (119 loc) · 5.34 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
package org.testcontainers.junit;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import lombok.NonNull;
import org.apache.commons.lang.SystemUtils;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testcontainers.containers.MySQLContainer;
import org.testcontainers.containers.output.Slf4jLogConsumer;
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;
import static org.rnorth.visibleassertions.VisibleAssertions.assertTrue;
/**
* @author richardnorth
*/
public class SimpleMySQLTest {
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 {
MySQLContainer mysql = (MySQLContainer) new MySQLContainer()
.withConfigurationOverride("somepath/mysql_conf_override")
.withLogConsumer(new Slf4jLogConsumer(logger));
mysql.start();
try {
ResultSet resultSet = performQuery(mysql, "SELECT 1");
int resultSetInt = resultSet.getInt(1);
assertEquals("A basic SELECT query succeeds", 1, resultSetInt);
} finally {
mysql.stop();
}
}
@Test
public void testSpecificVersion() throws SQLException {
MySQLContainer mysqlOldVersion = (MySQLContainer) new MySQLContainer("mysql:5.5")
.withConfigurationOverride("somepath/mysql_conf_override")
.withLogConsumer(new Slf4jLogConsumer(logger));
mysqlOldVersion.start();
try {
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"));
} finally {
mysqlOldVersion.stop();
}
}
@Test
public void testMySQLWithCustomIniFile() throws SQLException {
assumeFalse(SystemUtils.IS_OS_WINDOWS);
MySQLContainer mysqlCustomConfig = new MySQLContainer("mysql:5.6")
.withConfigurationOverride("somepath/mysql_conf_override");
mysqlCustomConfig.start();
try {
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);
} finally {
mysqlCustomConfig.stop();
}
}
@Test
public void testCommandOverride() throws SQLException {
MySQLContainer mysqlCustomConfig = (MySQLContainer) new MySQLContainer().withCommand("mysqld --auto_increment_increment=42");
mysqlCustomConfig.start();
try {
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);
} finally {
mysqlCustomConfig.stop();
}
}
@Test
public void testMySQL8() throws SQLException {
assumeFalse(SystemUtils.IS_OS_WINDOWS);
MySQLContainer container = new MySQLContainer<>("mysql:8.0.11")
.withCommand("mysqld --default-authentication-plugin=mysql_native_password");
container.start();
try {
ResultSet resultSet = performQuery(container, "SELECT VERSION()");
String resultSetString = resultSet.getString(1);
assertTrue("The database version can be set using a container rule parameter", "8.0.11".equals(resultSetString));
}
finally {
container.stop();
}
}
@NonNull
protected ResultSet performQuery(MySQLContainer containerRule, String sql) throws SQLException {
HikariConfig hikariConfig = new HikariConfig();
hikariConfig.setDriverClassName(containerRule.getDriverClassName());
hikariConfig.setJdbcUrl(containerRule.getJdbcUrl());
hikariConfig.setUsername(containerRule.getUsername());
hikariConfig.setPassword(containerRule.getPassword());
HikariDataSource ds = new HikariDataSource(hikariConfig);
Statement statement = ds.getConnection().createStatement();
statement.execute(sql);
ResultSet resultSet = statement.getResultSet();
resultSet.next();
return resultSet;
}
}