JAVA-13870 Move spring-caching,spring-caching-2 to spring-boot-module… (#13457)
* JAVA-13870 Move spring-caching,spring-caching-2 to spring-boot-modules (conti-1) * JAVA-13870 Making spring boot as the parent for spring-caching and spring caching-2 --------- Co-authored-by: timis1 <noreplay@yahoo.com>
This commit is contained in:
+24
@@ -0,0 +1,24 @@
|
||||
package com.baeldung.caching.boot;
|
||||
|
||||
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.cache.CacheManager;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest("spring.cache.type=simple")
|
||||
public class SimpleCacheCustomizerIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private CacheManager cacheManager;
|
||||
|
||||
@Test
|
||||
public void givenCacheManagerCustomizerWhenBootstrappedThenCacheManagerCustomized() {
|
||||
assertThat(cacheManager.getCacheNames())
|
||||
.containsOnly(SimpleCacheCustomizer.USERS_CACHE, SimpleCacheCustomizer.TRANSACTIONS_CACHE);
|
||||
}
|
||||
}
|
||||
+79
@@ -0,0 +1,79 @@
|
||||
package com.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 com.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<Cache> 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()));
|
||||
}
|
||||
}
|
||||
+96
@@ -0,0 +1,96 @@
|
||||
package com.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 com.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<Cache> 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()));
|
||||
}
|
||||
}
|
||||
+71
@@ -0,0 +1,71 @@
|
||||
package com.baeldung.caching.test;
|
||||
|
||||
import com.baeldung.caching.config.CachingConfig;
|
||||
import com.baeldung.caching.example.Customer;
|
||||
import com.baeldung.caching.example.CustomerDataService;
|
||||
import com.baeldung.caching.example.CustomerServiceWithParent;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = { CachingConfig.class }, loader = AnnotationConfigContextLoader.class)
|
||||
public class SpringCachingIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private CustomerDataService service;
|
||||
|
||||
@Autowired
|
||||
private CustomerServiceWithParent serviceWithParent;
|
||||
|
||||
//
|
||||
|
||||
@Test
|
||||
public void whenGettingAddress_thenCorrect() {
|
||||
final Customer cust = new Customer("Tom", "67-2, Downing Street, NY");
|
||||
service.getAddress(cust);
|
||||
service.getAddress(cust);
|
||||
|
||||
service.getAddress1(cust);
|
||||
service.getAddress1(cust);
|
||||
|
||||
service.getAddress2(cust);
|
||||
service.getAddress2(cust);
|
||||
|
||||
service.getAddress3(cust);
|
||||
service.getAddress3(cust);
|
||||
|
||||
service.getAddress4(cust);
|
||||
service.getAddress4(cust);
|
||||
|
||||
service.getAddress5(cust);
|
||||
service.getAddress5(cust);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUsingServiceWithParent_whenGettingAddress_thenCorrect() {
|
||||
final Customer cust = new Customer("Tom", "67-2, Downing Street, NY");
|
||||
|
||||
serviceWithParent.getAddress(cust);
|
||||
serviceWithParent.getAddress(cust);
|
||||
|
||||
serviceWithParent.getAddress1(cust);
|
||||
serviceWithParent.getAddress1(cust);
|
||||
|
||||
serviceWithParent.getAddress2(cust);
|
||||
serviceWithParent.getAddress2(cust);
|
||||
|
||||
serviceWithParent.getAddress3(cust);
|
||||
serviceWithParent.getAddress3(cust);
|
||||
|
||||
// serviceWithParent.getAddress4(cust);
|
||||
// serviceWithParent.getAddress4(cust);
|
||||
|
||||
serviceWithParent.getAddress5(cust);
|
||||
serviceWithParent.getAddress5(cust);
|
||||
}
|
||||
|
||||
}
|
||||
+45
@@ -0,0 +1,45 @@
|
||||
package com.baeldung.ehcache;
|
||||
|
||||
import com.baeldung.ehcache.calculator.SquaredCalculator;
|
||||
import com.baeldung.ehcache.config.CacheHelper;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class SquareCalculatorUnitTest {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(SquareCalculatorUnitTest.class);
|
||||
|
||||
private final SquaredCalculator squaredCalculator = new SquaredCalculator();
|
||||
private final 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));
|
||||
LOGGER.debug("Square value of {} is: {}", i, squaredCalculator.getSquareValueOfNumber(i));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCalculatingSquareValueAgain_thenCacheHasAllValues() {
|
||||
for (int i = 10; i < 15; i++) {
|
||||
assertFalse(cacheHelper.getSquareNumberCache().containsKey(i));
|
||||
LOGGER.debug("Square value of {} is: {}", i, squaredCalculator.getSquareValueOfNumber(i));
|
||||
}
|
||||
|
||||
for (int i = 10; i < 15; i++) {
|
||||
assertTrue(cacheHelper.getSquareNumberCache().containsKey(i));
|
||||
LOGGER.debug("Square value of {} is: {}", i, squaredCalculator.getSquareValueOfNumber(i) + "\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
+64
@@ -0,0 +1,64 @@
|
||||
package com.baeldung.multiplecachemanager;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.Mockito;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||
import org.springframework.cache.Cache;
|
||||
import org.springframework.cache.CacheManager;
|
||||
import org.springframework.cache.caffeine.CaffeineCache;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import com.baeldung.multiplecachemanager.bo.OrderDetailBO;
|
||||
import com.baeldung.multiplecachemanager.entity.Order;
|
||||
import com.baeldung.multiplecachemanager.repository.OrderDetailRepository;
|
||||
|
||||
@SpringBootApplication
|
||||
@SpringBootTest
|
||||
public class MultipleCacheManagerIntegrationTest {
|
||||
|
||||
@MockBean
|
||||
private OrderDetailRepository orderDetailRepository;
|
||||
|
||||
@Autowired
|
||||
private OrderDetailBO orderDetailBO;
|
||||
|
||||
@Autowired
|
||||
private CacheManager cacheManager;
|
||||
|
||||
@Autowired
|
||||
private CacheManager alternateCacheManager;
|
||||
|
||||
@Test
|
||||
public void givenCacheResolverIsConfigured_whenCallGetOrderDetail_thenDataShouldBeInCaffieneCacheManager() {
|
||||
Integer key = 30001;
|
||||
cacheManager.getCache("orders")
|
||||
.evict(key);
|
||||
Order order = new Order();
|
||||
order.setCustomerId(1001);
|
||||
order.setItemId(10001);
|
||||
order.setOrderId(30001);
|
||||
order.setQuantity(2);
|
||||
Mockito.when(orderDetailRepository.getOrderDetail(key))
|
||||
.thenReturn(order);
|
||||
orderDetailBO.getOrderDetail(key);
|
||||
org.springframework.cache.caffeine.CaffeineCache cache = (CaffeineCache) cacheManager.getCache("orders");
|
||||
Assert.notNull(cache.get(key)
|
||||
.get(), "caffieneCache should have had the data");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCacheResolverIsConfigured_whenCallGetOrderPrice_thenDataShouldBeInAlternateCacheManager() {
|
||||
Integer key = 30001;
|
||||
alternateCacheManager.getCache("orderprice")
|
||||
.evict(key);
|
||||
Mockito.when(orderDetailRepository.getOrderPrice(key))
|
||||
.thenReturn(500.0);
|
||||
orderDetailBO.getOrderPrice(key);
|
||||
Cache cache = alternateCacheManager.getCache("orderprice");
|
||||
Assert.notNull(cache.get(key)
|
||||
.get(), "alternateCache should have had the data");
|
||||
}
|
||||
}
|
||||
+86
@@ -0,0 +1,86 @@
|
||||
package com.baeldung.springdatacaching.repositories;
|
||||
|
||||
import com.baeldung.springdatacaching.model.Book;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.cache.CacheManager;
|
||||
import org.springframework.cache.annotation.EnableCaching;
|
||||
import org.springframework.cache.concurrent.ConcurrentMapCacheManager;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||
import org.springframework.test.util.AopTestUtils;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
import static java.util.Optional.of;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
@ContextConfiguration
|
||||
@ExtendWith(SpringExtension.class)
|
||||
public class BookRepositoryCachingIntegrationTest {
|
||||
|
||||
private static final Book DUNE = new Book(UUID.randomUUID(), "Dune");
|
||||
private static final Book FOUNDATION = new Book(UUID.randomUUID(), "Foundation");
|
||||
|
||||
private BookRepository mock;
|
||||
|
||||
@Autowired
|
||||
private BookRepository bookRepository;
|
||||
|
||||
@EnableCaching
|
||||
@Configuration
|
||||
public static class CachingTestConfig {
|
||||
|
||||
@Bean
|
||||
public BookRepository bookRepositoryMockImplementation() {
|
||||
return mock(BookRepository.class);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public CacheManager cacheManager() {
|
||||
return new ConcurrentMapCacheManager("books");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
mock = AopTestUtils.getTargetObject(bookRepository);
|
||||
|
||||
reset(mock);
|
||||
|
||||
when(mock.findFirstByTitle(eq("Foundation")))
|
||||
.thenReturn(of(FOUNDATION));
|
||||
|
||||
when(mock.findFirstByTitle(eq("Dune")))
|
||||
.thenReturn(of(DUNE))
|
||||
.thenThrow(new RuntimeException("Book should be cached!"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenCachedBook_whenFindByTitle_thenRepositoryShouldNotBeHit() {
|
||||
assertEquals(of(DUNE), bookRepository.findFirstByTitle("Dune"));
|
||||
verify(mock).findFirstByTitle("Dune");
|
||||
|
||||
assertEquals(of(DUNE), bookRepository.findFirstByTitle("Dune"));
|
||||
assertEquals(of(DUNE), bookRepository.findFirstByTitle("Dune"));
|
||||
|
||||
verifyNoMoreInteractions(mock);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenNotCachedBook_whenFindByTitle_thenRepositoryShouldBeHit() {
|
||||
assertEquals(of(FOUNDATION), bookRepository.findFirstByTitle("Foundation"));
|
||||
assertEquals(of(FOUNDATION), bookRepository.findFirstByTitle("Foundation"));
|
||||
assertEquals(of(FOUNDATION), bookRepository.findFirstByTitle("Foundation"));
|
||||
|
||||
verify(mock, times(3)).findFirstByTitle("Foundation");
|
||||
}
|
||||
|
||||
}
|
||||
+58
@@ -0,0 +1,58 @@
|
||||
package com.baeldung.springdatacaching.repositories;
|
||||
|
||||
import com.baeldung.caching.boot.CacheApplication;
|
||||
import com.baeldung.springdatacaching.model.Book;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.domain.EntityScan;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.cache.CacheManager;
|
||||
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
|
||||
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
import static java.util.Optional.empty;
|
||||
import static java.util.Optional.ofNullable;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
@ExtendWith(SpringExtension.class)
|
||||
@EntityScan(basePackageClasses = Book.class)
|
||||
@SpringBootTest(classes = CacheApplication.class)
|
||||
@EnableJpaRepositories(basePackageClasses = BookRepository.class)
|
||||
public class BookRepositoryIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
CacheManager cacheManager;
|
||||
|
||||
@Autowired
|
||||
BookRepository repository;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
repository.save(new Book(UUID.randomUUID(), "Dune"));
|
||||
repository.save(new Book(UUID.randomUUID(), "Foundation"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenBookThatShouldBeCached_whenFindByTitle_thenResultShouldBePutInCache() {
|
||||
Optional<Book> dune = repository.findFirstByTitle("Dune");
|
||||
|
||||
assertEquals(dune, getCachedBook("Dune"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenBookThatShouldNotBeCached_whenFindByTitle_thenResultShouldNotBePutInCache() {
|
||||
repository.findFirstByTitle("Foundation");
|
||||
|
||||
assertEquals(empty(), getCachedBook("Foundation"));
|
||||
}
|
||||
|
||||
private Optional<Book> getCachedBook(String title) {
|
||||
return ofNullable(cacheManager.getCache("books")).map(c -> c.get(title, Book.class));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user