J2EE RI サーバ上での EJB のテスト/EJB Testing with J2EE RI

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
  • コード規約
  • ビルドの結果


  • 序文/Forewords

    This tutorial shows how to write test cases for unit testing EJB methods. The EJB used in this example is a simple Session Bean.

    本チュートリアルでは、 EJB メソッドを単体テストするためのテストケースの記述法について説明します。 ここ例で使われる EJB は簡単なセッション Bean です。

    This tutorial assumes the reader has already understood how to use Cactus to test Servlet. To test an EJB, you need to prepare a war to contain the cactus setting and test cases, and then prepare a jar of your ejb. You need to match the JNDI name in the ejb jar and the war.

    このチュートリアルでは、 読者が既にサーブレットをテストするためのCactus の利用法を理解していると仮定しています。 EJB をテストするには、 Cactus の設定やテストケースを入れるための WAR ファイルを準備し、 自分の EJB の jar を準備する必要があります。 JNDI 名を EJB jar ファイル、WAR ファイルに合わせて設定しておく必要があります。

    Note This is an EJB 1.1 example but the same principles are valid for EJB 2.0

    Note これは EJB 1.1 の例題ですが、同じ原則は EJB 2.0 についても有効です。


    EJB Jar ファイル/EJB Jar

    ステップ 1 : サンプル EJB の準備/Step 1 : Prepare the Sample EJB

    The EJB used in this tutorial is a simple Session Bean with a method for converting Yen to Dollar.

    このチュートリアルで使われる EJB は、 円をドルに変換するメソッドを持つ単純なセッション Bean です。

    リモート EJB/EJB Remote

    package org.apache.cactus.sample.ejb;
    
    import java.rmi.*;
    import javax.ejb.*;
    
    public interface Converter extends EJBObject
    {
        public double convertYenToDollar(double theYenAmount) throws RemoteException;
    }
    

    ホーム EJB/EJB Home

    package org.apache.cactus.sample.ejb;
    
    import java.rmi.*;
    import javax.ejb.*;
    
    public interface ConverterHome extends EJBHome
    {
        public Converter create() throws RemoteException, CreateException;
    }
    

    EJB Bean

    package org.apache.cactus.sample.ejb;
    
    import javax.ejb.*;
    
    public class ConverterEJB implements SessionBean
    {
        private SessionContext context;
    
        public double convertYenToDollar(double theYenAmount)
        {
            return theYenAmount / 100.0;
        }
    
        public ConverterEJB()
        {
        }
    
        public void ejbCreate() throws CreateException
        {
        }
    
        public void setSessionContext(SessionContext theContext)
        {
            this.context = theContext;
        }
    
        public void ejbActivate()
        {
        }
    
        public void ejbPassivate()
        {
        }
    
        public void ejbRemove()
        {
        }
    }
    


    ステップ 2 : EJB 配備記述ファイルの準備 (ejb-jar.xml)/Step 2 : Prepare EJB Deployment Descriptor (ejb-jar.xml)

    Prepare the standard EJB Deployment Descriptor for the Session EJB :

    セッション EJB のための EJB 標準配備記述ファイルを準備します :

    <?xml version="1.0" encoding="UTF-8"?>
    
    <!DOCTYPE ejb-jar PUBLIC
    '-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 1.1//EN'
    'http://java.sun.com/j2ee/dtds/ejb-jar_1_1.dtd'>
    
    <ejb-jar>
      <display-name>testejb</display-name>
      <enterprise-beans>
        <session>
          <description>Converter Session Bean</description>
          <display-name>Converter</display-name>
          <ejb-name>Converter</ejb-name>
          <home>org.apache.cactus.sample.ejb.ConverterHome</home>
          <remote>org.apache.cactus.sample.ejb.Converter</remote>
          <ejb-class>org.apache.cactus.sample.ejb.ConverterEJB</ejb-class>
          <session-type>Stateless</session-type>
          <transaction-type>Container</transaction-type>
        </session>
      </enterprise-beans>
      <assembly-descriptor>
        <container-transaction>
          <method>
    	<ejb-name>Converter</ejb-name>
    	<method-intf>Remote</method-intf>
    	<method-name>*</method-name>
          </method>
          <trans-attribute>NotSupported</trans-attribute>
        </container-transaction>
      </assembly-descriptor>
    </ejb-jar>
    

    ステップ 3 : EJB ランタイムの準備(J2EE RI でのみ有効)/Step 3 : Prepare EJB Runtime (only valid for J2EE RI)

    <?xml version="1.0" encoding="ISO-8859-1"?>
    
    <j2ee-ri-specific-information>
      <server-name></server-name>
      <rolemapping />
    
      <enterprise-beans>
        <ejb>
          <ejb-name>Converter</ejb-name>
          <jndi-name>ejb/Converter</jndi-name>
        </ejb>
      </enterprise-beans>
    
    </j2ee-ri-specific-information>
    


    Cactus ウェブアプリケーション/Cactus Web Application

    ステップ 1 : テストコードの準備/Step 1 : Prepare test code

    package org.apache.cactus.sample.ejb;
    
    import javax.naming.*;
    import javax.rmi.*;
    import junit.framework.*;
    
    import org.apache.cactus.*;
    
    public class ConverterTest extends ServletTestCase
    {
        private Converter converter;
    
        public ConverterTest(String name)
        {
            super(name);
        }
    
        public static Test suite()
        {
            return new TestSuite(ConverterTest.class);
        }
    
        public void setUp()
        {
            Context ctx = new InitialContext();
            ConverterHome home = (ConverterHome)
                PortableRemoteObject.narrow(ctx.lookup("java:comp/ejb/Converter"),
                ConverterHome.class);
            this.converter = home.create();
        }
    
        public void testConvert() throws Exception
        {
            double dollar = this.converter.convertYenToDollar(100.0);
            assertEquals("dollar", 1.0, dollar, 0.01);
        }
    }
    

    ステップ 2 : ウェブアプリケーション配備記述ファイルの準備 (web.xml)/Step 2 : Prepare Web Application Deployment Descriptor (web.xml)

    <?xml version="1.0" encoding="UTF-8"?>
    
    <!DOCTYPE web-app
        PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"
        "http://java.sun.com/j2ee/dtds/web-app_2.2.dtd">
    
    <web-app>
    
        <servlet>
            <servlet-name>ServletRedirector</servlet-name>
            <servlet-class>org.apache.cactus.server.ServletTestRedirector</servlet-class>
        </servlet>
    
        <servlet-mapping>
            <servlet-name>ServletRedirector</servlet-name>
            <url-pattern>/ServletRedirector/</url-pattern>
        </servlet-mapping>
    
    [...]
    
        <ejb-ref>
            <ejb-ref-name>ejb/Converter</ejb-ref-name>
            <ejb-ref-type>Session</ejb-ref-type>
            <home>org.apache.cactus.sample.ejb.ConverterHome</home>
            <remote>org.apache.cactus.sample.ejb.Converter</remote>
        </ejb-ref>
    
    </web-app>
    

    ステップ 3 : ウェブアプリケーションのパッケージ化/Step 3 : Package Web Application

    Package the web application into a war and then deploy it. Please note that the ejb-ref-name in the deployment descriptor need to match with the JNDI name of the test EJB.

    ウェブアプリケーションをパッケージ化し、配備します。 JNDI 名と テスト用 EJB を対応させるために、 ejb-ref-name が配備記述ファイルに必要であることに注意してください。


    ステップ 4 : (J2EE RI 1.2.1 でのみ有効) : War ランタイム/Step 4 : (Only valid to J2EE RI 1.2.1): War Runtime

    <j2ee-ri-specific-information>
    
      <server-name/>
      <rolemapping/>
    
      <web>
        <display-name>test</display-name>
        <context-root>test</context-root>
        <ejb-ref>
          <ejb-ref-name>ejb/Converter</ejb-ref-name>
          <jndi-name>ejb/Converter</jndi-name>
        </ejb-ref>
      </web>
    
    </j2ee-ri-specific-information>
    

    ステップ 5 : 配備/Step 5 : Deploy

    You need to deploy the war and the ejb-jar.xml based on the deployment procedure of your servlet container and your ejb container.

    使用しているサーブレットコンテナと EJB コンテナの配備手続きに従って、 war および ejb-jar.xml を配備する必要があります。





    [訳注:これは 漆島賢二 が翻訳しました。日本語訳に対するコメントがあれば こちらに送ってください]
    Copyright © 2000-2002 The Apache Software Foundation. All Rights Reserved.