+ fix initialization problem for root object (inside SpEL eval context)
+ add integration tests for root object expressions


git-svn-id: https://src.springframework.org/svn/spring-framework/trunk@3828 50f2f4bb-b051-0410-bef5-90022cba6387
This commit is contained in:
Costin Leau 2010-12-21 15:47:22 +00:00
parent a010a7f63a
commit 09ed0d0835
5 changed files with 39 additions and 0 deletions

View File

@ -46,6 +46,8 @@ class LazyParamAwareEvaluationContext extends StandardEvaluationContext {
LazyParamAwareEvaluationContext(Object rootObject, ParameterNameDiscoverer paramDiscoverer, Method method,
Object[] args, Class<?> targetClass, Map<Method, Method> methodCache) {
super(rootObject);
this.paramDiscoverer = paramDiscoverer;
this.method = method;
this.args = args;

View File

@ -20,6 +20,8 @@ import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
@ -36,6 +38,8 @@ public abstract class AbstractAnnotationTest {
protected CacheableService ccs;
protected CacheManager cm;
protected abstract String getConfig();
@Before
@ -43,6 +47,7 @@ public abstract class AbstractAnnotationTest {
ctx = new ClassPathXmlApplicationContext(getConfig());
cs = ctx.getBean("service", CacheableService.class);
ccs = ctx.getBean("classService", CacheableService.class);
cm = ctx.getBean(CacheManager.class);
}
public void testCacheable(CacheableService service) throws Exception {
@ -107,6 +112,16 @@ public abstract class AbstractAnnotationTest {
assertEquals(nr + 1, service.nullInvocations().intValue());
}
public void testMethodName(CacheableService service, String keyName)
throws Exception {
Object key = new Object();
Object r1 = service.name(key);
assertSame(r1, service.name(key));
Cache<Object, Object> cache = cm.getCache("default");
// assert the method name is used
assertTrue(cache.containsKey(keyName));
}
@Test
public void testCacheable() throws Exception {
testCacheable(cs);
@ -155,4 +170,14 @@ public abstract class AbstractAnnotationTest {
assertEquals(nr + 1, AnnotatedClassCacheableService.nullInvocations
.intValue());
}
@Test
public void testMethodName() throws Exception {
testMethodName(cs, "name");
}
@Test
public void testClassMethodName() throws Exception {
testMethodName(ccs, "namedefault");
}
}

View File

@ -47,6 +47,11 @@ public class AnnotatedClassCacheableService implements CacheableService {
return counter.getAndIncrement();
}
@Cacheable(value = "default", key = "#root.methodName + #root.caches[0].name")
public Object name(Object arg1) {
return counter.getAndIncrement();
}
public Object nullValue(Object arg1) {
nullInvocations.incrementAndGet();
return null;

View File

@ -32,6 +32,8 @@ public interface CacheableService<T> {
T key(Object arg1, Object arg2);
T name(Object arg1);
T nullValue(Object arg1);
Number nullInvocations();

View File

@ -50,6 +50,11 @@ public class DefaultCacheableService implements CacheableService<Long> {
return counter.getAndIncrement();
}
@Cacheable(value = "default", key = "#root.methodName")
public Long name(Object arg1) {
return counter.getAndIncrement();
}
@Cacheable("default")
public Long nullValue(Object arg1) {
nullInvocations.incrementAndGet();