-
-
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
222 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/CrateDBLegacyDriverContainer.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 CrateDBLegacyDriverContainer<SELF extends CrateDBContainer<SELF>> extends CrateDBContainer<SELF> { | ||
|
||
public static final String NAME = "cratedb_legacy"; | ||
|
||
public CrateDBLegacyDriverContainer(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 | ||
); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
...edb/src/main/java/org/testcontainers/containers/CrateDBLegacyDriverContainerProvider.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,19 @@ | ||
package org.testcontainers.containers; | ||
|
||
import org.testcontainers.utility.DockerImageName; | ||
|
||
/** | ||
* Factory for CrateDB containers using its own legacy JDBC driver. | ||
*/ | ||
public class CrateDBLegacyDriverContainerProvider extends JdbcDatabaseContainerProvider { | ||
|
||
@Override | ||
public boolean supports(String databaseType) { | ||
return databaseType.equals(CrateDBLegacyDriverContainer.NAME); | ||
} | ||
|
||
@Override | ||
public JdbcDatabaseContainer<?> newInstance(String tag) { | ||
return new CrateDBLegacyDriverContainer<>(DockerImageName.parse(CrateDBContainer.IMAGE).withTag(tag)); | ||
} | ||
} |
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.CrateDBLegacyDriverContainerProvider |
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/CrateDBLegacyDriverConnectionURLTest.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 CrateDBLegacyDriverConnectionURLTest { | ||
|
||
@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 CrateDBLegacyDriverContainer<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 CrateDBLegacyDriverContainer<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/SimpleCrateDBLegacyDriverTest.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.CrateDBLegacyDriverContainer; | ||
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 SimpleCrateDBLegacyDriverTest 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 ( | ||
CrateDBLegacyDriverContainer cratedb = new CrateDBLegacyDriverContainer<>( | ||
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 CrateDBLegacyDriverContainer<>(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 CrateDBLegacyDriverContainer<>(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(CrateDBLegacyDriverContainer<?> 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) | ||
); | ||
} | ||
} |