1 package org.apache.commons.jcs3.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.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
38
39 public class RemoteCacheListenerUnitTest
40 extends TestCase
41 {
42
43
44
45
46
47
48
49 public void testUpdate_PutOnPut()
50 throws Exception
51 {
52
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
71 listener.handlePut( element );
72
73
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
87
88
89
90
91
92 public void testUpdate_RemoveOnPut()
93 throws Exception
94 {
95
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
114 listener.handlePut( element );
115
116
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 }