<?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">
  <!-- each test verifies that the PropertyEvaluator delegate works -->
  <import file="../antunit-base.xml" />

  <condition property="prereqs-ok">
    <and>
      <available classname="org.apache.bsf.BSFManager"/>
      <available classname="bsh.util.BeanShellBSFEngine"/>
    </and>
  </condition>

  <target name="setUp" unless="setup.complete" if="prereqs-ok">
    <script language="beanshell" manager="bsf">
      import org.apache.tools.ant.PropertyHelper;
      public class MapEvaluator implements PropertyHelper.PropertyEvaluator {
        HashMap map = new HashMap();
        public MapEvaluator() {
          map.put("string", "string");
          map.put("object", new Object());
          map.put("int", new Integer(1));
          map.put("null", null);
        }
        public Object evaluate(String property, PropertyHelper propertyHelper) {
          return map.get(property.toLowerCase());
        }
      }
      project.addReference("mapEvaluator", new MapEvaluator());
    </script>
    <propertyhelper>
      <delegate refid="mapEvaluator" />
    </propertyhelper>
    <property name="setup.complete" value="true" />
  </target>

  <target name="testValueTypes" depends="setUp" if="prereqs-ok">
    <!-- verify BC, strings -->
    <au:assertPropertyEquals name="string" value="${STRING}" />

    <!-- verify non-string properties -->
    <au:assertPropertyEquals name="object" value="${OBJECT}" />
    <au:assertPropertyEquals name="int" value="${INT}" />

    <!-- verify that a string containing nothing but a property reference is a valid value -->
    <property name="string2" value="${string}" />
    <au:assertPropertyEquals name="string2" value="${string}" />

    <property name="object2" value="${object}" />
    <!-- demonstrate that equals args can be non-string -->
    <au:assertPropertyEquals name="object2" value="${object}" />

    <property name="int2" value="${int}" />
    <au:assertPropertyEquals name="int2" value="${int}" />
  </target>

  <target name="testNull" depends="setUp" if="prereqs-ok">
    <!-- demonstrate that a null value always implies a nonexistent property -->
    <au:assertFalse>
      <isset property="null" />
    </au:assertFalse>
  </target>

  <target name="testAvailable" depends="setUp" if="prereqs-ok">
    <!-- verify the available task can set a non-string property -->
    <available file="${ant.file}" type="file" property="available.string" value="bc" />
    <au:assertPropertyEquals name="available.string" value="bc" />
    <available file="${ant.file}" type="file" property="available.object" value="${object}" />
    <au:assertPropertyEquals name="available.object" value="${OBJECT}" />
  </target>

  <target name="testCondition" depends="setUp" if="prereqs-ok">
    <!-- verify the condition task can set a non-string property -->
    <echo>$${ant.file}=${ant.file}</echo>
    <condition property="condition.true.string">
      <available file="${ant.file}" type="file" />
    </condition>
    <au:assertPropertyEquals name="condition.true.string" value="true" />
    <condition property="condition.else.string" value="true" else="false">
      <not><available file="${ant.file}" type="file" /></not>
    </condition>
    <au:assertPropertyEquals name="condition.else.string" value="false" />
    <condition property="condition.true.object" value="${object}">
      <available file="${ant.file}" type="file" />
    </condition>
    <au:assertPropertyEquals name="condition.true.object" value="${OBJECT}" />
    <condition property="condition.else.int" value="${object}" else="${int}">
      <not><available file="${ant.file}" type="file" /></not>
    </condition>
    <au:assertPropertyEquals name="condition.else.int" value="${INT}" />
  </target>

  <target name="testEmbeddedNonString" if="prereqs-ok">
    <!-- verify that a property embedded in a string is a substring -->
    <au:assertTrue>
      <equals arg1="@${int}@" arg2="@1@" />
    </au:assertTrue>
  </target>

  <target name="XtestLoadPropertiesWithObjects" if="prereqs-ok" depends="setUp">
    <au:assertFalse>
      <isset property="object2" />
    </au:assertFalse>
    <string id="props" value="object2=$${object}" />
    <!-- verify the property is not yet expanded -->
    <au:assertTrue>
      <length length="17">
        <resource refid="props" />
      </length>
    </au:assertTrue>
    <loadproperties>
      <resource refid="props" />
    </loadproperties>
    <au:assertPropertyEquals name="object2" value="${object}" />
    <au:assertPropertyEquals name="object2" value="${OBJECT}" />
  </target>

  <target name="testLoadPropertiesWithStrings" if="prereqs-ok" depends="setUp">
    <au:assertFalse>
      <isset property="string2" />
    </au:assertFalse>
    <string id="props" value="string2=$${string}" />
    <!-- verify the property is not yet expanded -->
    <au:assertTrue>
      <length length="17">
        <resource refid="props" />
      </length>
    </au:assertTrue>
    <loadproperties>
      <resource refid="props" />
    </loadproperties>
    <au:assertPropertyEquals name="string2" value="${string}" />
    <au:assertPropertyEquals name="string2" value="${STRING}" />
  </target>

</project>
