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