Skip to content

Commit 9847d59

Browse files
Fix: ContainerDatabaseDriver does not register Properties object (#5829)
Co-authored-by: Eddú Meléndez Gonzales <[email protected]>
1 parent de1a77e commit 9847d59

File tree

3 files changed

+64
-6
lines changed

3 files changed

+64
-6
lines changed

modules/jdbc/src/main/java/org/testcontainers/containers/JdbcDatabaseContainer.java

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -216,9 +216,23 @@ public Driver getJdbcDriverInstance() throws NoDriverFoundException {
216216
* @throws SQLException if there is a repeated failure to create the connection
217217
*/
218218
public Connection createConnection(String queryString) throws SQLException, NoDriverFoundException {
219-
final Properties info = new Properties();
220-
info.put("user", this.getUsername());
221-
info.put("password", this.getPassword());
219+
return createConnection(queryString, new Properties());
220+
}
221+
222+
/**
223+
* Creates a connection to the underlying containerized database instance.
224+
*
225+
* @param queryString query string parameters that should be appended to the JDBC connection URL.
226+
* The '?' character must be included
227+
* @param info additional properties to be passed to the JDBC driver
228+
* @return a Connection
229+
* @throws SQLException if there is a repeated failure to create the connection
230+
*/
231+
public Connection createConnection(String queryString, Properties info)
232+
throws SQLException, NoDriverFoundException {
233+
Properties properties = new Properties(info);
234+
properties.put("user", this.getUsername());
235+
properties.put("password", this.getPassword());
222236
final String url = constructUrlForConnection(queryString);
223237

224238
final Driver jdbcDriverInstance = getJdbcDriverInstance();
@@ -234,10 +248,10 @@ public Connection createConnection(String queryString) throws SQLException, NoDr
234248
"Trying to create JDBC connection using {} to {} with properties: {}",
235249
jdbcDriverInstance.getClass().getName(),
236250
url,
237-
info
251+
properties
238252
);
239253

240-
return jdbcDriverInstance.connect(url, info);
254+
return jdbcDriverInstance.connect(url, properties);
241255
} catch (SQLException e) {
242256
lastException = e;
243257
Thread.sleep(100L);

modules/jdbc/src/main/java/org/testcontainers/jdbc/ContainerDatabaseDriver.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ public synchronized Connection connect(String url, final Properties info) throws
127127
/*
128128
Create a connection using the delegated driver. The container must be ready to accept connections.
129129
*/
130-
Connection connection = container.createConnection(queryString);
130+
Connection connection = container.createConnection(queryString, info);
131131

132132
/*
133133
If this container has not been initialized, AND
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package org.testcontainers.jdbc.mysql;
2+
3+
import org.junit.Test;
4+
import org.testcontainers.jdbc.ContainerDatabaseDriver;
5+
6+
import java.sql.Connection;
7+
import java.sql.ResultSet;
8+
import java.sql.SQLException;
9+
import java.sql.Statement;
10+
import java.util.Properties;
11+
12+
import static org.assertj.core.api.Assertions.assertThat;
13+
14+
public class MySQLDatabaseContainerDriverTest {
15+
16+
@Test
17+
public void shouldRespectBothUrlPropertiesAndParameterProperties() throws SQLException {
18+
ContainerDatabaseDriver driver = new ContainerDatabaseDriver();
19+
String url = "jdbc:tc:mysql:5.7.22://hostname/databasename?padCharsWithSpace=true";
20+
Properties properties = new Properties();
21+
properties.setProperty("maxRows", "1");
22+
23+
try (Connection connection = driver.connect(url, properties)) {
24+
try (Statement statement = connection.createStatement()) {
25+
statement.execute("CREATE TABLE arbitrary_table (length_5_string CHAR(5))");
26+
statement.execute("INSERT INTO arbitrary_table VALUES ('abc')");
27+
statement.execute("INSERT INTO arbitrary_table VALUES ('123')");
28+
29+
// Check that maxRows is set
30+
try (ResultSet resultSet = statement.executeQuery("SELECT * FROM arbitrary_table")) {
31+
resultSet.next();
32+
assertThat(resultSet.isFirst()).isTrue();
33+
assertThat(resultSet.isLast()).isTrue();
34+
}
35+
36+
// Check that pad with chars is set
37+
try (ResultSet resultSet = statement.executeQuery("SELECT * FROM arbitrary_table")) {
38+
assertThat(resultSet.next()).isTrue();
39+
assertThat(resultSet.getString(1)).isEqualTo("abc ");
40+
}
41+
}
42+
}
43+
}
44+
}

0 commit comments

Comments
 (0)