Browse Source

polishing

This fixes a Java6 backward compatible issue introduced in the JCache
implementation.

This commit also adds new representative tests.

Issue: SPR-9616
pull/512/head
Stephane Nicoll 12 years ago
parent
commit
7b5e9e8c8e
  1. 2
      spring-context-support/src/main/java/org/springframework/cache/jcache/model/BaseCacheOperation.java
  2. 23
      spring-context/src/test/java/org/springframework/cache/CacheTestUtils.java
  3. 99
      spring-context/src/test/java/org/springframework/cache/config/EnableCachingIntegrationTests.java
  4. 14
      spring-context/src/test/java/org/springframework/cache/interceptor/CacheResolverCustomisationTests.java

2
spring-context-support/src/main/java/org/springframework/cache/jcache/model/BaseCacheOperation.java vendored

@ -119,7 +119,7 @@ public abstract class BaseCacheOperation<A extends Annotation> implements JCache @@ -119,7 +119,7 @@ public abstract class BaseCacheOperation<A extends Annotation> implements JCache
private static List<CacheParameterDetail> initializeAllParameterDetails(Method method) {
List<CacheParameterDetail> result = new ArrayList<CacheParameterDetail>();
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);
}

23
spring-context/src/test/java/org/springframework/cache/CacheTestUtils.java vendored

@ -16,6 +16,8 @@ @@ -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 { @@ -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

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

14
spring-context/src/test/java/org/springframework/cache/interceptor/CacheResolverCustomisationTests.java vendored

@ -17,6 +17,7 @@ @@ -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 { @@ -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

Loading…
Cancel
Save