
    <!-- Global properties -->
    
    <!-- Default port on which to start the containers -->
    <property name="cactus.port" value="8080"/>

    <!-- Tmp dir used by Cactus for its operations -->
    <property name="cactus.target.dir" value="target"/>

    <!-- Default Cactus test files to include in the test -->
    <property name="cactus.src.includes" value="**/*Test*.java"/>

    <!-- Default Cactus test files to exclude from the test -->
    <property name="cactus.src.excludes" 
        value="**/AllTests.java,**/Test*All.java"/>

    <!-- Default Java VM to use for java Ant tasks -->
    <property name="cactus.jvm" value="java"/>
    
    <!-- Default webapp context -->
    <property name="cactus.context" value="cactustest"/>

    <!-- Decides whether the test result will be printed to the console or
         not. Default is true -->
    <property name="cactus.junit.usefile" value="false"/>   

    <!-- Location of junit test reports for the Cactus tests -->
    <property name="cactus.reports.dir" 
      value="${cactus.target.dir}/test-cactus-reports"/>

    <!-- Decides whether to halt the build on error and/or on failure -->
    <property name="cactus.halt.on.failure" value="true"/>
    <property name="cactus.halt.on.error" value="true"/>

    <!-- JUnit Runner to use for executing the tests. Valid values are
         "text" and "swing" -->
    <property name="cactus.runner" value="text"/>

    <!--
       ========================================================================
         Set the properties related to the source tree.
       ========================================================================
    -->
    <property name="cactus.conf.dir" value="confs"/>
    <property name="cactus.lib.dir" value="lib"/>
    <property name="cactus.lib.client.dir" value="${cactus.lib.dir}/client"/>
    <property name="cactus.lib.server.dir" value="${cactus.lib.dir}/server"/>
    <property name="cactus.lib.common.dir" value="${cactus.lib.dir}/common"/>
    <property name="cactus.script.dir" value="scripts"/>

    <property name="cactus.target.classes.dir" value="${cactus.target.dir}/classes"/>
    <property name="cactus.target.conf.dir" value="${cactus.target.dir}/conf"/>

    <!-- Cactus related jar locations -->
    <property name="cactus.ant.jar" 
        value="${cactus.lib.client.dir}/@cactus.ant.jar.name@"/>

    <property name="cactus.jar" 
        value="${cactus.lib.common.dir}/@cactus.jar.name@"/>
    <property name="cactus.cargo.jar" 
        value="${cactus.lib.common.dir}/@cargo.jar.name@"/>
    <property name="cactus.log4j.jar" 
        value="${cactus.lib.common.dir}/@log4j.jar.name@"/>
    <property name="cactus.aspectjrt.jar" 
        value="${cactus.lib.common.dir}/@aspectjrt.jar.name@"/>
    <property name="cactus.commons.logging.jar" 
        value="${cactus.lib.common.dir}/@commons.logging.jar.name@"/>
    <property name="cactus.junit.jar" 
        value="${cactus.lib.common.dir}/@junit.jar.name@"/>
    <property name="cactus.httpunit.jar" 
        value="${cactus.lib.common.dir}/@httpunit.jar.name@"/>

    <property name="cactus.commons.httpclient.jar" 
        value="${cactus.lib.client.dir}/@commons.httpclient.jar.name@"/>
    <property name="cactus.nekohtml.jar" 
        value="${cactus.lib.client.dir}/@nekohtml.jar.name@"/>
    <property name="cactus.xerces.jar" 
        value="${cactus.lib.client.dir}/@xerces.jar.name@"/>
    <property name="cactus.xmlapis.jar" 
        value="${cactus.lib.client.dir}/@xmlapis.jar.name@"/>

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

        <!-- Filters -->
        <filter token="cactus.port" value="${cactus.port}"/>
        <filter token="cactus.context" value="${cactus.context}"/>

        <!-- Initialize Cactus custom Ant task needed for running the server
             tests -->
        <taskdef resource="cactus.tasks" classpath="${cactus.ant.jar}"/>         

    </target>

    <!--
       ========================================================================
         Compile the Cactus tests.
       ========================================================================
    -->
    <target name="cactus.compile" if="cactus.src.dir" description="Compile the Cactus tests">

        <mkdir dir="${cactus.target.classes.dir}"/>

        <javac destdir="${cactus.target.classes.dir}" srcdir="${cactus.src.dir}">
            <classpath>
                <pathelement location="${cactus.jar}"/>
                <pathelement location="${cactus.cargo.jar}"/>
                <pathelement location="${cactus.log4j.jar}"/>
                <pathelement location="${cactus.aspectjrt.jar}"/>
                <pathelement location="${cactus.commons.logging.jar}"/>
                <pathelement location="${cactus.junit.jar}"/>
                <pathelement location="${cactus.commons.httpclient.jar}"/>
                <pathelement location="${cactus.httpunit.jar}"/>
                <path refid="cactus.classpath"/>
            </classpath>
        </javac>

        <!-- Copy non-java files -->
        <copy todir="${cactus.target.classes.dir}">
            <fileset dir="${cactus.src.dir}">
                <exclude name="**/*.java"/>
                <exclude name="**/*.html"/>
            </fileset>
        </copy>
        
    </target>

    <!--
       ========================================================================
         Copy the Cactus test classes if sources are provided
       ========================================================================
    -->
    <target name="cactus.copyclasses" if="cactus.src.dir" depends="cactus.compile">

        <!-- Copy the Cactus test classes -->
        <copy todir="${cactus.target.dir}/${cactus.context}/WEB-INF/classes">
            <fileset dir="${cactus.target.classes.dir}"/>
        </copy>

    </target>

    <!--
       ========================================================================
         Merge the user's web.xml file with the Cactus-provided one. If the 
         source web.xml file does not exist, simply return the cactus-provided 
         web.xml file.
       ========================================================================
    -->
    <target name="cactus.war.merge.check">

        <condition property="cactus.war.should.merge">
            <available 
              file="${cactus.target.dir}/${cactus.context}/WEB-INF/web.xml"/>
        </condition>
            
    </target>

    <target name="cactus.war.merge.yes" depends="cactus.war.merge.check"
      if="cactus.war.should.merge">

        <!-- Perform a web.xml merge -->
        <webxmlmerge 
          srcfile="${cactus.target.dir}/${cactus.context}/WEB-INF/web.xml"
          mergefile="${cactus.conf.dir}/web.xml"
          destfile="${cactus.target.dir}/${cactus.context}/WEB-INF/web-temp.xml">
            <xmlcatalog>
                <dtd
                  publicId="-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
                  location="${cactus.conf.dir}/web-app.dtd"/>
                <dtd
                  publicId="-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"
                  location="${cactus.conf.dir}/web-app.dtd"/>
            </xmlcatalog>
        </webxmlmerge>

        <!-- Rename the merged web.xml -->
        <move file="${cactus.target.dir}/${cactus.context}/WEB-INF/web-temp.xml"
            tofile="${cactus.target.dir}/${cactus.context}/WEB-INF/web.xml"/>
 
    </target>

    <target name="cactus.war.merge.no" depends="cactus.war.merge.check"
      unless="cactus.war.should.merge">

        <!-- Copy the Cactus-provided web.xml file -->
        <copy tofile="${cactus.target.dir}/${cactus.context}/WEB-INF/web.xml"
            file="${cactus.conf.dir}/web.xml"/>
 
    </target>

    <target name="cactus.war.merge" 
      depends="cactus.war.merge.yes,cactus.war.merge.no"/>
    
    <!--
       ========================================================================
         Cactifify a plain war (pointed to by the cactus.war property).
       ========================================================================
    -->
    <target name="cactus.war"
        description="Cactify the war">
                
        <unwar src="${cactus.war}" 
            dest="${cactus.target.dir}/${cactus.context}"/>

        <!-- Merge the web.xml files -->
        <antcall target="cactus.war.merge"/>

        <!-- Copy the log4j configuration files -->
        <copy tofile="${cactus.target.dir}/${cactus.context}/WEB-INF/classes/log4j.properties"
            file="${cactus.conf.dir}/log_server.properties"/>
            
        <antcall target="cactus.copyclasses"/>
    
        <!-- Copy the Cactus jars that should be on server side classpath. 
             Note: we used the named jars as it should
             be possible to override the locations of jars and they should not
             be expected to be in ${cactus.lib.*.dir}. For example this feature
             is used by the Maven plugin for Cactus. -->
        <copy todir="${cactus.target.dir}/${cactus.context}/WEB-INF/lib"
          file="${cactus.jar}"/>
        <copy todir="${cactus.target.dir}/${cactus.context}/WEB-INF/lib"
          file="${cactus.cargo.jar}"/>
        <copy todir="${cactus.target.dir}/${cactus.context}/WEB-INF/lib"
          file="${cactus.log4j.jar}"/>
        <copy todir="${cactus.target.dir}/${cactus.context}/WEB-INF/lib"
          file="${cactus.aspectjrt.jar}"/>
        <copy todir="${cactus.target.dir}/${cactus.context}/WEB-INF/lib"
          file="${cactus.commons.logging.jar}"/>
        <copy todir="${cactus.target.dir}/${cactus.context}/WEB-INF/lib"
          file="${cactus.junit.jar}"/>
        <copy todir="${cactus.target.dir}/${cactus.context}/WEB-INF/lib"
          file="${cactus.httpunit.jar}"/>

        <!-- Copy the JSP Redirector -->
        <copy todir="${cactus.target.dir}/${cactus.context}">
            <fileset dir="${cactus.conf.dir}">
                <include name="jspRedirector.jsp"/>
            </fileset>          
        </copy>
                    
    </target>
   
    <!--
       ========================================================================
         Trigger the Cactus test by running a JUnit Test Runner
       ========================================================================
    -->
    <target name="cactus.test">

        <!-- Copy the log4j client side configuration file in a location that
            we put in the classpath of the JUnit Test Runner -->
        <copy tofile="${cactus.target.conf.dir}/log4j.properties"
            file="${cactus.conf.dir}/log_client.properties"/>

        <!-- Create the test report directory before running the tests -->
        <mkdir dir="${cactus.reports.dir}"/>

        <!-- Call the correct test runner -->
        <antcall target="cactus.test.${cactus.runner}" inheritRefs="true"/>
        
    </target>

    <target name="cactus.test.check.result">

        <!-- Decides whether to halt the build if errors or failures have
              occurred -->

        <condition property="cactus.test.error.shouldstop">
          <and>
            <isset property="cactus.junit.error"/>
            <istrue value="${cactus.halt.on.error}"/>
          </and>
        </condition>
        <fail if="cactus.test.error.shouldstop"
            message="There were test errors"/>

        <condition property="cactus.test.failure.shouldstop">
          <and>
            <isset property="cactus.junit.failure"/>
            <istrue value="${cactus.halt.on.failure}"/>
          </and>
        </condition>
        <fail if="cactus.test.failure.shouldstop"
            message="There were test failures"/>

    </target>

    <!--
       ========================================================================
         Run the Cactus tests using the JUnit text runner.
       ========================================================================
    -->
    <target name="cactus.test.text">

        <junit printsummary="yes" failureproperty="cactus.junit.failure" 
            errorproperty="cactus.junit.error" fork="yes">

            <sysproperty key="cactus.contextURL"
                value="http://localhost:${cactus.port}/${cactus.context}"/>

            <!-- This is needed to override the default mapping which is 
                 "/FilterRedirector". The reason is that in web.xml we have
                 mapped the filter redirector to "test/filterRedirector.jsp"
                 as some containers do not support mapping to a directory -->
            <sysproperty key="cactus.filterRedirectorName"
                value="test/filterRedirector.jsp"/>
                 
            <classpath>
                <pathelement location="${cactus.target.conf.dir}"/>
                <pathelement location="${cactus.target.classes.dir}"/>
                <pathelement location="${cactus.jar}"/>
                <pathelement location="${cactus.cargo.jar}"/>
                <pathelement location="${cactus.log4j.jar}"/>
                <pathelement location="${cactus.aspectjrt.jar}"/>
                <pathelement location="${cactus.commons.logging.jar}"/>
                <pathelement location="${cactus.junit.jar}"/>
                <pathelement location="${cactus.commons.httpclient.jar}"/>
                <pathelement location="${cactus.httpunit.jar}"/>
                <pathelement location="${cactus.nekohtml.jar}"/>
                <pathelement location="${cactus.xerces.jar}"/>
                <pathelement location="${cactus.xmlapis.jar}"/>
                <path refid="cactus.classpath"/>
            </classpath>

            <formatter type="xml"/>
            <formatter type="plain" usefile="${cactus.junit.usefile}"/>

            <!-- TODO: Automatically discover Cactus tests -->

            <batchtest todir="${cactus.reports.dir}">
                <fileset dir="${cactus.src.dir}" 
                    includes="${cactus.src.includes}" 
                    excludes="${cactus.src.excludes}"/>
            </batchtest>

        </junit>

        <antcall target="cactus.test.check.result"/>

    </target>

    <!--
       ========================================================================
         Run the Cactus tests using the JUnit swing runner.
       ========================================================================
    -->
    <target name="cactus.test.swing">

        <java classname="junit.swingui.TestRunner" fork="yes">

            <sysproperty key="cactus.contextURL"
                value="http://localhost:${cactus.port}/${cactus.context}"/>

            <!-- This is needed to override the default mapping which is 
                 "/FilterRedirector". The reason is that in web.xml we have
                 mapped the filter redirector to "test/filterRedirector.jsp"
                 as some containers do not support mapping to a directory -->
            <sysproperty key="cactus.filterRedirectorName"
                value="test/filterRedirector.jsp"/>
                 
            <classpath>
                <pathelement location="${cactus.target.conf.dir}"/>
                <pathelement location="${cactus.target.classes.dir}"/>
                <pathelement location="${cactus.jar}"/>
                <pathelement location="${cactus.cargo.jar}"/>
                <pathelement location="${cactus.log4j.jar}"/>
                <pathelement location="${cactus.aspectjrt.jar}"/>
                <pathelement location="${cactus.commons.logging.jar}"/>
                <pathelement location="${cactus.junit.jar}"/>
                <pathelement location="${cactus.commons.httpclient.jar}"/>
                <pathelement location="${cactus.httpunit.jar}"/>
                <pathelement location="${cactus.nekohtml.jar}"/>
                <pathelement location="${cactus.xerces.jar}"/>
                <pathelement location="${cactus.xmlapis.jar}"/>
                <path refid="cactus.classpath"/>
            </classpath>

            <arg value="-noloading"/>

        </java>

        <antcall target="cactus.test.check.result"/>

    </target>
          
    <!--
       ========================================================================
         Clean generated files (including distributables)
       ========================================================================
    -->
    <target name="cactus.clean" depends="cactus.init" 
      description="Clean target directory">
        <delete includeEmptyDirs="true">
            <fileset dir="${cactus.target.dir}/${cactus.context}"/>
        </delete>
    </target>
