diff --git a/AUTHORS b/AUTHORS index 0a877a777bb..2b3140224c7 100644 --- a/AUTHORS +++ b/AUTHORS @@ -1,4 +1,5 @@ -Richard North +Richard North Gurpreet Sohal Alex Boldt -Robert Požarickij \ No newline at end of file +Robert Požarickij +Krystian Nowak diff --git a/modules/jdbc/src/main/java/org/testcontainers/jdbc/ContainerDatabaseDriver.java b/modules/jdbc/src/main/java/org/testcontainers/jdbc/ContainerDatabaseDriver.java index efd7b958574..9d0ce359100 100644 --- a/modules/jdbc/src/main/java/org/testcontainers/jdbc/ContainerDatabaseDriver.java +++ b/modules/jdbc/src/main/java/org/testcontainers/jdbc/ContainerDatabaseDriver.java @@ -72,6 +72,13 @@ public boolean acceptsURL(String url) throws SQLException { @Override public synchronized Connection connect(String url, final Properties info) throws SQLException { + /** + * The driver should return "null" if it realizes it is the wrong kind of driver to connect to the given URL. + */ + if(!acceptsURL(url)) { + return null; + } + String queryString = ""; /** * If we already have a running container for this exact connection string, we want to connect diff --git a/modules/jdbc/src/test/java/org/testcontainers/jdbc/ContainerDatabaseDriverTest.java b/modules/jdbc/src/test/java/org/testcontainers/jdbc/ContainerDatabaseDriverTest.java new file mode 100644 index 00000000000..8b11722c357 --- /dev/null +++ b/modules/jdbc/src/test/java/org/testcontainers/jdbc/ContainerDatabaseDriverTest.java @@ -0,0 +1,35 @@ +package org.testcontainers.jdbc; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; +import java.util.Properties; + +import org.hamcrest.CoreMatchers; +import org.junit.Assert; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + +public class ContainerDatabaseDriverTest { + + private static final String PLAIN_POSTGRESQL_JDBC_URL = "jdbc:postgresql://localhost:5432/test"; + + @Rule + public ExpectedException thrown = ExpectedException.none(); + + @Test + public void shouldNotTryToConnectToNonMatchingJdbcUrlDirectly() throws SQLException { + ContainerDatabaseDriver driver = new ContainerDatabaseDriver(); + Connection connection = driver.connect(PLAIN_POSTGRESQL_JDBC_URL, new Properties()); + Assert.assertNull(connection); + } + + @Test + public void shouldNotTryToConnectToNonMatchingJdbcUrlViaDriverManager() throws SQLException { + thrown.expect(SQLException.class); + thrown.expectMessage(CoreMatchers.startsWith("No suitable driver found for ")); + DriverManager.getConnection(PLAIN_POSTGRESQL_JDBC_URL); + } + +} diff --git a/pom.xml b/pom.xml index 097b3df5ff8..32226945b23 100644 --- a/pom.xml +++ b/pom.xml @@ -38,7 +38,12 @@ rpozarickij Robert Požarickij - robert.pozarickij@gmail.com> + robert.pozarickij@gmail.com + + + krystiannowak + Krystian Nowak + krystian.nowak@gmail.com