Webアプリ開発エンジニアのための技術情報サイト「テックスコア」

1.2. Cactusテストの実行

Cactusフレームワークには、Cactusの理解を深めるためのサンプルコードが付属しています。ファイルセットの中のsample-servletというディレクトリ下にサンプルプログラムのソースがあります。Cactusのサンプルコードをビルドし、実行してみましょう。以下の手順で、行います。

  1. 準備
  2. 設定ファイルの編集
  3. ビルドとテストの実行

まずはじめに、準備をします。当然ですが、Java 2 がインストールされている必要があります。JAVA_HOMEなどの環境変数も正しく設定されていることを確認してください。また、Cactusはサーバサイドのプログラムのテストを行うフレームワークです。ですから、サーブレットやJSPを動かす、サーブレットエンジンがインストールされている必要があります。ここでは、Tomcatを使用します。Tomcatの以下の設定を確認してください。

  • インストールディレクトリ
  • (tomcatをサーバ上で動かしている場合)アクセスするのに使用しているポート
  • (tomcatをサーバ上で動かしている場合)停止する際に使用するポート

このサンプルプログラムは、テストを実行する前に、別にテスト用のサーブレットエンジンを起動し、テスト終了後、テスト用のサーブレットエンジンを停止します。よって、サーブレットエンジンが稼動中であれば、使用中のポートとは別のポートをテスト用に指定する必要があります。

最後に、サンプルコードをビルドするためにはAntが必要です。インストールされていなければ、インストールしてください。また、サンプルテストを実行するのに、antのjunitタスクを使用していますので、その設定も行います。具体的にはantのホームにあるlibディレクトリにjunit.jarを配置します。(詳しくは、antの解説をご覧下さい)

次に、ビルドのための設定ファイルを編集します。(Cactusのルートディレクトリ)/sample-servlet/buildディレクトリの下に、build.properties.sampleという名前のファイルがあります。これは、ビルドの設定ファイル、build.propertiesのサンプルファイルになります。このサンプルファイルの名前をbuild.propertiesに変更して、編集します。

build.propertiesファイルの内容は以下のようになっています。

1: # The location of the Cactus framework jar
2: cactus.framework.jar = ../lib/cactus.jar
3: 
4: # The location of the Cactus custom Ant tasks jar
5: cactus.ant.jar = ../lib/cactus-ant.jar
6: 
7: # The location of the Servlet API 2.2 jar. Note: This file is not provided
8: # with the Cactus distribution (you'll have to get it).
9: servlet.jar = ../lib/servlet.jar
10:
11:# The location of the AspectJ runtime jar
12:aspectjrt.jar = ../lib/aspectjrt.jar
13:
14:# The location of the log4j jar (optional). Only needed if you want Cactus to
15:# generate logs
16:log4j.jar = ../lib/log4j-1.2rc1.jar
17:
18:# The location of the Commons Httpclient jar
19:commons.httpclient.jar = ../lib/commons-httpclient-20020421.jar
20:
21:# The location of the Junit jar
22:junit.jar = ../lib/junit.jar
23:
24:# The location of the Httpunit jar
25:httpunit.jar = ../lib/httpunit.jar
26:
27:# -----------------------------------------------------------------------------
28:# Optional properties
29:# -----------------------------------------------------------------------------
30:
31:# The port to use for starting the servers during unit testing. If not
32:# specified, it defaults to port 8080.
33:test.port = 8081
34:
35:# Servlet engine locations for the tests
36:
37:# Note: If you don't want to run the test on a given servlet engine, just
38:#       comment it's home property. For example, if you don't want to run the
39:#       tests on the Resin 1.2, comment the "resin.home.12" property.
40:
41:#resin.home.20 = c:/Apps/resin-2.1.0
42:#resin.home.13 = xxx
43:tomcat.home.40 = /usr/local/tomcat
44:#orion.home.15 = c:/Apps/orion-1.5.4
45:
46:# Note: weblogic.home.61 is the location of BEA Home and NOT the location of
47:# wlserver6.1, inside the bea home directory.
48:#weblogic.home.61 = c:/Apps/bea
49:
50:# Note: There seems to be a bug in Orion 1.4 preventing it to run correctly
51:#       the testSendUserData() test. It runs fine in Orion 1.5 though.
52:#orion.home.14 = xxx

まず、1〜25行目で必要なjarファイルのパスを設定しています。デフォルトでは、必要なファイルは、(Cactusのホーム)/libディレクトリ下に納めることになっています。servlet.jar以外のファイルは、Cactusの配布パッケージに含まれています。servlet.jarのみ、(Cactusのホーム)/lib下にコピーしてください。

