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

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

  <extension-point name="testExtensionPointWorksLikeTarget"
                depends="setProperty, assertProperty"/>

  <target name="setProperty">
    <property name="foo" value="bar"/>
  </target>

  <target name="assertProperty">
    <au:assertPropertyEquals name="foo" value="bar"/>
  </target>

  <target name="testExtensionPointMustBeEmpty">
    <mkdir dir="${output}"/>
    <echo file="${output}/build.xml"><![CDATA[
<project>
  <extension-point name="foo">
    <echo>bar</echo>
  </extension-point>
</project>]]></echo>
    <au:expectfailure
       expectedMessage="you must not nest child elements into an extension-point">
      <ant dir="${output}"/>
    </au:expectfailure>
  </target>

  <target name="testAddToExtensionPoint">
    <mkdir dir="${output}"/>
    <echo file="${output}/build.xml"><![CDATA[
<project default="foo">
  <extension-point name="foo"/>
  <target name="bar" extensionOf="foo">
    <echo>In target bar</echo>
  </target>
</project>]]></echo>
    <ant dir="${output}"/>
    <au:assertLogContains text="In target bar"/>
  </target>

  <target name="testCantAddToPlainTarget">
    <mkdir dir="${output}"/>
    <echo file="${output}/build.xml"><![CDATA[
<project default="foo">
  <target name="foo"/>
  <target name="bar" extensionOf="foo"/>
</project>]]></echo>
    <au:expectfailure
       expectedMessage="referenced target foo is not an extension-point">
      <ant dir="${output}"/>
    </au:expectfailure>
  </target>

  <target name="testExtensionPointInImportedBuildfile" description="Bug 48804">
    <mkdir dir="${output}"/>
    <echo file="${output}/master.xml"><![CDATA[
<project default="bar">
  <extension-point name="foo"/>
  <target name="bar" depends="foo"/>
</project>]]></echo>
    <echo file="${output}/build.xml"><![CDATA[
<project>
  <import file="master.xml"/>
  <target name="prepare" extensionOf="foo">
    <echo>in target prepare</echo>
  </target>
</project>]]></echo>
    <ant dir="${output}" target="bar"/>
    <au:assertLogContains text="in target prepare"/>
  </target>

  <target name="testExtensionPointInIncludedBuildfileWithNestedInclude">
    <mkdir dir="${output}"/>
    <echo file="${output}/abstract-compile.xml"><![CDATA[
<project name="abstract-compile">
  <extension-point name="compile"/>
</project>]]></echo>
    <echo file="${output}/compile-java.xml"><![CDATA[
<project name="compile-java">
  <include file="abstract-compile.xml" as="abstract-compile"/>
  <target name="compile-java" extensionOf="abstract-compile.compile">
    <echo>in compile java</echo>
  </target>
</project>]]></echo>
    <echo file="${output}/build.xml"><![CDATA[
<project name="master">
  <include file="compile-java.xml" as="compile"/>
</project>]]></echo>
    <!-- here prefix should be concatenated from each include first
         "compile" then "abstract-compile"-->
    <ant dir="${output}" target="compile.abstract-compile.compile"/>
    <au:assertLogContains text="in compile java"/>

  </target>

  <target name="testExtensionPointInIncludedBuildfileWithNestedImport">
    <mkdir dir="${output}"/>
    <echo file="${output}/abstract-compile.xml"><![CDATA[
<project name="abstract-compile">
  <extension-point name="compile"/>
</project>]]></echo>
    <echo file="${output}/compile-java.xml"><![CDATA[
<project name="compile-java">
  <import file="abstract-compile.xml"/>
  <target name="compile-java" extensionOf="compile">
    <echo>in compile java</echo>
  </target>
</project>]]></echo>
    <echo file="${output}/build.xml"><![CDATA[
<project name="master">
  <include file="compile-java.xml" as="compile"/>
</project>]]></echo>
    <!-- here the prefix should be "compile" as the import containing
         the extension point is done inside an include using "compile"
         as prefix -->
    <ant dir="${output}" target="compile.compile"/>
    <au:assertLogContains text="in compile java"/>

  </target>

  <target name="testExtensionPointInImportedBuildfileWithNestedImport">
    <mkdir dir="${output}"/>
    <echo file="${output}/abstract-compile.xml"><![CDATA[
<project name="abstract-compile">
  <extension-point name="compile"/>
</project>]]></echo>
    <echo file="${output}/compile-java.xml"><![CDATA[
<project name="compile-java">
  <import file="abstract-compile.xml"/>
  <target name="compile-java" extensionOf="compile">
    <echo>in compile java</echo>
  </target>
</project>]]></echo>
    <echo file="${output}/build.xml"><![CDATA[
<project name="master">
  <import file="compile-java.xml"/>
</project>]]></echo>
    <!-- here extension point should not be prefixed at all -->
    <ant dir="${output}" target="compile"/>
    <au:assertLogContains text="in compile java"/>

  </target>



  <target name="testMissingExtensionPointCausesError">
    <mkdir dir="${output}"/>
    <echo file="${output}/build.xml"><![CDATA[
<project default="bar">
  <target name="bar" extensionOf="foo"/>
</project>]]></echo>
    <au:expectfailure
        expectedMessage="can't add target bar to extension-point foo because the extension-point is unknown">
      <ant dir="${output}" target="bar"/>
    </au:expectfailure>
  </target>

  <target name="testMissingExtensionPointCausesWarningWhenConfigured">
    <mkdir dir="${output}"/>
    <echo file="${output}/build.xml"><![CDATA[
<project default="bar">
  <target name="bar" extensionOf="foo" onMissingExtensionPoint="warn"/>
</project>]]></echo>
    <ant dir="${output}" target="bar"/>
    <au:assertLogContains level="warning"
        text="can't add target bar to extension-point foo because the extension-point is unknown" />
  </target>

  <target name="testMissingExtensionPointIgnoredWhenConfigured">
    <mkdir dir="${output}"/>
    <echo file="${output}/build.xml"><![CDATA[
<project default="bar">
  <target name="bar" extensionOf="foo" onMissingExtensionPoint="ignore"/>
</project>]]></echo>
    <ant dir="${output}" target="bar"/>
    <au:assertLogDoesntContain level="warning"
        text="can't add target bar to extension-point foo because the extension-point is unknown" />
  </target>

  <target name="testOnlyAllowsExtensionPointMissingAttributeWhenExtensionOfPresent">
    <mkdir dir="${output}"/>
    <echo file="${output}/build.xml"><![CDATA[
<project default="bar">
  <target name="bar" onMissingExtensionPoint="ignore"/>
</project>]]></echo>
    <au:expectfailure
        expectedMessage="onMissingExtensionPoint attribute cannot be specified unless extensionOf is specified">
      <ant dir="${output}" target="bar"/>
    </au:expectfailure>
  </target>
</project>
