<?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

https://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="setupandteardown-test"
      default="all"
         xmlns:au="antlib:org.apache.ant.antunit">
         
<typedef name="reportlistener"
    classname="org.apache.ant.antunit.SetUpAndTearDownTest$TestReportListener"/>
    
  <target name="all">
    <au:antunit>
      <file file="${ant.file}" />
      <au:plainlistener/>
    </au:antunit>
  </target>
  
  <!-- Verifies that both setup and teardown are called for each test -->
  <target name="testBoth">
    <tempfile property="outputfile" deleteonexit="true" />
    <au:antunit>
      <file file="antunit/setupandteardown/both.xml" />
      <propertyset>
        <propertyref name="outputfile" />
      </propertyset>
    </au:antunit>
    <au:assertFileExists file="${outputfile}" message="Output file doesn't exist" />
    <loadfile property="output" srcFile="${outputfile}" />
    <au:assertEquals expected="SETSETSETSET" actual="${output}" />
  </target>
  
  <!-- Tests no setup -->
  <target name="testNoSetup">
    <tempfile property="outputfile" deleteonexit="true" />
    <au:antunit>
      <file file="antunit/setupandteardown/nosetup.xml" />
      <propertyset>
        <propertyref name="outputfile" />
      </propertyset>
    </au:antunit>
    <au:assertFileExists file="${outputfile}" message="Output file doesn't exist" />
    <loadfile property="output" srcFile="${outputfile}" />
    <au:assertEquals expected="ETETETET" actual="${output}" />
  </target>
  
  <!-- Tests no teardown -->
  <target name="testNoTeardown">
    <tempfile property="outputfile" deleteonexit="true" />
    <au:antunit>
      <file file="antunit/setupandteardown/noteardown.xml" />
      <propertyset>
        <propertyref name="outputfile" />
      </propertyset>
    </au:antunit>
    <au:assertFileExists file="${outputfile}" message="Output file doesn't exist" />
    <loadfile property="output" srcFile="${outputfile}" />
    <au:assertEquals expected="SESESESE" actual="${output}" />
  </target>
  
  <!-- Verifies teardown is called for a failed test -->
  <target name="testFailedTest">
    <tempfile property="outputfile" deleteonexit="true" />
    <au:antunit failOnError="false" errorProperty="failure">
      <file file="antunit/setupandteardown/failedtest.xml" />
      <propertyset>
        <propertyref name="outputfile" />
      </propertyset>
    </au:antunit>
    <fail unless="failure" message="Test should have failed." />
    <au:assertFileExists file="${outputfile}" message="Output file doesn't exist" />
    <loadfile property="output" srcFile="${outputfile}" />
    <au:assertEquals expected="SET" actual="${output}" />
  </target>
  
  <!-- Tests a failure in setup -->
  <target name="testFailureSetup">
    <tempfile property="outputfile" deleteonexit="true" />
    <au:antunit failOnError="false" errorProperty="failure">
      <file file="antunit/setupandteardown/failuresetup.xml" />
      <au:plainlistener/>
      <propertyset>
        <propertyref name="outputfile" />
      </propertyset>
    </au:antunit>
    <fail unless="failure" message="Test should have failed." />
    <au:assertFileExists file="${outputfile}" message="Output file doesn't exist" />
    <loadfile property="output" srcFile="${outputfile}" />
    <au:assertEquals expected="STSTST" actual="${output}" />
  </target>
  
  <!-- Tests an error in setup -->
  <target name="testErrorSetup">
    <tempfile property="outputfile" deleteonexit="true" />
    <au:antunit failOnError="false" errorProperty="failure">
      <file file="antunit/setupandteardown/errorsetup.xml" />
      <au:plainlistener/>
      <propertyset>
        <propertyref name="outputfile" />
      </propertyset>
    </au:antunit>
    <fail unless="failure" message="Test should have failed." />
    <au:assertFileExists file="${outputfile}" message="Output file doesn't exist" />
    <loadfile property="output" srcFile="${outputfile}" />
    <au:assertEquals expected="STSTST" actual="${output}" />
  </target>
  
  <!-- Tests a failure in teardown -->
  <target name="testFailureTeardown">
    <tempfile property="outputfile" deleteonexit="true" />
    <au:antunit failOnError="false" errorProperty="failure">
      <file file="antunit/setupandteardown/failureteardown.xml" />
      <au:plainlistener/>
      <propertyset>
        <propertyref name="outputfile" />
      </propertyset>
    </au:antunit>
    <fail unless="failure" message="Test should have failed." />
    <au:assertFileExists file="${outputfile}" message="Output file doesn't exist" />
    <loadfile property="output" srcFile="${outputfile}" />
    <au:assertEquals expected="SETSETSET" actual="${output}" />
  </target>
  
  <!-- Tests a error in teardown -->
  <target name="testErrorTeardown">
    <tempfile property="outputfile" deleteonexit="true" />
    <au:antunit failOnError="false" errorProperty="failure">
      <file file="antunit/setupandteardown/errorteardown.xml" />
      <au:plainlistener/>
      <propertyset>
        <propertyref name="outputfile" />
      </propertyset>
    </au:antunit>
    <fail unless="failure" message="Test should have failed." />
    <au:assertFileExists file="${outputfile}" message="Output file doesn't exist" />
    <loadfile property="output" srcFile="${outputfile}" />
    <au:assertEquals expected="SETSETSET" actual="${output}" />
  </target>

  <!-- Verifies that both suiteSetUp and suiteTearDown are called for the whole test script -->
  <target name="testBothSuite">
    <tempfile property="outputfile" deleteonexit="true" />
    <au:antunit>
      <file file="antunit/setupandteardown/bothsuite.xml" />
      <propertyset>
        <propertyref name="outputfile" />
      </propertyset>
    </au:antunit>
    <au:assertFileExists file="${outputfile}" message="Output file doesn't exist" />
    <loadfile property="output" srcFile="${outputfile}" />
    <au:assertEquals expected="ASETSETSETSETB" actual="${output}" />
  </target>
  
  <!-- Tests no suiteSetUp -->
  <target name="testNoSuiteSetUp">
    <tempfile property="outputfile" deleteonexit="true" />
    <au:antunit>
      <file file="antunit/setupandteardown/nosuitesetup.xml" />
      <propertyset>
        <propertyref name="outputfile" />
      </propertyset>
    </au:antunit>
    <au:assertFileExists file="${outputfile}" message="Output file doesn't exist" />
    <loadfile property="output" srcFile="${outputfile}" />
    <au:assertEquals expected="EEEEB" actual="${output}" />
  </target>
  
  <!-- Tests no suiteTearDown -->
  <target name="testNoSuiteTearDown">
    <tempfile property="outputfile" deleteonexit="true" />
    <au:antunit>
      <file file="antunit/setupandteardown/nosuiteteardown.xml" />
      <propertyset>
        <propertyref name="outputfile" />
      </propertyset>
    </au:antunit>
    <au:assertFileExists file="${outputfile}" message="Output file doesn't exist" />
    <loadfile property="output" srcFile="${outputfile}" />
    <au:assertEquals expected="AEEEE" actual="${output}" />
  </target>
  
  <!-- Verifies suite teardown is called for a failed test -->
  <target name="testFailedTestSuite">
    <tempfile property="outputfile" deleteonexit="true" />
    <au:antunit failOnError="false" errorProperty="failure">
      <file file="antunit/setupandteardown/failedtestsuite.xml" />
      <propertyset>
        <propertyref name="outputfile" />
      </propertyset>
    </au:antunit>
    <fail unless="failure" message="Test should have failed." />
    <au:assertFileExists file="${outputfile}" message="Output file doesn't exist" />
    <loadfile property="output" srcFile="${outputfile}" />
    <au:assertEquals expected="ASETB" actual="${output}" />
  </target>

  <!-- Tests a failure in suiteSetUp -->
  <target name="testFailureSuiteSetUp">
    <tempfile property="outputfile" deleteonexit="true" />
    <au:antunit failOnError="false" errorProperty="failure">
      <file file="antunit/setupandteardown/failuresuitesetup.xml" />
      <au:plainlistener/>
      <reportlistener/>
      <propertyset>
        <propertyref name="outputfile" />
      </propertyset>
    </au:antunit>
    <fail unless="failure" message="Test should have failed." />
    <au:assertFileExists file="${outputfile}" message="Output file doesn't exist" />
    <loadfile property="output" srcFile="${outputfile}" />
    <au:assertEquals expected="AB" actual="${output}" />
  </target>

  <!-- Tests an error in suiteSetUp -->
  <target name="testErrorSuiteSetUp">
    <tempfile property="outputfile" deleteonexit="true" />
    <au:antunit failOnError="false" errorProperty="failure">
      <file file="antunit/setupandteardown/errorsuitesetup.xml" />
      <au:plainlistener/>
      <reportlistener/>
      <propertyset>
        <propertyref name="outputfile" />
      </propertyset>
    </au:antunit>
    <fail unless="failure" message="Test should have failed." />
    <au:assertFileExists file="${outputfile}" message="Output file doesn't exist" />
    <loadfile property="output" srcFile="${outputfile}" />
    <au:assertEquals expected="AB" actual="${output}" />
  </target>
  
  <!-- Tests a failure in suiteTearDown -->
  <target name="testFailureSuiteTearDown">
    <tempfile property="outputfile" deleteonexit="true" />
    <au:antunit failOnError="false" errorProperty="failure">
      <file file="antunit/setupandteardown/failuresuiteteardown.xml" />
      <au:plainlistener/>
      <reportlistener/>
      <propertyset>
        <propertyref name="outputfile" />
      </propertyset>
    </au:antunit>
    <fail unless="failure" message="Test should have failed." />
    <au:assertFileExists file="${outputfile}" message="Output file doesn't exist" />
    <loadfile property="output" srcFile="${outputfile}" />
    <au:assertEquals expected="ASETSETSETSETB" actual="${output}" />
  </target>
  
  <!-- Tests an error in suiteTearDown -->
  <target name="testErrorSuiteTearDown">
    <tempfile property="outputfile" deleteonexit="true" />
    <au:antunit failOnError="false" errorProperty="failure">
      <file file="antunit/setupandteardown/errorsuiteteardown.xml" />
      <au:plainlistener/>
      <reportlistener/>
      <propertyset>
        <propertyref name="outputfile" />
      </propertyset>
    </au:antunit>
    <fail unless="failure" message="Test should have failed." />
    <au:assertFileExists file="${outputfile}" message="Output file doesn't exist" />
    <loadfile property="output" srcFile="${outputfile}" />
    <au:assertEquals expected="ASETSETSETSETB" actual="${output}" />
  </target>

</project>
