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.Enumeration; |
22 | |
import java.util.HashMap; |
23 | |
import java.util.Iterator; |
24 | |
import java.util.List; |
25 | |
|
26 | |
import javax.servlet.ServletContext; |
27 | |
import javax.servlet.http.HttpSession; |
28 | |
import javax.servlet.http.HttpSessionAttributeListener; |
29 | |
import javax.servlet.http.HttpSessionBindingEvent; |
30 | |
import javax.servlet.http.HttpSessionContext; |
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
public class MockHttpSession implements HttpSession { |
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
public MockHttpSession() { |
48 | |
|
49 | 18 | super(); |
50 | |
|
51 | 18 | } |
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
public MockHttpSession(ServletContext servletContext) { |
61 | |
|
62 | |
super(); |
63 | |
setServletContext(servletContext); |
64 | |
|
65 | |
} |
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
public void addAttributeListener(HttpSessionAttributeListener listener) { |
78 | |
attributeListeners.add(listener); |
79 | |
} |
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
public void setServletContext(ServletContext servletContext) { |
88 | |
|
89 | 18 | this.servletContext = servletContext; |
90 | |
|
91 | 18 | } |
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | 18 | private List attributeListeners = new ArrayList(); |
98 | 18 | private HashMap attributes = new HashMap(); |
99 | 18 | private String id = "123"; |
100 | 18 | private ServletContext servletContext = null; |
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
public void setId(String id) { |
112 | |
this.id = id; |
113 | |
} |
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
public Object getAttribute(String name) { |
121 | |
|
122 | 20 | return attributes.get(name); |
123 | |
|
124 | |
} |
125 | |
|
126 | |
|
127 | |
|
128 | |
public Enumeration getAttributeNames() { |
129 | |
|
130 | |
return new MockEnumeration(attributes.keySet().iterator()); |
131 | |
|
132 | |
} |
133 | |
|
134 | |
|
135 | |
|
136 | |
public long getCreationTime() { |
137 | |
|
138 | |
throw new UnsupportedOperationException(); |
139 | |
|
140 | |
} |
141 | |
|
142 | |
|
143 | |
|
144 | |
public String getId() { |
145 | |
|
146 | |
return this.id; |
147 | |
|
148 | |
} |
149 | |
|
150 | |
|
151 | |
|
152 | |
public long getLastAccessedTime() { |
153 | |
|
154 | |
throw new UnsupportedOperationException(); |
155 | |
|
156 | |
} |
157 | |
|
158 | |
|
159 | |
|
160 | |
public int getMaxInactiveInterval() { |
161 | |
|
162 | |
throw new UnsupportedOperationException(); |
163 | |
|
164 | |
} |
165 | |
|
166 | |
|
167 | |
|
168 | |
public ServletContext getServletContext() { |
169 | |
|
170 | |
return this.servletContext; |
171 | |
|
172 | |
} |
173 | |
|
174 | |
|
175 | |
|
176 | |
public HttpSessionContext getSessionContext() { |
177 | |
|
178 | |
throw new UnsupportedOperationException(); |
179 | |
|
180 | |
} |
181 | |
|
182 | |
|
183 | |
|
184 | |
public Object getValue(String name) { |
185 | |
|
186 | |
throw new UnsupportedOperationException(); |
187 | |
|
188 | |
} |
189 | |
|
190 | |
|
191 | |
|
192 | |
public String[] getValueNames() { |
193 | |
|
194 | |
throw new UnsupportedOperationException(); |
195 | |
|
196 | |
} |
197 | |
|
198 | |
|
199 | |
|
200 | |
public void invalidate() { |
201 | |
|
202 | |
throw new UnsupportedOperationException(); |
203 | |
|
204 | |
} |
205 | |
|
206 | |
|
207 | |
|
208 | |
public boolean isNew() { |
209 | |
|
210 | |
throw new UnsupportedOperationException(); |
211 | |
|
212 | |
} |
213 | |
|
214 | |
|
215 | |
|
216 | |
public void putValue(String name, Object value) { |
217 | |
|
218 | |
throw new UnsupportedOperationException(); |
219 | |
|
220 | |
} |
221 | |
|
222 | |
|
223 | |
|
224 | |
public void removeAttribute(String name) { |
225 | |
|
226 | |
if (attributes.containsKey(name)) { |
227 | |
Object value = attributes.remove(name); |
228 | |
fireAttributeRemoved(name, value); |
229 | |
} |
230 | |
|
231 | |
} |
232 | |
|
233 | |
|
234 | |
|
235 | |
public void removeValue(String name) { |
236 | |
|
237 | |
throw new UnsupportedOperationException(); |
238 | |
|
239 | |
} |
240 | |
|
241 | |
|
242 | |
|
243 | |
public void setAttribute(String name, Object value) { |
244 | |
|
245 | 9 | if (name == null) { |
246 | |
throw new IllegalArgumentException("Attribute name cannot be null"); |
247 | |
} |
248 | 9 | if (value == null) { |
249 | |
removeAttribute(name); |
250 | |
return; |
251 | |
} |
252 | 9 | if (attributes.containsKey(name)) { |
253 | |
Object oldValue = attributes.get(name); |
254 | |
attributes.put(name, value); |
255 | |
fireAttributeReplaced(name, oldValue); |
256 | |
} else { |
257 | 9 | attributes.put(name, value); |
258 | 9 | fireAttributeAdded(name, value); |
259 | |
} |
260 | |
|
261 | 9 | } |
262 | |
|
263 | |
|
264 | |
|
265 | |
public void setMaxInactiveInterval(int interval) { |
266 | |
|
267 | |
throw new UnsupportedOperationException(); |
268 | |
|
269 | |
} |
270 | |
|
271 | |
|
272 | |
|
273 | |
|
274 | |
|
275 | |
|
276 | |
|
277 | |
|
278 | |
|
279 | |
|
280 | |
|
281 | |
private void fireAttributeAdded(String key, Object value) { |
282 | 9 | if (attributeListeners.size() < 1) { |
283 | 9 | return; |
284 | |
} |
285 | |
HttpSessionBindingEvent event = |
286 | |
new HttpSessionBindingEvent(this, key, value); |
287 | |
Iterator listeners = attributeListeners.iterator(); |
288 | |
while (listeners.hasNext()) { |
289 | |
HttpSessionAttributeListener listener = |
290 | |
(HttpSessionAttributeListener) listeners.next(); |
291 | |
listener.attributeAdded(event); |
292 | |
} |
293 | |
} |
294 | |
|
295 | |
|
296 | |
|
297 | |
|
298 | |
|
299 | |
|
300 | |
|
301 | |
|
302 | |
private void fireAttributeRemoved(String key, Object value) { |
303 | |
if (attributeListeners.size() < 1) { |
304 | |
return; |
305 | |
} |
306 | |
HttpSessionBindingEvent event = |
307 | |
new HttpSessionBindingEvent(this, key, value); |
308 | |
Iterator listeners = attributeListeners.iterator(); |
309 | |
while (listeners.hasNext()) { |
310 | |
HttpSessionAttributeListener listener = |
311 | |
(HttpSessionAttributeListener) listeners.next(); |
312 | |
listener.attributeRemoved(event); |
313 | |
} |
314 | |
} |
315 | |
|
316 | |
|
317 | |
|
318 | |
|
319 | |
|
320 | |
|
321 | |
|
322 | |
|
323 | |
private void fireAttributeReplaced(String key, Object value) { |
324 | |
if (attributeListeners.size() < 1) { |
325 | |
return; |
326 | |
} |
327 | |
HttpSessionBindingEvent event = |
328 | |
new HttpSessionBindingEvent(this, key, value); |
329 | |
Iterator listeners = attributeListeners.iterator(); |
330 | |
while (listeners.hasNext()) { |
331 | |
HttpSessionAttributeListener listener = |
332 | |
(HttpSessionAttributeListener) listeners.next(); |
333 | |
listener.attributeReplaced(event); |
334 | |
} |
335 | |
} |
336 | |
|
337 | |
|
338 | |
} |