<?xml version="1.0" ?>
<!--
   Licensed to the Apache Software Foundation (ASF) under one or more
   contributor license agreements.  See the NOTICE file distributed with
   this work for additional information regarding copyright ownership.
   The ASF licenses this file to You under the Apache License, Version 2.0
   (the "License"); you may not use this file except in compliance with
   the License.  You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
-->

<project name="Beehive/WSM/Test" default="test" basedir=".">

    <import file="../wsm-imports.xml"/>
    <import file="tomcat-imports.xml"/>

    <property name="test.root" location="${basedir}"/>
    <property name="build.dir" location="build"/>
    <property name="test.logs" location="${build.dir}/logs"/>

    <property name="junit.src" location="src/junit"/>
    <property name="junit-services.src" location="src/junit-services"/>
    <property name="junit-server.src" location="webapp/tests"/>

    <property name="build.classes" location="${build.dir}/classes"/>
    <property name="build.gen-src-wsdl" location="${build.dir}/gen-src-wsdl"/>
    <property name="build.gen-src-apt" location="${build.dir}/tmpbeansrc"/>

    <property name="junit.classes" location="${build.classes}/junit"/>
    <property name="junit-server.classes" location="${build.classes}/junit-server"/>

    <property name="build.gen-tests" location="${build.classes}/gen-tests"/>

    <property name="webapp.dir" location="webapp/build"/>

    <property name="axis.lib.dir" location="../external/axis-1.3"/>
    <property name="test.wsdl.file" location="${junit.src}/org/apache/beehive/wsm/test/jsr181/wsdl/web/wsdl2javatest.wsdl"/>

    <property name="server.root.url" value="http://localhost:8080"/>
    <property name="webapp.name" value="wsmWeb"/>
    <property name="webapp.waitfor.url" value="${server.root.url}/${webapp.name}"/>

    <path id="test.classpath">
        <path refid="velocity.dependency.path"/>
        <path refid="xbean.dependency.path"/>
        <path refid="junit.dependency.path"/>
        <path refid="servlet.dependency.path"/>
        <path refid="commons-codec.dependency.path"/>
        <path refid="commons-discovery.dependency.path"/>
        <path refid="commons-logging.dependency.path"/>
        <path refid="controls.dependency.path"/>
        <path refid="log4j.dependency.path"/>
        <path refid="annogen.dependency.path"/>

        <pathelement location="../build/jars/beehive-jsr181.jar"/>
        <pathelement location="../build/jars/beehive-wsdltypes.jar"/>
        <pathelement location="../build/jars/beehive-wsm.jar"/>
        <pathelement location="../build/jars/beehive-wsm-axis1.jar"/>

        <fileset refid="axis.fileset"/>
        <fileset refid="axis-wsdl4j.fileset"/>
        <fileset refid="axis-jaxrpc.fileset"/>
        <fileset refid="axis-saaj.fileset"/>

        <pathelement location="${webapp.dir}/WEB-INF/classes"/>
    </path>

    <target name="test">
        <echo message="--------------------------------------------------"/>
        <echo message="|         WSM test starting                       |"/>
        <echo message="--------------------------------------------------"/>
        <antcall target="clean"/>

        <antcall target="build.junit"/>
        <antcall target="test.junit"/>

        <antcall target="build.webapp"/>
        <antcall target="build.webapp.client"/>
        <antcall target="test.server"/>
        <echo message="--------------------------------------------------"/>
        <echo message="|         WSM test ending                         |"/>
        <echo message="--------------------------------------------------"/>
    </target>

    <target name="clean" depends="clean.webapp">
        <delete dir="${build.dir}"/>
        <delete file="velocity.log"/>
    </target>

    <target name="build" depends="dirs,build.junit,build.webapp"/>

    <target name="dirs">
        <mkdir dir="${build.classes}"/>
        <mkdir dir="${build.gen-src-wsdl}"/>
        <mkdir dir="${build.gen-src-apt}"/>
        <mkdir dir="${build.gen-tests}"/>
        <mkdir dir="${junit.classes}"/>
        <mkdir dir="${webapp.dir}/WEB-INF/temp"/>
        <mkdir dir="${test.logs}"/>
    </target>

    <target name="build.junit-services" description="Build the standalone web services">
        <taskdef name="xmlbeanbuild" 
                 classname="org.apache.xmlbeans.impl.tool.XMLBean" 
                 classpathref="xbean.dependency.path"/>

        <xmlbeanbuild classpathref="xbean.dependency.path"
                      schema="src/schemas"
                      classgendir="${junit.classes}"
                      failonerror="true"/>

        <taskdef name="apt"
                 classname="org.apache.beehive.controls.runtime.generator.AptTask"
                 classpathref="test.classpath"
                 onerror="fail"/>

        <apt srcdir="${junit-services.src}"
             destdir="${junit.classes}"
             gendir="${build.gen-src-apt}"
             classpathref="test.classpath"
             compileByExtension="true"
             debug="true"
             nocompile="false"/>
    </target>

    <target name="build.junit" description="Build the standalone tests">
        <mkdir dir="${junit.classes}"/>

        <property name="cp" refid="test.classpath"/>
        <echo>cp: ${cp}</echo>

        <antcall target="build.junit-services"/>

        <javac srcdir="${junit.src}"
               destdir="${junit.classes}"
               classpathref="test.classpath"
               debug="on"
               optimize="on"
               verbose="false"
               failonerror="true"/>

        <copy todir="${junit.classes}">
            <fileset dir="${junit.src}" includes="**/*.xml"/>
            <fileset dir="${junit.src}" includes="**/*.wsdl"/>
            <fileset dir="src" includes="schemas/*.wsdl"/>
        </copy>
    </target>

    <target name="run.wsdl2java" description="Generates a Java class from a wsdl.">
        <taskdef name="wsdl2AJava" 
                 classname="org.apache.beehive.wsm.tools.wsdl2ajava.WSDL2AJavaTask"
                 classpathref="test.classpath"/>

        <wsdl2AJava outputdir="${build.gen-src-wsdl}" wsdlfile="${test.wsdl.file}">
            <classpath>
                <path refid="test.classpath"/>
            </classpath>
        </wsdl2AJava>

        <taskdef name="apt"
                 classname="org.apache.beehive.controls.runtime.generator.AptTask"
                 classpathref="test.classpath"
                 onerror="fail"/>

        <apt srcdir="${build.gen-src-wsdl}"
             destdir="${build.gen-tests}"
             gendir="${build.gen-src-apt}"
             classpathref="test.classpath"
             compileByExtension="true"
             debug="true"
             nocompile="false"/>
    </target>

    <target name="test.junit" description="Run the standalone tests">
        <mkdir dir="${test.logs}"/>
        <mkdir dir="${build.gen-src-wsdl}"/>
        <mkdir dir="${build.gen-tests}"/>

        <antcall target="run.wsdl2java"/>

        <junit failureproperty="wsmjunittestfailed" printsummary="on" tempdir="${build.dir}" fork="yes">
