1 package org.apache.commons.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 import org.apache.commons.jcs.engine.CacheElement;
24 import org.apache.commons.jcs.engine.CacheStatus;
25 import org.apache.commons.jcs.engine.behavior.ICacheElement;
26 import org.apache.commons.jcs.engine.behavior.ICacheEventQueue;
27 import org.apache.commons.jcs.utils.timing.SleepUtil;
28
29 import java.util.HashMap;
30 import java.util.HashSet;
31 import java.util.Map;
32 import java.util.Set;
33
34
35
36
37
38
39 public class RemoteCacheNoWaitUnitTest
40 extends TestCase
41 {
42
43
44
45
46
47 public void testUpdate()
48 throws Exception
49 {
50
51 MockRemoteCacheClient<String, String> client = new MockRemoteCacheClient<String, String>();
52 RemoteCacheNoWait<String, String> noWait = new RemoteCacheNoWait<String, String>( client );
53
54 ICacheElement<String, String> element = new CacheElement<String, String>( "testUpdate", "key", "value" );
55
56
57 noWait.update( element );
58
59
60 SleepUtil.sleepAtLeast( 10 );
61
62 assertEquals( "Wrong number updated.", 1, client.updateList.size() );
63 assertEquals( "Wrong element", element, client.updateList.get( 0 ) );
64 }
65
66
67
68
69
70
71 public void testGet()
72 throws Exception
73 {
74
75 MockRemoteCacheClient<String, String> client = new MockRemoteCacheClient<String, String>();
76 RemoteCacheNoWait<String, String> noWait = new RemoteCacheNoWait<String, String>( client );
77
78 ICacheElement<String, String> input = new CacheElement<String, String>( "testUpdate", "key", "value" );
79 client.getSetupMap.put( "key", input );
80
81
82 ICacheElement<String, String> result = noWait.get( "key" );
83
84
85 assertEquals( "Wrong element", input, result );
86 }
87
88
89
90
91
92
93 public void testGetMultiple()
94 throws Exception
95 {
96
97 MockRemoteCacheClient<String, String> client = new MockRemoteCacheClient<String, String>();
98 RemoteCacheNoWait<String, String> noWait = new RemoteCacheNoWait<String, String>( client );
99
100 ICacheElement<String, String> inputElement = new CacheElement<String, String>( "testUpdate", "key", "value" );
101 Map<String, ICacheElement<String, String>> inputMap = new HashMap<String, ICacheElement<String,String>>();
102 inputMap.put( "key", inputElement );
103
104 Set<String> keys = new HashSet<String>();
105 keys.add( "key" );
106
107 client.getMultipleSetupMap.put( keys, inputMap );
108
109
110 Map<String, ICacheElement<String, String>> result = noWait.getMultiple( keys );
111
112
113 assertEquals( "elements map", inputMap, result );
114 }
115
116
117
118
119
120
121 public void testRemove()
122 throws Exception
123 {
124
125 MockRemoteCacheClient<String, String> client = new MockRemoteCacheClient<String, String>();
126 RemoteCacheNoWait<String, String> noWait = new RemoteCacheNoWait<String, String>( client );
127
128 String input = "MyKey";
129
130
131 noWait.remove( input );
132
133 SleepUtil.sleepAtLeast( 10 );
134
135
136 assertEquals( "Wrong number updated.", 1, client.removeList.size() );
137 assertEquals( "Wrong key", input, client.removeList.get( 0 ) );
138 }
139
140
141
142
143
144
145 public void testGetStats()
146 throws Exception
147 {
148
149 MockRemoteCacheClient<String, String> client = new MockRemoteCacheClient<String, String>();
150 client.status = CacheStatus.ALIVE;
151 RemoteCacheNoWait<String, String> noWait = new RemoteCacheNoWait<String, String>( client );
152
153
154 String result = noWait.getStats();
155
156
157 assertTrue( "Status should contain 'ALIVE'", result.indexOf( "ALIVE" ) != -1 );
158 }
159
160
161
162
163
164
165 public void testGetStatus_error()
166 throws Exception
167 {
168
169 MockRemoteCacheClient<String, String> client = new MockRemoteCacheClient<String, String>();
170 client.status = CacheStatus.ERROR;
171 RemoteCacheNoWait<String, String> noWait = new RemoteCacheNoWait<String, String>( client );
172
173
174 CacheStatus result = noWait.getStatus();
175
176
177 assertEquals( "Wrong status", CacheStatus.ERROR, result );
178 }
179
180
181
182
183
184
185
186 public void testFixCache()
187 throws Exception
188 {
189
190 MockRemoteCacheClient<String, String> client = new MockRemoteCacheClient<String, String>();
191 client.status = CacheStatus.ALIVE;
192 RemoteCacheNoWait<String, String> noWait = new RemoteCacheNoWait<String, String>( client );
193
194 MockRemoteCacheService<String, String> service = new MockRemoteCacheService<String, String>();
195
196 ICacheElement<String, String> element = new CacheElement<String, String>( "testUpdate", "key", "value" );
197
198
199 noWait.update( element );
200 SleepUtil.sleepAtLeast( 10 );
201 ICacheEventQueue<String, String> originalQueue = noWait.getCacheEventQueue();
202
203 noWait.fixCache( service );
204
205 noWait.update( element );
206 SleepUtil.sleepAtLeast( 10 );
207 ICacheEventQueue<String, String> newQueue = noWait.getCacheEventQueue();
208
209
210 assertEquals( "Wrong status", service, client.fixed );
211 assertFalse( "Original queue should not alive", originalQueue.isAlive() );
212 assertTrue( "New queue should be alive." + newQueue, newQueue.isAlive() );
213 }
214 }