View Javadoc
1   package org.apache.commons.jcs3.auxiliary.remote;
2   
3   import org.apache.commons.jcs3.engine.CacheElement;
4   import org.apache.commons.jcs3.engine.ZombieCacheServiceNonLocal;
5   import org.apache.commons.jcs3.engine.behavior.ICacheElement;
6   
7   /*
8    * Licensed to the Apache Software Foundation (ASF) under one
9    * or more contributor license agreements.  See the NOTICE file
10   * distributed with this work for additional information
11   * regarding copyright ownership.  The ASF licenses this file
12   * to you under the Apache License, Version 2.0 (the
13   * "License"); you may not use this file except in compliance
14   * with the License.  You may obtain a copy of the License at
15   *
16   *   http://www.apache.org/licenses/LICENSE-2.0
17   *
18   * Unless required by applicable law or agreed to in writing,
19   * software distributed under the License is distributed on an
20   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21   * KIND, either express or implied.  See the License for the
22   * specific language governing permissions and limitations
23   * under the License.
24   */
25  
26  import junit.framework.TestCase;
27  
28  /**
29   * Tests for the zombie remote cache service.
30   */
31  public class ZombieRemoteCacheServiceUnitTest
32      extends TestCase
33  {
34      /**
35       * Verify that an update event gets added and then is sent to the service passed to propagate.
36       * <p>
37       * @throws Exception
38       */
39      public void testUpdateThenWalk()
40          throws Exception
41      {
42          // SETUP
43          final MockRemoteCacheService<String, String> service = new MockRemoteCacheService<>();
44  
45          final ZombieCacheServiceNonLocal<String, String> zombie = new ZombieCacheServiceNonLocal<>( 10 );
46  
47          final String cacheName = "testUpdate";
48  
49          // DO WORK
50          final ICacheElement<String, String> element = new CacheElement<>( cacheName, "key", "value" );
51          zombie.update( element, 123L );
52          zombie.propagateEvents( service );
53  
54          // VERIFY
55          assertEquals( "Updated element is not as expected.", element, service.lastUpdate );
56      }
57  
58      /**
59       * Verify that nothing is added if the max is set to 0.
60       * <p>
61       * @throws Exception
62       */
63      public void testUpdateThenWalk_zeroSize()
64          throws Exception
65      {
66          // SETUP
67          final MockRemoteCacheService<String, String> service = new MockRemoteCacheService<>();
68  
69          final ZombieCacheServiceNonLocal<String, String> zombie = new ZombieCacheServiceNonLocal<>( 0 );
70  
71          final String cacheName = "testUpdate";
72  
73          // DO WORK
74          final ICacheElement<String, String> element = new CacheElement<>( cacheName, "key", "value" );
75          zombie.update( element, 123L );
76          zombie.propagateEvents( service );
77  
78          // VERIFY
79          assertNull( "Nothing should have been put to the service.", service.lastUpdate );
80      }
81  
82      /**
83       * Verify that a remove event gets added and then is sent to the service passed to propagate.
84       * <p>
85       * @throws Exception
86       */
87      public void testRemoveThenWalk()
88          throws Exception
89      {
90          // SETUP
91          final MockRemoteCacheService<String, String> service = new MockRemoteCacheService<>();
92  
93          final ZombieCacheServiceNonLocal<String, String> zombie = new ZombieCacheServiceNonLocal<>( 10 );
94  
95          final String cacheName = "testRemoveThenWalk";
96          final String key = "myKey";
97  
98          // DO WORK
99          zombie.remove( cacheName, key, 123L );
100         zombie.propagateEvents( service );
101 
102         // VERIFY
103         assertEquals( "Updated element is not as expected.", key, service.lastRemoveKey );
104     }
105 
106     /**
107      * Verify that a removeAll event gets added and then is sent to the service passed to propagate.
108      * <p>
109      * @throws Exception
110      */
111     public void testRemoveAllThenWalk()
112         throws Exception
113     {
114         // SETUP
115         final MockRemoteCacheService<String, String> service = new MockRemoteCacheService<>();
116 
117         final ZombieCacheServiceNonLocal<String, String> zombie = new ZombieCacheServiceNonLocal<>( 10 );
118 
119         final String cacheName = "testRemoveThenWalk";
120 
121         // DO WORK
122         zombie.removeAll( cacheName, 123L );
123         zombie.propagateEvents( service );
124 
125         // VERIFY
126         assertEquals( "Updated element is not as expected.", cacheName, service.lastRemoveAllCacheName);
127     }
128 }