33行目では、テストの時にサーバが使用するポート番号を指定します。デフォルトでは、この記述はコメントアウトされ、8080が指定されます。もし、Tomcatなどのサーバが既に起動中なのであれば、テストサーバ用に別のポート番号を指定する必要があります。ここでは、8081を指定します。

41〜52行目では、サーブレットエンジンの場所を指定しています。サーブレットエンジンの指定の方法は以下のとおりです。

(サーブレットエンジン名).home.(バージョン)=(サーブレットエンジンがインストールされているディレクトリ)

上の設定ファイルでは、サーブレットエンジンにTomcat 4.0を使用すること、Tomcat 4.0 は/usr/local/tomcatにインストールされていることが記されています。また、サーブレットエンジンは複数指定できます。サンプルコードは、複数のサーブレットエンジン上のテストが一度にできるように設計されています。

これで、build.propertiesの編集は終了です。

本来ならば、これで設定は完了なのですが、サンプルコードのバグがあります。以下の変更作業を行ってください。

  • build-share.xmlの編集
    以下の記述をコメントアウトします。
    <fail message="Missing property or property pointing to an invalid
           file (check your build.properties file)" unless="properties.ok"/> 
  • もし、Tomcatが稼動中であれば、(Cactusのルートディレクトリ)/sample-servlet/conf/test/tomcat40ディレクトリの下のserver.xmlファイルを編集します。Server要素のport属性の値は、Cactus用Tomcatの停止を行う際に使用するポート番号です。サンプルを動かすサーバ上でTomcatが稼動中の場合は、port属性の値を、現在空いているポートの番号に変更してください。ここで指定する番号はbuild.propertiesで指定しているポート番号とも異なる必要があります。

それでは、ビルドして、テストを実行してみましょう。(Cactusのルートディレクトリ)/servlet-sample/buildディレクトリで以下のコマンドを実行します。

ant test.tomcat.40

実行すると、以下のように出力されます。

Buildfile: build.xml

check.test.tomcat.40:

display.properties:
...


check.properties:

init:

compile.sample:
    [mkdir] Created dir: /usr/local/java/cactus1.3/sample-servlet/target/classes/sample
    [javac] Compiling 13 source files to /usr/local/java/cactus1.3/sample-servlet/target/classes/
sample

compile.unit:
    [javac] Compiling 15 source files to /usr/local/java/cactus1.3/sample-servlet/target/classes/
unit

compile:

prepare.testwar.log4j:
     [copy] Copying 1 file to /usr/local/java/cactus1.3/sample-servlet/target/lib

prepare.test:
     [copy] Copying 1 file to /usr/local/java/cactus1.3/sample-servlet/target/conf
     [copy] Copying 1 file to /usr/local/java/cactus1.3/sample-servlet/target/conf

testwar:
     [copy] Copying 1 file to /usr/local/java/cactus1.3/sample-servlet/target/lib
     [copy] Copying 1 file to /usr/local/java/cactus1.3/sample-servlet/target/lib
     [copy] Copying 1 file to /usr/local/java/cactus1.3/sample-servlet/target/lib
    [mkdir] Created dir: /usr/local/java/cactus1.3/sample-servlet/target/test
      [war] Building war: /usr/local/java/cactus1.3/sample-servlet/target/test/test.war

prepare.test.tomcat.40:
     [echo] tomcat.home.40 = /usr/local/tomcat
...

test.tomcat.40:

start.tomcat.40:
...
test:
...
stop.tomcat.40:
     [java] Stopping service Tomcat-Standalone
     [java] HttpConnector[8081] Stopping background thread
     [java] StandardHost[localhost]: Removing web application at context path /test
[runservertests] Server stopped !

BUILD SUCCESSFUL


まず、build.propertiesファイルの内容を確認しています。次に、テストされるサーブレット、テストクラス、Cactusの設定ファイルが自動的に作成されます。その後、サーブレットエンジンの起動、テストが実行されます。テスト実行時の出力をみると、junitが拡張されていることがよくわかると思います。最後に、サーブレットエンジンを停止し、終了です。

設定や、Cactusの処理の流れなど、次章で詳しく見ていきます。

(実習課題1)

Cactusをインストールし、サンプルコードを実行しなさい。
(注1)Java1.4ではコンパイルがうまくできない場合があります。その場合は、エラーが出ているクラスで、自分自身をimportしている部分をコメントアウトしてください。
(注2) antが原因でうまくいかない場合は、Cactus付属のantを試してみてください。



前のページへ TECHSCOREのTOPページへ 次のページへ
techscore(トップページへ)
TECHSCORE書店
TECHSCOREトップページJavaSQLXMLリッチクライアントモデリングセマンティックWebその他技術Tuigwaa