Skip to content

Commit f332ea3

Browse files
jxblummp911de
authored andcommitted
Declare 'cassandra.version' JVM System property on integration test JVM command-line.
This property is used to get the desired Cassandra Testcotnainer at our supported Apache Cassandra version when running SD Cassandrta integration tests using Testcontainers (profile). Refactor CassandraDelegate to resolve the Cassandra version property used in the Testcontainers Docker Image name. Refactor EmbeddedCassandraServerHelper to guard against NPEs when resolving the RPC address (InetAddress) hostname. Add logging to distinguish which method (Embedded or Testcontainers) to start the Cassandra server. See #1170.
1 parent cd0e669 commit f332ea3

File tree

5 files changed

+92
-18
lines changed

5 files changed

+92
-18
lines changed

pom.xml

+6-11
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,7 @@
8585
<properties>
8686
<build.cassandra.host>localhost</build.cassandra.host>
8787
<build.cassandra.mode>embedded</build.cassandra.mode>
88-
<build.cassandra.native_transport_port>19042
89-
</build.cassandra.native_transport_port>
88+
<build.cassandra.native_transport_port>19042</build.cassandra.native_transport_port>
9089
<build.cassandra.rpc_port>19160</build.cassandra.rpc_port>
9190
<build.cassandra.ssl_storage_port>17001</build.cassandra.ssl_storage_port>
9291
<build.cassandra.storage_port>17000</build.cassandra.storage_port>
@@ -240,8 +239,8 @@
240239
<groupId>org.apache.maven.plugins</groupId>
241240
<artifactId>maven-failsafe-plugin</artifactId>
242241
<configuration>
242+
<argLine>-Xms1g -Xmx1500m -Xss256k -Dcassandra.version=${cassandra.version}</argLine>
243243
<forkCount>1</forkCount>
244-
<argLine>-Xms1g -Xmx1500m -Xss256k</argLine>
245244
<reuseForks>true</reuseForks>
246245
<useFile>false</useFile>
247246
<includes>
@@ -345,26 +344,22 @@
345344
</profile>
346345
<profile>
347346
<id>external-cassandra</id>
348-
349347
<properties>
350348
<build.cassandra.mode>external</build.cassandra.mode>
351-
<build.cassandra.native_transport_port>9042
352-
</build.cassandra.native_transport_port>
349+
<build.cassandra.native_transport_port>9042 </build.cassandra.native_transport_port>
353350
<build.cassandra.rpc_port>9160</build.cassandra.rpc_port>
354-
<build.cassandra.storage_port>7000</build.cassandra.storage_port>
355351
<build.cassandra.ssl_storage_port>7001</build.cassandra.ssl_storage_port>
352+
<build.cassandra.storage_port>7000</build.cassandra.storage_port>
356353
</properties>
357354
</profile>
358355
<profile>
359356
<id>testcontainers-cassandra</id>
360-
361357
<properties>
362358
<build.cassandra.mode>testcontainers</build.cassandra.mode>
363-
<build.cassandra.native_transport_port>0
364-
</build.cassandra.native_transport_port>
359+
<build.cassandra.native_transport_port>0 </build.cassandra.native_transport_port>
365360
<build.cassandra.rpc_port>0</build.cassandra.rpc_port>
366-
<build.cassandra.storage_port>0</build.cassandra.storage_port>
367361
<build.cassandra.ssl_storage_port>0</build.cassandra.ssl_storage_port>
362+
<build.cassandra.storage_port>0</build.cassandra.storage_port>
368363
</properties>
369364
</profile>
370365
</profiles>

spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/util/CassandraDelegate.java

+15-3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import java.util.Map;
2323
import java.util.Optional;
2424

25+
import org.slf4j.Logger;
26+
import org.slf4j.LoggerFactory;
2527
import org.testcontainers.containers.CassandraContainer;
2628

