1 /*
2 * Copyright 2004-2009 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.plugins.s2.unit;
17
18 import java.lang.reflect.Field;
19
20 import javax.servlet.http.HttpServletRequest;
21 import javax.servlet.http.HttpServletResponse;
22
23 import org.seasar.cubby.action.ActionResult;
24 import org.seasar.cubby.unit.CubbyAssert;
25 import org.seasar.cubby.unit.CubbyRunner;
26 import org.seasar.framework.mock.servlet.MockHttpServletRequest;
27 import org.seasar.framework.mock.servlet.MockHttpServletResponse;
28 import org.seasar.framework.unit.S2TigerTestCase;
29
30 /**
31 * CubbyのActionクラスの単体テスト用のクラスです。
32 * <p>
33 * このクラスを継承して、それぞれのActionクラス用の単体テストを作成します。詳細はドキュメントの「アクションのテスト」を参照下さい。
34 *
35 * <pre>
36 * public class HelloActionTest extends CubbyTestCase {
37 * // 対象のアクション
38 * private HelloAction action;
39 *
40 * // 初期化処理
41 * protected void setUp() throws Exception {
42 * // diconファイルの読み込み
43 * include("app.dicon");
44 * }
45 *
46 * public void testIndex() throws Exception {
47 * // アクションの実行
48 * ActionResult result = processAction("/hello/");
49 * // 結果のチェック
50 * assertPathEquals(Forward.class, "input.jsp", result);
51 * }
52 *
53 * public void setUpMessage() {
54 * // リクエストパラメータのセット
55 * getRequest().addParameter("name", "name1");
56 * }
57 *
58 * public void testMessage() throws Exception {
59 * // アクションの実行
60 * ActionResult result = processAction("/hello/message");
61 * // 結果のチェック
62 * assertPathEquals(Forward.class, "result.jsp", result);
63 * // 実行後のアクションの状態を確認
64 * assertEquals("name1", action.name);
65 * }
66 * }
67 * </pre>
68 *
69 * <pre>
70 * public class TodoActionTest extends CubbyTestCase {
71 *
72 * private TodoAction action;
73 *
74 * public void setUpShow() throws Exception {
75 * emulateLogin();
76 * }
77 *
78 * private void emulateLogin() throws Exception {
79 * User user = new User();
80 * user.setId("mock");
81 * user.setName("mock");
82 * user.setPassword("mock");
83 * getRequest().getSession().setAttribute("user", user);
84 * }
85 *
86 * public void testShow() throws Exception {
87 * this.readXlsAllReplaceDb("TodoActionTest_PREPARE.xls");
88 * // CoolURIの場合のテスト
89 * ActionResult result = processAction("/todo/1");
90 * assertPathEquals(Forward.class, "show.jsp", result);
91 * assertEquals(new Integer(1), action.id);
92 * assertEquals("todo1", action.text);
93 * assertEquals("todo1 memo", action.memo);
94 * assertEquals(new Integer(1), action.todoType.getId());
95 * assertEquals("type1", action.todoType.getName());
96 * assertEquals("2008-01-01", action.limitDate);
97 * }
98 * }
99 * </pre>
100 *
101 * </p>
102 *
103 * @author agata
104 * @author baba
105 */
106 public abstract class CubbyTestCase extends S2TigerTestCase {
107
108 /**
109 * 指定された {@link ActionResult} の型とパスを検証します。
110 *
111 * @param expectedType
112 * 期待する <code>ActionResult</code> の型
113 * @param expectedPath
114 * 期待する <code>ActionResult</code> のパス
115 * @param actual
116 * 実際の <code>ActionResult</code>
117 */
118 public static void assertPathEquals(
119 final Class<? extends ActionResult> expectedType,
120 final String expectedPath, final ActionResult actual) {
121 CubbyAssert.assertPathEquals(expectedType, expectedPath, actual);
122 }
123
124 /**
125 * 指定された {@link ActionResult} の型とパスを検証します。
126 *
127 * @param message
128 * メッセージ
129 * @param expectedType
130 * 期待する <code>ActionResult</code> の型
131 * @param expectedPath
132 * 期待する <code>ActionResult</code> のパス
133 * @param actual
134 * 実際の <code>ActionResult</code>
135 * @since 2.0.2
136 */
137 public static void assertPathEquals(final String message,
138 final Class<? extends ActionResult> expectedType,
139 final String expectedPath, final ActionResult actual) {
140 CubbyAssert.assertPathEquals(message, expectedType, expectedPath,
141 actual);
142 }
143
144 /**
145 * 指定された {@link ActionResult} の型とパスを検証します。
146 *
147 * @param expectedType
148 * 期待する <code>ActionResult</code> の型
149 * @param expectedPath
150 * 期待する <code>ActionResult</code> のパス
151 * @param actual
152 * 実際の <code>ActionResult</code>
153 * @param characterEncoding
154 * URI のエンコーディング
155 */
156 public static void assertPathEquals(
157 final Class<? extends ActionResult> expectedType,
158 final String expectedPath, final ActionResult actual,
159 final String characterEncoding) {
160 CubbyAssert.assertPathEquals(expectedType, expectedPath, actual,
161 characterEncoding);
162 }
163
164 /**
165 * 指定された {@link ActionResult} の型とパスを検証します。
166 *
167 * @param message
168 * メッセージ
169 * @param expectedType
170 * 期待する <code>ActionResult</code> の型
171 * @param expectedPath
172 * 期待する <code>ActionResult</code> のパス
173 * @param actual
174 * 実際の <code>ActionResult</code>
175 * @param characterEncoding
176 * URI のエンコーディング
177 * @since 2.0.2
178 */
179 public static void assertPathEquals(final String message,
180 final Class<? extends ActionResult> expectedType,
181 final String expectedPath, final ActionResult actual,
182 final String characterEncoding) {
183 CubbyAssert.assertPathEquals(message, expectedType, expectedPath,
184 actual, characterEncoding);
185 }
186
187 /**
188 * 指定されたパスのアクションメソッドを実行します。
189 *
190 * @param path
191 * パス
192 * @return アクションメソッドの実行結果。アクションメソッドが見つからなかった場合は <code>null</code>
193 * @throws Exception
194 * アクションメソッドの実行時に例外が発生した場合
195 */
196 protected ActionResult processAction(final String path) throws Exception {
197 final MockHttpServletRequest request = getRequest();
198 setServletPath(request, path);
199 final MockHttpServletResponse response = getResponse();
200 return processAction(request, response);
201 }
202
203 /**
204 * 指定されたモックリクエストのサーブレットパスを設定します。
205 *
206 * @param request
207 * リクエスト
208 * @param servletPath
209 * サーブレットパス
210 */
211 private static void setServletPath(final MockHttpServletRequest request,
212 final String servletPath) {
213 try {
214 final Field servletPathField = request.getClass().getDeclaredField(
215 "servletPath");
216 servletPathField.setAccessible(true);
217 servletPathField.set(request, servletPath);
218 } catch (final Exception ex) {
219 throw new IllegalStateException(ex);
220 }
221 }
222
223 /**
224 * アクションメソッドを実行します。
225 *
226 * @param request
227 * 要求
228 * @param response
229 * 応答
230 * @return アクションメソッドの実行結果。アクションメソッドが見つからなかったり結果がない場合は <code>null</code>
231 * @throws Exception
232 * アクションメソッドの実行時に例外が発生した場合
233 */
234 protected ActionResult processAction(final HttpServletRequest request,
235 final HttpServletResponse response) throws Exception {
236 return CubbyRunner.processAction(request, response);
237 }
238
239 }