2009/05/20 - Apache Shale has been retired.
For more information, please explore the Attic.
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 * <p>Mock impementation of <code>Map</code> for the session scope
34 * attributes managed by {@link MockExternalContext}.</p>
35 *
36 * $Id$
37 */
38
39 class MockSessionMap implements Map {
40
41
42
43
44
45 /***
46 * <p>Construct an instance exposing the attributes of the specified
47 * session.</p>
48 *
49 * @param session Session whose attributes are to be exposed
50 * as a Map
51 */
52 public MockSessionMap(HttpSession session) {
53
54 this.session = session;
55
56 }
57
58
59
60
61
62
63
64
65 /***
66 * <p>The session whose attributes we are exposing as a Map.</p>
67 */
68 private HttpSession session = null;
69
70
71
72
73
74 /*** {@inheritDoc} */
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 /*** {@inheritDoc} */
86 public boolean containsKey(Object key) {
87
88 return session.getAttribute(key(key)) != null;
89
90 }
91
92
93 /*** {@inheritDoc} */
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 /*** {@inheritDoc} */
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 /*** {@inheritDoc} */
125 public boolean equals(Object o) {
126
127 return session.equals(o);
128
129 }
130
131
132 /*** {@inheritDoc} */
133 public Object get(Object key) {
134
135 return session.getAttribute(key(key));
136
137 }
138
139
140 /*** {@inheritDoc} */
141 public int hashCode() {
142
143 return session.hashCode();
144
145 }
146
147
148 /*** {@inheritDoc} */
149 public boolean isEmpty() {
150
151 return size() < 1;
152
153 }
154
155
156 /*** {@inheritDoc} */
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 /*** {@inheritDoc} */
170 public Object put(Object key, Object value) {
171
172 if (value == null) {
173 return remove(key);
174 }
175 String skey = key(key);
176 Object previous = session.getAttribute(skey);
177 session.setAttribute(skey, value);
178 return previous;
179
180 }
181
182
183 /*** {@inheritDoc} */
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 /*** {@inheritDoc} */
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 /*** {@inheritDoc} */
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 /*** {@inheritDoc} */
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 * <p>Return the specified key, after converting it to a String.</p>
238 *
239 * @param key Key to be converted
240 */
241 private String key(Object key) {
242
243 if (key == null) {
244 throw new IllegalArgumentException();
245 } else if (key instanceof String) {
246 return ((String) key);
247 } else {
248 return (key.toString());
249 }
250
251 }
252
253
254 }