<?xml version="1.0" encoding="EUC-JP"?>

<!DOCTYPE document SYSTEM "./dtd/document-v10.dtd">

<document>
  <header>
    <title>JspTestCase の原則/JspTestCase Principles</title>
    <authors>
      <person name="Nicholas Lesiecki" email="ndlesiecki@yahoo.com"/>
    </authors>
  </header>

  <body>

    <s1 title="いつ使うのか/When to use ?">

      <p>
        Your test case class should extend <code>JspTestCase</code>
        whenever you are unit testing :
      </p>
      <p>
	次のようなものを単体テストするときには、
	いつでもテストケースクラスは
	<code>JspTestCase</code> を拡張しなければなりません : 
      </p>
      <ul>
        <li>
          Custom tags,
        </li>
        <li>
          カスタムタグ
        </li>
        <li>
          Any java code that uses JSP API objects
          (<code>PageContext</code>, ...)
        </li>
        <li>
	  (<code>PageContext</code>など)JSP API オブジェクトを使った、どんな
	  java コードでも
        </li>
      </ul>
      <p>
        This tutorial focuses on testing custom tags, as they are the principal
        code which uses the JSP API objects. Future versions of this tutorial
        will expand upon testing actual JSPs.
      </p>
      <p>
	本チュートリアルでは、
	JSP API オブジェクトを使った原則的コードであるので、
	カスタムタグのテストに焦点を当てています。
	将来、このチュートリアルは実際の JSP のテストについても書き加えられるでしょう。
      </p>
    </s1>

    <s1 title="タグライブラリテストの概要/Overview of Tag Library Testing">
        <p>
            Custom tags consist of entries in a Tag Library Descriptor file 
            (TLD) and a tag handler class. Cactus provides the facility to test 
            both aspects of a custom tag. However, since the TLD contains no 
            logic, you will use Cactus primarily to test the tag handler class.
        </p>
        <p>
	    カスタムタグは、
	    タグライブラリ記述ファイル(TLD)に登録された項目および、
	    タグハンドラークラスにより構成されています。
	    Cactus では、その両方のカスタムタグのテストの機能を提供しています。
	    しかしながら、TDL にはロジック含まれていないので、
	    Cactus をタグハンドラークラスのテストをするために、主に使うでしょう。
        </p>
        <p>
            To test the tag handler class, use the implicit objects provided by
            JspTestCase to set up the initial state for the test. Then create 
            and initialize your custom tag using the <code>pageContext</code>
            implicit
            object. After setting up the tag, call the lifecycle methods 
            implemented by your tag in the correct order and verify that the 
            methods return the expected results. The tag's output can be 
            inspected in the <code>endXXX()</code> method.
        </p>
        <p>
	    タグハンドラークラスをテストするには、
	    JstTestCase により提供される、
	    テストの初期状態を設定する暗黙オブジェクトを使います。
	    そして次に、<code>pageContext</code> 暗黙オブジェクトを使って、
	    自分のカスタムタグを生成、初期化します。
	    そのタグを設定した後に、
	    正しい順序で、
	    自分のタグで実装された lifecycle メソッドを呼び出し、
	    そのメソッドが期待した結果を返すか検証します。
	    タグの出力は、<code>endXXX()</code> メソッドで調べることができます。
        </p>
        <p>
            For an additional degree of integration testing, you can create a
            JSP that exercises your custom tag and call it from within a regular
            Cactus test case. See the section on <jump 
            anchor="further_integration">Further Integration Testing</jump> 
            for details.
        </p>
        <p>
	    結合テストの追加の程度によって、
	    自分のカスタムタグをテストするJSP を作り、
	    それを通常の Cactus テストケースから呼び出す事ができます。
	    詳しくは、
	    <jump anchor="further_integration">さらなる統合テスト</jump>の節をご覧ください。
        </p>
    </s1>

    <s1 title="提供される暗黙オブジェクト/Provided Implicit Objects">

      <p>
        Cactus automatically initializes the following implicit objects.
        They are made available to your <code>setUp()</code>,
        <code>testXXX()</code> and <code>tearDown()</code> methods as
        instance variables of the <code>JspTestCase</code> class (and thus
        as instance variables of your test case class).
      </p>
      <p>
	Cactus は自動的に次に示す暗黙オブジェクトを初期化します。
	<code>JspTestCase</code> クラスのインスタンス変数として、
	(従って、自分のテストケースクラスのインスタンス変数として)
	自分の<code>setUp()</code>、<code>testXXX()</code>、および
	<code>tearDown()</code> メソッドで利用可能になります。
      </p>
      <note>
        See the <link href="how_it_works.html">How it
        works</link> guide for details on how Cactus initializes these objects.
      </note>
      <note>
	Cactus がどのように、これらのオブジェクトを初期化するかについての詳細は
	<link href="how_it_works.html">How it
        works</link> ガイドをご覧ください。
      </note>

      <p>
        The provided implicit objects are :
      </p>
      <p>
        提供される暗黙オブジェクトは次の通りです :
      </p>

      <anchor id="jsp_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="jsp_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>
	 <code>ServletTestCase</code><code> の
         <link href="howto_testcase_servlet.html#servlet_response">response
         </link></code> 暗黙オブジェクトのドキュメントをご覧ください。
        </p>

      </s2>

      <anchor id="jsp_config"/>
      <s2 title="config">

        <p>
         <code>ServletTestCase</code><code> の
         <link href="howto_testcase_servlet.html#servlet_config">config
         </link></code> 暗黙オブジェクトのドキュメントをご覧ください。
        </p>
      </s2>

      <anchor id="jsp_session"/>
      <s2 title="session">

        <p>
         <code>ServletTestCase</code><code> の
         <link href="howto_testcase_servlet.html#servlet_session">session
         </link></code> 暗黙オブジェクトのドキュメントをご覧ください。
        </p>
      </s2>

      <anchor id="jsp_out"/>
      <s2 title="out">

        <table>
          <tr>
            <td>
              Instance variable name
            </td>
            <td>
              <strong><code>out</code></strong>
            </td>
          </tr>
          <tr>
            <td>
              Class name
            </td>
            <td>
              <code>public javax.servlet.jsp.JspWriter</code>
            </td>
          </tr>
        </table>
        <table>
          <tr>
            <td>
              インスタンス変数名
            </td>
            <td>
              <strong><code>out</code></strong>
            </td>
          </tr>
          <tr>
            <td>
              クラス名
            </td>
            <td>
              <code>public javax.servlet.jsp.JspWriter</code>
            </td>
          </tr>
        </table>

        <note>
          Cactus does not wrap the out object.
        </note>
        <note>
          Cactus は out オブジェクトをラップしません。
        </note>
        <p>
         You can use this object to write data to the response, thereby
         simulating the body of a tag (if the tag does not modify its body). If
         the tag <strong>does</strong> modify its body, then you will need to 
         generate a BodyContent object before writing out the simualted body.
         See <jump anchor="body_content"><code>bodyContent</code></jump> for
         details.
        </p>
        <p>
	 このオブジェクトはタグのボディをシミュレートしている場所に、	 
	 レスポンスに対してデータを書き加えるのに使えます
	 (タグがそのボディを書き換えない場合)。
	 もし、タグがそのボディを<strong>書き換える</strong>場合、
	 シミュレートされたボディを書き出す前に、BodyContent
	 オブジェクトを生成する必要があります。
	 詳しくは
	 <jump anchor="body_content"><code>bodyContent</code></jump>
	 を見てください。
        </p>
      </s2>

      <anchor id="jsp_pageContext"/>
      <s2 title="pageContext">

        <table>
          <tr>
            <td>
              Instance variable name
            </td>
            <td>
              <strong><code>pageContext</code></strong>
            </td>
          </tr>
          <tr>
            <td>
              Class name
            </td>
            <td>
              <code>org.apache.cactus.server.PageContextWrapper
              </code>, which inherits from
              <code>javax.servlet.jsp.PageContext</code>
            </td>
          </tr>
        </table>
        <table>
          <tr>
            <td>
              インスタンス変数名
            </td>
            <td>
              <strong><code>pageContext</code></strong>
            </td>
          </tr>
          <tr>
            <td>
              クラス名
            </td>
            <td>
	      <code>javax.servlet.jsp.PageContext</code> を継承した
              <code>org.apache.cactus.server.PageContextWrapper</code>
            </td>
          </tr>
        </table>

        <p>
          Custom tags rely exclusively on the <code>pageContext</code> object to
          provide information about the enclosing JSP. Therefore this is the 
          most important implicit object for testing custom tags. Cactus 
          provides a very thin wrapper that ensures that all of the objects that 
          <code>pageContext</code> returns (such as the <code>ServletRequest
          </code> from <code>pageContext.getRequest()</code>) are the correctly
          wrapped versions available in the other implicit variables.
        </p>
        <p>
	  カスタムタグは排他的に、
	  入れ物である JSP についての情報を提供するための
	  <code>pageContext</code> オブジェクトに依存しています。
	  従って、これは、カスタムタグをテストするのに最も重要な暗黙オブジェクトです。	  	  
	  Cactus は、
	  <code>pageContext</code> が返す
	  (<code>pageContext.getRequest()</code> から返される
	  <code>ServletRequest</code> のように)
	  オブジェクトの全てが、
	  他の暗黙変数で利用可能な正しくラップされたバージョンであることを保証するとても薄いラッパーを提供します。
        </p>
        <p>
          See the javadoc for
          <code>org.apache.cactus.server.PageContextWrapper</code>
          for more details. You should also look at the
          samples provided in the Cactus distribution.
        </p>
        <p>
	  詳しくは、
	  <code>org.apache.cactus.server.PageContextWrapper</code> 
	  の javadoc をご覧ください。
	  Cactus ディストリビューションで提供されるサンプルもご覧ください。
        </p>
      </s2>


    <anchor id="jsp_bodyContent"/>
      <s2 title="bodyContent">

        <table>
          <tr>
            <td>
              Instance variable name
            </td>
            <td>
              <strong><code>bodyContent</code></strong>
            </td>
          </tr>
          <tr>
            <td>
              Class name
            </td>
            <td>
              <code>javax.servlet.jsp.tagext.BodyContent</code>
            </td>
          </tr>
        </table>

        <table>
          <tr>
            <td>
              インスタンス変数名
            </td>
            <td>
              <strong><code>bodyContent</code></strong>
            </td>
          </tr>
          <tr>
            <td>
              クラス名
            </td>
            <td>
              <code>javax.servlet.jsp.tagext.BodyContent</code>
            </td>
          </tr>
        </table>

        <p>
          JspTestCase does not actually provide a <code>bodyContent</code> 
          implicit object for use with <code>BodyTags</code>. However, obtaining
          one is so easy that it deserves mention here. Calling <code>
          pageContext.pushBody()</code> returns an object of type <code>
          javax.servlet.jsp.tagext.BodyContent</code> (which inherits from 
          <code>JspWriter</code>). This call also changes the value of the 
          "out" variable stored in page scope (and thus the value of 
          <code>pageContext.getOut()</code>). To put test content into the 
          <code>bodyContent</code> object, simply use its writer methods. To
          quote Sun's API reference on the matter: "Note that the content of
          BodyContent is the result of evaluation, so it will not contain
          actions and the like, but the result of their invocation." 
          See <jump anchor="using_body_content">Body Tags</jump> 
          for more information.
        </p>
        <p>
	  JspTestCase は実際には、
	  <code>BodyTags</code> での使用のために
	  <code>bodyContent</code>  暗黙オブジェクトを提供していません。
	  しかしながら、それを得ることは、ここで述べる価値があるほど簡単です。
	  <code>pageContext.pushBody()</code> を呼び出すと、
	  <code>JspWriter</code> を継承した
	  <code>javax.servlet.jsp.tagext.BodyContent</code>
	  型のオブジェクトを返します。
	  この呼び出しは、ページスコープに保存されている変数
	  (従って<code>pageContext.getOut()</code>の値を)"out" の値を変更します
	  。
	  <code>bodyContent</code> オブジェクトにテストコンテンツを置くためには、
	  単に、そのライターメソッドを使えばよいのです。
	  問題部分の Sun の API リファレンスを引用すると次の通りです:
	  "BodyContent のコンテンツは評価の結果なので、
	  アクションやそれに類するもの含まず、
	  その実行の結果を含むということに注意してください。"
	  詳しくは <jump anchor="using_body_content">ボディタグ</jump> 
	  をご覧ください。
        </p>
            <note>
                It's important to balance 
                calls to <code>pushBody()</code> with calls to 
                <code>popBody()</code>--otherwise many servlet engines will not
                output the tag's body. The easiest way to accomplish this is to
                call pushBody in <code>setUp()</code> and <code>popBody()</code>
                in <code>tearDown()</code>.
            </note>
            <note>
		<code>pushBody()</code> の呼び出しと、
	        <code>popBody()</code> の呼び出しの回数が合っていることが重要です。
		さもないと、多くのサーブレットエンジンでは、
		タグの内容を出力しないでしょう。
		これを行う最も簡単な方法は、
		pushBody を <code>setUp()</code> 内で呼び出し、	
		<code>popBody()</code> を <code>setUp()</code> 内で呼び出すことです。
            </note>
        
      </s2>
    </s1>

    <s1 title="カスタムタグの設定/Custom Tag Set Up">
        <p>
            Creating the test fixture for a custom tag test involves several 
            steps. The exact order of the steps can vary depending on the 
            needs of the test. For instance, placing the test data in the 
            correct scope would probably happen before a real JSP began its
            execution. You can emulate this, or choose to do it after the 
            tag has been in initialized (as described below). In most cases 
            you can determine the exact order of the steps based on what 
            is most convenient for a given test (some steps may be specific 
            to only one test in the <code>TestCase</code> and so should 
            be executed after common <code>setUp()</code> code).
        </p>
        <p>
	カスタムタグテストのためのテストの環境を作るには、
	幾つかの手順があります。
	手順の正しい順序は、必要するテストによって変わります。
	例えば、正しい場所にテストデータを置くことは、
	実際の JSP が実行される前に、行っておかなくてはなりません。
	開発者は、これをエミュレーションしたり、
	(下で述べるように)タグが初期化された後で、
	行うか選べます。
	ほとんどのケースでは、
	与えられたテストに対して何が最も便利であるかに基づき、
	手順の正確な順番を決定できます。
	(幾つかの手順は
	<code>TestCase</code> 中の、
	たった一つのテストに特定のものかもしれないので、
	共通の <code>setUp()</code> コードの後で実行すべきかもしれない。)
        </p>

        <s2 title="ステップ 1: タグの生成(必須)/Step 1: Create the Tag (Required)">
            <p>
                Instantiate a copy of the tag you wish to test.
            </p>
            <p>
                テストしたいタグのコピーを初期化します
            </p>

