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