1 package org.apache.commons.jcs.auxiliary.remote.http.client;
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.auxiliary.remote.value.RemoteCacheResponse;
24 import org.apache.commons.jcs.auxiliary.remote.value.RemoteRequestType;
25 import org.apache.commons.jcs.engine.CacheElement;
26 import org.apache.commons.jcs.engine.behavior.ICacheElement;
27
28 import java.io.IOException;
29 import java.util.Collections;
30 import java.util.HashMap;
31 import java.util.Map;
32 import java.util.Set;
33
34
35 public class RemoteHttpCacheClientUnitTest
36 extends TestCase
37 {
38
39
40
41
42
43 public void testGet_nullFromDispatcher()
44 throws IOException
45 {
46
47 RemoteHttpCacheAttributes attributes = new RemoteHttpCacheAttributes();
48 RemoteHttpCacheClient<String, String> client = new RemoteHttpCacheClient<String, String>( attributes );
49
50 MockRemoteCacheDispatcher mockDispatcher = new MockRemoteCacheDispatcher();
51 client.setRemoteDispatcher( mockDispatcher );
52
53 String cacheName = "test";
54 String key = "key";
55
56 mockDispatcher.setupRemoteCacheResponse = null;
57
58
59 ICacheElement<String, String> result = client.get( cacheName, key );
60
61
62 assertNull( "Wrong result.", result );
63 assertEquals( "Wrong type.", RemoteRequestType.GET, mockDispatcher.lastRemoteCacheRequest
64 .getRequestType() );
65 }
66
67
68
69
70
71
72 public void testGet_normal()
73 throws IOException
74 {
75
76 RemoteHttpCacheAttributes attributes = new RemoteHttpCacheAttributes();
77 RemoteHttpCacheClient<String, String> client = new RemoteHttpCacheClient<String, String>( attributes );
78
79 MockRemoteCacheDispatcher mockDispatcher = new MockRemoteCacheDispatcher();
80 client.setRemoteDispatcher( mockDispatcher );
81
82 String cacheName = "test";
83 String key = "key";
84
85 ICacheElement<String, String> expected = new CacheElement<String, String>( cacheName, key, "value" );
86 RemoteCacheResponse<ICacheElement<String, String>> remoteHttpCacheResponse =
87 new RemoteCacheResponse<ICacheElement<String,String>>();
88 remoteHttpCacheResponse.setPayload( expected );
89
90 mockDispatcher.setupRemoteCacheResponse = remoteHttpCacheResponse;
91
92
93 ICacheElement<String, String> result = client.get( cacheName, key );
94
95
96 assertEquals( "Wrong result.", expected, result );
97 assertEquals( "Wrong type.", RemoteRequestType.GET, mockDispatcher.lastRemoteCacheRequest
98 .getRequestType() );
99 }
100
101
102
103
104
105
106 public void testGetMatching_normal()
107 throws IOException
108 {
109
110 RemoteHttpCacheAttributes attributes = new RemoteHttpCacheAttributes();
111 RemoteHttpCacheClient<String, String> client = new RemoteHttpCacheClient<String, String>( attributes );
112
113 MockRemoteCacheDispatcher mockDispatcher = new MockRemoteCacheDispatcher();
114 client.setRemoteDispatcher( mockDispatcher );
115
116 String cacheName = "test";
117 String pattern = "key";
118
119 ICacheElement<String, String> expected = new CacheElement<String, String>( cacheName, "key", "value" );
120 Map<String, ICacheElement<String, String>> expectedMap = new HashMap<String, ICacheElement<String,String>>();
121 expectedMap.put( "key", expected );
122 RemoteCacheResponse<Map<String, ICacheElement<String, String>>> remoteHttpCacheResponse =
123 new RemoteCacheResponse<Map<String,ICacheElement<String,String>>>();
124 remoteHttpCacheResponse.setPayload( expectedMap );
125
126 mockDispatcher.setupRemoteCacheResponse = remoteHttpCacheResponse;
127
128
129 Map<String, ICacheElement<String, String>> result = client.getMatching( cacheName, pattern );
130
131
132 assertEquals( "Wrong result.", expected, result.get( "key" ) );
133 assertEquals( "Wrong type.", RemoteRequestType.GET_MATCHING,
134 mockDispatcher.lastRemoteCacheRequest.getRequestType() );
135 }
136
137
138
139
140
141
142 public void testGetMultiple_normal()
143 throws IOException
144 {
145
146 RemoteHttpCacheAttributes attributes = new RemoteHttpCacheAttributes();
147 RemoteHttpCacheClient<String, String> client = new RemoteHttpCacheClient<String, String>( attributes );
148
149 MockRemoteCacheDispatcher mockDispatcher = new MockRemoteCacheDispatcher();
150 client.setRemoteDispatcher( mockDispatcher );
151
152 String cacheName = "test";
153 Set<String> keys = Collections.emptySet();
154
155 ICacheElement<String, String> expected = new CacheElement<String, String>( cacheName, "key", "value" );
156 Map<String, ICacheElement<String, String>> expectedMap = new HashMap<String, ICacheElement<String,String>>();
157 expectedMap.put( "key", expected );
158 RemoteCacheResponse<Map<String, ICacheElement<String, String>>> remoteHttpCacheResponse =
159 new RemoteCacheResponse<Map<String,ICacheElement<String,String>>>();
160 remoteHttpCacheResponse.setPayload( expectedMap );
161
162 mockDispatcher.setupRemoteCacheResponse = remoteHttpCacheResponse;
163
164
165 Map<String, ICacheElement<String, String>> result = client.getMultiple( cacheName, keys );
166
167
168 assertEquals( "Wrong result.", expected, result.get( "key" ) );
169 assertEquals( "Wrong type.", RemoteRequestType.GET_MULTIPLE,
170 mockDispatcher.lastRemoteCacheRequest.getRequestType() );
171 }
172
173
174
175
176
177
178 public void testRemove_normal()
179 throws IOException
180 {
181
182 RemoteHttpCacheAttributes attributes = new RemoteHttpCacheAttributes();
183 RemoteHttpCacheClient<String, String> client = new RemoteHttpCacheClient<String, String>( attributes );
184
185 MockRemoteCacheDispatcher mockDispatcher = new MockRemoteCacheDispatcher();
186 client.setRemoteDispatcher( mockDispatcher );
187
188 String cacheName = "test";
189 String key = "key";
190
191
192 client.remove( cacheName, key );
193
194
195 assertEquals( "Wrong type.", RemoteRequestType.REMOVE, mockDispatcher.lastRemoteCacheRequest
196 .getRequestType() );
197 }
198
199
200
201
202
203
204 public void testRemoveAll_normal()
205 throws IOException
206 {
207
208 RemoteHttpCacheAttributes attributes = new RemoteHttpCacheAttributes();
209 RemoteHttpCacheClient<String, String> client = new RemoteHttpCacheClient<String, String>( attributes );
210
211 MockRemoteCacheDispatcher mockDispatcher = new MockRemoteCacheDispatcher();
212 client.setRemoteDispatcher( mockDispatcher );
213
214 String cacheName = "test";
215
216
217 client.removeAll( cacheName );
218
219
220 assertEquals( "Wrong type.", RemoteRequestType.REMOVE_ALL, mockDispatcher.lastRemoteCacheRequest
221 .getRequestType() );
222 }
223
224
225
226
227
228
229 public void testUpdate_normal()
230 throws IOException
231 {
232
233 RemoteHttpCacheAttributes attributes = new RemoteHttpCacheAttributes();
234 RemoteHttpCacheClient<String, String> client = new RemoteHttpCacheClient<String, String>( attributes );
235
236 MockRemoteCacheDispatcher mockDispatcher = new MockRemoteCacheDispatcher();
237 client.setRemoteDispatcher( mockDispatcher );
238
239 String cacheName = "test";
240
241 ICacheElement<String, String> element = new CacheElement<String, String>( cacheName, "key", "value" );
242
243
244 client.update( element );
245
246
247 assertEquals( "Wrong type.", RemoteRequestType.UPDATE, mockDispatcher.lastRemoteCacheRequest
248 .getRequestType() );
249 }
250
251
252
253
254
255
256 public void testDispose_normal()
257 throws IOException
258 {
259
260 RemoteHttpCacheAttributes attributes = new RemoteHttpCacheAttributes();
261 RemoteHttpCacheClient<String, String> client = new RemoteHttpCacheClient<String, String>( attributes );
262
263 MockRemoteCacheDispatcher mockDispatcher = new MockRemoteCacheDispatcher();
264 client.setRemoteDispatcher( mockDispatcher );
265
266 String cacheName = "test";
267
268
269 client.dispose( cacheName );
270
271
272 assertEquals( "Wrong type.", RemoteRequestType.DISPOSE, mockDispatcher.lastRemoteCacheRequest
273 .getRequestType() );
274 }
275 }