View Javadoc
1   package org.apache.commons.jcs.engine;
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  import org.apache.commons.jcs.engine.behavior.ICacheElement;
24  
25  /**
26   * Tests for the zombie remote cache service.
27   */
28  public class ZombieCacheServiceNonLocalUnitTest
29      extends TestCase
30  {
31      /**
32       * Verify that an update event gets added and then is sent to the service passed to propagate.
33       * <p>
34       * @throws Exception
35       */
36      public void testUpdateThenWalk()
37          throws Exception
38      {
39          // SETUP
40          MockCacheServiceNonLocal<String, String> service = new MockCacheServiceNonLocal<String, String>();
41  
42          ZombieCacheServiceNonLocal<String, String> zombie = new ZombieCacheServiceNonLocal<String, String>( 10 );
43  
44          String cacheName = "testUpdate";
45  
46          // DO WORK
47          ICacheElement<String, String> element = new CacheElement<String, String>( cacheName, "key", "value" );
48          zombie.update( element, 123l );
49          zombie.propagateEvents( service );
50  
51          // VERIFY
52          assertEquals( "Updated element is not as expected.", element, service.lastUpdate );
53      }
54  
55      /**
56       * Verify that nothing is added if the max is set to 0.
57       * <p>
58       * @throws Exception
59       */
60      public void testUpdateThenWalk_zeroSize()
61          throws Exception
62      {
63          // SETUP
64          MockCacheServiceNonLocal<String, String> service = new MockCacheServiceNonLocal<String, String>();
65  
66          ZombieCacheServiceNonLocal<String, String> zombie = new ZombieCacheServiceNonLocal<String, String>( 0 );
67  
68          String cacheName = "testUpdate";
69  
70          // DO WORK
71          ICacheElement<String, String> element = new CacheElement<String, String>( cacheName, "key", "value" );
72          zombie.update( element, 123l );
73          zombie.propagateEvents( service );
74  
75          // VERIFY
76          assertNull( "Nothing should have been put to the service.", service.lastUpdate );
77      }
78  
79      /**
80       * Verify that a remove event gets added and then is sent to the service passed to propagate.
81       * <p>
82       * @throws Exception
83       */
84      public void testRemoveThenWalk()
85          throws Exception
86      {
87          // SETUP
88          MockCacheServiceNonLocal<String, String> service = new MockCacheServiceNonLocal<String, String>();
89  
90          ZombieCacheServiceNonLocal<String, String> zombie = new ZombieCacheServiceNonLocal<String, String>( 10 );
91  
92          String cacheName = "testRemoveThenWalk";
93          String key = "myKey";
94  
95          // DO WORK
96          zombie.remove( cacheName, key, 123l );
97          zombie.propagateEvents( service );
98  
99          // VERIFY
100         assertEquals( "Updated element is not as expected.", key, service.lastRemoveKey );
101     }
102 
103     /**
104      * Verify that a removeAll event gets added and then is sent to the service passed to propagate.
105      * <p>
106      * @throws Exception
107      */
108     public void testRemoveAllThenWalk()
109         throws Exception
110     {
111         // SETUP
112         MockCacheServiceNonLocal<String, String> service = new MockCacheServiceNonLocal<String, String>();
113 
114         ZombieCacheServiceNonLocal<String, String> zombie = new ZombieCacheServiceNonLocal<String, String>( 10 );
115 
116         String cacheName = "testRemoveThenWalk";
117 
118         // DO WORK
119         zombie.removeAll( cacheName, 123l );
120         zombie.propagateEvents( service );
121 
122         // VERIFY
123         assertEquals( "Updated element is not as expected.", cacheName, service.lastRemoveAllCacheName );
124     }
125 }