1 package com.atlassian.cache.ehcache;
2
3 import java.util.Collections;
4 import java.util.List;
5 import java.util.concurrent.TimeUnit;
6
7 import com.atlassian.cache.CacheSettingsBuilder;
8
9 import org.junit.Before;
10 import org.junit.Test;
11 import org.mockito.ArgumentCaptor;
12 import org.mockito.Mock;
13
14 import net.sf.ehcache.Cache;
15 import net.sf.ehcache.CacheManager;
16 import net.sf.ehcache.config.CacheConfiguration;
17 import net.sf.ehcache.config.Configuration;
18 import net.sf.ehcache.distribution.CacheManagerPeerProvider;
19
20 import static com.atlassian.cache.ehcache.EhCacheHelper.PERSISTENCE_STRATEGY;
21 import static net.sf.ehcache.config.CacheConfiguration.CacheEventListenerFactoryConfiguration;
22 import static net.sf.ehcache.config.CacheConfiguration.DEFAULT_TTI;
23 import static net.sf.ehcache.config.CacheConfiguration.DEFAULT_TTL;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertTrue;
27 import static org.mockito.Mockito.mock;
28 import static org.mockito.Mockito.verify;
29 import static org.mockito.Mockito.when;
30 import static org.mockito.MockitoAnnotations.initMocks;
31
32
33
34
35
36
37 public class EhCacheHelperTest
38 {
39
40 private static final int MAX_ENTRIES = 10;
41 private static final long EXPIRE_AFTER_ACCESS = 20;
42 private static final long EXPIRE_AFTER_WRITE = 30;
43 private static final String CACHE_NAME = "MyCache";
44
45
46 private EhCacheHelper helper;
47 @Mock private CacheManager mockCacheManager;
48 private CacheConfiguration defaultCacheConfiguration;
49
50 @Before
51 public void setUp()
52 {
53 initMocks(this);
54 this.helper = new EhCacheHelper();
55
56 defaultCacheConfiguration = new CacheConfiguration();
57 final Configuration configuration = new Configuration();
58 configuration.setDefaultCacheConfiguration(defaultCacheConfiguration);
59 when(mockCacheManager.getConfiguration()).thenReturn(configuration);
60 }
61
62 @Test
63 public void cacheCreatedWithLimitedParametersShouldHaveExpectedValuesWhenLocalAndRmiSetUp()
64 {
65 assertCacheCreatedWithLimitedParameters(true, true, false);
66 }
67 @Test
68 public void cacheCreatedWithLimitedParametersShouldHaveExpectedValuesWhenLocalAndRmiNotSetUp()
69 {
70 assertCacheCreatedWithLimitedParameters(true, false, false);
71 }
72 @Test
73 public void cacheCreatedWithLimitedParametersShouldHaveExpectedValuesWhenNotLocalAndRmiSetUp()
74 {
75 assertCacheCreatedWithLimitedParameters(false, true, false);
76 }
77 @Test
78 public void selfLoadingCacheCreatedWithLimitedParametersShouldHaveExpectedValuesWhenNotLocalAndRmiSetUp()
79 {
80 assertCacheCreatedWithLimitedParameters(false, true, true);
81 }
82 @Test
83 public void cacheCreatedWithLimitedParametersShouldHaveExpectedValuesWhenNotLocalAndRmiNotSetUp()
84 {
85 assertCacheCreatedWithLimitedParameters(false, false, false);
86 }
87
88 private void assertCacheCreatedWithLimitedParameters(final boolean local, final boolean setUpRmiProvider, final boolean selfLoading)
89 {
90
91 if (setUpRmiProvider)
92 {
93 setUpCacheManagerPeerProvider();
94 }
95 final boolean replicateAsynchronously = false;
96 final boolean replicateViaCopy = false;
97 final CacheConfiguration cache = createCacheWithLimitedParameters(local, replicateAsynchronously, replicateViaCopy, selfLoading);
98
99
100 assertEquals(CACHE_NAME, cache.getName());
101 assertEquals(0L, cache.getMaxEntriesLocalHeap());
102 assertEquals(PERSISTENCE_STRATEGY, cache.getPersistenceConfiguration().getStrategy());
103 assertTrue(cache.getStatistics());
104 assertEquals(DEFAULT_TTI, cache.getTimeToIdleSeconds());
105 assertEquals(DEFAULT_TTL, cache.getTimeToLiveSeconds());
106 assertFalse(cache.isEternal());
107 @SuppressWarnings("unchecked")
108 final List<CacheEventListenerFactoryConfiguration> cacheEventListenerConfigurations = cache.getCacheEventListenerConfigurations();
109 if (local || !setUpRmiProvider)
110 {
111 assertEquals(Collections.<CacheEventListenerFactoryConfiguration>emptyList(), cacheEventListenerConfigurations);
112 }
113 else {
114 assertEquals(1, cacheEventListenerConfigurations.size());
115 final CacheEventListenerFactoryConfiguration listenerConfiguration = cacheEventListenerConfigurations.get(0);
116 assertEquals(RMICacheReplicatorFactory.class.getName(), listenerConfiguration.getFullyQualifiedClassPath());
117 if (!selfLoading && replicateViaCopy)
118 {
119 assertTrue(listenerConfiguration.getProperties().contains("replicatePuts=true"));
120 }
121 else
122 {
123 assertTrue(listenerConfiguration.getProperties().contains("replicatePuts=false"));
124 }
125 }
126 }
127
128 private void setUpCacheManagerPeerProvider()
129 {
130 final CacheManagerPeerProvider mockRmiProvider = mock(CacheManagerPeerProvider.class);
131 when(mockCacheManager.getCacheManagerPeerProvider("RMI")).thenReturn(mockRmiProvider);
132 }
133
134 @Test
135 public void cacheCreatedWithAllParametersShouldHaveSpecifiedName()
136 {
137
138 final CacheConfiguration cache = createCacheWithAllParameters(MAX_ENTRIES, EXPIRE_AFTER_ACCESS, EXPIRE_AFTER_WRITE);
139
140
141 assertEquals(CACHE_NAME, cache.getName());
142 }
143
144 @Test
145 public void cacheCreatedWithAllParametersShouldHaveSpecifiedMaxEntries()
146 {
147
148 final CacheConfiguration cache = createCacheWithAllParameters(MAX_ENTRIES, EXPIRE_AFTER_ACCESS, EXPIRE_AFTER_WRITE);
149
150
151 assertEquals(MAX_ENTRIES, cache.getMaxEntriesLocalHeap());
152 }
153
154 @Test
155 public void cacheCreatedWithAllParametersShouldHaveSpecifiedExpireAfterAccess()
156 {
157
158 final CacheConfiguration cache = createCacheWithAllParameters(MAX_ENTRIES, EXPIRE_AFTER_ACCESS, EXPIRE_AFTER_WRITE);
159
160
161 assertEquals(EXPIRE_AFTER_ACCESS, cache.getTimeToIdleSeconds());
162 }
163
164 @Test
165 public void cacheCreatedWithAllParametersShouldHaveSpecifiedExpireAfterWrite()
166 {
167
168 final CacheConfiguration cache = createCacheWithAllParameters(MAX_ENTRIES, EXPIRE_AFTER_ACCESS, EXPIRE_AFTER_WRITE);
169
170
171 assertEquals(EXPIRE_AFTER_WRITE, cache.getTimeToLiveSeconds());
172 }
173
174 @Test
175 public void cacheShouldDefaultToEHCacheDefaultValues()
176 {
177 defaultCacheConfiguration.setMaxEntriesLocalHeap(999);
178
179
180 final CacheConfiguration cache = createCacheWithLimitedParameters(false, false, false,false);
181
182 assertEquals(999, cache.getMaxEntriesLocalHeap());
183 }
184
185 private CacheConfiguration createCacheWithLimitedParameters(
186 final boolean local, final boolean replicateAsynchronously, final boolean replicateViaCopy, final boolean selfLoading)
187 {
188
189 CacheSettingsBuilder builder = new CacheSettingsBuilder();
190 if (local)
191 {
192 builder.local();
193 }
194 else
195 {
196 builder.remote();
197 }
198 if (replicateAsynchronously)
199 {
200 builder.replicateAsynchronously();
201 }
202 if (replicateViaCopy)
203 {
204 builder.replicateViaCopy();
205 }
206
207 helper.getEhcache(CACHE_NAME, mockCacheManager, builder.build(), selfLoading, true);
208
209
210 final ArgumentCaptor<Cache> cacheCaptor = ArgumentCaptor.forClass(Cache.class);
211 verify(mockCacheManager).addCacheIfAbsent(cacheCaptor.capture());
212 return cacheCaptor.getValue().getCacheConfiguration();
213 }
214
215 private CacheConfiguration createCacheWithAllParameters(
216 final Integer maxEntries, final Long expireAfterAccess, final Long expireAfterWrite)
217 {
218
219 CacheSettingsBuilder builder = new CacheSettingsBuilder();
220 builder.local();
221 if (null != maxEntries)
222 {
223 builder.maxEntries(maxEntries);
224 }
225 if (null != expireAfterAccess)
226 {
227 builder.expireAfterAccess(expireAfterAccess, TimeUnit.SECONDS);
228 }
229 if (null != expireAfterWrite)
230 {
231 builder.expireAfterWrite(expireAfterWrite, TimeUnit.SECONDS);
232 }
233
234 helper.getEhcache(
235 CACHE_NAME, mockCacheManager, builder.build(), false, true);
236
237
238 final ArgumentCaptor<Cache> cacheCaptor = ArgumentCaptor.forClass(Cache.class);
239 verify(mockCacheManager).addCacheIfAbsent(cacheCaptor.capture());
240 return cacheCaptor.getValue().getCacheConfiguration();
241 }
242
243 @Test
244 public void cacheShouldBeEternalIfNoExpiryIsSetAndDefaultIsTrue()
245 {
246 defaultCacheConfiguration.setEternal(true);
247 assertEternal(null, null, true);
248 }
249
250 @Test
251 public void cacheShouldNotBeEternalIfNoExpiryIsSetAndDefaultIsFalse()
252 {
253 defaultCacheConfiguration.setEternal(false);
254 assertEternal(null, null, false);
255 }
256
257 @Test
258 public void cacheShouldNotBeEternalIfExpireAfterAccessIsSet()
259 {
260 defaultCacheConfiguration.setEternal(true);
261 assertEternal(200L, null, false);
262 }
263
264 @Test
265 public void cacheShouldNotBeEternalIfExpireAfterWriteIsSet()
266 {
267 defaultCacheConfiguration.setEternal(true);
268 assertEternal(null, 200L, false);
269 }
270
271 @Test
272 public void cacheShouldNotBeEternalIfExpireAfterAccessAndExpireAfterWriteAreSet()
273 {
274 defaultCacheConfiguration.setEternal(true);
275 assertEternal(100L, 200L, false);
276 }
277
278 private void assertEternal(final Long expireAfterAccess, final Long expireAfterWrite, final boolean expectedValue)
279 {
280
281 final CacheConfiguration configuration =
282 createCacheWithAllParameters(MAX_ENTRIES, expireAfterAccess, expireAfterWrite);
283
284
285 assertEquals(expectedValue, configuration.isEternal());
286 }
287 }