forked from testcontainers/testcontainers-java
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix invalid connection URLs returned by PostgreSQLContainer (fixes te…
…stcontainers#1976) (testcontainers#2049) * Reproduce testcontainers#1976 in tests * Override constructUrlForConnection for PostgreSQLContainer * Lighten PostgreSQLConnectionURLTest * Lighten PostgreSQLConnectionURLTest * Cater for base URLs without query strings
- Loading branch information
Showing
2 changed files
with
89 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
...s/postgresql/src/test/java/org/testcontainers/containers/PostgreSQLConnectionURLTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package org.testcontainers.containers; | ||
|
||
import org.junit.Test; | ||
|
||
import static org.rnorth.visibleassertions.VisibleAssertions.assertEquals; | ||
import static org.rnorth.visibleassertions.VisibleAssertions.assertFalse; | ||
import static org.rnorth.visibleassertions.VisibleAssertions.assertThrows; | ||
import static org.rnorth.visibleassertions.VisibleAssertions.assertTrue; | ||
|
||
public class PostgreSQLConnectionURLTest { | ||
|
||
@Test | ||
public void shouldCorrectlyAppendQueryString() { | ||
PostgreSQLContainer postgres = new FixedJdbcUrlPostgreSQLContainer(); | ||
String connectionUrl = postgres.constructUrlForConnection("?stringtype=unspecified&stringtype=unspecified"); | ||
String queryString = connectionUrl.substring(connectionUrl.indexOf('?')); | ||
|
||
assertTrue("Query String contains expected params", queryString.contains("&stringtype=unspecified&stringtype=unspecified")); | ||
assertEquals("Query String starts with '?'", 0, queryString.indexOf('?')); | ||
assertFalse("Query String does not contain extra '?'", queryString.substring(1).contains("?")); | ||
} | ||
|
||
@Test | ||
public void shouldCorrectlyAppendQueryStringWhenNoBaseParams() { | ||
PostgreSQLContainer postgres = new NoParamsUrlPostgreSQLContainer(); | ||
String connectionUrl = postgres.constructUrlForConnection("?stringtype=unspecified&stringtype=unspecified"); | ||
String queryString = connectionUrl.substring(connectionUrl.indexOf('?')); | ||
|
||
assertTrue("Query String contains expected params", queryString.contains("?stringtype=unspecified&stringtype=unspecified")); | ||
assertEquals("Query String starts with '?'", 0, queryString.indexOf('?')); | ||
assertFalse("Query String does not contain extra '?'", queryString.substring(1).contains("?")); | ||
} | ||
|
||
@Test | ||
public void shouldReturnOriginalURLWhenEmptyQueryString() { | ||
PostgreSQLContainer postgres = new FixedJdbcUrlPostgreSQLContainer(); | ||
String connectionUrl = postgres.constructUrlForConnection(""); | ||
|
||
assertTrue("Query String remains unchanged", postgres.getJdbcUrl().equals(connectionUrl)); | ||
} | ||
|
||
@Test | ||
public void shouldRejectInvalidQueryString() { | ||
assertThrows("Fails when invalid query string provided", IllegalArgumentException.class, | ||
() -> new NoParamsUrlPostgreSQLContainer().constructUrlForConnection("stringtype=unspecified")); | ||
} | ||
|
||
static class FixedJdbcUrlPostgreSQLContainer extends PostgreSQLContainer { | ||
|
||
@Override | ||
public String getContainerIpAddress() { | ||
return "localhost"; | ||
} | ||
|
||
@Override | ||
public Integer getMappedPort(int originalPort) { | ||
return 34532; | ||
} | ||
} | ||
|
||
static class NoParamsUrlPostgreSQLContainer extends PostgreSQLContainer { | ||
|
||
@Override | ||
public String getJdbcUrl() { | ||
return "jdbc:postgresql://host:port/database"; | ||
} | ||
} | ||
} |