View Javadoc
1   package org.apache.commons.jcs3.auxiliary.remote;
2   
3   import java.util.HashMap;
4   import java.util.HashSet;
5   import java.util.Map;
6   import java.util.Set;
7   
8   import org.apache.commons.jcs3.utils.timing.SleepUtil;
9   import org.apache.commons.jcs3.engine.CacheElement;
10  import org.apache.commons.jcs3.engine.CacheStatus;
11  import org.apache.commons.jcs3.engine.behavior.ICacheElement;
12  
13  /*
14   * Licensed to the Apache Software Foundation (ASF) under one
15   * or more contributor license agreements.  See the NOTICE file
16   * distributed with this work for additional information
17   * regarding copyright ownership.  The ASF licenses this file
18   * to you under the Apache License, Version 2.0 (the
19   * "License"); you may not use this file except in compliance
20   * with the License.  You may obtain a copy of the License at
21   *
22   *   http://www.apache.org/licenses/LICENSE-2.0
23   *
24   * Unless required by applicable law or agreed to in writing,
25   * software distributed under the License is distributed on an
26   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
27   * KIND, either express or implied.  See the License for the
28   * specific language governing permissions and limitations
29   * under the License.
30   */
31  
32  import junit.framework.TestCase;
33  
34  /**
35   * Unit tests for the remote cache no wait. The no wait manages a queue on top of the client.
36   */
37  public class RemoteCacheNoWaitUnitTest
38      extends TestCase
39  {
40      /**
41       * Simply verify that the client gets updated via the no wait.
42       * <p>
43       * @throws Exception
44       */
45      public void testUpdate()
46          throws Exception
47      {
48          // SETUP
49          final MockRemoteCacheClient<String, String> client = new MockRemoteCacheClient<>();
50          final RemoteCacheNoWait<String, String> noWait = new RemoteCacheNoWait<>( client );
51  
52          final ICacheElement<String, String> element = new CacheElement<>( "testUpdate", "key", "value" );
53  
54          // DO WORK
55          noWait.update( element );
56  
57          // VERIFY
58          SleepUtil.sleepAtLeast( 10 );
59  
60          assertEquals( "Wrong number updated.", 1, client.updateList.size() );
61          assertEquals( "Wrong element", element, client.updateList.get( 0 ) );
62      }
63  
64      /**
65       * Simply verify that the client get is called from the no wait.
66       * <p>
67       * @throws Exception
68       */
69      public void testGet()
70          throws Exception
71      {
72          // SETUP
73          final MockRemoteCacheClient<String, String> client = new MockRemoteCacheClient<>();
74          final RemoteCacheNoWait<String, String> noWait = new RemoteCacheNoWait<>( client );
75  
76          final ICacheElement<String, String> input = new CacheElement<>( "testUpdate", "key", "value" );
77          client.getSetupMap.put( "key", input );
78  
79          // DO WORK
80          final ICacheElement<String, String> result = noWait.get( "key" );
81  
82          // VERIFY
83          assertEquals( "Wrong element", input, result );
84      }
85  
86      /**
87       * Simply verify that the client getMultiple is called from the no wait.
88       * <p>
89       * @throws Exception
90       */
91      public void testGetMultiple()
92          throws Exception
93      {
94          // SETUP
95          final MockRemoteCacheClient<String, String> client = new MockRemoteCacheClient<>();
96          final RemoteCacheNoWait<String, String> noWait = new RemoteCacheNoWait<>( client );
97  
98          final ICacheElement<String, String> inputElement = new CacheElement<>( "testUpdate", "key", "value" );
99          final Map<String, ICacheElement<String, String>> inputMap = new HashMap<>();
100         inputMap.put( "key", inputElement );
101 
102         final Set<String> keys = new HashSet<>();
103         keys.add( "key" );
104 
105         client.getMultipleSetupMap.put( keys, inputMap );
106 
107         // DO WORK
108         final Map<String, ICacheElement<String, String>> result = noWait.getMultiple( keys );
109 
110         // VERIFY
111         assertEquals( "elements map", inputMap, result );
112     }
113 
114     /**
115      * Simply verify that the client gets updated via the no wait.
116      * <p>
117      * @throws Exception
118      */
119     public void testRemove()
120         throws Exception
121     {
122         // SETUP
123         final MockRemoteCacheClient<String, String> client = new MockRemoteCacheClient<>();
124         final RemoteCacheNoWait<String, String> noWait = new RemoteCacheNoWait<>( client );
125 
126         final String input = "MyKey";
127 
128         // DO WORK
129         noWait.remove( input );
130 
131         SleepUtil.sleepAtLeast( 10 );
132 
133         // VERIFY
134         assertEquals( "Wrong number updated.", 1, client.removeList.size() );
135         assertEquals( "Wrong key", input, client.removeList.get( 0 ) );
136     }
137 
138     /**
139      * Simply verify that the client status is returned in the stats.
140      * <p>
141      * @throws Exception
142      */
143     public void testGetStats()
144         throws Exception
145     {
146         // SETUP
147         final MockRemoteCacheClient<String, String> client = new MockRemoteCacheClient<>();
148         client.status = CacheStatus.ALIVE;
149         final RemoteCacheNoWait<String, String> noWait = new RemoteCacheNoWait<>( client );
150 
151         // DO WORK
152         final String result = noWait.getStats();
153 
154         // VERIFY
155         assertTrue( "Status should contain 'ALIVE'", result.indexOf( "ALIVE" ) != -1 );
156     }
157 
158     /**
159      * Simply verify that we get a status of error if the cache is in error..
160      * <p>
161      * @throws Exception
162      */
163     public void testGetStatus_error()
164         throws Exception
165     {
166         // SETUP
167         final MockRemoteCacheClient<String, String> client = new MockRemoteCacheClient<>();
168         client.status = CacheStatus.ERROR;
169         final RemoteCacheNoWait<String, String> noWait = new RemoteCacheNoWait<>( client );
170 
171         // DO WORK
172         final CacheStatus result = noWait.getStatus();
173 
174         // VERIFY
175         assertEquals( "Wrong status", CacheStatus.ERROR, result );
176     }
177 
178     /**
179      * Simply verify that the serviced supplied to fix is passed onto the client. Verify that the
180      * original event queue is destroyed. A new event queue willbe plugged in on fix.
181      * <p>
182      * @throws Exception
183      */
184     public void testFixCache()
185         throws Exception
186     {
187         // SETUP
188         final MockRemoteCacheClient<String, String> client = new MockRemoteCacheClient<>();
189         client.status = CacheStatus.ALIVE;
190         final RemoteCacheNoWait<String, String> noWait = new RemoteCacheNoWait<>( client );
191 
192         final MockRemoteCacheService<String, String> service = new MockRemoteCacheService<>();
193 
194         final ICacheElement<String, String> element = new CacheElement<>( "testUpdate", "key", "value" );
195 
196         // DO WORK
197         noWait.update( element );
198         SleepUtil.sleepAtLeast( 10 );
199         // ICacheEventQueue<String, String> originalQueue = noWait.getCacheEventQueue();
200 
201         noWait.fixCache( service );
202 
203         noWait.update( element );
204         SleepUtil.sleepAtLeast( 10 );
205 
206         // VERIFY
207         assertEquals( "Wrong status", service, client.fixed );
208     }
209 }