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