Skip to content

Commit 75e2763

Browse files
committed
Use Testcontainers for Cassandra examples.
See #606.
1 parent 7938ee4 commit 75e2763

File tree

6 files changed

+62
-710
lines changed

6 files changed

+62
-710
lines changed

cassandra/util/pom.xml

+6-28
Original file line numberDiff line numberDiff line change
@@ -21,40 +21,18 @@
2121
</dependency>
2222

2323
<dependency>
24-
<groupId>org.apache.cassandra</groupId>
25-
<artifactId>cassandra-all</artifactId>
26-
<version>3.11.5</version>
27-
<exclusions>
28-
<exclusion>
29-
<artifactId>guava</artifactId>
30-
<groupId>com.google.guava</groupId>
31-
</exclusion>
32-
<exclusion>
33-
<groupId>io.netty</groupId>
34-
<artifactId>netty-all</artifactId>
35-
</exclusion>
36-
<exclusion>
37-
<groupId>com.addthis.metrics</groupId>
38-
<artifactId>reporter-config3</artifactId>
39-
</exclusion>
40-
</exclusions>
24+
<groupId>junit</groupId>
25+
<artifactId>junit</artifactId>
4126
</dependency>
4227

4328
<dependency>
44-
<groupId>com.datastax.oss</groupId>
45-
<artifactId>java-driver-core</artifactId>
29+
<groupId>org.testcontainers</groupId>
30+
<artifactId>cassandra</artifactId>
4631
</dependency>
4732

4833
<dependency>
49-
<groupId>org.cassandraunit</groupId>
50-
<artifactId>cassandra-unit</artifactId>
51-
<version>4.3.1.0</version>
52-
<exclusions>
53-
<exclusion>
54-
<groupId>io.netty</groupId>
55-
<artifactId>netty-handler</artifactId>
56-
</exclusion>
57-
</exclusions>
34+
<groupId>com.datastax.oss</groupId>
35+
<artifactId>java-driver-core</artifactId>
5836
</dependency>
5937

6038
<dependency>

cassandra/util/src/main/java/example/springdata/cassandra/util/CassandraExtension.java

+35-9
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@
2222
import org.junit.jupiter.api.extension.ExtensionContext;
2323
import org.junit.platform.commons.util.AnnotationUtils;
2424

25+
import org.springframework.util.StringUtils;
26+
27+
import org.testcontainers.containers.CassandraContainer;
28+
2529
import com.datastax.oss.driver.api.core.CqlSession;
2630

