<?xml version="1.0" encoding="UTF-8" ?>
<project name="carrot2.common.webtest">
  <!--
        Deploys the provided WAR file and runs webtests on it.
    -->
  <macrodef name="webtest-war">
    <attribute name="cargo.tmp.dir"  />
    <attribute name="war.path"  />
    <attribute name="war.base.name"  />
    <attribute name="webtest.tests.dir" />
    <attribute name="webtest.report.dir" />
    <attribute name="webtest.port" default="8181" />
    
    <sequential>
      <!-- Deploy the WAR file -->
      <taskdef resource="cargo.tasks">
        <classpath>
          <fileset dir="${cargo.home}" />
        </classpath>
      </taskdef>

      <property name="tomcat.config.dir" location="@{cargo.tmp.dir}/config" />
      <property name="tomcat.log.dir" location="@{cargo.tmp.dir}/log" />
        
      <delete dir="@{cargo.tmp.dir}" />
      <mkdir dir="${tomcat.log.dir}"/>
      <mkdir dir="${tomcat.config.dir}"/>
      
      <cargo containerId="tomcat5x" home="${tomcat.home}" output="${tomcat.log.dir}/output.log" 
          log="${tomcat.log.dir}/cargo.log" action="start" wait="100">
        <configuration home="${tomcat.config.dir}">
          <property name="cargo.servlet.port" value="@{webtest.port}"/>
          <property name="cargo.logging" value="high"/>
          <deployable type="war" file="@{war.path}"/>
        </configuration>
      </cargo>
      <echo>Started: http://localhost:@{webtest.port}/@{war.base.name}/</echo>

      <!-- Run webtests -->
      <webtest-deployed webtest.tests.dir="@{webtest.tests.dir}"
                     webtest.report.dir="@{webtest.report.dir}"
                     webtest.host="localhost"
                     webtest.port="@{webtest.port}"
                     webtest.basepath="/@{war.base.name}">
      </webtest-deployed>
      
      <cargo containerId="tomcat5x" home="${tomcat.home}" output="${tomcat.log.dir}/output.log" 
          log="${tomcat.log.dir}/cargo.log" action="stop" wait="100">
        <configuration home="${tomcat.config.dir}" />
      </cargo>
      <echo>Started: http://localhost:@{webtest.port}/@{war.base.name}/</echo>
      
      <!-- 
           In theory we should be able to delete @{cargo.tmp.dir} here, but for some
           reason it's still locked at this point.
        -->
      <delete dir="@{cargo.tmp.dir}" failonerror="false" />
    </sequential>
  </macrodef>

  <!--
        Runs webtests on an already deployed application.
    -->
  <macrodef name="webtest-deployed">
    <attribute name="webtest.tests.dir" />
    <attribute name="webtest.report.dir" />
    <attribute name="webtest.host" default="localhost" />
    <attribute name="webtest.port" default="8080" />
    <attribute name="webtest.basepath" default="/" />

    <sequential>
      <ant antfile="@{webtest.tests.dir}/webtest.xml"
           inheritall="true"
           dir="@{webtest.tests.dir}">
        <property name="wt.config.resultfile" value="@{webtest.report.dir}/webtest-results.xml" />
        <property name="wt.config.resultpath" location="@{webtest.report.dir}" />
        <property name="wt.config.browser" value="FF3" />
        <property name="wt.headless" value="true" />
  
        <property name="wt.config.host" value="@{webtest.host}" />
        <property name="wt.config.protocol" value="http" />
        <property name="wt.config.basepath" value="@{webtest.basepath}" />
        <property name="wt.config.port" value="@{webtest.port}" />
      </ant>
    </sequential>
  </macrodef>

  <target name="war.prepare">
    <condition property="webtest.war.resources.available">
      <and>
        <isset property="cargo.home" />
        <isset property="webtest.home" />
        <isset property="tomcat.home" />
      </and>
    </condition>
  </target>

  <target name="deployed.check">
    <condition property="webtest.deployed.resources.available">
      <and>
        <isset property="webtest.home" />
      </and>
    </condition>

    <fail unless="webtest.deployed.resources.available">
Please set webtest.home to run webtests. 
    </fail>
  </target>
</project>
