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

Apply simple filtering of the query string passed through to real database drivers #357

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public static Iterable<Object[]> data() {
new Object[][]{
{"jdbc:tc:mysql:5.5.43://hostname/databasename", false, false, false},
{"jdbc:tc:mysql://hostname/databasename?TC_INITSCRIPT=somepath/init_mysql.sql", true, false, false},
{"jdbc:tc:mysql://hostname/databasename?TC_INITFUNCTION=org.testcontainers.jdbc.JDBCDriverTest::sampleInitFunction", true, false, false},
{"jdbc:tc:mysql://hostname/databasename?TC_INITFUNCTION=org.testcontainers.jdbc.JDBCDriverTest::sampleInitFunction&useUnicode=yes", true, false, false},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this test isn't adding any verification of the actual implementation, since the tests will succeed even if I change the code in ContainerDatabaseDriver to the original implementation.

TBH I don't have a better idea to test matching and building the JDBC url but refactoring this component out into it's own class and unit testing it. If you like I can take on this work and also switch to using an URL builder like @bsideup suggested.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, fair point - I'll have a bit more of a think about it, and probably break out into a unit test.

I do have a branch for refactoring towards the Apache URL parser, but I'm not quite happy with it yet. It was the same amount of code (or actually I think a few more lines), and ends up mixing the two URL parsing approaches anyway. We probably shouldn't use both. IIRC I originally used regexes because JDBC URLs couldn't be parsed (colon in scheme part), but I could be wrong. I may have just been using the JRE URL parsing libs; perhaps the Apache library can handle it though.

I'll have a try!

{"jdbc:tc:mysql://hostname/databasename?useUnicode=yes&characterEncoding=utf8", false, true, false},
{"jdbc:tc:mysql://hostname/databasename", false, false, false},
{"jdbc:tc:postgresql://hostname/databasename", false, false, false},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,8 @@ 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);
String filteredQueryString = getQueryStringWithoutTCParams(queryString);
Connection connection = container.createConnection(filteredQueryString);

/*
If this container has not been initialized, AND
Expand Down Expand Up @@ -175,6 +176,14 @@ private Map<String, String> getContainerParameters(String url) {
return results;
}

private String getQueryStringWithoutTCParams(String queryString) {
return TC_PARAM_MATCHING_PATTERN.matcher(queryString)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if we just re-create URL without TC_ query parameters?
Something like https://stackoverflow.com/a/41725109

.replaceAll("")
.replaceAll("\\?&", "?")
.replaceAll("&&", "&")
.replaceAll("[?&]$", "");
}

/**
* Wrap the connection, setting up a callback to be called when the connection is closed.
* <p>
Expand Down