Skip to content

Commit 329fa8d

Browse files
committed
Run cache smoke tests against all cache providers
Closes gh-31137
1 parent 6efa77f commit 329fa8d

File tree

5 files changed

+162
-32
lines changed

5 files changed

+162
-32
lines changed

spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-cache/build.gradle

Lines changed: 85 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,32 +5,21 @@ plugins {
55

66
description = "Spring Boot cache smoke test"
77

8-
def caches = [
9-
"caffeine": [
10-
"com.github.ben-manes.caffeine:caffeine"
11-
],
12-
"couchbase": [
13-
project(":spring-boot-project:spring-boot-starters:spring-boot-starter-data-couchbase")
14-
],
15-
"ehcache": [
16-
"javax.cache:cache-api",
17-
"org.ehcache:ehcache"
18-
],
19-
"ehcache2": [
20-
"net.sf.ehcache:ehcache"
21-
],
22-
"hazelcast": [
23-
"com.hazelcast:hazelcast",
24-
"com.hazelcast:hazelcast-spring"
25-
],
26-
"infinispan": [
27-
"org.infinispan:infinispan-jcache",
28-
"org.infinispan:infinispan-spring5-embedded"
29-
],
30-
"redis": [
31-
project(":spring-boot-project:spring-boot-starters:spring-boot-starter-data-redis")
32-
]
33-
]
8+
sourceSets {
9+
redisTest {
10+
compileClasspath += sourceSets.main.output
11+
runtimeClasspath += sourceSets.main.output
12+
}
13+
}
14+
15+
configurations {
16+
caffeine
17+
couchbase
18+
ehcache
19+
ehcache2
20+
hazelcast
21+
infinispan
22+
}
3423

3524
dependencies {
3625
implementation(project(":spring-boot-project:spring-boot-starters:spring-boot-starter-actuator"))
@@ -39,7 +28,73 @@ dependencies {
3928

4029
testImplementation(project(":spring-boot-project:spring-boot-starters:spring-boot-starter-test"))
4130

42-
if (project.hasProperty("cache")) {
43-
caches[project.getProperty("cache")].each { runtimeOnly it }
44-
}
45-
}
31+
caffeine(enforcedPlatform(project(":spring-boot-project:spring-boot-dependencies")))
32+
caffeine("com.github.ben-manes.caffeine:caffeine")
33+
34+
couchbase(enforcedPlatform(project(":spring-boot-project:spring-boot-dependencies")))
35+
couchbase(project(":spring-boot-project:spring-boot-starters:spring-boot-starter-data-couchbase"))
36+
37+
ehcache(enforcedPlatform(project(":spring-boot-project:spring-boot-dependencies")))
38+
ehcache("javax.cache:cache-api")
39+
ehcache("org.ehcache:ehcache")
40+
ehcache2(enforcedPlatform(project(":spring-boot-project:spring-boot-dependencies")))
41+
ehcache2("net.sf.ehcache:ehcache")
42+
43+
hazelcast(enforcedPlatform(project(":spring-boot-project:spring-boot-dependencies")))
44+
hazelcast("com.hazelcast:hazelcast")
45+
hazelcast("com.hazelcast:hazelcast-spring")
46+
47+
infinispan(enforcedPlatform(project(":spring-boot-project:spring-boot-dependencies")))
48+
infinispan("javax.cache:cache-api")
49+
infinispan("org.infinispan:infinispan-jcache")
50+
infinispan("org.infinispan:infinispan-spring5-embedded")
51+
52+
redisTestImplementation(enforcedPlatform(project(":spring-boot-project:spring-boot-parent")))
53+
redisTestImplementation(project(":spring-boot-project:spring-boot-starters:spring-boot-starter-data-redis"))
54+
redisTestImplementation(project(":spring-boot-project:spring-boot-starters:spring-boot-starter-test"))
55+
redisTestImplementation(project(":spring-boot-project:spring-boot-tools:spring-boot-test-support"))
56+
redisTestImplementation("org.testcontainers:testcontainers")
57+
redisTestImplementation("org.testcontainers:junit-jupiter")
58+
}
59+
60+
def testCaffeine = tasks.register("testCaffeine", Test) {
61+
description = "Runs the tests against Caffeine"
62+
classpath = sourceSets.test.runtimeClasspath + configurations.caffeine
63+
}
64+
65+
def testCouchbase = tasks.register("testCouchbase", Test) {
66+
description = "Runs the tests against Couchbase"
67+
classpath = sourceSets.test.runtimeClasspath + configurations.couchbase
68+
}
69+
70+
def testEhcache = tasks.register("testEhcache", Test) {
71+
description = "Runs the tests against Ehcache"
72+
classpath = sourceSets.test.runtimeClasspath + configurations.ehcache
73+
systemProperties = ["spring.cache.jcache.config" : "classpath:ehcache3.xml"]
74+
}
75+
76+
def testEhcache2 = tasks.register("testEhcache2", Test) {
77+
description = "Runs the tests against Ehcache2"
78+
classpath = sourceSets.test.runtimeClasspath + configurations.ehcache2
79+
}
80+
81+
def testHazelcast = tasks.register("testHazelcast", Test) {
82+
description = "Runs the tests against Hazelcast"
83+
classpath = sourceSets.test.runtimeClasspath + configurations.hazelcast
84+
}
85+
86+
def testInfinispan = tasks.register("testInfinispan", Test) {
87+
description = "Runs the tests against Infinispan"
88+
classpath = sourceSets.test.runtimeClasspath + configurations.infinispan
89+
systemProperties = ["spring.cache.jcache.config" : "classpath:infinispan.xml"]
90+
}
91+
92+
def testRedis = tasks.register("testRedis", Test) {
93+
description = "Runs the tests against Redis"
94+
classpath = sourceSets.redisTest.runtimeClasspath
95+
testClassesDirs = sourceSets.redisTest.output.classesDirs
96+
}
97+
98+
tasks.named("check").configure {
99+
dependsOn testCaffeine, testCouchbase, testEhcache, testEhcache2, testHazelcast, testInfinispan, testRedis
100+
}
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
11
management.endpoints.web.exposure.include=*
2-
3-
spring.cache.jcache.config=classpath:ehcache3.xml
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<infinispan>
3+
4+
<!-- ************************************** -->
5+
<!-- Corresponds to @Cacheable("cache-name") -->
6+
<!-- ************************************** -->
7+
<cache-container default-cache="countries">
8+
<local-cache name="countries"/>
9+
</cache-container>
10+
11+
</infinispan>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright 2012-2022 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+
17+
package smoketest.cache;
18+
19+
import org.junit.jupiter.api.Test;
20+
import org.testcontainers.junit.jupiter.Container;
21+
import org.testcontainers.junit.jupiter.Testcontainers;
22+
23+
import org.springframework.beans.factory.annotation.Autowired;
24+
import org.springframework.boot.test.context.SpringBootTest;
25+
import org.springframework.boot.testsupport.testcontainers.RedisContainer;
26+
import org.springframework.cache.Cache;
27+
import org.springframework.cache.CacheManager;
28+
import org.springframework.test.context.DynamicPropertyRegistry;
29+
import org.springframework.test.context.DynamicPropertySource;
30+
31+
import static org.assertj.core.api.Assertions.assertThat;
32+
33+
@SpringBootTest
34+
@Testcontainers(disabledWithoutDocker = true)
35+
class SampleCacheApplicationRedisTests {
36+
37+
@Container
38+
private static final RedisContainer redis = new RedisContainer();
39+
40+
@Autowired
41+
private CacheManager cacheManager;
42+
43+
@Autowired
44+
private CountryRepository countryRepository;
45+
46+
@DynamicPropertySource
47+
static void redisProperties(DynamicPropertyRegistry properties) {
48+
properties.add("spring.redis.url",
49+
() -> "redis://" + redis.getContainerIpAddress() + ":" + redis.getFirstMappedPort());
50+
}
51+
52+
@Test
53+
void validateCache() {
54+
Cache countries = this.cacheManager.getCache("countries");
55+
assertThat(countries).isNotNull();
56+
countries.clear(); // Simple test assuming the cache is empty
57+
assertThat(countries.get("BE")).isNull();
58+
Country be = this.countryRepository.findByCode("BE");
59+
assertThat((Country) countries.get("BE").get()).isEqualTo(be);
60+
}
61+
62+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<configuration>
3+
<include resource="org/springframework/boot/logging/logback/base.xml"/>
4+
</configuration>

0 commit comments

Comments
 (0)