View Javadoc

1   package com.atlassian.cache.hazelcast;
2   
3   import com.atlassian.cache.Cache;
4   import com.atlassian.cache.CacheFactory;
5   import com.atlassian.cache.CacheLoader;
6   import com.atlassian.cache.CacheManager;
7   import com.atlassian.cache.CacheSettings;
8   import com.atlassian.cache.CacheSettingsBuilder;
9   import com.atlassian.cache.CachedReference;
10  import com.atlassian.cache.ManagedCache;
11  import com.atlassian.cache.Supplier;
12  
13  import org.junit.Before;
14  import org.junit.ClassRule;
15  import org.junit.Test;
16  import org.junit.runner.RunWith;
17  import org.mockito.Mock;
18  import org.mockito.runners.MockitoJUnitRunner;
19  
20  import static org.hamcrest.CoreMatchers.equalTo;
21  import static org.hamcrest.MatcherAssert.assertThat;
22  import static org.mockito.Matchers.any;
23  import static org.mockito.Matchers.anyString;
24  import static org.mockito.Matchers.eq;
25  import static org.mockito.Mockito.mock;
26  import static org.mockito.Mockito.times;
27  import static org.mockito.Mockito.verify;
28  import static org.mockito.Mockito.verifyNoMoreInteractions;
29  import static org.mockito.Mockito.when;
30  import static org.mockito.Mockito.withSettings;
31  
32  @RunWith(MockitoJUnitRunner.class)
33  @SuppressWarnings("rawtypes")  // mocks
34  public class HazelcastLocalCacheTest
35  {
36      @ClassRule
37      public static InitOnceHazelcastCluster cluster = InitOnceHazelcastCluster.getInstance();
38  
39      @Mock
40      private CacheFactory localFactory;
41      private Cache cache;
42      private CachedReference cachedReference;
43      private CacheManager cacheManager;
44  
45      @Before
46      public void setup()
47      {
48          cache = mock(Cache.class, withSettings().extraInterfaces(ManagedCache.class));
49          cachedReference = mock(CachedReference.class, withSettings().extraInterfaces(ManagedCache.class));
50          when(localFactory.getCache(anyString(), any(CacheLoader.class), any(CacheSettings.class)))
51                  .thenReturn(cache);
52          when(localFactory.getCachedReference(anyString(), any(Supplier.class), any(CacheSettings.class)))
53                  .thenReturn(cachedReference);
54  
55          cacheManager = new HazelcastCacheManager(cluster.getNode(0), localFactory, HazelcastTestSupport.getDefaultsProvider());
56      }
57  
58      @Test
59      public void testLocalCacheDelegatedToLocalFactory()
60      {
61          CacheLoader loader = mock(CacheLoader.class);
62          assertThat(cacheManager.getCache("test", loader, new CacheSettingsBuilder().local().build()), equalTo(cache));
63  
64          // the cacheManager should keep track of the created Cache instance and not call the localFactory again
65          assertThat(cacheManager.getCache("test"), equalTo(cache));
66  
67          verify(localFactory).getCache(eq("test"), eq(loader), any(CacheSettings.class));
68          verifyNoMoreInteractions(localFactory);
69      }
70  
71      @Test
72      public void testLocalCachedReferenceDelegatedToLocalFactory()
73      {
74          Supplier supplier = mock(Supplier.class);
75          assertThat(cacheManager.getCachedReference("test", supplier, new CacheSettingsBuilder().local().build()), equalTo(cachedReference));
76  
77          // the cacheManager call the localFactory again when a new reference is requested
78          assertThat(cacheManager.getCachedReference("test", supplier, new CacheSettingsBuilder().local().build()), equalTo(cachedReference));
79  
80          verify(localFactory, times(2)).getCachedReference(eq("test"), eq(supplier), any(CacheSettings.class));
81          verifyNoMoreInteractions(localFactory);
82      }
83  }