<source><![CDATA[SomeTag tag = new SomeTag();]]></source>
        </s2>

        <s2 title="ステップ 2: pageContext の設定 (オプション)/Step 2: Set the pageContext (Optional)">

            <p>
                Call the <code>setPageContext()</code> method with the implicit
                object provided by Cactus to register the pageContext with the
                tag.
            </p>
            <p>
		タグに pageContext を登録するために、
		Cactus により提供される暗黙オブジェクトにより、
		<code>setPageContext()</code> メソッドを呼び出します。
            </p>

<source><![CDATA[tag.setPageContext(pageContext);]]></source>
        </s2>

        <s2 title="ステップ 3: タグの属性の設定 (オプション)/Step 3: Set the tag's attributes (Optional)">
            <p>
                If your tag takes attributes, call setter methods to initialize
                the tag's state. Setters on the tag handler class represent the 
                attributes of custom tags. Thus to emulate this JSP fragment:
            </p>
            <p>
		自分のタグが属性を取る場合、
		タグの状態を初期化するために設定用メソッドを呼び出します。
		タグハンドラークラスの設定用メソッドは、
		カスタムタグの属性をあらわします。
		ですから、これをエミュレートするには、
		次をエミュレートするには、JSP のコード部分は : 
            </p>
<source><![CDATA[<someTag foo="10" bar="11"/>]]></source>
            <p>
                You would need to use the following:
            </p>
            <p>
                次を使う必要があります :
            </p>
