View Javadoc

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