22
22
import org .junit .jupiter .api .extension .ExtensionContext ;
23
23
import org .junit .platform .commons .util .AnnotationUtils ;
24
24
25
+ import org .springframework .util .StringUtils ;
26
+
27
+ import org .testcontainers .containers .CassandraContainer ;
28
+
25
29
import com .datastax .oss .driver .api .core .CqlSession ;
26
30
27
31
/**
@@ -36,20 +40,26 @@ class CassandraExtension implements BeforeAllCallback {
36
40
.create (CassandraExtension .class );
37
41
38
42
@ 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 );
40
47
41
- ExtensionContext .Store store = context .getStore (NAMESPACE );
42
- CassandraKeyspace cassandra = findAnnotation (context );
48
+ var keyspace = store .getOrComputeIfAbsent (CassandraServer .class , it -> {
43
49
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 );
46
56
}, CassandraServer .class );
47
57
48
58
keyspace .before ();
49
59
50
- CqlSession session = store .getOrComputeIfAbsent (CqlSession .class , it -> {
60
+ var session = store .getOrComputeIfAbsent (CqlSession .class , it -> {
51
61
52
- return CqlSession .builder ().addContactPoint (new InetSocketAddress ("localhost" , 9042 ))
62
+ return CqlSession .builder ().addContactPoint (new InetSocketAddress (keyspace . host (), keyspace . port () ))
53
63
.withLocalDatacenter ("datacenter1" ).build ();
54
64
}, CqlSession .class );
55
65
@@ -59,10 +69,26 @@ public void beforeAll(ExtensionContext context) throws Exception {
59
69
60
70
private static CassandraKeyspace findAnnotation (ExtensionContext context ) {
61
71
62
- Class <?> testClass = context .getRequiredTestClass ();
72
+ var testClass = context .getRequiredTestClass ();
63
73
64
- Optional < CassandraKeyspace > annotation = AnnotationUtils .findAnnotation (testClass , CassandraKeyspace .class );
74
+ var annotation = AnnotationUtils .findAnnotation (testClass , CassandraKeyspace .class );
65
75
66
76
return annotation .orElseThrow (() -> new IllegalStateException ("Test class not annotated with @Cassandra" ));
67
77
}
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
+ }
68
94
}
0 commit comments