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