View Javadoc
1   package org.apache.commons.jcs3.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.jcs3.engine.behavior.ICacheElement;
31  import org.apache.commons.jcs3.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<>();
53  
54      /** List of request ids. */
55      public List<Long> updateRequestIdList = new ArrayList<>();
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( final String cacheName, final K key, final 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( final String cacheName )
82      {
83          return new HashSet<>();
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( final String cacheName, final K key, final long requesterId )
95      {
96          lastRemoveKey = key;
97      }
98  
99      /**
100      * Set the lastRemoveAllCacheName to the cacheName.
101      */
102     @Override
103     public void removeAll( final String cacheName, final 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( final ICacheElement<K, V> item, final 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( final String cacheName )
130     {
131     }
132 
133     /**
134      * @param cacheName
135      * @param key
136      * @return null
137      */
138     @Override
139     public ICacheElement<K, V> get( final String cacheName, final K key )
140     {
141         return get( cacheName, key, 0 );
142     }
143 
144     /**
145      * Do nothing.
146      */
147     @Override
148     public void release()
149     {
150     }
151 
152     /**
153      * Set the last remove key.
154      * <p>
155      * @param cacheName
156      * @param key
157      */
158     @Override
159     public void remove( final String cacheName, final K key )
160     {
161         lastRemoveKey = key;
162     }
163 
164     /**
165      * Set the last remove all cache name.
166      * <p>
167      * @param cacheName
168      */
169     @Override
170     public void removeAll( final String cacheName )
171     {
172         lastRemoveAllCacheName = cacheName;
173     }
174 
175     /**
176      * Set the last update item.
177      * <p>
178      * @param item
179      */
180     @Override
181     public void update( final ICacheElement<K, V> item )
182     {
183         lastUpdate = item;
184     }
185 
186     /**
187      * @param cacheName
188      * @param keys
189      * @param requesterId
190      * @return empty map
191      */
192     @Override
193     public Map<K, ICacheElement<K, V>> getMultiple( final String cacheName, final Set<K> keys, final long requesterId )
194     {
195         lastGetMultipleKeys = keys;
196         return new HashMap<>();
197     }
198 
199     /**
200      * @param cacheName
201      * @param keys
202      * @return empty map
203      */
204     @Override
205     public Map<K, ICacheElement<K, V>> getMultiple( final String cacheName, final Set<K> keys )
206     {
207         return getMultiple( cacheName, keys, 0 );
208     }
209 
210     /**
211      * Returns an empty map. Zombies have no internal data.
212      * <p>
213      * @param cacheName
214      * @param pattern
215      * @return an empty map
216      * @throws IOException
217      */
218     @Override
219     public Map<K, ICacheElement<K, V>> getMatching( final String cacheName, final String pattern )
220         throws IOException
221     {
222         return getMatching( cacheName, pattern, 0 );
223     }
224 
225     /**
226      * @param cacheName
227      * @param pattern
228      * @param requesterId
229      * @return Map
230      * @throws IOException
231      */
232     @Override
233     public Map<K, ICacheElement<K, V>> getMatching( final String cacheName, final String pattern, final long requesterId )
234         throws IOException
235     {
236         lastGetMatchingPattern = pattern;
237         return new HashMap<>();
238     }
239 }