FilterTestCase の原則/FilterTestCase Principles

Last update : July 6 2002
Doc for : v1.3

Cactusについて
  • Cactus とは
  • ニュース
  • 変更履歴
  • 特徴/開発状況
  • 目標
  • ロードマップ/ToDo
  • 協力者
  • 協力者募集
  • Cactus ユーザ
  • テスト済環境 ...
  • ライセンス


  • ダウンロード
  • ダウンロード


  • ドキュメント
  • Cactus の仕組み
  • さぁ始めよう
  • モック対コンテナ
  • Javadocs
  • └Javadocs
  • よくある質問


  • Howto ガイド
  • クラスパス Howto
  • 設定 Howto
  • アップグレードHowto
  • テストケース Howto
  • セキュリティHowto
  • Ant Howto
  • HttpUnit Howto
  • サンプル Howto
  • EJB Howto
  • IDE Howto
  • JUnitEE Howto


  • サポート
  • Bug DB
  • メーリングリスト


  • その他
  • 名前の由来
  • ロゴコンテスト
  • 参考文献
  • アクセス状況
  • └WebAlizer


  • 開発者向け
  • CVS
  • コード規約
  • ビルドの結果


  • いつ使うのか/When to use ?

    Your test case class should extend FilterTestCase whenever you are unit testing :

    次のようなものを単体テストする時にはいつでも、 テストケースクラスはFilterTestCaseを拡張しなければなりません :

    • Servlet Filters,
    • Servlet フィルター
    • Any java code that uses Filter API objects (FilterConfig, ...)
    • Filter API オブジェクトを使った、どんな java コードでも Any java code that uses Filter API objects (FilterConfig など)

    Note 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.

    Note フィルターは Servlet API 2.3 にのみ存在するので、 これらをテストを記述したい場合には、 Servlet API 2.3 用のバージョンの Cactus を使わなければなりません。

    Note Filter unit testing is only available in Cactus 1.2 and above.

    Note フィルター単体テストは Cactus 1.2 以上でのみ利用可能です。


    提供される暗黙的なオブジェクト/Provided Implicit Objects

    Cactus automatically initializes the implicit objects for you and they are made available to your setUp(), testXXX() and tearDown() methods as instance variables of the FilterTestCase class (and thus as instance variables of your test case class as it extends FilterTestCase).

    Cactus は開発者のために、自動的に暗黙的なオブジェクトを初期化します。 そして、それらは FilterTestCase クラスのインスタンス変数として、 setUp()testXXX()tearDown() メソッドを利用可能にします。 (その結果、テストケースクラスのインスタンス変数として、 FilterTestCaseを拡張するように)

    Note You may ask yourself how Cactus initializes these objects. The mechanism is described in the How it works guide.

    Note Cactus はこれらのオブジェクトをどうやって初期化するのか気になるかもしれません。 その機構はどのように動くか のガイドに詳しく書かれています。

    The provided implicit objects are :

    提供される暗黙的なオブジェクトは次の通りです :

    request

    See ServletTestCase request implicit object for documentation.

    ServletTestCaseの、 request 暗黙オブジェクトのドキュメントをご覧ください。


    response

    See ServletTestCase response implicit object for documentation.

    See ServletTestCase response 暗黙オブジェクトのドキュメントをご覧ください。


    config

    Instance variable name   config  
    Class name   org.apache.cactus.server.FilterConfigWrapper , which inherits from javax.servlet.FilterConfig  
    インスタンス変数名   config  
    クラス名   javax.servlet.FilterConfig を継承した org.apache.cactus.server.FilterConfigWrapper  

    Cactus wraps the original Filter Config for two reasons :

    Cactus では2つの理由から、オリジナルの Filter Config をラップしました :

    • In order to provide additional methods. For example, it is possible to initialise filter parameters without setting them in web.xml, etc...,
    • 追加メソッドを提供するため。 例えば、web.xml で設定せずに、 フィルターパラメーターを初期化できるなどです。
    • 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).
    • オリジナルのものではなく、ラップされた Servlet Context を 返せるように。その理由は、 Servlet Context は、 ページ転送や、挿入を行うのに使われ、 オリジナルのリクエストや応答をこれらのメソッドに、 渡す必要があるためです。 ラップされたリクエストがあるので、 (ラップされたものではない)オリジナルリクエストを渡すのに、 Servlet Context をラップする必要があります。

    The config implicit object will contain all initialisation parameters defined in web.xml under the Filter Redirector filter definition.

    config 暗黙オブジェクトには、 Filter Redirector のフィルター定義の下に、 web.xml で定義された初期化パラメーターが 入っています。

    See the javadoc for the org.apache.cactus.server.FilterConfigWrapper class for all details. You should also look at the samples provided in the Cactus distribution.

    詳しくはorg.apache.cactus.server.FilterConfigWrapper クラスの javadoc をご覧ください。 See the javadoc for the また、Cactus ディストリビューションで提供されるサンプルもご覧ください。


    filterChain

    Instance variable name   filterChain  
    Class name   javax.servlet.FilterChain  
    インスタンス変数名   filterChain  
    クラス名   javax.servlet.FilterChain  

    Note Cactus does not wrap the filter chain.

    Note Cactus ではフィルターチェインをラップしません。



    Tips と Tricks/Tips and Tricks

    チェインになった次のフィルターのシミュレーション/Simulating the next fitler in chain

    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 :

    自分のフィルターの単体テストを行う時、 単体テストの一部として、他のフィルターが自分のフィルターにテスト中に何を返すかを決められるよう、 フィルターチェイン次のフィルターをシミュレートしたいと思います。 次に、どうすればできるか示します : Here is how you could do that :

    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);
    [...]
    }
    

    See the samples provided as part of the Cactus distribution.

    Cactus ディストリビューションの一部として提供されるサンプルをご覧ください。



    例題/Sample

    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 web.xml.

    これは、Filter 単体テストの雰囲気を味わうための、 とても基本的な例題です。 広範囲な例については、ディストリビューション中のサンプルを見てください。 ここでテストするフィルターは、 単にレスポンスに対して、ヘッダとフッタを加えるものです。 ヘッダおよびフッタテキストは web.xmlにおいて、 パラメーターとして定義されています。

    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());
    }
    



    Copyright © 2000-2002 The Apache Software Foundation. All Rights Reserved.