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