Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ContainerDatabaseDriver does not register Properties object #5829

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -216,9 +216,23 @@ public Driver getJdbcDriverInstance() throws NoDriverFoundException {
* @throws SQLException if there is a repeated failure to create the connection
*/
public Connection createConnection(String queryString) throws SQLException, NoDriverFoundException {
final Properties info = new Properties();
info.put("user", this.getUsername());
info.put("password", this.getPassword());
return createConnection(queryString, new Properties());
}

/**
* Creates a connection to the underlying containerized database instance.
*
* @param queryString query string parameters that should be appended to the JDBC connection URL.
* The '?' character must be included
* @param info additional properties to be passed to the JDBC driver
* @return a Connection
* @throws SQLException if there is a repeated failure to create the connection
*/
public Connection createConnection(String queryString, Properties info)
throws SQLException, NoDriverFoundException {
Properties properties = new Properties(info);
properties.put("user", this.getUsername());
properties.put("password", this.getPassword());
final String url = constructUrlForConnection(queryString);

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

return jdbcDriverInstance.connect(url, info);
return jdbcDriverInstance.connect(url, properties);
} catch (SQLException e) {
lastException = e;
Thread.sleep(100L);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public synchronized Connection connect(String url, final Properties info) throws
/*
Create a connection using the delegated driver. The container must be ready to accept connections.
*/
Connection connection = container.createConnection(queryString);
Connection connection = container.createConnection(queryString, info);

/*
If this container has not been initialized, AND
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package org.testcontainers.jdbc.mysql;

import org.junit.Test;
import org.testcontainers.jdbc.ContainerDatabaseDriver;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

import static org.assertj.core.api.Assertions.assertThat;

public class MySQLDatabaseContainerDriverTest {

@Test
public void shouldRespectBothUrlPropertiesAndParameterProperties() throws SQLException {
ContainerDatabaseDriver driver = new ContainerDatabaseDriver();
String url = "jdbc:tc:mysql:5.7.22://hostname/databasename?padCharsWithSpace=true";
Properties properties = new Properties();
properties.setProperty("maxRows", "1");

try (Connection connection = driver.connect(url, properties)) {
try (Statement statement = connection.createStatement()) {
statement.execute("CREATE TABLE arbitrary_table (length_5_string CHAR(5))");
statement.execute("INSERT INTO arbitrary_table VALUES ('abc')");
statement.execute("INSERT INTO arbitrary_table VALUES ('123')");

// Check that maxRows is set
try (ResultSet resultSet = statement.executeQuery("SELECT * FROM arbitrary_table")) {
resultSet.next();
assertThat(resultSet.isFirst()).isTrue();
assertThat(resultSet.isLast()).isTrue();
}

// Check that pad with chars is set
try (ResultSet resultSet = statement.executeQuery("SELECT * FROM arbitrary_table")) {
assertThat(resultSet.next()).isTrue();
assertThat(resultSet.getString(1)).isEqualTo("abc ");
}
}
}
}
}