Skip to content

Commit 8fb8041

Browse files
committed
Refactor test framework infrastructure classes.
Resolves gh-1115.
1 parent 4492e1a commit 8fb8041

File tree

4 files changed

+108
-98
lines changed

4 files changed

+108
-98
lines changed

spring-data-cassandra/src/test/java/org/springframework/data/cassandra/support/CassandraConnectionProperties.java

Lines changed: 39 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -24,93 +24,91 @@
2424
import java.util.Properties;
2525

2626
import org.springframework.core.convert.converter.Converter;
27+
import org.springframework.lang.NonNull;
2728
import org.springframework.util.Assert;
2829

2930
/**
3031
* Cassandra connection properties using {@code config/cassandra-connection.properties}. Properties are generated during
3132
* the build and can be override using system properties.
3233
*
3334
* @author Mark Paluch
35+
* @author John Blum
3436
*/
35-
@SuppressWarnings("serial")
37+
@SuppressWarnings("unused")
3638
public class CassandraConnectionProperties extends Properties {
3739

3840
private final static List<WeakReference<CassandraConnectionProperties>> instances = new ArrayList<>();
3941

40-
private String resourceName;
42+
private final String resourceName;
4143

4244
/**
43-
* Create a new {@link CassandraConnectionProperties} using properties from
44-
* {@code config/cassandra-connection.properties}.
45+
* Construct a new instance of {@link CassandraConnectionProperties} using properties
46+
* from {@code config/cassandra-connection.properties}.
4547
*/
4648
public CassandraConnectionProperties() {
4749
this("/config/cassandra-connection.properties");
4850
}
4951

52+
private CassandraConnectionProperties(@NonNull String resourceName) {
53+
54+
this.resourceName = resourceName;
55+
56+
loadProperties();
57+
58+
instances.add(new WeakReference<>(this));
59+
}
60+
5061
public void update() {
62+
5163
try {
5264
// Caution: Rewriting properties during initialization.
53-
File file = new File(getClass().getResource(resourceName).toURI());
65+
File file = new File(getClass().getResource(this.resourceName).toURI());
5466

55-
try (FileOutputStream fos = new FileOutputStream(file)) {
56-
store(fos, "");
67+
try (FileOutputStream out = new FileOutputStream(file)) {
68+
store(out, "");
5769
}
5870

5971
reload();
60-
} catch (Exception e) {
61-
e.printStackTrace();
62-
throw new IllegalStateException(e);
72+
}
73+
catch (Exception cause) {
74+
cause.printStackTrace();
75+
throw new IllegalStateException(cause);
6376
}
6477
}
6578

6679
private static void reload() {
80+
6781
for (WeakReference<CassandraConnectionProperties> ref : instances) {
6882

6983
CassandraConnectionProperties properties = ref.get();
84+
7085
if (properties != null) {
7186
properties.loadProperties();
7287
}
7388
}
7489
}
7590

76-
private CassandraConnectionProperties(String resourceName) {
77-
78-
this.resourceName = resourceName;
79-
loadProperties();
80-
81-
instances.add(new WeakReference<>(this));
82-
}
83-
8491
private void loadProperties() {
85-
8692
loadProperties(this.resourceName);
8793
}
8894

8995
private void loadProperties(String resourceName) {
9096

91-
InputStream in = null;
92-
93-
try {
94-
in = getClass().getResourceAsStream(resourceName);
95-
97+
try (InputStream in = getClass().getResourceAsStream(resourceName)){
9698
if (in != null) {
9799
load(in);
98100
}
99-
} catch (Exception cause) {
101+
}
102+
catch (Exception cause) {
100103
throw new RuntimeException(cause);
101-
} finally {
102-
if (in != null) {
103-
try {
104-
in.close();
105-
} catch (Exception ignore) {}
106-
}
107104
}
108105
}
109106

110107
@Override
111108
public String getProperty(String key) {
112109

113110
String value = super.getProperty(key);
111+
114112
if (value == null) {
115113
value = System.getProperty(key);
116114
}
@@ -154,17 +152,17 @@ public int getCassandraRpcPort() {
154152
}
155153

156154
/**
157-
* @return the Cassandra Storage port
155+
* @return the Cassandra SSL Storage port
158156
*/
159-
public int getCassandraStoragePort() {
160-
return getInt("build.cassandra.storage_port");
157+
public int getCassandraSslStoragePort() {
158+
return getInt("build.cassandra.ssl_storage_port");
161159
}
162160

163161
/**
164-
* @return the Cassandra SSL Storage port
162+
* @return the Cassandra Storage port
165163
*/
166-
public int getCassandraSslStoragePort() {
167-
return getInt("build.cassandra.ssl_storage_port");
164+
public int getCassandraStoragePort() {
165+
return getInt("build.cassandra.storage_port");
168166
}
169167

170168
/**
@@ -174,12 +172,9 @@ public CassandraType getCassandraType() {
174172

175173
String cassandraType = getProperty("build.cassandra.mode");
176174

177-
if (CassandraType.TESTCONTAINERS.name().equalsIgnoreCase(cassandraType)) {
178-
return CassandraType.TESTCONTAINERS;
179-
}
180-
181-
return CassandraType.EXTERNAL.name().equalsIgnoreCase(cassandraType) ? CassandraType.EXTERNAL
182-
: CassandraType.EMBEDDED;
175+
return CassandraType.TESTCONTAINERS.name().equalsIgnoreCase(cassandraType) ? CassandraType.TESTCONTAINERS
176+
: CassandraType.EXTERNAL.name().equalsIgnoreCase(cassandraType) ? CassandraType.EXTERNAL
177+
: CassandraType.EMBEDDED;
183178
}
184179

185180
/**
@@ -229,6 +224,6 @@ private <T> T convert(String propertyName, Class<T> type, Converter<String, T> c
229224
}
230225

231226
public enum CassandraType {
232-
EMBEDDED, EXTERNAL, TESTCONTAINERS;
227+
EMBEDDED, EXTERNAL, TESTCONTAINERS
233228
}
234229
}

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

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,16 @@
2222
import java.util.Map;
2323
import java.util.Optional;
2424

25+
import org.testcontainers.containers.CassandraContainer;
26+
2527
import org.springframework.data.cassandra.core.cql.SessionCallback;
2628
import org.springframework.data.cassandra.support.CassandraConnectionProperties;
2729
import org.springframework.data.cassandra.support.CqlDataSet;
30+
import org.springframework.lang.NonNull;
2831
import org.springframework.util.Assert;
2932
import org.springframework.util.SocketUtils;
3033
import org.springframework.util.StringUtils;
3134

32-
import org.testcontainers.containers.CassandraContainer;
33-
3435
import com.datastax.oss.driver.api.core.CqlIdentifier;
3536
import com.datastax.oss.driver.api.core.CqlSession;
3637
import com.datastax.oss.driver.api.core.CqlSessionBuilder;
@@ -48,22 +49,25 @@
4849
* @author Mark Paluch
4950
* @author John Blum
5051
* @author Tomasz Lelek
52+
* @see org.springframework.data.cassandra.support.CassandraConnectionProperties
53+
* @see com.datastax.oss.driver.api.core.CqlSessionBuilder
54+
* @see com.datastax.oss.driver.api.core.CqlSession
5155
* @since 1.5
52-
* @see CassandraConnectionProperties
5356
*/
5457
class CassandraDelegate {
5558

56-
private static ResourceHolder resourceHolder;
57-
5859
private static CassandraContainer<?> container;
5960

61+
private static ResourceHolder resourceHolder;
62+
6063
private final long startupTimeout;
6164

62-
@SuppressWarnings("all") private final CassandraConnectionProperties properties = new CassandraConnectionProperties();
65+
private final CassandraConnectionProperties properties = new CassandraConnectionProperties();
6366

67+
private CqlSession session;
6468
private CqlSession system;
6569

66-
private CqlSession session;
70+
private CqlSessionBuilder sessionBuilder;
6771

6872
private Integer cassandraPort;
6973

@@ -72,16 +76,16 @@ class CassandraDelegate {
7276

7377
private final Map<SessionCallback<?>, InvocationMode> invocationModeMap = new HashMap<>();
7478

75-
private CqlSessionBuilder sessionBuilder;
76-
7779
private final String configurationFilename;
7880

7981
/**
80-
* Create a new {@link CassandraDelegate} and allows the use of a config file.
82+
* Create a new {@link CassandraDelegate} allowing the use of a config file.
8183
*
82-
* @param yamlConfigurationResource name of the configuration resource, must not be {@literal null} and not empty
84+
* @param yamlConfigurationResource {@link String name} of the configuration resource;
85+
* must not be {@literal null} or {@literal empty}.
86+
* @see #CassandraDelegate(String, long)
8387
*/
84-
public CassandraDelegate(String yamlConfigurationResource) {
88+
public CassandraDelegate(@NonNull String yamlConfigurationResource) {
8589
this(yamlConfigurationResource, EmbeddedCassandraServerHelper.DEFAULT_STARTUP_TIMEOUT_MS);
8690
}
8791

@@ -232,6 +236,7 @@ public void before() throws Exception {
232236
private void startCassandraIfNeeded() throws Exception {
233237

234238
if (isStartNeeded()) {
239+
235240
configureRemoteJmxPort();
236241

237242
if (isEmbedded()) {
@@ -263,12 +268,9 @@ private void runEmbeddedCassandra() throws Exception {
263268
private void runTestcontainerCassandra() {
264269

265270
if (container == null) {
266-
String cassandra_version = System.getenv("CASSANDRA_VERSION");
267-
if (StringUtils.hasText(cassandra_version)) {
268-
container = new CassandraContainer<>("cassandra:" + cassandra_version);
269-
} else {
270-
container = new CassandraContainer<>();
271-
}
271+
272+
container = getCassandraDockerImageName().map(CassandraContainer::new)
273+
.orElseGet(CassandraContainer::new);
272274

273275
container.start();
274276

@@ -278,6 +280,13 @@ private void runTestcontainerCassandra() {
278280
}
279281
}
280282

283+
private Optional<String> getCassandraDockerImageName() {
284+
285+
return Optional.ofNullable(System.getenv("CASSANDRA_VERSION"))
286+
.filter(StringUtils::hasText)
287+
.map(cassandraVersion -> String.format("cassandra:%s", cassandraVersion));
288+
}
289+
281290
private synchronized void initializeConnection() {
282291

283292
this.cassandraPort = resolvePort();
@@ -420,7 +429,8 @@ private void load(CqlSession session, CqlDataSet cqlDataSet) {
420429
/**
421430
* Create a {@link CqlSession} object.
422431
*
423-
* @return
432+
* @return a new {@link CqlSession}.
433+
* @see com.datastax.oss.driver.api.core.CqlSession
424434
*/
425435
CqlSession createSession() {
426436
return sessionBuilder.build();

0 commit comments

Comments
 (0)