<source><![CDATA[
someTag.setFoo("10");
someTag.setBar("11");
]]></source>

        </s2>

        <s2 title="ステップ 4: 親タグの設定 (オプション) /Step 4: Set the parent tag (Optional)">
            <p>
                If you would like the tag you are testing to access a parent 
                tag, you will need to call 
            </p>
            <p>
		テストしたいタグが親のタグへアクセスしたい場合には、
		次のように呼び出す必要があります。
            </p>
<source><![CDATA[tag.setParent(enclosingTag);]]></source>
            <p>
                This will allow tag to successfully call <code>getParent</code>
                and <code>TagSupport.findAncestorWithClass()</code>. Of course 
                <code>enclosingTag</code> will have to be instantiated and 
                set up as well, including another call to 
                <code>setParent()</code> if you would like to simulate multiple
                levels of nesting.
            </p>
            <p>
		これにより、タグが
		<code>getParent</code> および
		<code>TagSupport.findAncestorWithClass()</code>
		を首尾よく呼び出せるようになります。
		もちろん、
		多段の入れ子をシミュレートしたい場合、
		<code>setParent()</code> の別の呼び出しを含めて、
		<code>enclosingTag</code> が同様に初期化され、
		設定されなければなりません。
            </p>
        </s2>
    

        <s2 title="ステップ 5: BodyContent オブジェクトの生成 (オプション)/Step 5: Create the BodyContent Object (Optional)">
            <p>
		自分のタグがそのボディを処理する場合、
		<code>BodyContent</code> を得るために
		<code>pageContext.pushBody()</code>  を	
		呼び出します。
		この手順を行う場合、
		タグの実行が終了した後で、
		<code>pageContext.popBody()</code> の呼び出しが含まれている事を確認しなければなりません。
		詳しくは、
		<jump anchor="using_body_content">Body Tags</jump> の節を見てください。
            </p>
        </s2>


        <s2 title="ステップ 6: ページ状態のセットアップ (オプション)/Step 6: Set up page state (Optional)">
            <p>
                Set up any necessary page state by putting test objects into the
                appropriate scopes. Tags frequently access data in the 
                session, the request, or the page. If your tag operates on data 
                contained in any of these (or in the application scope), be sure
                to set up this part of the test fixture. Objects can be placed 
                in these scopes by using the implicit objects provided by Cactus
                directly, or by accessing them indirectly through the 
                <code>pageContext</code> object.
            </p>
            <p>
		テストオブジェクトを、適切なスコープに置くことにより、
		必要なページ状態のセットアップを行います。
		タグは頻繁にセッション、リクエスト、あるいはページ
		(あるいは、アプリケーションスコープ)
		の中のデータにアクセスします。
		自分のタグが、これらのいずれかに含まれるデータを処理する場合、
		テスト環境のこの部分を必ずセットアップしてください。
		Cactus により直接提供される暗黙オブジェクトを用いて、
		あるいは、<code>pageContext</code>
		オブジェクトを通じて間接的にアクセスすることにより
		これらのスコープにオブジェクトを置くことができます。
            </p>
