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.util.HashSet;
23  import java.util.Map;
24  
25  import junit.framework.TestCase;
26  
27  import org.apache.commons.jcs.auxiliary.MockCacheEventLogger;
28  import org.apache.commons.jcs.auxiliary.remote.behavior.IRemoteCacheAttributes;
29  import org.apache.commons.jcs.engine.CacheElement;
30  import org.apache.commons.jcs.engine.ZombieCacheServiceNonLocal;
31  import org.apache.commons.jcs.engine.behavior.ICacheElement;
32  import org.apache.commons.jcs.engine.behavior.ICacheElementSerialized;
33  import org.apache.commons.jcs.utils.serialization.SerializationConversionUtil;
34  
35  /**
36   * Unit Tests for the Remote Cache.
37   */
38  public class RemoteCacheUnitTest
39      extends TestCase
40  {
41      private IRemoteCacheAttributes cattr;
42      private MockRemoteCacheService<String, String> service;
43      private MockRemoteCacheListener<String, String> listener;
44      private RemoteCacheMonitor monitor;
45  
46      /**
47       * @see junit.framework.TestCase#setUp()
48       */
49      @Override
50      protected void setUp() throws Exception
51      {
52          super.setUp();
53          cattr = new RemoteCacheAttributes();
54          service = new MockRemoteCacheService<String, String>();
55          listener = new MockRemoteCacheListener<String, String>();
56          monitor = new RemoteCacheMonitor();
57      }
58  
59      /**
60       * Verify that the remote service update method is called. The remote cache serializes the object
61       * first.
62       * <p>
63       * @throws Exception
64       */
65      public void testUpdate()
66          throws Exception
67      {
68          // SETUP
69          long listenerId = 123;
70          listener.setListenerId( listenerId );
71  
72          RemoteCache<String, String> remoteCache = new RemoteCache<String, String>( cattr, service, listener, monitor );
73  
74          String cacheName = "testUpdate";
75  
76          // DO WORK
77          ICacheElement<String, String> element = new CacheElement<String, String>( cacheName, "key", "value" );
78          remoteCache.update( element );
79  
80          // VERIFY
81          assertTrue( "The element should be in the serialized wrapper.",
82                      service.lastUpdate instanceof ICacheElementSerialized );
83          ICacheElement<String, String> result = SerializationConversionUtil
84              .getDeSerializedCacheElement( (ICacheElementSerialized<String, String>) service.lastUpdate, remoteCache
85                  .getElementSerializer() );
86          assertEquals( "Wrong element updated.", element.getVal(), result.getVal() );
87          assertEquals( "Wrong listener id.", Long.valueOf( listenerId ), service.updateRequestIdList.get( 0 ) );
88      }
89  
90      /**
91       * Verify that when we call fix events queued in the zombie are propagated to the new service.
92       * <p>
93       * @throws Exception
94       */
95      public void testUpdateZombieThenFix()
96          throws Exception
97      {
98          // SETUP
99          ZombieCacheServiceNonLocal<String, String> zombie = new ZombieCacheServiceNonLocal<String, String>( 10 );
100 
101         // set the zombie
102         RemoteCache<String, String> remoteCache = new RemoteCache<String, String>( cattr, zombie, listener, monitor );
103 
104         String cacheName = "testUpdate";
105 
106         // DO WORK
107         ICacheElement<String, String> element = new CacheElement<String, String>( cacheName, "key", "value" );
108         remoteCache.update( element );
109         // set the new service, this should call propagate
110         remoteCache.fixCache( service );
111 
112         // VERIFY
113         assertTrue( "The element should be in the serialized warapper.",
114                     service.lastUpdate instanceof ICacheElementSerialized );
115         ICacheElement<String, String> result = SerializationConversionUtil
116             .getDeSerializedCacheElement( (ICacheElementSerialized<String, String>) service.lastUpdate, remoteCache
117                 .getElementSerializer() );
118         assertEquals( "Wrong element updated.", element.getVal(), result.getVal() );
119     }
120 
121     /**
122      * Verify event log calls.
123      * <p>
124      * @throws Exception
125      */
126     public void testUpdate_simple()
127         throws Exception
128     {
129         RemoteCache<String, String> remoteCache = new RemoteCache<String, String>( cattr, service, listener, monitor );
130 
131         MockCacheEventLogger cacheEventLogger = new MockCacheEventLogger();
132         remoteCache.setCacheEventLogger( cacheEventLogger );
133 
134         ICacheElement<String, String> item = new CacheElement<String, String>( "region", "key", "value" );
135 
136         // DO WORK
137         remoteCache.update( item );
138 
139         // VERIFY
140         assertEquals( "Start should have been called.", 1, cacheEventLogger.startICacheEventCalls );
141         assertEquals( "End should have been called.", 1, cacheEventLogger.endICacheEventCalls );
142     }
143 
144     /**
145      * Verify event log calls.
146      * <p>
147      * @throws Exception
148      */
149     public void testGet_simple()
150         throws Exception
151     {
152         RemoteCache<String, String> remoteCache = new RemoteCache<String, String>( cattr, service, listener, monitor );
153 
154         MockCacheEventLogger cacheEventLogger = new MockCacheEventLogger();
155         remoteCache.setCacheEventLogger( cacheEventLogger );
156 
157         // DO WORK
158         remoteCache.get( "key" );
159 
160         // VERIFY
161         assertEquals( "Start should have been called.", 1, cacheEventLogger.startICacheEventCalls );
162         assertEquals( "End should have been called.", 1, cacheEventLogger.endICacheEventCalls );
163     }
164 
165     /**
166      * Verify event log calls.
167      * <p>
168      * @throws Exception
169      */
170     public void testGetMultiple_simple()
171         throws Exception
172     {
173         RemoteCache<String, String> remoteCache = new RemoteCache<String, String>( cattr, service, listener, monitor );
174 
175         MockCacheEventLogger cacheEventLogger = new MockCacheEventLogger();
176         remoteCache.setCacheEventLogger( cacheEventLogger );
177 
178         // DO WORK
179         remoteCache.getMultiple( new HashSet<String>() );
180 
181         // VERIFY
182         assertEquals( "Start should have been called.", 1, cacheEventLogger.startICacheEventCalls );
183         assertEquals( "End should have been called.", 1, cacheEventLogger.endICacheEventCalls );
184     }
185 
186     /**
187      * Verify event log calls.
188      * <p>
189      * @throws Exception
190      */
191     public void testRemove_simple()
192         throws Exception
193     {
194         RemoteCache<String, String> remoteCache = new RemoteCache<String, String>( cattr, service, listener, monitor );
195 
196         MockCacheEventLogger cacheEventLogger = new MockCacheEventLogger();
197         remoteCache.setCacheEventLogger( cacheEventLogger );
198 
199         // DO WORK
200         remoteCache.remove( "key" );
201 
202         // VERIFY
203         assertEquals( "Start should have been called.", 1, cacheEventLogger.startICacheEventCalls );
204         assertEquals( "End should have been called.", 1, cacheEventLogger.endICacheEventCalls );
205     }
206 
207     /**
208      * Verify event log calls.
209      * <p>
210      * @throws Exception
211      */
212     public void testRemoveAll_simple()
213         throws Exception
214     {
215         RemoteCache<String, String> remoteCache = new RemoteCache<String, String>( cattr, service, listener, monitor );
216 
217         MockCacheEventLogger cacheEventLogger = new MockCacheEventLogger();
218         remoteCache.setCacheEventLogger( cacheEventLogger );
219 
220         // DO WORK
221         remoteCache.remove( "key" );
222 
223         // VERIFY
224         assertEquals( "Start should have been called.", 1, cacheEventLogger.startICacheEventCalls );
225         assertEquals( "End should have been called.", 1, cacheEventLogger.endICacheEventCalls );
226     }
227 
228     /**
229      * Verify event log calls.
230      * <p>
231      * @throws Exception
232      */
233     public void testGetMatching_simple()
234         throws Exception
235     {
236         // SETUP
237         String pattern = "adsfasdfasd.?";
238 
239         RemoteCache<String, String> remoteCache = new RemoteCache<String, String>( cattr, service, listener, monitor );
240 
241         MockCacheEventLogger cacheEventLogger = new MockCacheEventLogger();
242         remoteCache.setCacheEventLogger( cacheEventLogger );
243 
244         // DO WORK
245         Map<String, ICacheElement<String, String>> result = remoteCache.getMatching( pattern );
246 
247         // VERIFY
248         assertNotNull( "Should have a map", result );
249         assertEquals( "Start should have been called.", 1, cacheEventLogger.startICacheEventCalls );
250         assertEquals( "End should have been called.", 1, cacheEventLogger.endICacheEventCalls );
251     }
252 
253     /**
254      * Verify event log calls.
255      * <p>
256      * @throws Exception
257      */
258     public void testDispose_simple()
259         throws Exception
260     {
261         RemoteCache<String, String> remoteCache = new RemoteCache<String, String>( cattr, service, listener, monitor );
262 
263         MockCacheEventLogger cacheEventLogger = new MockCacheEventLogger();
264         remoteCache.setCacheEventLogger( cacheEventLogger );
265 
266         // DO WORK
267         remoteCache.dispose( );
268 
269         // VERIFY
270         assertEquals( "Start should have been called.", 1, cacheEventLogger.startICacheEventCalls );
271         assertEquals( "End should have been called.", 1, cacheEventLogger.endICacheEventCalls );
272     }
273 
274     /**
275      * Verify that there is no problem if there is no listener.
276      * <p>
277      * @throws Exception
278      */
279     public void testDispose_nullListener()
280         throws Exception
281     {
282         // SETUP
283         RemoteCache<String, String> remoteCache = new RemoteCache<String, String>( cattr, service, null, monitor );
284 
285         MockCacheEventLogger cacheEventLogger = new MockCacheEventLogger();
286         remoteCache.setCacheEventLogger( cacheEventLogger );
287 
288         // DO WORK
289         remoteCache.dispose( );
290 
291         // VERIFY
292         assertEquals( "Start should have been called.", 1, cacheEventLogger.startICacheEventCalls );
293         assertEquals( "End should have been called.", 1, cacheEventLogger.endICacheEventCalls );
294     }
295 }