Skip to content

Commit

Permalink
Pass queryString to the JdbcDatabaseDelegate (#741)
Browse files Browse the repository at this point in the history
Fix for the #727 issue.
  • Loading branch information
dmart28 authored and rnorth committed Jul 10, 2018
1 parent 2ffb8a3 commit 5b4d0ee
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@ public static Iterable<Object[]> data() {
{"jdbc:tc:mysql:5.5.43://hostname/databasename?user=someuser&TC_INITFUNCTION=org.testcontainers.jdbc.JDBCDriverTest::sampleInitFunction", true, false, false, true},
{"jdbc:tc:mysql:5.5.43://hostname/databasename?user=someuser&password=somepwd&TC_INITSCRIPT=somepath/init_mysql.sql", true, false, false, true},
{"jdbc:tc:mysql:5.5.43://hostname/databasename?user=someuser&password=somepwd&TC_INITFUNCTION=org.testcontainers.jdbc.JDBCDriverTest::sampleInitFunction", true, false, false, true},
{"jdbc:tc:mysql:5.5.43://hostname/databasename?useUnicode=yes&characterEncoding=utf8", false, true, false, false},
{"jdbc:tc:mysql:5.5.43://hostname/databasename?TC_INITSCRIPT=somepath/init_unicode_mysql.sql&useUnicode=yes&characterEncoding=utf8", false, true, false, false},
{"jdbc:tc:mysql:5.5.43://hostname/databasename", false, false, false, false},
{"jdbc:tc:mysql:5.5.43://hostname/databasename?useSSL=false", false, false, false, false},
{"jdbc:tc:postgresql:9.6.8://hostname/databasename", false, false, false, false},
{"jdbc:tc:mysql:5.6://hostname/databasename?TC_MY_CNF=somepath/mysql_conf_override", false, false, true, false},
{"jdbc:tc:mariadb://hostname/databasename", false, false, false, false},
{"jdbc:tc:mariadb:10.2.14://hostname/databasename", false, false, false, false},
{"jdbc:tc:mariadb:10.2.14://hostname/databasename?useUnicode=yes&characterEncoding=utf8", false, true, false, false},
{"jdbc:tc:mariadb:10.2.14://hostname/databasename?TC_INITSCRIPT=somepath/init_unicode_mysql.sql&useUnicode=yes&characterEncoding=utf8", false, true, false, false},
{"jdbc:tc:mariadb:10.2.14://hostname/databasename?TC_INITSCRIPT=somepath/init_mariadb.sql", true, false, false, false},
{"jdbc:tc:mariadb:10.2.14://hostname/databasename?TC_INITFUNCTION=org.testcontainers.jdbc.JDBCDriverTest::sampleInitFunction", true, false, false, false},
{"jdbc:tc:mariadb:10.2.14://hostname/databasename?TC_MY_CNF=somepath/mariadb_conf_override", false, false, true, false}});
Expand Down Expand Up @@ -89,6 +89,8 @@ public void test() throws SQLException {
//connections are created from cached containers.
performSimpleTestWithCharacterSet(jdbcUrl);
performSimpleTestWithCharacterSet(jdbcUrl);

performTestForCharacterEncodingForInitialScriptConnection(jdbcUrl);
}

if (performTestForCustomIniFile) {
Expand Down Expand Up @@ -129,6 +131,8 @@ private void performTestForJDBCParamUsage(String jdbcUrl) throws SQLException {
return true;
});

assertTrue("The database returned a record as expected", result);

result = new QueryRunner(dataSource).query("SELECT DATABASE()", rs -> {
rs.next();
String resultDB = rs.getString(1);
Expand All @@ -137,7 +141,19 @@ private void performTestForJDBCParamUsage(String jdbcUrl) throws SQLException {
});

assertTrue("The database returned a record as expected", result);
}
}

private void performTestForCharacterEncodingForInitialScriptConnection(String jdbcUrl) throws SQLException {
try (HikariDataSource dataSource = getDataSource(jdbcUrl, 1)) {
boolean result = new QueryRunner(dataSource).query("SELECT foo FROM bar WHERE foo LIKE '%мир'", rs -> {
rs.next();
String resultSetString = rs.getString(1);
assertEquals("A SELECT query succeed and the correct charset has been applied for the init script", "привет мир", resultSetString);
return true;
});

assertTrue("The database returned a record as expected", result);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
CREATE TABLE bar (
foo varchar(255) character set utf8
);

INSERT INTO bar (foo) VALUES ('привет мир');
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public synchronized Connection connect(String url, final Properties info) throws
an init script or function has been specified, use it
*/
if (!initializedContainers.contains(container.getContainerId())) {
DatabaseDelegate databaseDelegate = new JdbcDatabaseDelegate(container);
DatabaseDelegate databaseDelegate = new JdbcDatabaseDelegate(container, queryString);
runInitScriptIfRequired(connectionUrl, databaseDelegate);
runInitFunctionIfRequired(connectionUrl, connection);
initializedContainers.add(container.getContainerId());
Expand All @@ -151,12 +151,7 @@ private Connection wrapConnection(final Connection connection, final JdbcDatabas

final boolean isDaemon = connectionUrl.isInDaemonMode();

Set<Connection> connections = containerConnections.get(container.getContainerId());

if (connections == null) {
connections = new HashSet<>();
containerConnections.put(container.getContainerId(), connections);
}
Set<Connection> connections = containerConnections.computeIfAbsent(container.getContainerId(), k -> new HashSet<>());

connections.add(connection);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class ContainerLessJdbcDelegate extends JdbcDatabaseDelegate {
private Connection connection;

public ContainerLessJdbcDelegate(Connection connection) {
super(null);
super(null, "");
this.connection = connection;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,17 @@
public class JdbcDatabaseDelegate extends AbstractDatabaseDelegate<Statement> {

private JdbcDatabaseContainer container;
private String queryString;

public JdbcDatabaseDelegate(JdbcDatabaseContainer container) {
public JdbcDatabaseDelegate(JdbcDatabaseContainer container, String queryString) {
this.container = container;
this.queryString = queryString;
}

@Override
protected Statement createNewConnection() {
try {
return container.createConnection("").createStatement();
return container.createConnection(queryString).createStatement();
} catch (SQLException e) {
log.error("Could not obtain JDBC connection");
throw new ConnectionCreationException("Could not obtain JDBC connection", e);
Expand Down

0 comments on commit 5b4d0ee

Please sign in to comment.