<?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='report-normal-operation'
    default="allTests"
    xmlns:au="antlib:org.apache.ant.antunit"
    xmlns:rat="antlib:org.apache.rat.anttasks">

  <property name="output.dir" location="../../../../target/testoutput"/>
  <mkdir dir="${output.dir}/dest"/>
  <path id="all-classes-needed-for-tests">
    <pathelement location="${jar.name}" />
    <pathelement path="${test.classpath}" />
    <pathelement location="${output.dir}/dest" />
  </path>

  <taskdef uri="antlib:org.apache.ant.antunit" 
           resource="org/apache/ant/antunit/antlib.xml" 
           classpathref="all-classes-needed-for-tests"
           loaderref="testloader"/>

  <taskdef uri="antlib:org.apache.rat.anttasks"
           resource="org/apache/rat/anttasks/antlib.xml"
           classpathref="all-classes-needed-for-tests"
           loaderref="testloader"/>

  <target name="setUp">
    <pathconvert dirsep="/" property="file.name">
      <path>
        <pathelement location="${ant.file}"/>
      </path>
    </pathconvert>
    <property name="expectedOutput" value=" AL    ${file.name}"/>
    <property name="expectedOutputXML" value="&lt;resource name='${file.name}'"/>
    <property name="expectedOutputXML2" value="&lt;header-type name='AL   '"/>
  </target>

  <target name="allTests">
    <au:antunit xmlns:au="antlib:org.apache.ant.antunit"
                failonerror="true" >
      <file file="${ant.file}"/>
      <au:plainlistener/>
      <propertyset>
        <propertyref name="test.classpath"/>
        <propertyref name="jar.name"/>
      </propertyset>
    </au:antunit>
  </target>

  <target name="tearDown">
    <delete dir="${output.dir}" quiet="true"/>
  </target>

  <target name="xtestWithReportSentToAnt">
    <rat:report>
      <file file="${ant.file}"/>
    </rat:report>
    <au:assertLogContains text="${expectedOutput}"/>
  </target>

  <target name="testWithReportSentToFile" depends="fileReportTest">
    <rat:report reportFile="${report.file}">
      <file file="${ant.file}"/>
    </rat:report>
    <au:assertLogDoesntContain text="${expectedOutput}"/>
    <au:assertFileExists file="${report.file}"/>
    <assertReportContains text="${expectedOutput}"/>
  </target>

  <target name="testXMLReportSentToFile" depends="fileReportTest">
    <rat:report reportFile="${report.file}.xml" format="xml">
      <file file="${ant.file}"/>
    </rat:report>
    <au:assertLogDoesntContain text="${expectedOutputXML}"/>
    <au:assertLogDoesntContain text="${expectedOutputXML2}"/>
    <au:assertFileExists file="${report.file}.xml"/>
    <assertReportContains text="${expectedOutputXML}"
                          file="${report.file}.xml"/>
    <assertReportContains text="${expectedOutputXML2}"
                          file="${report.file}.xml"/>
  </target>

  <target name="xtestWithALUnknown">
    <rat:report addDefaultLicenseMatchers="false">
      <file file="${ant.file}"/>
      <rat:w3c/>
    </rat:report>
    <au:assertLogDoesntContain text="${expectedOutput}"/>
    <au:assertLogContains text="!????? ${file.name}"/>
  </target>

  <target name="testWithALUnknownSentToFile" depends="fileReportTest">
    <rat:report addDefaultLicenseMatchers="false" reportFile="${report.file}">
      <file file="${ant.file}"/>
      <rat:w3c/>
    </rat:report>
    <au:assertLogDoesntContain text="${expectedOutput}"/>
    <au:assertLogDoesntContain text="!????? ${file.name}"/>
    <assertReportContains text="!????? ${file.name}"/>
  </target>

  <target name="xtestCustomMatcher" depends="prepareCustomMatcher">
    <rat:report>
      <file file="${ant.file}"/>
      <exmpl/>
      <exmpl-license/>
    </rat:report>
    <au:assertLogDoesntContain text="${expectedOutput}"/>
    <au:assertLogContains text=" EXMPL ${file.name}"/>
  </target>

  <target name="testCustomMatcherSentToFile"
          depends="fileReportTest,prepareCustomMatcher">
    <rat:report reportFile="${report.file}">
      <file file="${ant.file}"/>
      <exmpl/>
      <exmpl-license/>
    </rat:report>
    <au:assertLogDoesntContain text="${expectedOutput}"/>
    <au:assertLogDoesntContain text=" EXMPL ${file.name}"/>
    <assertReportContains text=" EXMPL ${file.name}"/>
  </target>

  <target name="testSubstringMatcherNotApprovedSentToFile"
          depends="fileReportTest">
    <rat:report reportFile="${report.file}"
                addDefaultLicenseMatchers="false">
      <file file="${ant.file}"/>
      <rat:substringMatcher
          licenseFamilyCategory="YASL1"
          licenseFamilyName="Yet Another Software License (YASL) 1.0"
          notes="Interesting License">
        <pattern substring="exmpl"/>
      </rat:substringMatcher>
    </rat:report>
    <assertReportContains text="!YASL1 ${file.name}"/>
    <assertReportDoesntContain text=" YASL1 ${file.name}"/>
  </target>

  <target name="testSubstringMatcherApprovedSentToFile"
          depends="fileReportTest">
    <rat:report reportFile="${report.file}"
                addDefaultLicenseMatchers="false">
      <file file="${ant.file}"/>
      <rat:substringMatcher
          licenseFamilyCategory="YASL1"
          licenseFamilyName="Yet Another Software License (YASL) 1.0"
          notes="Interesting License">
        <pattern substring="exmpl"/>
      </rat:substringMatcher>
      <rat:approvedLicense
          familyName="Yet Another Software License (YASL) 1.0"/>
    </rat:report>
    <assertReportContains text=" YASL1 ${file.name}"/>
    <assertReportDoesntContain text="!YASL1 ${file.name}"/>
  </target>

  <target name="testSubstringMatcherSentToFileNoMatch"
          depends="fileReportTest">
    <rat:report reportFile="${report.file}"
                addDefaultLicenseMatchers="false">
      <file file="${ant.file}"/>
      <rat:substringMatcher
          licenseFamilyCategory="YASL1"
          licenseFamilyName="Yet Another Software License (YASL) 1.0"
          notes="Interesting License">
        <!-- abusing XML attribute whitespace normalization to create
             a substring that is not contained inside the build file
             itself -->
        <pattern substring="exmpl
