Coverage Report - org.seasar.cubby.plugins.s2.unit.CubbyTestCase
 
Classes in this File Line Coverage Branch Coverage Complexity
CubbyTestCase
4%
1/21
N/A
1.286
 
 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  
  */
 106  1
 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  0
                 CubbyAssert.assertPathEquals(expectedType, expectedPath, actual);
 122  0
         }
 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  0
                 CubbyAssert.assertPathEquals(message, expectedType, expectedPath,
 141  
                                 actual);
 142  0
         }
 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  0
                 CubbyAssert.assertPathEquals(expectedType, expectedPath, actual,
 161  
                                 characterEncoding);
 162  0
         }
 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  0
                 CubbyAssert.assertPathEquals(message, expectedType, expectedPath,
 184  
                                 actual, characterEncoding);
 185  0
         }
 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  0
                 final MockHttpServletRequest request = getRequest();
 198  0
                 setServletPath(request, path);
 199  0
                 final MockHttpServletResponse response = getResponse();
 200  0
                 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  0
                         final Field servletPathField = request.getClass().getDeclaredField(
 215  
                                         "servletPath");
 216  0
                         servletPathField.setAccessible(true);
 217  0
                         servletPathField.set(request, servletPath);
 218  0
                 } catch (final Exception ex) {
 219  0
                         throw new IllegalStateException(ex);
 220  0
                 }
 221  0
         }
 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  0
                 return CubbyRunner.processAction(request, response);
 237  
         }
 238  
 
 239  
 }