1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.springframework.cache.concurrent;
18
19 import java.util.concurrent.ConcurrentHashMap;
20 import java.util.concurrent.ConcurrentMap;
21
22 import org.junit.Before;
23 import org.junit.Test;
24
25 import org.springframework.cache.Cache;
26
27 import static org.junit.Assert.*;
28
29
30
31
32
33
34 public class ConcurrentCacheTests {
35
36 protected final static String CACHE_NAME = "testCache";
37
38 protected ConcurrentMap<Object, Object> nativeCache;
39
40 protected Cache cache;
41
42
43 @Before
44 public void setUp() throws Exception {
45 nativeCache = new ConcurrentHashMap<Object, Object>();
46 cache = new ConcurrentMapCache(CACHE_NAME, nativeCache, true);
47 cache.clear();
48 }
49
50
51 @Test
52 public void testCacheName() throws Exception {
53 assertEquals(CACHE_NAME, cache.getName());
54 }
55
56 @Test
57 public void testNativeCache() throws Exception {
58 assertSame(nativeCache, cache.getNativeCache());
59 }
60
61 @Test
62 public void testCachePut() throws Exception {
63 Object key = "enescu";
64 Object value = "george";
65
66 assertNull(cache.get(key));
67 assertNull(cache.get(key, String.class));
68 assertNull(cache.get(key, Object.class));
69
70 cache.put(key, value);
71 assertEquals(value, cache.get(key).get());
72 assertEquals(value, cache.get(key, String.class));
73 assertEquals(value, cache.get(key, Object.class));
74 assertEquals(value, cache.get(key, null));
75
76 cache.put(key, null);
77 assertNotNull(cache.get(key));
78 assertNull(cache.get(key).get());
79 assertNull(cache.get(key, String.class));
80 assertNull(cache.get(key, Object.class));
81 }
82
83 @Test
84 public void testCachePutIfAbsent() throws Exception {
85 Object key = new Object();
86 Object value = "initialValue";
87
88 assertNull(cache.get(key));
89 assertNull(cache.putIfAbsent(key, value));
90 assertEquals(value, cache.get(key).get());
91 assertEquals("initialValue", cache.putIfAbsent(key, "anotherValue").get());
92 assertEquals(value, cache.get(key).get());
93 }
94
95 @Test
96 public void testCacheRemove() throws Exception {
97 Object key = "enescu";
98 Object value = "george";
99
100 assertNull(cache.get(key));
101 cache.put(key, value);
102 }
103
104 @Test
105 public void testCacheClear() throws Exception {
106 assertNull(cache.get("enescu"));
107 cache.put("enescu", "george");
108 assertNull(cache.get("vlaicu"));
109 cache.put("vlaicu", "aurel");
110 cache.clear();
111 assertNull(cache.get("vlaicu"));
112 assertNull(cache.get("enescu"));
113 }
114
115 }