BAEL-19967: Migrate spring-caching module to the com.baeldung package

This commit is contained in:
Krzysiek
2019-12-18 21:31:15 +01:00
parent 9df9e5b068
commit 1e15094846
18 changed files with 31 additions and 31 deletions

View File

@@ -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()));
}
}

View File

@@ -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()));
}
}

View File

@@ -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);
}
}

View File

@@ -0,0 +1,40 @@
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 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");
}
}
}