View Javadoc

1   package com.atlassian.cache;
2   
3   import java.util.concurrent.atomic.AtomicInteger;
4   
5   import org.junit.Test;
6   
7   import static org.hamcrest.core.IsEqual.equalTo;
8   import static org.hamcrest.core.IsInstanceOf.instanceOf;
9   import static org.junit.Assert.assertNotSame;
10  import static org.junit.Assert.assertThat;
11  import static org.junit.Assert.fail;
12  
13  /**
14   * Test the Lazy Cache
15   *
16   * @since 2.0
17   */
18  public abstract class AbstractCacheLazyTest extends AbstractCacheTest
19  {
20  
21      @Test
22      public void testGetName() throws Exception
23      {
24          Cache<String, Long> cache = makeUnexpiringCache();
25  
26          assertThat(cache.getName(), equalTo("mycache"));
27      }
28  
29      @Test
30      public void testFactoryGeneratedName() throws Exception
31      {
32          Cache<String, Long> cache = factory.getCache(Object.class, "mycache");
33  
34          assertThat(cache.getName(), equalTo("java.lang.Object.mycache"));
35      }
36  
37      @Test
38      public void testGetKeys() throws Exception
39      {
40          Cache<String, Long> cache = makeUnexpiringCache();
41          // Add some entries using the builder
42          cache.get("1");
43          cache.get("2");
44          cache.get("3");
45          cache.get("4");
46          assertThat(cache.get("1"), equalTo(1L));
47          assertThat(cache.get("2"), equalTo(2L));
48          assertThat(cache.get("3"), equalTo(3L));
49          assertThat(cache.get("4"), equalTo(4L));
50          assertSize(cache, 4);
51      }
52  
53      @Test
54      public void testConstructExpiringCache() throws Exception
55      {
56          Cache<String, Long> cache = makeExpiringCache();
57          // Add some entries using the builder
58          cache.get("1");
59          cache.get("2");
60          cache.get("3");
61          cache.get("4");
62          assertThat(cache.get("1"), equalTo(1L));
63          assertThat(cache.get("2"), equalTo(2L));
64          assertThat(cache.get("3"), equalTo(3L));
65          assertThat(cache.get("4"), equalTo(4L));
66          assertSize(cache, 4);
67      }
68  
69      @Test
70      public void testPut() throws Exception
71      {
72          Cache<String, Long> cache = makeUnexpiringCache();
73          // Add some entries using the builder
74          cache.put("1", 11L);
75          cache.put("2", 12L);
76          cache.put("3", 13L);
77          cache.put("4", 14L);
78          assertThat(cache.get("1"), equalTo(11L));
79          assertThat(cache.get("2"), equalTo(12L));
80          assertThat(cache.get("3"), equalTo(13L));
81          assertThat(cache.get("4"), equalTo(14L));
82          assertSize(cache, (4));
83      }
84  
85      @Test
86      public void testGet() throws Exception
87      {
88          Cache<String, Long> cache = makeUnexpiringCache();
89          // Add some entries using the builder
90          cache.put("1", 11L);
91          cache.put("2", 12L);
92          cache.put("3", 13L);
93          cache.put("4", 14L);
94          assertThat(cache.get("1"), equalTo(11L));
95          assertThat(cache.get("2"), equalTo(12L));
96          assertThat(cache.get("3"), equalTo(13L));
97          assertThat(cache.get("4"), equalTo(14L));
98          assertThat(cache.get("5"), equalTo(5L));
99          assertThat(cache.get("6"), equalTo(6L));
100         assertThat(cache.get("7"), equalTo(7L));
101         assertSize(cache, (7));
102     }
103 
104     @Test
105     public void testRemove() throws Exception
106     {
107         Cache<String, Long> cache = makeUnexpiringCache();
108         // Add some entries using the builder
109         cache.put("1", 11L);
110         cache.put("2", 12L);
111         cache.put("3", 13L);
112         cache.put("4", 14L);
113         assertThat(cache.get("1"), equalTo(11L));
114         assertThat(cache.get("2"), equalTo(12L));
115         assertThat(cache.get("3"), equalTo(13L));
116         assertThat(cache.get("4"), equalTo(14L));
117         assertSize(cache, (4));
118 
119         cache.remove("1");
120         cache.remove("2");
121         cache.remove("3");
122         assertSize(cache, (1));
123 
124         // Removed values should be recomputed
125         assertThat(cache.get("1"), equalTo(1L));
126         assertThat(cache.get("2"), equalTo(2L));
127         assertThat(cache.get("3"), equalTo(3L));
128         assertThat(cache.get("4"), equalTo(14L));
129         assertSize(cache, (4));
130     }
131 
132     @Test
133     public void testRemoveTwice() throws Exception
134     {
135         Cache<String, Long> cache = makeUnexpiringCache();
136 
137         // Add some entries using the builder
138         cache.put("1", 11L);
139         cache.put("2", 12L);
140         cache.put("3", 13L);
141         cache.put("4", 14L);
142         assertThat(cache.get("1"), equalTo(11L));
143         assertThat(cache.get("2"), equalTo(12L));
144         assertThat(cache.get("3"), equalTo(13L));
145         assertThat(cache.get("4"), equalTo(14L));
146         assertSize(cache, (4));
147 
148         cache.remove("1");
149         // Remove again should be a no-op
150         cache.remove("1");
151         assertSize(cache, (3));
152 
153         // Removed values should be recomputed
154         assertThat(cache.get("1"), equalTo(1L));
155         assertThat(cache.get("2"), equalTo(12L));
156         assertThat(cache.get("3"), equalTo(13L));
157         assertThat(cache.get("4"), equalTo(14L));
158         assertSize(cache, (4));
159     }
160 
161     @Test
162     public void testRemoveAll() throws Exception
163     {
164         Cache<String, Long> cache = makeUnexpiringCache();
165         // Add some entries using the builder
166         cache.put("1", 11L);
167         cache.put("2", 12L);
168         cache.put("3", 13L);
169         cache.put("4", 14L);
170         assertThat(cache.get("1"), equalTo(11L));
171         assertThat(cache.get("2"), equalTo(12L));
172         assertThat(cache.get("3"), equalTo(13L));
173         assertThat(cache.get("4"), equalTo(14L));
174         assertSize(cache, (4));
175 
176         cache.removeAll();
177         assertEmpty(cache);
178 
179         // Removed values should be recomputed
180         assertThat(cache.get("1"), equalTo(1L));
181         assertThat(cache.get("2"), equalTo(2L));
182         assertThat(cache.get("3"), equalTo(3L));
183         assertThat(cache.get("4"), equalTo(4L));
184         assertSize(cache, (4));
185     }
186 
187     @Test
188     public void testClear() throws Exception
189     {
190         Cache<String, Long> cache = makeUnexpiringCache();
191         // Add some entries using the builder
192         cache.put("1", 11L);
193         cache.put("2", 12L);
194         cache.put("3", 13L);
195         cache.put("4", 14L);
196         assertThat(cache.get("1"), equalTo(11L));
197         assertThat(cache.get("2"), equalTo(12L));
198         assertThat(cache.get("3"), equalTo(13L));
199         assertThat(cache.get("4"), equalTo(14L));
200         assertSize(cache, (4));
201 
202         ((ManagedCache) cache).clear();
203         assertEmpty(cache);
204 
205         // Removed values should be recomputed
206         assertThat(cache.get("1"), equalTo(1L));
207         assertThat(cache.get("2"), equalTo(2L));
208         assertThat(cache.get("3"), equalTo(3L));
209         assertThat(cache.get("4"), equalTo(4L));
210         assertSize(cache, (4));
211     }
212 
213     @Test (expected = CacheException.class)
214     public void testNullKey() throws Exception
215     {
216         makeNullReturningCache().get(null);
217     }
218 
219     @Test (expected = CacheException.class)
220     public void testNullValue() throws Exception
221     {
222         Cache<String, Long> cache = makeNullReturningCache();
223 
224         // Add some entries using the builder
225         assertThat(cache.get("1"), equalTo(1L));
226         assertThat(cache.get("2"), equalTo(2L));
227         assertThat(cache.get("3"), equalTo(3L));
228 
229         cache.get("George");
230     }
231 
232     @Test
233     public void testMaxEntries() throws Exception
234     {
235         final AtomicInteger loadCounter = new AtomicInteger();
236         Cache<String, Long> cache = makeSizeLimitedCache(3, loadCounter);
237 
238         // Add some entries using the builder
239         assertThat(cache.get("1"), equalTo(1L));
240         assertThat(cache.get("1"), equalTo(1L));
241         assertThat(cache.get("2"), equalTo(2L));
242         assertThat(cache.get("3"), equalTo(3L));
243         assertThat("loadCounter", loadCounter.get(), equalTo(3));
244         assertSize(cache, (3));
245         assertThat(cache.get("3"), equalTo(3L));
246         assertThat(cache.get("3"), equalTo(3L));
247         assertThat("loadCounter", loadCounter.get(), equalTo(3));
248         assertSize(cache, (3));
249         assertThat(cache.get("4"), equalTo(4L));
250         assertThat("loadCounter", loadCounter.get(), equalTo(4));
251         assertSize(cache, (3));
252     }
253 
254     @Test
255     public void testExceptionHandling() throws Exception
256     {
257         Cache<String, Long> cache = makeExceptionalCache();
258 
259         // Add some entries using the builder
260         assertThat(cache.get("1"), equalTo(1L));
261         assertThat(cache.get("2"), equalTo(2L));
262         assertThat(cache.get("3"), equalTo(3L));
263         try
264         {
265             cache.get("George");
266             fail("Should throw CacheException when the loader throws an exception");
267         }
268         catch (CacheException ex)
269         {
270             assertThat("This exception should wrap the original Exception", ex.getCause(), instanceOf(NumberFormatException.class));
271         }
272         try
273         {
274             Long v = cache.get(null);
275             fail("Should throw CacheException when the Key is a null");
276         }
277         catch (CacheException ex)
278         {
279             assertThat("This exception should wrap the original Exception", ex.getCause(), instanceOf(NullPointerException.class));
280         }
281         assertSize(cache, (3));
282     }
283 
284     @Test
285     public void testNewInstanceForEveryGet()
286     {
287         Cache<String, Long> cache1 = makeUnexpiringCache();
288         Cache<String, Long> cache2 = makeUnexpiringCache();
289 
290         assertNotSame("The cache manager should not return the same cache twice", cache1, cache2);
291     }
292 }