Skip to content

Commit

Permalink
Fix: ContainerDatabaseDriver does not register Properties object (#5829)
Browse files Browse the repository at this point in the history
Co-authored-by: Eddú Meléndez Gonzales <eddu.melendez@gmail.com>
  • Loading branch information
aidando73 and eddumelendez committed Sep 26, 2022
1 parent de1a77e commit 9847d59
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 6 deletions.
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 ");
}
}
}
}
}

0 comments on commit 9847d59

Please sign in to comment.