1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.seasar.cubby.internal.controller.impl;
17
18 import static org.easymock.EasyMock.anyObject;
19 import static org.easymock.EasyMock.createMock;
20 import static org.easymock.EasyMock.expect;
21 import static org.easymock.EasyMock.expectLastCall;
22 import static org.easymock.EasyMock.getCurrentArguments;
23 import static org.easymock.EasyMock.isA;
24 import static org.easymock.EasyMock.replay;
25 import static org.junit.Assert.assertArrayEquals;
26 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.assertFalse;
28 import static org.junit.Assert.assertNull;
29 import static org.junit.Assert.assertSame;
30 import static org.junit.Assert.assertTrue;
31
32 import java.util.ArrayList;
33 import java.util.Collection;
34 import java.util.Enumeration;
35 import java.util.HashMap;
36 import java.util.Hashtable;
37 import java.util.List;
38 import java.util.Map;
39
40 import javax.servlet.http.HttpServletRequest;
41 import javax.servlet.http.HttpServletResponse;
42
43 import org.easymock.IAnswer;
44 import org.junit.After;
45 import org.junit.Before;
46 import org.junit.Test;
47 import org.seasar.cubby.CubbyConstants;
48 import org.seasar.cubby.action.Action;
49 import org.seasar.cubby.controller.MessagesBehaviour;
50 import org.seasar.cubby.controller.impl.DefaultMessagesBehaviour;
51 import org.seasar.cubby.internal.controller.ThreadContext;
52 import org.seasar.cubby.internal.controller.ThreadContext.Command;
53 import org.seasar.cubby.mock.MockContainerProvider;
54 import org.seasar.cubby.plugin.PluginRegistry;
55 import org.seasar.cubby.plugins.BinderPlugin;
56 import org.seasar.cubby.spi.BeanDescProvider;
57 import org.seasar.cubby.spi.ContainerProvider;
58 import org.seasar.cubby.spi.beans.impl.DefaultBeanDescProvider;
59 import org.seasar.cubby.spi.container.Container;
60 import org.seasar.cubby.spi.container.LookupException;
61
62
63
64
65
66 public class CubbyHttpServletRequestWrapperTest {
67
68 private final PluginRegistry pluginRegistry = PluginRegistry.getInstance();
69
70 private HttpServletRequest request;
71
72 private HttpServletResponse response;
73
74 private Hashtable<String, String[]> parameters = new Hashtable<String, String[]>();
75
76 @Before
77 public void setupProvider() {
78 final BinderPlugin binderPlugin = new BinderPlugin();
79 binderPlugin.bind(BeanDescProvider.class).toInstance(
80 new DefaultBeanDescProvider());
81 final MessagesBehaviour messagesBehaviour = new DefaultMessagesBehaviour();
82 binderPlugin.bind(ContainerProvider.class).toInstance(
83 new MockContainerProvider(new Container() {
84
85 public <T> T lookup(Class<T> type) throws LookupException {
86 if (MessagesBehaviour.class.equals(type)) {
87 return type.cast(messagesBehaviour);
88 }
89 throw new LookupException(type.getName());
90 }
91
92 }));
93 pluginRegistry.register(binderPlugin);
94 }
95
96 @After
97 public void teardownProvider() {
98 pluginRegistry.clear();
99 }
100
101 @Before
102 @SuppressWarnings("unchecked")
103 public void setupRequest() {
104 final Hashtable<String, Object> attributes = new Hashtable<String, Object>();
105 request = createMock(HttpServletRequest.class);
106 expect(request.getContextPath()).andStubReturn("/context");
107 expect(request.getLocale()).andStubReturn(null);
108 expect(request.getAttribute(String.class.cast(anyObject())))
109 .andStubAnswer(new IAnswer<Object>() {
110
111 public Object answer() throws Throwable {
112 return attributes.get(getCurrentArguments()[0]);
113 }
114
115 });
116 request.setAttribute(String.class.cast(anyObject()), anyObject());
117 expectLastCall().andStubAnswer(new IAnswer<Object>() {
118
119 public Object answer() throws Throwable {
120 attributes.put(String.class.cast(getCurrentArguments()[0]),
121 getCurrentArguments()[1]);
122 return null;
123 }
124
125 });
126 expect(request.getAttributeNames()).andStubAnswer(
127 new IAnswer<Enumeration>() {
128
129 public Enumeration answer() throws Throwable {
130 return attributes.keys();
131 }
132
133 });
134 expect(request.getParameter(isA(String.class))).andStubAnswer(
135 new IAnswer<String>() {
136
137 public String answer() throws Throwable {
138 return parameters.get(String.class
139 .cast(getCurrentArguments()[0]))[0];
140 }
141
142 });
143 expect(request.getParameterMap()).andStubAnswer(new IAnswer<Map>() {
144
145 public Map answer() throws Throwable {
146 return parameters;
147 }
148
149 });
150 expect(request.getParameterNames()).andAnswer(
151 new IAnswer<Enumeration>() {
152
153 public Enumeration answer() throws Throwable {
154 return parameters.keys();
155 }
156
157 });
158 expect(request.getParameterValues(isA(String.class))).andAnswer(
159 new IAnswer<String[]>() {
160
161 public String[] answer() throws Throwable {
162 return parameters.get(String.class
163 .cast(getCurrentArguments()[0]));
164 }
165
166 });
167 response = createMock(HttpServletResponse.class);
168 replay(request, response);
169 }
170
171 @Test
172 public void getAttributeNames() throws Exception {
173 request.setAttribute("a1", "a1");
174 Collection<String> origNames = toCollection(request.getAttributeNames());
175 assertTrue("追加済みの属性", origNames.contains("a1"));
176 assertFalse("存在しない属性", origNames.contains("a2"));
177 assertFalse("ラップ後の追加の属性", origNames
178 .contains(CubbyConstants.ATTR_CONTEXT_PATH));
179 assertFalse("ラップ後の追加の属性", origNames
180 .contains(CubbyConstants.ATTR_MESSAGES));
181 assertFalse("ラップ後の追加の属性", origNames
182 .contains(CubbyConstants.ATTR_ACTION));
183
184 CubbyHttpServletRequestWrapper wrapper = new CubbyHttpServletRequestWrapper(
185 request, new HashMap<String, String[]>());
186 Action action = new MockAction();
187 wrapper.setAttribute(CubbyConstants.ATTR_ACTION, action);
188
189 Collection<String> wrappedNames = toCollection(wrapper
190 .getAttributeNames());
191 assertTrue("追加済みの属性", origNames.contains("a1"));
192 assertFalse("存在しない属性", origNames.contains("a2"));
193 assertTrue("ラップ後の追加の属性", wrappedNames
194 .contains(CubbyConstants.ATTR_CONTEXT_PATH));
195 assertTrue("ラップ後の追加の属性", wrappedNames
196 .contains(CubbyConstants.ATTR_MESSAGES));
197 assertTrue("ラップ後の追加の属性", wrappedNames
198 .contains(CubbyConstants.ATTR_ACTION));
199 }
200
201 private Collection<String> toCollection(Enumeration<?> attributeNames) {
202 List<String> names = new ArrayList<String>();
203 while (attributeNames.hasMoreElements()) {
204 names.add((String) attributeNames.nextElement());
205 }
206 return names;
207 }
208
209 @Test
210 public void getAttribute() throws Exception {
211 ThreadContext.runInContext(request, response, new Command() {
212
213 public void execute(final HttpServletRequest request,
214 final HttpServletResponse response) throws Exception {
215 CubbyHttpServletRequestWrapper wrapper = new CubbyHttpServletRequestWrapper(
216 request, new HashMap<String, String[]>());
217
218 assertEquals("/context", wrapper
219 .getAttribute(CubbyConstants.ATTR_CONTEXT_PATH));
220 assertEquals(ThreadContext.getMessagesMap(), wrapper
221 .getAttribute(CubbyConstants.ATTR_MESSAGES));
222
223 assertNull(wrapper.getAttribute("name"));
224 Action action = new MockAction();
225 wrapper.setAttribute(CubbyConstants.ATTR_ACTION, action);
226 assertSame(action, wrapper
227 .getAttribute(CubbyConstants.ATTR_ACTION));
228 assertEquals("expect name", wrapper.getAttribute("name"));
229 assertNull(wrapper.getAttribute("value"));
230 assertNull(wrapper.getAttribute("noprop"));
231 }
232
233 });
234 }
235
236 @Test
237 public void parameter() {
238 parameters.put("abc", new String[] { "value1" });
239 parameters.put("def", new String[] { "value2" });
240
241 Map<String, String[]> uriParameterMap = new HashMap<String, String[]>();
242 uriParameterMap.put("abc", new String[] { "value3" });
243 uriParameterMap.put("ghi", new String[] { "value4" });
244
245 CubbyHttpServletRequestWrapper wrapper = new CubbyHttpServletRequestWrapper(
246 request, uriParameterMap);
247
248 Hashtable<String, String[]> expects = new Hashtable<String, String[]>();
249 expects.put("abc", new String[] { "value1", "value3" });
250 expects.put("def", new String[] { "value2" });
251 expects.put("ghi", new String[] { "value4" });
252
253 @SuppressWarnings("unchecked")
254 Enumeration parameterNames = wrapper.getParameterNames();
255 while (parameterNames.hasMoreElements()) {
256 String parameterName = (String) parameterNames.nextElement();
257 assertArrayEquals(expects.get(parameterName), wrapper
258 .getParameterValues(parameterName));
259 assertEquals(expects.get(parameterName)[0], wrapper
260 .getParameter(parameterName));
261 expects.remove(parameterName);
262 }
263 assertTrue(expects.isEmpty());
264
265 assertNull(wrapper.getParameter("jkl"));
266
267 @SuppressWarnings("unchecked")
268 Map<String, String[]> parameterMap = wrapper.getParameterMap();
269 assertEquals(3, parameterMap.size());
270 assertArrayEquals(new String[] { "value1", "value3" }, parameterMap
271 .get("abc"));
272 assertArrayEquals(new String[] { "value2" }, parameterMap.get("def"));
273 assertArrayEquals(new String[] { "value4" }, parameterMap.get("ghi"));
274
275 }
276
277 public static class MockAction extends Action {
278 public String getName() {
279 return "expect name";
280 }
281
282 public void setValue(String value) {
283 }
284 }
285
286 }