<?xml version="1.0" encoding="Shift_JIS"?>

<!DOCTYPE document SYSTEM "./dtd/document-v10.dtd">

<document>
  <header>
    <title>ServletTestCase の原則/ServletTestCase 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>ServletTestCase</code>
        whenever you are unit testing :
      </p>
      <p>
	次のようなものを単体テストするときには、
	いつでもテストケースクラスは
	<code>ServletTestCase</code> を拡張しなければなりません : 
      </p>
      <ul>
        <li>
          Servlets,
        </li>
        <li>
          サーブレット
        </li>
        <li>
          Any java code that uses Servlet API objects
          (<code>HttpServletRequest</code>, ...)
        </li>
        <li>
          (<code>HttpServletRequest</code>など)
	  Servlet API オブジェクトを使った、どんな java コードでも
        </li>
      </ul>

    </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>ServletTestCase</code> class (and thus
        as instance variables of your test case class as it extends
        <code>ServletTestCase</code>).
      </p>
      <p>
	Cactus は自動的に開発者用に暗黙オブジェクトを初期化し、
	<code>ServletTestCase</code> クラスのインスタンス変数として、
	(従って、<code>ServletTestCase</code>を拡張するような
	自分のテストケースクラスのインスタンス変数として)
	自分の<code>setUp()</code>、<code>testXXX()</code>、および
	<code>tearDown()</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="servlet_request"/>
      <s2 title="request">

        <table>
          <tr>
            <td>
              Instance variable name
            </td>
            <td>
              <strong><code>request</code></strong>
            </td>
          </tr>
          <tr>
            <td>
              Class name
            </td>
            <td>
              <code>org.apache.cactus.server.HttpServletRequestWrapper
              </code>, which inherits from
              <code>javax.servlet.http.HttpServletRequest</code>
            </td>
          </tr>
        </table>

        <table>
          <tr>
            <td>
              インスタンス変数名
            </td>
            <td>
              <strong><code>request</code></strong>
            </td>
          </tr>
          <tr>
            <td>
              クラス名
            </td>
            <td>
              <code>javax.servlet.http.HttpServletRequest</code> を継承した
              <code>org.apache.cactus.server.HttpServletRequestWrapper</code>
            </td>
          </tr>
        </table>

        <p>
          Cactus wraps methods of the original HTTP request in order to return
          the HTTP related parameters set up in the <code>beginXXX()</code>
          method. Thus, you will be able to pass this request object to
          your code to test and set the needed parameter in the
          <code>beginXXX()</code> method.
        </p>
        <p>
	  <code>beginXXX()</code> メソッドでセットアップされる HTTP
	  に関連するパラメーターを返すために、Cactus はオリジナルの HTTP リクエストのメソッドをラップします。
	  これにより、テスト用にこのリクエストオブジェクトを自分のコードに渡し、
	  <code>beginXXX()</code> メソッドのおいて、必要なパラメーターを設定するのです。
        </p>
        <p>
          For example, if your code under test calls
          <code>getCookies()</code> on the request object that you have
          passed to it, it will return the cookies that you have added to the
          HTTP request in <code>beginXXX()</code> by calling the
          <code>WebRequest.addCookie()</code> method.
        </p>
        <p>
	  例えば、自分が渡したリクエストオブジェクト上で、
	  自分のテスト中のコードが <code>getCookies()</code> を呼び出した時、
	  <code>WebRequest.addCookie()</code> メソッドを呼び出すことにより、
	  <code>beginXXX()</code> 中で HTTP リクエストに加えたクッキーを返します。
        </p>
        <p>
          See the javadoc for the
          <code>org.apache.cactus.WebRequest</code> and
          <code>org.apache.cactus.server.HttpServletRequestWrapper
          </code> classes for all details. You should also look at the
          samples provided in the Cactus distribution.
        </p>
        <p>
	  詳しくは、
          <code>org.apache.cactus.WebRequest</code> および
          <code>org.apache.cactus.server.HttpServletRequestWrapper</code>
	  クラスの javadoc をご覧下さい。
	  また、Cactus のディストリビューションで提供される例題もご覧ください。
        </p>
      </s2>

      <anchor id="servlet_response"/>
      <s2 title="response">

        <table>
          <tr>
            <td>
              Instance variable name
            </td>
            <td>
              <strong><code>response</code></strong>
            </td>
          </tr>
          <tr>
            <td>
              Class name
            </td>
            <td>
              <code>javax.servlet.http.HttpServletResponse</code>
            </td>
          </tr>
        </table>

        <table>
          <tr>
            <td>
              インスタンス変数名
            </td>
            <td>
              <strong><code>response</code></strong>
            </td>
          </tr>
          <tr>
            <td>
              クラス名
            </td>
            <td>
              <code>javax.servlet.http.HttpServletResponse</code>
            </td>
          </tr>
        </table>

        <note>
          Cactus does not wrap the response.
        </note>
        <note>
	  Cactus は response をラップしません。
        </note>

      </s2>

      <anchor id="servlet_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.ServletConfigWrapper
              </code>, which inherits from
              <code>javax.servlet.ServletConfig</code>
            </td>
          </tr>
        </table>

        <table>
          <tr>
            <td>
              インスタンス変数名
            </td>
            <td>
              <strong><code>config</code></strong>
            </td>
          </tr>
          <tr>
            <td>
              クラス名
            </td>
            <td>
              <code>javax.servlet.ServletConfig</code> を継承した
              <code>org.apache.cactus.server.ServletConfigWrapper</code>n
            </td>
          </tr>
        </table>

        <p>
          Cactus wraps the original Servlet Config for two reasons :
        </p>
        <p>
          次の 2 つの理由により Cactus はオリジナルの Servlet Config をラップします :
        </p>
        <ul>
          <li>
            In order to provide additional methods. For example, it is
            possible to initialise 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 Servlet Redirector servlet definition.
        </p>
        <p>
	  <code>config</code> 暗黙オブジェクトは
	  Servlet Redirector 定義の元で、
	  <code>web.xml</code> で定義された全ての初期化パラメーターを持っています。
        </p>
        <p>
          See the javadoc for the
          <code>org.apache.cactus.server.ServletConfigWrapper</code>
          class for all details. You should also look at the
          samples provided in the Cactus distribution.
        </p>
        <p>
	  詳細については、
	  <code>org.apache.cactus.server.ServletConfigWrapper</code> クラスの
	  javadoc をご覧ください。
	  また、Cactus ディストリビューションで提供される例題もご覧ください。
        </p>
      </s2>

      <anchor id="servlet_session"/>
      <s2 title="session">

        <table>
          <tr>
            <td>
              Instance variable name
            </td>
            <td>
              <strong><code>session</code></strong>
            </td>
          </tr>
          <tr>
            <td>
              Class name
            </td>
            <td>
              <code>javax.servlet.http.HttpSession</code>
            </td>
          </tr>
        </table>

        <table>
          <tr>
            <td>
              インスタンス変数名
            </td>
            <td>
              <strong><code>session</code></strong>
            </td>
          </tr>
          <tr>
            <td>
              クラス名
            </td>
            <td>
              <code>javax.servlet.http.HttpSession</code>
            </td>
          </tr>
        </table>

        <note>
          Cactus does not wrap the response.
        </note>
        <note>
          Cactus は response をラップしません
        </note>

        <p>
          By default, Cactus always creates an HTRP session for your test
          case. It is possible to tell it not to do so by calling the
          <code>WebRequest.setAutomaticSession(false)</code> method (it is
          <code>true</code> by default). This could be useful for cases
          where your code under test verifies branches when
          "<code>request.getSession(false)</code>" is <code>null</code>.
        </p>
        <p>
	  デフォルトでは、常に Cactus は自分のテストケース用に HTTP セッションを生成します。
	  <code>WebRequest.setAutomaticSession(false)</code> メソッドを呼び出すことにより、
	  そうしないよう、指定することができます。
	  (デフォルトでは <code>true</code>です。)

          "<code>request.getSession(false)</code>" が <code>null</code> であるとき、
	  テスト中のコードがブランチを検証するようなケースの場合便利かもしれません。
        </p>

      </s2>

    </s1>

    <s1 title="Tip と Trick/Tips and Tricks">

      <s2 title="パラメーターの初期化/Parameter initialisation">

        <p>
          If your code under test make use of any of the servlet methods
          inherited from <code>javax.servlet.GenericServlet</code> (these are
          the <code>log()</code>, <code>getServletConfig()</code>, ...
          methods), then you need to call the <code>init(ServletConfig)</code>
          method of your servlet to initialise its internal
          <code>ServletConfig</code> object.
        </p>
        <p>
	  自分のテスト中のコードが
	  <code>javax.servlet.GenericServlet</code>
 	  から継承したいずれかのサーブレットメソッド
	  (<code>log()</code>、<code>getServletConfig()</code>メソッドなど)
	  を使う場合、
	  <code>ServletConfig</code> 内部オブジェクトを初期化するために、
	  自分のサーブレットの<code>init(ServletConfig)</code>メソッドを呼び出さなければなりません。
        </p>
        <p>
          For example :
        </p>
        <p>
          例 :
        </p>

