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