View Javadoc

1   /***
2    *  Copyright 2003-2010 Terracotta, Inc.
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   */
16  
17  package net.sf.ehcache.terracotta;
18  
19  import java.io.IOException;
20  import java.util.ArrayList;
21  import java.util.Collection;
22  import java.util.HashMap;
23  import java.util.List;
24  import java.util.Map;
25  import java.util.Set;
26  
27  import net.sf.ehcache.CacheException;
28  import net.sf.ehcache.Element;
29  import net.sf.ehcache.Status;
30  import net.sf.ehcache.concurrent.CacheLockProvider;
31  import net.sf.ehcache.constructs.nonstop.NullCacheLockProvider;
32  import net.sf.ehcache.search.Attribute;
33  import net.sf.ehcache.search.Results;
34  import net.sf.ehcache.search.attribute.AttributeExtractor;
35  import net.sf.ehcache.store.ElementValueComparator;
36  import net.sf.ehcache.store.Policy;
37  import net.sf.ehcache.store.StoreListener;
38  import net.sf.ehcache.store.StoreQuery;
39  import net.sf.ehcache.store.TerracottaStore;
40  import net.sf.ehcache.writer.CacheWriterManager;
41  
42  public class TestRejoinStore implements TerracottaStore {
43  
44      public enum StoreAction {
45          NONE, BLOCKING, EXCEPTION;
46      }
47  
48      private final Map<Object, Element> map = new HashMap<Object, Element>();
49      private volatile StoreAction storeAction = StoreAction.NONE;
50      private final CacheLockProvider cacheLockProvider = new NullCacheLockProvider();
51      private final List<String> calledMethods = new ArrayList<String>();
52  
53      public void setBlocking(boolean blocking) {
54          if (blocking) {
55              this.setStoreAction(StoreAction.BLOCKING);
56          } else {
57              this.setStoreAction(StoreAction.NONE);
58          }
59      }
60  
61      public void setStoreAction(StoreAction storeAction) {
62          this.storeAction = storeAction;
63      }
64  
65      public synchronized List<String> getCalledMethods() {
66          return calledMethods;
67      }
68  
69      public synchronized void clearCalledMethods() {
70          calledMethods.clear();
71      }
72  
73      private void alwaysCalledMethod() {
74          StackTraceElement lastMethod = new Exception().getStackTrace()[1];
75          synchronized (this) {
76              calledMethods.add(lastMethod.getMethodName());
77          }
78  
79          switch (storeAction) {
80              case BLOCKING: {
81                  try {
82                      Thread.currentThread().join();
83                  } catch (InterruptedException e) {
84                      e.printStackTrace();
85                  }
86                  break;
87              }
88              case EXCEPTION: {
89                  throw new RuntimeException("You want exception, you get it");
90              }
91          }
92      }
93  
94      public void addStoreListener(StoreListener listener) {
95          alwaysCalledMethod();
96  
97      }
98  
99      public boolean bufferFull() {
100         return false;
101     }
102 
103     public boolean containsKey(Object key) {
104         alwaysCalledMethod();
105         return map.containsKey(key);
106     }
107 
108     public boolean containsKeyInMemory(Object key) {
109         alwaysCalledMethod();
110         return map.containsKey(key);
111     }
112 
113     public boolean containsKeyOffHeap(Object key) {
114         alwaysCalledMethod();
115         return false;
116     }
117 
118     public boolean containsKeyOnDisk(Object key) {
119         alwaysCalledMethod();
120         return false;
121     }
122 
123     public void dispose() {
124         alwaysCalledMethod();
125 
126     }
127 
128     public Results executeQuery(StoreQuery query) {
129         alwaysCalledMethod();
130         return null;
131     }
132 
133     public <T> Attribute<T> getSearchAttribute(String attributeName) {
134         alwaysCalledMethod();
135         return null;
136     }
137 
138     public void expireElements() {
139         alwaysCalledMethod();
140 
141     }
142 
143     public void flush() throws IOException {
144         alwaysCalledMethod();
145 
146     }
147 
148     public Element get(Object key) {
149         alwaysCalledMethod();
150         return map.get(key);
151     }
152 
153     public Policy getInMemoryEvictionPolicy() {
154         alwaysCalledMethod();
155         return null;
156     }
157 
158     public int getInMemorySize() {
159         alwaysCalledMethod();
160         return 0;
161     }
162 
163     public long getInMemorySizeInBytes() {
164         alwaysCalledMethod();
165         return 0;
166     }
167 
168     public Object getInternalContext() {
169         alwaysCalledMethod();
170         return cacheLockProvider;
171     }
172 
173     public List getKeys() {
174         alwaysCalledMethod();
175         return new ArrayList(map.keySet());
176     }
177 
178     public Object getMBean() {
179         alwaysCalledMethod();
180         return null;
181     }
182 
183     public int getOffHeapSize() {
184         alwaysCalledMethod();
185         return 0;
186     }
187 
188     public long getOffHeapSizeInBytes() {
189         alwaysCalledMethod();
190         return 0;
191     }
192 
193     public int getOnDiskSize() {
194         alwaysCalledMethod();
195         return 0;
196     }
197 
198     public long getOnDiskSizeInBytes() {
199         alwaysCalledMethod();
200         return 0;
201     }
202 
203     public Element getQuiet(Object key) {
204         alwaysCalledMethod();
205         return map.get(key);
206     }
207 
208     public int getSize() {
209         alwaysCalledMethod();
210         return 0;
211     }
212 
213     public Status getStatus() {
214         alwaysCalledMethod();
215         return null;
216     }
217 
218     public int getTerracottaClusteredSize() {
219         alwaysCalledMethod();
220         return 0;
221     }
222 
223     public boolean isCacheCoherent() {
224         alwaysCalledMethod();
225         return false;
226     }
227 
228     public boolean isClusterCoherent() {
229         alwaysCalledMethod();
230         return true;
231     }
232 
233     public boolean isNodeCoherent() {
234         alwaysCalledMethod();
235         return true;
236     }
237 
238     public boolean put(Element element) throws CacheException {
239         alwaysCalledMethod();
240         return map.put(element.getKey(), element) == null;
241     }
242 
243     public void putAll(Collection<Element> elements) throws CacheException {
244         alwaysCalledMethod();
245         for (Element element : elements) {
246             put(element);
247         }
248     }
249 
250     public Element putIfAbsent(Element element) throws NullPointerException {
251         alwaysCalledMethod();
252         return map.put(element.getKey(), element);
253     }
254 
255     public boolean putWithWriter(Element element, CacheWriterManager writerManager) throws CacheException {
256         alwaysCalledMethod();
257         return map.put(element.getKey(), element) == null;
258     }
259 
260     public Element remove(Object key) {
261         alwaysCalledMethod();
262         return map.remove(key);
263     }
264 
265     public void removeAll(Collection<Object> keys) {
266         alwaysCalledMethod();
267         for(Object key : keys) {
268             map.remove(key);
269         }
270     }
271 
272     public void removeAll() throws CacheException {
273         alwaysCalledMethod();
274         map.clear();
275     }
276 
277     public Element removeElement(Element element, ElementValueComparator comparator) throws NullPointerException {
278         alwaysCalledMethod();
279         return null;
280     }
281 
282     public void removeStoreListener(StoreListener listener) {
283         alwaysCalledMethod();
284 
285     }
286 
287     public Element removeWithWriter(Object key, CacheWriterManager writerManager) throws CacheException {
288         alwaysCalledMethod();
289         return null;
290     }
291 
292     public boolean replace(Element old, Element element, ElementValueComparator comparator) throws NullPointerException,
293             IllegalArgumentException {
294         alwaysCalledMethod();
295         return false;
296     }
297 
298     public Element replace(Element element) throws NullPointerException {
299         alwaysCalledMethod();
300         return null;
301     }
302 
303     public void setAttributeExtractors(Map<String, AttributeExtractor> extractors) {
304         alwaysCalledMethod();
305 
306     }
307 
308     public void setInMemoryEvictionPolicy(Policy policy) {
309         alwaysCalledMethod();
310 
311     }
312 
313     public void setNodeCoherent(boolean coherent) throws UnsupportedOperationException {
314         alwaysCalledMethod();
315 
316     }
317 
318     public void waitUntilClusterCoherent() throws UnsupportedOperationException {
319         alwaysCalledMethod();
320 
321     }
322 
323     public Set getLocalKeys() {
324         alwaysCalledMethod();
325         return null;
326     }
327 
328     public Element unlockedGet(Object key) {
329         alwaysCalledMethod();
330         return null;
331     }
332 
333     public Element unlockedGetQuiet(Object key) {
334         alwaysCalledMethod();
335         return null;
336     }
337 
338     public Element unsafeGet(Object key) {
339         alwaysCalledMethod();
340         return null;
341     }
342 
343     public Element unsafeGetQuiet(Object key) {
344         alwaysCalledMethod();
345         return null;
346     }
347 
348 }