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 junit.framework.TestCase;
23  import org.apache.commons.jcs.engine.CacheElement;
24  import org.apache.commons.jcs.engine.CacheStatus;
25  import org.apache.commons.jcs.engine.behavior.ICacheElement;
26  import org.apache.commons.jcs.engine.behavior.ICacheEventQueue;
27  import org.apache.commons.jcs.utils.timing.SleepUtil;
28  
29  import java.util.HashMap;
30  import java.util.HashSet;
31  import java.util.Map;
32  import java.util.Set;
33  
34  /**
35   * Unit tests for the remote cache no wait. The no wait manages a queue on top of the client.
36   * <p>
37   * @author Aaron Smuts
38   */
39  public class RemoteCacheNoWaitUnitTest
40      extends TestCase
41  {
42      /**
43       * Simply verify that the client gets updated via the no wait.
44       * <p>
45       * @throws Exception
46       */
47      public void testUpdate()
48          throws Exception
49      {
50          // SETUP
51          MockRemoteCacheClient<String, String> client = new MockRemoteCacheClient<String, String>();
52          RemoteCacheNoWait<String, String> noWait = new RemoteCacheNoWait<String, String>( client );
53  
54          ICacheElement<String, String> element = new CacheElement<String, String>( "testUpdate", "key", "value" );
55  
56          // DO WORK
57          noWait.update( element );
58  
59          // VERIFY
60          SleepUtil.sleepAtLeast( 10 );
61  
62          assertEquals( "Wrong number updated.", 1, client.updateList.size() );
63          assertEquals( "Wrong element", element, client.updateList.get( 0 ) );
64      }
65  
66      /**
67       * Simply verify that the client get is called from the no wait.
68       * <p>
69       * @throws Exception
70       */
71      public void testGet()
72          throws Exception
73      {
74          // SETUP
75          MockRemoteCacheClient<String, String> client = new MockRemoteCacheClient<String, String>();
76          RemoteCacheNoWait<String, String> noWait = new RemoteCacheNoWait<String, String>( client );
77  
78          ICacheElement<String, String> input = new CacheElement<String, String>( "testUpdate", "key", "value" );
79          client.getSetupMap.put( "key", input );
80  
81          // DO WORK
82          ICacheElement<String, String> result = noWait.get( "key" );
83  
84          // VERIFY
85          assertEquals( "Wrong element", input, result );
86      }
87  
88      /**
89       * Simply verify that the client getMultiple is called from the no wait.
90       * <p>
91       * @throws Exception
92       */
93      public void testGetMultiple()
94          throws Exception
95      {
96          // SETUP
97          MockRemoteCacheClient<String, String> client = new MockRemoteCacheClient<String, String>();
98          RemoteCacheNoWait<String, String> noWait = new RemoteCacheNoWait<String, String>( client );
99  
100         ICacheElement<String, String> inputElement = new CacheElement<String, String>( "testUpdate", "key", "value" );
101         Map<String, ICacheElement<String, String>> inputMap = new HashMap<String, ICacheElement<String,String>>();
102         inputMap.put( "key", inputElement );
103 
104         Set<String> keys = new HashSet<String>();
105         keys.add( "key" );
106 
107         client.getMultipleSetupMap.put( keys, inputMap );
108 
109         // DO WORK
110         Map<String, ICacheElement<String, String>> result = noWait.getMultiple( keys );
111 
112         // VERIFY
113         assertEquals( "elements map", inputMap, result );
114     }
115 
116     /**
117      * Simply verify that the client gets updated via the no wait.
118      * <p>
119      * @throws Exception
120      */
121     public void testRemove()
122         throws Exception
123     {
124         // SETUP
125         MockRemoteCacheClient<String, String> client = new MockRemoteCacheClient<String, String>();
126         RemoteCacheNoWait<String, String> noWait = new RemoteCacheNoWait<String, String>( client );
127 
128         String input = "MyKey";
129 
130         // DO WORK
131         noWait.remove( input );
132 
133         SleepUtil.sleepAtLeast( 10 );
134 
135         // VERIFY
136         assertEquals( "Wrong number updated.", 1, client.removeList.size() );
137         assertEquals( "Wrong key", input, client.removeList.get( 0 ) );
138     }
139 
140     /**
141      * Simply verify that the client status is returned in the stats.
142      * <p>
143      * @throws Exception
144      */
145     public void testGetStats()
146         throws Exception
147     {
148         // SETUP
149         MockRemoteCacheClient<String, String> client = new MockRemoteCacheClient<String, String>();
150         client.status = CacheStatus.ALIVE;
151         RemoteCacheNoWait<String, String> noWait = new RemoteCacheNoWait<String, String>( client );
152 
153         // DO WORK
154         String result = noWait.getStats();
155 
156         // VERIFY
157         assertTrue( "Status should contain 'ALIVE'", result.indexOf( "ALIVE" ) != -1 );
158     }
159 
160     /**
161      * Simply verify that we get a status of error if the cache is in error..
162      * <p>
163      * @throws Exception
164      */
165     public void testGetStatus_error()
166         throws Exception
167     {
168         // SETUP
169         MockRemoteCacheClient<String, String> client = new MockRemoteCacheClient<String, String>();
170         client.status = CacheStatus.ERROR;
171         RemoteCacheNoWait<String, String> noWait = new RemoteCacheNoWait<String, String>( client );
172 
173         // DO WORK
174         CacheStatus result = noWait.getStatus();
175 
176         // VERIFY
177         assertEquals( "Wrong status", CacheStatus.ERROR, result );
178     }
179 
180     /**
181      * Simply verify that the serviced supplied to fix is passed onto the client. Verify that the
182      * original event queue is destroyed. A new event queue willbe plugged in on fix.
183      * <p>
184      * @throws Exception
185      */
186     public void testFixCache()
187         throws Exception
188     {
189         // SETUP
190         MockRemoteCacheClient<String, String> client = new MockRemoteCacheClient<String, String>();
191         client.status = CacheStatus.ALIVE;
192         RemoteCacheNoWait<String, String> noWait = new RemoteCacheNoWait<String, String>( client );
193 
194         MockRemoteCacheService<String, String> service = new MockRemoteCacheService<String, String>();
195 
196         ICacheElement<String, String> element = new CacheElement<String, String>( "testUpdate", "key", "value" );
197 
198         // DO WORK
199         noWait.update( element );
200         SleepUtil.sleepAtLeast( 10 );
201         ICacheEventQueue<String, String> originalQueue = noWait.getCacheEventQueue();
202 
203         noWait.fixCache( service );
204 
205         noWait.update( element );
206         SleepUtil.sleepAtLeast( 10 );
207         ICacheEventQueue<String, String> newQueue = noWait.getCacheEventQueue();
208 
209         // VERIFY
210         assertEquals( "Wrong status", service, client.fixed );
211         assertFalse( "Original queue should not alive", originalQueue.isAlive() );
212         assertTrue( "New queue should be alive." + newQueue, newQueue.isAlive() );
213     }
214 }