<?xml version="1.0"?>

<!--
  =============================================================================
    Build file for the Cactus EJB Sample subproject. It shows how to use
    Cactus for unit testing EJBs.

    The following Ant tasks need to be available in your ant installation (i.e.
    the Ant task themselves and their dependent jars need to be put in
    ANT_HOME/lib):

        junit                  [REQUIRED] JUnit Ant task

    Check the build.properties file to find out what properties need to be set
    in order to successfully run this script.

    This script should be started with the following command line:

        ant <target>

    Run "ant -projecthelp" to get a list of available targets.
  =============================================================================
-->
<project name="Cactus EJB Sample" default="dist" basedir=".">

    <!-- Give user a chance to override without editing this file
       (and without typing -D each time it compiles it) -->
  <property file="build.properties" />
  <property file="${user.home}/build.properties" />

  <!-- Global project properties -->
  <property name="project.name.text" value="Cactus EJB Sample"/>
  <property name="project.name.file" value="cactus-sample-ejb"/>
  <property name="project.version" value="@version@"/>

  <!-- Prefix to add to all distributable files -->
  <property name="project.prefix" value="jakarta-"/>

  <!-- Generic properties -->
  <property name="year" value="@year@"/>
  <property name="debug" value="on"/>
  <property name="optimize" value="off"/>
  <property name="deprecation" value="off"/>

  <!-- Properties for the Cactus tests -->
  <property name="cactus.port" value="8080"/>
  <property name="cactus.jboss.startUpWait" value="2000"/>
  
  <!--
     ========================================================================
       Set the properties related to the source tree.
     ========================================================================
  -->
  <property name="src.dir" location="src"/>
  <property name="src.java.dir" location="${src.dir}/java"/>
  <property name="src.cactus.dir" location="${src.dir}/test-cactus"/>
  <property name="src.webapp.dir" location="${src.dir}/webapp"/>
  <property name="src.ejb.dir" location="${src.dir}/ejb"/>
  <property name="src.application.dir" location="${src.dir}/application"/>

  <!--
     ========================================================================
       Set the properties related to the target area
     ========================================================================
  -->
  <!-- Destination locations for the build -->
  <property name="target.dir" location="target"/>
  <property name="target.classes.dir" location="${target.dir}/classes"/>
  <property name="target.classes.java.dir"
      location="${target.classes.dir}/java"/>
  <property name="target.classes.cactus.dir"
      location="${target.classes.dir}/cactus"/>
  <property name="target.testreports.dir"
      location="${target.dir}/test-reports"/>

  <!-- Distribution directory, i.e. where the expanded distibutable files
       are located -->
  <property name="dist.dir" location="dist"/>

  <!--
     ========================================================================
       Set the properties related to the required libraries
     ========================================================================
  -->

  <!-- Libraries required for the sample project -->
  <property name="j2ee.jar"
      location="../../lib/@j2ee-dist.jar.name@"/>
  <path id="project.classpath">
    <pathelement location="${j2ee.jar}"/>
  </path>

  <!-- Libraries required for the Cactus tests -->
  <property name="aspectjrt.jar"
      location="../../lib/@aspectjrt.jar.name@"/>
  <property name="cactus.jar"
      location="../../lib/@cactus.jar.name@"/>
  <property name="cactus.ant.jar"
      location="../../lib/@cactus.ant.jar.name@"/>
  <property name="cargo.jar"
      location="../../lib/@cargo.jar.name@"/>
  <property name="commons.httpclient.jar"
      location="../../lib/@commons.httpclient.jar.name@"/>
  <property name="commons.logging.jar"
      location="../../lib/@commons.logging.jar.name@"/>
  <property name="junit.jar"
      location="../../lib/@junit.jar.name@"/>
  <path id="cactus.classpath">
    <path refid="project.classpath"/>
    <pathelement location="${aspectjrt.jar}"/>
    <pathelement location="${cactus.jar}"/>
    <pathelement location="${cactus.ant.jar}"/>
    <pathelement location="${cargo.jar}"/>
    <pathelement location="${commons.httpclient.jar}"/>
    <pathelement location="${commons.logging.jar}"/>
    <pathelement location="${junit.jar}"/>
  </path>

  <!--
     ========================================================================
       Initialize the build. Must be called by all targets
     ========================================================================
  -->
  <target name="init">

    <condition property="properties.ok">
      <and>
        <available file="${j2ee.jar}"/>
      </and>
    </condition>
    <fail unless="properties.ok">Missing property or property pointing to an invalid file
