18
18
19
19
import java .util .UUID ;
20
20
21
- import org .junit .Before ;
22
21
import org .junit .Test ;
23
22
24
23
import org .springframework .cache .support .NoOpCacheManager ;
25
24
26
25
import static org .junit .Assert .*;
27
26
27
+ /**
28
+ * Tests for {@link NoOpCacheManager}.
29
+ *
30
+ * @author Costin Leau
31
+ * @author Stephane Nicoll
32
+ */
28
33
public class NoOpCacheManagerTests {
29
34
30
- private CacheManager manager ;
31
-
32
- @ Before
33
- public void setup () {
34
- manager = new NoOpCacheManager ();
35
- }
35
+ private final CacheManager manager = new NoOpCacheManager ();
36
36
37
37
@ Test
38
38
public void testGetCache () throws Exception {
39
- Cache cache = manager .getCache ("bucket" );
39
+ Cache cache = this . manager .getCache ("bucket" );
40
40
assertNotNull (cache );
41
- assertSame (cache , manager .getCache ("bucket" ));
41
+ assertSame (cache , this . manager .getCache ("bucket" ));
42
42
}
43
43
44
44
@ Test
45
45
public void testNoOpCache () throws Exception {
46
- String name = UUID . randomUUID (). toString ();
47
- Cache cache = manager .getCache (name );
46
+ String name = createRandomKey ();
47
+ Cache cache = this . manager .getCache (name );
48
48
assertEquals (name , cache .getName ());
49
49
Object key = new Object ();
50
50
cache .put (key , new Object ());
@@ -56,8 +56,37 @@ public void testNoOpCache() throws Exception {
56
56
@ Test
57
57
public void testCacheName () throws Exception {
58
58
String name = "bucket" ;
59
- assertFalse (manager .getCacheNames ().contains (name ));
60
- manager .getCache (name );
61
- assertTrue (manager .getCacheNames ().contains (name ));
59
+ assertFalse (this .manager .getCacheNames ().contains (name ));
60
+ this .manager .getCache (name );
61
+ assertTrue (this .manager .getCacheNames ().contains (name ));
62
+ }
63
+
64
+ @ Test
65
+ public void testCacheCallable () throws Exception {
66
+ String name = createRandomKey ();
67
+ Cache cache = this .manager .getCache (name );
68
+ Object returnValue = new Object ();
69
+ Object value = cache .get (new Object (), () -> returnValue );
70
+ assertEquals (returnValue , value );
62
71
}
72
+
73
+ @ Test
74
+ public void testCacheGetCallableFail () {
75
+ Cache cache = this .manager .getCache (createRandomKey ());
76
+ String key = createRandomKey ();
77
+ try {
78
+ cache .get (key , () -> {
79
+ throw new UnsupportedOperationException ("Expected exception" );
80
+ });
81
+ }
82
+ catch (Cache .ValueRetrievalException ex ) {
83
+ assertNotNull (ex .getCause ());
84
+ assertEquals (UnsupportedOperationException .class , ex .getCause ().getClass ());
85
+ }
86
+ }
87
+
88
+ private String createRandomKey () {
89
+ return UUID .randomUUID ().toString ();
90
+ }
91
+
63
92
}
0 commit comments