1   /*
2    * Copyright 2004-2008 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  package org.seasar.cubby.action;
17  
18  import java.io.IOException;
19  import java.lang.reflect.Method;
20  import java.util.HashMap;
21  import java.util.LinkedHashMap;
22  import java.util.Map;
23  
24  import javax.servlet.http.HttpServletResponse;
25  import javax.servlet.http.HttpServletResponseWrapper;
26  
27  import org.seasar.cubby.exception.ActionRuntimeException;
28  import org.seasar.extension.unit.S2TestCase;
29  import org.seasar.framework.mock.servlet.MockHttpServletRequest;
30  import org.seasar.framework.mock.servlet.MockHttpServletResponse;
31  import org.seasar.framework.mock.servlet.MockServletContext;
32  import org.seasar.framework.util.ClassUtil;
33  
34  public class RedirectTest extends S2TestCase {
35  
36  	public MockAction action;
37  
38  	@Override
39  	protected void setUp() throws Exception {
40  		include(this.getClass().getName().replaceAll("\\.", "/") + ".dicon");
41  	}
42  
43  	public void testBasicSequence() throws Exception {
44  		final MockServletContext servletContext = this.getServletContext();
45  		servletContext.setServletContextName("/cubby");
46  		final MockHttpServletRequest request = this.getRequest();
47  		final MockHttpServletResponse response = this.getResponse();
48  		final Method method = ClassUtil.getMethod(action.getClass(), "dummy1",
49  				null);
50  
51  		final Redirect redirect = new Redirect("path.jsp");
52  		assertFalse(action.isPrerendered());
53  		redirect.execute(action, MockAction.class, method, request,
54  				new RequestDispatcherAssertionWrapper(response, new Asserter() {
55  					public void assertDispatchPath(final String path) {
56  						assertEquals("/cubby/mock/path.jsp", path);
57  					}
58  				}));
59  		assertFalse(action.isPrerendered());
60  		assertFalse(action.isPostrendered());
61  	}
62  
63  	public void testBasicSequenceWithProtocol() throws Exception {
64  		final MockServletContext servletContext = this.getServletContext();
65  		servletContext.setServletContextName("/cubby");
66  		final MockHttpServletRequest request = this.getRequest();
67  		final MockHttpServletResponse response = this.getResponse();
68  		final Method method = ClassUtil.getMethod(action.getClass(), "dummy1",
69  				null);
70  
71  		final Redirect redirect = new Redirect("path.jsp", "https");
72  		assertFalse(action.isPrerendered());
73  		redirect.execute(action, MockAction.class, method, request,
74  				new RequestDispatcherAssertionWrapper(response, new Asserter() {
75  					public void assertDispatchPath(final String path) {
76  						assertEquals("https://localhost/cubby/mock/path.jsp",
77  								path);
78  					}
79  				}));
80  		assertFalse(action.isPrerendered());
81  		assertFalse(action.isPostrendered());
82  	}
83  
84  	public void testBasicSequenceWithProtocolAndPort() throws Exception {
85  		final MockServletContext servletContext = this.getServletContext();
86  		servletContext.setServletContextName("/cubby");
87  		final MockHttpServletRequest request = this.getRequest();
88  		final MockHttpServletResponse response = this.getResponse();
89  		final Method method = ClassUtil.getMethod(action.getClass(), "dummy1",
90  				null);
91  
92  		final Redirect redirect = new Redirect("path.jsp", "http", 8080);
93  		assertFalse(action.isPrerendered());
94  		redirect.execute(action, MockAction.class, method, request,
95  				new RequestDispatcherAssertionWrapper(response, new Asserter() {
96  					public void assertDispatchPath(final String path) {
97  						assertEquals(
98  								"http://localhost:8080/cubby/mock/path.jsp",
99  								path);
100 					}
101 				}));
102 		assertFalse(action.isPrerendered());
103 		assertFalse(action.isPostrendered());
104 	}
105 
106 	public void testRelativePath() throws Exception {
107 		final MockServletContext servletContext = this.getServletContext();
108 		servletContext.setServletContextName("/cubby");
109 		final MockHttpServletRequest request = this.getRequest();
110 		final MockHttpServletResponse response = this.getResponse();
111 		final Method method = ClassUtil.getMethod(action.getClass(), "dummy1",
112 				null);
113 
114 		final Redirect redirect = new Redirect("page.jsp");
115 		redirect.execute(action, MockAction.class, method, request,
116 				new RequestDispatcherAssertionWrapper(response, new Asserter() {
117 					public void assertDispatchPath(final String path) {
118 						assertEquals("/cubby/mock/page.jsp", path);
119 					}
120 				}));
121 	}
122 
123 	public void testRelativePathWithProtocol() throws Exception {
124 		final MockServletContext servletContext = this.getServletContext();
125 		servletContext.setServletContextName("/cubby");
126 		final MockHttpServletRequest request = this.getRequest();
127 		final MockHttpServletResponse response = this.getResponse();
128 		final Method method = ClassUtil.getMethod(action.getClass(), "dummy1",
129 				null);
130 
131 		final Redirect redirect = new Redirect("page.jsp", "https");
132 		redirect.execute(action, MockAction.class, method, request,
133 				new RequestDispatcherAssertionWrapper(response, new Asserter() {
134 					public void assertDispatchPath(final String path) {
135 						assertEquals("https://localhost/cubby/mock/page.jsp",
136 								path);
137 					}
138 				}));
139 	}
140 
141 	public void testAbsolutePath() throws Exception {
142 		final MockServletContext servletContext = this.getServletContext();
143 		servletContext.setServletContextName("/cubby");
144 		final MockHttpServletRequest request = this.getRequest();
145 		final MockHttpServletResponse response = this.getResponse();
146 		final Method method = ClassUtil.getMethod(action.getClass(), "dummy1",
147 				null);
148 
149 		final Redirect redirect = new Redirect("/absolute/path.jsp");
150 		redirect.execute(action, MockAction.class, method, request,
151 				new RequestDispatcherAssertionWrapper(response, new Asserter() {
152 					public void assertDispatchPath(final String path) {
153 						assertEquals("/cubby/absolute/path.jsp", path);
154 					}
155 				}));
156 	}
157 
158 	public void testAbsolutePathWithProtocol() throws Exception {
159 		final MockServletContext servletContext = this.getServletContext();
160 		servletContext.setServletContextName("/cubby");
161 		final MockHttpServletRequest request = this.getRequest();
162 		final MockHttpServletResponse response = this.getResponse();
163 		final Method method = ClassUtil.getMethod(action.getClass(), "dummy1",
164 				null);
165 
166 		final Redirect redirect = new Redirect("/absolute/path.jsp", "https");
167 		redirect.execute(action, MockAction.class, method, request,
168 				new RequestDispatcherAssertionWrapper(response, new Asserter() {
169 					public void assertDispatchPath(final String path) {
170 						assertEquals(
171 								"https://localhost/cubby/absolute/path.jsp",
172 								path);
173 					}
174 				}));
175 	}
176 
177 	public void testAbsoluteURL() throws Exception {
178 		final MockServletContext servletContext = this.getServletContext();
179 		servletContext.setServletContextName("/cubby");
180 		final MockHttpServletRequest request = this.getRequest();
181 		final MockHttpServletResponse response = this.getResponse();
182 		final Method method = ClassUtil.getMethod(action.getClass(), "dummy1",
183 				null);
184 
185 		final Redirect redirect = new Redirect("http://example.com/");
186 		redirect.execute(action, MockAction.class, method, request,
187 				new RequestDispatcherAssertionWrapper(response, new Asserter() {
188 					public void assertDispatchPath(final String path) {
189 						assertEquals("http://example.com/", path);
190 					}
191 				}));
192 	}
193 
194 	public void testRootContextPath() throws Exception {
195 		final MockServletContext servletContext = this.getServletContext();
196 		servletContext.setServletContextName("/");
197 		final MockHttpServletRequest request = this.getRequest();
198 		final MockHttpServletResponse response = this.getResponse();
199 		final Method method = ClassUtil.getMethod(action.getClass(), "dummy1",
200 				null);
201 
202 		final Redirect redirect = new Redirect("path.jsp");
203 		redirect.execute(action, MockAction.class, method, request,
204 				new RequestDispatcherAssertionWrapper(response, new Asserter() {
205 					public void assertDispatchPath(final String path) {
206 						assertEquals("/mock/path.jsp", path);
207 					}
208 				}));
209 	}
210 
211 	public void testRedirectByClassAndMethod1() throws Exception {
212 		final Redirect redirect = new Redirect(MockAction.class, "dummy1");
213 		assertEquals("/routing/test", redirect.getPath());
214 	}
215 
216 	public void testRedirectByClassAndMethod2() throws Exception {
217 		final Map<String, String[]> values = new LinkedHashMap<String, String[]>();
218 		values.put("value1", new String[] { "123" });
219 		values.put("value2", new String[] { "456" });
220 
221 		final Redirect redirect = new Redirect(MockAction.class, "dummy1",
222 				values);
223 		assertEquals("/routing/test?value1=123&value2=456", redirect.getPath());
224 	}
225 
226 	public void testRedirectByClassAndMethod3() throws Exception {
227 		final Map<String, String[]> values = new LinkedHashMap<String, String[]>();
228 		values.put("value1", new String[] { "123" });
229 		values.put("value2", new String[] { "456" });
230 		final Redirect redirect = new Redirect(MockAction.class, "dummy2",
231 				values);
232 		assertEquals("/routing/test/123/456", redirect.getPath());
233 	}
234 
235 	public void testRedirectByClassAndMethod4() throws Exception {
236 		final Map<String, String[]> values = new LinkedHashMap<String, String[]>();
237 		values.put("value1", new String[] { "123" });
238 		values.put("value2", new String[] { "456" });
239 		values.put("value3", new String[] { "789" });
240 		final Redirect redirect = new Redirect(MockAction.class, "dummy2",
241 				values);
242 		assertEquals("/routing/test/123/456?value3=789", redirect.getPath());
243 	}
244 
245 	public void testRedirectByClassAndMethodFailureNoRouting() throws Exception {
246 		try {
247 			new Redirect(MockAction.class, "none").getPath();
248 			fail();
249 		} catch (final ActionRuntimeException e) {
250 			// ok
251 		}
252 	}
253 
254 	public void testRedirectByClassAndMethodFailureLessParameter()
255 			throws Exception {
256 		try {
257 			new Redirect(MockAction.class, "dummy2").getPath();
258 			fail();
259 		} catch (final ActionRuntimeException e) {
260 			// ok
261 		}
262 	}
263 
264 	public void testRedirectByClassAndMethodFailureUnmatchParameter()
265 			throws Exception {
266 		final Map<String, String[]> values = new LinkedHashMap<String, String[]>();
267 		values.put("value1", new String[] { "abc" });
268 		values.put("value2", new String[] { "456" });
269 		try {
270 			new Redirect(MockAction.class, "dummy2", values).getPath();
271 			fail();
272 		} catch (final ActionRuntimeException e) {
273 			// ok
274 		}
275 	}
276 
277 	public void testGetPath() throws Exception {
278 		final Redirect redirect = new Redirect("/absolute/redirect");
279 		assertEquals("/absolute/redirect", redirect.getPath());
280 	}
281 
282 	public void testParam1() throws Exception {
283 		final Redirect redirect = new Redirect(MockAction.class, "dummy1")
284 			.param("value1", "123")
285 			.param("value2", "456");
286 		assertEquals("/routing/test?value1=123&value2=456", redirect.getPath());
287 	}
288 
289 	public void testParam2() throws Exception {
290 		Map<String, String[]> params = new HashMap<String, String[]>();
291 		params.put("value1", new String[] { "123" });
292 		final Redirect redirect = new Redirect(MockAction.class, "dummy1", params)
293 			.param("value2", "456");
294 		assertEquals("/routing/test?value1=123&value2=456", redirect.getPath());
295 	}
296 
297 	public void testParam3() throws Exception {
298 		Redirect redirect = new Redirect("hoge").param("value1", "123")
299 			.param("value2", "456");
300 		assertEquals("hoge?value1=123&value2=456", redirect.getPath());
301 	}
302 
303 	interface Asserter {
304 		void assertDispatchPath(String path);
305 	}
306 
307 	class RequestDispatcherAssertionWrapper extends HttpServletResponseWrapper {
308 
309 		private final Asserter asserter;
310 
311 		public RequestDispatcherAssertionWrapper(
312 				final HttpServletResponse response, final Asserter asserter) {
313 			super(response);
314 			this.asserter = asserter;
315 		}
316 
317 		@Override
318 		public void sendRedirect(final String location) throws IOException {
319 			asserter.assertDispatchPath(location);
320 			super.sendRedirect(location);
321 		}
322 
323 	}
324 
325 }