-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use two variants, one with CrateDB driver, one with PGSQL
- Loading branch information
Showing
11 changed files
with
237 additions
and
6 deletions.
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
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
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
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
31 changes: 31 additions & 0 deletions
31
...les/cratedb/src/main/java/org/testcontainers/containers/CrateDBCustomDriverContainer.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,31 @@ | ||
package org.testcontainers.containers; | ||
|
||
import org.testcontainers.utility.DockerImageName; | ||
|
||
public class CrateDBCustomDriverContainer<SELF extends CrateDBContainer<SELF>> extends CrateDBContainer<SELF> { | ||
|
||
public static final String NAME = "cratedbcustom"; | ||
|
||
public CrateDBCustomDriverContainer(final DockerImageName dockerImageName) { | ||
super(dockerImageName); | ||
} | ||
|
||
@Override | ||
public String getDriverClassName() { | ||
return "io.crate.client.jdbc.CrateDriver"; | ||
} | ||
|
||
@Override | ||
public String getJdbcUrl() { | ||
String additionalUrlParams = constructUrlParameters("?", "&"); | ||
return ( | ||
"jdbc:crate://" + | ||
getHost() + | ||
":" + | ||
getMappedPort(CRATEDB_PG_PORT) + | ||
"/" + | ||
getDatabaseName() + | ||
additionalUrlParams | ||
); | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
...edb/src/main/java/org/testcontainers/containers/CrateDBCustomDriverContainerProvider.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,34 @@ | ||
package org.testcontainers.containers; | ||
|
||
import org.testcontainers.jdbc.ConnectionUrl; | ||
import org.testcontainers.utility.DockerImageName; | ||
|
||
/** | ||
* Factory for CrateDB containers using its own JDBC driver. | ||
*/ | ||
public class CrateDBCustomDriverContainerProvider extends JdbcDatabaseContainerProvider { | ||
|
||
public static final String USER_PARAM = "user"; | ||
|
||
public static final String PASSWORD_PARAM = "password"; | ||
|
||
@Override | ||
public boolean supports(String databaseType) { | ||
return databaseType.equals(CrateDBCustomDriverContainer.NAME); | ||
} | ||
|
||
@Override | ||
public JdbcDatabaseContainer newInstance() { | ||
return newInstance(CrateDBContainer.DEFAULT_TAG); | ||
} | ||
|
||
@Override | ||
public JdbcDatabaseContainer newInstance(String tag) { | ||
return new CrateDBCustomDriverContainer(DockerImageName.parse(CrateDBContainer.IMAGE).withTag(tag)); | ||
} | ||
|
||
@Override | ||
public JdbcDatabaseContainer newInstance(ConnectionUrl connectionUrl) { | ||
return newInstanceFromConnectionUrl(connectionUrl, USER_PARAM, PASSWORD_PARAM); | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
...n/resources/META-INF/services/org.testcontainers.containers.JdbcDatabaseContainerProvider
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
org.testcontainers.containers.CrateDBContainerProvider | ||
org.testcontainers.containers.CrateDBCustomDriverContainerProvider |
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
84 changes: 84 additions & 0 deletions
84
...edb/src/test/java/org/testcontainers/containers/CrateDBCustomDriverConnectionURLTest.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,84 @@ | ||
package org.testcontainers.containers; | ||
|
||
import org.junit.Test; | ||
import org.testcontainers.CrateDBTestImages; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.catchThrowable; | ||
|
||
public class CrateDBCustomDriverConnectionURLTest { | ||
|
||
@Test | ||
public void shouldCorrectlyAppendQueryString() { | ||
CrateDBContainer<?> cratedb = new FixedJdbcUrlCrateDBContainer(); | ||
String connectionUrl = cratedb.constructUrlForConnection("?stringtype=unspecified&stringtype=unspecified"); | ||
String queryString = connectionUrl.substring(connectionUrl.indexOf('?')); | ||
|
||
assertThat(queryString) | ||
.as("Query String contains expected params") | ||
.contains("?stringtype=unspecified&stringtype=unspecified"); | ||
assertThat(queryString.indexOf('?')).as("Query String starts with '?'").isZero(); | ||
assertThat(queryString.substring(1)).as("Query String does not contain extra '?'").doesNotContain("?"); | ||
} | ||
|
||
@Test | ||
public void shouldCorrectlyAppendQueryStringWhenNoBaseParams() { | ||
CrateDBContainer<?> cratedb = new NoParamsUrlCrateDBContainer(); | ||
String connectionUrl = cratedb.constructUrlForConnection("?stringtype=unspecified&stringtype=unspecified"); | ||
String queryString = connectionUrl.substring(connectionUrl.indexOf('?')); | ||
|
||
assertThat(queryString) | ||
.as("Query String contains expected params") | ||
.contains("?stringtype=unspecified&stringtype=unspecified"); | ||
assertThat(queryString.indexOf('?')).as("Query String starts with '?'").isZero(); | ||
assertThat(queryString.substring(1)).as("Query String does not contain extra '?'").doesNotContain("?"); | ||
} | ||
|
||
@Test | ||
public void shouldReturnOriginalURLWhenEmptyQueryString() { | ||
CrateDBContainer<?> cratedb = new FixedJdbcUrlCrateDBContainer(); | ||
String connectionUrl = cratedb.constructUrlForConnection(""); | ||
|
||
assertThat(cratedb.getJdbcUrl()).as("Query String remains unchanged").isEqualTo(connectionUrl); | ||
} | ||
|
||
@Test | ||
public void shouldRejectInvalidQueryString() { | ||
assertThat( | ||
catchThrowable(() -> { | ||
new NoParamsUrlCrateDBContainer().constructUrlForConnection("stringtype=unspecified"); | ||
}) | ||
) | ||
.as("Fails when invalid query string provided") | ||
.isInstanceOf(IllegalArgumentException.class); | ||
} | ||
|
||
static class FixedJdbcUrlCrateDBContainer extends CrateDBCustomDriverContainer<FixedJdbcUrlCrateDBContainer> { | ||
|
||
public FixedJdbcUrlCrateDBContainer() { | ||
super(CrateDBTestImages.CRATEDB_TEST_IMAGE); | ||
} | ||
|
||
@Override | ||
public String getHost() { | ||
return "localhost"; | ||
} | ||
|
||
@Override | ||
public Integer getMappedPort(int originalPort) { | ||
return 5432; | ||
} | ||
} | ||
|
||
static class NoParamsUrlCrateDBContainer extends CrateDBCustomDriverContainer<FixedJdbcUrlCrateDBContainer> { | ||
|
||
public NoParamsUrlCrateDBContainer() { | ||
super(CrateDBTestImages.CRATEDB_TEST_IMAGE); | ||
} | ||
|
||
@Override | ||
public String getJdbcUrl() { | ||
return "jdbc:crate://host:port/database"; | ||
} | ||
} | ||
} |
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
76 changes: 76 additions & 0 deletions
76
...cratedb/src/test/java/org/testcontainers/junit/cratedb/SimpleCrateDBCustomDriverTest.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,76 @@ | ||
package org.testcontainers.junit.cratedb; | ||
|
||
import org.junit.Test; | ||
import org.testcontainers.CrateDBTestImages; | ||
import org.testcontainers.containers.CrateDBContainer; | ||
import org.testcontainers.containers.CrateDBCustomDriverContainer; | ||
import org.testcontainers.db.AbstractContainerDatabaseTest; | ||
|
||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
import java.util.logging.Level; | ||
import java.util.logging.LogManager; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
public class SimpleCrateDBCustomDriverTest extends AbstractContainerDatabaseTest { | ||
static { | ||
// Postgres JDBC driver uses JUL; disable it to avoid annoying, irrelevant, stderr logs during connection testing | ||
LogManager.getLogManager().getLogger("").setLevel(Level.OFF); | ||
} | ||
|
||
@Test | ||
public void testSimple() throws SQLException { | ||
try ( | ||
CrateDBCustomDriverContainer cratedb = new CrateDBCustomDriverContainer<>( | ||
CrateDBTestImages.CRATEDB_TEST_IMAGE | ||
) | ||
) { | ||
cratedb.start(); | ||
|
||
ResultSet resultSet = performQuery(cratedb, "SELECT 1"); | ||
int resultSetInt = resultSet.getInt(1); | ||
assertThat(resultSetInt).as("A basic SELECT query succeeds").isEqualTo(1); | ||
assertHasCorrectExposedAndLivenessCheckPorts(cratedb); | ||
} | ||
} | ||
|
||
@Test | ||
public void testCommandOverride() throws SQLException { | ||
try ( | ||
CrateDBContainer<?> cratedb = new CrateDBCustomDriverContainer<>(CrateDBTestImages.CRATEDB_TEST_IMAGE) | ||
.withCommand("crate -C cluster.name=testcontainers") | ||
) { | ||
cratedb.start(); | ||
|
||
ResultSet resultSet = performQuery(cratedb, "select name from sys.cluster"); | ||
String result = resultSet.getString(1); | ||
assertThat(result).as("cluster name should be overriden").isEqualTo("testcontainers"); | ||
} | ||
} | ||
|
||
@Test | ||
public void testExplicitInitScript() throws SQLException { | ||
try ( | ||
CrateDBContainer<?> cratedb = new CrateDBCustomDriverContainer<>(CrateDBTestImages.CRATEDB_TEST_IMAGE) | ||
.withInitScript("somepath/init_cratedb.sql") | ||
) { | ||
cratedb.start(); | ||
|
||
ResultSet resultSet = performQuery(cratedb, "SELECT foo FROM bar"); | ||
|
||
String firstColumnValue = resultSet.getString(1); | ||
assertThat(firstColumnValue).as("Value from init script should equal real value").isEqualTo("hello world"); | ||
} | ||
} | ||
|
||
private void assertHasCorrectExposedAndLivenessCheckPorts(CrateDBCustomDriverContainer<?> cratedb) { | ||
assertThat(cratedb.getExposedPorts()) | ||
.containsExactly(CrateDBContainer.CRATEDB_PG_PORT, CrateDBContainer.CRATEDB_HTTP_PORT); | ||
assertThat(cratedb.getLivenessCheckPortNumbers()) | ||
.containsExactlyInAnyOrder( | ||
cratedb.getMappedPort(CrateDBContainer.CRATEDB_PG_PORT), | ||
cratedb.getMappedPort(CrateDBContainer.CRATEDB_HTTP_PORT) | ||
); | ||
} | ||
} |