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  
24  import org.apache.commons.jcs.auxiliary.remote.behavior.IRemoteCacheAttributes;
25  import org.apache.commons.jcs.engine.CacheElementSerialized;
26  import org.apache.commons.jcs.engine.ElementAttributes;
27  import org.apache.commons.jcs.engine.behavior.ICache;
28  import org.apache.commons.jcs.engine.behavior.ICacheElement;
29  import org.apache.commons.jcs.engine.behavior.ICacheElementSerialized;
30  import org.apache.commons.jcs.engine.behavior.ICompositeCacheManager;
31  import org.apache.commons.jcs.engine.behavior.IElementAttributes;
32  import org.apache.commons.jcs.engine.behavior.IElementSerializer;
33  import org.apache.commons.jcs.engine.control.MockCompositeCacheManager;
34  import org.apache.commons.jcs.utils.serialization.StandardSerializer;
35  
36  /**
37   * Tests for the remote cache listener.
38   * <p>
39   * @author Aaron Smuts
40   */
41  public class RemoteCacheListenerUnitTest
42      extends TestCase
43  {
44      /**
45       * Create a RemoteCacheListener with a mock cache manager.  Set remove on put to false.
46       * Create a serialized element.  Call put on the listener.
47       * Verify that the deserialized element is in the cache.
48       * <p>
49       * @throws Exception
50       */
51      public void testUpdate_PutOnPut()
52          throws Exception
53      {
54          // SETUP
55          IRemoteCacheAttributes irca = new RemoteCacheAttributes();
56          irca.setRemoveUponRemotePut( false );
57          ICompositeCacheManager cacheMgr = new MockCompositeCacheManager();
58          RemoteCacheListener<String, String> listener = new RemoteCacheListener<String, String>( irca, cacheMgr, new StandardSerializer() );
59  
60          String cacheName = "testName";
61          String key = "key";
62          String value = "value fdsadf dsafdsa fdsaf dsafdsaf dsafdsaf dsaf dsaf dsaf dsafa dsaf dsaf dsafdsaf";
63          IElementAttributes attr = new ElementAttributes();
64          attr.setMaxLife(34);
65  
66          IElementSerializer elementSerializer = new StandardSerializer();
67  
68          ICacheElementSerialized<String, String> element =
69              new CacheElementSerialized<String, String>( cacheName, key, elementSerializer
70              .serialize( value ), attr );
71  
72          // DO WORK
73          listener.handlePut( element );
74  
75          // VERIFY
76          ICache<String, String> cache = cacheMgr.getCache( cacheName );
77          ICacheElement<String, String> after = cache.get( key );
78  
79          assertNotNull( "Should have a deserialized object.", after );
80          assertEquals( "Values should be the same.", value, after.getVal() );
81          assertEquals( "Attributes should be the same.", attr.getMaxLife(), after
82              .getElementAttributes().getMaxLife() );
83          assertEquals( "Keys should be the same.", key, after.getKey() );
84          assertEquals( "Cache name should be the same.", cacheName, after.getCacheName() );
85      }
86  
87      /**
88       * Create a RemoteCacheListener with a mock cache manager.  Set remove on put to true.
89       * Create a serialized element.  Call put on the listener.
90       * Verify that the deserialized element is not in the cache.
91       * <p>
92       * @throws Exception
93       */
94      public void testUpdate_RemoveOnPut()
95          throws Exception
96      {
97          // SETUP
98          IRemoteCacheAttributes irca = new RemoteCacheAttributes();
99          irca.setRemoveUponRemotePut( true );
100         ICompositeCacheManager cacheMgr = new MockCompositeCacheManager();
101         RemoteCacheListener<String, String> listener = new RemoteCacheListener<String, String>( irca, cacheMgr, new StandardSerializer() );
102 
103         String cacheName = "testName";
104         String key = "key";
105         String value = "value fdsadf dsafdsa fdsaf dsafdsaf dsafdsaf dsaf dsaf dsaf dsafa dsaf dsaf dsafdsaf";
106         IElementAttributes attr = new ElementAttributes();
107         attr.setMaxLife(34);
108 
109         IElementSerializer elementSerializer = new StandardSerializer();
110 
111         ICacheElementSerialized<String, String> element =
112             new CacheElementSerialized<String, String>( cacheName, key, elementSerializer
113             .serialize( value ), attr );
114 
115         // DO WORK
116         listener.handlePut( element );
117 
118         // VERIFY
119         ICache<String, String> cache = cacheMgr.getCache( cacheName );
120         ICacheElement<String, String> after = cache.get( key );
121 
122         assertNull( "Should not have a deserialized object since remove on put is true.", after );
123     }
124 }