001
014
015 package com.liferay.portal.cache.test;
016
017 import com.liferay.portal.kernel.cache.PortalCache;
018 import com.liferay.portal.kernel.cache.PortalCacheException;
019 import com.liferay.portal.kernel.cache.PortalCacheListener;
020 import com.liferay.portal.kernel.util.HashUtil;
021
022 import java.io.Serializable;
023
024 import java.util.ArrayList;
025 import java.util.List;
026
027 import org.junit.Assert;
028
029
032 public class TestPortalCacheListener<K extends Serializable, V>
033 implements PortalCacheListener<K, V> {
034
035 public void assertActionsCount(int count) {
036 Assert.assertEquals(count, _actions.size());
037 }
038
039 public void assertEvicted(K key, V value, int timeToLive) {
040 _assertAction(ActionType.EVICT, key, value, timeToLive);
041 }
042
043 public void assertExpired(K key, V value, int timeToLive) {
044 _assertAction(ActionType.EXPIRE, key, value, timeToLive);
045 }
046
047 public void assertPut(K key, V value) {
048 _assertAction(
049 ActionType.PUT, key, value, PortalCache.DEFAULT_TIME_TO_LIVE);
050 }
051
052 public void assertPut(K key, V value, int timeToLive) {
053 _assertAction(ActionType.PUT, key, value, timeToLive);
054 }
055
056 public void assertRemoveAll() {
057 _assertAction(
058 ActionType.REMOVE_ALL, null, null,
059 PortalCache.DEFAULT_TIME_TO_LIVE);
060 }
061
062 public void assertRemoved(K key, V value) {
063 _assertAction(
064 ActionType.REMOVE, key, value, PortalCache.DEFAULT_TIME_TO_LIVE);
065 }
066
067 public void assertRemoved(K key, V value, int timeToLive) {
068 _assertAction(ActionType.REMOVE, key, value, timeToLive);
069 }
070
071 public void assertUpdated(K key, V value) {
072 _assertAction(
073 ActionType.UPDATE, key, value, PortalCache.DEFAULT_TIME_TO_LIVE);
074 }
075
076 public void assertUpdated(K key, V value, int timeToLive) {
077 _assertAction(ActionType.UPDATE, key, value, timeToLive);
078 }
079
080 @Override
081 public void dispose() {
082 }
083
084 @Override
085 public void notifyEntryEvicted(
086 PortalCache<K, V> portalCache, K key, V value, int timeToLive)
087 throws PortalCacheException {
088
089 _actions.add(new Action(ActionType.EVICT, key, value, timeToLive));
090 }
091
092 @Override
093 public void notifyEntryExpired(
094 PortalCache<K, V> portalCache, K key, V value, int timeToLive)
095 throws PortalCacheException {
096
097 _actions.add(new Action(ActionType.EXPIRE, key, value, timeToLive));
098 }
099
100 @Override
101 public void notifyEntryPut(
102 PortalCache<K, V> portalCache, K key, V value, int timeToLive)
103 throws PortalCacheException {
104
105 _actions.add(new Action(ActionType.PUT, key, value, timeToLive));
106 }
107
108 @Override
109 public void notifyEntryRemoved(
110 PortalCache<K, V> portalCache, K key, V value, int timeToLive)
111 throws PortalCacheException {
112
113 _actions.add(new Action(ActionType.REMOVE, key, value, timeToLive));
114 }
115
116 @Override
117 public void notifyEntryUpdated(
118 PortalCache<K, V> portalCache, K key, V value, int timeToLive)
119 throws PortalCacheException {
120
121 _actions.add(new Action(ActionType.UPDATE, key, value, timeToLive));
122 }
123
124 @Override
125 public void notifyRemoveAll(PortalCache<K, V> portalCache)
126 throws PortalCacheException {
127
128 _actions.add(
129 new Action(
130 ActionType.REMOVE_ALL, null, null,
131 PortalCache.DEFAULT_TIME_TO_LIVE));
132 }
133
134 public void reset() {
135 _actions.clear();
136 }
137
138 private void _assertAction(
139 ActionType actionType, K key, V value, int timeToLive) {
140
141 Action action = new Action(actionType, key, value, timeToLive);
142
143 Assert.assertTrue(_actions.contains(action));
144 }
145
146 private final List<Action> _actions = new ArrayList<>();
147
148 private static class Action {
149
150 public Action(
151 ActionType actionType, Object key, Object value, int timeToLive) {
152
153 _actionType = actionType;
154
155 if (key == null) {
156 key = _NULL_OBJECT;
157 }
158
159 if (value == null) {
160 value = _NULL_OBJECT;
161 }
162
163 _key = key;
164 _value = value;
165 _timeToLive = timeToLive;
166 }
167
168 @Override
169 public boolean equals(Object object) {
170 if (this == object) {
171 return true;
172 }
173
174 if (!(object instanceof Action)) {
175 return false;
176 }
177
178 Action action = (Action)object;
179
180 if (_actionType.equals(action._actionType) &&
181 _key.equals(action._key) && _value.equals(action._value) &&
182 (_timeToLive == action._timeToLive)) {
183
184 return true;
185 }
186
187 return false;
188 }
189
190 @SuppressWarnings("unused")
191 public ActionType getActionType() {
192 return _actionType;
193 }
194
195 @SuppressWarnings("unused")
196 public Object getKey() {
197 return _key;
198 }
199
200 @SuppressWarnings("unused")
201 public int getTimeToLive() {
202 return _timeToLive;
203 }
204
205 @SuppressWarnings("unused")
206 public Object getValue() {
207 return _value;
208 }
209
210 @Override
211 public int hashCode() {
212 int hash = HashUtil.hash(0, _actionType);
213
214 hash = HashUtil.hash(hash, _key);
215 hash = HashUtil.hash(hash, _value);
216
217 return HashUtil.hash(hash, _timeToLive);
218 }
219
220 private static final Object _NULL_OBJECT = new Object();
221
222 private final ActionType _actionType;
223 private final Object _key;
224 private int _timeToLive;
225 private final Object _value;
226
227 }
228
229 private enum ActionType {
230
231 EVICT, EXPIRE, PUT, REMOVE, REMOVE_ALL, UPDATE
232
233 }
234
235 }