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