<?xml version="1.0" encoding="UTF-8"?>
<!--

    Copyright (c) 2012, 2023 Oracle and/or its affiliates. All rights reserved.

    This program and the accompanying materials are made available under the
    terms of the Eclipse Public License v. 2.0, which is available at
    http://www.eclipse.org/legal/epl-2.0.

    This Source Code may also be made available under the following Secondary
    Licenses when the conditions for such availability set forth in the
    Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
    version 2 with the GNU Classpath Exception, which is available at
    https://www.gnu.org/software/classpath/license.html.

    SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0

-->

<project name="mailtck" default="default" basedir="." xmlns:if="ant:if">
    <description>Builds and runs the project mailtck.</description>

    <property name="version" value="2.1"/>
    <property name="version.tck" value="2.1.2"/>
    <property name="api.module.name" value="jakarta.mail"/>


    <property environment="env"/>
    <dirname property="mailtck.basedir" file="${ant.file.mailtck}"/>
    <property name="ts.home" value="${mailtck.basedir}"/>

    <property file="${ts.home}/lib/ts.jte"/>

    <property name="class.dir" value="${ts.home}${file.separator}classes"/>
    <property name="tests.dir" value="${ts.home}${file.separator}tests"/>
    <property name="work.dir" value="${ts.home}/JTwork"/>
    <property name="report.dir" value="${ts.home}/JTreport"/>

    <property name="lib.dir" value="${ts.home}${file.separator}lib"/>
    <property name="harness.dir" value="${ts.home}${file.separator}classes-harness"/>

    <property name="class.pluggability.dir" value="${ts.home}${file.separator}classes-pluggability"/>
    <property name="class.providers.dir" value="${ts.home}${file.separator}classes-providers"/>

    <property name="report.api.dir" value="${ts.home}/JTreport"/>
    <property name="report.pluggability.dir" value="${ts.home}/JTreport-Pluggability"/>

    <mkdir dir="${harness.dir}"/>
    <mkdir dir="${class.dir}"/>
    <mkdir dir="${class.pluggability.dir}"/>
    <mkdir dir="${class.providers.dir}"/>
    <mkdir dir="${work.dir}"/>
    <mkdir dir="${report.pluggability.dir}"/>
    <mkdir dir="${class.dir}"/>
    <mkdir dir="${work.dir}"/>
    <mkdir dir="${report.dir}"/>

    <condition property="tests.arg" value="-tests ${tests}" else="">
            <isset property="tests"/>
    </condition>

    <condition property="tests.debug" value="-D" else="">
            <isset property="debug"/>
    </condition>

    <property name="module.path" value="${API_JAR}"/>
    <property name="class.path" value="${class.dir}${path.separator}${ts.home}/sigtest.jar${path.separator}${ts.home}/javatest.jar${path.separator}${module.path}"/>
    <property name="pluggability.class.path" value="${class.pluggability.dir}${path.separator}${ts.home}/javatest.jar${path.separator}"/>

    <property name="java.classes.for.sig.ext" value="lib/modules"/>

    <!-- Empty string to test without modules, "Mod" to test with modules -->
    <property name="lib.modules" value="" />

    <target name="build"
            description="Compiles all sources to the classes directory">
        <javac includeantruntime="false" deprecation="yes"
               release="11" srcdir="tests/api"
               classpath="${class.path}" destdir="${class.dir}"
               debug="on">
        </javac>
        <javac includeantruntime="false" deprecation="yes"
               release="11" srcdir="tests/pluggability"
               classpath="${class.path}" destdir="${class.pluggability.dir}"
               debug="on">
        </javac>
        <javac includeantruntime="false" deprecation="yes"
               release="11"
               srcdir="providers"
               classpath="${class.path}" destdir="${class.providers.dir}"
               debug="on">
        </javac>
        <copy todir="${class.providers.dir}/META-INF">
            <fileset dir="providers/META-INF"/>
        </copy>
        <jar destfile="${class.providers.dir}/mail_alternate_provider.jar" basedir="${class.providers.dir}"/>
        <javac includeantruntime="false" deprecation="yes"
               release="11" srcdir="harness"
               classpath="${class.path}" destdir="${harness.dir}"
               debug="on">
        </javac>
        <copy todir="${class.dir}">
            <fileset dir="${harness.dir}"/>
        </copy>
        <copy todir="${class.pluggability.dir}">
            <fileset dir="${harness.dir}"/>
        </copy>

        <javac includeantruntime="false" deprecation="yes"
               release="11"
               srcdir="${tests.dir}${file.separator}mailboxes"
               classpath="${class.path}"
               destdir="${tests.dir}${file.separator}mailboxes"
               debug="on">
        </javac>
    </target>

    <target name="clean"
            description="Cleans all classes from the classes directory">
        <delete failonerror="false" includeEmptyDirs="true">
            <fileset dir="${class.dir}" includes="*/**"/>
            <fileset dir="${harness.dir}" includes="*/**"/>
            <fileset dir="${class.pluggability.dir}" includes="*/**"/>
            <fileset dir="${class.providers.dir}" includes="*/**"/>
        </delete>
    </target>

    <target name="run"
            description="Executes the Jakarta Mail TCK in batch mode">
        <local name="ts.jte"/>
        <local name="run.class.path"/>
        <local name="report.dir"/>
        <local name="test.suite"/>
        <local name="ts.jtx"/>
        <property name="report.dir" value="${report.api.dir}"/>
        <property name="test.suite" value="tests/api"/>
        <property name="ts.jte" value="ts.jte"/>
        <property name="ts.jtx" value="${ts.home}/lib/ts.jtx"/>
        <property name="run.class.path" value="${class.path}"/>
        <javatest.batch/>
    </target>

    <target name="run.pluggability"
            description="Executes the Mail TCK pluggability tests in batch mode">
        <local name="ts.jte"/>
        <local name="run.class.path"/>
        <local name="report.dir"/>
        <local name="test.suite"/>
        <local name="ts.jtx"/>
        <property name="report.dir" value="${report.pluggability.dir}"/>
        <property name="test.suite" value="tests/pluggability"/>
        <property name="ts.jte" value="ts.pluggability.jte"/>
        <property name="ts.jtx" value="${ts.home}/lib/ts.jtx"/>
        <property name="run.class.path" value="${pluggability.class.path}"/>
        <javatest.batch/>
    </target>

    <target name="runmod"
            description="Executes the Jakarta Mail TCK in batch mode with modules">
        <local name="ts.jte"/>
        <local name="run.class.path"/>
        <local name="report.dir"/>
        <local name="test.suite"/>
        <local name="ts.jtx"/>
        <property name="report.dir" value="${report.api.dir}"/>
        <property name="test.suite" value="tests/api"/>
        <property name="ts.jte" value="ts.jte"/>
        <property name="ts.jtx" value="${ts.home}/lib/ts.jtx"/>
        <property name="run.class.path" value="${class.path}"/>
        <property name="lib.modules" value="Mod" />
        <javatest.batch/>
    </target>

    <target name="gui"
            description="Executes the Jakarta Mail TCK in GUI mode">
        <javatest.gui/>
    </target>

    <target name="record.sigtest">
        <java fork="true" jar="./sigtestdev.jar">
                <arg value="Setup"/>
                <arg value="-Classpath"/>
                <arg value="${java.home}/${java.classes.for.sig.ext}${path.separator}${class.path}"/>
                <arg value="-Package"/>
                <arg value="jakarta.mail"/>
                <arg value="-Package"/>
                <arg value="jakarta.mail.event"/>
                <arg value="-Package"/>
                <arg value="jakarta.mail.internet"/>
                <arg value="-Package"/>
                <arg value="jakarta.mail.search"/>
                <arg value="-Package"/>
                <arg value="jakarta.mail.util"/>
                <arg value="-FileName"/>
                <arg value="${ts.home}/tests/jakarta.mail_${version}_java_${java.specification.version}.sig"/>
                <arg value="-Static"/>
                <arg value="-Xjimage" if:true="${jdk.modules}"/>
                <arg value="${java.home}/bin/jimage" if:true="${jdk.modules}"/>
        </java>
    </target>

    <presetdef name="javatest">

        <!--
          The javatest task is a presetdef that sets many of the common
          attributes and elements used by both the GUI and batch mode
          invocations of javatest with the spider workspace.  To see the preset
          values, refer to the included XML below that shows the content of
          the javatest presetdef.
        -->

        <java classname="com.sun.javatest.tool.Main"
              failonerror="${failOnError}" fork="yes" classpath="${run.class.path}">
	    <sysproperty key="testDebug" value="${tests.debug}"/>
        </java>
    </presetdef>

    <presetdef name="javatest.batch">

        <!--
          The javatest.batch task is a presetdef that calls the javatest
          presetdef passing the additional XML content needed to invoke
          javatest in batch mode.  To see the preset values, refer to the
          included XML below that shows the content of the javatest.batch
          presetdef.
        -->

        <javatest>
            <arg line=" -EsysProps" />
            <arg line=" -batch" />
            <arg line=" -ts ${test.suite}/testsuite.html" />
            <arg line=" -workdir ${work.dir}" />
            <arg line=" -envFiles ${ts.home}/lib/${ts.jte}" />
            <arg line=" -env mailUnix${lib.modules}" />
            <arg line=" -excludeList ${ts.home}/lib/ts.jtx" />
            <arg line=" -timeoutFactor 1" />
            <arg line=" ${tests.arg}" />
            <arg line=" -runtests -writereport ${report.dir}"/>
        </javatest>
    </presetdef>


    <presetdef name="javatest.gui">

        <!--
          The javatest.gui task is a presetdef that calls the javatest presetdef
          passing the additional XML content needed to invoke the javatest GUI.
          To see the preset values, refer to the included XML below that shows
          the content of the javatest.gui presetdef.
        -->

        <javatest>
            <arg    line="-open ${ts.home}/lib/ts.jti" />
            <arg    line="-EsysProps" />
        </javatest>
    </presetdef>

    <presetdef name="javatest.help">

        <javatest>
            <arg line="-help" />

        </javatest>
    </presetdef>

</project>
