1 package org.apache.jcs.auxiliary.remote;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import junit.framework.TestCase;
23
24 import org.apache.jcs.auxiliary.remote.behavior.IRemoteCacheAttributes;
25 import org.apache.jcs.engine.CacheElementSerialized;
26 import org.apache.jcs.engine.ElementAttributes;
27 import org.apache.jcs.engine.behavior.ICacheElement;
28 import org.apache.jcs.engine.behavior.ICacheElementSerialized;
29 import org.apache.jcs.engine.behavior.ICompositeCacheManager;
30 import org.apache.jcs.engine.behavior.IElementAttributes;
31 import org.apache.jcs.engine.behavior.IElementSerializer;
32 import org.apache.jcs.engine.control.CompositeCacheManagerMockImpl;
33 import org.apache.jcs.utils.serialization.StandardSerializer;
34
35 /***
36 * Tests for the remote cache listener.
37 * <p>
38 * @author Aaron Smuts
39 *
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 *
49 * @throws Exception
50 */
51 public void testUpdate()
52 throws Exception
53 {
54 IRemoteCacheAttributes irca = new RemoteCacheAttributes();
55 irca.setRemoveUponRemotePut( false );
56 ICompositeCacheManager cacheMgr = new CompositeCacheManagerMockImpl();
57 RemoteCacheListener listener = new RemoteCacheListener( irca, cacheMgr );
58
59 String cacheName = "testName";
60 String key = "key";
61 String value = "value fdsadf dsafdsa fdsaf dsafdsaf dsafdsaf dsaf dsaf dsaf dsafa dsaf dsaf dsafdsaf";
62 IElementAttributes attr = new ElementAttributes();
63 attr.setMaxLifeSeconds( 34 );
64
65 IElementSerializer elementSerializer = new StandardSerializer();
66
67 ICacheElementSerialized element = new CacheElementSerialized( cacheName, key, elementSerializer
68 .serialize( value ), attr );
69 listener.handlePut( element );
70
71 ICacheElement after = cacheMgr.getCache( cacheName ).get( key );
72
73 assertNotNull( "Should have a deserialized object.", after );
74 assertEquals( "Values should be the same.", value, after.getVal() );
75 assertEquals( "Attributes should be the same.", attr.getMaxLifeSeconds(), after
76 .getElementAttributes().getMaxLifeSeconds() );
77 assertEquals( "Keys should be the same.", key, after.getKey() );
78 assertEquals( "Cache name should be the same.", cacheName, after.getCacheName() );
79 }
80
81 /***
82 * Create a RemoteCacheListener with a mock cache manager. Set remove on put to false.
83 * Create a serialized element. Call put on the listener.
84 * Verify that the deserialized element is in the cache.
85 *
86 * @throws Exception
87 */
88 public void testUpdate_RemoveOnPut()
89 throws Exception
90 {
91 IRemoteCacheAttributes irca = new RemoteCacheAttributes();
92 irca.setRemoveUponRemotePut( true );
93 ICompositeCacheManager cacheMgr = new CompositeCacheManagerMockImpl();
94 RemoteCacheListener listener = new RemoteCacheListener( irca, cacheMgr );
95
96 String cacheName = "testName";
97 String key = "key";
98 String value = "value fdsadf dsafdsa fdsaf dsafdsaf dsafdsaf dsaf dsaf dsaf dsafa dsaf dsaf dsafdsaf";
99 IElementAttributes attr = new ElementAttributes();
100 attr.setMaxLifeSeconds( 34 );
101
102 IElementSerializer elementSerializer = new StandardSerializer();
103
104 ICacheElementSerialized element = new CacheElementSerialized( cacheName, key, elementSerializer
105 .serialize( value ), attr );
106 listener.handlePut( element );
107
108 ICacheElement after = cacheMgr.getCache( cacheName ).get( key );
109
110 assertNull( "Should not have a deserialized object since remove on put is true.", after );
111 }
112
113 }