Skip to content

Commit 54007f1

Browse files
authored
Merge pull request #220 from zonkyio/mariadb-driver-3
#208 Upgrade to mariadb-java-client 3.1 (JDBC driver)
2 parents 3448f10 + c7b19ca commit 54007f1

File tree

4 files changed

+48
-7
lines changed

4 files changed

+48
-7
lines changed

build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ ext {
6868
[name: '8.0', 'mysql': '8.0', 'mysql-driver': 'default']
6969
]],
7070
[name: 'mariadb', versions: [
71-
[name: '10.2', 'mariadb': '10.2', 'mariadb-driver': 'default'],
71+
[name: '10.2', 'mariadb': '10.2', 'mariadb-driver': '2.7.7'],
7272
[name: '10.8', 'mariadb': '10.8', 'mariadb-driver': 'default']
7373
]],
7474
[name: 'h2', versions: [
@@ -210,7 +210,7 @@ project(':embedded-database-spring-test') {
210210
compile 'org.postgresql:postgresql:42.4.2', optional
211211
compile 'com.microsoft.sqlserver:mssql-jdbc:11.2.0.jre8', optional
212212
compile 'mysql:mysql-connector-java:8.0.30', optional
213-
compile 'org.mariadb.jdbc:mariadb-java-client:2.7.6', optional
213+
compile 'org.mariadb.jdbc:mariadb-java-client:3.1.0', optional
214214
compile 'com.h2database:h2:2.1.214', optional
215215

216216
compile 'org.flywaydb:flyway-core:9.8.2', optional

embedded-database-spring-test/src/main/java/io/zonky/test/db/provider/mariadb/DockerMariaDBDatabaseProvider.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -214,9 +214,8 @@ private void executeStatement(ClientConfig config, String ddlStatement) throws S
214214
private EmbeddedDatabase getDatabase(ClientConfig config, String dbName) throws SQLException {
215215
MariaDbDataSource dataSource = new MariaDbDataSource();
216216

217-
dataSource.setServerName(container.getContainerIpAddress());
218-
dataSource.setPortNumber(container.getMappedPort(MARIADB_PORT));
219-
dataSource.setDatabaseName(dbName);
217+
dataSource.setUrl(String.format("jdbc:mariadb://%s:%s/%s",
218+
container.getContainerIpAddress(), container.getMappedPort(MARIADB_PORT), dbName));
220219

221220
if ("mysql".equals(dbName)) {
222221
dataSource.setUser("root");

embedded-database-spring-test/src/main/java/io/zonky/test/db/provider/mariadb/MariaDBEmbeddedDatabase.java

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,29 @@
1818

1919
import io.zonky.test.db.provider.support.AbstractEmbeddedDatabase;
2020
import org.mariadb.jdbc.MariaDbDataSource;
21+
import org.springframework.util.ClassUtils;
2122
import org.springframework.util.StringUtils;
2223

2324
import javax.sql.DataSource;
25+
import java.util.List;
2426

2527
import static io.zonky.test.db.util.ReflectionUtils.getField;
28+
import static io.zonky.test.db.util.ReflectionUtils.invokeMethod;
2629

2730
public class MariaDBEmbeddedDatabase extends AbstractEmbeddedDatabase {
2831

2932
private final MariaDbDataSource dataSource;
33+
private final Object conf;
3034

3135
public MariaDBEmbeddedDatabase(MariaDbDataSource dataSource, Runnable closeCallback) {
3236
super(closeCallback);
3337
this.dataSource = dataSource;
38+
39+
if (ClassUtils.hasMethod(MariaDbDataSource.class, "getUrl")) {
40+
conf = getField(dataSource, "conf");
41+
} else {
42+
conf = null;
43+
}
3444
}
3545

3646
@Override
@@ -41,14 +51,46 @@ protected DataSource getDataSource() {
4151
@Override
4252
public String getJdbcUrl() {
4353
String url = String.format("jdbc:mariadb://%s:%s/%s?user=%s",
44-
dataSource.getServerName(), dataSource.getPort(), dataSource.getDatabaseName(), dataSource.getUser());
54+
getServerName(), getPortNumber(), getDatabaseName(), dataSource.getUser());
4555
if (StringUtils.hasText(getPassword())) {
4656
url += String.format("&password=%s", getPassword());
4757
}
4858
return url;
4959
}
5060

61+
public String getServerName() {
62+
if (conf != null) {
63+
return getField(getHostAddress(), "host");
64+
} else {
65+
return invokeMethod(dataSource, "getServerName");
66+
}
67+
}
68+
69+
public int getPortNumber() {
70+
if (conf != null) {
71+
return getField(getHostAddress(), "port");
72+
} else {
73+
return invokeMethod(dataSource, "getPortNumber");
74+
}
75+
}
76+
77+
public String getDatabaseName() {
78+
if (conf != null) {
79+
return getField(conf, "database");
80+
} else {
81+
return invokeMethod(dataSource, "getDatabaseName");
82+
}
83+
}
84+
5185
private String getPassword() {
5286
return getField(dataSource, "password");
5387
}
88+
89+
private Object getHostAddress() {
90+
List<?> addresses = invokeMethod(conf, "addresses");
91+
if (addresses != null && addresses.size() > 0) {
92+
return addresses.get(0);
93+
}
94+
throw new IllegalStateException("Missing host address");
95+
}
5496
}

embedded-database-spring-test/src/test/java/io/zonky/test/db/provider/mariadb/DockerMariaDBDatabaseProviderTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ public void providersWithDifferentConfigurationShouldNotEquals() {
237237
}
238238

239239
private static int getPort(DataSource dataSource) throws SQLException {
240-
return dataSource.unwrap(MariaDbDataSource.class).getPortNumber();
240+
return dataSource.unwrap(MariaDBEmbeddedDatabase.class).getPortNumber();
241241
}
242242

243243
private static MariaDBContainerCustomizer mariadbContainerCustomizer(long timeout) {

0 commit comments

Comments
 (0)