Skip to content

Commit

Permalink
Upgrade the cassandra driver to verion 4.10.0 to support jdk9+
Browse files Browse the repository at this point in the history
  • Loading branch information
vroyer committed Mar 23, 2021
1 parent 8965712 commit 0cd4411
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 44 deletions.
4 changes: 2 additions & 2 deletions modules/cassandra/build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
description = "TestContainers :: Cassandra"

dependencies {
compile project(":database-commons")
compile "com.datastax.cassandra:cassandra-driver-core:3.7.1"
implementation project(":database-commons")
implementation "com.datastax.oss:java-driver-core:4.10.0"
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.testcontainers.containers;

import com.datastax.driver.core.Cluster;
import com.datastax.oss.driver.api.core.CqlSession;
import com.datastax.oss.driver.api.core.CqlSessionBuilder;
import com.github.dockerjava.api.command.InspectContainerResponse;
import org.apache.commons.io.IOUtils;
import org.testcontainers.containers.delegate.CassandraDatabaseDelegate;
Expand All @@ -12,6 +13,7 @@

import javax.script.ScriptException;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Optional;
Expand All @@ -38,7 +40,7 @@ public class CassandraContainer<SELF extends CassandraContainer<SELF>> extends G

private String configLocation;
private String initScriptPath;
private boolean enableJmxReporting;
private Object metricRegistry;

/**
* @deprecated use {@link #CassandraContainer(DockerImageName)} instead
Expand All @@ -59,7 +61,6 @@ public CassandraContainer(DockerImageName dockerImageName) {

addExposedPort(CQL_PORT);
setStartupAttempts(3);
this.enableJmxReporting = false;
}

@Override
Expand Down Expand Up @@ -136,10 +137,13 @@ public SELF withInitScript(String initScriptPath) {
}

/**
* Initialize Cassandra client with JMX reporting enabled or disabled
* Register an external Metric Registry object in the Cassandra driver,
* see https://docs.datastax.com/en/developer/java-driver/4.10/manual/core/metrics/#metric-registry
*
* @param metricRegistry
*/
public SELF withJmxReporting(boolean enableJmxReporting) {
this.enableJmxReporting = enableJmxReporting;
public SELF withMetricRegistry(Object metricRegistry) {
this.metricRegistry = metricRegistry;
return self();
}

Expand Down Expand Up @@ -167,27 +171,32 @@ public String getPassword() {
return PASSWORD;
}

public String getCqlHostAddress() {
return getHost() + ":" + getMappedPort(CQL_PORT);
}

/**
* Get configured Cluster
*
* Can be used to obtain connections to Cassandra in the container
*/
public Cluster getCluster() {
return getCluster(this, enableJmxReporting);
public CqlSession getCqlSession() {
return getCqlSession(this, this.metricRegistry);
}

public static Cluster getCluster(ContainerState containerState, boolean enableJmxReporting) {
final Cluster.Builder builder = Cluster.builder()
.addContactPoint(containerState.getHost())
.withPort(containerState.getMappedPort(CQL_PORT));
if (!enableJmxReporting) {
builder.withoutJMXReporting();
public static CqlSession getCqlSession(ContainerState containerState, Object meterRegistry) {
InetSocketAddress endpoint = new InetSocketAddress(containerState.getHost(), containerState.getMappedPort(CQL_PORT));
final CqlSessionBuilder builder = CqlSession.builder()
.addContactPoint(endpoint)
.withLocalDatacenter("datacenter1");
if (meterRegistry != null) {
builder.withMetricRegistry(meterRegistry);
}
return builder.build();
}

public static Cluster getCluster(ContainerState containerState) {
return getCluster(containerState, false);
public CqlSession getCqlSession(ContainerState containerState) {
return getCqlSession(containerState, false);
}

private DatabaseDelegate getDatabaseDelegate() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package org.testcontainers.containers.delegate;

import com.datastax.driver.core.ResultSet;
import com.datastax.driver.core.Session;
import com.datastax.driver.core.exceptions.DriverException;

import com.datastax.oss.driver.api.core.CqlSession;
import com.datastax.oss.driver.api.core.DriverException;
import com.datastax.oss.driver.api.core.cql.ResultSet;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.testcontainers.containers.CassandraContainer;
Expand All @@ -18,15 +19,14 @@
*/
@Slf4j
@RequiredArgsConstructor
public class CassandraDatabaseDelegate extends AbstractDatabaseDelegate<Session> {
public class CassandraDatabaseDelegate extends AbstractDatabaseDelegate<CqlSession> {

private final ContainerState container;

@Override
protected Session createNewConnection() {
protected CqlSession createNewConnection() {
try {
return CassandraContainer.getCluster(container)
.newSession();
return CassandraContainer.getCqlSession(container, false);
} catch (DriverException e) {
log.error("Could not obtain cassandra connection");
throw new ConnectionCreationException("Could not obtain cassandra connection", e);
Expand All @@ -48,9 +48,9 @@ public void execute(String statement, String scriptPath, int lineNumber, boolean
}

@Override
protected void closeConnectionQuietly(Session session) {
protected void closeConnectionQuietly(CqlSession cqlSession) {
try {
session.getCluster().close();
cqlSession.close();
} catch (Exception e) {
log.error("Could not close cassandra connection", e);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package org.testcontainers.containers;

import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.ResultSet;
import com.datastax.driver.core.Row;
import com.datastax.driver.core.Session;

import com.datastax.oss.driver.api.core.CqlSession;
import com.datastax.oss.driver.api.core.cql.ResultSet;
import com.datastax.oss.driver.api.core.cql.Row;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.testcontainers.containers.wait.CassandraQueryWaitStrategy;
import org.testcontainers.utility.DockerImageName;

import java.net.InetSocketAddress;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
Expand All @@ -19,7 +21,7 @@
@Slf4j
public class CassandraContainerTest {

private static final DockerImageName CASSANDRA_IMAGE = DockerImageName.parse("cassandra:3.11.2");
private static final DockerImageName CASSANDRA_IMAGE = DockerImageName.parse("cassandra:3.11.9");

private static final String TEST_CLUSTER_NAME_IN_CONF = "Test Cluster Integration Test";

Expand Down Expand Up @@ -107,7 +109,7 @@ public void testCassandraQueryWaitStrategy() {
public void testCassandraGetCluster() {
try (CassandraContainer<?> cassandraContainer = new CassandraContainer<>()) {
cassandraContainer.start();
ResultSet resultSet = performQuery(cassandraContainer.getCluster(), "SELECT release_version FROM system.local");
ResultSet resultSet = performQuery(cassandraContainer, "SELECT release_version FROM system.local");
assertTrue("Query was not applied", resultSet.wasApplied());
assertNotNull("Result set has no release_version", resultSet.one().getString(0));
}
Expand All @@ -122,17 +124,10 @@ private void testInitScript(CassandraContainer<?> cassandraContainer) {
}

private ResultSet performQuery(CassandraContainer<?> cassandraContainer, String cql) {
Cluster explicitCluster = Cluster.builder()
.addContactPoint(cassandraContainer.getHost())
.withPort(cassandraContainer.getMappedPort(CassandraContainer.CQL_PORT))
InetSocketAddress endpoint = new InetSocketAddress(cassandraContainer.getHost(), CassandraContainer.CQL_PORT);
CqlSession cqlSession = CqlSession.builder()
.addContactPoint(endpoint)
.build();
return performQuery(explicitCluster, cql);
}

private ResultSet performQuery(Cluster cluster, String cql) {
try (Cluster closeableCluster = cluster) {
Session session = closeableCluster.newSession();
return session.execute(cql);
}
return cqlSession.execute(cql);
}
}

0 comments on commit 0cd4411

Please sign in to comment.