View Javadoc
1   package com.atlassian.cache.impl;
2   
3   import java.util.concurrent.atomic.AtomicInteger;
4   
5   import com.atlassian.cache.CacheEntryListener;
6   
7   import org.junit.Test;
8   
9   import static com.atlassian.cache.impl.CacheEntryEventMatcher.event;
10  import static org.junit.Assert.assertEquals;
11  import static org.mockito.Mockito.mock;
12  import static org.mockito.Mockito.verify;
13  import static org.mockito.Mockito.verifyNoMoreInteractions;
14  
15  public class ValueCacheEntryListenerSupportTest
16  {
17      @Test
18      public void testNotifyListeners()
19      {
20          final AtomicInteger callCount = new AtomicInteger(0);
21          ValueCacheEntryListenerSupport<String, String> listenerSupport = new ValueCacheEntryListenerSupport<String, String>()
22          {
23              @Override
24              protected void init(CacheEntryListenerSupport<String, String> actualListenerSupport)
25              {
26                  callCount.incrementAndGet();
27              }
28          };
29  
30          CacheEntryListener<String, String> valueListener = mock(CacheEntryListener.class);
31          CacheEntryListener<String, String> valuelessListener = mock(CacheEntryListener.class);
32  
33          listenerSupport.add(valueListener, true);
34          listenerSupport.add(valuelessListener, false);
35  
36          listenerSupport.notifyAdd("ADD", "ADD_VALUE");
37          listenerSupport.notifyEvict("EVICT", "EVICT_VALUE");
38          listenerSupport.notifyRemove("REMOVE", "REMOVE_VALUE");
39          listenerSupport.notifyUpdate("UPDATE", "UPDATE_VALUE", "UPDATE_OLD_VALUE");
40  
41          verify(valueListener).onAdd(event("ADD", "ADD_VALUE", null));
42          verify(valueListener).onEvict(event("EVICT", null, "EVICT_VALUE"));
43          verify(valueListener).onRemove(event("REMOVE", null, "REMOVE_VALUE"));
44          verify(valueListener).onUpdate(event("UPDATE", "UPDATE_VALUE", "UPDATE_OLD_VALUE"));
45          verifyNoMoreInteractions(valueListener);
46  
47          verify(valuelessListener).onAdd(event("ADD", null, null));
48          verify(valuelessListener).onEvict(event("EVICT", null, null));
49          verify(valuelessListener).onRemove(event("REMOVE", null, null));
50          verify(valuelessListener).onUpdate(event("UPDATE", null, null));
51          verifyNoMoreInteractions(valuelessListener);
52  
53          assertEquals(1, callCount.get());
54      }
55  
56      @Test
57      public void testLazyValue()
58      {
59          final AtomicInteger callCount = new AtomicInteger(0);
60          ValueCacheEntryListenerSupport<String, String> listenerSupport = new ValueCacheEntryListenerSupport<String, String>()
61          {
62              @Override
63              protected void init(CacheEntryListenerSupport<String, String> actualListenerSupport)
64              {
65                  callCount.incrementAndGet();
66              }
67  
68              @Override
69              protected void initValue(CacheEntryListenerSupport<String, String> actualListenerSupport)
70              {
71                  throw new RuntimeException("Should not be called");
72              }
73          };
74  
75          CacheEntryListener<String, String> valuelessListener = mock(CacheEntryListener.class);
76          listenerSupport.add(valuelessListener, false);
77  
78          listenerSupport.notifyAdd("ADD", "ADD_VALUE");
79          listenerSupport.notifyEvict("EVICT", "EVICT_VALUE");
80          listenerSupport.notifyRemove("REMOVE", "REMOVE_VALUE");
81          listenerSupport.notifyUpdate("UPDATE", "UPDATE_VALUE", "UPDATE_OLD_VALUE");
82  
83          verify(valuelessListener).onAdd(event("ADD", null, null));
84          verify(valuelessListener).onEvict(event("EVICT", null, null));
85          verify(valuelessListener).onRemove(event("REMOVE", null, null));
86          verify(valuelessListener).onUpdate(event("UPDATE", null, null));
87          verifyNoMoreInteractions(valuelessListener);
88  
89          assertEquals(1, callCount.get());
90      }
91  
92      @Test
93      public void testLazyValueless()
94      {
95          final AtomicInteger callCount = new AtomicInteger(0);
96          ValueCacheEntryListenerSupport<String, String> listenerSupport = new ValueCacheEntryListenerSupport<String, String>()
97          {
98              @Override
99              protected void init(CacheEntryListenerSupport<String, String> actualListenerSupport)
100             {
101                 callCount.incrementAndGet();
102             }
103 
104             @Override
105             protected void initValueless(CacheEntryListenerSupport<String, String> actualListenerSupport)
106             {
107                 throw new RuntimeException("Should not be called");
108             }
109         };
110 
111         CacheEntryListener<String, String> valueListener = mock(CacheEntryListener.class);
112         listenerSupport.add(valueListener, true);
113 
114         listenerSupport.notifyAdd("ADD", "ADD_VALUE");
115         listenerSupport.notifyEvict("EVICT", "EVICT_VALUE");
116         listenerSupport.notifyRemove("REMOVE", "REMOVE_VALUE");
117         listenerSupport.notifyUpdate("UPDATE", "UPDATE_VALUE", "UPDATE_OLD_VALUE");
118 
119         verify(valueListener).onAdd(event("ADD", "ADD_VALUE", null));
120         verify(valueListener).onEvict(event("EVICT", null, "EVICT_VALUE"));
121         verify(valueListener).onRemove(event("REMOVE", null, "REMOVE_VALUE"));
122         verify(valueListener).onUpdate(event("UPDATE", "UPDATE_VALUE", "UPDATE_OLD_VALUE"));
123         verifyNoMoreInteractions(valueListener);
124 
125         assertEquals(1, callCount.get());
126     }
127 }