(check your build.properties file)</fail>

    <taskdef resource="cactus.tasks" classpathref="cactus.classpath"/>

    <tstamp/>

  </target>

  <!--
     ========================================================================
       Compiles the sources
     ========================================================================
  -->
  <!-- Compiles the java sources -->
  <target name="compile.java" depends="init">

    <mkdir dir="${target.classes.java.dir}"/>
    <javac destdir="${target.classes.java.dir}"
        deprecation="${deprecation}" optimize="${optimize}">
      <src path="${src.java.dir}"/>
      <classpath>
        <path refid="project.classpath"/>
      </classpath>
    </javac>

  </target>

  <!-- Compiles the Cactus test sources -->
  <target name="compile.cactus" depends="compile.java">

    <mkdir dir="${target.classes.cactus.dir}"/>
    <javac destdir="${target.classes.cactus.dir}"
        deprecation="${deprecation}" optimize="${optimize}">
      <src path="${src.cactus.dir}"/>
      <classpath>
        <path refid="cactus.classpath"/>
        <pathelement location="${target.classes.java.dir}"/>
      </classpath>
    </javac>

  </target>

  <target name="compile" depends="compile.java, compile.cactus"
      description="Compile the sources"/>

  <!--
     ========================================================================
       Create the runtime ejb-jar file.
     ========================================================================
  -->
  <target name="ejb" depends="compile.java"
      description="Generate the runtime ejb-jar">

    <!-- ATM, only support containers which do not need an ejb compilation
         step. For example: JBoss -->
    <jar jarfile="${target.dir}/${project.name.file}.jar">
      <fileset dir="${target.classes.java.dir}"/>
      <metainf dir="${src.ejb.dir}"/>
    </jar>

  </target>

  <!--
     ========================================================================
       Create the runtime ear file.
     ========================================================================
  -->
  <target name="ear" depends="ejb"
      description="Generate the runtime ear">

    <ear earfile="${target.dir}/${project.name.file}.ear"
        appxml="${src.application.dir}/application.xml">
      <fileset file="${target.dir}/${project.name.file}.jar"/>
    </ear>

  </target>

  <!--
     ========================================================================
       Generate the distributable files
     ========================================================================
  -->
  <target name="dist" depends="clean, ear, test"
      description="Generate the distributable files">

    <copy todir="${dist.dir}"
        file="${target.dir}/${project.name.file}.ear"/>

  </target>

  <!--
     ========================================================================
       Clean generated files (including distributables)
     ========================================================================
  -->
  <target name="clean" depends="init" description="Clean all generated files">

    <delete dir="${target.dir}"/>
    <delete dir="${dist.dir}"/>

  </target>

  <!--
     ========================================================================
       Create the war file that will hold the Cactus tests. At this point in
       time, as there isn't a <cactifyear> Ant task, we need to create
       manually a war file even if our application doesn't need one.
     ========================================================================
  -->
  <target name="test.war" depends="compile.cactus">

    <war warfile="${target.dir}/${project.name.file}.war"
         webxml="${src.webapp.dir}/WEB-INF/web.xml"/>

    <!-- Cactify the web-app archive -->
    <cactifywar srcfile="${target.dir}/${project.name.file}.war"
        destfile="${target.dir}/${project.name.file}-cactified.war"
        mergewebxml="${src.webapp.dir}/WEB-INF/cactus-web.xml">
      <classes dir="${target.classes.cactus.dir}"/>
      <!-- Needed for Clover coverage reports -->
      <@clover.begin@lib file="${clover.jar}"/@clover.end@>
    </cactifywar>

    <!-- Cactify the EAR -->    
    <ear earfile="${target.dir}/${project.name.file}-cactified.ear"
        appxml="${src.application.dir}/cactus/application.xml">   
      <zipfileset excludes="META-INF/application.xml"
          src="${target.dir}/${project.name.file}.ear"/>
      <fileset file="${target.dir}/${project.name.file}-cactified.war"/>
    </ear>
        
  </target>

  <!--
     ========================================================================
       Run the tests on the containers for which the Ant property
       "cactus.home.[container name]" has been defined.
     ========================================================================
  -->
  <target name="test.prepare" depends="ear, test.war">
  
    <!-- Prepare the directories for the JUnit reports -->
    <mkdir dir="${target.testreports.dir}"/>
    <@j2ee13.begin@mkdir dir="${target.testreports.dir}/jboss3x"/@j2ee13.end@>
    <mkdir dir="${target.testreports.dir}/orion1x"/>
    <mkdir dir="${target.testreports.dir}/orion2x"/>

  </target>

  <target name="test" depends="test.prepare"
      description="Run the tests on the defined containers">

    <!-- Run the tests -->
    <cactus earfile="${target.dir}/${project.name.file}-cactified.ear" 
        fork="yes" failureproperty="tests.failed">
      <classpath>
        <path refid="project.classpath"/>
        <@clover.begin@pathelement location="${clover.jar}"/@clover.end@>
        <pathelement location="${target.classes.java.dir}"/>
        <pathelement location="${target.classes.cactus.dir}"/>
      </classpath>
      <containerset>
        <@j2ee13.begin@jboss3x if="cactus.home.jboss3x"
            startUpWait="${cactus.jboss.startUpWait}"
            dir="${cactus.home.jboss3x}"
            output="${target.testreports.dir}/jboss3x.out"
            todir="${target.testreports.dir}/jboss3x"/@j2ee13.end@>
        <orion1x if="cactus.home.orion1x"
            dir="${cactus.home.orion1x}" port="${cactus.port}"
            output="${target.testreports.dir}/orion1x.out"
            todir="${target.testreports.dir}/orion1x"/>
        <orion2x if="cactus.home.orion2x"
            dir="${cactus.home.orion2x}" port="${cactus.port}"
            output="${target.testreports.dir}/orion2x.out"
            todir="${target.testreports.dir}/orion2x"/>
      </containerset>
      <formatter type="brief" usefile="false"/>
      <formatter type="xml"/>
      <batchtest>
        <fileset dir="${src.cactus.dir}">
          <include name="**/Test*.java"/>
          <exclude name="**/Test*All.java"/>
        </fileset>
      </batchtest>
    </cactus>

    <!-- Generate the JUnit reports -->
    <@j2ee13.begin@junitreport todir="${target.testreports.dir}/jboss3x">
      <fileset dir="${target.testreports.dir}/jboss3x"
          includes="TEST-*.xml"/>
      <report todir="${target.testreports.dir}/jboss3x"
          format="frames"/>
    </junitreport@j2ee13.end@>
    <junitreport todir="${target.testreports.dir}/orion1x">
      <fileset dir="${target.testreports.dir}/orion1x"
          includes="TEST-*.xml"/>
      <report todir="${target.testreports.dir}/orion1x"
          format="frames"/>
    </junitreport>
    <junitreport todir="${target.testreports.dir}/orion2x">
      <fileset dir="${target.testreports.dir}/orion2x"
          includes="TEST-*.xml"/>
      <report todir="${target.testreports.dir}/orion2x"
          format="frames"/>
    </junitreport>

    <fail if="tests.failed">At least one test failed!</fail>

  </target>

</project>
