<?xml version="1.0" encoding="Shift_JIS"?>

<!DOCTYPE document SYSTEM "./dtd/document-v10.dtd">

<document>
  <header>
    <title>FilterTestCase の原則/FilterTestCase Principles</title>
    <authors>
      <person name="Vincent Massol" email="vmassol@apache.org"/>
    </authors>
  </header>

  <body>

    <s1 title="いつ使うのか/When to use ?">

      <p>
        Your test case class should extend <code>FilterTestCase</code>
        whenever you are unit testing :
      </p>
      <p>
	次のようなものを単体テストする時にはいつでも、
	テストケースクラスは<code>FilterTestCase</code>を拡張しなければなりません : 
      </p>
      <ul>
        <li>
          Servlet Filters,
        </li>
        <li>
          Servlet フィルター
        </li>
        <li>
          Any java code that uses Filter API objects
          (<code>FilterConfig</code>, ...)
        </li>
        <li>
	  Filter API オブジェクトを使った、どんな java コードでも
          Any java code that uses Filter API objects
          (<code>FilterConfig</code> など)
        </li>
      </ul>

      <note>
        <strong>As filters only exist in Servlet API 2.3, you must use the
        Cactus version for Servlet API 2.3 if you wish to write tests for
        them.</strong>
      </note>
      <note>
        <strong>フィルターは Servlet API 2.3 にのみ存在するので、
	これらをテストを記述したい場合には、
	Servlet API 2.3 用のバージョンの Cactus を使わなければなりません。
	</strong>
      </note>
      <note>
        <strong>Filter unit testing is only available in Cactus 1.2 and
        above.</strong>
      </note>
      <note>
        <strong>フィルター単体テストは Cactus 1.2 以上でのみ利用可能です。
	</strong>
      </note>

    </s1>

    <s1 title="提供される暗黙的なオブジェクト/Provided Implicit Objects">

      <p>
        Cactus automatically initializes the implicit objects for you and
        they are made available to your <code>setUp()</code>,
        <code>testXXX()</code> and <code>tearDown()</code> methods as
        instance variables of the <code>FilterTestCase</code> class (and thus
        as instance variables of your test case class as it extends
        <code>FilterTestCase</code>).
      </p>
      <p>
	Cactus は開発者のために、自動的に暗黙的なオブジェクトを初期化します。		そして、それらは
	<code>FilterTestCase</code> クラスのインスタンス変数として、
	<code>setUp()</code>、<code>testXXX()</code>、<code>tearDown()</code>
	メソッドを利用可能にします。
	(その結果、テストケースクラスのインスタンス変数として、
	<code>FilterTestCase</code>を拡張するように)
      </p>
      <note>
        You may ask yourself how Cactus initializes these objects. The
        mechanism is described in the <link href="how_it_works.html">How it
        works</link> guide.
      </note>
      <note>
	Cactus はこれらのオブジェクトをどうやって初期化するのか気になるかもしれません。
	その機構は<link href="how_it_works.html">どのように動くか</link>
	のガイドに詳しく書かれています。
      </note>

      <p>
        The provided implicit objects are :
      </p>
      <p>
        提供される暗黙的なオブジェクトは次の通りです :
      </p>

      <anchor id="filter_request"/>
      <s2 title="request">

        <p>
         See <code>ServletTestCase</code><code>
         <link href="howto_testcase_servlet.html#servlet_request">request
         </link></code> implicit object for documentation.
       </p>
        <p>
	<code>ServletTestCase</code>の、
	<code>
         <link href="howto_testcase_servlet.html#servlet_request">request
         </link></code>暗黙オブジェクトのドキュメントをご覧ください。
       </p>

      </s2>

      <anchor id="filter_response"/>
      <s2 title="response">

        <p>
         See <code>ServletTestCase</code><code>
         <link href="howto_testcase_servlet.html#servlet_response">response
         </link></code> implicit object for documentation.
       </p>
        <p>
         See 
	 <code>ServletTestCase</code>の
	<code>
         <link href="howto_testcase_servlet.html#servlet_response">response
         </link></code>暗黙オブジェクトのドキュメントをご覧ください。
       </p>

      </s2>

      <anchor id="filter_config"/>
      <s2 title="config">

        <table>
          <tr>
            <td>
              Instance variable name
            </td>
            <td>
              <strong><code>config</code></strong>
            </td>
          </tr>
          <tr>
            <td>
              Class name
            </td>
            <td>
              <code>org.apache.cactus.server.FilterConfigWrapper
              </code>, which inherits from
              <code>javax.servlet.FilterConfig</code>
            </td>
          </tr>
        </table>
        <table>
          <tr>
            <td>
              インスタンス変数名
            </td>
            <td>
              <strong><code>config</code></strong>
            </td>
          </tr>
          <tr>
            <td>
              クラス名
            </td>
            <td>
              <code>javax.servlet.FilterConfig</code> を継承した
              <code>org.apache.cactus.server.FilterConfigWrapper
              </code>
            </td>
          </tr>
        </table>

        <p>
          Cactus wraps the original Filter Config for two reasons :
        </p>
        <p>
	  Cactus では2つの理由から、オリジナルの Filter Config をラップしました : 
        </p>
        <ul>
          <li>
            In order to provide additional methods. For example, it is
            possible to initialise filter parameters without setting them in
            <code>web.xml</code>, etc...,
          </li>
          <li>
	    追加メソッドを提供するため。
	    例えば、<code>web.xml</code> で設定せずに、
	    フィルターパラメーターを初期化できるなどです。
          </li>
          <li>
            So that it can return a wrapped Servlet Context instead of the
            original one. This is because the Servlet Context is used
            to perform forwards and includes and we need to pass to these
            methods the original request and response. As we have wrapped
            the request, we need to wrap the Servlet Context to pass the
            original request (and not the wrapped one).
          </li>
          <li>
	    オリジナルのものではなく、ラップされた Servlet Context を
	    返せるように。その理由は、
	    Servlet Context は、
            ページ転送や、挿入を行うのに使われ、
	    オリジナルのリクエストや応答をこれらのメソッドに、
	    渡す必要があるためです。
	    ラップされたリクエストがあるので、
	    (ラップされたものではない)オリジナルリクエストを渡すのに、
	    Servlet Context をラップする必要があります。
          </li>
        </ul>
        <p>
          The <code>config</code> implicit object will contain all
          initialisation parameters defined in <code>web.xml</code>
          under the Filter Redirector filter definition.
        </p>
        <p>
	  <code>config</code> 暗黙オブジェクトには、
	  Filter Redirector のフィルター定義の下に、
	  <code>web.xml</code> で定義された初期化パラメーターが
	  入っています。
        </p>
        <p>
          See the javadoc for the
          <code>org.apache.cactus.server.FilterConfigWrapper</code>
          class for all details. You should also look at the
          samples provided in the Cactus distribution.
        </p>
        <p>
	  詳しくは<code>org.apache.cactus.server.FilterConfigWrapper</code> 
	  クラスの javadoc をご覧ください。
          See the javadoc for the
	  また、Cactus ディストリビューションで提供されるサンプルもご覧ください。
        </p>
      </s2>

      <anchor id="filter_filterChain"/>
      <s2 title="filterChain">

        <table>
          <tr>
            <td>
              Instance variable name
            </td>
            <td>
              <strong><code>filterChain</code></strong>
            </td>
          </tr>
          <tr>
            <td>
              Class name
            </td>
            <td>
              <code>javax.servlet.FilterChain</code>
            </td>
          </tr>
        </table>

        <table>
          <tr>
            <td>
              インスタンス変数名
            </td>
            <td>
              <strong><code>filterChain</code></strong>
            </td>
          </tr>
          <tr>
            <td>
              クラス名
            </td>
            <td>
              <code>javax.servlet.FilterChain</code>
            </td>
          </tr>
        </table>

        <note>
          Cactus does not wrap the filter chain.
        </note>
        <note>
          Cactus ではフィルターチェインをラップしません。
        </note>

      </s2>

    </s1>

    <s1 title="Tips と Tricks/Tips and Tricks">

      <s2 title="チェインになった次のフィルターのシミュレーション/Simulating the next fitler in chain">

        <p>
          As you are performing unit testing of your filter, you may wish
          to simulate the next filter in the filter chain so that you can
          decide, as part of your unit test, what this other filter returns
          to your filter under test. Here is how you could do that :
        </p>
        <p>
	  自分のフィルターの単体テストを行う時、
	  単体テストの一部として、他のフィルターが自分のフィルターにテスト中に何を返すかを決められるよう、
	  フィルターチェイン次のフィルターをシミュレートしたいと思います。
	  次に、どうすればできるか示します : 
