<?xml version="1.0" encoding="utf-8" ?>
 <!--
 * 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="etch-tests" basedir="." default="help">
    <description>Functional Tests</description>
    <property name="Etch.basedir" location="${basedir}/.." />
    <import file="${Etch.basedir}/build-support/etch.includes.xml" />
    <target name="help"><echo>Please select a target...</echo></target>
    
    <!-- import etch task -->
    <taskdef
        onerror="report"
        resource="org/apache/etch/tools/ant/etch.xml"
        classpath="${Etch.HOME}/lib/${etch-ant-task.jar}" />

    <!-- set properties -->
    <property name="target"            location="${basedir}/target" />
    <property name="src"               location="${basedir}/src" />
    <property name="bin"               location="${target}/bin" />
    <property name="generated-sources" location="${target}/generated-sources/" />
    <property name="classes"           location="${target}/classes" />
    <property name="test-results"      location="${target}/test-results" />
    <property name="nunit-results"     location="${target}/nunit-results" />

    <!-- macros -->
    <macrodef name="etch-test">
        <attribute name="home" default="${Etch.HOME}" />
        <attribute name="binding" />
        <attribute name="file" />
        <attribute name="what" default="both, intf" />
        <attribute name="outputDir" default="${generated-sources}/main/etch/@{binding}" />
        <attribute name="templateOutputDir" default="${src}/main/@{binding}" />
        <element name="etchelements" implicit="true" optional="true" />
        <sequential>
            <etch home="@{home}" binding="@{binding}" outputDir="@{outputDir}" what="@{what}" file="@{file}" >
                <etchelements />
            </etch>
        </sequential>
    </macrodef>
    
    <macrodef name="generate-sources">
        <attribute name="binding" />
        <sequential>
            
            <!-- generate sources -->
            <mkdir dir="${generated-sources}/main/etch/@{binding}" />
            <etch-test binding="@{binding}" file="${src}/main/etch/Async.etch" />
            <etch-test binding="@{binding}" file="${src}/main/etch/Bar.etch" />
            <etch-test binding="@{binding}" file="${src}/main/etch/Baz.etch" />
            <etch-test binding="@{binding}" file="${src}/main/etch/BigIdl.etch" />
            <etch-test binding="@{binding}" file="${src}/main/etch/Closing.etch" />
            <etch-test binding="@{binding}" file="${src}/main/etch/Cuae.etch" />
            <etch-test binding="@{binding}" file="${src}/main/etch/Foo.etch" />
            <etch-test binding="@{binding}" file="${src}/main/etch/Inheritance.etch" />
            <etch-test binding="@{binding}" what="all,force" file="${src}/main/etch/Test1.etch" />
            <etch-test binding="@{binding}" file="${src}/main/etch/Test2.etch" />
            <etch-test binding="@{binding}" file="${src}/main/etch/Test3.etch" />
            <etch-test binding="@{binding}" file="${src}/main/etch/Test4.etch" />
            <etch-test binding="@{binding}" file="${src}/main/etch/Test6.etch" />
            <etch-test binding="@{binding}" what="all,force" file="${src}/main/etch/TestIncludes.etch" />
            <etch-test binding="@{binding}" file="${src}/main/etch/Types.etch" />
        
            <!-- negative tests -->
            <!-- <etch-test binding="@{binding}" file="${src}/main/etch/Test5.etch" /> -->
            <!-- <etch-test binding="@{binding}" file="${src}/main/etch/TestReservedWords.etch" /> -->

        </sequential>
    </macrodef>
    
    <!-- initialize -->
    <target name="init" >
        <mkdir dir="${target}" />
        <mkdir dir="${bin}" />
        <mkdir dir="${generated-sources}" />
        <mkdir dir="${classes}" />
        <mkdir dir="${test-results}" />
        <mkdir dir="${nunit-results}" />
    </target>
    
    <!-- build the server -->
    <target name="generate-java" >
        <generate-sources binding="java" />
    </target>

    <target name="generate-xml" >
        <generate-sources binding="xml" />
    </target>
    
    <target name="generate-csharp" >
        <generate-sources binding="csharp" />
    </target>
    
    <target name="init-sources-for-clover" if="Clover.enabled">
        <initialize-clover suffix="etchtests" >
            <fileset dir="${src}/main/java" >
                <include name="**/*.java" />
            </fileset>
            <fileset dir="${generated-sources}/main/etch/java" >
                <include name="**/*.java" />
                <exclude name="**/*BigIdl*.java" />
            </fileset>
            <testsources dir="${src}/test/java" >
                <include name="**/*.java" />
            </testsources>
        </initialize-clover>
    </target>
    
    <target name="build-java" depends="generate-java,init-sources-for-clover">
        <!-- compile sources -->
        <mkdir dir="${classes}" />
        <javac target="1.5" destdir="${classes}">
            <src path="${generated-sources}/main/etch/java" />
            <src path="${src}/main/java" />
            <src path="${src}/test/java" />
            <classpath>
                <pathelement location="${Etch.HOME}/lib/${etch-java-runtime.jar}" />
                <pathelement location="${Etch.dependency.junit.jar}" />
                <pathelement location="${Etch.HOME}/lib/${etch-compiler.jar}" />
            </classpath>
        </javac>
    </target>
    
    <target name="test-java" depends="build-java" >
        <!-- Run Unit Tests -->
        <junit printsummary="yes" haltonfailure="no" dir="${classes}"
            errorProperty="build.tests.fail" failureProperty="build.tests.fail">
            <classpath>
                <pathelement location="${classes}" />
                <pathelement location="${Etch.HOME}/lib/${etch-java-runtime.jar}" />
                <pathelement location="${Etch.dependency.junit.jar}" />
                <pathelement location="${Etch.HOME}/lib/${etch-compiler.jar}" />
                <!-- TODO: remove if clover not available -->
                <pathelement location="${Etch.dependency.clover.jar}"/>
            </classpath>
            <formatter type="xml"/>
            <batchtest fork="true" todir="${test-results}">
                <fileset dir="${src}/test/java">
                    <include name="**/Test*.java" />
                    <!-- TODO   debug Test*Xml failures -->
                    <exclude name="**/Test*Xml.java" />
                </fileset>
            </batchtest>
        </junit>
    </target>
    
    <target name="build-csharp" depends="generate-csharp,build-tests-csharp"
        if="BUILD.csharp" />
    
    <target name="build-tests-csharp"
        depends="build-tests-csharp-dotnet,build-tests-csharp-mono" />
    
    <target name="build-tests-csharp-dotnet" if="USE.dotnet" >
        <msbuild buildfile="${src}/test/csharp/TestsProj/TestsProj.csproj">
            <property name="Configuration" value="${Etch.build.target}" />
        </msbuild>
    </target>
    
    <target name="build-tests-csharp-mono" if="USE.mono" >
        <echo message="TODO: build w/ mono" />
    </target>

    <target name="test-csharp" depends="build-csharp" if="BUILD.csharp" >
        <mkdir dir="${nunit-results}" />

        <nunit xmlout="${nunit-results}/NUnitResults.xml">
            <testassembly name="${src}/test/csharp/TestsProj/bin/${Etch.build.target}/Tests.dll" />
        </nunit>

        <xslt style="${Etch.buildSupportDirectory}/NUnitToJUnit.xsl"
                in="${nunit-results}/NUnitResults.xml" out="${test-results}/TEST-NUnitResults.xml" />
    </target>
    
    <target name="build-xml" depends="generate-xml" >
    
    </target>
    
    <target name="test-xml" depends="build-xml" >
    
    </target>
    
    <!-- build sources -->
    <target name="build" depends="init,build-java,build-csharp,build-xml" />

    <!-- run unit tests -->
    <target name="test" depends="build,test-java,test-csharp,test-xml" />
        
    <!-- clean -->    
    <target name="clean">
        <delete dir="${target}" quiet="true" />
        <delete dir="${src}/test/csharp/TestsProj/obj" />
        <delete dir="${src}/test/csharp/TestsProj/bin" />
    </target>

</project>
