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;