<!-- due to some configuration problems, asserts can't be enabled yet... -->
<!--            <jvmarg value="-ea:org.apache.beehive.wsm..."/> -->
            <formatter type="plain"/>
            <classpath>
                <path refid="test.classpath"/>
                <pathelement location="${junit.classes}"/>
                <pathelement location="${build.gen-tests}"/>
            </classpath>
            <batchtest filtertrace="off" todir="${test.logs}">
                <fileset dir="src/junit">
                    <include name="**/*Test.java"/>
                    <exclude name="**/Abstract*Test.java"/>
                </fileset>
            </batchtest>
        </junit>

        <fail if="wsmjunittestfailed" message="The WSM Tests failed"/>
    </target>

    <target name="test.server" description="Run the server tests">
        <parallel>
            <antcall target="server-start"/>
            <sequential>
                <antcall target="ensure.deployed"/>
                <echo message="JUnit logs in: ${test.logs}"/>

                <!-- execute JUnit tests -->
                <antcall target="junit.server"/>

                <antcall target="undeploy"/>
                <echo message="Stopping server"/>
                <antcall target="server-stop"/>
            </sequential>
        </parallel>
        <fail if="wsmservertestfailed" message="The WSM Tests failed"/>
    </target>

    <target name="junit.server" description="Execute the JUnit tests against a running server">
        <mkdir dir="${test.logs}"/>

        <junit failureproperty="wsmservertestfailed" printsummary="on" tempdir="${build.dir}" fork="yes">
            <jvmarg value="-ea:org.apache.beehive.wsm..."/>
            <sysproperty key="wsm.test.url" value="${webapp.waitfor.url}"/>
            <formatter type="plain"/>
            <classpath>
                <path refid="test.classpath"/>
                <pathelement location="${junit-server.classes}"/>
                <pathelement location="${build.gen-tests}"/>
                <pathelement location="${webapp.dir}/WEB-INF/classes"/>
            </classpath>
            <batchtest filtertrace="off" todir="${test.logs}">
                <fileset dir="${junit-server.src}">
                    <include name="**/*Test.java"/>
                </fileset>
            </batchtest>
        </junit>
    </target>

    <target name="build.webapp.client" description="Build the client-side JUnit tests">
        <mkdir dir="${junit-server.classes}"/>

        <javac srcdir="${junit-server.src}"
               destdir="${junit-server.classes}"
               classpathref="test.classpath"
               debug="on"
               optimize="on"
               verbose="false"
               failonerror="true"/>
    </target>

    <target name="build.webapp">
        <ant dir="webapp/" target="build" inheritAll="false"/>
    </target>

    <target name="clean.webapp">
        <ant dir="webapp/" target="clean" inheritAll="false"/>
    </target>

    <!-- ================================================================ -->
    <!--                                                                  -->
    <!-- Targets for deploying the webapp on a server                     -->
    <!--                                                                  -->
    <!-- ================================================================ -->
    <target name="deploy" description="Deploy webapp">
        <ant antfile="tomcat-imports.xml" target="deploy" inheritAll="false">
            <property name="webapp.dir" location="${webapp.dir}"/>
            <property name="context.path" value="${webapp.name}"/>
        </ant>
    </target>

    <target name="undeploy" description="Undeploy webapp">
        <ant antfile="tomcat-imports.xml" target="undeploy" inheritAll="false">
            <property name="context.path" value="${webapp.name}"/>
        </ant>
    </target>

    <target name="redeploy" description="Redeploy webapp">
        <ant antfile="tomcat-imports.xml" target="redeploy" inheritAll="false">
            <property name="context.path" value="${webapp.name}"/>
        </ant>
    </target>

    <!-- ================================================================ -->
    <!--                                                                  -->
    <!-- Targets for starting / stopping a server                         -->
    <!--                                                                  -->
    <!-- ================================================================ -->
    <target name="server-start" description="Start server with asserts enabled for org.apache.beehive">
        <ant antfile="tomcat-imports.xml" target="start.with.shmem" inheritAll="false">
            <property name="java.options" value="-ea:org.apache.beehive..."/>
        </ant>
    </target>

    <target name="server-stop" description="Stop server">
        <ant antfile="tomcat-imports.xml" target="stop" inheritAll="false"/>
    </target>

    <target name="ensure.deployed" description="Deploy webapp for the test recorder">
        <echo>Ensuring that the webapp ${webapp.name} is deployed on a running server at the url ${webapp.waitfor.url}</echo>

        <waitfor maxwait="5" maxwaitunit="second" timeoutproperty="unavailable">
            <http url="${webapp.waitfor.url}"/>
        </waitfor>
        <antcall target="do.deploy"/>
        <antcall target="do.redeploy"/>
        <fail if="still.unavailable" message="Couldn't find webapp with path ${webapp.waitfor.url}"/>
    </target>

    <target name="do.deploy" if="unavailable">
        <echo>Webapp is not deployed; deploying</echo>

        <antcall target="deploy"/>
        <echo>...deploy complete</echo>

        <waitfor maxwait="120" maxwaitunit="second" timeoutproperty="still.unavailable">
            <http url="${webapp.waitfor.url}"/>
        </waitfor>
    </target>

    <target name="do.redeploy" unless="unavailable">
        <echo>Webapp is deployed; undeploy and redeploy</echo>

        <antcall target="undeploy"/>
        <echo>...undeploy complete</echo>

        <antcall target="deploy"/>
        <echo>...deploy complete</echo>

        <waitfor maxwait="120" maxwaitunit="second" timeoutproperty="still.unavailable">
            <http url="${webapp.waitfor.url}"/>
        </waitfor>
    </target>

    <target name="usage">
        <echo message=""/>
        <echo message=""/>
        <echo message="================================================================"/>
        <echo message="|                          Usage                               |"/>
        <echo message="================================================================"/>
        <echo message="----------------------------------------------------------------"/>
        <echo message="|                      Standard Targets                        |"/>
        <echo message="----------------------------------------------------------------"/>
        <echo message="build   - Build all classes needed for running the tests."/>
        <echo message="clean   - Delete all classes needed for running the tests."/>
        <echo message="test     - Run 'build', 'run.test'."/>
        <echo message="----------------------------------------------------------------"/>
    </target>
    
</project>
