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