From 0053319c6279a55094a723dc5fc0955b901cfd1c Mon Sep 17 00:00:00 2001 From: Costin Leau Date: Thu, 22 Dec 2011 15:24:23 +0200 Subject: [PATCH] Add test to corner potential bug with @CacheEvict Cannot yet actually reproduce the issue, but this remains a useful test case. Issue: SPR-8934 --- .../AnnotationCacheOperationSourceTest.java | 4 + .../interceptor/ExpressionEvalutatorTest.java | 86 +++++++++++++++++++ 2 files changed, 90 insertions(+) create mode 100644 org.springframework.context/src/test/java/org/springframework/cache/interceptor/ExpressionEvalutatorTest.java diff --git a/org.springframework.context/src/test/java/org/springframework/cache/annotation/AnnotationCacheOperationSourceTest.java b/org.springframework.context/src/test/java/org/springframework/cache/annotation/AnnotationCacheOperationSourceTest.java index 6f9cac9cf7..b7564dbd93 100644 --- a/org.springframework.context/src/test/java/org/springframework/cache/annotation/AnnotationCacheOperationSourceTest.java +++ b/org.springframework.context/src/test/java/org/springframework/cache/annotation/AnnotationCacheOperationSourceTest.java @@ -114,6 +114,10 @@ public class AnnotationCacheOperationSourceTest { @EvictBar public void multipleStereotype() { } + + @Caching(cacheable = { @Cacheable(value = "test", key = "a"), @Cacheable(value = "test", key = "b") }) + public void multipleCaching() { + } } @Retention(RetentionPolicy.RUNTIME) diff --git a/org.springframework.context/src/test/java/org/springframework/cache/interceptor/ExpressionEvalutatorTest.java b/org.springframework.context/src/test/java/org/springframework/cache/interceptor/ExpressionEvalutatorTest.java new file mode 100644 index 0000000000..b52f364746 --- /dev/null +++ b/org.springframework.context/src/test/java/org/springframework/cache/interceptor/ExpressionEvalutatorTest.java @@ -0,0 +1,86 @@ +/* + * Copyright 2011 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cache.interceptor; + +import static org.junit.Assert.*; + +import java.lang.reflect.Method; +import java.util.Collection; +import java.util.Iterator; + +import org.junit.Test; +import org.springframework.cache.Cache; +import org.springframework.cache.annotation.AnnotationCacheOperationSource; +import org.springframework.cache.annotation.Cacheable; +import org.springframework.cache.annotation.Caching; +import org.springframework.cache.concurrent.ConcurrentMapCache; +import org.springframework.expression.EvaluationContext; +import org.springframework.util.ReflectionUtils; + +import edu.emory.mathcs.backport.java.util.Collections; + +public class ExpressionEvalutatorTest { + private ExpressionEvaluator eval = new ExpressionEvaluator(); + + private AnnotationCacheOperationSource source = new AnnotationCacheOperationSource(); + + private Collection getOps(String name) { + Method method = ReflectionUtils.findMethod(AnnotatedClass.class, name, Object.class, Object.class); + return source.getCacheOperations(method, AnnotatedClass.class); + } + + @Test + public void testMultipleCachingSource() throws Exception { + Collection ops = getOps("multipleCaching"); + assertEquals(2, ops.size()); + Iterator it = ops.iterator(); + CacheOperation next = it.next(); + assertTrue(next instanceof CacheableOperation); + assertTrue(next.getCacheNames().contains("test")); + assertEquals("#a", next.getKey()); + next = it.next(); + assertTrue(next instanceof CacheableOperation); + assertTrue(next.getCacheNames().contains("test")); + assertEquals("#b", next.getKey()); + } + + @Test + public void testMultipleCachingEval() throws Exception { + AnnotatedClass target = new AnnotatedClass(); + Method method = ReflectionUtils.findMethod(AnnotatedClass.class, "multipleCaching", Object.class, + Object.class); + Object[] args = new Object[] { new Object(), new Object() }; + Collection map = Collections.singleton(new ConcurrentMapCache("test")); + + EvaluationContext evalCtx = eval.createEvaluationContext(map, method, args, target, target.getClass()); + Collection ops = getOps("multipleCaching"); + + Iterator it = ops.iterator(); + + Object keyA = eval.key(it.next().getKey(), method, evalCtx); + Object keyB = eval.key(it.next().getKey(), method, evalCtx); + + assertEquals(args[0], keyA); + assertEquals(args[1], keyB); + } + + private static class AnnotatedClass { + @Caching(cacheable = { @Cacheable(value = "test", key = "#a"), @Cacheable(value = "test", key = "#b") }) + public void multipleCaching(Object a, Object b) { + } + } +} \ No newline at end of file