View Javadoc

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