1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
package org.apache.shale.test.mock; |
19 | |
|
20 | |
import java.util.ArrayList; |
21 | |
import java.util.Collection; |
22 | |
import java.util.Enumeration; |
23 | |
import java.util.HashSet; |
24 | |
import java.util.Iterator; |
25 | |
import java.util.List; |
26 | |
import java.util.Map; |
27 | |
import java.util.Set; |
28 | |
|
29 | |
import javax.servlet.http.HttpServletRequest; |
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
class MockRequestMap implements Map { |
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | 36 | public MockRequestMap(HttpServletRequest request) { |
51 | |
|
52 | 36 | this.request = request; |
53 | |
|
54 | 36 | } |
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | 36 | private HttpServletRequest request = null; |
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
public void clear() { |
75 | |
|
76 | |
Iterator keys = keySet().iterator(); |
77 | |
while (keys.hasNext()) { |
78 | |
request.removeAttribute((String) keys.next()); |
79 | |
} |
80 | |
|
81 | |
} |
82 | |
|
83 | |
|
84 | |
|
85 | |
public boolean containsKey(Object key) { |
86 | |
|
87 | 22 | return request.getAttribute(key(key)) != null; |
88 | |
|
89 | |
} |
90 | |
|
91 | |
|
92 | |
|
93 | |
public boolean containsValue(Object value) { |
94 | |
|
95 | |
if (value == null) { |
96 | |
return false; |
97 | |
} |
98 | |
Enumeration keys = request.getAttributeNames(); |
99 | |
while (keys.hasMoreElements()) { |
100 | |
Object next = request.getAttribute((String) keys.nextElement()); |
101 | |
if (next == value) { |
102 | |
return true; |
103 | |
} |
104 | |
} |
105 | |
return false; |
106 | |
|
107 | |
} |
108 | |
|
109 | |
|
110 | |
|
111 | |
public Set entrySet() { |
112 | |
|
113 | |
Set set = new HashSet(); |
114 | |
Enumeration keys = request.getAttributeNames(); |
115 | |
while (keys.hasMoreElements()) { |
116 | |
set.add(request.getAttribute((String) keys.nextElement())); |
117 | |
} |
118 | |
return set; |
119 | |
|
120 | |
} |
121 | |
|
122 | |
|
123 | |
|
124 | |
public boolean equals(Object o) { |
125 | |
|
126 | |
return request.equals(o); |
127 | |
|
128 | |
} |
129 | |
|
130 | |
|
131 | |
|
132 | |
public Object get(Object key) { |
133 | |
|
134 | 21 | return request.getAttribute(key(key)); |
135 | |
|
136 | |
} |
137 | |
|
138 | |
|
139 | |
|
140 | |
public int hashCode() { |
141 | |
|
142 | |
return request.hashCode(); |
143 | |
|
144 | |
} |
145 | |
|
146 | |
|
147 | |
|
148 | |
public boolean isEmpty() { |
149 | |
|
150 | |
return size() < 1; |
151 | |
|
152 | |
} |
153 | |
|
154 | |
|
155 | |
|
156 | |
public Set keySet() { |
157 | |
|
158 | |
Set set = new HashSet(); |
159 | |
Enumeration keys = request.getAttributeNames(); |
160 | |
while (keys.hasMoreElements()) { |
161 | |
set.add(keys.nextElement()); |
162 | |
} |
163 | |
return set; |
164 | |
|
165 | |
} |
166 | |
|
167 | |
|
168 | |
|
169 | |
public Object put(Object key, Object value) { |
170 | |
|
171 | 6 | if (value == null) { |
172 | |
return (remove(key)); |
173 | |
} |
174 | 6 | String skey = key(key); |
175 | 6 | Object previous = request.getAttribute(skey); |
176 | 6 | request.setAttribute(skey, value); |
177 | 6 | return previous; |
178 | |
|
179 | |
} |
180 | |
|
181 | |
|
182 | |
|
183 | |
public void putAll(Map map) { |
184 | |
|
185 | |
Iterator keys = map.keySet().iterator(); |
186 | |
while (keys.hasNext()) { |
187 | |
String key = (String) keys.next(); |
188 | |
request.setAttribute(key, map.get(key)); |
189 | |
} |
190 | |
|
191 | |
} |
192 | |
|
193 | |
|
194 | |
|
195 | |
public Object remove(Object key) { |
196 | |
|
197 | |
String skey = key(key); |
198 | |
Object previous = request.getAttribute(skey); |
199 | |
request.removeAttribute(skey); |
200 | |
return previous; |
201 | |
|
202 | |
} |
203 | |
|
204 | |
|
205 | |
|
206 | |
public int size() { |
207 | |
|
208 | |
int n = 0; |
209 | |
Enumeration keys = request.getAttributeNames(); |
210 | |
while (keys.hasMoreElements()) { |
211 | |
keys.nextElement(); |
212 | |
n++; |
213 | |
} |
214 | |
return n; |
215 | |
|
216 | |
} |
217 | |
|
218 | |
|
219 | |
|
220 | |
public Collection values() { |
221 | |
|
222 | |
List list = new ArrayList(); |
223 | |
Enumeration keys = request.getAttributeNames(); |
224 | |
while (keys.hasMoreElements()) { |
225 | |
list.add(request.getAttribute((String) keys.nextElement())); |
226 | |
} |
227 | |
return list; |
228 | |
|
229 | |
} |
230 | |
|
231 | |
|
232 | |
|
233 | |
|
234 | |
|
235 | |
|
236 | |
|
237 | |
|
238 | |
|
239 | |
|
240 | |
private String key(Object key) { |
241 | |
|
242 | 49 | if (key == null) { |
243 | |
throw new IllegalArgumentException(); |
244 | 49 | } else if (key instanceof String) { |
245 | 49 | return ((String) key); |
246 | |
} else { |
247 | |
return (key.toString()); |
248 | |
} |
249 | |
|
250 | |
} |
251 | |
|
252 | |
|
253 | |
} |