<source><![CDATA[
request.setAttribute("key", new DomainObject("testValue"));
//or
pageContext.setAttribute("key", new DomainObject("testValue"), PageContext.REQUEST_SCOPE);
]]></source>
        </s2>
    </s1>

    <s1 title="テストの実行/Running the Test">
        <p>
            Once the tag has been set up and any necessary page data has been 
            placed in the appropriate scopes, testing a custom tag consists of 
            calling the relevant life-cycle methods and then using JUnit 
            asserts to verify the outcome.
        </p>
        <p>
	タグがセットアップされ、必要なページデータが適切なスコープに置かれたら、
	カスタムタグのテストは、関連するライフサイクルメソッドの呼び出しにより構成され、
	次に、出力を検証するために、JUnit の使用を宣言します。
        </p>
        <s2 title="個々のメソッドの検証/Verifying individual methods">
            <p>
                Most of the life cycle methods return <code>ints</code>, 
                which signal that 
                the container should take a certain action after the method. 
                For instance, the constant <code>EVAL_BODY_INCLUDE</code> 
                returned from <code>doStartTag()</code> tells the container to 
                include the tag's body in the JSP's output response. So a tag 
                which conditionally includes its body based on the value of one 
                of its attributes might be verified like this:
            </p>
            <p>
		ほとんどのライフサイクルメソッドは、
		コンテナがそのメソッドの実行後に必ずアクションを受け取るシグナルである<code>int</code> を返します。
		例えば、<code>doStartTag()</code> より返される定数
		<code>EVAL_BODY_INCLUDE</code> は、
		コンテナに
		JSP の出力レスポンス中のタグのボディを含むよう知らせます。
		ですから、条件によりその属性の一つの値に基づきボディを挿入するタグは、次のように検証されます : 
            </p>