2729
import org.springframework.data.cassandra.core.cql.SessionCallback;
@@ -58,6 +60,8 @@ class CassandraDelegate {
5860

5961
private static CassandraContainer<?> container;
6062

63+
private static final Logger log = LoggerFactory.getLogger(CassandraDelegate.class);
64+
6165
private static ResourceHolder resourceHolder;
6266

6367
private final long startupTimeout;
@@ -269,11 +273,14 @@ private void runTestcontainerCassandra() {
269273

270274
if (container == null) {
271275

272-
container = getCassandraDockerImageName().map(CassandraContainer::new)
276+
container = getCassandraDockerImageName()
277+
.map(CassandraContainer::new)
273278
.orElseGet(CassandraContainer::new);
274279

275280
container.start();
276281

282+
log.info("Running with Cassandra Docker Testcontainer Image Name [{}]", container.getDockerImageName());
283+
277284
this.properties.setCassandraHost(container.getContainerIpAddress());
278285
this.properties.setCassandraPort(container.getFirstMappedPort());
279286
this.properties.update();
@@ -282,11 +289,16 @@ private void runTestcontainerCassandra() {
282289

283290
private Optional<String> getCassandraDockerImageName() {
284291

285-
return Optional.ofNullable(System.getenv("CASSANDRA_VERSION"))
286-
.filter(StringUtils::hasText)
292+
return resolveCassandraVersion()
287293
.map(cassandraVersion -> String.format("cassandra:%s", cassandraVersion));
288294
}
289295

296+
private Optional<String> resolveCassandraVersion() {
297+
298+
return Optional.ofNullable(System.getProperty("cassandra.version", System.getenv("CASSANDRA_VERSION")))
299+
.filter(StringUtils::hasText);
300+
}
301+
290302
private synchronized void initializeConnection() {
291303

292304
this.cassandraPort = resolvePort();

spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/util/EmbeddedCassandraServerHelper.java

+22-4
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,17 @@
1515
*/
1616
package org.springframework.data.cassandra.test.util;
1717

18-
import static java.util.concurrent.TimeUnit.*;
18+
import static java.util.concurrent.TimeUnit.MILLISECONDS;
1919

2020
import java.io.File;
2121
import java.io.FileOutputStream;
2222
import java.io.IOException;
2323
import java.io.InputStream;
2424
import java.io.OutputStream;
25+
import java.net.InetAddress;
26+
import java.net.UnknownHostException;
2527
import java.util.Arrays;
28+
import java.util.Optional;
2629
import java.util.concurrent.ExecutionException;
2730
import java.util.concurrent.ExecutorService;
2831
import java.util.concurrent.Executors;
@@ -47,10 +50,12 @@
4750
@SuppressWarnings("unused")
4851
class EmbeddedCassandraServerHelper {
4952

53+
public static final long DEFAULT_STARTUP_TIMEOUT_MS = TimeUnit.SECONDS.toMillis(20);
54+
5055
private static final Log LOG = LogFactory.getLog(EmbeddedCassandraServerHelper.class);
5156

52-
public static final long DEFAULT_STARTUP_TIMEOUT_MS = TimeUnit.SECONDS.toMillis(20);
5357
private static final String DEFAULT_TMP_DIR = "target/embeddedCassandra";
58+
private static final String LOCALHOST = "localhost";
5459

5560
private static final AtomicReference<Object> sync = new AtomicReference<>();
5661
private static final AtomicReference<CassandraDaemon> cassandraRef = new AtomicReference<>();
@@ -72,7 +77,20 @@ public static String getClusterName() {
7277
* @return the cassandra host
7378
*/
7479
public static String getHost() {
75-
return DatabaseDescriptor.getRpcAddress().getHostName();
80+
81+
return Optional.ofNullable(DatabaseDescriptor.getRpcAddress())
82+
.map(InetAddress::getHostName)
83+
.orElseGet(EmbeddedCassandraServerHelper::getLocalhost);
84+
}
85+
86+
private static String getLocalhost() {
87+
88+
try {
89+
return InetAddress.getLocalHost().getHostName();
90+
}
91+
catch (UnknownHostException ignore) {
92+
return LOCALHOST;
93+
}
7694
}
7795

7896
/**
@@ -155,7 +173,7 @@ private static void startEmbeddedCassandra(File file, long timeout) throws Excep
155173

156174
checkConfigNameForRestart(file.getAbsolutePath());
157175

158-
LOG.debug("Starting cassandra...");
176+
LOG.info(String.format("Starting Embedded Cassandra [v%s]...", System.getProperty("cassandra.version")));
159177
LOG.debug("Initialization needed");
160178

161179
System.setProperty("cassandra.config", "file:" + file.getAbsolutePath());
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright 2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.cassandra.core
17+
18+
import org.springframework.data.cassandra.core.mapping.*
19+
import java.time.LocalDate
20+
import java.util.*
21+
22+
@Table("person")
23+
data class Person(
24+
@PrimaryKey("person_id")
25+
val personId: UUID,
26+
@field:Column("first_name")
27+
val firstName: String,
28+
@field:Column("last_name")
29+
val lastName: String,
30+
@field:Column("date_of_birth")
31+
val dateOfBirth: LocalDate,
32+
@field:Column("last_updated_at")
33+
val lastUpdatedAt: LocalDate
34+
)
35+
36+
37+
fun main() {
38+
39+
40+
val cctx = CassandraMappingContext()
41+
42+
val pe = cctx.getRequiredPersistentEntity(Person::class.java)
43+
44+
pe.doWithProperties { persistentProperty: CassandraPersistentProperty -> println(persistentProperty.columnName) }
45+
46+
47+
}

spring-data-cassandra/src/test/resources/logback.xml

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
<logger name="org.springframework" level="ERROR" />
1111
<logger name="org.springframework.data.cassandra" level="ERROR" />
1212

13+
<logger name="org.springframework.data.cassandra.test.util" level="INFO"/>
14+
1315
<logger name="com.datastax" level="ERROR" />
1416

1517
<!-- See https://issues.apache.org/jira/browse/CASSANDRA-8220 -->

0 commit comments

Comments
 (0)