<?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 default="antunit" name="project-test" xmlns:au="antlib:org.apache.ant.antunit">
  <import file="../antunit-base.xml" />

  <target name="-createBuildDatePrinter">
    <mkdir dir="${input}"/>
    <mkdir dir="${output}"/>
    <echo file="${input}/BuildDatePrinter.java"><![CDATA[
      import org.apache.tools.ant.BuildException;
      import org.apache.tools.ant.Project;
      import java.text.SimpleDateFormat;
      import java.util.*;
      public class BuildDatePrinter {
        public static void main(String[] args) {
          Project p = new Project();
          int argsLen = args.length;
          if (argsLen % 2 != 0) {
            throw new BuildException("can't handle odd number of arguments");
          }
          for (int i = 0; i < argsLen; i += 2) {
            p.setProperty(args[i], args[i + 1]);
          }
          Date buildDate = p.getBuildDate();
          SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
          format.setTimeZone(TimeZone.getTimeZone("UTC"));
          System.out.println("BUILDDATE is " + format.format(buildDate));
        }
      }
    ]]></echo>
    <javac srcdir="${input}" destdir="${output}"/>
  </target>

  <target name="testGetBuildDateMagicProperty" depends="-createBuildDatePrinter">
    <local name="testout"/>
    <java classname="BuildDatePrinter"
          failonerror="true"
          outputproperty="testout"
          fork="true">
      <classpath>
        <pathelement location="${output}"/>
        <pathelement path="${java.class.path}"/>
      </classpath>
      <arg value="ant.tstamp.now"/>
      <arg value="86400"/>
    </java>
    <au:assertPropertyContains name="testout" value="BUILDDATE is 1970-01-02T00:00:00"/>
  </target>

  <target name="testGetBuildDateMagicPropertyIso" depends="-createBuildDatePrinter">
    <local name="testout"/>
    <java classname="BuildDatePrinter"
          failonerror="true"
          outputproperty="testout"
          fork="true">
      <classpath>
        <pathelement location="${output}"/>
        <pathelement path="${java.class.path}"/>
      </classpath>
      <arg value="ant.tstamp.now.iso"/>
      <arg value="1972-04-17T08:07:00Z"/>
    </java>
    <au:assertPropertyContains name="testout" value="BUILDDATE is 1972-04-17T08:07:00"/>
  </target>

  <target name="testGetBuildDateMagicPropertyBoth" depends="-createBuildDatePrinter">
    <local name="testout"/>
    <java classname="BuildDatePrinter"
          failonerror="true"
          outputproperty="testout"
          fork="true">
      <classpath>
        <pathelement location="${output}"/>
        <pathelement path="${java.class.path}"/>
      </classpath>
      <arg value="ant.tstamp.now"/>
      <arg value="86400"/>
      <arg value="ant.tstamp.now.iso"/>
      <arg value="1972-04-17T08:07:00Z"/>
    </java>
    <au:assertPropertyContains name="testout" value="BUILDDATE is 1972-04-17T08:07:00"/>
  </target>

  <target name="testGetBuildDateSourceDateEpoch" depends="-createBuildDatePrinter">
    <local name="testout"/>
    <java classname="BuildDatePrinter"
          failonerror="true"
          outputproperty="testout"
          fork="true">
      <classpath>
        <pathelement location="${output}"/>
        <pathelement path="${java.class.path}"/>
      </classpath>
      <arg value="ant.tstamp.now"/>
      <arg value="86400"/>
      <arg value="ant.tstamp.now.iso"/>
      <arg value="1972-04-17T08:07:00Z"/>
      <env key="SOURCE_DATE_EPOCH" value="1650585600"/>
    </java>
    <au:assertPropertyContains name="testout" value="BUILDDATE is 2022-04-22T00:00:00"/>
  </target>

  <target name="testGetBuildDateRealNow" depends="-createBuildDatePrinter">
    <local name="dstamp"/>
    <local name="testout"/>
    <!-- ignoring the time part and hoping the clock doesn't pass
         midnight between running tstamp and BuildDatePrinter -->
    <tstamp>
      <format pattern="yyyy-MM-dd" property="dstamp" timezone="UTC"/>
    </tstamp>
    <java classname="BuildDatePrinter"
          failonerror="true"
          outputproperty="testout"
          fork="true">
      <classpath>
        <pathelement location="${output}"/>
        <pathelement path="${java.class.path}"/>
      </classpath>
    </java>
    <au:assertPropertyContains name="testout" value="BUILDDATE is ${dstamp}T"/>
  </target>
</project>
