From cb3524ff30d821e8728d6f1500d1517041396704 Mon Sep 17 00:00:00 2001 From: Costin Leau Date: Tue, 6 Dec 2011 14:05:33 +0000 Subject: [PATCH] + fix failing cache tests + renamed afterInvocation to beforeInvocation (and changed the docs and tests accordingly) --- build-spring-framework/resources/changelog.txt | 1 + .../cache/config/AnnotatedClassCacheableService.java | 4 ++-- .../cache/config/DefaultCacheableService.java | 4 ++-- .../springframework/cache/annotation/CacheEvict.java | 2 +- .../annotation/SpringCacheAnnotationParser.java | 4 ++-- .../cache/config/CacheAdviceParser.java | 4 ++-- .../cache/interceptor/CacheAspectSupport.java | 12 ++++++------ .../cache/interceptor/CacheEvictOperation.java | 12 ++++++------ .../cache/config/spring-cache-3.1.xsd | 2 +- .../cache/config/AnnotatedClassCacheableService.java | 4 ++-- .../cache/config/DefaultCacheableService.java | 4 ++-- .../springframework/cache/config/cache-advice.xml | 8 ++++---- spring-framework-reference/src/cache.xml | 4 ++-- 13 files changed, 33 insertions(+), 32 deletions(-) diff --git a/build-spring-framework/resources/changelog.txt b/build-spring-framework/resources/changelog.txt index 29f77a493eb..af884f9b657 100644 --- a/build-spring-framework/resources/changelog.txt +++ b/build-spring-framework/resources/changelog.txt @@ -8,6 +8,7 @@ Changes in version 3.1 GA (2011-12-12) * fixed QuartzJobBean to work with Quartz 2.0/2.1 as well * added String constants to MediaType +* renamed attribute @CacheEvict#afterInvocation to beforeInvocation (for better readability) Changes in version 3.1 RC2 (2011-11-28) diff --git a/org.springframework.aspects/src/test/java/org/springframework/cache/config/AnnotatedClassCacheableService.java b/org.springframework.aspects/src/test/java/org/springframework/cache/config/AnnotatedClassCacheableService.java index 2db724f6f19..8a5a4da9e9d 100644 --- a/org.springframework.aspects/src/test/java/org/springframework/cache/config/AnnotatedClassCacheableService.java +++ b/org.springframework.aspects/src/test/java/org/springframework/cache/config/AnnotatedClassCacheableService.java @@ -53,7 +53,7 @@ public class AnnotatedClassCacheableService implements CacheableService public void evictAll(Object arg1) { } - @CacheEvict(value = "default", afterInvocation = false) + @CacheEvict(value = "default", beforeInvocation = false) public void evictEarly(Object arg1) { throw new RuntimeException("exception thrown - evict should still occur"); } @@ -62,7 +62,7 @@ public class AnnotatedClassCacheableService implements CacheableService public void evict(Object arg1, Object arg2) { } - @CacheEvict(value = "default", key = "#p0", afterInvocation = false) + @CacheEvict(value = "default", key = "#p0", beforeInvocation = false) public void invalidateEarly(Object arg1, Object arg2) { throw new RuntimeException("exception thrown - evict should still occur"); } diff --git a/org.springframework.aspects/src/test/java/org/springframework/cache/config/DefaultCacheableService.java b/org.springframework.aspects/src/test/java/org/springframework/cache/config/DefaultCacheableService.java index aeabc9a046f..65ec02f293b 100644 --- a/org.springframework.aspects/src/test/java/org/springframework/cache/config/DefaultCacheableService.java +++ b/org.springframework.aspects/src/test/java/org/springframework/cache/config/DefaultCacheableService.java @@ -51,7 +51,7 @@ public class DefaultCacheableService implements CacheableService { public void evictAll(Object arg1) { } - @CacheEvict(value = "default", afterInvocation = false) + @CacheEvict(value = "default", beforeInvocation = false) public void evictEarly(Object arg1) { throw new RuntimeException("exception thrown - evict should still occur"); } @@ -60,7 +60,7 @@ public class DefaultCacheableService implements CacheableService { public void evict(Object arg1, Object arg2) { } - @CacheEvict(value = "default", key = "#p0", afterInvocation = false) + @CacheEvict(value = "default", key = "#p0", beforeInvocation = false) public void invalidateEarly(Object arg1, Object arg2) { throw new RuntimeException("exception thrown - evict should still occur"); } diff --git a/org.springframework.context/src/main/java/org/springframework/cache/annotation/CacheEvict.java b/org.springframework.context/src/main/java/org/springframework/cache/annotation/CacheEvict.java index 6a38e03ab57..b46367f6702 100644 --- a/org.springframework.context/src/main/java/org/springframework/cache/annotation/CacheEvict.java +++ b/org.springframework.context/src/main/java/org/springframework/cache/annotation/CacheEvict.java @@ -68,5 +68,5 @@ public @interface CacheEvict { * or before. The latter causes the eviction to occur irrespective of the method outcome (whether * it threw an exception or not) while the former does not. */ - boolean afterInvocation() default true; + boolean beforeInvocation() default false; } diff --git a/org.springframework.context/src/main/java/org/springframework/cache/annotation/SpringCacheAnnotationParser.java b/org.springframework.context/src/main/java/org/springframework/cache/annotation/SpringCacheAnnotationParser.java index ab26c400920..07280ead2be 100644 --- a/org.springframework.context/src/main/java/org/springframework/cache/annotation/SpringCacheAnnotationParser.java +++ b/org.springframework.context/src/main/java/org/springframework/cache/annotation/SpringCacheAnnotationParser.java @@ -93,7 +93,7 @@ public class SpringCacheAnnotationParser implements CacheAnnotationParser, Seria ceo.setCondition(caching.condition()); ceo.setKey(caching.key()); ceo.setCacheWide(caching.allEntries()); - ceo.setAfterInvocation(caching.afterInvocation()); + ceo.setBeforeInvocation(caching.beforeInvocation()); ceo.setName(ae.toString()); return ceo; } @@ -152,6 +152,6 @@ public class SpringCacheAnnotationParser implements CacheAnnotationParser, Seria } } - return anns; + return (anns.isEmpty() ? null : anns); } } \ No newline at end of file diff --git a/org.springframework.context/src/main/java/org/springframework/cache/config/CacheAdviceParser.java b/org.springframework.context/src/main/java/org/springframework/cache/config/CacheAdviceParser.java index df40c82db6c..a1206c576f7 100644 --- a/org.springframework.context/src/main/java/org/springframework/cache/config/CacheAdviceParser.java +++ b/org.springframework.context/src/main/java/org/springframework/cache/config/CacheAdviceParser.java @@ -188,9 +188,9 @@ class CacheAdviceParser extends AbstractSingleBeanDefinitionParser { op.setCacheWide(Boolean.valueOf(wide.trim())); } - String after = opElement.getAttribute("after-invocation"); + String after = opElement.getAttribute("before-invocation"); if (StringUtils.hasText(after)) { - op.setAfterInvocation(Boolean.valueOf(after.trim())); + op.setBeforeInvocation(Boolean.valueOf(after.trim())); } Collection col = cacheOpMap.get(nameHolder); diff --git a/org.springframework.context/src/main/java/org/springframework/cache/interceptor/CacheAspectSupport.java b/org.springframework.context/src/main/java/org/springframework/cache/interceptor/CacheAspectSupport.java index ea0cc2b4ed4..1a600193b93 100644 --- a/org.springframework.context/src/main/java/org/springframework/cache/interceptor/CacheAspectSupport.java +++ b/org.springframework.context/src/main/java/org/springframework/cache/interceptor/CacheAspectSupport.java @@ -225,14 +225,14 @@ public abstract class CacheAspectSupport implements InitializingBean { } private void inspectBeforeCacheEvicts(Collection evictions) { - inspectCacheEvicts(evictions, false); - } - - private void inspectAfterCacheEvicts(Collection evictions) { inspectCacheEvicts(evictions, true); } - private void inspectCacheEvicts(Collection evictions, boolean afterInvocation) { + private void inspectAfterCacheEvicts(Collection evictions) { + inspectCacheEvicts(evictions, false); + } + + private void inspectCacheEvicts(Collection evictions, boolean beforeInvocation) { if (!evictions.isEmpty()) { @@ -241,7 +241,7 @@ public abstract class CacheAspectSupport implements InitializingBean { for (CacheOperationContext context : evictions) { CacheEvictOperation evictOp = (CacheEvictOperation) context.operation; - if (afterInvocation == evictOp.isAfterInvocation()) { + if (beforeInvocation == evictOp.isBeforeInvocation()) { if (context.isConditionPassing()) { // for each cache // lazy key initialization diff --git a/org.springframework.context/src/main/java/org/springframework/cache/interceptor/CacheEvictOperation.java b/org.springframework.context/src/main/java/org/springframework/cache/interceptor/CacheEvictOperation.java index 203aa0164d6..62186d89661 100644 --- a/org.springframework.context/src/main/java/org/springframework/cache/interceptor/CacheEvictOperation.java +++ b/org.springframework.context/src/main/java/org/springframework/cache/interceptor/CacheEvictOperation.java @@ -25,7 +25,7 @@ package org.springframework.cache.interceptor; public class CacheEvictOperation extends CacheOperation { private boolean cacheWide = false; - private boolean afterInvocation = true; + private boolean beforeInvocation = false; public void setCacheWide(boolean cacheWide) { this.cacheWide = cacheWide; @@ -35,12 +35,12 @@ public class CacheEvictOperation extends CacheOperation { return this.cacheWide; } - public void setAfterInvocation(boolean afterInvocation) { - this.afterInvocation = afterInvocation; + public void setBeforeInvocation(boolean beforeInvocation) { + this.beforeInvocation = beforeInvocation; } - public boolean isAfterInvocation() { - return this.afterInvocation; + public boolean isBeforeInvocation() { + return this.beforeInvocation; } @Override @@ -49,7 +49,7 @@ public class CacheEvictOperation extends CacheOperation { sb.append(","); sb.append(this.cacheWide); sb.append(","); - sb.append(this.afterInvocation); + sb.append(this.beforeInvocation); return sb; } } diff --git a/org.springframework.context/src/main/resources/org/springframework/cache/config/spring-cache-3.1.xsd b/org.springframework.context/src/main/resources/org/springframework/cache/config/spring-cache-3.1.xsd index ff3c3746ee6..eab936d9ab9 100644 --- a/org.springframework.context/src/main/resources/org/springframework/cache/config/spring-cache-3.1.xsd +++ b/org.springframework.context/src/main/resources/org/springframework/cache/config/spring-cache-3.1.xsd @@ -214,7 +214,7 @@ Whether all the entries should be evicted.]]> - + public void evictAll(Object arg1) { } - @CacheEvict(value = "default", afterInvocation = false) + @CacheEvict(value = "default", beforeInvocation = true) public void evictEarly(Object arg1) { throw new RuntimeException("exception thrown - evict should still occur"); } @@ -62,7 +62,7 @@ public class AnnotatedClassCacheableService implements CacheableService public void evict(Object arg1, Object arg2) { } - @CacheEvict(value = "default", key = "#p0", afterInvocation = false) + @CacheEvict(value = "default", key = "#p0", beforeInvocation = true) public void invalidateEarly(Object arg1, Object arg2) { throw new RuntimeException("exception thrown - evict should still occur"); } diff --git a/org.springframework.context/src/test/java/org/springframework/cache/config/DefaultCacheableService.java b/org.springframework.context/src/test/java/org/springframework/cache/config/DefaultCacheableService.java index 034df1143a6..0f7493c554b 100644 --- a/org.springframework.context/src/test/java/org/springframework/cache/config/DefaultCacheableService.java +++ b/org.springframework.context/src/test/java/org/springframework/cache/config/DefaultCacheableService.java @@ -51,7 +51,7 @@ public class DefaultCacheableService implements CacheableService { public void evictAll(Object arg1) { } - @CacheEvict(value = "default", afterInvocation = false) + @CacheEvict(value = "default", beforeInvocation = true) public void evictEarly(Object arg1) { throw new RuntimeException("exception thrown - evict should still occur"); } @@ -60,7 +60,7 @@ public class DefaultCacheableService implements CacheableService { public void evict(Object arg1, Object arg2) { } - @CacheEvict(value = "default", key = "#p0", afterInvocation = false) + @CacheEvict(value = "default", key = "#p0", beforeInvocation = true) public void invalidateEarly(Object arg1, Object arg2) { throw new RuntimeException("exception thrown - evict should still occur"); } diff --git a/org.springframework.context/src/test/resources/org/springframework/cache/config/cache-advice.xml b/org.springframework.context/src/test/resources/org/springframework/cache/config/cache-advice.xml index ec36d11ffa6..36201c291d0 100644 --- a/org.springframework.context/src/test/resources/org/springframework/cache/config/cache-advice.xml +++ b/org.springframework.context/src/test/resources/org/springframework/cache/config/cache-advice.xml @@ -20,8 +20,8 @@ - - + + @@ -60,8 +60,8 @@ - - + + diff --git a/spring-framework-reference/src/cache.xml b/spring-framework-reference/src/cache.xml index 4bfab4d63bd..f3d8b337c82 100644 --- a/spring-framework-reference/src/cache.xml +++ b/spring-framework-reference/src/cache.xml @@ -248,9 +248,9 @@ public void loadBooks(InputStream batch)]]> all the entires are removed in one operation as shown above. Note that the framework will ignore any key specified in this scenario as it does not apply (the entire cache is evicted not just one entry). - One can also indicate whether the eviction should occur after (the default) or before the method executes (the default) through the afterInvocation attribute. + One can also indicate whether the eviction should occur after (the default) or before the method executes through the beforeInvocation attribute. The former provides the same semantics as the rest of the annotations - once the method completes successfully, an action (in this case eviction) on the cache is executed. If the method does not - execute (as it might be cached) or an exception is thrown, the eviction does not occur. The latter (afterInvocation=false) causes the eviction to occur always, before the method + execute (as it might be cached) or an exception is thrown, the eviction does not occur. The latter (beforeInvocation=true) causes the eviction to occur always, before the method is invoked - this is useful in cases where the eviction does not need to be tied to the method outcome. It is important to note that void methods can be used with @CacheEvict - as the methods act as triggers, the return values are ignored (as they don't interact with