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 initMocks(this);
53 this.helper = new EhCacheHelper();
54
55 defaultCacheConfiguration = new CacheConfiguration();
56 final Configuration configuration = new Configuration();
57 configuration.setDefaultCacheConfiguration(defaultCacheConfiguration);
58 when(mockCacheManager.getConfiguration()).thenReturn(configuration);
59 }
60
61 @Test
62 public void cacheCreatedWithLimitedParametersShouldHaveExpectedValuesWhenLocalAndRmiSetUp() {
63 assertCacheCreatedWithLimitedParameters(true, true, false);
64 }
65 @Test
66 public void cacheCreatedWithLimitedParametersShouldHaveExpectedValuesWhenLocalAndRmiNotSetUp() {
67 assertCacheCreatedWithLimitedParameters(true, false, false);
68 }
69 @Test
70 public void cacheCreatedWithLimitedParametersShouldHaveExpectedValuesWhenNotLocalAndRmiSetUp() {
71 assertCacheCreatedWithLimitedParameters(false, true, false);
72 }
73 @Test
74 public void selfLoadingCacheCreatedWithLimitedParametersShouldHaveExpectedValuesWhenNotLocalAndRmiSetUp() {
75 assertCacheCreatedWithLimitedParameters(false, true, true);
76 }
77 @Test
78 public void cacheCreatedWithLimitedParametersShouldHaveExpectedValuesWhenNotLocalAndRmiNotSetUp() {
79 assertCacheCreatedWithLimitedParameters(false, false, false);
80 }
81
82 private void assertCacheCreatedWithLimitedParameters(final boolean local, final boolean setUpRmiProvider, final boolean selfLoading) {
83
84 if (setUpRmiProvider) {
85 setUpCacheManagerPeerProvider();
86 }
87 final CacheConfiguration cache = createCacheWithLimitedParameters(local, false, false, selfLoading);
88
89
90 assertEquals(CACHE_NAME, cache.getName());
91 assertEquals(0L, cache.getMaxEntriesLocalHeap());
92 assertEquals(PERSISTENCE_STRATEGY, cache.getPersistenceConfiguration().getStrategy());
93 assertTrue(cache.getStatistics());
94 assertEquals(DEFAULT_TTI, cache.getTimeToIdleSeconds());
95 assertEquals(DEFAULT_TTL, cache.getTimeToLiveSeconds());
96 assertFalse(cache.isEternal());
97 @SuppressWarnings("unchecked")
98 final List<CacheEventListenerFactoryConfiguration> cacheEventListenerConfigurations = cache.getCacheEventListenerConfigurations();
99 if (local || !setUpRmiProvider) {
100 assertEquals(Collections.<CacheEventListenerFactoryConfiguration>emptyList(), cacheEventListenerConfigurations);
101 }
102 else {
103 assertEquals(1, cacheEventListenerConfigurations.size());
104 final CacheEventListenerFactoryConfiguration listenerConfiguration = cacheEventListenerConfigurations.get(0);
105 assertEquals(RMICacheReplicatorFactory.class.getName(), listenerConfiguration.getFullyQualifiedClassPath());
106 if (selfLoading)
107 {
108 assertTrue(listenerConfiguration.getProperties().contains("replicatePuts=false"));
109 }
110 else
111 {
112 assertTrue(listenerConfiguration.getProperties().contains("replicatePuts=true"));
113 }
114 }
115 }
116
117 private void setUpCacheManagerPeerProvider() {
118 final CacheManagerPeerProvider mockRmiProvider = mock(CacheManagerPeerProvider.class);
119 when(mockCacheManager.getCacheManagerPeerProvider("RMI")).thenReturn(mockRmiProvider);
120 }
121
122 @Test
123 public void cacheCreatedWithAllParametersShouldHaveSpecifiedName() {
124
125 final CacheConfiguration cache = createCacheWithAllParameters(MAX_ENTRIES, EXPIRE_AFTER_ACCESS, EXPIRE_AFTER_WRITE);
126
127
128 assertEquals(CACHE_NAME, cache.getName());
129 }
130
131 @Test
132 public void cacheCreatedWithAllParametersShouldHaveSpecifiedMaxEntries() {
133
134 final CacheConfiguration cache = createCacheWithAllParameters(MAX_ENTRIES, EXPIRE_AFTER_ACCESS, EXPIRE_AFTER_WRITE);
135
136
137 assertEquals(MAX_ENTRIES, cache.getMaxEntriesLocalHeap());
138 }
139
140 @Test
141 public void cacheCreatedWithAllParametersShouldHaveSpecifiedExpireAfterAccess() {
142
143 final CacheConfiguration cache = createCacheWithAllParameters(MAX_ENTRIES, EXPIRE_AFTER_ACCESS, EXPIRE_AFTER_WRITE);
144
145
146 assertEquals(EXPIRE_AFTER_ACCESS, cache.getTimeToIdleSeconds());
147 }
148
149 @Test
150 public void cacheCreatedWithAllParametersShouldHaveSpecifiedExpireAfterWrite() {
151
152 final CacheConfiguration cache = createCacheWithAllParameters(MAX_ENTRIES, EXPIRE_AFTER_ACCESS, EXPIRE_AFTER_WRITE);
153
154
155 assertEquals(EXPIRE_AFTER_WRITE, cache.getTimeToLiveSeconds());
156 }
157
158 @Test
159 public void cacheShouldDefaultToEHCacheDefaultValues()
160 {
161 defaultCacheConfiguration.setMaxEntriesLocalHeap(999);
162
163
164 final CacheConfiguration cache = createCacheWithLimitedParameters(false, false, false,false);
165
166 assertEquals(999, cache.getMaxEntriesLocalHeap());
167 }
168
169 private CacheConfiguration createCacheWithLimitedParameters(
170 final boolean local, final boolean replicateAsynchronously, final boolean replicateViaCopy, final boolean selfLoading)
171 {
172
173 CacheSettingsBuilder builder = new CacheSettingsBuilder();
174 if (local)
175 {
176 builder.local();
177 }
178 else
179 {
180 builder.remote();
181 }
182 if (replicateAsynchronously)
183 {
184 builder.replicateAsynchronously();
185 }
186 if (replicateViaCopy)
187 {
188 builder.replicateViaCopy();
189 }
190
191 helper.getEhcache(CACHE_NAME, mockCacheManager, builder.build(), selfLoading, true);
192
193
194 final ArgumentCaptor<Cache> cacheCaptor = ArgumentCaptor.forClass(Cache.class);
195 verify(mockCacheManager).addCacheIfAbsent(cacheCaptor.capture());
196 return cacheCaptor.getValue().getCacheConfiguration();
197 }
198
199 private CacheConfiguration createCacheWithAllParameters(
200 final Integer maxEntries, final Long expireAfterAccess, final Long expireAfterWrite)
201 {
202
203 CacheSettingsBuilder builder = new CacheSettingsBuilder();
204 builder.local();
205 if (null != maxEntries)
206 {
207 builder.maxEntries(maxEntries);
208 }
209 if (null != expireAfterAccess)
210 {
211 builder.expireAfterAccess(expireAfterAccess, TimeUnit.SECONDS);
212 }
213 if (null != expireAfterWrite)
214 {
215 builder.expireAfterWrite(expireAfterWrite, TimeUnit.SECONDS);
216 }
217
218 helper.getEhcache(
219 CACHE_NAME, mockCacheManager, builder.build(), false, true);
220
221
222 final ArgumentCaptor<Cache> cacheCaptor = ArgumentCaptor.forClass(Cache.class);
223 verify(mockCacheManager).addCacheIfAbsent(cacheCaptor.capture());
224 return cacheCaptor.getValue().getCacheConfiguration();
225 }
226
227 @Test
228 public void cacheShouldBeEternalIfNoExpiryIsSetAndDefaultIsTrue() {
229 defaultCacheConfiguration.setEternal(true);
230 assertEternal(null, null, true);
231 }
232
233 @Test
234 public void cacheShouldNotBeEternalIfNoExpiryIsSetAndDefaultIsFalse() {
235 defaultCacheConfiguration.setEternal(false);
236 assertEternal(null, null, false);
237 }
238
239 @Test
240 public void cacheShouldNotBeEternalIfExpireAfterAccessIsSet() {
241 defaultCacheConfiguration.setEternal(true);
242 assertEternal(200L, null, false);
243 }
244
245 @Test
246 public void cacheShouldNotBeEternalIfExpireAfterWriteIsSet() {
247 defaultCacheConfiguration.setEternal(true);
248 assertEternal(null, 200L, false);
249 }
250
251 @Test
252 public void cacheShouldNotBeEternalIfExpireAfterAccessAndExpireAfterWriteAreSet() {
253 defaultCacheConfiguration.setEternal(true);
254 assertEternal(100L, 200L, false);
255 }
256
257 private void assertEternal(final Long expireAfterAccess, final Long expireAfterWrite, final boolean expectedValue) {
258
259 final CacheConfiguration configuration =
260 createCacheWithAllParameters(MAX_ENTRIES, expireAfterAccess, expireAfterWrite);
261
262
263 assertEquals(expectedValue, configuration.isEternal());
264 }
265 }