SpringCacheAnnotationParser properly accepts empty @Caching annotation

Issue: SPR-14162
This commit is contained in:
Juergen Hoeller 2016-04-13 13:25:29 +02:00
parent a8418d3bcd
commit 3d6b0ca5be
2 changed files with 40 additions and 20 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2014 the original author or authors. * Copyright 2002-2016 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -70,7 +70,10 @@ public class SpringCacheAnnotationParser implements CacheAnnotationParser, Seria
if (cachings != null) { if (cachings != null) {
ops = lazyInit(ops); ops = lazyInit(ops);
for (Caching caching : cachings) { for (Caching caching : cachings) {
ops.addAll(parseCachingAnnotation(ae, caching)); Collection<CacheOperation> cachingOps = parseCachingAnnotation(ae, caching);
if (cachingOps != null) {
ops.addAll(cachingOps);
}
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2011 the original author or authors. * Copyright 2002-2016 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -16,8 +16,6 @@
package org.springframework.cache.annotation; package org.springframework.cache.annotation;
import static org.junit.Assert.*;
import java.lang.annotation.ElementType; import java.lang.annotation.ElementType;
import java.lang.annotation.Retention; import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy; import java.lang.annotation.RetentionPolicy;
@ -27,22 +25,20 @@ import java.util.Collection;
import java.util.Iterator; import java.util.Iterator;
import org.junit.Test; import org.junit.Test;
import org.springframework.cache.interceptor.CacheEvictOperation; import org.springframework.cache.interceptor.CacheEvictOperation;
import org.springframework.cache.interceptor.CacheOperation; import org.springframework.cache.interceptor.CacheOperation;
import org.springframework.cache.interceptor.CacheableOperation; import org.springframework.cache.interceptor.CacheableOperation;
import org.springframework.util.ReflectionUtils;
import static org.junit.Assert.*;
/** /**
* @author Costin Leau * @author Costin Leau
*/ */
public class AnnotationCacheOperationSourceTests { public class AnnotationCacheOperationSourceTests {
private AnnotationCacheOperationSource source = new AnnotationCacheOperationSource(); private final AnnotationCacheOperationSource source = new AnnotationCacheOperationSource();
private Collection<CacheOperation> getOps(String name) {
Method method = ReflectionUtils.findMethod(AnnotatedClass.class, name);
return source.getCacheOperations(method, AnnotatedClass.class);
}
@Test @Test
public void testSingularAnnotation() throws Exception { public void testSingularAnnotation() throws Exception {
@ -69,6 +65,12 @@ public class AnnotationCacheOperationSourceTests {
assertTrue(it.next() instanceof CacheEvictOperation); assertTrue(it.next() instanceof CacheEvictOperation);
} }
@Test
public void testEmptyCaching() throws Exception {
Collection<CacheOperation> ops = getOps("emptyCaching");
assertTrue(ops.isEmpty());
}
@Test @Test
public void testSingularStereotype() throws Exception { public void testSingularStereotype() throws Exception {
Collection<CacheOperation> ops = getOps("singleStereotype"); Collection<CacheOperation> ops = getOps("singleStereotype");
@ -90,7 +92,15 @@ public class AnnotationCacheOperationSourceTests {
assertTrue(next.getCacheNames().contains("bar")); assertTrue(next.getCacheNames().contains("bar"));
} }
private Collection<CacheOperation> getOps(String name) throws Exception {
Method method = AnnotatedClass.class.getMethod(name);
return source.getCacheOperations(method, AnnotatedClass.class);
}
private static class AnnotatedClass { private static class AnnotatedClass {
@Cacheable("test") @Cacheable("test")
public void singular() { public void singular() {
} }
@ -100,13 +110,16 @@ public class AnnotationCacheOperationSourceTests {
public void multiple() { public void multiple() {
} }
@Caching(cacheable = { @Cacheable("test") }, evict = { @CacheEvict("test") }) @Caching(cacheable = @Cacheable("test"), evict = @CacheEvict("test"))
public void caching() { public void caching() {
} }
@Caching
public void emptyCaching() {
}
@EvictFoo @EvictFoo
public void singleStereotype() { public void singleStereotype() {
} }
@EvictFoo @EvictFoo
@ -120,21 +133,25 @@ public class AnnotationCacheOperationSourceTests {
} }
} }
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD) @Target(ElementType.METHOD)
@Cacheable("foo") @Cacheable("foo")
public @interface CacheableFoo { public @interface CacheableFoo {
} }
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@CacheEvict(value = "foo")
public @interface EvictFoo {
}
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD) @Target(ElementType.METHOD)
@CacheEvict(value = "bar") @CacheEvict("foo")
public @interface EvictFoo {
}
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@CacheEvict("bar")
public @interface EvictBar { public @interface EvictBar {
} }
} }