From 382a3e789b9113f1c644de559f72c01aa8383842 Mon Sep 17 00:00:00 2001 From: TINO M THOMAS Date: Fri, 5 Oct 2018 20:20:49 +0300 Subject: [PATCH 1/3] BAEL -2251 Cache eviction in Spring Boot --- .../SpringBootMvcApplication.java | 4 ++ .../caching/CachingController.java | 17 ++++++ .../springbootmvc/caching/CachingService.java | 53 ++++++++++++++++++ .../CacheEvictAnnotationIntegrationTest.java | 44 +++++++++++++++ .../CacheManagerEvictIntegrationTest.java | 55 +++++++++++++++++++ 5 files changed, 173 insertions(+) create mode 100644 spring-boot-mvc/src/main/java/com/baeldung/springbootmvc/caching/CachingController.java create mode 100644 spring-boot-mvc/src/main/java/com/baeldung/springbootmvc/caching/CachingService.java create mode 100644 spring-boot-mvc/src/test/java/com/baeldung/caching/CacheEvictAnnotationIntegrationTest.java create mode 100644 spring-boot-mvc/src/test/java/com/baeldung/caching/CacheManagerEvictIntegrationTest.java diff --git a/spring-boot-mvc/src/main/java/com/baeldung/springbootmvc/SpringBootMvcApplication.java b/spring-boot-mvc/src/main/java/com/baeldung/springbootmvc/SpringBootMvcApplication.java index df8f72f500..99f081c602 100644 --- a/spring-boot-mvc/src/main/java/com/baeldung/springbootmvc/SpringBootMvcApplication.java +++ b/spring-boot-mvc/src/main/java/com/baeldung/springbootmvc/SpringBootMvcApplication.java @@ -2,8 +2,12 @@ package com.baeldung.springbootmvc; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cache.annotation.EnableCaching; +import org.springframework.scheduling.annotation.EnableScheduling; @SpringBootApplication +@EnableCaching +@EnableScheduling public class SpringBootMvcApplication { public static void main(String[] args) { diff --git a/spring-boot-mvc/src/main/java/com/baeldung/springbootmvc/caching/CachingController.java b/spring-boot-mvc/src/main/java/com/baeldung/springbootmvc/caching/CachingController.java new file mode 100644 index 0000000000..390759de9b --- /dev/null +++ b/spring-boot-mvc/src/main/java/com/baeldung/springbootmvc/caching/CachingController.java @@ -0,0 +1,17 @@ +package com.baeldung.springbootmvc.caching; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class CachingController { + + @Autowired + CachingService cachingService; + + @GetMapping("clearAllCaches") + public void clearAllCaches() { + cachingService.evictAllCaches(); + } +} diff --git a/spring-boot-mvc/src/main/java/com/baeldung/springbootmvc/caching/CachingService.java b/spring-boot-mvc/src/main/java/com/baeldung/springbootmvc/caching/CachingService.java new file mode 100644 index 0000000000..e291640bd8 --- /dev/null +++ b/spring-boot-mvc/src/main/java/com/baeldung/springbootmvc/caching/CachingService.java @@ -0,0 +1,53 @@ +package com.baeldung.springbootmvc.caching; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.cache.CacheManager; +import org.springframework.cache.annotation.CacheEvict; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Component; + +@Component +public class CachingService { + + @Autowired + CacheManager cacheManager; + + public void putToCache(String cacheName, String key, String value) { + cacheManager.getCache(cacheName).put(key, value); + } + + public String getFromCache(String cacheName, String key) { + String value = null; + if (cacheManager.getCache(cacheName).get(key) != null) { + value = cacheManager.getCache(cacheName).get(key).get().toString(); + } + return value; + } + + @CacheEvict(value = "first", key = "#cacheKey") + public void evictSingleCacheValue(String cacheKey) { + } + + @CacheEvict(value = "first", allEntries = true) + public void evictAllCacheValues() { + } + + public void evictSingleCacheValue(String cacheName, String cacheKey) { + cacheManager.getCache(cacheName).evict(cacheKey); + } + + public void evictAllCacheValues(String cacheName) { + cacheManager.getCache(cacheName).clear(); + } + + public void evictAllCaches() { + cacheManager.getCacheNames() + .parallelStream() + .forEach(cacheName -> cacheManager.getCache(cacheName).clear()); + } + + @Scheduled(fixedRate = 6000) + public void evictAllcachesAtIntervals() { + evictAllCaches(); + } +} diff --git a/spring-boot-mvc/src/test/java/com/baeldung/caching/CacheEvictAnnotationIntegrationTest.java b/spring-boot-mvc/src/test/java/com/baeldung/caching/CacheEvictAnnotationIntegrationTest.java new file mode 100644 index 0000000000..dc50d48215 --- /dev/null +++ b/spring-boot-mvc/src/test/java/com/baeldung/caching/CacheEvictAnnotationIntegrationTest.java @@ -0,0 +1,44 @@ +package com.baeldung.caching; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.nullValue; +import static org.junit.Assert.assertThat; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +import com.baeldung.springbootmvc.SpringBootMvcApplication; +import com.baeldung.springbootmvc.caching.CachingService; + +@RunWith(SpringRunner.class) +@SpringBootTest(classes = {SpringBootMvcApplication.class, CachingService.class}) +public class CacheEvictAnnotationIntegrationTest { + + @Autowired CachingService cachingService; + + @Before + public void before() { + cachingService.putToCache("first", "key1", "Baeldung"); + cachingService.putToCache("first", "key2", "Article"); + } + + @Test + public void givenFirstCache_whenSingleCacheValueEvictRequested_thenEmptyCacheValue() { + cachingService.evictSingleCacheValue("key1"); + String key1 = cachingService.getFromCache("first", "key1"); + assertThat(key1, is(nullValue())); + } + + @Test + public void givenFirstCache_whenAllCacheValueEvictRequested_thenEmptyCache() { + cachingService.evictAllCacheValues(); + String key1 = cachingService.getFromCache("first", "key1"); + String key2 = cachingService.getFromCache("first", "key2"); + assertThat(key1, is(nullValue())); + assertThat(key2, is(nullValue())); + } +} diff --git a/spring-boot-mvc/src/test/java/com/baeldung/caching/CacheManagerEvictIntegrationTest.java b/spring-boot-mvc/src/test/java/com/baeldung/caching/CacheManagerEvictIntegrationTest.java new file mode 100644 index 0000000000..7f7921fbe5 --- /dev/null +++ b/spring-boot-mvc/src/test/java/com/baeldung/caching/CacheManagerEvictIntegrationTest.java @@ -0,0 +1,55 @@ +package com.baeldung.caching; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.nullValue; +import static org.junit.Assert.assertThat; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +import com.baeldung.springbootmvc.SpringBootMvcApplication; +import com.baeldung.springbootmvc.caching.CachingService; + +@RunWith(SpringRunner.class) +@SpringBootTest(classes = {SpringBootMvcApplication.class, CachingService.class}) +public class CacheManagerEvictIntegrationTest { + + @Autowired CachingService cachingService; + + @Before + public void before() { + cachingService.putToCache("first", "key1", "Baeldung"); + cachingService.putToCache("first", "key2", "Article"); + cachingService.putToCache("second", "key", "Article"); + } + + @Test + public void givenFirstCache_whenSingleCacheValueEvictRequested_thenEmptyCacheValue() { + cachingService.evictSingleCacheValue("first","key1"); + String key1 = cachingService.getFromCache("first", "key1"); + assertThat(key1, is(nullValue())); + } + + @Test + public void givenFirstCache_whenAllCacheValueEvictRequested_thenEmptyCache() { + cachingService.evictAllCacheValues("first"); + String key1 = cachingService.getFromCache("first", "key1"); + String key2 = cachingService.getFromCache("first", "key2"); + assertThat(key1, is(nullValue())); + assertThat(key2, is(nullValue())); + } + + @Test + public void givenAllCaches_whenAllCacheEvictRequested_thenEmptyAllCaches() { + cachingService.evictAllCaches(); + String key1 = cachingService.getFromCache("first", "key1"); + assertThat(key1, is(nullValue())); + + String key = cachingService.getFromCache("second", "key"); + assertThat(key, is(nullValue())); + } +} From e2aad41b7815ba30718025b43c04a60dc84559b2 Mon Sep 17 00:00:00 2001 From: Tino M Thomas Date: Wed, 24 Oct 2018 09:36:56 +0300 Subject: [PATCH 2/3] BAEL - 2251 Moved the code from spring-boot-mvc to spring-all --- .../caching/eviction/CacheEvictionMain.java | 17 ++++++ .../cotrollers/CachingController.java | 18 ++++++ .../eviction/service/CachingService.java | 53 ++++++++++++++++++ .../CacheEvictAnnotationIntegrationTest.java | 44 +++++++++++++++ .../CacheManagerEvictIntegrationTest.java | 55 +++++++++++++++++++ .../SpringBootMvcApplication.java | 4 -- .../caching/CachingController.java | 17 ------ .../springbootmvc/caching/CachingService.java | 53 ------------------ .../CacheEvictAnnotationIntegrationTest.java | 44 --------------- .../CacheManagerEvictIntegrationTest.java | 55 ------------------- 10 files changed, 187 insertions(+), 173 deletions(-) create mode 100644 spring-all/src/main/java/org/baeldung/caching/eviction/CacheEvictionMain.java create mode 100644 spring-all/src/main/java/org/baeldung/caching/eviction/cotrollers/CachingController.java create mode 100644 spring-all/src/main/java/org/baeldung/caching/eviction/service/CachingService.java create mode 100644 spring-all/src/test/java/org/baeldung/caching/test/CacheEvictAnnotationIntegrationTest.java create mode 100644 spring-all/src/test/java/org/baeldung/caching/test/CacheManagerEvictIntegrationTest.java delete mode 100644 spring-boot-mvc/src/main/java/com/baeldung/springbootmvc/caching/CachingController.java delete mode 100644 spring-boot-mvc/src/main/java/com/baeldung/springbootmvc/caching/CachingService.java delete mode 100644 spring-boot-mvc/src/test/java/com/baeldung/caching/CacheEvictAnnotationIntegrationTest.java delete mode 100644 spring-boot-mvc/src/test/java/com/baeldung/caching/CacheManagerEvictIntegrationTest.java diff --git a/spring-all/src/main/java/org/baeldung/caching/eviction/CacheEvictionMain.java b/spring-all/src/main/java/org/baeldung/caching/eviction/CacheEvictionMain.java new file mode 100644 index 0000000000..18fac83ad4 --- /dev/null +++ b/spring-all/src/main/java/org/baeldung/caching/eviction/CacheEvictionMain.java @@ -0,0 +1,17 @@ +package org.baeldung.caching.eviction; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cache.annotation.EnableCaching; +import org.springframework.scheduling.annotation.EnableScheduling; + + +@SpringBootApplication +@EnableCaching +@EnableScheduling +public class CacheEvictionMain { + + public static void main(String[] args) { + SpringApplication.run(CacheEvictionMain.class, args); + } +} diff --git a/spring-all/src/main/java/org/baeldung/caching/eviction/cotrollers/CachingController.java b/spring-all/src/main/java/org/baeldung/caching/eviction/cotrollers/CachingController.java new file mode 100644 index 0000000000..92265f4f18 --- /dev/null +++ b/spring-all/src/main/java/org/baeldung/caching/eviction/cotrollers/CachingController.java @@ -0,0 +1,18 @@ +package org.baeldung.caching.eviction.cotrollers; + +import org.baeldung.caching.eviction.service.CachingService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class CachingController { + + @Autowired + CachingService cachingService; + + @GetMapping("clearAllCaches") + public void clearAllCaches() { + cachingService.evictAllCaches(); + } +} diff --git a/spring-all/src/main/java/org/baeldung/caching/eviction/service/CachingService.java b/spring-all/src/main/java/org/baeldung/caching/eviction/service/CachingService.java new file mode 100644 index 0000000000..7e37215954 --- /dev/null +++ b/spring-all/src/main/java/org/baeldung/caching/eviction/service/CachingService.java @@ -0,0 +1,53 @@ +package org.baeldung.caching.eviction.service; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.cache.CacheManager; +import org.springframework.cache.annotation.CacheEvict; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Component; + +@Component +public class CachingService { + + @Autowired + CacheManager cacheManager; + + public void putToCache(String cacheName, String key, String value) { + cacheManager.getCache(cacheName).put(key, value); + } + + public String getFromCache(String cacheName, String key) { + String value = null; + if (cacheManager.getCache(cacheName).get(key) != null) { + value = cacheManager.getCache(cacheName).get(key).get().toString(); + } + return value; + } + + @CacheEvict(value = "first", key = "#cacheKey") + public void evictSingleCacheValue(String cacheKey) { + } + + @CacheEvict(value = "first", allEntries = true) + public void evictAllCacheValues() { + } + + public void evictSingleCacheValue(String cacheName, String cacheKey) { + cacheManager.getCache(cacheName).evict(cacheKey); + } + + public void evictAllCacheValues(String cacheName) { + cacheManager.getCache(cacheName).clear(); + } + + public void evictAllCaches() { + cacheManager.getCacheNames() + .parallelStream() + .forEach(cacheName -> cacheManager.getCache(cacheName).clear()); + } + + @Scheduled(fixedRate = 6000) + public void evictAllcachesAtIntervals() { + evictAllCaches(); + } +} diff --git a/spring-all/src/test/java/org/baeldung/caching/test/CacheEvictAnnotationIntegrationTest.java b/spring-all/src/test/java/org/baeldung/caching/test/CacheEvictAnnotationIntegrationTest.java new file mode 100644 index 0000000000..3ad4ded745 --- /dev/null +++ b/spring-all/src/test/java/org/baeldung/caching/test/CacheEvictAnnotationIntegrationTest.java @@ -0,0 +1,44 @@ +package org.baeldung.caching.test; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.nullValue; +import static org.junit.Assert.assertThat; + +import org.baeldung.caching.eviction.CacheEvictionMain; +import org.baeldung.caching.eviction.service.CachingService; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +@RunWith(SpringRunner.class) +@SpringBootTest(classes = { CacheEvictionMain.class, CachingService.class }) +public class CacheEvictAnnotationIntegrationTest { + + @Autowired + CachingService cachingService; + + @Before + public void before() { + cachingService.putToCache("first", "key1", "Baeldung"); + cachingService.putToCache("first", "key2", "Article"); + } + + @Test + public void givenFirstCache_whenSingleCacheValueEvictRequested_thenEmptyCacheValue() { + cachingService.evictSingleCacheValue("key1"); + String key1 = cachingService.getFromCache("first", "key1"); + assertThat(key1, is(nullValue())); + } + + @Test + public void givenFirstCache_whenAllCacheValueEvictRequested_thenEmptyCache() { + cachingService.evictAllCacheValues(); + String key1 = cachingService.getFromCache("first", "key1"); + String key2 = cachingService.getFromCache("first", "key2"); + assertThat(key1, is(nullValue())); + assertThat(key2, is(nullValue())); + } +} diff --git a/spring-all/src/test/java/org/baeldung/caching/test/CacheManagerEvictIntegrationTest.java b/spring-all/src/test/java/org/baeldung/caching/test/CacheManagerEvictIntegrationTest.java new file mode 100644 index 0000000000..e65e8800a2 --- /dev/null +++ b/spring-all/src/test/java/org/baeldung/caching/test/CacheManagerEvictIntegrationTest.java @@ -0,0 +1,55 @@ +package org.baeldung.caching.test; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.nullValue; +import static org.junit.Assert.assertThat; + +import org.baeldung.caching.eviction.CacheEvictionMain; +import org.baeldung.caching.eviction.service.CachingService; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +@RunWith(SpringRunner.class) +@SpringBootTest(classes = { CacheEvictionMain.class, CachingService.class }) +public class CacheManagerEvictIntegrationTest { + + @Autowired + CachingService cachingService; + + @Before + public void before() { + cachingService.putToCache("first", "key1", "Baeldung"); + cachingService.putToCache("first", "key2", "Article"); + cachingService.putToCache("second", "key", "Article"); + } + + @Test + public void givenFirstCache_whenSingleCacheValueEvictRequested_thenEmptyCacheValue() { + cachingService.evictSingleCacheValue("first", "key1"); + String key1 = cachingService.getFromCache("first", "key1"); + assertThat(key1, is(nullValue())); + } + + @Test + public void givenFirstCache_whenAllCacheValueEvictRequested_thenEmptyCache() { + cachingService.evictAllCacheValues("first"); + String key1 = cachingService.getFromCache("first", "key1"); + String key2 = cachingService.getFromCache("first", "key2"); + assertThat(key1, is(nullValue())); + assertThat(key2, is(nullValue())); + } + + @Test + public void givenAllCaches_whenAllCacheEvictRequested_thenEmptyAllCaches() { + cachingService.evictAllCaches(); + String key1 = cachingService.getFromCache("first", "key1"); + assertThat(key1, is(nullValue())); + + String key = cachingService.getFromCache("second", "key"); + assertThat(key, is(nullValue())); + } +} diff --git a/spring-boot-mvc/src/main/java/com/baeldung/springbootmvc/SpringBootMvcApplication.java b/spring-boot-mvc/src/main/java/com/baeldung/springbootmvc/SpringBootMvcApplication.java index 99f081c602..df8f72f500 100644 --- a/spring-boot-mvc/src/main/java/com/baeldung/springbootmvc/SpringBootMvcApplication.java +++ b/spring-boot-mvc/src/main/java/com/baeldung/springbootmvc/SpringBootMvcApplication.java @@ -2,12 +2,8 @@ package com.baeldung.springbootmvc; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.cache.annotation.EnableCaching; -import org.springframework.scheduling.annotation.EnableScheduling; @SpringBootApplication -@EnableCaching -@EnableScheduling public class SpringBootMvcApplication { public static void main(String[] args) { diff --git a/spring-boot-mvc/src/main/java/com/baeldung/springbootmvc/caching/CachingController.java b/spring-boot-mvc/src/main/java/com/baeldung/springbootmvc/caching/CachingController.java deleted file mode 100644 index 390759de9b..0000000000 --- a/spring-boot-mvc/src/main/java/com/baeldung/springbootmvc/caching/CachingController.java +++ /dev/null @@ -1,17 +0,0 @@ -package com.baeldung.springbootmvc.caching; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RestController; - -@RestController -public class CachingController { - - @Autowired - CachingService cachingService; - - @GetMapping("clearAllCaches") - public void clearAllCaches() { - cachingService.evictAllCaches(); - } -} diff --git a/spring-boot-mvc/src/main/java/com/baeldung/springbootmvc/caching/CachingService.java b/spring-boot-mvc/src/main/java/com/baeldung/springbootmvc/caching/CachingService.java deleted file mode 100644 index e291640bd8..0000000000 --- a/spring-boot-mvc/src/main/java/com/baeldung/springbootmvc/caching/CachingService.java +++ /dev/null @@ -1,53 +0,0 @@ -package com.baeldung.springbootmvc.caching; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.cache.CacheManager; -import org.springframework.cache.annotation.CacheEvict; -import org.springframework.scheduling.annotation.Scheduled; -import org.springframework.stereotype.Component; - -@Component -public class CachingService { - - @Autowired - CacheManager cacheManager; - - public void putToCache(String cacheName, String key, String value) { - cacheManager.getCache(cacheName).put(key, value); - } - - public String getFromCache(String cacheName, String key) { - String value = null; - if (cacheManager.getCache(cacheName).get(key) != null) { - value = cacheManager.getCache(cacheName).get(key).get().toString(); - } - return value; - } - - @CacheEvict(value = "first", key = "#cacheKey") - public void evictSingleCacheValue(String cacheKey) { - } - - @CacheEvict(value = "first", allEntries = true) - public void evictAllCacheValues() { - } - - public void evictSingleCacheValue(String cacheName, String cacheKey) { - cacheManager.getCache(cacheName).evict(cacheKey); - } - - public void evictAllCacheValues(String cacheName) { - cacheManager.getCache(cacheName).clear(); - } - - public void evictAllCaches() { - cacheManager.getCacheNames() - .parallelStream() - .forEach(cacheName -> cacheManager.getCache(cacheName).clear()); - } - - @Scheduled(fixedRate = 6000) - public void evictAllcachesAtIntervals() { - evictAllCaches(); - } -} diff --git a/spring-boot-mvc/src/test/java/com/baeldung/caching/CacheEvictAnnotationIntegrationTest.java b/spring-boot-mvc/src/test/java/com/baeldung/caching/CacheEvictAnnotationIntegrationTest.java deleted file mode 100644 index dc50d48215..0000000000 --- a/spring-boot-mvc/src/test/java/com/baeldung/caching/CacheEvictAnnotationIntegrationTest.java +++ /dev/null @@ -1,44 +0,0 @@ -package com.baeldung.caching; - -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.CoreMatchers.nullValue; -import static org.junit.Assert.assertThat; - -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.test.context.junit4.SpringRunner; - -import com.baeldung.springbootmvc.SpringBootMvcApplication; -import com.baeldung.springbootmvc.caching.CachingService; - -@RunWith(SpringRunner.class) -@SpringBootTest(classes = {SpringBootMvcApplication.class, CachingService.class}) -public class CacheEvictAnnotationIntegrationTest { - - @Autowired CachingService cachingService; - - @Before - public void before() { - cachingService.putToCache("first", "key1", "Baeldung"); - cachingService.putToCache("first", "key2", "Article"); - } - - @Test - public void givenFirstCache_whenSingleCacheValueEvictRequested_thenEmptyCacheValue() { - cachingService.evictSingleCacheValue("key1"); - String key1 = cachingService.getFromCache("first", "key1"); - assertThat(key1, is(nullValue())); - } - - @Test - public void givenFirstCache_whenAllCacheValueEvictRequested_thenEmptyCache() { - cachingService.evictAllCacheValues(); - String key1 = cachingService.getFromCache("first", "key1"); - String key2 = cachingService.getFromCache("first", "key2"); - assertThat(key1, is(nullValue())); - assertThat(key2, is(nullValue())); - } -} diff --git a/spring-boot-mvc/src/test/java/com/baeldung/caching/CacheManagerEvictIntegrationTest.java b/spring-boot-mvc/src/test/java/com/baeldung/caching/CacheManagerEvictIntegrationTest.java deleted file mode 100644 index 7f7921fbe5..0000000000 --- a/spring-boot-mvc/src/test/java/com/baeldung/caching/CacheManagerEvictIntegrationTest.java +++ /dev/null @@ -1,55 +0,0 @@ -package com.baeldung.caching; - -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.CoreMatchers.nullValue; -import static org.junit.Assert.assertThat; - -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.test.context.junit4.SpringRunner; - -import com.baeldung.springbootmvc.SpringBootMvcApplication; -import com.baeldung.springbootmvc.caching.CachingService; - -@RunWith(SpringRunner.class) -@SpringBootTest(classes = {SpringBootMvcApplication.class, CachingService.class}) -public class CacheManagerEvictIntegrationTest { - - @Autowired CachingService cachingService; - - @Before - public void before() { - cachingService.putToCache("first", "key1", "Baeldung"); - cachingService.putToCache("first", "key2", "Article"); - cachingService.putToCache("second", "key", "Article"); - } - - @Test - public void givenFirstCache_whenSingleCacheValueEvictRequested_thenEmptyCacheValue() { - cachingService.evictSingleCacheValue("first","key1"); - String key1 = cachingService.getFromCache("first", "key1"); - assertThat(key1, is(nullValue())); - } - - @Test - public void givenFirstCache_whenAllCacheValueEvictRequested_thenEmptyCache() { - cachingService.evictAllCacheValues("first"); - String key1 = cachingService.getFromCache("first", "key1"); - String key2 = cachingService.getFromCache("first", "key2"); - assertThat(key1, is(nullValue())); - assertThat(key2, is(nullValue())); - } - - @Test - public void givenAllCaches_whenAllCacheEvictRequested_thenEmptyAllCaches() { - cachingService.evictAllCaches(); - String key1 = cachingService.getFromCache("first", "key1"); - assertThat(key1, is(nullValue())); - - String key = cachingService.getFromCache("second", "key"); - assertThat(key, is(nullValue())); - } -} From 0d4fa7c74862fe5ac85fe4d77ed1c9fc018e809a Mon Sep 17 00:00:00 2001 From: TINO Date: Sun, 28 Oct 2018 19:52:59 +0300 Subject: [PATCH 3/3] BAEL - 2251 Removed Spring boot dependency. --- .../caching/eviction/CacheEvictionMain.java | 17 ---- .../CachingController.java | 2 +- .../CacheEvictAnnotationIntegrationTest.java | 87 +++++++++++++------ .../CacheManagerEvictIntegrationTest.java | 51 +++++++++-- 4 files changed, 108 insertions(+), 49 deletions(-) delete mode 100644 spring-all/src/main/java/org/baeldung/caching/eviction/CacheEvictionMain.java rename spring-all/src/main/java/org/baeldung/caching/eviction/{cotrollers => controllers}/CachingController.java (87%) diff --git a/spring-all/src/main/java/org/baeldung/caching/eviction/CacheEvictionMain.java b/spring-all/src/main/java/org/baeldung/caching/eviction/CacheEvictionMain.java deleted file mode 100644 index 18fac83ad4..0000000000 --- a/spring-all/src/main/java/org/baeldung/caching/eviction/CacheEvictionMain.java +++ /dev/null @@ -1,17 +0,0 @@ -package org.baeldung.caching.eviction; - -import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.cache.annotation.EnableCaching; -import org.springframework.scheduling.annotation.EnableScheduling; - - -@SpringBootApplication -@EnableCaching -@EnableScheduling -public class CacheEvictionMain { - - public static void main(String[] args) { - SpringApplication.run(CacheEvictionMain.class, args); - } -} diff --git a/spring-all/src/main/java/org/baeldung/caching/eviction/cotrollers/CachingController.java b/spring-all/src/main/java/org/baeldung/caching/eviction/controllers/CachingController.java similarity index 87% rename from spring-all/src/main/java/org/baeldung/caching/eviction/cotrollers/CachingController.java rename to spring-all/src/main/java/org/baeldung/caching/eviction/controllers/CachingController.java index 92265f4f18..675cb7a516 100644 --- a/spring-all/src/main/java/org/baeldung/caching/eviction/cotrollers/CachingController.java +++ b/spring-all/src/main/java/org/baeldung/caching/eviction/controllers/CachingController.java @@ -1,4 +1,4 @@ -package org.baeldung.caching.eviction.cotrollers; +package org.baeldung.caching.eviction.controllers; import org.baeldung.caching.eviction.service.CachingService; import org.springframework.beans.factory.annotation.Autowired; diff --git a/spring-all/src/test/java/org/baeldung/caching/test/CacheEvictAnnotationIntegrationTest.java b/spring-all/src/test/java/org/baeldung/caching/test/CacheEvictAnnotationIntegrationTest.java index 3ad4ded745..f24cdef917 100644 --- a/spring-all/src/test/java/org/baeldung/caching/test/CacheEvictAnnotationIntegrationTest.java +++ b/spring-all/src/test/java/org/baeldung/caching/test/CacheEvictAnnotationIntegrationTest.java @@ -4,41 +4,76 @@ import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.nullValue; import static org.junit.Assert.assertThat; -import org.baeldung.caching.eviction.CacheEvictionMain; +import java.util.ArrayList; +import java.util.List; + import org.baeldung.caching.eviction.service.CachingService; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.cache.Cache; +import org.springframework.cache.CacheManager; +import org.springframework.cache.annotation.EnableCaching; +import org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean; +import org.springframework.cache.support.SimpleCacheManager; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; -@RunWith(SpringRunner.class) -@SpringBootTest(classes = { CacheEvictionMain.class, CachingService.class }) +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration public class CacheEvictAnnotationIntegrationTest { - @Autowired - CachingService cachingService; + @Configuration + @EnableCaching + static class ContextConfiguration { - @Before - public void before() { - cachingService.putToCache("first", "key1", "Baeldung"); - cachingService.putToCache("first", "key2", "Article"); - } + @Bean + public CachingService cachingService() { + return new CachingService(); + } + + @Bean + public CacheManager cacheManager(){ + SimpleCacheManager cacheManager = new SimpleCacheManager(); + List caches = new ArrayList<>(); + caches.add(cacheBean().getObject()); + cacheManager.setCaches(caches ); + return cacheManager; + } + + @Bean + public ConcurrentMapCacheFactoryBean cacheBean(){ + ConcurrentMapCacheFactoryBean cacheFactoryBean = new ConcurrentMapCacheFactoryBean(); + cacheFactoryBean.setName("first"); + return cacheFactoryBean; + } + } - @Test - public void givenFirstCache_whenSingleCacheValueEvictRequested_thenEmptyCacheValue() { - cachingService.evictSingleCacheValue("key1"); - String key1 = cachingService.getFromCache("first", "key1"); - assertThat(key1, is(nullValue())); - } + @Autowired + CachingService cachingService; - @Test - public void givenFirstCache_whenAllCacheValueEvictRequested_thenEmptyCache() { - cachingService.evictAllCacheValues(); - String key1 = cachingService.getFromCache("first", "key1"); - String key2 = cachingService.getFromCache("first", "key2"); - assertThat(key1, is(nullValue())); - assertThat(key2, is(nullValue())); - } + @Before + public void before() { + cachingService.putToCache("first", "key1", "Baeldung"); + cachingService.putToCache("first", "key2", "Article"); + } + + @Test + public void givenFirstCache_whenSingleCacheValueEvictRequested_thenEmptyCacheValue() { + cachingService.evictSingleCacheValue("key1"); + String key1 = cachingService.getFromCache("first", "key1"); + assertThat(key1, is(nullValue())); + } + + @Test + public void givenFirstCache_whenAllCacheValueEvictRequested_thenEmptyCache() { + cachingService.evictAllCacheValues(); + String key1 = cachingService.getFromCache("first", "key1"); + String key2 = cachingService.getFromCache("first", "key2"); + assertThat(key1, is(nullValue())); + assertThat(key2, is(nullValue())); + } } diff --git a/spring-all/src/test/java/org/baeldung/caching/test/CacheManagerEvictIntegrationTest.java b/spring-all/src/test/java/org/baeldung/caching/test/CacheManagerEvictIntegrationTest.java index e65e8800a2..9c6aaea892 100644 --- a/spring-all/src/test/java/org/baeldung/caching/test/CacheManagerEvictIntegrationTest.java +++ b/spring-all/src/test/java/org/baeldung/caching/test/CacheManagerEvictIntegrationTest.java @@ -4,18 +4,59 @@ import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.nullValue; import static org.junit.Assert.assertThat; -import org.baeldung.caching.eviction.CacheEvictionMain; +import java.util.ArrayList; +import java.util.List; + import org.baeldung.caching.eviction.service.CachingService; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.cache.Cache; +import org.springframework.cache.CacheManager; +import org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean; +import org.springframework.cache.support.SimpleCacheManager; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; -@RunWith(SpringRunner.class) -@SpringBootTest(classes = { CacheEvictionMain.class, CachingService.class }) +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration public class CacheManagerEvictIntegrationTest { + + @Configuration + static class ContextConfiguration { + + @Bean + public CachingService cachingService() { + return new CachingService(); + } + + @Bean + public CacheManager cacheManager(){ + SimpleCacheManager cacheManager = new SimpleCacheManager(); + List caches = new ArrayList<>(); + caches.add(cacheBeanFirst().getObject()); + caches.add(cacheBeanSecond().getObject()); + cacheManager.setCaches(caches ); + return cacheManager; + } + + @Bean + public ConcurrentMapCacheFactoryBean cacheBeanFirst(){ + ConcurrentMapCacheFactoryBean cacheFactoryBean = new ConcurrentMapCacheFactoryBean(); + cacheFactoryBean.setName("first"); + return cacheFactoryBean; + } + + @Bean + public ConcurrentMapCacheFactoryBean cacheBeanSecond(){ + ConcurrentMapCacheFactoryBean cacheFactoryBean = new ConcurrentMapCacheFactoryBean(); + cacheFactoryBean.setName("second"); + return cacheFactoryBean; + } + } @Autowired CachingService cachingService;