From e848a0ffcfb907d6db86e14d43bee8cd6032225f Mon Sep 17 00:00:00 2001 From: Denis Date: Sat, 31 Aug 2019 15:51:56 +0200 Subject: [PATCH] extract spring-caching module --- spring-all/README.md | 3 - spring-all/pom.xml | 5 - .../main/java/org/baeldung/model/User.java | 2 +- .../baeldung/repository/UserRepository.java | 2 +- .../UserRepositoryIntegrationTest.java | 2 +- spring-caching/README.md | 4 + spring-caching/pom.xml | 43 ++++ .../config/ApplicationCacheConfig.java | 0 .../caching/config/CachingConfig.java | 0 .../caching/config/CustomKeyGenerator.java | 0 .../controllers/CachingController.java | 36 ++-- .../eviction/service/CachingService.java | 106 +++++----- .../caching/example/AbstractService.java | 0 .../baeldung/caching/example/BookService.java | 2 +- .../baeldung/caching/example/Customer.java | 0 .../caching/example/CustomerDataService.java | 0 .../example/CustomerServiceWithParent.java | 0 .../org/baeldung/caching}/model/Book.java | 2 +- .../ehcache/calculator/SquaredCalculator.java | 0 .../baeldung/ehcache/config/CacheHelper.java | 0 .../src/main/resources/config.xml | 0 .../CacheEvictAnnotationIntegrationTest.java | 158 +++++++------- .../CacheManagerEvictIntegrationTest.java | 192 +++++++++--------- .../test/SpringCachingIntegrationTest.java | 0 .../ehcache/SquareCalculatorUnitTest.java | 80 ++++---- 25 files changed, 338 insertions(+), 299 deletions(-) create mode 100644 spring-caching/README.md create mode 100644 spring-caching/pom.xml rename {spring-all => spring-caching}/src/main/java/org/baeldung/caching/config/ApplicationCacheConfig.java (100%) rename {spring-all => spring-caching}/src/main/java/org/baeldung/caching/config/CachingConfig.java (100%) rename {spring-all => spring-caching}/src/main/java/org/baeldung/caching/config/CustomKeyGenerator.java (100%) rename {spring-all => spring-caching}/src/main/java/org/baeldung/caching/eviction/controllers/CachingController.java (96%) rename {spring-all => spring-caching}/src/main/java/org/baeldung/caching/eviction/service/CachingService.java (96%) rename {spring-all => spring-caching}/src/main/java/org/baeldung/caching/example/AbstractService.java (100%) rename {spring-all => spring-caching}/src/main/java/org/baeldung/caching/example/BookService.java (93%) rename {spring-all => spring-caching}/src/main/java/org/baeldung/caching/example/Customer.java (100%) rename {spring-all => spring-caching}/src/main/java/org/baeldung/caching/example/CustomerDataService.java (100%) rename {spring-all => spring-caching}/src/main/java/org/baeldung/caching/example/CustomerServiceWithParent.java (100%) rename {spring-all/src/main/java/org/baeldung => spring-caching/src/main/java/org/baeldung/caching}/model/Book.java (94%) rename {spring-all => spring-caching}/src/main/java/org/baeldung/ehcache/calculator/SquaredCalculator.java (100%) mode change 100755 => 100644 rename {spring-all => spring-caching}/src/main/java/org/baeldung/ehcache/config/CacheHelper.java (100%) mode change 100755 => 100644 rename {spring-all => spring-caching}/src/main/resources/config.xml (100%) rename {spring-all => spring-caching}/src/test/java/org/baeldung/caching/test/CacheEvictAnnotationIntegrationTest.java (96%) rename {spring-all => spring-caching}/src/test/java/org/baeldung/caching/test/CacheManagerEvictIntegrationTest.java (97%) rename {spring-all => spring-caching}/src/test/java/org/baeldung/caching/test/SpringCachingIntegrationTest.java (100%) rename {spring-all => spring-caching}/src/test/java/org/baeldung/ehcache/SquareCalculatorUnitTest.java (97%) diff --git a/spring-all/README.md b/spring-all/README.md index 3db7599914..95b87cfee1 100644 --- a/spring-all/README.md +++ b/spring-all/README.md @@ -17,15 +17,12 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring - [Running Setup Data on Startup in Spring](http://www.baeldung.com/running-setup-logic-on-startup-in-spring) - [Quick Guide to Spring Controllers](http://www.baeldung.com/spring-controllers) - [Quick Guide to Spring Bean Scopes](http://www.baeldung.com/spring-bean-scopes) -- [Introduction To Ehcache](http://www.baeldung.com/ehcache) - [Custom Scope in Spring](http://www.baeldung.com/spring-custom-scope) - [A CLI with Spring Shell](http://www.baeldung.com/spring-shell-cli) - [JasperReports with Spring](http://www.baeldung.com/spring-jasper) - [Model, ModelMap, and ModelView in Spring MVC](http://www.baeldung.com/spring-mvc-model-model-map-model-view) -- [A Guide To Caching in Spring](http://www.baeldung.com/spring-cache-tutorial) - [@Order in Spring](http://www.baeldung.com/spring-order) - [Spring Web Contexts](http://www.baeldung.com/spring-web-contexts) -- [Spring Cache – Creating a Custom KeyGenerator](http://www.baeldung.com/spring-cache-custom-keygenerator) - [Spring @Primary Annotation](http://www.baeldung.com/spring-primary) - [Spring Events](https://www.baeldung.com/spring-events) - [Spring Null-Safety Annotations](https://www.baeldung.com/spring-null-safety-annotations) diff --git a/spring-all/pom.xml b/spring-all/pom.xml index 8c88efb970..1b733555da 100644 --- a/spring-all/pom.xml +++ b/spring-all/pom.xml @@ -147,10 +147,6 @@ ${easymock.version} test - - org.ehcache - ehcache - org.apache.logging.log4j log4j-api @@ -243,7 +239,6 @@ 25.1-jre - 3.5.2 3.6 3.6.1 6.6.0 diff --git a/spring-boot/src/main/java/org/baeldung/model/User.java b/spring-boot/src/main/java/org/baeldung/model/User.java index eb886338a0..049b118b12 100644 --- a/spring-boot/src/main/java/org/baeldung/model/User.java +++ b/spring-boot/src/main/java/org/baeldung/model/User.java @@ -1,4 +1,4 @@ -package org.baeldung.model; +package org.baeldung.caching.model; import javax.persistence.Entity; import javax.persistence.GeneratedValue; diff --git a/spring-boot/src/main/java/org/baeldung/repository/UserRepository.java b/spring-boot/src/main/java/org/baeldung/repository/UserRepository.java index 7335af2616..0113c4c5d4 100644 --- a/spring-boot/src/main/java/org/baeldung/repository/UserRepository.java +++ b/spring-boot/src/main/java/org/baeldung/repository/UserRepository.java @@ -1,6 +1,6 @@ package org.baeldung.repository; -import org.baeldung.model.User; +import org.baeldung.caching.model.User; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Sort; diff --git a/spring-boot/src/test/java/org/baeldung/repository/UserRepositoryIntegrationTest.java b/spring-boot/src/test/java/org/baeldung/repository/UserRepositoryIntegrationTest.java index 04bb1170f8..54ce3b4bf3 100644 --- a/spring-boot/src/test/java/org/baeldung/repository/UserRepositoryIntegrationTest.java +++ b/spring-boot/src/test/java/org/baeldung/repository/UserRepositoryIntegrationTest.java @@ -1,7 +1,7 @@ package org.baeldung.repository; import org.baeldung.boot.config.H2JpaConfig; -import org.baeldung.model.User; +import org.baeldung.caching.model.User; import org.junit.After; import org.junit.Test; import org.junit.runner.RunWith; diff --git a/spring-caching/README.md b/spring-caching/README.md new file mode 100644 index 0000000000..14dfe94041 --- /dev/null +++ b/spring-caching/README.md @@ -0,0 +1,4 @@ +### Relevant articles: +- [Introduction To Ehcache](http://www.baeldung.com/ehcache) +- [A Guide To Caching in Spring](http://www.baeldung.com/spring-cache-tutorial) +- [Spring Cache – Creating a Custom KeyGenerator](http://www.baeldung.com/spring-cache-custom-keygenerator) diff --git a/spring-caching/pom.xml b/spring-caching/pom.xml new file mode 100644 index 0000000000..3329cd5f77 --- /dev/null +++ b/spring-caching/pom.xml @@ -0,0 +1,43 @@ + + 4.0.0 + spring-caching + 0.1-SNAPSHOT + spring-caching + war + + + parent-boot-2 + com.baeldung + 0.0.1-SNAPSHOT + ../parent-boot-2 + + + + + org.springframework + spring-context + + + org.springframework + spring-web + + + org.springframework + spring-webmvc + + + org.ehcache + ehcache + + + org.springframework + spring-test + test + + + + + 3.5.2 + + \ No newline at end of file diff --git a/spring-all/src/main/java/org/baeldung/caching/config/ApplicationCacheConfig.java b/spring-caching/src/main/java/org/baeldung/caching/config/ApplicationCacheConfig.java similarity index 100% rename from spring-all/src/main/java/org/baeldung/caching/config/ApplicationCacheConfig.java rename to spring-caching/src/main/java/org/baeldung/caching/config/ApplicationCacheConfig.java diff --git a/spring-all/src/main/java/org/baeldung/caching/config/CachingConfig.java b/spring-caching/src/main/java/org/baeldung/caching/config/CachingConfig.java similarity index 100% rename from spring-all/src/main/java/org/baeldung/caching/config/CachingConfig.java rename to spring-caching/src/main/java/org/baeldung/caching/config/CachingConfig.java diff --git a/spring-all/src/main/java/org/baeldung/caching/config/CustomKeyGenerator.java b/spring-caching/src/main/java/org/baeldung/caching/config/CustomKeyGenerator.java similarity index 100% rename from spring-all/src/main/java/org/baeldung/caching/config/CustomKeyGenerator.java rename to spring-caching/src/main/java/org/baeldung/caching/config/CustomKeyGenerator.java diff --git a/spring-all/src/main/java/org/baeldung/caching/eviction/controllers/CachingController.java b/spring-caching/src/main/java/org/baeldung/caching/eviction/controllers/CachingController.java similarity index 96% rename from spring-all/src/main/java/org/baeldung/caching/eviction/controllers/CachingController.java rename to spring-caching/src/main/java/org/baeldung/caching/eviction/controllers/CachingController.java index 675cb7a516..aaaf8a5918 100644 --- a/spring-all/src/main/java/org/baeldung/caching/eviction/controllers/CachingController.java +++ b/spring-caching/src/main/java/org/baeldung/caching/eviction/controllers/CachingController.java @@ -1,18 +1,18 @@ -package org.baeldung.caching.eviction.controllers; - -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(); - } -} +package org.baeldung.caching.eviction.controllers; + +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-caching/src/main/java/org/baeldung/caching/eviction/service/CachingService.java similarity index 96% rename from spring-all/src/main/java/org/baeldung/caching/eviction/service/CachingService.java rename to spring-caching/src/main/java/org/baeldung/caching/eviction/service/CachingService.java index 7e37215954..a0dd30c329 100644 --- a/spring-all/src/main/java/org/baeldung/caching/eviction/service/CachingService.java +++ b/spring-caching/src/main/java/org/baeldung/caching/eviction/service/CachingService.java @@ -1,53 +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(); - } -} +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/main/java/org/baeldung/caching/example/AbstractService.java b/spring-caching/src/main/java/org/baeldung/caching/example/AbstractService.java similarity index 100% rename from spring-all/src/main/java/org/baeldung/caching/example/AbstractService.java rename to spring-caching/src/main/java/org/baeldung/caching/example/AbstractService.java diff --git a/spring-all/src/main/java/org/baeldung/caching/example/BookService.java b/spring-caching/src/main/java/org/baeldung/caching/example/BookService.java similarity index 93% rename from spring-all/src/main/java/org/baeldung/caching/example/BookService.java rename to spring-caching/src/main/java/org/baeldung/caching/example/BookService.java index 26118d61de..9f3aaf89cf 100644 --- a/spring-all/src/main/java/org/baeldung/caching/example/BookService.java +++ b/spring-caching/src/main/java/org/baeldung/caching/example/BookService.java @@ -1,6 +1,6 @@ package org.baeldung.caching.example; -import org.baeldung.model.Book; +import org.baeldung.caching.model.Book; import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Component; diff --git a/spring-all/src/main/java/org/baeldung/caching/example/Customer.java b/spring-caching/src/main/java/org/baeldung/caching/example/Customer.java similarity index 100% rename from spring-all/src/main/java/org/baeldung/caching/example/Customer.java rename to spring-caching/src/main/java/org/baeldung/caching/example/Customer.java diff --git a/spring-all/src/main/java/org/baeldung/caching/example/CustomerDataService.java b/spring-caching/src/main/java/org/baeldung/caching/example/CustomerDataService.java similarity index 100% rename from spring-all/src/main/java/org/baeldung/caching/example/CustomerDataService.java rename to spring-caching/src/main/java/org/baeldung/caching/example/CustomerDataService.java diff --git a/spring-all/src/main/java/org/baeldung/caching/example/CustomerServiceWithParent.java b/spring-caching/src/main/java/org/baeldung/caching/example/CustomerServiceWithParent.java similarity index 100% rename from spring-all/src/main/java/org/baeldung/caching/example/CustomerServiceWithParent.java rename to spring-caching/src/main/java/org/baeldung/caching/example/CustomerServiceWithParent.java diff --git a/spring-all/src/main/java/org/baeldung/model/Book.java b/spring-caching/src/main/java/org/baeldung/caching/model/Book.java similarity index 94% rename from spring-all/src/main/java/org/baeldung/model/Book.java rename to spring-caching/src/main/java/org/baeldung/caching/model/Book.java index 9305ce9653..c110c79db0 100644 --- a/spring-all/src/main/java/org/baeldung/model/Book.java +++ b/spring-caching/src/main/java/org/baeldung/caching/model/Book.java @@ -1,4 +1,4 @@ -package org.baeldung.model; +package org.baeldung.caching.model; public class Book { diff --git a/spring-all/src/main/java/org/baeldung/ehcache/calculator/SquaredCalculator.java b/spring-caching/src/main/java/org/baeldung/ehcache/calculator/SquaredCalculator.java old mode 100755 new mode 100644 similarity index 100% rename from spring-all/src/main/java/org/baeldung/ehcache/calculator/SquaredCalculator.java rename to spring-caching/src/main/java/org/baeldung/ehcache/calculator/SquaredCalculator.java diff --git a/spring-all/src/main/java/org/baeldung/ehcache/config/CacheHelper.java b/spring-caching/src/main/java/org/baeldung/ehcache/config/CacheHelper.java old mode 100755 new mode 100644 similarity index 100% rename from spring-all/src/main/java/org/baeldung/ehcache/config/CacheHelper.java rename to spring-caching/src/main/java/org/baeldung/ehcache/config/CacheHelper.java diff --git a/spring-all/src/main/resources/config.xml b/spring-caching/src/main/resources/config.xml similarity index 100% rename from spring-all/src/main/resources/config.xml rename to spring-caching/src/main/resources/config.xml diff --git a/spring-all/src/test/java/org/baeldung/caching/test/CacheEvictAnnotationIntegrationTest.java b/spring-caching/src/test/java/org/baeldung/caching/test/CacheEvictAnnotationIntegrationTest.java similarity index 96% rename from spring-all/src/test/java/org/baeldung/caching/test/CacheEvictAnnotationIntegrationTest.java rename to spring-caching/src/test/java/org/baeldung/caching/test/CacheEvictAnnotationIntegrationTest.java index f24cdef917..e00305d4a2 100644 --- a/spring-all/src/test/java/org/baeldung/caching/test/CacheEvictAnnotationIntegrationTest.java +++ b/spring-caching/src/test/java/org/baeldung/caching/test/CacheEvictAnnotationIntegrationTest.java @@ -1,79 +1,79 @@ -package org.baeldung.caching.test; - -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.CoreMatchers.nullValue; -import static org.junit.Assert.assertThat; - -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.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(SpringJUnit4ClassRunner.class) -@ContextConfiguration -public class CacheEvictAnnotationIntegrationTest { - - @Configuration - @EnableCaching - static class ContextConfiguration { - - @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; - } - } - - @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())); - } -} +package org.baeldung.caching.test; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.nullValue; +import static org.junit.Assert.assertThat; + +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.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(SpringJUnit4ClassRunner.class) +@ContextConfiguration +public class CacheEvictAnnotationIntegrationTest { + + @Configuration + @EnableCaching + static class ContextConfiguration { + + @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; + } + } + + @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-caching/src/test/java/org/baeldung/caching/test/CacheManagerEvictIntegrationTest.java similarity index 97% rename from spring-all/src/test/java/org/baeldung/caching/test/CacheManagerEvictIntegrationTest.java rename to spring-caching/src/test/java/org/baeldung/caching/test/CacheManagerEvictIntegrationTest.java index 9c6aaea892..4b12ed60b6 100644 --- a/spring-all/src/test/java/org/baeldung/caching/test/CacheManagerEvictIntegrationTest.java +++ b/spring-caching/src/test/java/org/baeldung/caching/test/CacheManagerEvictIntegrationTest.java @@ -1,96 +1,96 @@ -package org.baeldung.caching.test; - -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.CoreMatchers.nullValue; -import static org.junit.Assert.assertThat; - -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.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(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; - - @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())); - } -} +package org.baeldung.caching.test; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.nullValue; +import static org.junit.Assert.assertThat; + +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.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(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; + + @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-all/src/test/java/org/baeldung/caching/test/SpringCachingIntegrationTest.java b/spring-caching/src/test/java/org/baeldung/caching/test/SpringCachingIntegrationTest.java similarity index 100% rename from spring-all/src/test/java/org/baeldung/caching/test/SpringCachingIntegrationTest.java rename to spring-caching/src/test/java/org/baeldung/caching/test/SpringCachingIntegrationTest.java diff --git a/spring-all/src/test/java/org/baeldung/ehcache/SquareCalculatorUnitTest.java b/spring-caching/src/test/java/org/baeldung/ehcache/SquareCalculatorUnitTest.java similarity index 97% rename from spring-all/src/test/java/org/baeldung/ehcache/SquareCalculatorUnitTest.java rename to spring-caching/src/test/java/org/baeldung/ehcache/SquareCalculatorUnitTest.java index 96265c11f5..0e6ac6fd8e 100644 --- a/spring-all/src/test/java/org/baeldung/ehcache/SquareCalculatorUnitTest.java +++ b/spring-caching/src/test/java/org/baeldung/ehcache/SquareCalculatorUnitTest.java @@ -1,40 +1,40 @@ -package org.baeldung.ehcache; - -import org.baeldung.ehcache.calculator.SquaredCalculator; -import org.baeldung.ehcache.config.CacheHelper; -import org.junit.Before; -import org.junit.Test; - -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; - -public class SquareCalculatorUnitTest { - private SquaredCalculator squaredCalculator = new SquaredCalculator(); - private CacheHelper cacheHelper = new CacheHelper(); - - @Before - public void setup() { - squaredCalculator.setCache(cacheHelper); - } - - @Test - public void whenCalculatingSquareValueOnce_thenCacheDontHaveValues() { - for (int i = 10; i < 15; i++) { - assertFalse(cacheHelper.getSquareNumberCache().containsKey(i)); - System.out.println("Square value of " + i + " is: " + squaredCalculator.getSquareValueOfNumber(i) + "\n"); - } - } - - @Test - public void whenCalculatingSquareValueAgain_thenCacheHasAllValues() { - for (int i = 10; i < 15; i++) { - assertFalse(cacheHelper.getSquareNumberCache().containsKey(i)); - System.out.println("Square value of " + i + " is: " + squaredCalculator.getSquareValueOfNumber(i) + "\n"); - } - - for (int i = 10; i < 15; i++) { - assertTrue(cacheHelper.getSquareNumberCache().containsKey(i)); - System.out.println("Square value of " + i + " is: " + squaredCalculator.getSquareValueOfNumber(i) + "\n"); - } - } -} +package org.baeldung.ehcache; + +import org.baeldung.ehcache.calculator.SquaredCalculator; +import org.baeldung.ehcache.config.CacheHelper; +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +public class SquareCalculatorUnitTest { + private SquaredCalculator squaredCalculator = new SquaredCalculator(); + private CacheHelper cacheHelper = new CacheHelper(); + + @Before + public void setup() { + squaredCalculator.setCache(cacheHelper); + } + + @Test + public void whenCalculatingSquareValueOnce_thenCacheDontHaveValues() { + for (int i = 10; i < 15; i++) { + assertFalse(cacheHelper.getSquareNumberCache().containsKey(i)); + System.out.println("Square value of " + i + " is: " + squaredCalculator.getSquareValueOfNumber(i) + "\n"); + } + } + + @Test + public void whenCalculatingSquareValueAgain_thenCacheHasAllValues() { + for (int i = 10; i < 15; i++) { + assertFalse(cacheHelper.getSquareNumberCache().containsKey(i)); + System.out.println("Square value of " + i + " is: " + squaredCalculator.getSquareValueOfNumber(i) + "\n"); + } + + for (int i = 10; i < 15; i++) { + assertTrue(cacheHelper.getSquareNumberCache().containsKey(i)); + System.out.println("Square value of " + i + " is: " + squaredCalculator.getSquareValueOfNumber(i) + "\n"); + } + } +}