1818
1919import java .util .UUID ;
2020
21- import org .junit .Before ;
2221import org .junit .Test ;
2322
2423import org .springframework .cache .support .NoOpCacheManager ;
2524
2625import static org .junit .Assert .*;
2726
27+ /**
28+ * Tests for {@link NoOpCacheManager}.
29+ *
30+ * @author Costin Leau
31+ * @author Stephane Nicoll
32+ */
2833public class NoOpCacheManagerTests {
2934
30- private CacheManager manager ;
31-
32- @ Before
33- public void setup () {
34- manager = new NoOpCacheManager ();
35- }
35+ private final CacheManager manager = new NoOpCacheManager ();
3636
3737 @ Test
3838 public void testGetCache () throws Exception {
39- Cache cache = manager .getCache ("bucket" );
39+ Cache cache = this . manager .getCache ("bucket" );
4040 assertNotNull (cache );
41- assertSame (cache , manager .getCache ("bucket" ));
41+ assertSame (cache , this . manager .getCache ("bucket" ));
4242 }
4343
4444 @ Test
4545 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 );
4848 assertEquals (name , cache .getName ());
4949 Object key = new Object ();
5050 cache .put (key , new Object ());
@@ -56,8 +56,37 @@ public void testNoOpCache() throws Exception {
5656 @ Test
5757 public void testCacheName () throws Exception {
5858 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 );
6271 }
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+
6392}
0 commit comments