<?xml version="1.0"?>
<!--

    Copyright (c) 2018, 2020 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="Assertion Coverage Generation" default="usage" basedir=".">
  
  <!-- ######################################################################## -->
  <!-- ######################################################################## -->
  <!--  This ant build file generates the coverage numbers for each technology  -->
  <!--  area that is part of the J2EE TCK.  Each technology area has a target   -->
  <!--  defined in this file which generates the coverage numbers for that      -->
  <!--  technology area.  There is also an "all" target that generates all the  -->
  <!--  coverage numbers for all defined technology targets.                    -->
  <!--                                                                          -->
  <!--  To define a new target copy one of the existing targets and supply the  -->
  <!--  appropriate properties.  Or use one of the template proerties listed at -->
  <!--  the end of this file.  The user must define the following params:       -->
  <!--    api.coverage.dir : location to write API coverage results to          -->
  <!--    api.assertions   : location of API assertion document                 -->
  <!--    spec.coverage.dir: location to write SPEC coverage results to         -->
  <!--    spec.assertions  : location of SPEC assertion document                -->
  <!--    test.src.dir     : relative location of test source code              -->
  <!--    filter.string    : this is an optional parameter, it is only used in  -->
  <!--                       the case where users want to pass a filter string  -->
  <!--                       to the legacy assert utility (see the legacy       -->
  <!--                       assert utility README for details)                 -->
  <!--                                                                          -->
  <!--  Note: Some technologies only have API or SPEC assertions.  In those     -->
  <!--        cases omit the params that do not apply.  For instance, if your   -->
  <!--        technology only has API assertions delete the spec.coverage.dir   -->
  <!--        and spec.assertions parameters from your technology task.         -->
  <!--                                                                          -->
  <!--        Be sure to add a version ID to name of the directory you write    -->
  <!--        your API and SPEC assertion coverage numbers to.                  -->
  <!--        All users should follow this style so there technology version    -->
  <!--        will be picked up by the assertion coverage tools.                -->
  <!--                                                                          -->
  <!--                                                                          -->
  <!--  After defining your technolgy target, add your technology target name   -->
  <!--  to the depends list of the all target.  This will cause your technology -->
  <!--  to be invoked when the all target is invoked.                           -->
  <!-- ######################################################################## -->
  <!-- ######################################################################## -->


<!-- ############################################################################ -->
<!--  Required properties                                                         -->
<!-- ############################################################################ -->

  <property environment="env"/>
  <property name="ts.home"                   value="${env.TS_HOME}"/>
  <property name="ts.test.dir"               value="${ts.home}/src/com/sun/ts/tests"/>
  <property name="ts.docs.dir"               value="${ts.home}/internal/docs"/>
  <property name="ts.tools.home"             value="${env.TS_TOOLS_HOME}"/>
  <property name="assertion.coverage"        value="${ts.tools.home}/tools/assertion-coverage"/>
  <property name="legacy.assert"             value="${ts.tools.home}/tools/legacy-assert"/>
  <property name="xsl.transform"             value="${ts.tools.home}/tools/xsl-transformer"/>
  <property name="legacy.assert.output"      value="/tmp/legacy-assert-output.temp"/>
  <property name="legacy.assert.output.spec" value="/tmp/spec-legacy-assert-output.temp"/>
  <property name="legacy.assert.output.api"  value="/tmp/api-legacy-assert-output.temp"/> 
<!--
  <property name="coverage.base.dir"         value="/net/jse/files2/entjava/CTS.other/XMLassertions/JavaEE5"/>
-->
  <property name="coverage.base.dir"         value="${ts.home}/internal/docs/security/coverage"/> 
  <property name="backup.ext"                value="bak"/>
  <property name="api.stylesheet"            value="${ts.tools.home}/docs/xsl/assertions/javadoc_assertions.xsl"/>
  <property name="spec.stylesheet"           value="${ts.tools.home}/docs/xsl/assertions/spec_assertions.xsl"/>
  <property name="api.html.assertion.file"   value="api-assertions.html"/>
  <property name="spec.html.assertion.file"  value="spec-assertions.html"/>


