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.HashMap;
24  import java.util.LinkedList;
25  import java.util.List;
26  import java.util.Map;
27  import java.util.Set;
28  
29  import org.apache.commons.jcs3.auxiliary.AbstractAuxiliaryCache;
30  import org.apache.commons.jcs3.auxiliary.AuxiliaryCacheAttributes;
31  import org.apache.commons.jcs3.auxiliary.remote.behavior.IRemoteCacheClient;
32  import org.apache.commons.jcs3.auxiliary.remote.behavior.IRemoteCacheListener;
33  import org.apache.commons.jcs3.engine.CacheStatus;
34  import org.apache.commons.jcs3.engine.behavior.ICacheElement;
35  import org.apache.commons.jcs3.engine.behavior.ICacheServiceNonLocal;
36  import org.apache.commons.jcs3.engine.stats.behavior.IStats;
37  import org.apache.commons.jcs3.log.Log;
38  import org.apache.commons.jcs3.log.LogManager;
39  
40  /**
41   * Used for testing the no wait.
42   */
43  public class MockRemoteCacheClient<K, V>
44      extends AbstractAuxiliaryCache<K, V>
45      implements IRemoteCacheClient<K, V>
46  {
47      /** log instance */
48      private static final Log log = LogManager.getLog( MockRemoteCacheClient.class );
49  
50      /** List of ICacheElement&lt;K, V&gt; objects passed into update. */
51      public List<ICacheElement<K, V>> updateList = new LinkedList<>();
52  
53      /** List of key objects passed into remove. */
54      public List<K> removeList = new LinkedList<>();
55  
56      /** status to return. */
57      public CacheStatus status = CacheStatus.ALIVE;
58  
59      /** Can setup values to return from get. values must be ICacheElement&lt;K, V&gt; */
60      public Map<K, ICacheElement<K, V>> getSetupMap = new HashMap<>();
61  
62      /** Can setup values to return from get. values must be Map&lt;K, ICacheElement&lt;K, V&gt;&gt; */
63      public Map<Set<K>, Map<K, ICacheElement<K, V>>> getMultipleSetupMap =
64          new HashMap<>();
65  
66      /** The last service passed to fixCache */
67      public ICacheServiceNonLocal<K, V> fixed;
68  
69      /** Attributes. */
70      public RemoteCacheAttributes attributes = new RemoteCacheAttributes();
71  
72      /**
73       * Stores the last argument as fixed.
74       */
75      @Override
76      @SuppressWarnings("unchecked") // Don't know how to do this properly
77      public void fixCache( final ICacheServiceNonLocal<?, ?> remote )
78      {
79          fixed = (ICacheServiceNonLocal<K, V>)remote;
80      }
81  
82      /**
83       * @return long
84       */
85      @Override
86      public long getListenerId()
87      {
88          return 0;
89      }
90  
91      /**
92       * @return null
93       */
94      @Override
95      public IRemoteCacheListener<K, V> getListener()
96      {
97          return null;
98      }
99  
100     /**
101      * Adds the argument to the updatedList.
102      */
103     @Override
104     public void update( final ICacheElement<K, V> ce )
105     {
106         updateList.add( ce );
107     }
108 
109     /**
110      * Looks in the getSetupMap for a value.
111      */
112     @Override
113     public ICacheElement<K, V> get( final K key )
114     {
115         log.info( "get [" + key + "]" );
116         return getSetupMap.get( key );
117     }
118 
119     /**
120      * Gets multiple items from the cache based on the given set of keys.
121      * <p>
122      * @param keys
123      * @return a map of K key to ICacheElement&lt;K, V&gt; element, or an empty map if there is no
124      *         data in cache for any of these keys
125      */
126     @Override
127     public Map<K, ICacheElement<K, V>> getMultiple(final Set<K> keys)
128     {
129         log.info( "get [" + keys + "]" );
130         return getMultipleSetupMap.get( keys );
131     }
132 
133     /**
134      * Adds the key to the remove list.
135      */
136     @Override
137     public boolean remove( final K key )
138     {
139         removeList.add( key );
140         return false;
141     }
142 
143     /**
144      * Removes all cached items from the cache.
145      */
146     @Override
147     public void removeAll()
148     {
149         // do nothing
150     }
151 
152     /**
153      * Prepares for shutdown.
154      */
155     @Override
156     public void dispose()
157     {
158         // do nothing
159     }
160 
161     /**
162      * Returns the current cache size in number of elements.
163      * <p>
164      * @return number of elements
165      */
166     @Override
167     public int getSize()
168     {
169         return 0;
170     }
171 
172     /**
173      * Returns the status setup variable.
174      */
175     @Override
176     public CacheStatus getStatus()
177     {
178         return status;
179     }
180 
181     /**
182      * Returns the cache name.
183      * <p>
184      * @return usually the region name.
185      */
186     @Override
187     public String getCacheName()
188     {
189         return null;
190     }
191 
192     /**
193      * @return null
194      */
195     @Override
196     public Set<K> getKeySet( )
197     {
198         return null;
199     }
200 
201     /**
202      * @return null
203      */
204     @Override
205     public IStats getStatistics()
206     {
207         return null;
208     }
209 
210     /**
211      * Returns the setup attributes. By default they are not null.
212      */
213     @Override
214     public AuxiliaryCacheAttributes getAuxiliaryCacheAttributes()
215     {
216         return attributes;
217     }
218 
219     /**
220      * Returns the cache stats.
221      * <p>
222      * @return String of important historical information.
223      */
224     @Override
225     public String getStats()
226     {
227         return null;
228     }
229 
230     /** @return 0 */
231     @Override
232     public CacheType getCacheType()
233     {
234         return CacheType.REMOTE_CACHE;
235     }
236 
237     /**
238      * @param pattern
239      * @return Map
240      * @throws IOException
241      */
242     @Override
243     public Map<K, ICacheElement<K, V>> getMatching(final String pattern)
244         throws IOException
245     {
246         return new HashMap<>();
247     }
248 
249     /**
250      * Nothing important
251      * <p>
252      * @return null
253      */
254     @Override
255     public String getEventLoggingExtraInfo()
256     {
257         return null;
258     }
259 }