"/>
      </rat:substringMatcher>
    </rat:report>
    <assertReportDoesntContain text="!YASL1 ${file.name}"/>
    <assertReportDoesntContain text=" YASL1 ${file.name}"/>
  </target>

  <target name="testFullTextMatcherNotApprovedSentToFile"
          depends="fileReportTest">
    <rat:report reportFile="${report.file}"
                addDefaultLicenseMatchers="false">
      <file file="${ant.file}"/>
      <rat:fullTextMatcher
          licenseFamilyCategory="YASL1"
          licenseFamilyName="Yet Another Software License (YASL) 1.0"
          notes="Interesting License">
        License Family Name: Yet Another Software License (YASL) 1.0
      </rat:fullTextMatcher>
    </rat:report>
    <assertReportContains text="!YASL1 ${file.name}"/>
    <assertReportDoesntContain text=" YASL1 ${file.name}"/>
  </target>

  <target name="testFullTextMatcherApprovedSentToFile"
          depends="fileReportTest">
    <rat:report reportFile="${report.file}"
                addDefaultLicenseMatchers="false">
      <file file="${ant.file}"/>
      <rat:fullTextMatcher
          licenseFamilyCategory="YASL1"
          licenseFamilyName="Yet Another Software License (YASL) 1.0"
          notes="Interesting License">
        License Family Name: Yet Another Software License (YASL) 1.0
      </rat:fullTextMatcher>
      <rat:approvedLicense
          familyName="Yet Another Software License (YASL) 1.0"/>
    </rat:report>
    <assertReportContains text=" YASL1 ${file.name}"/>
    <assertReportDoesntContain text="!YASL1 ${file.name}"/>
  </target>

  <target name="testFullTextMatcherSentToFileNoMatch"
          depends="fileReportTest">
    <pathconvert dirsep="/" property="alt.file.name">
      <path>
        <pathelement location="report-bad-configurations.xml"/>
      </path>
    </pathconvert>
    <rat:report reportFile="${report.file}"
                addDefaultLicenseMatchers="false">
      <file file="report-bad-configurations.xml"/>
      <rat:fullTextMatcher
          licenseFamilyCategory="YASL1"
          licenseFamilyName="Yet Another Software License (YASL) 1.0"
          notes="Interesting License">
