<?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 default="antunit" xmlns:au="antlib:org.apache.ant.antunit">
  <import file="../../../antunit-base.xml" />

  <path id="junit">
    <fileset dir="../../../../../../lib/optional" includes="junit*"/>
  </path>

  <macrodef name="empty-test">
    <attribute name="classname"/>
    <sequential>
      <echo file="${input}/@{classname}.java"><![CDATA[
package test;
import junit.framework.TestCase;

public class @{classname} extends TestCase {
    public void testEmpty() {}
}
]]>  </echo>
    </sequential>
  </macrodef>

  <target name="setUp">
    <mkdir dir="${input}"/>
    <mkdir dir="${output}"/>
  </target>

  <target name="testTimeoutLogOfBatchTests">
    <mkdir dir="${input}"/>
    <mkdir dir="${output}"/>
    <empty-test classname="ATest"/>
      <echo file="${input}/BTest.java"><![CDATA[
package test;
import junit.framework.TestCase;

public class BTest extends TestCase {
    public void testEmpty() throws Exception {
        Thread.sleep(20 * 1000);
    }
}
]]>  </echo>
    <empty-test classname="CTest"/>
    <empty-test classname="DTest"/>
    <javac srcdir="${input}" destdir="${output}">
      <classpath refid="junit"/>
    </javac>
    <junit fork="true" forkMode="perBatch" timeout="5000" printsummary="yes">
      <classpath refid="junit"/>
      <classpath location="${output}"/>
      <batchtest>
        <fileset dir="${output}">
          <include name="**/*Test.class" />
        </fileset>
      </batchtest>
    </junit>
    <au:assertLogContains text="ATest"/>
    <au:assertLogContains text="BTest"/>
    <au:assertLogDoesntContain text="CTest"/>
    <au:assertLogDoesntContain text="DTest"/>
  </target>

  <target name="testFailurePropertyOnTestCase">
    <mkdir dir="${input}"/>
    <mkdir dir="${output}"/>
    <echo file="${input}/ATest.java"><![CDATA[
package test;
import junit.framework.TestCase;

public class ATest extends TestCase {
    public void testFail() {
        assertTrue(false);
    }
}
]]>  </echo>
    <javac srcdir="${input}" destdir="${output}">
      <classpath refid="junit"/>
    </javac>
    <junit failureProperty="testcase.failed" haltonfailure="false">
      <classpath refid="junit"/>
      <classpath location="${output}"/>
      <batchtest>
        <fileset dir="${output}">
          <include name="**/*Test.class" />
        </fileset>
      </batchtest>
    </junit>
    <au:assertPropertySet name="testcase.failed"/>
  </target>

  <target name="testFailurePropertyOnTestSuite">
    <mkdir dir="${input}"/>
    <mkdir dir="${output}"/>
    <echo file="${input}/ATest.java"><![CDATA[
package test;
import junit.framework.Assert;
import junit.framework.TestSuite;

public class ATest extends TestSuite {
    public ATest() {
        super(test.ATest.class);
    }
    public void testFail() {
        Assert.assertTrue(false);
    }
}
]]>  </echo>
    <javac srcdir="${input}" destdir="${output}">
      <classpath refid="junit"/>
    </javac>
    <junit failureProperty="testsuite.failed" haltonfailure="false">
      <classpath refid="junit"/>
      <classpath location="${output}"/>
      <batchtest>
        <fileset dir="${output}">
          <include name="**/*Test.class" />
        </fileset>
      </batchtest>
    </junit>
    <au:assertPropertySet name="testsuite.failed"/>
  </target>

  <target name="testTimeoutAndFormattersForkPerTest">
    <antcall target="runTimeoutAndFormattersTest">
      <param name="forkMode" value="perTest"/>
    </antcall>
    <au:assertFileExists file="${output}/TEST-test.CTest.txt"/>
    <au:assertFileExists file="${output}/TEST-test.CTest.xml"/>
    <au:assertFileExists file="${output}/TEST-test.DTest.txt"/>
    <au:assertFileExists file="${output}/TEST-test.DTest.xml"/>
  </target>

  <target name="testTimeoutAndFormattersForkOnce">
    <antcall target="runTimeoutAndFormattersTest">
      <param name="forkMode" value="once"/>
    </antcall>
    <au:assertFileDoesntExist file="${output}/TEST-test.CTest.txt"/>
    <au:assertFileDoesntExist file="${output}/TEST-test.CTest.xml"/>
    <au:assertFileDoesntExist file="${output}/TEST-test.DTest.txt"/>
    <au:assertFileDoesntExist file="${output}/TEST-test.DTest.xml"/>
  </target>

  <target name="runTimeoutAndFormattersTest"
          description="https://issues.apache.org/bugzilla/show_bug.cgi?id=35634">
    <mkdir dir="${input}"/>
    <mkdir dir="${output}"/>
    <empty-test classname="ATest"/>
    <echo file="${input}/BTest.java"><![CDATA[
package test;
import junit.framework.TestCase;

public class BTest extends TestCase {
    public void testEmpty() throws Exception {
        Thread.sleep(20 * 1000);
    }
}
]]></echo>
    <empty-test classname="CTest"/>
    <empty-test classname="DTest"/>
    <javac srcdir="${input}" destdir="${output}">
      <classpath refid="junit"/>
    </javac>
    <junit fork="true" timeout="5000" forkmode="${forkMode}">
      <classpath refid="junit"/>
      <classpath location="${output}"/>
      <batchtest todir="${output}">
        <fileset dir="${output}">
          <include name="**/*Test.class" />
        </fileset>
      </batchtest>
      <formatter type="brief"/>
      <formatter type="xml"/>
    </junit>
    <au:assertFileExists file="${output}/TEST-test.ATest.txt"/>
    <au:assertFileExists file="${output}/TEST-test.ATest.xml"/>
    <au:assertFileExists file="${output}/TEST-test.BTest.txt"/>
    <au:assertFileExists file="${output}/TEST-test.BTest.xml"/>
  </target>

  <target name="-ifUnlessSetup" depends="setUp">
    <empty-test classname="ATest"/>
    <empty-test classname="BTest"/>
    <empty-test classname="CTest"/>
    <empty-test classname="DTest"/>
    <empty-test classname="ETest"/>
    <empty-test classname="FTest"/>
    <empty-test classname="GTest"/>
    <empty-test classname="HTest"/>
    <javac srcdir="${input}" destdir="${output}">
      <classpath refid="junit"/>
    </javac>
    <macrodef name="j">
      <sequential>
        <junit fork="true" forkMode="perBatch" printsummary="yes">
          <classpath refid="junit"/>
          <classpath location="${output}"/>
          <test name="test.ATest" if="${if}"/>
          <test name="test.BTest" if="if"/>
          <test name="test.CTest" unless="${if}"/>
          <test name="test.DTest" unless="if"/>
          <batchtest if="${if}">
            <fileset dir="${output}">
              <include name="**/ETest.class" />
            </fileset>
          </batchtest>
          <batchtest if="if">
            <fileset dir="${output}">
              <include name="**/FTest.class" />
            </fileset>
          </batchtest>
          <batchtest unless="${if}">
            <fileset dir="${output}">
              <include name="**/GTest.class" />
            </fileset>
          </batchtest>
          <batchtest unless="if">
            <fileset dir="${output}">
              <include name="**/HTest.class" />
            </fileset>
          </batchtest>
        </junit>
      </sequential>
    </macrodef>
  </target>

  <target name="testPropertiesNotSet" depends="-ifUnlessSetup">
    <j/>
    <au:assertLogDoesntContain text="Running test.ATest"/>
    <au:assertLogDoesntContain text="Running test.BTest"/>
    <au:assertLogContains text="Running test.CTest"/>
    <au:assertLogContains text="Running test.DTest"/>
    <au:assertLogDoesntContain text="Running test.ETest"/>
    <au:assertLogDoesntContain text="Running test.FTest"/>
    <au:assertLogContains text="Running test.GTest"/>
    <au:assertLogContains text="Running test.HTest"/>
  </target>

  <target name="testPropertiesSet" depends="-ifUnlessSetup">
    <property name="if" value="whatever"/>
    <j/>
    <au:assertLogDoesntContain text="Running test.ATest"/>
    <au:assertLogContains text="Running test.BTest"/>
    <au:assertLogContains text="Running test.CTest"/>
    <au:assertLogDoesntContain text="Running test.DTest"/>
    <au:assertLogDoesntContain text="Running test.ETest"/>
    <au:assertLogContains text="Running test.FTest"/>
    <au:assertLogContains text="Running test.GTest"/>
    <au:assertLogDoesntContain text="Running test.HTest"/>
  </target>

  <target name="testPropertiesTrue" depends="-ifUnlessSetup">
    <property name="if" value="true"/>
    <j/>
    <au:assertLogContains text="Running test.ATest"/>
    <au:assertLogContains text="Running test.BTest"/>
    <au:assertLogDoesntContain text="Running test.CTest"/>
    <au:assertLogDoesntContain text="Running test.DTest"/>
    <au:assertLogContains text="Running test.ETest"/>
    <au:assertLogContains text="Running test.FTest"/>
    <au:assertLogDoesntContain text="Running test.GTest"/>
    <au:assertLogDoesntContain text="Running test.HTest"/>
  </target>

  <target name="testPropertiesFalse" depends="-ifUnlessSetup">
    <property name="if" value="false"/>
    <j/>
    <au:assertLogDoesntContain text="Running test.ATest"/>
    <au:assertLogContains text="Running test.BTest"/>
    <au:assertLogContains text="Running test.CTest"/>
    <au:assertLogDoesntContain text="Running test.DTest"/>
    <au:assertLogDoesntContain text="Running test.ETest"/>
    <au:assertLogContains text="Running test.FTest"/>
    <au:assertLogContains text="Running test.GTest"/>
    <au:assertLogDoesntContain text="Running test.HTest"/>
  </target>

</project>
