Skip to content
This repository was archived by the owner on Sep 2, 2025. It is now read-only.

Commit e80b224

Browse files
author
Abhijit Sarkar
committed
Improves documentation
1 parent a2918ec commit e80b224

File tree

3 files changed

+32
-3
lines changed

3 files changed

+32
-3
lines changed

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ ktlintVersion=9.4.0
1010
bintrayPluginVersion=1.8.5
1111

1212
projectGroup=com.asarkar.spring
13-
projectVersion=1.0.6
13+
projectVersion=1.0.7
1414
projectDescription=Starts the Cassandra server and makes the ports available as Spring Boot environment properties
1515
licenseName=Apache-2.0
1616
licenseUrl=http://www.apache.org/licenses/LICENSE-2.0.txt

src/main/kotlin/com/asarkar/spring/test/CassandraUnit.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ import java.lang.reflect.Field
77
import java.nio.file.Files
88
import java.nio.file.Paths
99

10+
/**
11+
* Utility to start the server and clean up any files left behind.
12+
*
13+
* @author Abhijit Sarkar
14+
* @since 1.0.5
15+
*/
1016
internal object CassandraUnit {
1117
private val cassandraDaemon: Field =
1218
ReflectionUtils.findField(EmbeddedCassandraServerHelper::class.java, "cassandraDaemon")!!
@@ -18,11 +24,13 @@ internal object CassandraUnit {
1824
else EmbeddedCassandraServerHelper.startEmbeddedCassandra(config, timeout)
1925
}
2026

27+
// https://github.com/jsevellec/cassandra-unit/issues/319
2128
fun cleanUp() {
2229
val log4jConfigFile =
2330
EmbeddedCassandraServerHelper.DEFAULT_TMP_DIR + EmbeddedCassandraServerHelper.DEFAULT_LOG4J_CONFIG_FILE
2431
Files.deleteIfExists(Paths.get(log4jConfigFile))
2532
}
2633

34+
// https://github.com/jsevellec/cassandra-unit/issues/318
2735
fun isRunning(): Boolean = cassandraDaemon.get(null) != null
2836
}

src/main/kotlin/com/asarkar/spring/test/CassandraUnitApplicationListener.kt

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,32 @@ import java.nio.file.Files
1717
import java.nio.file.Paths
1818
import kotlin.concurrent.thread
1919

20+
/**
21+
* Listener that prepares the environment so that CassandraUnit will work when it is initialized.
22+
* For instance, by finding free ports for the server to listen on.
23+
*
24+
* @author Abhijit Sarkar
25+
* @since 1.0.0
26+
*/
2027
@Order(Ordered.LOWEST_PRECEDENCE)
2128
class CassandraUnitApplicationListener : ApplicationListener<ApplicationPreparedEvent> {
2229
private val log = LoggerFactory.getLogger(CassandraUnitApplicationListener::class.java)
2330
private val prefix = "cassandra-unit"
2431

2532
override fun onApplicationEvent(event: ApplicationPreparedEvent) {
2633
val env = event.applicationContext.environment
34+
// If the config is not found it means the AutoConfigureCassandraUnit hasn't been initialized.
2735
val config = env.getProperty("$prefix.config", String::class.java) ?: return
36+
// Already running; get the ports from EmbeddedCassandraServerHelper.
2837
val cassandra = if (CassandraUnit.isRunning()) {
2938
mapOf(
3039
"$prefix.native-transport-port" to EmbeddedCassandraServerHelper.getNativeTransportPort(),
3140
"$prefix.rpc-port" to EmbeddedCassandraServerHelper.getRpcPort()
3241
)
3342
} else {
43+
// If using default config with known ports, parse the config for the ports.
44+
// If not using default config, assign a free port to whichever one is zero, and create a new
45+
// config file.
3446
val outFile = config.takeIf { it != EmbeddedCassandraServerHelper.DEFAULT_CASSANDRA_YML_FILE }
3547
?.let { this.createTempFile(it) }
3648
discoverPorts(config, outFile).also {
@@ -64,13 +76,19 @@ class CassandraUnitApplicationListener : ApplicationListener<ApplicationPrepared
6476
var portName: String? = null
6577
Yaml().parse(reader)
6678
.forEach {
79+
// If not a ScalarEvent, pass-through.
6780
if (it !is ScalarEvent) emitter?.emit(it)
6881
else {
82+
// Try to get the port name and value from the event.
6983
val (name, value) = getPortNameAndValue(it, portName)
84+
// Existing port value is zero; new value assigned.
7085
if (value != null) {
7186
cassandra["$prefix.$portName"] = value
7287
emitter?.emit(it.withValue(value))
73-
} else emitter?.emit(it)
88+
}
89+
// No need to modify the event; either it isn't a port, or the existing
90+
// port value isn't zero.
91+
else emitter?.emit(it)
7492
portName = name
7593
}
7694
}
@@ -80,9 +98,12 @@ class CassandraUnitApplicationListener : ApplicationListener<ApplicationPrepared
8098
}
8199

82100
private fun getPortNameAndValue(event: ScalarEvent, portName: String?): Pair<String?, Int?> {
101+
// Found a port key.
83102
if (portName == null && event.value.endsWith("_port")) {
84103
return event.value.replace('_', '-') to null
85-
} else if (portName != null) {
104+
}
105+
// Found the port value corresponding to the key found previously.
106+
else if (portName != null) {
86107
val port = if (event.value.matches("0+".toRegex())) SocketUtils.findAvailableTcpPort()
87108
else event.value.toInt()
88109
return null to port

0 commit comments

Comments
 (0)