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