<source><![CDATA[
tag.setValueThatResultsInInclude("correct value");
assertEquals(Tag.EVAL_BODY_INCLUDE, tag.doStartTag());
]]></source>
        </s2>

        <s2 title="ページデータへの効果の検証/Checking effects on page data">
            <p>
            In addition to "listening" for the signals that your tag sends to 
            the container, you may want to verify that the tag's execution 
            has the appropriate effects upon the page data. Use 
            <code>JspTestCase's</code> implicit objects to verify that the 
            tag has correctly modified the information. The following 
            snippet verifies that the <code>CatalogListTag</code> has placed a 
            collection of objects in the request under the key "catalogs":
            </p>
            <p>
	    自分のタグがコンテナへ送るシグナルの"受信待ち"に加えて、
	    タグの実行がページデータに対して適切な効果を与えたか検証したいでしょう。
	    タグが正しく情報を変換したか検証するには、
	    <code>JspTestCase</code> の暗黙オブジェクトを使います。
	    次のプログラム部分は
	    <code>CatalogListTag</code> が
	    "catalogs" キーのリクエストで、
	    オブジェクトの集まりを変換したか検証するためのものです。
            </p>
<source><![CDATA[
catalogListTag.doStartTag();
Collection catalogs = (Collection)request.getAttribute("catalogs");
assertNotNull(catalogs);
]]></source>
        </s2>
        
        <s2 title="タグの出力の検証/Verifying tag output">
            <p>
                Use the <code>endXXX</code> method to verify that your tag's 
                methods have resulted in the correct data being written 
                to the response.
            </p>
            <p>
		自分のタグメソッドがレスポンスに対して正しいデータを出力したか検証するためには、<code>endXXX</code> メソッドを使います。
<code>endXXX</code> 
            </p>
            <note>
                    This example uses the <code>endXXX()</code> 
                    signature from Cactus 1.2 or above.
            </note>
            <note>
		この例では Cactus 1.2 以上の <code>endXXX()</code>の書式を用いています。
            </note>
<source><![CDATA[
public void endSomeTagTest (WebResponse response)
{
    String output = response.getText();
    assertEquals("<b>expected output</b>", output);
}
]]></source>
        </s2>

    </s1>

    <s1 title="特別な例/Special Cases">
        <p>
            There are a few scenarios in custom tag testing that deserve extra
            attention.
        </p>
        <p>
	より注意しておくべき、
	カスタムタグのテストのシナリオが 2, 3 あります。
        </p>
        <s2 title="イテレーションタグ/Iteration Tags">
            <p>
                To test a tag that repeats its body processing a number of 
                times, simply create a <code>do-while</code> loop that mimics  
                the life cycle of an iteration tag:
            </p>
            <p>
		ボディを何回か繰り返し処理するタグをテストするには、
		単に、イテレーションタグのライフサイクルを真似する
		<code>do-while</code> ループを作ります : 
            </p>