This text shouldn't exist in report-bad-configuration.xml
      </rat:fullTextMatcher>
    </rat:report>
    <assertReportDoesntContain text="!YASL1 ${alt.file.name}"/>
    <assertReportDoesntContain text=" YASL1 ${alt.file.name}"/>
  </target>

  <target name="testFullTextMatcherExpandsProperties"
          depends="fileReportTest">
    <pathconvert dirsep="/" property="alt.file.name">
      <path>
        <pathelement location="report-bad-configurations.xml"/>
      </path>
    </pathconvert>
    <property name="test.version" value="1.0"/>
    <rat:report reportFile="${report.file}"
                addDefaultLicenseMatchers="false">
      <file file="report-bad-configurations.xml"/>
      <rat:fullTextMatcher
          licenseFamilyCategory="YASL1"
          licenseFamilyName="Yet Another Software License (YASL) 1.0"
          notes="Interesting License">
        License Family Name: Yet Another Software License (YASL) ${test.version}
      </rat:fullTextMatcher>
    </rat:report>
    <assertReportContains text="!YASL1 ${alt.file.name}"/>
    <assertReportDoesntContain text=" YASL1 ${alt.file.name}"/>
  </target>

  <target name="fileReportTest" depends="setUp">
    <property name="report.file" location="${output.dir}/selftest.report"/>
    <au:assertFileDoesntExist file="${report.file}"/>
    <macrodef name="assertReportContains">
      <attribute name="text"/>
      <attribute name="file" default="${report.file}"/>
      <sequential>
        <loadfile srcFile="@{file}" property="report"/>
        <au:assertTrue message="expected report to contain '@{text}' but was '${report}'">
          <contains string="${report}" substring="@{text}"/>
        </au:assertTrue>
      </sequential>
    </macrodef>
    <macrodef name="assertReportDoesntContain">
      <attribute name="text"/>
      <attribute name="file" default="${report.file}"/>
      <sequential>
        <loadfile srcFile="@{file}" property="report"/>
        <au:assertFalse message="expected report to not contain '@{text}' but was '${report}'">
          <contains string="${report}" substring="@{text}"/>
        </au:assertFalse>
      </sequential>
    </macrodef>
  </target>

  <target name="prepareCustomMatcher">
    <mkdir dir="${output.dir}/dest"/>
    <mkdir dir="${output.dir}/src/org/example"/>
    <echo file="${output.dir}/src/org/example/Matcher.java"><![CDATA[
package org.example;
import org.apache.rat.api.Document;
import org.apache.rat.api.MetaData;
import org.apache.rat.analysis.IHeaderMatcher;
import org.apache.rat.analysis.license.BaseLicense;
import org.apache.rat.analysis.RatHeaderAnalysisException;
public class Matcher extends BaseLicense implements IHeaderMatcher {
    public Matcher() {
        super(new MetaData.Datum(MetaData.RAT_URL_LICENSE_FAMILY_CATEGORY, "EXMPL"),
              new MetaData.Datum(MetaData.RAT_URL_LICENSE_FAMILY_NAME, "Example License"),
              "");
    }
    public void reset() {}
    
    public boolean match(Document subject, String line) throws RatHeaderAnalysisException {
        reportOnLicense(subject);
        return true;
    }
}
]]></echo>
    <echo file="${output.dir}/src/org/example/LicenseFamily.java"><![CDATA[
package org.example;
import org.apache.rat.license.ILicenseFamily;
public class LicenseFamily implements ILicenseFamily {
    public String getFamilyName() {
        return "Example License";
    }
}
]]></echo>
    <javac srcdir="${output.dir}/src" destdir="${output.dir}/dest"
           classpath="${test.classpath}" fork="true"/>
    <typedef name="exmpl" classname="org.example.Matcher"
             classpathref="all-classes-needed-for-tests"
             loaderref="testloader"/>
    <typedef name="exmpl-license" classname="org.example.LicenseFamily"
             classpathref="all-classes-needed-for-tests"
             loaderref="testloader"/>
  </target>

</project>
