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