1   /*
2    * Copyright 2004-2010 the Seasar Foundation and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  
17  package org.seasar.cubby.unit;
18  
19  import static org.easymock.EasyMock.anyObject;
20  import static org.easymock.EasyMock.createMock;
21  import static org.easymock.EasyMock.expect;
22  import static org.easymock.EasyMock.expectLastCall;
23  import static org.easymock.EasyMock.getCurrentArguments;
24  import static org.easymock.EasyMock.isA;
25  import static org.easymock.EasyMock.replay;
26  import static org.easymock.EasyMock.verify;
27  import static org.junit.Assert.assertEquals;
28  import static org.junit.Assert.assertSame;
29  
30  import java.util.Arrays;
31  import java.util.Collection;
32  import java.util.HashMap;
33  import java.util.Hashtable;
34  import java.util.Map;
35  
36  import javax.servlet.ServletContext;
37  import javax.servlet.http.HttpServletRequest;
38  import javax.servlet.http.HttpServletResponse;
39  
40  import org.easymock.IAnswer;
41  import org.junit.After;
42  import org.junit.Before;
43  import org.junit.Test;
44  import org.seasar.cubby.action.ActionContext;
45  import org.seasar.cubby.action.ActionResult;
46  import org.seasar.cubby.action.RequestParameter;
47  import org.seasar.cubby.controller.RequestParser;
48  import org.seasar.cubby.controller.impl.DefaultRequestParser;
49  import org.seasar.cubby.plugin.PluginRegistry;
50  import org.seasar.cubby.plugins.BinderPlugin;
51  import org.seasar.cubby.routing.PathResolver;
52  import org.seasar.cubby.routing.impl.PathResolverImpl;
53  import org.seasar.cubby.routing.impl.PathTemplateParserImpl;
54  import org.seasar.cubby.spi.BeanDescProvider;
55  import org.seasar.cubby.spi.ContainerProvider;
56  import org.seasar.cubby.spi.ConverterProvider;
57  import org.seasar.cubby.spi.PathResolverProvider;
58  import org.seasar.cubby.spi.RequestParserProvider;
59  import org.seasar.cubby.spi.beans.impl.DefaultBeanDescProvider;
60  import org.seasar.cubby.spi.container.Container;
61  import org.seasar.cubby.spi.container.LookupException;
62  import org.seasar.cubby.spi.impl.AbstractRequestParserProvider;
63  
64  public class CubbyRunnerTest {
65  
66  	private PluginRegistry pluginRegistry = PluginRegistry.getInstance();
67  
68  	private final Hashtable<String, Object> attributes = new Hashtable<String, Object>();
69  
70  	private final Map<String, String[]> parameterMap = new HashMap<String, String[]>();
71  
72  	private MockAction mockAction = new MockAction();
73  
74  	@Before
75  	public void before() {
76  		PathResolver pathResolver = new PathResolverImpl(
77  				new PathTemplateParserImpl());
78  		pathResolver.add(MockAction.class);
79  
80  		BinderPlugin binderPlugin = new BinderPlugin();
81  		binderPlugin.bind(RequestParserProvider.class).toInstance(
82  				new AbstractRequestParserProvider() {
83  
84  					@Override
85  					protected Collection<RequestParser> getRequestParsers() {
86  						return Arrays
87  								.asList(new RequestParser[]{new DefaultRequestParser()});
88  					}
89  
90  				});
91  		binderPlugin.bind(ContainerProvider.class).toInstance(
92  				new MockContainerProvider(new Container() {
93  
94  					public <T> T lookup(Class<T> type) throws LookupException {
95  						if (MockAction.class.equals(type)) {
96  							return type.cast(mockAction);
97  						}
98  						throw new LookupException("type:" + type);
99  					}
100 
101 				}));
102 		binderPlugin.bind(BeanDescProvider.class).toInstance(
103 				new DefaultBeanDescProvider());
104 		binderPlugin.bind(ConverterProvider.class).toInstance(
105 				new MockConverterProvider());
106 		binderPlugin.bind(PathResolverProvider.class).toInstance(
107 				new MockPathResolverProvider(pathResolver));
108 		pluginRegistry.register(binderPlugin);
109 	}
110 
111 	@After
112 	public void after() {
113 		pluginRegistry.clear();
114 	}
115 
116 	@Test
117 	public void processAction() throws Exception {
118 		parameterMap.put("name", new String[]{"abcdefg"});
119 
120 		HttpServletRequest request = createMock(HttpServletRequest.class);
121 		request.setAttribute(isA(String.class), anyObject());
122 		expectLastCall().andStubAnswer(new IAnswer<Void>() {
123 
124 			public Void answer() throws Throwable {
125 				attributes.put((String) getCurrentArguments()[0],
126 						getCurrentArguments()[1]);
127 				return null;
128 			}
129 
130 		});
131 		expect(request.getAttribute(isA(String.class))).andStubAnswer(
132 				new IAnswer<Object>() {
133 
134 					public Object answer() throws Throwable {
135 						return attributes.get(getCurrentArguments()[0]);
136 					}
137 
138 				});
139 		request.removeAttribute(isA(String.class));
140 		expectLastCall().andStubAnswer(new IAnswer<Void>() {
141 
142 			public Void answer() throws Throwable {
143 				attributes.remove(getCurrentArguments()[0]);
144 				return null;
145 			}
146 
147 		});
148 		expect(request.getServletPath()).andReturn("/mock/execute");
149 		expect(request.getPathInfo()).andReturn(null);
150 		expect(request.getParameterMap()).andReturn(parameterMap);
151 		expect(request.getRequestURI()).andReturn("/context/mock/execute");
152 		expect(request.getMethod()).andReturn("GET");
153 		expect(request.getCharacterEncoding()).andReturn("UTF-8");
154 		expect(request.getSession(false)).andReturn(null);
155 		HttpServletResponse response = createMock(HttpServletResponse.class);
156 		ActionResult actionResult = createMock(ActionResult.class);
157 		replay(request, response, actionResult);
158 
159 		mockAction.setActionResult(actionResult);
160 		ActionResult actual = CubbyRunner.processAction(request, response);
161 		assertSame(actionResult, actual);
162 		assertEquals("abcdefg", mockAction.getName());
163 
164 		verify(request, response, actionResult);
165 	}
166 
167 	@Test
168 	public void processActionWithServletContext() throws Exception {
169 		parameterMap.put("name", new String[]{"abcdefg"});
170 
171 		HttpServletRequest request = createMock(HttpServletRequest.class);
172 		request.setAttribute(isA(String.class), anyObject());
173 		expectLastCall().andStubAnswer(new IAnswer<Void>() {
174 
175 			public Void answer() throws Throwable {
176 				attributes.put((String) getCurrentArguments()[0],
177 						getCurrentArguments()[1]);
178 				return null;
179 			}
180 
181 		});
182 		expect(request.getAttribute(isA(String.class))).andStubAnswer(
183 				new IAnswer<Object>() {
184 
185 					public Object answer() throws Throwable {
186 						return attributes.get(getCurrentArguments()[0]);
187 					}
188 
189 				});
190 		request.removeAttribute(isA(String.class));
191 		expectLastCall().andStubAnswer(new IAnswer<Void>() {
192 
193 			public Void answer() throws Throwable {
194 				attributes.remove(getCurrentArguments()[0]);
195 				return null;
196 			}
197 
198 		});
199 		expect(request.getServletPath()).andReturn("/mock/execute");
200 		expect(request.getPathInfo()).andReturn(null);
201 		expect(request.getParameterMap()).andReturn(parameterMap);
202 		expect(request.getRequestURI()).andReturn("/context/mock/execute");
203 		expect(request.getMethod()).andReturn("GET");
204 		expect(request.getCharacterEncoding()).andReturn("UTF-8");
205 		expect(request.getSession(false)).andReturn(null);
206 		HttpServletResponse response = createMock(HttpServletResponse.class);
207 		ServletContext servletContext = createMock(ServletContext.class);
208 		ActionResult actionResult = createMock(ActionResult.class);
209 		replay(request, response, servletContext, actionResult);
210 
211 		mockAction.setActionResult(actionResult);
212 		ActionResult actual = CubbyRunner.processAction(servletContext,
213 				request, response);
214 		assertSame(actionResult, actual);
215 		assertEquals("abcdefg", mockAction.getName());
216 
217 		verify(request, response, servletContext, actionResult);
218 	}
219 
220 	@Test
221 	public void processActionAndExecuteActionResult() throws Exception {
222 		parameterMap.put("name", new String[] { "abcdefg" });
223 
224 		HttpServletRequest request = createMock(HttpServletRequest.class);
225 		request.setAttribute(isA(String.class), anyObject());
226 		expectLastCall().andStubAnswer(new IAnswer<Void>() {
227 
228 			public Void answer() throws Throwable {
229 				attributes.put((String) getCurrentArguments()[0],
230 						getCurrentArguments()[1]);
231 				return null;
232 			}
233 
234 		});
235 		expect(request.getAttribute(isA(String.class))).andStubAnswer(
236 				new IAnswer<Object>() {
237 
238 					public Object answer() throws Throwable {
239 						return attributes.get(getCurrentArguments()[0]);
240 					}
241 
242 				});
243 		request.removeAttribute(isA(String.class));
244 		expectLastCall().andStubAnswer(new IAnswer<Void>() {
245 
246 			public Void answer() throws Throwable {
247 				attributes.remove(getCurrentArguments()[0]);
248 				return null;
249 			}
250 
251 		});
252 		expect(request.getServletPath()).andReturn("/mock/execute");
253 		expect(request.getPathInfo()).andReturn(null);
254 		expect(request.getParameterMap()).andReturn(parameterMap);
255 		expect(request.getRequestURI()).andReturn("/context/mock/execute");
256 		expect(request.getMethod()).andReturn("GET");
257 		expect(request.getCharacterEncoding()).andReturn("UTF-8");
258 		expect(request.getSession(false)).andReturn(null);
259 		HttpServletResponse response = createMock(HttpServletResponse.class);
260 		ActionResult actionResult = createMock(ActionResult.class);
261 		actionResult.execute(isA(ActionContext.class),
262 				isA(HttpServletRequest.class), isA(HttpServletResponse.class));
263 		replay(request, response, actionResult);
264 
265 		mockAction.setActionResult(actionResult);
266 		ActionResult actual = CubbyRunner.processActionAndExecuteActionResult(
267 				request, response);
268 
269 		assertSame(actionResult, actual);
270 		assertEquals("abcdefg", mockAction.getName());
271 
272 		verify(request, response, actionResult);
273 	}
274 
275 	@Test
276 	public void processActionAndExecuteActionResultWithServletContext()
277 			throws Exception {
278 		parameterMap.put("name", new String[] { "abcdefg" });
279 
280 		HttpServletRequest request = createMock(HttpServletRequest.class);
281 		request.setAttribute(isA(String.class), anyObject());
282 		expectLastCall().andStubAnswer(new IAnswer<Void>() {
283 
284 			public Void answer() throws Throwable {
285 				attributes.put((String) getCurrentArguments()[0],
286 						getCurrentArguments()[1]);
287 				return null;
288 			}
289 
290 		});
291 		expect(request.getAttribute(isA(String.class))).andStubAnswer(
292 				new IAnswer<Object>() {
293 
294 					public Object answer() throws Throwable {
295 						return attributes.get(getCurrentArguments()[0]);
296 					}
297 
298 				});
299 		request.removeAttribute(isA(String.class));
300 		expectLastCall().andStubAnswer(new IAnswer<Void>() {
301 
302 			public Void answer() throws Throwable {
303 				attributes.remove(getCurrentArguments()[0]);
304 				return null;
305 			}
306 
307 		});
308 		expect(request.getServletPath()).andReturn("/mock/execute");
309 		expect(request.getPathInfo()).andReturn(null);
310 		expect(request.getParameterMap()).andReturn(parameterMap);
311 		expect(request.getRequestURI()).andReturn("/context/mock/execute");
312 		expect(request.getMethod()).andReturn("GET");
313 		expect(request.getCharacterEncoding()).andReturn("UTF-8");
314 		expect(request.getSession(false)).andReturn(null);
315 		HttpServletResponse response = createMock(HttpServletResponse.class);
316 		ServletContext servletContext = createMock(ServletContext.class);
317 		ActionResult actionResult = createMock(ActionResult.class);
318 		actionResult.execute(isA(ActionContext.class),
319 				isA(HttpServletRequest.class), isA(HttpServletResponse.class));
320 		replay(request, response, servletContext, actionResult);
321 
322 		mockAction.setActionResult(actionResult);
323 		ActionResult actual = CubbyRunner.processActionAndExecuteActionResult(
324 				servletContext, request, response);
325 		assertSame(actionResult, actual);
326 		assertEquals("abcdefg", mockAction.getName());
327 
328 		verify(request, response, servletContext, actionResult);
329 	}
330 
331 	public static class MockAction {
332 		private String name;
333 
334 		private ActionResult actionResult;
335 
336 		@RequestParameter
337 		public void setName(String name) {
338 			this.name = name;
339 		}
340 
341 		public String getName() {
342 			return name;
343 		}
344 
345 		public void setActionResult(ActionResult actionResult) {
346 			this.actionResult = actionResult;
347 		}
348 
349 		public ActionResult execute() {
350 			return actionResult;
351 		}
352 	}
353 
354 }