21
21
import java .lang .annotation .RetentionPolicy ;
22
22
import java .lang .annotation .Target ;
23
23
import java .lang .reflect .Method ;
24
+ import java .util .Arrays ;
24
25
import java .util .Collection ;
25
26
import java .util .Iterator ;
26
27
31
32
import org .springframework .cache .interceptor .CacheEvictOperation ;
32
33
import org .springframework .cache .interceptor .CacheOperation ;
33
34
import org .springframework .cache .interceptor .CacheableOperation ;
35
+ import org .springframework .core .annotation .AliasFor ;
34
36
import org .springframework .util .ReflectionUtils ;
35
37
36
38
import static org .junit .Assert .*;
42
44
public class AnnotationCacheOperationSourceTests {
43
45
44
46
@ Rule
45
- public final ExpectedException thrown = ExpectedException .none ();
47
+ public final ExpectedException exception = ExpectedException .none ();
46
48
47
49
private AnnotationCacheOperationSource source = new AnnotationCacheOperationSource ();
48
50
49
- private Collection < CacheOperation > getOps ( Class <?> target , String name ,
50
- int expectedNumberOfOperations ) {
51
+
52
+ private Collection < CacheOperation > getOps ( Class <?> target , String name , int expectedNumberOfOperations ) {
51
53
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 ());
54
55
return result ;
55
56
}
56
57
@@ -60,35 +61,35 @@ private Collection<CacheOperation> getOps(Class<?> target, String name) {
60
61
}
61
62
62
63
@ Test
63
- public void testSingularAnnotation () throws Exception {
64
+ public void singularAnnotation () throws Exception {
64
65
Collection <CacheOperation > ops = getOps (AnnotatedClass .class , "singular" , 1 );
65
66
assertTrue (ops .iterator ().next () instanceof CacheableOperation );
66
67
}
67
68
68
69
@ Test
69
- public void testMultipleAnnotation () throws Exception {
70
+ public void multipleAnnotation () throws Exception {
70
71
Collection <CacheOperation > ops = getOps (AnnotatedClass .class , "multiple" , 2 );
71
72
Iterator <CacheOperation > it = ops .iterator ();
72
73
assertTrue (it .next () instanceof CacheableOperation );
73
74
assertTrue (it .next () instanceof CacheEvictOperation );
74
75
}
75
76
76
77
@ Test
77
- public void testCaching () throws Exception {
78
+ public void caching () throws Exception {
78
79
Collection <CacheOperation > ops = getOps (AnnotatedClass .class , "caching" , 2 );
79
80
Iterator <CacheOperation > it = ops .iterator ();
80
81
assertTrue (it .next () instanceof CacheableOperation );
81
82
assertTrue (it .next () instanceof CacheEvictOperation );
82
83
}
83
84
84
85
@ Test
85
- public void testSingularStereotype () throws Exception {
86
+ public void singularStereotype () throws Exception {
86
87
Collection <CacheOperation > ops = getOps (AnnotatedClass .class , "singleStereotype" , 1 );
87
88
assertTrue (ops .iterator ().next () instanceof CacheEvictOperation );
88
89
}
89
90
90
91
@ Test
91
- public void testMultipleStereotypes () throws Exception {
92
+ public void multipleStereotypes () throws Exception {
92
93
Collection <CacheOperation > ops = getOps (AnnotatedClass .class , "multipleStereotype" , 3 );
93
94
Iterator <CacheOperation > it = ops .iterator ();
94
95
assertTrue (it .next () instanceof CacheableOperation );
@@ -101,65 +102,57 @@ public void testMultipleStereotypes() throws Exception {
101
102
}
102
103
103
104
@ Test
104
- public void testCustomKeyGenerator () {
105
+ public void customKeyGenerator () {
105
106
Collection <CacheOperation > ops = getOps (AnnotatedClass .class , "customKeyGenerator" , 1 );
106
107
CacheOperation cacheOperation = ops .iterator ().next ();
107
108
assertEquals ("Custom key generator not set" , "custom" , cacheOperation .getKeyGenerator ());
108
109
}
109
110
110
111
@ Test
111
- public void testCustomKeyGeneratorInherited () {
112
+ public void customKeyGeneratorInherited () {
112
113
Collection <CacheOperation > ops = getOps (AnnotatedClass .class , "customKeyGeneratorInherited" , 1 );
113
114
CacheOperation cacheOperation = ops .iterator ().next ();
114
115
assertEquals ("Custom key generator not set" , "custom" , cacheOperation .getKeyGenerator ());
115
116
}
116
117
117
118
@ 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" );
125
122
}
126
123
127
124
@ Test
128
- public void testCustomCacheManager () {
125
+ public void customCacheManager () {
129
126
Collection <CacheOperation > ops = getOps (AnnotatedClass .class , "customCacheManager" , 1 );
130
127
CacheOperation cacheOperation = ops .iterator ().next ();
131
128
assertEquals ("Custom cache manager not set" , "custom" , cacheOperation .getCacheManager ());
132
129
}
133
130
134
131
@ Test
135
- public void testCustomCacheManagerInherited () {
132
+ public void customCacheManagerInherited () {
136
133
Collection <CacheOperation > ops = getOps (AnnotatedClass .class , "customCacheManagerInherited" , 1 );
137
134
CacheOperation cacheOperation = ops .iterator ().next ();
138
135
assertEquals ("Custom cache manager not set" , "custom" , cacheOperation .getCacheManager ());
139
136
}
140
137
141
138
@ Test
142
- public void testCustomCacheResolver () {
139
+ public void customCacheResolver () {
143
140
Collection <CacheOperation > ops = getOps (AnnotatedClass .class , "customCacheResolver" , 1 );
144
141
CacheOperation cacheOperation = ops .iterator ().next ();
145
142
assertEquals ("Custom cache resolver not set" , "custom" , cacheOperation .getCacheResolver ());
146
143
}
147
144
148
145
@ Test
149
- public void testCustomCacheResolverInherited () {
146
+ public void customCacheResolverInherited () {
150
147
Collection <CacheOperation > ops = getOps (AnnotatedClass .class , "customCacheResolverInherited" , 1 );
151
148
CacheOperation cacheOperation = ops .iterator ().next ();
152
149
assertEquals ("Custom cache resolver not set" , "custom" , cacheOperation .getCacheResolver ());
153
150
}
154
151
155
152
@ 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" );
163
156
}
164
157
165
158
@ Test
@@ -239,14 +232,14 @@ private void assertSharedConfig(CacheOperation actual, String keyGenerator, Stri
239
232
assertEquals ("Wrong key manager" , keyGenerator , actual .getKeyGenerator ());
240
233
assertEquals ("Wrong cache manager" , cacheManager , actual .getCacheManager ());
241
234
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 )));
247
239
}
248
240
249
241
private static class AnnotatedClass {
242
+
250
243
@ Cacheable ("test" )
251
244
public void singular () {
252
245
}
@@ -256,7 +249,7 @@ public void singular() {
256
249
public void multiple () {
257
250
}
258
251
259
- @ Caching (cacheable = { @ Cacheable ("test" )} , evict = { @ CacheEvict ("test" )} )
252
+ @ Caching (cacheable = @ Cacheable ("test" ), evict = @ CacheEvict ("test" ))
260
253
public void caching () {
261
254
}
262
255
@@ -357,7 +350,6 @@ public void methodLevelCacheResolver() {
357
350
@ Cacheable
358
351
public void noCustomization () {
359
352
}
360
-
361
353
}
362
354
363
355
@ CacheConfigFoo
@@ -411,4 +403,20 @@ public void multipleCacheConfig() {
411
403
cacheManager = "classCacheManager" , cacheResolver = "classCacheResolver" )
412
404
public @interface CacheConfigFoo {
413
405
}
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
+
414
422
}
0 commit comments