<source><![CDATA[
//[...tag set up and early life cycle methods omitted...]

int count = 0;
do
{
    count++;

} while (tag.doAfterBody() == tag.EVAL_BODY_AGAIN);

tag.doEndTag();

//based on setUp we expect 9 repetitions
assertEquals(9, count);
]]></source>

<source><![CDATA[
//[...タグの設定と初期のライフサイクルメソッドは省略します...]

int count = 0;
do
{
    count++;

} while (tag.doAfterBody() == tag.EVAL_BODY_AGAIN);

tag.doEndTag();

// 設定に基づき、9 回の繰り返しを想定します
assertEquals(9, count);
]]></source>

            <p>
                You can use a count variable (such as the one illustrated 
                in the example) to check whether the tag's body was processed 
                the expected number of times.
            </p>
            <p>
		(例で示したように)
		タグのボディが期待した回数処理されたかどうかチェックするのに、
		カウンタ変数が利用可能です。
            </p>
        </s2>
        <anchor id="using_body_content"/>
        <s2 title="Body Tag/Body Tags">
            <p>
                Unless specified otherwise by the deployment descriptor, all 
                tags can include a body, which can in turn include other tags 
                or scriptlet expressions. These are automatically evaluated at 
                run time, and the content of the body is simply written out 
                if the tag signals it should be (with 
                <code>EVAL_BODY_INCLUDE </code> for instance). Nothing special 
                is required to test this sort of tag, since the tag is 
                unconcerned about its contents.
            </p>
            <p>
		別の方法で配備記述により指定されない限りは、
		全てのタグは、
		代りに他のタグやスクリプトレットの式を含んだりできるボディを持つことができます。
		これらは実行時に自動的に評価され、
		タグのシグナル要求
		(例えば<code>EVAL_BODY_INCLUDE </code>)
		があれば、単にボディのコンテンツが出力されます。
		タグは、そのコンテンツについては関与しないので、
		この種類のタグをテストするのに、
		特別なものは何も必要ありません。
            </p>
            <p>
                Testing BodyTags--tags which actually perform some processing
                on their content--is a little trickier.
                BodyTags can choose to return a constant (
                <code>EVAL_BODY_TAG</code> in JSP 1.1, 
                <code>EVAL_BODY_BUFFERED</code> in 1.2)  from 
                <code>doStartTag()</code> which signals to the container that 
                the tag would like a chance to handle its own body. 
                If it receives this result, the container calls 
                <code>pageContext.pushBody()</code> to obtain a 
                <code>BodyContent</code>object. The <code>BodyContent</code> 
                object is passed to the tag through the tag's 
                <code>setBodyContent()</code>method. The container then uses 
                this object (the old out object is saved) to capture all of the 
                response writing that goes on in the body of the tag. After the 
                tag's body has been evaluated, the tag itself has a chance to 
                do something with the result of the evaluation in it's 
                <code>doAfterBody()</code> method. After the tag has completed 
                its execution, the container restores the old out object with
                a call to <code>pageContext.popBody()</code>.
            </p>
            <p>
		実際に自分の中身を処理を行うタグである BodyTag のテストは、
		少しトリッキーです。
		BodyTag は、
		コンテナへのタグが自分のボディを処理できる機会となるシグナルとなる、

		<code>doStartTag()</code> からの戻り値となる定数
		(JSP 1.1 では<code>EVAL_BODY_TAG</code>、
		1.2 では <code>EVAL_BODY_BUFFERED</code>)
		を選択できます。
		この戻り値を受け取ったとき、
		<code>BodyContent</code> オブジェクトを得るために、
		コンテナは<code>pageContext.pushBody()</code>を呼び出します。
		タグの<code>setBodyContent()</code>メソッドを通じて、
		<code>BodyContent</code> オブジェクトがタグに渡されます。
		そして、
		タグ中で行われていることを出力する全てのレスポンスを捕らえるために、
		コンテナはこのオブジェクト(古い out オブジェクトが保存されている)
		を使います。
		タグのボディが評価された後で、
		タグの <code>doAfterBody()</code> メソッドの中で、
		タグ自身は評価の結果に対し何かを行うチャンスがあります。
		評価が完了した後で、
		コンテナは、<code>pageContext.popBody()</code> を呼び出して、
		古い out オブジェクトを取り出します。
            </p>
            <p>
                To test body tags, your test must replicate this somewhat   
                complicated lifecycle. The following code covers all of the 
                steps as they might appear in a typical test:
            </p>
            <p>
		ボディタグをテストするには、テストが、幾分複雑なライフサイクルを繰り返す必要があります : 

            </p>
