<?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="assertions" basedir="." default="tearDown">

  <import file="../buildfiletest-base.xml"/>

  <target name="setUp">
    <available property="jdk1.6+" classname="java.net.CookieStore"/>
    <condition property="source" value="6">
      <isset property="jdk1.6+"/>
    </condition>
    <property name="source" value="1.4"/>
    <mkdir dir="${output}"/>
    <javac srcdir="${src.dir}"
           includes="*.java"
           source="${source}"
           debug="true"
           destdir="${output}"
        />
  </target>

  <property name="src.dir" location="assertions"/>
  <property name="classname" value="AssertionMain"/>
  <property name="test.classname" value="AssertionTest"/>
  
  <path id="assert.classpath">
    <pathelement location="${output}"/>
  </path>                                   
  
  
  <!-- if per-class assertions work, this run asserts -->
  <target name="test-classname" depends="setUp">
    <java fork="true" failonerror="true"
      classname="${classname}"
      classpathref="assert.classpath">
      <assertions enablesystemassertions="true">
        <enable class="${classname}" />
      </assertions>
    </java>
  </target>
  
  <!-- if package works, this run asserts -->
  <target name="test-package" depends="setUp">
    <java fork="true"  failonerror="true"
      classname="${classname}"
      classpathref="assert.classpath">
      <assertions enableSystemAssertions="false" >
        <enable package="..." />
      </assertions>
    </java>
  </target>

  <!-- this test should run the app successfully -->
  <target name="test-empty-assertions" depends="setUp">
    <java fork="true"  failonerror="true"
      classname="${classname}"
      classpathref="assert.classpath">
      <assertions/>
    </java>
  </target>  

  <!-- this test should run the app successfully -->
  <target name="test-disable" depends="setUp">
    <java fork="true" failonerror="true"
      classname="${classname}"
      classpathref="assert.classpath">
      <assertions enableSystemAssertions="false" >
        <enable package="..." />
        <disable class="${classname}" />
      </assertions>
    </java>
  </target>  

  <!-- repeated settigns result in the last declaration winning
    except that the rule 'classes win over packages takes priority
    this run will assert -->
  <target name="test-override" depends="setUp">
    <java fork="true" failonerror="true"
      classname="${classname}"
      classpathref="assert.classpath">
      <assertions enableSystemAssertions="false" >
        <enable package="..." />
        <disable class="${classname}" />
        <enable class="${classname}" />
        <disable package="..." />
      </assertions>
    </java>
  </target>  

  <!-- repeated settigns result in the last declaration winning;
    this run will not assert -->
  <target name="test-override2" depends="setUp">
    <java fork="true" failonerror="true"
      classname="${classname}"
      classpathref="assert.classpath">
      <assertions enableSystemAssertions="false" >
        <enable package="..." />
        <enable class="${classname}" />
        <disable class="${classname}" />
      </assertions>
    </java>
  </target>  
  
  <!-- if references work, this run asserts -->
  <target name="test-references">
  <assertions id="project.assertions" >
    <enable package="org.apache.test" />
    <disable package="org.apache.log4j"/>
    <enable package="..."/>
  </assertions>  
    <java fork="true" failonerror="true"
      classname="${classname}"
      classpathref="assert.classpath">
      <assertions refid="project.assertions"/>
    </java>
  </target>
  
  <!-- when fork=false; we need to reject the construct -->
  <target name="test-nofork" depends="setUp">
    <java fork="false" failonerror="true"
      classname="${classname}"
      classpathref="assert.classpath">
      <assertions enablesystemassertions="true">
        <enable class="${classname}" />
      </assertions>
    </java>
  </target>  

  <!-- this throws a build error -->
  <target name="test-multiple-assertions" depends="setUp">
    <java fork="true" failonerror="true"
      classname="${classname}"
      classpathref="assert.classpath">
      <assertions enablesystemassertions="true">
        <enable class="${classname}" />
      </assertions>
      <assertions/>
    </java>
  </target>  
  
  <!-- should throw a build exception -->
  <target name="test-reference-abuse" depends="setUp">
  <assertions id="project.assertions2" >
    <enable package="org.apache.test" />
    <disable package="org.apache.log4j"/>
    <enable package="..."/>
  </assertions>  
    <java fork="true" failonerror="true"
      classname="${classname}"
      classpathref="assert.classpath">
      <assertions refid="project.assertions2">
        <disable class="${classname}" />
      </assertions>      
    </java>
  </target>    
  
    
  <target name="test-junit" depends="setUp">
    <junit fork="true" 
      haltonerror="true" haltonfailure="true"
      >
      <classpath>
        <path refid="assert.classpath"/>
      </classpath>
      <formatter type="plain"     usefile="false"/>
      <assertions >
        <enable class="${test.classname}" />
      </assertions> 
      <test name="${test.classname}"/>
    </junit>
  </target>
    
  <!-- This is here to show that setting it as a property works
  so there is some defect in pass-on of assertions that
  is causing the problem -->
  <target name="test-junit-manual-setup" depends="setUp">
    <junit fork="true" 
      haltonerror="true" haltonfailure="true"
      >
      <classpath>
        <path refid="assert.classpath"/>
      </classpath>
      <formatter type="plain"     usefile="false"/>
      <test name="${test.classname}"/>
      <jvmarg value="-ea:AssertionTest"/>
    </junit>
  </target>  
  
</project>
