View Javadoc
1   package org.apache.commons.jcs.auxiliary.remote;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.IOException;
23  import java.util.ArrayList;
24  import java.util.HashMap;
25  import java.util.HashSet;
26  import java.util.List;
27  import java.util.Map;
28  import java.util.Set;
29  
30  import org.apache.commons.jcs.engine.behavior.ICacheElement;
31  import org.apache.commons.jcs.engine.behavior.ICacheServiceNonLocal;
32  
33  /**
34   * This is a mock impl of the remote cache service.
35   */
36  public class MockRemoteCacheService<K, V>
37      implements ICacheServiceNonLocal<K, V>
38  {
39      /** The key last passed to get */
40      public K lastGetKey;
41  
42      /** The pattern last passed to get */
43      public String lastGetMatchingPattern;
44  
45      /** The keya last passed to getMatching */
46      public Set<K> lastGetMultipleKeys;
47  
48      /** The object that was last passed to update. */
49      public ICacheElement<K, V> lastUpdate;
50  
51      /** List of updates. */
52      public List<ICacheElement<K, V>> updateRequestList = new ArrayList<ICacheElement<K,V>>();
53  
54      /** List of request ids. */
55      public List<Long> updateRequestIdList = new ArrayList<Long>();
56  
57      /** The key that was last passed to remove. */
58      public K lastRemoveKey;
59  
60      /** The cache name that was last passed to removeAll. */
61      public String lastRemoveAllCacheName;
62  
63      /**
64       * @param cacheName
65       * @param key
66       * @param requesterId
67       * @return null
68       */
69      @Override
70      public ICacheElement<K, V> get( String cacheName, K key, long requesterId )
71      {
72          lastGetKey = key;
73          return null;
74      }
75  
76      /**
77       * @param cacheName
78       * @return empty set
79       */
80      @Override
81      public Set<K> getKeySet( String cacheName )
82      {
83          return new HashSet<K>();
84      }
85  
86      /**
87       * Set the last remove key.
88       * <p>
89       * @param cacheName
90       * @param key
91       * @param requesterId
92       */
93      @Override
94      public void remove( String cacheName, K key, long requesterId )
95      {
96          lastRemoveKey = key;
97      }
98  
99      /**
100      * Set the lastRemoveAllCacheName to the cacheName.
101      */
102     @Override
103     public void removeAll( String cacheName, long requesterId )
104         throws IOException
105     {
106         lastRemoveAllCacheName = cacheName;
107     }
108 
109     /**
110      * Set the last update item.
111      * <p>
112      * @param item
113      * @param requesterId
114      */
115     @Override
116     public void update( ICacheElement<K, V> item, long requesterId )
117     {
118         lastUpdate = item;
119         updateRequestList.add( item );
120         updateRequestIdList.add( Long.valueOf( requesterId ) );
121     }
122 
123     /**
124      * Do nothing.
125      * <p>
126      * @param cacheName
127      */
128     @Override
129     public void dispose( String cacheName )
130     {
131         return;
132     }
133 
134     /**
135      * @param cacheName
136      * @param key
137      * @return null
138      */
139     @Override
140     public ICacheElement<K, V> get( String cacheName, K key )
141     {
142         return get( cacheName, key, 0 );
143     }
144 
145     /**
146      * Do nothing.
147      */
148     @Override
149     public void release()
150     {
151         return;
152     }
153 
154     /**
155      * Set the last remove key.
156      * <p>
157      * @param cacheName
158      * @param key
159      */
160     @Override
161     public void remove( String cacheName, K key )
162     {
163         lastRemoveKey = key;
164     }
165 
166     /**
167      * Set the last remove all cache name.
168      * <p>
169      * @param cacheName
170      */
171     @Override
172     public void removeAll( String cacheName )
173     {
174         lastRemoveAllCacheName = cacheName;
175     }
176 
177     /**
178      * Set the last update item.
179      * <p>
180      * @param item
181      */
182     @Override
183     public void update( ICacheElement<K, V> item )
184     {
185         lastUpdate = item;
186     }
187 
188     /**
189      * @param cacheName
190      * @param keys
191      * @param requesterId
192      * @return empty map
193      */
194     @Override
195     public Map<K, ICacheElement<K, V>> getMultiple( String cacheName, Set<K> keys, long requesterId )
196     {
197         lastGetMultipleKeys = keys;
198         return new HashMap<K, ICacheElement<K, V>>();
199     }
200 
201     /**
202      * @param cacheName
203      * @param keys
204      * @return empty map
205      */
206     @Override
207     public Map<K, ICacheElement<K, V>> getMultiple( String cacheName, Set<K> keys )
208     {
209         return getMultiple( cacheName, keys, 0 );
210     }
211 
212     /**
213      * Returns an empty map. Zombies have no internal data.
214      * <p>
215      * @param cacheName
216      * @param pattern
217      * @return an empty map
218      * @throws IOException
219      */
220     @Override
221     public Map<K, ICacheElement<K, V>> getMatching( String cacheName, String pattern )
222         throws IOException
223     {
224         return getMatching( cacheName, pattern, 0 );
225     }
226 
227     /**
228      * @param cacheName
229      * @param pattern
230      * @param requesterId
231      * @return Map
232      * @throws IOException
233      */
234     @Override
235     public Map<K, ICacheElement<K, V>> getMatching( String cacheName, String pattern, long requesterId )
236         throws IOException
237     {
238         lastGetMatchingPattern = pattern;
239         return new HashMap<K, ICacheElement<K, V>>();
240     }
241 }