View Javadoc

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.unit;
17  
18  import java.io.IOException;
19  import java.lang.reflect.Field;
20  
21  import javax.servlet.FilterChain;
22  import javax.servlet.ServletException;
23  import javax.servlet.ServletRequest;
24  import javax.servlet.ServletResponse;
25  
26  import org.seasar.cubby.action.ActionResult;
27  import org.seasar.cubby.action.Forward;
28  import org.seasar.cubby.action.Redirect;
29  import org.seasar.cubby.controller.ActionProcessor;
30  import org.seasar.cubby.filter.RequestRoutingFilter;
31  import org.seasar.framework.unit.S2TigerTestCase;
32  import org.seasar.framework.util.FieldUtil;
33  
34  /**
35   * CubbyのActionクラスの単体テスト用のクラスです。
36   * <p>
37   * このクラスを継承して、それぞれのActionクラス用の単体テストを作成します。 親クラスに
38   * 
39   * <pre>
40   * public class HelloActionTest extends CubbyTestCase&lt;HelloAction&gt; {
41   * 	private HelloAction action;
42   * 
43   * 	protected void setUp() throws Exception {
44   * 		include(&quot;app.dicon&quot;);
45   * 	}
46   * 
47   * 	public void testIndex() throws Exception {
48   * 		ActionResult result = processAction(&quot;/hello/&quot;);
49   * 		assertPathEquals(Forward.class, &quot;input.jsp&quot;, result);
50   * 	}
51   * 
52   * 	public void testMessage() throws Exception {
53   * 		getRequest().addParameter(&quot;name&quot;, &quot;name1&quot;);
54   * 		ActionResult result = processAction(&quot;/hello/message&quot;);
55   * 		assertPathEquals(Forward.class, &quot;result.jsp&quot;, result);
56   * 		assertEquals(&quot;name1&quot;, action.name);
57   * 	}
58   * }
59   * </pre>
60   * 
61   * </p>
62   * 
63   * @author agata
64   */
65  public class CubbyTestCase extends S2TigerTestCase {
66  
67  	/** ルーティング */
68  	private RequestRoutingFilter.Router router = new RequestRoutingFilter.Router();
69  
70  	private MockFilterChain filterChain = new MockFilterChain();
71  
72  	/** ActionProcessor */
73  	private ActionProcessor actionProcessor;
74  
75  	/**
76  	 * ActionResultの型とパスをチェックします。
77  	 * 
78  	 * @param resultClass
79  	 *            ActionResultの型
80  	 * @param expectedPath
81  	 *            期待されるパス
82  	 * @param actualResult
83  	 *            チェックするActionResult
84  	 */
85  	public static void assertPathEquals(
86  			Class<? extends ActionResult> resultClass, String expectedPath,
87  			ActionResult actualResult) {
88  		assertEquals("ActionResultの型をチェック", resultClass, actualResult
89  				.getClass());
90  		if (actualResult instanceof Forward) {
91  			assertEquals("パスのチェック", expectedPath, ((Forward) actualResult)
92  					.getPath());
93  		} else if (actualResult instanceof Redirect) {
94  			assertEquals("パスのチェック", expectedPath, ((Redirect) actualResult)
95  					.getPath());
96  		}
97  	}
98  
99  	/**
100 	 * アクションメソッドを実行します。
101 	 * 
102 	 * @param orginalPath
103 	 *            パス
104 	 * @return アクションメソッドの実行結果。アクションメソッドが見つからなかったり結果がない場合、null
105 	 * @throws Exception
106 	 */
107 	@SuppressWarnings( { "hiding", "unchecked" })
108 	protected ActionResult processAction(String orginalPath) throws Exception {
109 		routing(orginalPath);
110 		return actionProcessor
111 				.process(getRequest(), getResponse(), filterChain);
112 	}
113 
114 	/**
115 	 * CubbyFilterで行っているルーティングをエミュレートして、内部フォワードパスをリクエストにセットします。
116 	 * 
117 	 * @param orginalPath
118 	 *            オリジナルパス
119 	 * @return 内部フォワードパス
120 	 * @throws NoSuchFieldException
121 	 */
122 	protected String routing(String orginalPath) throws NoSuchFieldException {
123 		Field field = getRequest().getClass().getDeclaredField("servletPath");
124 		field.setAccessible(true);
125 		FieldUtil.set(field, getRequest(), orginalPath);
126 		String forwardPath = router.routing(getRequest(), getResponse());
127 		FieldUtil.set(field, getRequest(), forwardPath);
128 		return forwardPath;
129 	}
130 
131 	/**
132 	 * モックのFilterChain
133 	 * 
134 	 * @author agata
135 	 */
136 	private static class MockFilterChain implements FilterChain {
137 		public void doFilter(ServletRequest request, ServletResponse response)
138 				throws IOException, ServletException {
139 		}
140 	}
141 
142 }