2731
/**
@@ -36,20 +40,26 @@ class CassandraExtension implements BeforeAllCallback {
3640
.create(CassandraExtension.class);
3741

3842
@Override
39-
public void beforeAll(ExtensionContext context) throws Exception {
43+
public void beforeAll(ExtensionContext context) {
44+
45+
var store = context.getStore(NAMESPACE);
46+
var cassandra = findAnnotation(context);
4047

41-
ExtensionContext.Store store = context.getStore(NAMESPACE);
42-
CassandraKeyspace cassandra = findAnnotation(context);
48+
var keyspace = store.getOrComputeIfAbsent(CassandraServer.class, it -> {
4349

44-
CassandraServer keyspace = store.getOrComputeIfAbsent(CassandraServer.class, it -> {
45-
return CassandraServer.embeddedIfNotRunning("localhost", 9042);
50+
CassandraContainer container = runTestcontainer();
51+
System.setProperty("spring.data.cassandra.port", "" + container.getMappedPort(9042));
52+
System.setProperty("spring.data.cassandra.contact-points", "" + container.getHost());
53+
54+
return new CassandraServer(container.getHost(), container.getMappedPort(9042),
55+
CassandraServer.RuntimeMode.EMBEDDED_IF_NOT_RUNNING);
4656
}, CassandraServer.class);
4757

4858
keyspace.before();
4959

50-
CqlSession session = store.getOrComputeIfAbsent(CqlSession.class, it -> {
60+
var session = store.getOrComputeIfAbsent(CqlSession.class, it -> {
5161

52-
return CqlSession.builder().addContactPoint(new InetSocketAddress("localhost", 9042))
62+
return CqlSession.builder().addContactPoint(new InetSocketAddress(keyspace.host(), keyspace.port()))
5363
.withLocalDatacenter("datacenter1").build();
5464
}, CqlSession.class);
5565

@@ -59,10 +69,26 @@ public void beforeAll(ExtensionContext context) throws Exception {
5969

6070
private static CassandraKeyspace findAnnotation(ExtensionContext context) {
6171

62-
Class<?> testClass = context.getRequiredTestClass();
72+
var testClass = context.getRequiredTestClass();
6373

64-
Optional<CassandraKeyspace> annotation = AnnotationUtils.findAnnotation(testClass, CassandraKeyspace.class);
74+
var annotation = AnnotationUtils.findAnnotation(testClass, CassandraKeyspace.class);
6575

6676
return annotation.orElseThrow(() -> new IllegalStateException("Test class not annotated with @Cassandra"));
6777
}
78+
79+
private CassandraContainer<?> runTestcontainer() {
80+
81+
var container = new CassandraContainer<>(getCassandraDockerImageName());
82+
container.withReuse(true);
83+
84+
container.start();
85+
86+
return container;
87+
}
88+
89+
private String getCassandraDockerImageName() {
90+
91+
return String.format("cassandra:%s",
92+
Optional.ofNullable(System.getenv("CASSANDRA_VERSION")).filter(StringUtils::hasText).orElse("3.11.10"));
93+
}
6894
}

cassandra/util/src/main/java/example/springdata/cassandra/util/CassandraServer.java

+5-19
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import java.net.Socket;
2020
import java.util.concurrent.TimeUnit;
2121

22-
import org.cassandraunit.utils.EmbeddedCassandraServerHelper;
2322
import org.junit.jupiter.api.Assumptions;
2423

2524
import org.springframework.util.Assert;
@@ -30,18 +29,8 @@
3029
*
3130
* @author Mark Paluch
3231
*/
33-
class CassandraServer {
34-
35-
private final String host;
36-
private final int port;
37-
private final RuntimeMode runtimeMode;
38-
39-
private CassandraServer(String host, int port, RuntimeMode runtimeMode) {
40-
41-
this.host = host;
42-
this.port = port;
43-
this.runtimeMode = runtimeMode;
44-
}
32+
record CassandraServer(String host, int port,
33+
example.springdata.cassandra.util.CassandraServer.RuntimeMode runtimeMode) {
4534

4635
/**
4736
* Require a running instance on {@code host:port}. Fails with {@link AssumptionViolatedException} if Cassandra is not
@@ -75,7 +64,7 @@ public static boolean isConnectable(String host, int port) {
7564

7665
Assert.hasText(host, "Host must not be null or empty!");
7766

78-
try (Socket socket = new Socket()) {
67+
try (var socket = new Socket()) {
7968

8069
socket.setSoLinger(true, 0);
8170
socket.connect(new InetSocketAddress(host, port), (int) TimeUnit.MILLISECONDS.convert(10, TimeUnit.SECONDS));
@@ -95,7 +84,7 @@ public int getPort() {
9584
return port;
9685
}
9786

98-
protected void before() throws Exception {
87+
protected void before() {
9988

10089
if (runtimeMode == RuntimeMode.REQUIRE_RUNNING_INSTANCE) {
10190
Assumptions.assumeTrue(isConnectable(getHost(), getPort()),
@@ -107,12 +96,9 @@ protected void before() throws Exception {
10796
return;
10897
}
10998
}
110-
111-
EmbeddedCassandraServerHelper.startEmbeddedCassandra("embedded-cassandra.yaml", "target/embeddedCassandra",
112-
TimeUnit.SECONDS.toMillis(60));
11399
}
114100

115-
private enum RuntimeMode {
101+
enum RuntimeMode {
116102
REQUIRE_RUNNING_INSTANCE, EMBEDDED_IF_NOT_RUNNING;
117103
}
118104
}

cassandra/util/src/main/java/example/springdata/cassandra/util/CassandraVersion.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ public static Version getReleaseVersion(CqlSession session) {
4242

4343
Assert.notNull(session, "Session must not be null");
4444

45-
ResultSet resultSet = session.execute("SELECT release_version FROM system.local;");
46-
Row row = resultSet.one();
45+
var resultSet = session.execute("SELECT release_version FROM system.local;");
46+
var row = resultSet.one();
4747

4848
return Version.parse(row.getString(0));
4949
}

0 commit comments

Comments
 (0)