polishing
This fixes a Java6 backward compatible issue introduced in the JCache implementation. This commit also adds new representative tests. Issue: SPR-9616
This commit is contained in:
@@ -16,6 +16,8 @@
|
||||
|
||||
package org.springframework.cache;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@@ -44,4 +46,25 @@ public class CacheTestUtils {
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Assert the following key is not held within the specified cache(s).
|
||||
*/
|
||||
public static void assertCacheMiss(Object key, Cache... caches) {
|
||||
for (Cache cache : caches) {
|
||||
assertNull("No entry in " + cache + " should have been found with key " + key, cache.get(key));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Assert the following key has a matching value within the specified cache(s).
|
||||
*/
|
||||
public static void assertCacheHit(Object key, Object value, Cache... caches) {
|
||||
for (Cache cache : caches) {
|
||||
Cache.ValueWrapper wrapper = cache.get(key);
|
||||
assertNotNull("An entry in " + cache + " should have been found with key " + key, wrapper);
|
||||
assertEquals("Wrong value in " + cache + " for entry with key " + key, value, wrapper.get());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
99
spring-context/src/test/java/org/springframework/cache/config/EnableCachingIntegrationTests.java
vendored
Normal file
99
spring-context/src/test/java/org/springframework/cache/config/EnableCachingIntegrationTests.java
vendored
Normal file
@@ -0,0 +1,99 @@
|
||||
package org.springframework.cache.config;
|
||||
|
||||
import static org.springframework.cache.CacheTestUtils.*;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.cache.Cache;
|
||||
import org.springframework.cache.CacheManager;
|
||||
import org.springframework.cache.CacheTestUtils;
|
||||
import org.springframework.cache.annotation.CacheConfig;
|
||||
import org.springframework.cache.annotation.Cacheable;
|
||||
import org.springframework.cache.annotation.CachingConfigurerSupport;
|
||||
import org.springframework.cache.annotation.EnableCaching;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
|
||||
/**
|
||||
* Tests that represent real use cases with advanced configuration
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
public class EnableCachingIntegrationTests {
|
||||
|
||||
@Test
|
||||
public void fooServiceWithInterface() {
|
||||
ConfigurableApplicationContext context = new AnnotationConfigApplicationContext(FooConfig.class);
|
||||
FooService service = context.getBean(FooService.class);
|
||||
fooGetSimple(context, service);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void fooServiceWithInterfaceCglib() {
|
||||
ConfigurableApplicationContext context = new AnnotationConfigApplicationContext(FooConfigCglib.class);
|
||||
FooService service = context.getBean(FooService.class);
|
||||
fooGetSimple(context, service);
|
||||
}
|
||||
|
||||
private void fooGetSimple(ApplicationContext context, FooService service) {
|
||||
CacheManager cacheManager = context.getBean(CacheManager.class);
|
||||
|
||||
Cache cache = cacheManager.getCache("default");
|
||||
|
||||
Object key = new Object();
|
||||
assertCacheMiss(key, cache);
|
||||
|
||||
Object value = service.getSimple(key);
|
||||
assertCacheHit(key, value, cache);
|
||||
}
|
||||
|
||||
@Configuration
|
||||
static class SharedConfig extends CachingConfigurerSupport {
|
||||
@Override
|
||||
@Bean
|
||||
public CacheManager cacheManager() {
|
||||
return CacheTestUtils.createSimpleCacheManager("default");
|
||||
}
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@Import(SharedConfig.class)
|
||||
@EnableCaching
|
||||
static class FooConfig {
|
||||
@Bean
|
||||
public FooService fooService() {
|
||||
return new FooServiceImpl();
|
||||
}
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@Import(SharedConfig.class)
|
||||
@EnableCaching(proxyTargetClass = true)
|
||||
static class FooConfigCglib {
|
||||
@Bean
|
||||
public FooService fooService() {
|
||||
return new FooServiceImpl();
|
||||
}
|
||||
}
|
||||
|
||||
private static interface FooService {
|
||||
public Object getSimple(Object key);
|
||||
}
|
||||
|
||||
@CacheConfig(cacheNames = "default")
|
||||
private static class FooServiceImpl implements FooService {
|
||||
private final AtomicLong counter = new AtomicLong();
|
||||
|
||||
@Override
|
||||
@Cacheable
|
||||
public Object getSimple(Object key) {
|
||||
return counter.getAndIncrement();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -17,6 +17,7 @@
|
||||
package org.springframework.cache.interceptor;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.springframework.cache.CacheTestUtils.*;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Collection;
|
||||
@@ -146,19 +147,6 @@ public class CacheResolverCustomisationTests {
|
||||
}
|
||||
}
|
||||
|
||||
protected void assertCacheMiss(Object key, Cache... caches) {
|
||||
for (Cache cache : caches) {
|
||||
assertNull("No entry in " + cache + " should have been found with key " + key, cache.get(key));
|
||||
}
|
||||
}
|
||||
|
||||
protected void assertCacheHit(Object key, Object value, Cache... caches) {
|
||||
for (Cache cache : caches) {
|
||||
Cache.ValueWrapper wrapper = cache.get(key);
|
||||
assertNotNull("An entry in " + cache + " should have been found with key " + key, wrapper);
|
||||
assertEquals("Wrong value in " + cache + " for entry with key " + key, value, wrapper.get());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Configuration
|
||||
|
||||
Reference in New Issue
Block a user