1   package org.apache.jcs.auxiliary.remote.server;
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.LinkedList;
23  import java.util.List;
24  
25  import junit.framework.TestCase;
26  
27  import org.apache.jcs.auxiliary.remote.RemoteCacheListenerMockImpl;
28  import org.apache.jcs.auxiliary.remote.behavior.IRemoteCacheAttributes;
29  import org.apache.jcs.auxiliary.remote.server.behavior.IRemoteCacheServerAttributes;
30  import org.apache.jcs.engine.CacheElement;
31  import org.apache.jcs.engine.behavior.ICacheElement;
32  
33  /***
34   * Since the server does not know that it is a server, it is easy to unit test. The factory does all
35   * the rmi work.
36   * <p>
37   * @author Aaron Smuts
38   */
39  public class RemoteCacheServerUnitTest
40      extends TestCase
41  {
42  
43      /***
44       * Add a listner. Pass the id of 0, verify that the server sets a new listener id. Do another
45       * and verify that the second gets an id of 2.
46       * <p>
47       * @throws Exception
48       */
49      public void testAddListenerToCache()
50          throws Exception
51      {
52          // SETUP
53          IRemoteCacheServerAttributes rcsa = new RemoteCacheServerAttributes();
54          rcsa.setConfigFileName( "/TestRemoteCacheServer.ccf" );
55          RemoteCacheServer server = new RemoteCacheServer( rcsa );
56  
57          RemoteCacheListenerMockImpl mockListener1 = new RemoteCacheListenerMockImpl();
58          RemoteCacheListenerMockImpl mockListener2 = new RemoteCacheListenerMockImpl();
59  
60          String cacheName = "testAddListener";
61  
62          // DO WORK
63          server.addCacheListener( cacheName, mockListener1 );
64          server.addCacheListener( cacheName, mockListener2 );
65  
66          // VERIFY
67          assertEquals( "Wrong listener id.", 1, mockListener1.getListenerId() );
68          assertEquals( "Wrong listener id.", 2, mockListener2.getListenerId() );
69      }
70  
71      /***
72       * Add a listner. Pass the id of 0, verify that the server sets a new listener id. Do another
73       * and verify that the second gets an id of 2.
74       * <p>
75       * @throws Exception
76       */
77      public void testAddListenerToAll()
78          throws Exception
79      {
80          // SETUP
81          IRemoteCacheServerAttributes rcsa = new RemoteCacheServerAttributes();
82          rcsa.setConfigFileName( "/TestRemoteCacheServer.ccf" );
83          RemoteCacheServer server = new RemoteCacheServer( rcsa );
84  
85          RemoteCacheListenerMockImpl mockListener1 = new RemoteCacheListenerMockImpl();
86          RemoteCacheListenerMockImpl mockListener2 = new RemoteCacheListenerMockImpl();
87  
88          // DO WORK
89          // don't specify the cache name
90          server.addCacheListener( mockListener1 );
91          server.addCacheListener( mockListener2 );
92  
93          // VERIFY
94          assertEquals( "Wrong listener id.", 1, mockListener1.getListenerId() );
95          assertEquals( "Wrong listener id.", 2, mockListener2.getListenerId() );
96      }
97  
98      /***
99       * Add a listner. Pass the id of 0, verify that the server sets a new listener id. Do another
100      * and verify that the second gets an id of 2. Call remove Listener and verify that it is
101      * removed.
102      * <p>
103      * @throws Exception
104      */
105     public void testAddListenerToAllThenRemove()
106         throws Exception
107     {
108         // SETUP
109         IRemoteCacheServerAttributes rcsa = new RemoteCacheServerAttributes();
110         rcsa.setConfigFileName( "/TestRemoteCacheServer.ccf" );
111         RemoteCacheServer server = new RemoteCacheServer( rcsa );
112 
113         RemoteCacheListenerMockImpl mockListener1 = new RemoteCacheListenerMockImpl();
114         RemoteCacheListenerMockImpl mockListener2 = new RemoteCacheListenerMockImpl();
115 
116         String cacheName = "testAddListenerToAllThenRemove";
117 
118         // DO WORK
119         server.addCacheListener( cacheName, mockListener1 );
120         server.addCacheListener( cacheName, mockListener2 );
121 
122         // VERIFY
123         assertEquals( "Wrong number of listeners.", 2, server.getCacheListeners( cacheName ).eventQMap.size() );
124         assertEquals( "Wrong listener id.", 1, mockListener1.getListenerId() );
125         assertEquals( "Wrong listener id.", 2, mockListener2.getListenerId() );
126 
127         // DO WORK
128         server.removeCacheListener( cacheName, mockListener1.getListenerId() );
129         assertEquals( "Wrong number of listeners.", 1, server.getCacheListeners( cacheName ).eventQMap.size() );
130     }
131 
132     /***
133      * Add a listner. Pass the id of 0, verify that the server sets a new listener id. Do another
134      * and verify that the second gets an id of 2. Call remove Listener and verify that it is
135      * removed.
136      * <p>
137      * @throws Exception
138      */
139     public void testAddListenerToAllThenRemove_clusterType()
140         throws Exception
141     {
142         // SETUP
143         IRemoteCacheServerAttributes rcsa = new RemoteCacheServerAttributes();
144         rcsa.setConfigFileName( "/TestRemoteCacheServer.ccf" );
145         RemoteCacheServer server = new RemoteCacheServer( rcsa );
146 
147         RemoteCacheListenerMockImpl mockListener1 = new RemoteCacheListenerMockImpl();
148         mockListener1.remoteType = IRemoteCacheServerAttributes.CLUSTER;
149         RemoteCacheListenerMockImpl mockListener2 = new RemoteCacheListenerMockImpl();
150         mockListener2.remoteType = IRemoteCacheServerAttributes.CLUSTER;
151 
152         String cacheName = "testAddListenerToAllThenRemove";
153 
154         // DO WORK
155         server.addCacheListener( cacheName, mockListener1 );
156         server.addCacheListener( cacheName, mockListener2 );
157 
158         // VERIFY
159         assertEquals( "Wrong number of listeners.", 0, server.getCacheListeners( cacheName ).eventQMap.size() );
160         assertEquals( "Wrong number of listeners.", 2, server.getClusterListeners( cacheName ).eventQMap.size() );
161         assertEquals( "Wrong listener id.", 1, mockListener1.getListenerId() );
162         assertEquals( "Wrong listener id.", 2, mockListener2.getListenerId() );
163 
164         // DO WORK
165         server.removeCacheListener( cacheName, mockListener1.getListenerId() );
166         assertEquals( "Wrong number of listeners.", 1, server.getClusterListeners( cacheName ).eventQMap.size() );
167     }
168 
169     /***
170      * Register a listener and then verify that it is called when we put using a different listener
171      * id.
172      * @throws Exception
173      */
174     public void testSimpleRegisterListenerAndPut()
175         throws Exception
176     {
177         // SETUP
178         IRemoteCacheServerAttributes rcsa = new RemoteCacheServerAttributes();
179         rcsa.setConfigFileName( "/TestRemoteCacheServer.ccf" );
180 
181         RemoteCacheListenerMockImpl mockListener = new RemoteCacheListenerMockImpl();
182         RemoteCacheServer server = new RemoteCacheServer( rcsa );
183 
184         String cacheName = "testSimpleRegisterListenerAndPut";
185         server.addCacheListener( cacheName, mockListener );
186 
187         // DO WORK
188         List inputItems = new LinkedList();
189         int numToPut = 10;
190 
191         for ( int i = 0; i < numToPut; i++ )
192         {
193             ICacheElement element = new CacheElement( cacheName, String.valueOf( i ), new Long( i ) );
194             inputItems.add( element );
195             server.update( element, 9999 );
196         }
197 
198         Thread.sleep( 100 );
199         Thread.yield();
200         Thread.sleep( 100 );
201 
202         // VERIFY
203         assertEquals( "Wrong number of items put to listener.", numToPut, mockListener.putItems.size() );
204         for ( int i = 0; i < numToPut; i++ )
205         {
206             assertEquals( "Wrong item.", inputItems.get( i ), mockListener.putItems.get( i ) );
207         }
208     }
209 
210     /***
211      * Register a listener and then verify that it is called when we put using a different listener
212      * id. The updates should come from a cluster listener and local cluster consistency should be
213      * true.
214      * <p>
215      * @throws Exception
216      */
217     public void testSimpleRegisterListenerAndPut_FromClusterWithLCC()
218         throws Exception
219     {
220         // SETUP
221         IRemoteCacheServerAttributes rcsa = new RemoteCacheServerAttributes();
222         rcsa.setLocalClusterConsistency( true );
223         rcsa.setConfigFileName( "/TestRemoteCacheServer.ccf" );
224         RemoteCacheServer server = new RemoteCacheServer( rcsa );
225 
226         // this is to get the listenr id for inserts.
227         RemoteCacheListenerMockImpl clusterListener = new RemoteCacheListenerMockImpl();
228         clusterListener.remoteType = IRemoteCacheAttributes.CLUSTER;
229 
230         // this should get the updates
231         RemoteCacheListenerMockImpl localListener = new RemoteCacheListenerMockImpl();
232         localListener.remoteType = IRemoteCacheAttributes.LOCAL;
233 
234         String cacheName = "testSimpleRegisterListenerAndPut_FromClusterWithLCC";
235         server.addCacheListener( cacheName, clusterListener );
236         server.addCacheListener( cacheName, localListener );
237 
238         // DO WORK
239         List inputItems = new LinkedList();
240         int numToPut = 10;
241 
242         for ( int i = 0; i < numToPut; i++ )
243         {
244             ICacheElement element = new CacheElement( cacheName, String.valueOf( i ), new Long( i ) );
245             inputItems.add( element );
246             // update using the cluster listener id
247             server.update( element, clusterListener.getListenerId() );
248         }
249 
250         Thread.sleep( 100 );
251         Thread.yield();
252         Thread.sleep( 100 );
253 
254         // VERIFY
255         assertEquals( "Wrong number of items put to listener.", numToPut, localListener.putItems.size() );
256         for ( int i = 0; i < numToPut; i++ )
257         {
258             assertEquals( "Wrong item.", inputItems.get( i ), localListener.putItems.get( i ) );
259         }
260     }
261 
262     /***
263      * Register a listener and then verify that it is called when we put using a different listener
264      * id.
265      * @throws Exception
266      */
267     public void testSimpleRegisterListenerAndRemove()
268         throws Exception
269     {
270         // SETUP
271         IRemoteCacheServerAttributes rcsa = new RemoteCacheServerAttributes();
272         rcsa.setConfigFileName( "/TestRemoteCacheServer.ccf" );
273 
274         RemoteCacheListenerMockImpl mockListener = new RemoteCacheListenerMockImpl();
275         RemoteCacheServer server = new RemoteCacheServer( rcsa );
276 
277         String cacheName = "testSimpleRegisterListenerAndPut";
278         server.addCacheListener( cacheName, mockListener );
279 
280         // DO WORK
281         int numToPut = 10;
282 
283         for ( int i = 0; i < numToPut; i++ )
284         {
285             // use a junk listener id
286             server.remove( cacheName, String.valueOf( i ), 9999 );
287         }
288 
289         Thread.sleep( 100 );
290         Thread.yield();
291         Thread.sleep( 100 );
292 
293         // VERIFY
294         assertEquals( "Wrong number of items removed from listener.", numToPut, mockListener.removedKeys.size() );
295         for ( int i = 0; i < numToPut; i++ )
296         {
297             assertEquals( "Wrong key.", String.valueOf( i ), mockListener.removedKeys.get( i ) );
298         }
299     }
300 
301 }