<source><![CDATA[
//standard set up
//標準セットアップ
YourTag tag = new YourTag();
tag.setPageContext(this.pageContext);
tag.doStartTag();

//obtain the bodyContent object--presumably doStartTag has returned 
//EVAL_BODY_TAG or EVAL_BODY_BUFFERED.
//bodyContent オブジェクトを得る -- 恐らく doStartTag は
//EVAL_BODY_TAG と EVAL_BODY_BUFFERED のいずれかを返したと思われます
BodyContent bodyContent = this.pageContext.pushBody();
this.tag.setBodyContent(bodyContent);
this.tag.doInitBody();


//write some "output" into the bodyContent so that endXXX can test for it.
//endXX が testできるよう、bodyContentに何か出力します
bodyContent.println("Some content");
bodyContent.print("Some evaluated content " + (5 + 9));

//actually handles the processing of the body
//実際にボディの処理を扱います
tag.doAfterBody();

//after the body processing completes
//ボディの処理が完了した後で
tag.doEndTag();

//finally call popBody
//最後にpopBodyを呼びます
this.pageContext.popBody();

]]></source>
            <p>
                This sample does not fully replicate the container's handling of
                the tag (for instance, the tag would only receive the 
                <code>bodyContent</code> object if the result of 
                <code>doStartTag</code> indicated that it should do so). 
                However, in a test environment, you can make assumptions if 
                doing so simplifies the workings of the test.
            </p>
            <p>
		この例題は、コンテナがタグを扱うことを完全に反映するものではありません。
		(例えば、
		<code>doStartTag</code> の結果が、そのように指定した場合、
		タグは <code>bodyContent</code> オブジェクトのみしか受け取りません。)
		しかしながら、テスト環境においては、
		テスト作業を簡単にするように仮定を設けます		
            </p>
            <note>
                Again, you can check that the body of the tag was handled
                correctly by verifying the total output in the 
                <code>endXXX()</code> method.
            </note>
            <note>
		繰り返しになりますが、
		<code>endXXX()</code> メソッドの出力全体を検証することにより、
		タグのボディが正しく処理されたことをチェックできます。
            </note>
        </s2>
        <s2 title="TagExtraInfo クラス/TagExtraInfo classes">
            <p>
                Cactus does not offer any specific services to support the 
                testing of <code>TagExtraInfo</code> classes because they do 
                not depend on any of the implicit objects.
            </p>
            <p>
		暗黙オブジェクトのいずれにも依存しないので、
		Cactus は
		<code>TagExtraInfo</code> クラスのテストをサポートする固有のサービスを提供していません。
            </p>
        </s2>
    </s1>

    <anchor id="further_integration"/>
    <s1 title="さらなる結合テスト/Further Integration Testing">
        <p>
            You can use Cactus to test how your tag will react when put into a 
            real JSP. This allows you to verify that there are no problems with 
            the deployment descriptor, or unexpected behavior on the part of the 
            container. You accomplish this by writing a small JSP that 
            makes use of your custom tag, and then calling it from within a 
            Cactus test case. You can even use JUnit assertions within 
            scriptlets to verify certain aspects of the Tag's behavior. However,
            this method requires that you write a separate JSP for each 
            test case (or lump several cases into a single JSP). Both options 
            pose problems, so it may be  best to 
            include one or two tests of this type and rely on 
            the more traditional methods described earlier to ensure 
            total coverage.
        </p>
        <p>
	    実際の JSP に挿入したときに自分のタグどう反応するのかテストするために、
	    Cactus を使うことができます。
	    これにより、配備記述ファイルに問題が無いか、
	    コンテナの一部に期待していない振舞いが起きないかなど検証することができます。
	    これは、自分のカスタムタグを使った小さい JSP を書き、
	    Cactus テストケースから呼び出すことにより、達成されます。
	    タグの振舞いのある側面を検証するのにスクリプト中で、JUnit
	    のアサートを使うこともできます。
	    しかしながら、この方法を使うには、
	    個々のテストケースに対して、別々の JSP を書かなければなりません。
	    (あるいは、幾つかのケースを一まとめに一つの JSP に入れなければなりません。)
	    どちらの方法も問題があるので、
	    方法の 1 あるいは 2 のどちらテストを入れるのが最善の方法かわからず、
	    全体を保障するには、前に述べたより伝統的な方法に頼ったほうがいいのかもしれません。
        </p>
        <s2 title="The test JSP">
            <p>
                All the JSP needs to do is include the tag library that 
                describes the tag you are testing and makes use of it in 
                some way. You can import <code>junit.framework.Assert</code>
                to do some simple checks on the effects of the tag. 
                Here is a short example of a JSP that exercises a tag:
            </p>
            <p>
		全ての JSP でやらなければならないことは、
		どうにかして、テストおよび使おうとしているタグを記述したタグライブラリを含めることです。
		タグの影響の簡単なチェックを行うために、
		<code>junit.framework.Assert</code> をインポートすることもできます。
		タグを試す短い JSP の例は次の通りです : 
            </p>