Here is how you could do that :
        </p>

<source><![CDATA[
public void testDoFilterOK() throws ServletException, IOException
{
    SampleFilter filter = new SampleFilter();
    filter.init(config);
[...]

    FilterChain mockFilterChain = new FilterChain() {
        public void doFilter(ServletRequest theRequest,
            ServletResponse theResponse) throws IOException, ServletException
        {
            PrintWriter writer = theResponse.getWriter();
            writer.print("<p>some content</p>");
            writer.close();
        }
        public void init(FilterConfig theConfig)
        {
        }
        public void destroy()
        {
        }
    };

    filter.doFilter(request, response, mockFilterChain);
[...]
}
]]></source>

        <p>
          See the samples provided as part of the Cactus distribution.
        </p>
        <p>
	  Cactus ディストリビューションの一部として提供されるサンプルをご覧ください。
        </p>

      </s2>

    </s1>

    <s1 title="例題/Sample">

      <p>
        This is a very basic sample intended to give you a flavour of Filter
        unit testing. Check the distribution samples for extensive
        examples. The filter we are testing here simply adds a header and
        a footer to the response. The header and footer texts are
        defined as a parameter defined in <code>web.xml</code>.
      </p>
      <p>
	これは、Filter 単体テストの雰囲気を味わうための、
	とても基本的な例題です。
	広範囲な例については、ディストリビューション中のサンプルを見てください。
	ここでテストするフィルターは、
	単にレスポンスに対して、ヘッダとフッタを加えるものです。
	ヘッダおよびフッタテキストは <code>web.xml</code>において、
	パラメーターとして定義されています。
      </p>

<source><![CDATA[
public void testXXX() throws ServletException, IOException
{
    SampleFilter filter = new SampleFilter();
    config.setInitParameter("header", "<h1>header</h1>");
    config.setInitParameter("footer", "<h1>footer</h1>");
    filter.init(config);

    FilterChain mockFilterChain = new FilterChain() {
        public void doFilter(ServletRequest theRequest,
            ServletResponse theResponse) throws IOException, ServletException
        {
            PrintWriter writer = theResponse.getWriter();
            writer.print("<p>some content</p>");
            writer.close();
        }
        public void init(FilterConfig theConfig)
        {
        }
        public void destroy()
        {
        }
    };

    filter.doFilter(request, response, mockFilterChain);
}

public void endXXX(WebResponse theResponse)
{
    assertEquals("<h1>header</h1><p>some content</p><h1>footer</h1>",
        theResponse.getText());
}
]]></source>

    </s1>

  </body>
</document>
