diff --git a/spring-context-support/src/main/java/org/springframework/cache/jcache/model/BaseCacheOperation.java b/spring-context-support/src/main/java/org/springframework/cache/jcache/model/BaseCacheOperation.java index 993454a760c..f2b66aa10a4 100644 --- a/spring-context-support/src/main/java/org/springframework/cache/jcache/model/BaseCacheOperation.java +++ b/spring-context-support/src/main/java/org/springframework/cache/jcache/model/BaseCacheOperation.java @@ -119,7 +119,7 @@ public abstract class BaseCacheOperation implements JCache private static List initializeAllParameterDetails(Method method) { List result = new ArrayList(); - for (int i = 0; i < method.getParameterCount(); i++) { + for (int i = 0; i < method.getParameterTypes().length; i++) { CacheParameterDetail detail = new CacheParameterDetail(method, i); result.add(detail); } diff --git a/spring-context/src/test/java/org/springframework/cache/CacheTestUtils.java b/spring-context/src/test/java/org/springframework/cache/CacheTestUtils.java index d50462d5ec9..6749920b942 100644 --- a/spring-context/src/test/java/org/springframework/cache/CacheTestUtils.java +++ b/spring-context/src/test/java/org/springframework/cache/CacheTestUtils.java @@ -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()); + } + } + } diff --git a/spring-context/src/test/java/org/springframework/cache/config/EnableCachingIntegrationTests.java b/spring-context/src/test/java/org/springframework/cache/config/EnableCachingIntegrationTests.java new file mode 100644 index 00000000000..9a03de67852 --- /dev/null +++ b/spring-context/src/test/java/org/springframework/cache/config/EnableCachingIntegrationTests.java @@ -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(); + } + } + +} diff --git a/spring-context/src/test/java/org/springframework/cache/interceptor/CacheResolverCustomisationTests.java b/spring-context/src/test/java/org/springframework/cache/interceptor/CacheResolverCustomisationTests.java index a74bce760ba..148a79df9e3 100644 --- a/spring-context/src/test/java/org/springframework/cache/interceptor/CacheResolverCustomisationTests.java +++ b/spring-context/src/test/java/org/springframework/cache/interceptor/CacheResolverCustomisationTests.java @@ -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