diff --git a/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-cache/build.gradle b/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-cache/build.gradle index 84e14c23b1fc..9fa1f3443f30 100644 --- a/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-cache/build.gradle +++ b/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-cache/build.gradle @@ -5,32 +5,21 @@ plugins { description = "Spring Boot cache smoke test" -def caches = [ - "caffeine": [ - "com.github.ben-manes.caffeine:caffeine" - ], - "couchbase": [ - project(":spring-boot-project:spring-boot-starters:spring-boot-starter-data-couchbase") - ], - "ehcache": [ - "javax.cache:cache-api", - "org.ehcache:ehcache" - ], - "ehcache2": [ - "net.sf.ehcache:ehcache" - ], - "hazelcast": [ - "com.hazelcast:hazelcast", - "com.hazelcast:hazelcast-spring" - ], - "infinispan": [ - "org.infinispan:infinispan-jcache", - "org.infinispan:infinispan-spring5-embedded" - ], - "redis": [ - project(":spring-boot-project:spring-boot-starters:spring-boot-starter-data-redis") - ] -] +sourceSets { + redisTest { + compileClasspath += sourceSets.main.output + runtimeClasspath += sourceSets.main.output + } +} + +configurations { + caffeine + couchbase + ehcache + ehcache2 + hazelcast + infinispan +} dependencies { implementation(project(":spring-boot-project:spring-boot-starters:spring-boot-starter-actuator")) @@ -39,7 +28,73 @@ dependencies { testImplementation(project(":spring-boot-project:spring-boot-starters:spring-boot-starter-test")) - if (project.hasProperty("cache")) { - caches[project.getProperty("cache")].each { runtimeOnly it } - } -} \ No newline at end of file + caffeine(enforcedPlatform(project(":spring-boot-project:spring-boot-dependencies"))) + caffeine("com.github.ben-manes.caffeine:caffeine") + + couchbase(enforcedPlatform(project(":spring-boot-project:spring-boot-dependencies"))) + couchbase(project(":spring-boot-project:spring-boot-starters:spring-boot-starter-data-couchbase")) + + ehcache(enforcedPlatform(project(":spring-boot-project:spring-boot-dependencies"))) + ehcache("javax.cache:cache-api") + ehcache("org.ehcache:ehcache") + ehcache2(enforcedPlatform(project(":spring-boot-project:spring-boot-dependencies"))) + ehcache2("net.sf.ehcache:ehcache") + + hazelcast(enforcedPlatform(project(":spring-boot-project:spring-boot-dependencies"))) + hazelcast("com.hazelcast:hazelcast") + hazelcast("com.hazelcast:hazelcast-spring") + + infinispan(enforcedPlatform(project(":spring-boot-project:spring-boot-dependencies"))) + infinispan("javax.cache:cache-api") + infinispan("org.infinispan:infinispan-jcache") + infinispan("org.infinispan:infinispan-spring5-embedded") + + redisTestImplementation(enforcedPlatform(project(":spring-boot-project:spring-boot-parent"))) + redisTestImplementation(project(":spring-boot-project:spring-boot-starters:spring-boot-starter-data-redis")) + redisTestImplementation(project(":spring-boot-project:spring-boot-starters:spring-boot-starter-test")) + redisTestImplementation(project(":spring-boot-project:spring-boot-tools:spring-boot-test-support")) + redisTestImplementation("org.testcontainers:testcontainers") + redisTestImplementation("org.testcontainers:junit-jupiter") +} + +def testCaffeine = tasks.register("testCaffeine", Test) { + description = "Runs the tests against Caffeine" + classpath = sourceSets.test.runtimeClasspath + configurations.caffeine +} + +def testCouchbase = tasks.register("testCouchbase", Test) { + description = "Runs the tests against Couchbase" + classpath = sourceSets.test.runtimeClasspath + configurations.couchbase +} + +def testEhcache = tasks.register("testEhcache", Test) { + description = "Runs the tests against Ehcache" + classpath = sourceSets.test.runtimeClasspath + configurations.ehcache + systemProperties = ["spring.cache.jcache.config" : "classpath:ehcache3.xml"] +} + +def testEhcache2 = tasks.register("testEhcache2", Test) { + description = "Runs the tests against Ehcache2" + classpath = sourceSets.test.runtimeClasspath + configurations.ehcache2 +} + +def testHazelcast = tasks.register("testHazelcast", Test) { + description = "Runs the tests against Hazelcast" + classpath = sourceSets.test.runtimeClasspath + configurations.hazelcast +} + +def testInfinispan = tasks.register("testInfinispan", Test) { + description = "Runs the tests against Infinispan" + classpath = sourceSets.test.runtimeClasspath + configurations.infinispan + systemProperties = ["spring.cache.jcache.config" : "classpath:infinispan.xml"] +} + +def testRedis = tasks.register("testRedis", Test) { + description = "Runs the tests against Redis" + classpath = sourceSets.redisTest.runtimeClasspath + testClassesDirs = sourceSets.redisTest.output.classesDirs +} + +tasks.named("check").configure { + dependsOn testCaffeine, testCouchbase, testEhcache, testEhcache2, testHazelcast, testInfinispan, testRedis +} diff --git a/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-cache/src/main/resources/application.properties b/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-cache/src/main/resources/application.properties index 709b891651cf..821da092741e 100644 --- a/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-cache/src/main/resources/application.properties +++ b/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-cache/src/main/resources/application.properties @@ -1,3 +1 @@ management.endpoints.web.exposure.include=* - -spring.cache.jcache.config=classpath:ehcache3.xml diff --git a/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-cache/src/main/resources/infinispan.xml b/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-cache/src/main/resources/infinispan.xml new file mode 100644 index 000000000000..88486356e608 --- /dev/null +++ b/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-cache/src/main/resources/infinispan.xml @@ -0,0 +1,11 @@ + + + + + + + + + + + diff --git a/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-cache/src/redisTest/java/smoketest/cache/SampleCacheApplicationRedisTests.java b/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-cache/src/redisTest/java/smoketest/cache/SampleCacheApplicationRedisTests.java new file mode 100644 index 000000000000..fee509dde11d --- /dev/null +++ b/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-cache/src/redisTest/java/smoketest/cache/SampleCacheApplicationRedisTests.java @@ -0,0 +1,62 @@ +/* + * Copyright 2012-2022 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package smoketest.cache; + +import org.junit.jupiter.api.Test; +import org.testcontainers.junit.jupiter.Container; +import org.testcontainers.junit.jupiter.Testcontainers; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.testsupport.testcontainers.RedisContainer; +import org.springframework.cache.Cache; +import org.springframework.cache.CacheManager; +import org.springframework.test.context.DynamicPropertyRegistry; +import org.springframework.test.context.DynamicPropertySource; + +import static org.assertj.core.api.Assertions.assertThat; + +@SpringBootTest +@Testcontainers(disabledWithoutDocker = true) +class SampleCacheApplicationRedisTests { + + @Container + private static final RedisContainer redis = new RedisContainer(); + + @Autowired + private CacheManager cacheManager; + + @Autowired + private CountryRepository countryRepository; + + @DynamicPropertySource + static void redisProperties(DynamicPropertyRegistry properties) { + properties.add("spring.redis.url", + () -> "redis://" + redis.getContainerIpAddress() + ":" + redis.getFirstMappedPort()); + } + + @Test + void validateCache() { + Cache countries = this.cacheManager.getCache("countries"); + assertThat(countries).isNotNull(); + countries.clear(); // Simple test assuming the cache is empty + assertThat(countries.get("BE")).isNull(); + Country be = this.countryRepository.findByCode("BE"); + assertThat((Country) countries.get("BE").get()).isEqualTo(be); + } + +} diff --git a/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-cache/src/redisTest/resources/logback-test.xml b/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-cache/src/redisTest/resources/logback-test.xml new file mode 100644 index 000000000000..b8a41480d7d6 --- /dev/null +++ b/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-cache/src/redisTest/resources/logback-test.xml @@ -0,0 +1,4 @@ + + + +