Coverage Report - org.seasar.cubby.unit.CubbyTestCase
 
Classes in this File Line Coverage Branch Coverage Complexity
CubbyTestCase
0%
0/18
0%
0/4
0
CubbyTestCase$1
N/A
N/A
0
CubbyTestCase$MockFilterChain
0%
0/2
N/A
0
 
 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  0
 public class CubbyTestCase extends S2TigerTestCase {
 66  
 
 67  
         /** ルーティング */
 68  0
         private RequestRoutingFilter.Router router = new RequestRoutingFilter.Router();
 69  
 
 70  0
         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  0
                 assertEquals("ActionResultの型をチェック", resultClass, actualResult
 89  
                                 .getClass());
 90  0
                 if (actualResult instanceof Forward) {
 91  0
                         assertEquals("パスのチェック", expectedPath, ((Forward) actualResult)
 92  
                                         .getPath());
 93  0
                 } else if (actualResult instanceof Redirect) {
 94  0
                         assertEquals("パスのチェック", expectedPath, ((Redirect) actualResult)
 95  
                                         .getPath());
 96  
                 }
 97  0
         }
 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  0
                 routing(orginalPath);
 110  0
                 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  0
                 Field field = getRequest().getClass().getDeclaredField("servletPath");
 124  0
                 field.setAccessible(true);
 125  0
                 FieldUtil.set(field, getRequest(), orginalPath);
 126  0
                 String forwardPath = router.routing(getRequest(), getResponse());
 127  0
                 FieldUtil.set(field, getRequest(), forwardPath);
 128  0
                 return forwardPath;
 129  
         }
 130  
 
 131  
         /**
 132  
          * モックのFilterChain
 133  
          * 
 134  
          * @author agata
 135  
          */
 136  0
         private static class MockFilterChain implements FilterChain {
 137  
                 public void doFilter(ServletRequest request, ServletResponse response)
 138  
                                 throws IOException, ServletException {
 139  0
                 }
 140  
         }
 141  
 
 142  
 }