View Javadoc

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(&quot;app.dicon&quot;);
44   * 	}
45   * 
46   * 	public void testIndex() throws Exception {
47   * 		// アクションの実行
48   * 		ActionResult result = processAction(&quot;/hello/&quot;);
49   * 		// 結果のチェック
50   * 		assertPathEquals(Forward.class, &quot;input.jsp&quot;, result);
51   * 	}
52   * 
53   * 	public void setUpMessage() {
54   * 		// リクエストパラメータのセット
55   * 		getRequest().addParameter(&quot;name&quot;, &quot;name1&quot;);
56   * 	}
57   * 
58   * 	public void testMessage() throws Exception {
59   * 		// アクションの実行
60   * 		ActionResult result = processAction(&quot;/hello/message&quot;);
61   * 		// 結果のチェック
62   * 		assertPathEquals(Forward.class, &quot;result.jsp&quot;, result);
63   * 		// 実行後のアクションの状態を確認
64   * 		assertEquals(&quot;name1&quot;, 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(&quot;mock&quot;);
81   * 		user.setName(&quot;mock&quot;);
82   * 		user.setPassword(&quot;mock&quot;);
83   * 		getRequest().getSession().setAttribute(&quot;user&quot;, user);
84   * 	}
85   * 
86   * 	public void testShow() throws Exception {
87   * 		this.readXlsAllReplaceDb(&quot;TodoActionTest_PREPARE.xls&quot;);
88   * 		// CoolURIの場合のテスト
89   * 		ActionResult result = processAction(&quot;/todo/1&quot;);
90   * 		assertPathEquals(Forward.class, &quot;show.jsp&quot;, result);
91   * 		assertEquals(new Integer(1), action.id);
92   * 		assertEquals(&quot;todo1&quot;, action.text);
93   * 		assertEquals(&quot;todo1 memo&quot;, action.memo);
94   * 		assertEquals(new Integer(1), action.todoType.getId());
95   * 		assertEquals(&quot;type1&quot;, action.todoType.getName());
96   * 		assertEquals(&quot;2008-01-01&quot;, action.limitDate);
97   * 	}
98   * }
99   * </pre>
100  * 
101  * </p>
102  * 
103  * @author agata
104  * @author baba
105  * @since 2.0.0
106  */
107 public abstract class CubbyTestCase extends S2TigerTestCase {
108 
109 	/**
110 	 * ActionResultの型とパスをチェックします。
111 	 * 
112 	 * @param resultClass
113 	 *            ActionResultの型
114 	 * @param expectedPath
115 	 *            期待されるパス
116 	 * @param actualResult
117 	 *            チェックするActionResult
118 	 */
119 	public static void assertPathEquals(
120 			final Class<? extends ActionResult> resultClass,
121 			final String expectedPath, final ActionResult actualResult) {
122 		CubbyAssert.assertPathEquals(resultClass, expectedPath, actualResult);
123 	}
124 
125 	/**
126 	 * ActionResultの型とパスをチェックします。
127 	 * 
128 	 * @param resultClass
129 	 *            ActionResultの型
130 	 * @param expectedPath
131 	 *            期待されるパス
132 	 * @param actualResult
133 	 *            チェックするActionResult
134 	 * @param characterEncoding
135 	 *            URI のエンコーディング
136 	 */
137 	public static void assertPathEquals(
138 			final Class<? extends ActionResult> resultClass,
139 			final String expectedPath, final ActionResult actualResult,
140 			final String characterEncoding) {
141 		CubbyAssert.assertPathEquals(resultClass, expectedPath, actualResult,
142 				characterEncoding);
143 	}
144 
145 	/**
146 	 * アクションメソッドを実行します。
147 	 * 
148 	 * @param originalPath
149 	 *            オリジナルパス
150 	 * @return アクションメソッドの実行結果。アクションメソッドが見つからなかったり結果がない場合は <code>null</code>
151 	 * @throws Exception
152 	 *             アクションメソッドの実行時に例外が発生した場合
153 	 */
154 	protected ActionResult processAction(final String originalPath)
155 			throws Exception {
156 		final MockHttpServletRequest request = getRequest();
157 		setServletPath(request, originalPath);
158 		final MockHttpServletResponse response = getResponse();
159 		return processAction(request, response);
160 	}
161 
162 	/**
163 	 * 指定されたモックリクエストのサーブレットパスを設定します。
164 	 * 
165 	 * @param request
166 	 *            リクエスト
167 	 * @param servletPath
168 	 *            サーブレットパス
169 	 */
170 	private static void setServletPath(final MockHttpServletRequest request,
171 			final String servletPath) {
172 		try {
173 			final Field servletPathField = request.getClass().getDeclaredField(
174 					"servletPath");
175 			servletPathField.setAccessible(true);
176 			servletPathField.set(request, servletPath);
177 		} catch (final Exception ex) {
178 			throw new IllegalStateException(ex);
179 		}
180 	}
181 
182 	/**
183 	 * アクションメソッドを実行します。
184 	 * 
185 	 * @param originalPath
186 	 *            オリジナルパス
187 	 * @return アクションメソッドの実行結果。アクションメソッドが見つからなかったり結果がない場合は <code>null</code>
188 	 * @throws Exception
189 	 *             アクションメソッドの実行時に例外が発生した場合
190 	 */
191 	protected ActionResult processAction(final HttpServletRequest request,
192 			final HttpServletResponse response) throws Exception {
193 		return CubbyRunner.processAction(request, response);
194 	}
195 
196 }