<?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="exec-test" default="spawn" basedir=".">
    <target name="init">
        <!-- this property can be overriden programatically in the Java test case -->
        <property name="timeToWait" value="10"/>
        <!-- this property can be overriden programatically in the Java test case -->
        <property name="logFile" value="${java.io.tmpdir}/spawn.log"/>
        <property environment="env"/>
        <!-- UNIX -->
        <available file="sh" filepath="${env.PATH}" property="sh.executable"/>
        <!-- CYGWIN -->
        <available file="sh.exe" filepath="${env.PATH}" property="sh.exe.executable"/>
        <condition property="test.can.run">
            <or>
                <isset property="sh.executable"/>
                <isset property="sh.exe.executable"/>
            </or>
        </condition>
        <!-- UNIX -->
        <available file="wc" filepath="${env.PATH}" property="wc.executable"/>
        <!-- CYGWIN -->
        <available file="wc.exe" filepath="${env.PATH}" property="wc.exe.executable"/>
        <condition property="wc.can.run">
            <or>
                <isset property="wc.executable"/>
                <isset property="wc.exe.executable"/>
            </or>
        </condition>
        <!-- UNIX -->
        <available file="cat" filepath="${env.PATH}" property="cat.executable"/>
        <!-- CYGWIN -->
        <available file="cat.exe" filepath="${env.PATH}" property="cat.exe.executable"/>
        <condition property="cat.can.run">
            <or>
                <isset property="cat.executable"/>
                <isset property="cat.exe.executable"/>
            </or>
        </condition>
    </target>

    <target name="spawn" depends="init" if="test.can.run">
        <exec executable="sh" spawn="true">
            <arg value="spawn.sh"/>
            <arg value="${timeToWait}" />
            <arg value="${logFile}" />
        </exec>
    </target>

    <target name="no-redirect" depends="init" if="test.can.run">
        <exec executable="sh">
            <arg value="parrot.sh"/>
            <arg value="${ant.file}" />
        </exec>
    </target>

    <target name="redirect1" depends="init" if="test.can.run">
        <exec executable="sh" output="redirect.out">
            <arg value="parrot.sh"/>
            <arg value="${ant.file}" />
        </exec>
    </target>

    <target name="redirect2" depends="init" if="test.can.run">
        <exec executable="sh" output="redirect.out" error="redirect.err">
            <arg value="parrot.sh"/>
            <arg value="${ant.file}" />
        </exec>
    </target>

    <target name="redirect3" depends="init" if="test.can.run">
        <exec executable="sh" logerror="true"
              output="redirect.out" outputproperty="redirect.out">
            <arg value="parrot.sh"/>
            <arg value="${ant.file}" />
        </exec>
    </target>

    <target name="redirect4" depends="init" if="test.can.run">
        <exec executable="sh"
              error="redirect.err" errorproperty="redirect.err"
              output="redirect.out" outputproperty="redirect.out">
            <arg value="parrot.sh"/>
            <arg value="${ant.file}" />
        </exec>
    </target>

    <target name="redirect5" depends="init" if="wc.can.run">
        <exec executable="wc" inputstring="x y z"
              error="redirect.err" errorproperty="redirect.err"
              output="redirect.out" outputproperty="redirect.out">
            <arg value="-w"/>
        </exec>
    </target>

    <target name="redirect6" depends="init" if="wc.can.run">
        <echo file="redirect.in">x y z</echo>
        <exec executable="wc" input="redirect.in"
              error="redirect.err" errorproperty="redirect.err"
              output="redirect.out" outputproperty="redirect.out">
            <arg value="-w"/>
        </exec>
    </target>

    <target name="redirect7" depends="init" if="wc.can.run">
        <exec executable="wc" inputstring="x y z"
              error="redirect.err"
              output="redirect.out" outputproperty="redirect.out">
            <arg value="-w"/>
        </exec>
    </target>

    <target name="redirector1" description="fail"
            depends="init" if="test.can.run">
        <exec executable="sh">
            <arg value="parrot.sh"/>
            <arg value="${ant.file}" />
            <redirector output="redirector.out" />
            <redirector output="whocares" />
        </exec>
    </target>

    <target name="redirector2" depends="init" if="test.can.run">
        <exec executable="sh">
            <arg value="parrot.sh"/>
            <arg value="${ant.file}" />
            <redirector output="redirector.out" />
        </exec>
    </target>

    <target name="redirector3" depends="init" if="test.can.run">
        <exec executable="sh">
            <arg value="parrot.sh"/>
            <arg value="${ant.file}" />
            <redirector output="redirector.out" error="redirector.err" />
        </exec>
    </target>

    <target name="redirector4" depends="init" if="test.can.run">
        <exec executable="sh">
            <arg value="parrot.sh"/>
            <arg value="${ant.file}" />
            <redirector output="redirector.out" logerror="true"
                        outputproperty="redirector.out" />
        </exec>
    </target>

    <target name="redirector5" depends="init" if="test.can.run">
        <exec executable="sh">
            <redirector error="redirector.err" errorproperty="redirector.err"
              output="redirector.out" outputproperty="redirector.out" />
            <arg value="parrot.sh"/>
            <arg value="${ant.file}" />
        </exec>
    </target>

    <target name="redirector6" depends="init" if="test.can.run">
        <exec executable="sh">
            <redirector outputproperty="redirector.out"
                        errorproperty="redirector.err">
                <outputmapper type="merge" to="redirector.out" />
                <errormapper type="merge" to="redirector.err" />
            </redirector>
            <arg value="parrot.sh" />
            <arg value="${ant.file}" />
        </exec>
    </target>

    <target name="redirector7" depends="init" if="test.can.run">
        <exec executable="sh">
            <redirector outputproperty="redirector.out"
                        errorproperty="redirector.err">
                <outputmapper type="merge" to="redirector.out" />
                <errormapper type="merge" to="redirector.err" />
                <errorfilterchain>
                    <replacestring from="err" to="ERROR!!!" />
                </errorfilterchain>
            </redirector>
            <arg value="parrot.sh" />
            <arg value="${ant.file}" />
        </exec>
    </target>

    <target name="redirector8" depends="init" if="wc.can.run">
        <echo file="redirector.in">x y z</echo>
        <exec executable="wc">
            <redirector outputproperty="redirector.out"
                        errorproperty="redirector.err">
                <inputmapper type="merge" to="redirector.in" />
                <outputmapper type="merge" to="redirector.out" />
                <errormapper type="merge" to="redirector.err" />
            </redirector>
            <arg value="-w"/>
        </exec>
    </target>

    <target name="redirector9" depends="init" if="cat.can.run">
        <echo file="redirector.in">blah before blah</echo>
        <exec executable="cat">
            <redirector outputproperty="redirector.out"
                        errorproperty="redirector.err">
                <inputfilterchain>
                    <replacestring from="before" to="after" />
                </inputfilterchain>
                <inputmapper type="merge" to="redirector.in" />
                <outputmapper type="merge" to="redirector.out" />
                <errormapper type="merge" to="redirector.err" />
            </redirector>
        </exec>
    </target>

    <target name="redirector10" depends="init" if="cat.can.run">
        <echo file="redirector.in">blah before blah</echo>
        <exec executable="cat">
            <redirector outputproperty="redirector.out"
                        errorproperty="redirector.err">
                <outputfilterchain>
                    <replacestring from="before" to="after" />
                </outputfilterchain>
                <outputmapper type="merge" to="redirector.out" />
                <errormapper type="merge" to="redirector.err" />
            </redirector>
            <arg value="redirector.in"/>
        </exec>
    </target>

    <target name="redirector11" depends="init" if="cat.can.run">
        <exec executable="cat">
            <redirector outputproperty="redirector.out"
                        errorproperty="redirector.err"
                        inputstring="blah before blah">
                <inputfilterchain>
                    <replacestring from="before" to="after" />
                </inputfilterchain>
                <outputmapper type="merge" to="redirector.out" />
                <errormapper type="merge" to="redirector.err" />
            </redirector>
        </exec>
    </target>

    <target name="redirector12" depends="init" if="cat.can.run">
        <echo file="redirector.in">blah before blah</echo>
        <exec executable="cat" output="redirector.out" error="redirector.err">
            <redirector outputproperty="redirector.out"
                        errorproperty="redirector.err">
                <outputfilterchain>
                    <replacestring from="before" to="after" />
                </outputfilterchain>
                <outputmapper type="glob" from="nomatch" to="nomatchout" />
                <errormapper type="glob" from="nomatch" to="nomatcherr" />
            </redirector>
            <arg value="redirector.in"/>
        </exec>
    </target>

    <target name="redirector13" depends="init" if="test.can.run">
        <exec executable="sh">
            <redirector>
                <outputfilterchain>
                    <replacestring from="out" to="OUTPUT???" />
                </outputfilterchain>
                <errorfilterchain>
                    <replacestring from="err" to="ERROR!!!" />
                </errorfilterchain>
            </redirector>
            <arg value="parrot.sh" />
            <arg value="${ant.file}" />
        </exec>
    </target>

    <target name="redirector14" depends="init" if="cat.can.run">
        <exec executable="cat">
            <redirector inputstring="blah before blah">
                <outputfilterchain>
                    <replacestring from="before" to="after" />
                </outputfilterchain>
                <outputmapper type="glob" from="nomatch" to="nomatchout" />
                <errormapper type="glob" from="nomatch" to="nomatcherr" />
            </redirector>
        </exec>
    </target>

    <target name="redirector15" depends="init" if="cat.can.run">
        <exec executable="cat">
            <redirector input="input/iso8859-1" output="redirector.out"
                        inputencoding="ISO8859_1" outputencoding="UTF8" />
        </exec>
    </target>

    <target name="redirector16" depends="init" if="test.can.run">
        <exec executable="sh">
            <redirector inputstring="exit"
                        output="redirector16.out" error="redirector16.err" />
        </exec>
        <condition property="16pass">
            <and>
                <available file="redirector16.out" type="file" />
                <available file="redirector16.err" type="file" />
            </and>
        </condition>
        <fail unless="16pass">Files were not created.</fail>
    </target>

    <target name="redirector17" depends="init" if="test.can.run">
        <exec executable="sh">
            <redirector inputstring="exit" createemptyfiles="false"
                        output="redirector17.out" error="redirector17.err" />
        </exec>
        <condition property="17fail">
            <or>
                <available file="redirector17.out" type="file" />
                <available file="redirector17.err" type="file" />
            </or>
        </condition>
        <fail if="17fail">Files were created.</fail>
    </target>

    <target name="redirector18" depends="init" if="test.can.run">
        <exec executable="sh">
            <arg value="parrot.sh"/>
            <arg value="${ant.file}" />
            <redirector alwayslog="true" logerror="true"
                        outputproperty="redirector.out" />
        </exec>
        <fail message="property redirector.out has unexpected content">
            <condition>
                <not>
                    <equals arg1="${ant.file} out" arg2="${redirector.out}" />
                </not>
            </condition>
        </fail>
    </target>

    <!-- test will succeed as the OS wont match-->
    <target name="testExecUnknownOS">
      <exec executable="nonexistent-program-we-expect"
        failonerror="true"
        os="ZX81">
      </exec>
    </target>

    <target name="testExecOSFamily">
      <exec executable="uptime"
        failonerror="true"
        osFamily="unix">
      </exec>
      <exec executable="cmd.exe"
        failonerror="true"
        osFamily="winnt">
        <arg value="/c" />
        <arg value="time /t" />
      </exec>
    </target>    
    
    <target name="testExecInconsistentSettings">
      <exec executable="nonexistent-program-we-expect"
        failonerror="true"
        osFamily="WIN9X"
        os="linux unix">
      </exec>
    </target>
    
    <target name="cleanup">
        <delete>
            <fileset file="${logFile}" />
            <fileset dir="${basedir}" includes="redirect*" />
            <fileset dir="${basedir}" includes="redirector*" />
        </delete>
    </target>
</project>