<source><![CDATA[
public void testXXX()
{
    MyServletToTest servlet = new MyServletToTest();
    servlet.init(config);

    // Call a method to test that uses a method inherited from Generic Servlet
    // Generic Servlet から継承したメソッドを利用するテストするためのメソッド呼び出し
    servlet.someMethodToTest();

[...]
}
]]></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 Servlet
        unit testing. Check the distribution samples for extensive
        examples.
      </p>
      <p>
	これは、サーブレットの単体テストの雰囲気をわかってもらうためのとても基本的な例題です。
	拡張した例題については、ディストリビューションのサンプルをご覧ください。
      </p>

      <note>
        This example is for Cactus 1.2 and above as it uses the new
        <code>WebRequest</code> and <code>WebResponse</code> objects.
      </note>
      <note>
	この例題は、
	新しい<code>WebRequest</code> および <code>WebResponse</code>オブジェクトを使っているので、
	Cactus 1.2 以上のためのものです。
      </note>

<source><![CDATA[
public void beginXXX(WebRequest theRequest)
{
    // Set up HTTP related parameters
    // HTTP関連のパラメーターを設定します
    theRequest.setURL("jakarta.apache.org", "/mywebapp", "/test/test.jsp",
        null, null);
    theRequest.addCookie("cookiename", "cookievalue");
}

public void testXXX()
{
    MyServletToTest servlet = new MyServletToTest();
    servlet.init(config);

    // Call method to test
    // テストのためのメソッドを呼び出します
    servlet.methodToTest();

    // Perform some server side asserts
    // 幾つかのサーバー側のアサーションを行います
    assertEquals("someValue", session.getAttribute("someAttribute"));
    assertEquals("jakarta.apache.org", request.getServerName());
}

public void endXXX(WebResponse theResponse)
{
    // Asserts the returned HTTP response
    // 返される HTTP レスポンスのためのアサーション

    Cookie cookie = theResponse.getCookie("someCookie");
    assertEquals("someValue2", cookie.getValue());

    assertEquals("some content here", theResponse.getText());
}
]]></source>

    </s1>

  </body>
</document>
