View Javadoc

1   package com.atlassian.cache;
2   
3   import java.util.concurrent.TimeUnit;
4   
5   import org.junit.Test;
6   import org.mockito.ArgumentCaptor;
7   
8   import static org.junit.Assert.assertEquals;
9   import static org.mockito.Mockito.mock;
10  import static org.mockito.Mockito.timeout;
11  import static org.mockito.Mockito.verify;
12  import static org.mockito.Mockito.when;
13  
14  @SuppressWarnings ("unchecked")
15  public abstract class AbstractCachedReferenceListenerTest
16  {
17  
18      //com.hazelcast.map.impl.eviction.ExpirationManager.INITIAL_DELAY/com.hazelcast.map.impl.eviction.ExpirationManager.PERIOD
19      private final static int EXPIRY_TIMEOUT = 5000;
20  
21      protected CacheFactory factory;
22  
23      @Test
24      public void testOnSet() throws Exception
25      {
26          Supplier<String> supplier = mock(Supplier.class);
27          when(supplier.get()).thenReturn("SET");
28  
29          CachedReference<String> reference = factory.getCachedReference("set", supplier, settingsBuilder().build());
30  
31          CachedReferenceListener listener = mock(CachedReferenceListener.class);
32          reference.addListener(listener, true);
33  
34          reference.get();
35  
36          ArgumentCaptor<CachedReferenceEvent> captor = ArgumentCaptor.forClass(CachedReferenceEvent.class);
37          verify(listener, timeout(1000)).onSet(captor.capture());
38  
39          CachedReferenceEvent<String> event = captor.getValue();
40          assertEquals("SET", event.getValue());
41      }
42  
43      @Test
44      public void testOnReset() throws Exception
45      {
46          Supplier<String> supplier = mock(Supplier.class);
47          when(supplier.get()).thenReturn("RESET");
48  
49          CachedReference<String> reference = factory.getCachedReference("reset", supplier, settingsBuilder().build());
50  
51          CachedReferenceListener listener = mock(CachedReferenceListener.class);
52          reference.addListener(listener, true);
53  
54          reference.get();
55  
56          reference.reset();
57  
58          ArgumentCaptor<CachedReferenceEvent> captor = ArgumentCaptor.forClass(CachedReferenceEvent.class);
59          //With the changes to the delegated memory reset / remove there will be another call on the
60          //listener when this test is run as part of the suite
61          verify(listener, timeout(1000).atLeastOnce()).onReset(captor.capture());
62  
63          CachedReferenceEvent<String> event = captor.getValue();
64          assertEquals("RESET", event.getValue());
65      }
66  
67      @Test
68      public void testOnEvict() throws Exception
69      {
70          Supplier<String> supplier = mock(Supplier.class);
71          when(supplier.get()).thenReturn("EVICT");
72  
73          CachedReference<String> reference = factory.getCachedReference("evict", supplier,
74                  settingsBuilder().expireAfterAccess(1, TimeUnit.SECONDS).expireAfterWrite(1, TimeUnit.SECONDS).build());
75  
76          CachedReferenceListener listener = mock(CachedReferenceListener.class);
77          reference.addListener(listener, true);
78  
79          reference.get();
80  
81          Thread.sleep(EXPIRY_TIMEOUT);
82  
83          reference.get();
84  
85          ArgumentCaptor<CachedReferenceEvent> captor = ArgumentCaptor.forClass(CachedReferenceEvent.class);
86          verify(listener, timeout(EXPIRY_TIMEOUT).atLeastOnce()).onEvict(captor.capture());
87  
88          CachedReferenceEvent<String> event = captor.getValue();
89          assertEquals("EVICT", event.getValue());
90      }
91  
92      protected CacheSettingsBuilder settingsBuilder()
93      {
94          return new CacheSettingsBuilder();
95      }
96  
97      protected boolean supportsSetValue()
98      {
99          return true;
100     }
101 
102     protected boolean supportsResetOldValue()
103     {
104         return true;
105     }
106 
107     protected boolean supportsEvictOldValue()
108     {
109         return true;
110     }
111 }