View Javadoc
1   package com.atlassian.cache.ehcache;
2   
3   import com.atlassian.cache.CacheSettingsBuilder;
4   import com.atlassian.cache.ehcache.wrapper.MockWrappedValue;
5   import com.atlassian.cache.ehcache.wrapper.WrapperTestUtils;
6   import net.sf.ehcache.Cache;
7   import net.sf.ehcache.CacheManager;
8   import net.sf.ehcache.Element;
9   import net.sf.ehcache.config.CacheConfiguration;
10  import net.sf.ehcache.config.Configuration;
11  import net.sf.ehcache.config.MemoryUnit;
12  import org.junit.After;
13  import org.junit.Before;
14  import org.junit.Test;
15  
16  import java.util.Collection;
17  
18  import static org.junit.Assert.assertEquals;
19  import static org.junit.Assert.assertSame;
20  import static org.junit.Assert.assertTrue;
21  
22  public class DelegatingCacheWrapperTest {
23  
24      private DelegatingCache<Object, Object> testedDecorator;
25      private Cache testedUnderlyingCache;
26  
27      private CacheManager cacheManager;
28  
29      @Before
30      public void init() {
31          cacheManager = new CacheManager(new Configuration().name(DelegatingCacheWrapperTest.class.getName()));
32  
33          testedUnderlyingCache = new Cache(new CacheConfiguration().name("tested").maxBytesLocalHeap(10, MemoryUnit.MEGABYTES));
34          testedDecorator = DelegatingCache.create(testedUnderlyingCache, new CacheSettingsBuilder().local().build(), WrapperTestUtils.getValueProcessor());
35  
36          cacheManager.removeAllCaches();
37          cacheManager.addCache(testedUnderlyingCache);
38      }
39  
40      @After
41      public void destroy() {
42          cacheManager.shutdown();
43      }
44  
45      @Test
46      public void testContainsKey() {
47          Object key = new Object();
48          Object value = new Object();
49  
50          testedUnderlyingCache.put(new Element(new MockWrappedValue(key), new MockWrappedValue(value)));
51  
52          assertTrue(testedDecorator.containsKey(key));
53      }
54  
55      @SuppressWarnings("ConstantConditions")
56      @Test
57      public void testGetKeys() {
58          Object key = new Object();
59          Object value = generateValueFromKey(key);
60  
61          testedUnderlyingCache.put(new Element(new MockWrappedValue(key), new MockWrappedValue(value)));
62  
63          Collection<Object> keys = testedDecorator.getKeys();
64          assertEquals(1, keys.size());
65  
66          Object retrievedKey = keys.stream().findFirst().get();
67          assertSame(key, retrievedKey);
68      }
69  
70      @Test
71      public void testPut() {
72          Object key = new Object();
73          Object value = new Object();
74  
75          testedDecorator.put(key, value);
76  
77          assertUnderlyingElementIsWrappedAndEquals(key, value);
78      }
79  
80      @Test
81      public void testPutIfAbsentNonExisting() {
82          Object key = new Object();
83          Object value = new Object();
84  
85          testedDecorator.putIfAbsent(key, value);
86  
87          assertUnderlyingElementIsWrappedAndEquals(key, value);
88      }
89  
90      @Test
91      public void testPutIfAbsentExisting() {
92          Object key = new Object();
93          Object oldValue = new Object();
94          Object newValue = new Object();
95  
96          testedUnderlyingCache.put(new Element(new MockWrappedValue(key), new MockWrappedValue(oldValue)));
97  
98          testedDecorator.putIfAbsent(key, newValue);
99  
100         assertUnderlyingElementIsWrappedAndEquals(key, oldValue);
101     }
102 
103     @Test
104     public void testGet() {
105         Object key = new Object();
106         Object value = new Object();
107 
108         testedUnderlyingCache.put(new Element(new MockWrappedValue(key), new MockWrappedValue(value)));
109 
110         Object retrievedValue = testedDecorator.get(key);
111         assertSame(value, retrievedValue);
112     }
113 
114     @Test
115     public void testGetWithLoaderExistingValue() {
116         Object key = new Object();
117         Object value = generateValueFromKey(key);
118 
119         testedUnderlyingCache.put(new Element(new MockWrappedValue(key), new MockWrappedValue(value)));
120 
121         Object retrievedValue = testedDecorator.get(key, () -> generateValueFromKey(key));
122         assertSame(value, retrievedValue);
123     }
124 
125     @Test
126     public void testGetWithLoaderNonExistingValue() {
127         Object key = new Object();
128         Object value = generateValueFromKey(key);
129 
130         Object retrievedValue = testedDecorator.get(key, () -> generateValueFromKey(key));
131         assertEquals(value, retrievedValue);
132         assertUnderlyingElementIsWrappedAndEquals(key, value);
133     }
134 
135     @Test
136     public void testRemove() {
137         Object key = new Object();
138         Object value = new Object();
139 
140         testedUnderlyingCache.put(new Element(new MockWrappedValue(key), new MockWrappedValue(value)));
141 
142         assertEquals(1, testedDecorator.getKeys().size());
143         testedDecorator.remove(key);
144         assertEquals(0, testedDecorator.getKeys().size());
145     }
146 
147     @Test
148     public void testRemoveWithSameValue() {
149         Object key = new Object();
150         Object value = new Object();
151 
152         testedUnderlyingCache.put(new Element(new MockWrappedValue(key), new MockWrappedValue(value)));
153 
154         assertEquals(1, testedDecorator.getKeys().size());
155         testedDecorator.remove(key, value);
156         assertEquals(0, testedDecorator.getKeys().size());
157     }
158 
159     @Test
160     public void testRemoveWithDifferentValue() {
161         Object key = new Object();
162         Object value = new Object();
163         Object otherValue = new Object();
164 
165         testedUnderlyingCache.put(new Element(new MockWrappedValue(key), new MockWrappedValue(value)));
166 
167         assertEquals(1, testedDecorator.getKeys().size());
168         testedDecorator.remove(key, otherValue);
169         assertEquals(1, testedDecorator.getKeys().size());
170         assertUnderlyingElementIsWrappedAndEquals(key, value);
171     }
172 
173     @Test
174     public void testReplaceWithNonExistent() {
175         Object key = new Object();
176         Object oldValue = new Object();
177         Object newValue = new Object();
178 
179         testedDecorator.replace(key, oldValue, newValue);
180         assertEquals(0, testedDecorator.getKeys().size());
181     }
182 
183     @Test
184     public void testReplaceWithDifferentOldValue() {
185         Object key = new Object();
186         Object oldValue = new Object();
187         Object newValue = new Object();
188         Object otherValue = new Object();
189 
190         testedUnderlyingCache.put(new Element(new MockWrappedValue(key), new MockWrappedValue(oldValue)));
191 
192         testedDecorator.replace(key, otherValue, newValue);
193         assertEquals(1, testedDecorator.getKeys().size());
194         assertUnderlyingElementIsWrappedAndEquals(key, oldValue);
195     }
196 
197     @Test
198     public void testReplaceWithSameOldValue() {
199         Object key = new Object();
200         Object oldValue = new Object();
201         Object newValue = new Object();
202 
203         testedUnderlyingCache.put(new Element(new MockWrappedValue(key), new MockWrappedValue(oldValue)));
204 
205         testedDecorator.replace(key, oldValue, newValue);
206         assertEquals(1, testedDecorator.getKeys().size());
207         assertUnderlyingElementIsWrappedAndEquals(key, newValue);
208     }
209 
210     @Test
211     public void testRemoveAll() {
212         Object key = new Object();
213         Object value = new Object();
214 
215         testedUnderlyingCache.put(new Element(new MockWrappedValue(key), new MockWrappedValue(value)));
216 
217         assertEquals(1, testedDecorator.getKeys().size());
218         testedDecorator.removeAll();
219         assertEquals(0, testedDecorator.getKeys().size());
220     }
221 
222     @Test
223     public void testClear() {
224         Object key = new Object();
225         Object value = new Object();
226 
227         testedUnderlyingCache.put(new Element(new MockWrappedValue(key), new MockWrappedValue(value)));
228 
229         assertEquals(1, testedDecorator.getKeys().size());
230         testedDecorator.clear();
231         assertEquals(0, testedDecorator.getKeys().size());
232     }
233 
234     private void assertUnderlyingElementIsWrappedAndEquals(final Object key, final Object value) {
235         Element underlyingElement = testedUnderlyingCache.get(new MockWrappedValue(key));
236 
237         assertEquals(MockWrappedValue.class, underlyingElement.getObjectValue().getClass());
238         assertEquals(MockWrappedValue.class, underlyingElement.getObjectKey().getClass());
239         assertEquals(value, ((MockWrappedValue) underlyingElement.getObjectValue()).getValue());
240         assertEquals(key, ((MockWrappedValue) underlyingElement.getObjectKey()).getValue());
241     }
242 
243     private String generateValueFromKey(final Object key) {
244         return "Loaded value: " + key.getClass().getName() + "@" + Integer.toHexString(key.hashCode());
245     }
246 }