<source><![CDATA[
<%@page import="junit.framework.Assert"%>
<%@taglib uri="WEB-INF/yourTagLib.tld" prefix="example"%>

Here is the custom tag this page verifies:
ここに、このページで検証するカスタムタグがあります:
<example:someTag variableName="foo" variableValue="bar"/>

Here is the JUnit assert that checks whether the tag correctly created a 
scripting variable named <code>foo</code> with the value "bar":
タグが、値を"bar"を持つ名前<code>foo</code>のスクリプト変数が正しく生成したかどうか
チェックする JUnit のアサーションは次のとおりです:
<%
//attempt to reference foo will cause a translation error if the tag did not 
//create the scripting variable
//タグがスクリプト変数を生成しなかったら、
//fooへの参照は変換エラーを発生させるでしょう
Assert.assertEquals("bar", foo);
%>
]]></source>
           <p>
                It's a bad idea to put too many assertions into the 
                JSP. In the example above, the creation of a scripting variable 
                can <strong>only</strong> be tested within the JSP page. (The 
                same goes for any objects in page scope, because each JSP 
                creates its own.) If you
                want to use other assertions with this type of test,
                call them in your test case after 
                <code>pageContext.include()</code> (See below for an example.)
           </p>
           <p>
		JSP にアサーションを入れすぎるのは、あまり賢明とは言えません。
		上述の例では、スクリプト変数の生成は、
		JSP ページで<strong>のみ</strong>テストできるのです。
		(ページスコープの任意のオブジェクトについても同じ事が言えます。
		なぜなら、個々の JSP は自分自身を生成するからです。)
		この種のテストにおいて他のアサーションを使いたい場合には、
		<code>pageContext.include()</code> の後で、
		自分のテストケースの中で、それらを呼び出してください。
		(下の例をご覧ください。)
           </p>
        </s2>

        <s2 title="テストケース/The TestCase">
            <p>
		テスト JSP を使うには、
		<code>JspTestCase</code> の中で、インクルードします。
		ユーティリティー関数 <code>pageContext.include()</code> は、
		これをうまく処理します : 
            </p>
<source><![CDATA[
public void testSomeTag () throws Exception
{
    pageContext.include("/test_some_tag.jsp");

    //an assert to check whether the page also mapped foo into the session
    //ページが、そのセッション中でも foo をマップしたかチェックするアサーション
    assert("bar", session.getAttribute("foo"));
}
]]></source>
            <p>
                Exceptions that result from either page translation 
                (such as required attributes being omitted, or the tag missing  
                a part of its descriptor entry) or page execution 
                (such as the tag being unable to find required data in the 
                appropriate scope) are automatically be thrown up to 
                this level. If you do not catch them there they will be 
                logged by Cactus/JUnit as failures--which is just what you want.
            </p>
            <p>
		(必要な属性が省略されたり、タグの記述子エントリの一部がなかったり、など)
		ページ変換からの結果、あるいは、
		(タグが、適切なスコープ中に、必要なデータを見つけられなかった、など)
		ページ実行の結果である例外は、
		自動的にこのレベルまでスローされます。
		そこで、キャッチしない場合、
		Cactus/JUnit により失敗
		(開発者の望んでいるのは、まさにこれです)
		としてログに書き込まれます。
            </p>
            <note>
                Any output that the test JSP generates can be checked normally 
                in the <code>endXXX</code> method.
            </note>
            <note>
		通常、
		テスト JSP 生成するどのような出力でも、
		<code>endXXX</code> メソッドにおいて、チェックできます。
            </note>
            <p>
                Of course, using this strategy means that you need to put 
                the <code>test_some_tag.jsp</code> in the specified location 
                within your web application. If you are using JSP test case 
                your build script should already deploy the redirector JSP, so 
                it should be easy to include another JSP in the build process.
            </p>
            <p>
		もちろん、この戦略を使う事は、
		<code>test_some_tag.jsp</code> を自分のウェブアプリケーションの指定された場所に置く必要があることを意味します。
		JSP テストケースを使っている場合、
		ビルド手続きにおいて他の JSP に簡単にインクルードできるように、
		自分のビルドファイルは、
		転送用 JSP を配備しなければなりません。
            </p>
        </s2>
    </s1>
  </body>
</document>


