Skip to content

Commit d66f85b

Browse files
committed
Polishing
1 parent 0ad59dd commit d66f85b

File tree

2 files changed

+48
-40
lines changed

2 files changed

+48
-40
lines changed

spring-context/src/main/java/org/springframework/cache/annotation/SpringCacheAnnotationParser.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -194,11 +194,11 @@ DefaultCacheConfig getDefaultCacheConfig(Class<?> target) {
194194
return new DefaultCacheConfig();
195195
}
196196

197-
private <T extends Annotation> Collection<T> getAnnotations(AnnotatedElement ae, Class<T> annotationType) {
198-
Collection<T> anns = new ArrayList<T>(2);
197+
private <A extends Annotation> Collection<A> getAnnotations(AnnotatedElement ae, Class<A> annotationType) {
198+
Collection<A> anns = new ArrayList<A>(2);
199199

200200
// look at raw annotation
201-
T ann = ae.getAnnotation(annotationType);
201+
A ann = ae.getAnnotation(annotationType);
202202
if (ann != null) {
203203
anns.add(AnnotationUtils.synthesizeAnnotation(ann, ae));
204204
}

spring-context/src/test/java/org/springframework/cache/annotation/AnnotationCacheOperationSourceTests.java

Lines changed: 45 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.lang.annotation.RetentionPolicy;
2222
import java.lang.annotation.Target;
2323
import java.lang.reflect.Method;
24+
import java.util.Arrays;
2425
import java.util.Collection;
2526
import java.util.Iterator;
2627

@@ -31,6 +32,7 @@
3132
import org.springframework.cache.interceptor.CacheEvictOperation;
3233
import org.springframework.cache.interceptor.CacheOperation;
3334
import org.springframework.cache.interceptor.CacheableOperation;
35+
import org.springframework.core.annotation.AliasFor;
3436
import org.springframework.util.ReflectionUtils;
3537

3638
import static org.junit.Assert.*;
@@ -42,15 +44,14 @@
4244
public class AnnotationCacheOperationSourceTests {
4345

4446
@Rule
45-
public final ExpectedException thrown = ExpectedException.none();
47+
public final ExpectedException exception = ExpectedException.none();
4648

4749
private AnnotationCacheOperationSource source = new AnnotationCacheOperationSource();
4850

49-
private Collection<CacheOperation> getOps(Class<?> target, String name,
50-
int expectedNumberOfOperations) {
51+
52+
private Collection<CacheOperation> getOps(Class<?> target, String name, int expectedNumberOfOperations) {
5153
Collection<CacheOperation> result = getOps(target, name);
52-
assertEquals("Wrong number of operation(s) for '"+name+"'",
53-
expectedNumberOfOperations, result.size());
54+
assertEquals("Wrong number of operation(s) for '" + name + "'", expectedNumberOfOperations, result.size());
5455
return result;
5556
}
5657

@@ -60,35 +61,35 @@ private Collection<CacheOperation> getOps(Class<?> target, String name) {
6061
}
6162

6263
@Test
63-
public void testSingularAnnotation() throws Exception {
64+
public void singularAnnotation() throws Exception {
6465
Collection<CacheOperation> ops = getOps(AnnotatedClass.class, "singular", 1);
6566
assertTrue(ops.iterator().next() instanceof CacheableOperation);
6667
}
6768

6869
@Test
69-
public void testMultipleAnnotation() throws Exception {
70+
public void multipleAnnotation() throws Exception {
7071
Collection<CacheOperation> ops = getOps(AnnotatedClass.class, "multiple", 2);
7172
Iterator<CacheOperation> it = ops.iterator();
7273
assertTrue(it.next() instanceof CacheableOperation);
7374
assertTrue(it.next() instanceof CacheEvictOperation);
7475
}
7576

7677
@Test
77-
public void testCaching() throws Exception {
78+
public void caching() throws Exception {
7879
Collection<CacheOperation> ops = getOps(AnnotatedClass.class, "caching", 2);
7980
Iterator<CacheOperation> it = ops.iterator();
8081
assertTrue(it.next() instanceof CacheableOperation);
8182
assertTrue(it.next() instanceof CacheEvictOperation);
8283
}
8384

8485
@Test
85-
public void testSingularStereotype() throws Exception {
86+
public void singularStereotype() throws Exception {
8687
Collection<CacheOperation> ops = getOps(AnnotatedClass.class, "singleStereotype", 1);
8788
assertTrue(ops.iterator().next() instanceof CacheEvictOperation);
8889
}
8990

9091
@Test
91-
public void testMultipleStereotypes() throws Exception {
92+
public void multipleStereotypes() throws Exception {
9293
Collection<CacheOperation> ops = getOps(AnnotatedClass.class, "multipleStereotype", 3);
9394
Iterator<CacheOperation> it = ops.iterator();
9495
assertTrue(it.next() instanceof CacheableOperation);
@@ -101,65 +102,57 @@ public void testMultipleStereotypes() throws Exception {
101102
}
102103

103104
@Test
104-
public void testCustomKeyGenerator() {
105+
public void customKeyGenerator() {
105106
Collection<CacheOperation> ops = getOps(AnnotatedClass.class, "customKeyGenerator", 1);
106107
CacheOperation cacheOperation = ops.iterator().next();
107108
assertEquals("Custom key generator not set", "custom", cacheOperation.getKeyGenerator());
108109
}
109110

110111
@Test
111-
public void testCustomKeyGeneratorInherited() {
112+
public void customKeyGeneratorInherited() {
112113
Collection<CacheOperation> ops = getOps(AnnotatedClass.class, "customKeyGeneratorInherited", 1);
113114
CacheOperation cacheOperation = ops.iterator().next();
114115
assertEquals("Custom key generator not set", "custom", cacheOperation.getKeyGenerator());
115116
}
116117

117118
@Test
118-
public void testKeyAndKeyGeneratorCannotBeSetTogether() {
119-
try {
120-
getOps(AnnotatedClass.class, "invalidKeyAndKeyGeneratorSet");
121-
fail("Should have failed to parse @Cacheable annotation");
122-
} catch (IllegalStateException e) {
123-
// expected
124-
}
119+
public void keyAndKeyGeneratorCannotBeSetTogether() {
120+
exception.expect(IllegalStateException.class);
121+
getOps(AnnotatedClass.class, "invalidKeyAndKeyGeneratorSet");
125122
}
126123

127124
@Test
128-
public void testCustomCacheManager() {
125+
public void customCacheManager() {
129126
Collection<CacheOperation> ops = getOps(AnnotatedClass.class, "customCacheManager", 1);
130127
CacheOperation cacheOperation = ops.iterator().next();
131128
assertEquals("Custom cache manager not set", "custom", cacheOperation.getCacheManager());
132129
}
133130

134131
@Test
135-
public void testCustomCacheManagerInherited() {
132+
public void customCacheManagerInherited() {
136133
Collection<CacheOperation> ops = getOps(AnnotatedClass.class, "customCacheManagerInherited", 1);
137134
CacheOperation cacheOperation = ops.iterator().next();
138135
assertEquals("Custom cache manager not set", "custom", cacheOperation.getCacheManager());
139136
}
140137

141138
@Test
142-
public void testCustomCacheResolver() {
139+
public void customCacheResolver() {
143140
Collection<CacheOperation> ops = getOps(AnnotatedClass.class, "customCacheResolver", 1);
144141
CacheOperation cacheOperation = ops.iterator().next();
145142
assertEquals("Custom cache resolver not set", "custom", cacheOperation.getCacheResolver());
146143
}
147144

148145
@Test
149-
public void testCustomCacheResolverInherited() {
146+
public void customCacheResolverInherited() {
150147
Collection<CacheOperation> ops = getOps(AnnotatedClass.class, "customCacheResolverInherited", 1);
151148
CacheOperation cacheOperation = ops.iterator().next();
152149
assertEquals("Custom cache resolver not set", "custom", cacheOperation.getCacheResolver());
153150
}
154151

155152
@Test
156-
public void testCacheResolverAndCacheManagerCannotBeSetTogether() {
157-
try {
158-
getOps(AnnotatedClass.class, "invalidCacheResolverAndCacheManagerSet");
159-
fail("Should have failed to parse @Cacheable annotation");
160-
} catch (IllegalStateException e) {
161-
// expected
162-
}
153+
public void cacheResolverAndCacheManagerCannotBeSetTogether() {
154+
exception.expect(IllegalStateException.class);
155+
getOps(AnnotatedClass.class, "invalidCacheResolverAndCacheManagerSet");
163156
}
164157

165158
@Test
@@ -239,14 +232,14 @@ private void assertSharedConfig(CacheOperation actual, String keyGenerator, Stri
239232
assertEquals("Wrong key manager", keyGenerator, actual.getKeyGenerator());
240233
assertEquals("Wrong cache manager", cacheManager, actual.getCacheManager());
241234
assertEquals("Wrong cache resolver", cacheResolver, actual.getCacheResolver());
242-
for (String cacheName : cacheNames) {
243-
assertTrue("Cache '"+cacheName+"' not found (got "+actual.getCacheNames(),
244-
actual.getCacheNames().contains(cacheName));
245-
}
246-
assertEquals("Wrong number of cache name(s)", cacheNames.length, actual.getCacheNames().size());
235+
assertEquals("Wrong number of cache names", cacheNames.length, actual.getCacheNames().size());
236+
Arrays.stream(cacheNames).forEach(
237+
cacheName -> assertTrue("Cache '" + cacheName + "' not found in " + actual.getCacheNames(),
238+
actual.getCacheNames().contains(cacheName)));
247239
}
248240

249241
private static class AnnotatedClass {
242+
250243
@Cacheable("test")
251244
public void singular() {
252245
}
@@ -256,7 +249,7 @@ public void singular() {
256249
public void multiple() {
257250
}
258251

259-
@Caching(cacheable = {@Cacheable("test")}, evict = {@CacheEvict("test")})
252+
@Caching(cacheable = @Cacheable("test"), evict = @CacheEvict("test"))
260253
public void caching() {
261254
}
262255

@@ -357,7 +350,6 @@ public void methodLevelCacheResolver() {
357350
@Cacheable
358351
public void noCustomization() {
359352
}
360-
361353
}
362354

363355
@CacheConfigFoo
@@ -411,4 +403,20 @@ public void multipleCacheConfig() {
411403
cacheManager = "classCacheManager", cacheResolver = "classCacheResolver")
412404
public @interface CacheConfigFoo {
413405
}
406+
407+
@Retention(RetentionPolicy.RUNTIME)
408+
@Target({ ElementType.METHOD, ElementType.TYPE })
409+
@Cacheable
410+
public @interface ComposedCacheable {
411+
412+
@AliasFor(annotation = Cacheable.class, attribute = "cacheNames")
413+
String[] value() default {};
414+
415+
@AliasFor(annotation = Cacheable.class, attribute = "cacheNames")
416+
String[] cacheNames() default {};
417+
418+
@AliasFor(annotation = Cacheable.class, attribute = "key")
419+
String key() default "";
420+
}
421+
414422
}

0 commit comments

Comments
 (0)