<!-- ############################################################################ -->
<!--  Helper targets used to calculate coverage numbers for each technology area  -->
<!-- ############################################################################ -->

  <target name="check.ts.home" unless="env.TS_HOME">
    <fail message="Please set the &quot;TS_HOME&quot; environment variable to an appropriate value."/>
  </target>

  <target name="check.ts.tools.home" unless="env.TS_TOOLS_HOME">
    <fail message="Please set the &quot;TS_TOOLS_HOME&quot; environment variable to an appropriate value."/>
  </target>

  <target name="check.src.dir" unless="test.src.dir.specified">
    <fail message="Please specify a valid value for the &quot;test.src.dir&quot; property"/>
  </target>

  <target name="backup">
    <property name="bak.dir" value="${move.dir}/bak"/>
    <mkdir dir="${bak.dir}"/>
    <copy todir="${bak.dir}" overwrite="true">
      <fileset dir="${move.dir}">
        <include name="*.xml"/>
        <include name="*.html"/>
      </fileset>
    </copy>
    <delete>
      <fileset dir="${move.dir}">
        <include name="*.xml"/>
        <include name="*.html"/>
      </fileset>
    </delete>
  </target>

  <target name="build.api.assertions.html" if="do.api">
    <echo message="Transforming API assertion doc:"/>
    <echo message="    xml-file    = ${api.assertions}"/>
    <echo message="    xslt-file   = ${api.stylesheet}"/>
    <echo message="    output-file = ${api.coverage.dir}/${api.html.assertion.file}"/>
    <ant dir="${xsl.transform}" target="run">
      <property name="xml-file"    value="${api.assertions}"/>
      <property name="xslt-file"   value="${api.stylesheet}"/>
      <property name="output-file" value="${api.coverage.dir}/${api.html.assertion.file}"/>
    </ant>
  </target>

  <target name="build.spec.assertions.html" if="do.spec">
    <echo message="Transforming SPEC assertion doc:"/>
    <echo message="    xml-file    = ${spec.assertions}"/>
    <echo message="    xslt-file   = ${spec.stylesheet}"/>
    <echo message="    output-file = ${spec.coverage.dir}/${spec.html.assertion.file}"/>
    <ant dir="${xsl.transform}" target="run">
      <property name="xml-file"    value="${spec.assertions}"/>
      <property name="xslt-file"   value="${spec.stylesheet}"/>
      <property name="output-file" value="${spec.coverage.dir}/${spec.html.assertion.file}"/>
    </ant>
  </target>

  <target name="generate.spec.assertion.coverage" if="do.spec">
    <antcall target="backup">
      <param name="move.dir" value="${spec.coverage.dir}"/>
    </antcall>
    <echo message="assertion-coverage (SPEC):"/>
    <echo message="    all-assertions-file    = ${spec.assertions}"/>
    <echo message="    tested-assertions-file = ${legacy.assert.output.spec}"/>
    <echo message="    coverage-dir           = ${spec.coverage.dir}"/>
    <ant dir="${assertion.coverage}" target="run">
      <property name="all-assertions-file"    value="${spec.assertions}"/>
      <property name="tested-assertions-file" value="${legacy.assert.output.spec}"/>
      <property name="coverage-dir"           value="${spec.coverage.dir}"/>
    </ant>
  </target>

  <target name="generate.api.assertion.coverage" if="do.api">
    <antcall target="backup">
      <param name="move.dir" value="${api.coverage.dir}"/>
    </antcall>
    <echo message="assertion-coverage (API):"/>
    <echo message="    all-assertions-file    = ${api.assertions}"/>
    <echo message="    tested-assertions-file = ${legacy.assert.output.api}"/>
    <echo message="    coverage-dir           = ${api.coverage.dir}"/>
    <ant dir="${assertion.coverage}" target="run">
      <property name="all-assertions-file"    value="${api.assertions}"/>
      <property name="tested-assertions-file" value="${legacy.assert.output.api}"/>
      <property name="coverage-dir"           value="${api.coverage.dir}"/>
    </ant>
  </target>

  <target name="generate.assertion.list.no.filter" unless="use.filter">
    <echo message="legacy-assert:"/>
    <echo message="    output.filename = ${legacy.assert.output}"/>
    <echo message="    initial.dir     = ${test.src.dir}"/>
    <ant dir="${legacy.assert}" target="get.assertions">
      <property name="output.filename"     value="${legacy.assert.output}"/>
      <property name="initial.dir"         value="${test.src.dir}"/>
      <property name="queue.handler.class" value="com.sun.ts.legacy.SplitCoverageHandler"/>
    </ant>
  </target>

  <target name="generate.assertion.list.filter" if="use.filter">
    <echo message="legacy-assert:"/>
    <echo message="    assertion.filter = ${filter.string}"/>
    <echo message="    output.filename  = ${legacy.assert.output}"/>
    <echo message="    initial.dir      = ${test.src.dir}"/>
    <ant dir="${legacy.assert}" target="get.assertions">
      <property name="output.filename"     value="${legacy.assert.output}"/>
      <property name="assertion.filter"    value="${filter.string}"/>
      <property name="initial.dir"         value="${test.src.dir}"/>
      <property name="queue.handler.class" value="com.sun.ts.legacy.SplitCoverageHandler"/>
    </ant>
  </target>

  <target name="calculate.coverage" depends="check.ts.home,check.ts.tools.home">
    <condition property="do.api">
      <and>
        <not>
          <equals arg1="${api.coverage.dir}" arg2="$${api.coverage.dir}"/>
        </not>
        <not>
          <equals arg1="${api.assertions}" arg2="$${api.assertions}"/>
        </not>
      </and>
    </condition>
    <condition property="do.spec">
      <and>
        <not>
          <equals arg1="${spec.coverage.dir}" arg2="$${spec.coverage.dir}"/>
        </not>
        <not>
          <equals arg1="${spec.assertions}" arg2="$${spec.assertions}"/>
        </not>
      </and>
    </condition>
    <condition property="use.filter">
      <not>
        <equals arg1="${filter.string}" arg2="$${filter.string}"/>
      </not>
    </condition>
    <condition property="test.src.dir.specified">
      <not>
        <equals arg1="${test.src.dir}" arg2="$${test.src.dir}"/>
      </not>
    </condition>
    <antcall target="check.src.dir"/>
    <antcall target="generate.assertion.list.filter"/>
    <antcall target="generate.assertion.list.no.filter"/>
    <antcall target="generate.api.assertion.coverage"/>
    <antcall target="generate.spec.assertion.coverage"/>
    <antcall target="build.api.assertions.html"/>    
    <antcall target="build.spec.assertions.html"/>
  </target>

