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