<?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 name="jar-test" basedir="." default="test1">

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

  <target name="setUp">
    <mkdir dir="${output}" />
  </target>

  <property name="tmp.jar" location="${output}/tmp.jar"/>
  <property name="tmp.dir" location="${output}/jartmp"/>
  <property name="tmp.zip" location="${output}/tmp.zip"/>
  <property name="tmp1.dir" location="${output}/jartmp1"/>
  <property name="tmp2.dir" location="${output}/jartmp2"/>

  <target name="test1">
    <jar/>
  </target>

  <target name="test2">
    <jar
    	jarfile="jar.tmp"
    	manifest="none"
    />
  </target>

  <target name="test3">
    <jar
    	destfile="jar.tmp"
	whenempty="format C: /y" 
    />
  </target>

  <target name="test4">
   <jar
    	destfile="${tmp.jar}"
	basedir="."
	includes="jar.xml"
    />
  </target>

  <target name="testNoRecreateWithUpdate">
    <jar
         destfile="${tmp.jar}"
         basedir="."
         includes="jar.xml"
         update="true"
    />
  </target>

  <target name="testRecreateNewerFileSetup" depends="test4">
    <touch file="jar.xml"/>
  </target>

  <target name="testRecreateWithoutUpdateAdditionalFiles">
    <jar
         destfile="${tmp.jar}"
         includes="*.xml"
         basedir="."
    />
  </target>

  <target name="testRecreateWithUpdateAdditionalFiles">
    <jar
         destfile="${tmp.jar}"
         basedir="."
         includes="*.xml"
         update="true"
    />
  </target>

  <target name="testRecreateWithoutUpdateNewerFile">
    <jar
         destfile="${tmp.jar}"
         basedir="."
         includes="jar.xml"
    />
  </target>

  <target name="testRecreateWithUpdateNewerFile">
    <jar
         destfile="${tmp.jar}"
         basedir="."
         includes="jar.xml"
         update="true"
    />
  </target>

  <target name="testManifestStaysIntact">
    <mkdir dir="${tmp.dir}"/>
    <manifest file="${tmp.dir}/manifest">
      <attribute name="Foo" value="bar"/>
    </manifest>
    <jar destfile="${tmp.jar}" basedir="." includes="jar.xml"
         manifest="${tmp.dir}/manifest"/>
    <touch file="jar.xml"/>
    <jar destfile="${tmp.jar}" basedir="." includes="jar.xml"
         update="true"/>
    <unjar src="${tmp.jar}" dest="${tmp.dir}"/>
  </target>    

  <target name="testNoRecreateBasedirExcludesWithUpdate">
    <jar
         destfile="${tmp.jar}"
         basedir="."
         includes="j*.xml"
         excludes="java.xml,jmod.xml"
         update="true"
    />
  </target>

  <target name="testNoRecreateBasedirExcludesWithoutUpdate">
    <jar
         destfile="${tmp.jar}"
         basedir="."
         includes="j*.xml"
         excludes="java.xml,jmod.xml"
    />
  </target>

  <target name="makezip">
    <zip destfile="${tmp.zip}"
         basedir="." includes="j*.xml"/>
  </target>

  <target name="testNoRecreateZipfilesetExcludesWithUpdate"
          depends="makezip">
    <jar destfile="${tmp.jar}"
         update="true">
      <zipfileset src="${tmp.zip}" excludes="java.xml,jmod.xml"/>
    </jar>
  </target>

  <target name="testNoRecreateZipfilesetExcludesWithoutUpdate"
          depends="makezip">
    <jar destfile="${tmp.jar}">
      <zipfileset src="${tmp.zip}" excludes="java.xml,jmod.xml"/>
    </jar>
  </target>

  <target name="testRecreateZipfilesetWithoutUpdateAdditionalFiles"
          depends="makezip">
    <jar destfile="${tmp.jar}">
      <zipfileset src="${tmp.zip}"/>
    </jar>
  </target>

  <target name="testRecreateZipfilesetWithUpdateAdditionalFiles"
          depends="makezip">
    <jar destfile="${tmp.jar}"
         update="true">
      <zipfileset src="${tmp.zip}"/>
    </jar>
  </target>

  <target name="testRecreateZipfilesetWithoutUpdateNewerFile"
          depends="makezip">
    <jar destfile="${tmp.jar}">
      <zipfileset src="${tmp.zip}" includes="jar.xml"/>
    </jar>
  </target>

  <target name="testRecreateZipfilesetWithUpdateNewerFile"
          depends="makezip">
    <jar destfile="${tmp.jar}"
         update="true">
      <zipfileset src="${tmp.zip}" includes="jar.xml"/>
    </jar>
  </target>

  <target name="testCreateWithEmptyFilesetSetUp">
    <mkdir dir="${tmp1.dir}"/>
    <mkdir dir="${tmp2.dir}"/>
    <echo file="${tmp2.dir}/foo.txt" message="foo"/>
  </target>

  <target name="testCreateWithEmptyFileset">
    <jar destfile="${tmp.jar}">
      <fileset dir="${tmp1.dir}">
        <include name="**/*.doesNotExist"/>
      </fileset>
      <fileset dir="${tmp2.dir}">
        <include name="**/foo.txt"/>
      </fileset>
    </jar>
  </target>

  <!-- bug 17780 -->
  <target name="testUpdateIfOnlyManifestHasChanged"
          depends="test4">
    <jar destfile="${tmp.jar}" update="true">
      <manifest>
        <attribute name="Foo" value="bar"/>
      </manifest>
    </jar>
    <mkdir dir="${tmp.dir}"/>
    <unzip src="${tmp.jar}" dest="${tmp.dir}"/>
  </target>

  <!-- bugs 10262 and 16972 -->
  <target name="testIndexTests">
    <mkdir dir="${tmp.dir}/META-INF"/>
    <touch file="${tmp.dir}/META-INF/INDEX.LIST"/>
    <touch file="${tmp.dir}/foo"/>
    <mkdir dir="${tmp.dir}/sub"/>
    <touch file="${tmp.dir}/sub/foo"/>
    <jar destfile="${tmp.jar}" index="yes" basedir="${tmp.dir}"/>
  </target>
    <!-- bug 32802 -->
  <target name="testManifestOnlyJar">
    <mkdir dir="${tmp.dir}"/>  
    <jar destfile="${tmp.jar}" duplicate="preserve">
      <manifest>
        <attribute name="Foo" value="bar"/>
      </manifest>
    </jar>
    <mkdir dir="${tmp.dir}"/>
    <unzip src="${tmp.jar}" dest="${tmp.dir}"/>

  </target>

  <!-- bug 37237 -->
  <target name="testIndexJarsPlusJarMarker">
    <mkdir dir="${tmp.dir}/a/b/c"/>
    <jar destfile="${tmp.jar}" basedir="${tmp.dir}"/>
    <delete dir="${tmp.dir}/a" quiet="true"/>
    <mkdir dir="${tmp.dir}/d/e/f"/>
    <jar destfile="${tmp.jar}2" basedir="${tmp.dir}" index="true">
      <indexjars>
        <fileset file="${tmp.jar}"/>
      </indexjars>
    </jar>
  </target>

  <target name="testIndexJarsPlusJarMarkerWithMapping">
    <mkdir dir="${tmp.dir}/a/b/c"/>
    <jar destfile="${tmp.jar}" basedir="${tmp.dir}"/>
    <delete dir="${tmp.dir}/a" quiet="true"/>
    <mkdir dir="${tmp.dir}/d/e/f"/>
    <jar destfile="${tmp.jar}2" basedir="${tmp.dir}" index="true">
      <indexjars>
        <fileset file="${tmp.jar}"/>
      </indexjars>
      <indexjarsmapper>
        <globmapper from="${output}/*" to="foo/*" handledirsep="true"/>
      </indexjarsmapper>
    </jar>
  </target>

  <target name="testNoVersionInfoNoStrict">
    <mkdir dir="${tmp.dir}"/>
    <jar destfile="${tmp.jar}" basedir="${tmp.dir}"/>
  </target>  

  <target name="testNoVersionInfoFail">
    <mkdir dir="${tmp.dir}"/>
    <jar destfile="${tmp.jar}" basedir="${tmp.dir}" strict="fail"/>
  </target>  

  <target name="testNoVersionInfoIgnore">
    <mkdir dir="${tmp.dir}"/>
    <jar destfile="${tmp.jar}" basedir="${tmp.dir}" strict="ignore"/>
  </target>  

  <target name="testNoVersionInfoWarn">
    <mkdir dir="${tmp.dir}"/>
    <jar destfile="${tmp.jar}" basedir="${tmp.dir}" strict="warn"/>
  </target>  
    
  <!-- see https://docs.oracle.com/javase/tutorial/deployment/jar/packageman.html -->  
  <target name="testHasVersionInfo">
    <mkdir dir="${tmp.dir}"/>
    <jar destfile="${tmp.jar}" basedir="${tmp.dir}" strict="fail">
      <manifest>
        <attribute name="Implementation-Title"   value="Packaging Version Test"/>
        <attribute name="Implementation-Version" value="1.0"/>
        <attribute name="Implementation-Vendor"  value="Apache Software Foundation"/>
      </manifest>
    </jar>
  </target>

  <target name="testZip64ModeJar" description="tests bz-66873">
    <mkdir dir="${tmp.dir}"/>
    <jar destfile="${tmp.dir}/zip64mode.jar" zip64mode="always" basedir="." includes="jar.xml"/>
  </target>
    
</project>
