23
23
import java .lang .reflect .Method ;
24
24
import java .util .Arrays ;
25
25
import java .util .Collection ;
26
+ import java .util .Collections ;
26
27
import java .util .Iterator ;
27
28
29
+ import org .junit .Ignore ;
28
30
import org .junit .Rule ;
29
31
import org .junit .Test ;
30
32
import org .junit .rules .ExpectedException ;
35
37
import org .springframework .core .annotation .AliasFor ;
36
38
import org .springframework .util .ReflectionUtils ;
37
39
40
+ import static org .hamcrest .CoreMatchers .*;
38
41
import static org .junit .Assert .*;
39
42
40
43
/**
41
44
* @author Costin Leau
42
45
* @author Stephane Nicoll
46
+ * @author Sam Brannen
43
47
*/
44
48
public class AnnotationCacheOperationSourceTests {
45
49
@@ -101,6 +105,36 @@ public void multipleStereotypes() throws Exception {
101
105
assertTrue (next .getCacheNames ().contains ("bar" ));
102
106
}
103
107
108
+ // TODO [SPR-13475] Enable test once @Cache* is supported as a composed annotation.
109
+ @ Ignore ("Disabled until SPR-13475 is resolved" )
110
+ @ Test
111
+ public void singleComposedAnnotation () throws Exception {
112
+ Collection <CacheOperation > ops = getOps (AnnotatedClass .class , "singleComposed" , 1 );
113
+ CacheOperation cacheOperation = ops .iterator ().next ();
114
+ assertThat (cacheOperation , instanceOf (CacheableOperation .class ));
115
+ assertThat (cacheOperation .getCacheNames (), equalTo (Collections .singleton ("composed" )));
116
+ }
117
+
118
+ // TODO [SPR-13475] Enable test once @Cache* is supported as a composed annotation.
119
+ @ Ignore ("Disabled until SPR-13475 is resolved" )
120
+ @ Test
121
+ public void multipleComposedAnnotations () throws Exception {
122
+ Collection <CacheOperation > ops = getOps (AnnotatedClass .class , "multipleComposed" , 3 );
123
+ Iterator <CacheOperation > it = ops .iterator ();
124
+
125
+ CacheOperation cacheOperation = it .next ();
126
+ assertThat (cacheOperation , instanceOf (CacheableOperation .class ));
127
+ assertThat (cacheOperation .getCacheNames (), equalTo (Collections .singleton ("composedCache" )));
128
+
129
+ cacheOperation = it .next ();
130
+ assertThat (cacheOperation , instanceOf (CacheableOperation .class ));
131
+ assertThat (cacheOperation .getCacheNames (), equalTo (Collections .singleton ("foo" )));
132
+
133
+ cacheOperation = it .next ();
134
+ assertThat (cacheOperation , instanceOf (CacheEvictOperation .class ));
135
+ assertThat (cacheOperation .getCacheNames (), equalTo (Collections .singleton ("composedCache" )));
136
+ }
137
+
104
138
@ Test
105
139
public void customKeyGenerator () {
106
140
Collection <CacheOperation > ops = getOps (AnnotatedClass .class , "customKeyGenerator" , 1 );
@@ -275,6 +309,16 @@ public void singleStereotype() {
275
309
public void multipleStereotype () {
276
310
}
277
311
312
+ @ ComposedCacheable ("composed" )
313
+ public void singleComposed () {
314
+ }
315
+
316
+ @ ComposedCacheable (cacheNames = "composedCache" , key = "composedKey" )
317
+ @ CacheableFoo
318
+ @ ComposedCacheEvict (cacheNames = "composedCache" , key = "composedKey" )
319
+ public void multipleComposed () {
320
+ }
321
+
278
322
@ Caching (cacheable = { @ Cacheable (cacheNames = "test" , key = "a" ), @ Cacheable (cacheNames = "test" , key = "b" ) })
279
323
public void multipleCaching () {
280
324
}
@@ -406,7 +450,7 @@ public void multipleCacheConfig() {
406
450
407
451
@ Retention (RetentionPolicy .RUNTIME )
408
452
@ Target ({ ElementType .METHOD , ElementType .TYPE })
409
- @ Cacheable
453
+ @ Cacheable ( cacheNames = "shadowed cache name" , key = "shadowed key" )
410
454
public @interface ComposedCacheable {
411
455
412
456
@ AliasFor (annotation = Cacheable .class , attribute = "cacheNames" )
@@ -419,4 +463,19 @@ public void multipleCacheConfig() {
419
463
String key () default "" ;
420
464
}
421
465
466
+ @ Retention (RetentionPolicy .RUNTIME )
467
+ @ Target ({ ElementType .METHOD , ElementType .TYPE })
468
+ @ CacheEvict (cacheNames = "shadowed cache name" , key = "shadowed key" )
469
+ public @interface ComposedCacheEvict {
470
+
471
+ @ AliasFor (annotation = Cacheable .class , attribute = "cacheNames" )
472
+ String [] value () default {};
473
+
474
+ @ AliasFor (annotation = Cacheable .class , attribute = "cacheNames" )
475
+ String [] cacheNames () default {};
476
+
477
+ @ AliasFor (annotation = Cacheable .class , attribute = "key" )
478
+ String key () default "" ;
479
+ }
480
+
422
481
}
0 commit comments