<?xml version="1.0" encoding="UTF-8"?>
<!--======================================================================-->
<!--                                                                      -->
<!-- JBoss, the OpenSource J2EE webOS                                     -->
<!--                                                                      -->
<!-- Distributable under LGPL license.                                    -->
<!-- See terms of license at http://www.gnu.org.                          -->
<!--                                                                      -->
<!--======================================================================-->
<project default="main" name="JBoss/mod_cluster Tests" xmlns:server="http://jboss.org/ns/test/ant/server">
  
  <!-- ======================================================== -->
  <!-- Initialization                                           -->
  <!-- ======================================================== -->
  <tstamp>
    <format property="TIMENOW" pattern="yyyy-MM-dd.HH-mm" timezone="GMT"/>
  </tstamp>
  <echo message="${TIMENOW}" file="run.tstamp"/>

  <!-- =================== -->
  <!-- Basic Configuration -->
  <!-- =================== -->

  <property name="buildlog.level" value="info"/>
  <property environment="env"/>

  <!--
    Set a hostname property based on COMPUTERNAME for win32, HOSTNAME
    otherwise and initialize the node0/node1 cluster hostnames to
    localhost and ${hostname} by default. If you cannot route multicast
    traffic between localhost and hostname, then you need to specify node0
    and node1 binding in the local.properties that can in order to be able
    to run the clustering tests.
  -->
  <condition property="hostname" value="${env.COMPUTERNAME}">
    <os family="windows"/>
  </condition>
  <condition property="hostname" value="${env.HOSTNAME}">
    <not><os family="windows"/></not>
  </condition>

  <!--
    We will use local.properties file to provide some configuration to the
    testsuite mainly for the Clustering framework. Please do not checkin
    your local.properties file into CVS
  -->
  <property file="local.properties" />

  <!-- Cluster node defaults -->
  <property name="node0" value="localhost" />
  <property name="node0.http.url" value="http://${node0}:8080" />
  <property name="node0.jndi.url" value="jnp://${node0}:1099" />
  <property name="node0.jgroups.bind_addr" value="${node0}" />
  <property name="node1" value="${hostname}" />
  <property name="node1.http.url" value="http://${node1}:8080" />
  <property name="node1.jndi.url" value="jnp://${node1}:1099" />
  <property name="node1.jgroups.bind_addr" value="${node1}" />
  <!--property name="node2" value="${env.MYTESTIP_3}" />
  <property name="node2.http.url" value="http://${node2}:8080" />
  <property name="node2.jndi.url" value="jnp://${node2}:1099" />
  <property name="node2.jgroups.bind_addr" value="${node2}" />
  <property name="node3" value="${env.MYTESTIP_4}" />
  <property name="node3.http.url" value="http://${node3}:8080" />
  <property name="node3.jndi.url" value="jnp://${node3}:1099" />
  <property name="node3.jgroups.bind_addr" value="${node3}" /-->

  <!-- UDP Group, must be done before server-config.xml -->
  <!-- The value of the -u option passed to jboss -->
  <!-- A blank value will prevent the -u option from being passed -->
  <!--
    Override this in the local.properties or pass to Ant as
    -DupdGroup=128.x.x.x
  -->
  <property name="udpGroup" value="" />
  <!-- Same thing, but passed to the client -->
  <property name="jbosstest.udpGroup" value="${udpGroup}" />

  <!-- Set "jboss.dist" to JBOSS_HOME -->
  <property name="jboss.dist" value="${env.JBOSS_HOME}" />

  <!-- Propagate the dist directory as a jbosstest system property -->
  <property name="jbosstest.dist" value="${jboss.dist}" />

  <!-- Externalized locations -->
  <property name="location.resources.main" value="${basedir}/src/main/resources" />
  <property name="location.resources.test" value="${basedir}/src/test/resources" />
  <property name="location.target" value="${basedir}/target" />
  <property name="location.artifact.tests" value="${location.target}/jboss-ejb3-testsuite-tests.jar" />
  <property name="location.dependencies" value="${location.target}/dependencies" />
  <property name="location.dependencies.unpacked" value="${location.dependencies}/unpacked" />
  <property name="location.dependencies.unpacked.jboss-test:jboss.test" value="${location.dependencies.unpacked}/jboss-test" />
  <property name="location.dependencies.lib" value="${location.dependencies}/lib" />
  <property name="location.dependencies.lib.jboss-test:jboss-test" value="${location.dependencies.lib}/jboss-test.jar" />
  <property name="location.dependencies.lib.org.jboss.jbossas:server-manager" value="${location.dependencies.lib}/jboss-server-manager.jar"/>
  <property name="location.dependencies.lib.junit:junit" value="${location.dependencies.lib}/junit.jar" />
  <property name="location.dependencies.lib.apache-httpclient:commons-httpclient" value="${location.dependencies.lib}/commons-httpclient.jar" />
  <property name="location.dependencies.lib.org.jboss.naming:jnp-client" value="${location.dependencies.lib}/jnp-client.jar" />
  <property name="location.dependencies.lib.org.jboss.logging:jboss-logging-spi" value="${location.dependencies.lib}/jboss-logging-spi.jar" />
  <property name="location.apache.httpd" value="/usr/sbin"/>
  <property name="location.apache.serverroot" value="${user.home}/httpd"/>
  <property name="location.apache.config" value="conf/mod-cluster-httpd.conf"/>
  
  <path id="server.taskdef.classpath">
    <pathelement path="${location.dependencies.lib.jboss-test:jboss-test}"/>
    <pathelement path="${location.dependencies.lib.org.jboss.jbossas:server-manager}"/>
    <pathelement path="${location.dependencies.lib.org.jboss.logging:jboss-logging-spi}"/>
    <pathelement path="${location.dependencies.lib.org.jboss.naming:jnp-client}"/>
  </path>
  
  <!-- Import the jboss server run targets -->
  <import file="${location.dependencies.unpacked.jboss-test:jboss.test}/server-config.xml" />

  <!-- Module name(s) & version -->
  <property name="module.name" value="mod-cluster" />
  <property name="module.Name" value="JBoss Testsuite" />
  <property name="module.version" value="DEV" />
  <property name="module.source" value="${basedir}/src" />
  <property name="module.output" value="${basedir}/target" />
   
  <!-- Where source files live -->
  <property name="source.resources" value="${module.source}/test/resources"/>
   
  <!-- Where build generated files will go -->
  <property name="build.classes" value="${module.output}/test-classes"/>
  <property name="build.lib" value="${module.output}/test-lib"/>
  <property name="build.resources" value="${module.output}/resources"/>
  <property name="build.reports" value="${module.output}/reports"/>
  <property name="build.testlog" value="${module.output}/log"/>
  
  <!-- Define common JVM Arguments for all mod-cluster JBossAS Runtimes -->
  <property name="mod_cluster.jboss.jvmargs" value="-Xms128m -Xmx512m -XX:MaxPermSize=256m -Dorg.jboss.resolver.warning=true -Djava.net.preferIPv4Stack=true -Dresolve.dns=false" />

  <property name="junit.timeout" value="300000"/>
  
  <!--
    Define Server Configurations for mod_cluster Integration Tests
  -->
  <server:config javaHome="${env.JAVA_HOME}" jbossHome="${jboss.dist}" udpGroup="${udpGroup}">

    <server name="mod-cluster-0" host="${node0}">
      <jvmarg value="${mod_cluster.jboss.jvmargs}" />
      <sysproperty key="java.endorsed.dirs" value="${jboss.dist}/lib/endorsed" />
      <sysproperty key="jgroups.udp.ip_ttl" value="${jbosstest.udp.ip_ttl}" />
      <sysproperty key="jboss.messaging.ServerPeerID" value="0" />
    </server>

    <server name="mod-cluster-1" host="${node1}">
      <jvmarg value="${mod_cluster.jboss.jvmargs}" />
      <sysproperty key="java.endorsed.dirs" value="${jboss.dist}/lib/endorsed" />
      <sysproperty key="jgroups.udp.ip_ttl" value="${jbosstest.udp.ip_ttl}" />
      <sysproperty key="jboss.messaging.ServerPeerID" value="1" />
    </server>

    <server name="mod-cluster-2" host="${node2}">
      <jvmarg value="${mod_cluster.jboss.jvmargs}" />
      <sysproperty key="java.endorsed.dirs" value="${jboss.dist}/lib/endorsed" />
      <sysproperty key="jgroups.udp.ip_ttl" value="${jbosstest.udp.ip_ttl}" />
      <sysproperty key="jboss.messaging.ServerPeerID" value="2" />
    </server>

    <server name="mod-cluster-3" host="${node3}">
      <jvmarg value="${mod_cluster.jboss.jvmargs}" />
      <sysproperty key="java.endorsed.dirs" value="${jboss.dist}/lib/endorsed" />
      <sysproperty key="jgroups.udp.ip_ttl" value="${jbosstest.udp.ip_ttl}" />
      <sysproperty key="jboss.messaging.ServerPeerID" value="3" />
    </server>

  </server:config>

  <!-- ===================== -->
  <!-- Paths and Patternsets -->
  <!-- ===================== -->

  <!-- Define test Classpath -->
  <path id="jboss.test.classpath">
    <pathelement path="${jboss.test.path}"/>
    <pathelement path="${location.dependencies.lib.junit:junit}"/>
    <pathelement path="${location.dependencies.lib.jboss-test:jboss-test}"/>
    <pathelement path="${location.dependencies.lib.org.jboss.naming:jnp-client}"/>
    <pathelement path="${location.dependencies.lib.org.jboss.logging:jboss-logging-spi}"/>
  </path>

  <!--  Classpath used by the Test Client -->
  <path id="client.classpath">
    <!-- JBoss Client Binaries -->
  </path>
  
  <!--
    Define the Path to Server Task Definitions, used by the JBoss Server
    Run targets
  -->
  <patternset id="jboss.server.all.includes">
    <include name="conf/**" />
    <include name="deploy/**" />
    <include name="deployers/**" />
    <include name="deploy-hasingleton/**" />
    <include name="lib/**" />
  </patternset>

  <!-- Define the test pattern sets Here -->
  <patternset id="apache.integration.test.includes">
    <include name="org/jboss/modcluster/test/*TestCase.class" />
  </patternset>
  
  <!-- Add exclude elements for any tests that should be excluded -->
  <patternset id="badtest.excludes"/>

  <!-- ============================================== -->
  <!-- Macros                                         -->
  <!-- ============================================== -->
  
  <macrodef name="httpd">
    <attribute name="location" default="/usr/sbin"/>
    <attribute name="action-type" default="start"/>
    <attribute name="server-root"/>
    <attribute name="config" default="conf/mod-cluster-httpd.conf"/>
    <attribute name="failonerror" default="true"/>
     
    <sequential>
      <echo message="Will @{action-type} the apache instance at @{location} using -d @{server-root} -f @{config}"/>
      <echo message="If you need apache to run on port 80, then"/>
      <echo message="you need to start apache with admin/root rights."/>
      <echo message="If so, start/stop apache independent of this testsuite"/>
      <exec executable="@{location}/httpd" failonerror="@{failonerror}" failifexecutionfails="@{failonerror}">
        <arg value="-k"/>
        <arg value="@{action-type}"/>
        <arg value="-d"/>
        <arg value="@{server-root}"/>
        <arg value="-f"/>
        <arg value="@{config}"/>
      </exec>
    </sequential>
  </macrodef>
  
  <macrodef name="create-httpd">
    <attribute name="server-root" description="the httpd serverroot to which newconf-src should be copied"/>
    <attribute name="newconf-src" description="The name of the tests-config to copy"/>
    <sequential>
      <echo message="Updating httpd config at @{server-root}" />
      <copy todir="@{server-root}" overwrite="true" failonerror="false">
        <fileset dir="${build.resources}/test-configs/@{newconf-src}" />
      </copy>
    </sequential>
  </macrodef>

  <!-- ============================================== -->
  <!-- Targets                                        -->
  <!-- ============================================== -->
  
  <!-- Import the test jars build targets -->
  <import file="test-jars.xml"/>
   
  <!-- "main" is the default target; depends on "jars" which is imported from test-jars.xml -->
  <target name="main"
          description="Executes the default target (jars)."
          depends="init, compile-resources, jars"/>
  
  <target name="init">
    <record name="${basedir}/build.log" append="yes" action="start" loglevel="error"/>
  </target>

  <!-- Compile resource files -->
  <target name="compile-resources">
    <mkdir dir="${build.resources}"/>
    <copy todir="${build.resources}" filtering="no">
      <fileset dir="${source.resources}">
        <include name="**/*"/>
      </fileset>
    </copy>
    <mkdir dir="${build.lib}"/>
  </target>

  <target name="tests-apache-integration"
          description="Execute integration tests requiring an apache load balanced two JBoss nodes.">
       
    <!--
      Determine whether we are configured to ignore failures and
      pass the appropriate property to tests-clustering-junit.
      One property we check below and fail the build; the other is ignored.
     -->    
    <condition property="apache.integration.junit.failure.property"
               value="apache.integration.junit.failure"
               else="apache.integration.junit.failure.ignored">
      <and>
        <isset property="ignore.integration.test.failures"/>
        <equals arg1="${ignore.integration.test.failures}" arg2="true" casesensitive="false" trim="true"/>
      </and>
    </condition>
    
    <echo message="Setting up httpd config in ${location.apache.serverroot}"/>
    <create-httpd server-root="${location.apache.serverroot}" newconf-src="mod-cluster-httpd"/>
    
    <create-config baseconf="all" newconf="mod-cluster-0" newconf-src="mod-cluster-jbossas">         
      <patternset id="jboss.server.all.includes">
        <include name="conf/**" />
        <include name="deploy/**" />
        <include name="deployers/**" />
        <include name="deploy-hasingleton/**" />
        <include name="lib/**" />
      </patternset>        
    </create-config>
    
    <copy todir="${jboss.dist}/server/mod-cluster-0/deploy">
      <fileset dir="${location.target}/mod-cluster.sar"></fileset>
    </copy>
    
    <create-config baseconf="all" newconf="mod-cluster-1" newconf-src="mod-cluster-jbossas">        
      <patternset id="jboss.server.all.includes">
        <include name="conf/**" />
        <include name="deploy/**" />
        <include name="deployers/**" />
        <include name="deploy-hasingleton/**" />
        <include name="lib/**" />
      </patternset>
    </create-config>
    
    <copy todir="${jboss.dist}/server/mod-cluster-1/deploy">
      <fileset dir="${location.target}/mod-cluster.sar"></fileset>
    </copy>
    
    <!--httpd location="${location.apache.httpd}" server-root="${location.apache.serverroot}" 
           config="${location.apache.config}" action-type="start" /-->  
    
    
    <server:start name="mod-cluster-0" />
    <!--server:start name="mod-cluster-1" /-->
    
    <echo message="Nodes have started, waiting for cluster to stablize..." />

    <echo message="Going to call target tests-clustering-unit" />

    <antcall target="tests-clustering-unit">
      <param name="cluster.includes.refid" value="apache.integration.test.includes" />
      <param name="jboss-junit-configuration" value="ModCluster" />
      <param name="tests-clustering-unit.failure.property" value="${apache.integration.junit.failure.property}"/>
    </antcall>

    <server:stop name="mod-cluster-0" />
    <!--server:stop name="mod-cluster-1" /-->

    <!--httpd location="${location.apache.httpd}" server-root="${location.apache.serverroot}" 
           config="${location.apache.config}" action-type="stop" /-->
    
    <!-- See if junit set the failure property that triggers build failure -->
    <fail message="JUnit tests failed; see test reports for details" if="apache.integration.junit.failure"/>
   
  </target>

  <!-- Executes a set of tests in a clustered environment -->
  <target name="tests-clustering-unit">
    
    <mkdir dir="${build.reports}" />
    <mkdir dir="${build.testlog}" />
    <junit dir="${module.output}" printsummary="on"
           haltonerror="false" haltonfailure="false"
           fork="true" timeout="${junit.timeout}"
           failureProperty="${tests-clustering-unit.failure.property}">

      <sysproperty key="java.net.preferIPv4Stack" value="true" />

      <sysproperty key="jboss.internal-server.serverroot" value="${jboss.dist}/server" />
      <sysproperty key="jbosstest.deploy.dir" file="${build.lib}" />
      <sysproperty key="jbosstest.cluster.node0" value="${node0}" />
      <sysproperty key="jbosstest.cluster.node0.http.url" value="${node0.http.url}" />
      <sysproperty key="jbosstest.cluster.node0.jndi.url" value="${node0.jndi.url}" />
      <sysproperty key="jbosstest.cluster.node0.serverroot" value="${jboss.dist}/server/${jbosstest.cluster.node0.config}" />
      <sysproperty key="jbosstest.cluster.node1" value="${node1}" />
      <sysproperty key="jbosstest.cluster.node1.http.url"  value="${node1.http.url}" />
      <sysproperty key="jbosstest.cluster.node1.jndi.url" value="${node1.jndi.url}" />
      <sysproperty key="jbosstest.cluster.node1.serverroot" value="${jboss.dist}/server/${jbosstest.cluster.node1.config}" />
      <sysproperty key="build.testlog" value="${build.testlog}" />
      <sysproperty key="log4j.configuration" value="file:${build.resources}/log4j.xml" />
      <!-- Pass along any jbosstest.* system properties -->
      <syspropertyset>
        <propertyref prefix="jbosstest." />
      </syspropertyset>

      <!-- Lets pass some parameters from local.properties file as system properties -->
      <syspropertyset>
        <propertyref prefix="org.apache." />
      </syspropertyset>
      <syspropertyset>
        <propertyref prefix="apache" />
      </syspropertyset>
      <syspropertyset>
        <propertyref prefix="node" />
      </syspropertyset>
  
      <!--jvmarg value="${junit.jvm.options}"/>
      <jvmarg value="-Xrunjdwp:transport=dt_socket,address=8789,server=y,suspend=y"/>
      <jvmarg value="-Djava.compiler=NONE"/>
      <jvmarg value="-Xdebug"/>
      <jvmarg value="-Xnoagent"/-->

      <classpath>
        <pathelement location="${build.classes}" />
        <pathelement location="${build.resources}" />
        <path refid="jboss.test.classpath"/>
      </classpath>

      <!-- definition of the property for keeping results between configuration -->
      <sysproperty key="jboss-junit-configuration" value="${jboss-junit-configuration}" />
      <formatter classname="org.jboss.ant.taskdefs.XMLJUnitMultipleResultFormatter"
                 usefile="${junit.formatter.usefile}" extension="-${jboss-junit-configuration}.xml" />

      <batchtest todir="${build.reports}" haltonerror="false"
                 haltonfailure="false" fork="true">

        <fileset dir="${build.classes}">
          <patternset refid="${cluster.includes.refid}" />
          <patternset refid="badtest.excludes" />
        </fileset>
      </batchtest>
    </junit>
  </target>

  <!-- Launches a test assuming the AS instances are already running -->
  <target name="one-cluster-test-nostart" if="test">

    <property name="jboss-junit-configuration" value="ModCluster" />

    <echo message="Going to call target tests-clustering-unit for ${test}" />

    <antcall target="tests-clustering-unit">
      <param name="cluster.includes.refid" value="one.test.includes" />
      <param name="jboss-junit-configuration" value="${jboss-junit-configuration}" />
      <param name="jbosstest.cluster.web.cache.config" value="standard-session-cache" />
      <param name="jbosstest.cluster.node0.config" value="mod-cluster-0" />
      <param name="jbosstest.cluster.node1.config" value="mod-cluster-1" />
    </antcall>

  </target>

</project>