<!-- ############################################################################ -->
<!--  Targets to create the assertion coverage numbers for each technology area   -->
<!-- ############################################################################ -->

  <target name="security">
    <antcall target="calculate.coverage">
      <param name="api.coverage.dir"  value="${coverage.base.dir}/security/api"/>
      <param name="spec.coverage.dir" value="${coverage.base.dir}/security/spec"/>
      <param name="api.assertions"    value="${ts.docs.dir}/security/JASPICJavadocAssertions.xml"/>
      <param name="spec.assertions"   value="${ts.docs.dir}/security/JASPICSpecAssertions.xml"/>
      <param name="test.src.dir"      value="/security"/>
      <param name="filter.string"     value="JASPIC"/>
    </antcall>
  </target>

  <property name="gen.index.dir" value="${ts.tools.home}/tools/gen-index"/>
  <target name="generate.index.page">
    <echo message=""/>
    <echo message="gen-index:"/>
    <echo message="    base.dir = ${coverage.base.dir}"/>
    <echo message=""/>
    <ant dir="${gen.index.dir}" target="run">
      <property name="base.dir" value="${coverage.base.dir}"/>
    </ant>
  </target>

  <property name="gen.summary.dir" value="${ts.tools.home}/tools/gen-summary"/>
  <target name="generate.exec.summary">
    <echo message=""/>
    <echo message="gen-exec-summary:"/>
    <echo message="    base.dir = ${coverage.base.dir}"/>
    <echo message=""/>
    <ant dir="${gen.summary.dir}" target="run">
      <property name="base.dir" value="${coverage.base.dir}"/>
    </ant>
  </target>

  <property name="document.dir" value="${ts.docs.dir}/html-assertions"/>
  <target name="copy.html.assertions">
    <copy todir="${document.dir}">
      <fileset dir="${coverage.base.dir}">
        <include name="**/api-assertions.html"/>
        <include name="**/spec-assertions.html"/>
        <include name="assertion-index.html"/>
        <exclude name="**/bak/*"/>
      </fileset>
    </copy>
  </target>

  <target name="set.permissions">
    <chmod perm="777" type="both">
      <fileset dir="${coverage.base.dir}" includes="**,**/*"/>
    </chmod>
  </target>

  <target name="build.page">
    <antcall target="generate.exec.summary"/>
    <antcall target="generate.index.page"/>
    <antcall target="set.permissions"/>
  </target>

  <target name="start.log">
   <record name="coverage.log" action="start"/>
  </target>

  <!-- APIs that are required by EE 5 spec but are missing assertion artifacts: -->
  <!-- JavaMail, JAF, connector, JAXB, Common Annotations (sorta, each area     -->
  <!-- must test these), JSP Debugging (may be included with jsp area,          -->
  <!-- not sure), stax. We also need to clean up webservices, the EE 5 spec     -->
  <!-- lists webservices 1.2, JAX-WS 2.0 and Web Services Metadata (not sure    -->
  <!-- how these map to our assertion docs).                                    -->

  <target name="all" depends="start.log, security">

    <antcall target="generate.exec.summary"/>
    <antcall target="generate.index.page"/>
    <antcall target="set.permissions"/>
    <record name="coverage.log" action="stop"/>
  </target>


<!-- ############################################################################ -->
<!--  Default target                                                              -->
<!-- ############################################################################ -->
  <target name="usage">
    <echo message="Usage ant -buildfile coverage-build.xml &lt;target&gt;"/>
    <echo message="  Where &lt;target&gt; can be:"/>
    <echo message="    usage   : Prints this message."/>
    <echo message="    all     : Calculates the assertion coverage numbers"/>
    <echo message="              for all defined technology targets."/>
    <echo message=""/>
    <echo message="Note: TS_HOME must be set to a valid TS workspace location and"/>
    <echo message="      TS_TOOLS_HOME must be set to a valid tools location that"/>
    <echo message="      has been built."/>
    <echo message=""/>
  </target>

</project>
