17
17
package org .springframework .cache ;
18
18
19
19
import java .util .Arrays ;
20
+ import java .util .Collection ;
20
21
import java .util .Collections ;
21
22
import java .util .List ;
22
23
24
+ import org .junit .Rule ;
23
25
import org .junit .Test ;
26
+ import org .junit .rules .ExpectedException ;
24
27
import org .mockito .Mockito ;
25
28
26
29
import org .springframework .cache .annotation .Cacheable ;
27
30
import org .springframework .cache .annotation .Caching ;
31
+ import org .springframework .cache .annotation .CachingConfigurerSupport ;
28
32
import org .springframework .cache .annotation .EnableCaching ;
29
33
import org .springframework .cache .concurrent .ConcurrentMapCache ;
30
34
import org .springframework .cache .concurrent .ConcurrentMapCacheManager ;
35
+ import org .springframework .cache .interceptor .AbstractCacheResolver ;
36
+ import org .springframework .cache .interceptor .CacheOperationInvocationContext ;
37
+ import org .springframework .cache .interceptor .CacheResolver ;
31
38
import org .springframework .cache .support .SimpleCacheManager ;
32
39
import org .springframework .context .annotation .AnnotationConfigApplicationContext ;
33
40
import org .springframework .context .annotation .Bean ;
45
52
*/
46
53
public class CacheReproTests {
47
54
55
+ @ Rule
56
+ public final ExpectedException thrown = ExpectedException .none ();
57
+
48
58
@ Test
49
59
public void spr11124 () throws Exception {
50
60
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext (Spr11124Config .class );
@@ -83,7 +93,7 @@ public void spr11592GetSimple() {
83
93
}
84
94
85
95
@ Test
86
- public void spr11592GetNeverCache (){
96
+ public void spr11592GetNeverCache () {
87
97
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext (Spr11592Config .class );
88
98
Spr11592Service bean = context .getBean (Spr11592Service .class );
89
99
Cache cache = context .getBean ("cache" , Cache .class );
@@ -99,6 +109,27 @@ public void spr11592GetNeverCache(){
99
109
context .close ();
100
110
}
101
111
112
+ @ Test
113
+ public void spr13081ConfigNoCacheNameIsRequired () {
114
+ AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext (Spr13081Config .class );
115
+ MyCacheResolver cacheResolver = context .getBean (MyCacheResolver .class );
116
+ Spr13081Service bean = context .getBean (Spr13081Service .class );
117
+ assertNull (cacheResolver .getCache ("foo" ).get ("foo" ));
118
+ Object result = bean .getSimple ("foo" ); // cache name = id
119
+ assertEquals (result , cacheResolver .getCache ("foo" ).get ("foo" ).get ());
120
+ }
121
+
122
+ @ Test
123
+ public void spr13081ConfigFailIfCacheResolverReturnsNullCacheName () {
124
+ AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext (Spr13081Config .class );
125
+ Spr13081Service bean = context .getBean (Spr13081Service .class );
126
+
127
+
128
+ thrown .expect (IllegalStateException .class );
129
+ thrown .expectMessage (MyCacheResolver .class .getName ());
130
+ bean .getSimple (null );
131
+ }
132
+
102
133
103
134
@ Configuration
104
135
@ EnableCaching
@@ -118,9 +149,9 @@ public Spr11124Service service() {
118
149
119
150
public interface Spr11124Service {
120
151
121
- public List <String > single (int id );
152
+ List <String > single (int id );
122
153
123
- public List <String > multiple (int id );
154
+ List <String > multiple (int id );
124
155
}
125
156
126
157
@@ -214,4 +245,49 @@ public Object getNeverCache(String key) {
214
245
}
215
246
}
216
247
248
+ @ Configuration
249
+ @ EnableCaching
250
+ public static class Spr13081Config extends CachingConfigurerSupport {
251
+
252
+ @ Bean
253
+ @ Override
254
+ public CacheResolver cacheResolver () {
255
+ return new MyCacheResolver ();
256
+ }
257
+
258
+ @ Bean
259
+ public Spr13081Service service () {
260
+ return new Spr13081Service ();
261
+ }
262
+
263
+ }
264
+
265
+ public static class MyCacheResolver extends AbstractCacheResolver {
266
+
267
+ public MyCacheResolver () {
268
+ super (new ConcurrentMapCacheManager ());
269
+ }
270
+
271
+ @ Override
272
+ protected Collection <String > getCacheNames (CacheOperationInvocationContext <?> context ) {
273
+ String cacheName = (String ) context .getArgs ()[0 ];
274
+ if (cacheName != null ) {
275
+ return Collections .singleton (cacheName );
276
+ }
277
+ return null ;
278
+ }
279
+
280
+ public Cache getCache (String name ) {
281
+ return getCacheManager ().getCache (name );
282
+ }
283
+ }
284
+
285
+ public static class Spr13081Service {
286
+
287
+ @ Cacheable
288
+ public Object getSimple (String cacheName ) {
289
+ return new Object ();
290
+ }
291
+ }
292
+
217
293
}
0 commit comments