1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.seasar.cubby.action;
17
18 import java.io.IOException;
19 import java.lang.reflect.Method;
20
21 import javax.servlet.http.HttpServletResponse;
22 import javax.servlet.http.HttpServletResponseWrapper;
23
24 import org.seasar.cubby.controller.ActionContext;
25 import org.seasar.cubby.controller.ActionDef;
26 import org.seasar.extension.unit.S2TestCase;
27 import org.seasar.framework.container.ComponentDef;
28 import org.seasar.framework.mock.servlet.MockHttpServletRequest;
29 import org.seasar.framework.mock.servlet.MockHttpServletResponse;
30 import org.seasar.framework.mock.servlet.MockServletContext;
31
32 public class RedirectTest extends S2TestCase {
33
34 public ActionContext context;
35
36 @Override
37 protected void setUp() throws Exception {
38 include(this.getClass().getName().replaceAll("\\.", "/") + ".dicon");
39 }
40
41 public void testBasicSequence() throws Exception {
42 MockServletContext servletContext = this.getServletContext();
43 servletContext.setServletContextName("/cubby");
44 MockHttpServletRequest request = this.getRequest();
45 MockHttpServletResponse response = this.getResponse();
46 ComponentDef componentDef = this.getComponentDef(MockAction.class);
47 context.initialize(new MockActionDef(componentDef));
48 MockAction action = (MockAction) context.getAction();
49
50 Redirect redirect = new Redirect("path.jsp");
51 redirect.prerender(context);
52 assertFalse(action.isPrerendered());
53 redirect.execute(context, request,
54 new RequestDispatcherAssertionWrapper(response, new Asserter() {
55 public void assertDispatchPath(String path) {
56 assertEquals("/cubby/mock/path.jsp", path);
57 }
58 }));
59 assertFalse(action.isPostrendered());
60 }
61
62 public void testRelativePath() throws Exception {
63 MockServletContext servletContext = this.getServletContext();
64 servletContext.setServletContextName("/cubby");
65 MockHttpServletRequest request = this.getRequest();
66 MockHttpServletResponse response = this.getResponse();
67 ComponentDef componentDef = this.getComponentDef(MockAction.class);
68 context.initialize(new MockActionDef(componentDef));
69
70 Redirect redirect = new Redirect("page.jsp");
71 redirect.execute(context, request,
72 new RequestDispatcherAssertionWrapper(response, new Asserter() {
73 public void assertDispatchPath(String path) {
74 assertEquals("/cubby/mock/page.jsp", path);
75 }
76 }));
77 }
78
79 public void testAbsolutePath() throws Exception {
80 MockServletContext servletContext = this.getServletContext();
81 servletContext.setServletContextName("/cubby");
82 MockHttpServletRequest request = this.getRequest();
83 MockHttpServletResponse response = this.getResponse();
84 ComponentDef componentDef = this.getComponentDef(MockAction.class);
85 context.initialize(new MockActionDef(componentDef));
86
87 Redirect redirect = new Redirect("/absolute/path.jsp");
88 redirect.execute(context, request, new RequestDispatcherAssertionWrapper(
89 response, new Asserter() {
90 public void assertDispatchPath(String path) {
91 assertEquals("/cubby/absolute/path.jsp", path);
92 }
93 }));
94 }
95
96 public void testRootContextPath() throws Exception {
97 MockServletContext servletContext = this.getServletContext();
98 servletContext.setServletContextName("/");
99 MockHttpServletRequest request = this.getRequest();
100 MockHttpServletResponse response = this.getResponse();
101 ComponentDef componentDef = this.getComponentDef(MockAction.class);
102 context.initialize(new MockActionDef(componentDef));
103
104 Redirect redirect = new Redirect("path.jsp");
105 redirect.execute(context, request, new RequestDispatcherAssertionWrapper(
106 response, new Asserter() {
107 public void assertDispatchPath(String path) {
108 assertEquals("/mock/path.jsp", path);
109 }
110 }));
111 }
112
113 public void testGetPath() throws Exception {
114 Redirect redirect = new Redirect("/absolute/redirect");
115 assertEquals("/absolute/redirect", redirect.getPath());
116 }
117
118 interface Asserter {
119 void assertDispatchPath(String path);
120 }
121
122 class RequestDispatcherAssertionWrapper extends HttpServletResponseWrapper {
123
124 private Asserter asserter;
125
126 public RequestDispatcherAssertionWrapper(HttpServletResponse response,
127 Asserter asserter) {
128 super(response);
129 this.asserter = asserter;
130 }
131
132 @Override
133 public void sendRedirect(String location) throws IOException {
134 asserter.assertDispatchPath(location);
135 super.sendRedirect(location);
136 }
137
138 }
139
140 class MockActionDef implements ActionDef {
141
142 private ComponentDef componentDef;
143
144 public MockActionDef(ComponentDef componentDef) {
145 this.componentDef = componentDef;
146 }
147
148 public ComponentDef getComponentDef() {
149 return componentDef;
150 }
151
152 public Method getMethod() {
153 return null;
154 }
155
156 }
157
158 public static class MockAction extends Action {
159
160 private boolean prerendered = false;
161
162 private boolean postrendered = false;
163
164 @Override
165 public void prerender() {
166 super.prerender();
167 prerendered = true;
168 }
169
170 @Override
171 public void postrender() {
172 super.postrender();
173 postrendered = true;
174 }
175
176 public boolean isPrerendered() {
177 return prerendered;
178 }
179
180 public boolean isPostrendered() {
181 return postrendered;
182 }
183
184 }
185
186 }