<?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">
  <import file="../antunit-base.xml" />

  <target name="testIndexOnlyUpdate">
    <mkdir dir="${output}"/>
    <property name="index.jar" location="${output}/index.jar" />

    <jar destfile="${index.jar}" index="false">
      <fileset dir="${basedir}" includes="*-test.xml" />
    </jar>
    <au:assertTrue>
      <resourcecount count="0">
        <restrict>
          <zipentry zipfile="${index.jar}" name="META-INF/INDEX.LIST" />
          <exists />
        </restrict>
      </resourcecount>
    </au:assertTrue>
    <jar destfile="${index.jar}" index="true" update="true">
      <fileset dir="${basedir}" includes="*-test.xml" />
    </jar>
    <au:assertTrue>
      <resourcecount count="1">
        <restrict>
          <zipentry zipfile="${index.jar}" name="META-INF/INDEX.LIST" />
          <exists />
        </restrict>
      </resourcecount>
    </au:assertTrue>
  </target>

  <target name="testResourcesOnly"
          description="https://issues.apache.org/bugzilla/show_bug.cgi?id=47470">
    <mkdir dir="${input}"/>
    <mkdir dir="${output}"/>
    <touch file="${input}/foo"/>
    <touch file="${output}/bar"/>
    <jar destfile="${output}/foo.jar">
      <union>
        <fileset dir="${input}"/>
        <fileset dir="${output}" excludes="foo.jar"/>
      </union>
    </jar>
    <au:assertLogDoesntContain text="skipping jar archive"/>
  </target>

  <target name="-metainf-setup">
    <mkdir dir="${input}/services"/>
    <touch file="${input}/services/foo"/>
    <mkdir dir="${output}"/>
  </target>

  <target name="testIndexNoMetaInf" depends="-metainf-setup">
    <jar index="true" destfile="${output}/test.jar">
      <metainf dir="${input}"/>
    </jar>
    <unjar src="${output}/test.jar" dest="${output}"/>
    <au:assertFileExists file="${output}/META-INF/INDEX.LIST"/>
    <au:assertResourceDoesntContain value="META-INF"
                                    resource="${output}/META-INF/INDEX.LIST"/>
  </target>

  <target name="testIndexMetaInf" depends="-metainf-setup">
    <jar index="true" destfile="${output}/test.jar" indexMetaInf="true">
      <metainf dir="${input}"/>
    </jar>
    <unjar src="${output}/test.jar" dest="${output}"/>
    <au:assertFileExists file="${output}/META-INF/INDEX.LIST"/>
    <au:assertResourceContains value="META-INF"
                               resource="${output}/META-INF/INDEX.LIST"/>
  </target>

  <target name="testMergeWithoutMain"
          description="https://issues.apache.org/bugzilla/show_bug.cgi?id=29731">
    <mkdir dir="${input}"/>
    <mkdir dir="${output}"/>
    <jar destfile="${input}/first.jar">
      <manifest>
        <attribute name="First" value="Main Section"/>
        <section name="Nested">
          <attribute name="First" value="Nested Section"/>
        </section>
      </manifest>
    </jar>
    <jar destfile="${output}/second.jar" filesetmanifest="mergewithoutmain">
      <manifest>
        <attribute name="Second" value="Main Section"/>
        <section name="Nested">
          <attribute name="Second" value="Nested Section"/>
        </section>
      </manifest>
      <zipgroupfileset dir="${input}" includes="first.jar"/>
    </jar>
    <unjar src="${output}/second.jar" dest="${output}"/>
    <au:assertFileExists file="${output}/META-INF/MANIFEST.MF"/>
    <au:assertResourceContains value="First: Nested Section"
                               resource="${output}/META-INF/MANIFEST.MF"/>
    <au:assertResourceContains value="Second: Nested Section"
                               resource="${output}/META-INF/MANIFEST.MF"/>
    <au:assertResourceDoesntContain
       value="First: Main Section"
       resource="${output}/META-INF/MANIFEST.MF"/>
    <au:assertResourceContains value="Second: Main Section"
                               resource="${output}/META-INF/MANIFEST.MF"/>
  </target>

  <target name="testWhenManifestOnly"
          description="https://issues.apache.org/bugzilla/show_bug.cgi?id=43946">
    <mkdir dir="${input}"/>
    <mkdir dir="${output}"/>
    <jar destfile="${output}/first.jar">
      <fileset dir="${input}"/>
    </jar>
    <au:assertFileExists file="${output}/first.jar"/>
    <au:assertLogDoesntContain text="skipping jar archive"/>
    <jar destfile="${output}/second.jar" whenmanifestonly="create">
      <fileset dir="${input}"/>
    </jar>
    <au:assertFileExists file="${output}/second.jar"/>
    <au:assertLogDoesntContain text="skipping jar archive"/>
    <jar destfile="${output}/third.jar" whenmanifestonly="skip">
      <fileset dir="${input}"/>
    </jar>
    <au:assertFileDoesntExist file="${output}/third.jar"/>
    <au:assertLogContains text="skipping jar archive"/>
    <au:expectfailure>
      <jar destfile="${output}/forth.jar" whenmanifestonly="fail">
        <fileset dir="${input}"/>
      </jar>
    </au:expectfailure>
  </target>

  <target name="test-update-and-filesetmanifest"
          description="https://issues.apache.org/bugzilla/show_bug.cgi?id=30751">
    <mkdir dir="${input}"/>
    <mkdir dir="${output}"/>
    <jar destfile="${input}/manifest.jar">
      <manifest>
        <attribute name="Origin" value="manifest.jar"/>
      </manifest>
    </jar>

    <touch file="${input}/foo"/>
    <jar destfile="${output}/test.jar">
      <fileset dir="${input}" excludes="*.jar"/>
      <manifest>
        <attribute name="Second-Origin" value="test.jar"/>
      </manifest>
    </jar>

    <touch file="${input}/bar"/>
    <jar destfile="${output}/test.jar" update="true" filesetmanifest="merge">
      <fileset dir="${input}" excludes="*.jar"/>
      <zipgroupfileset dir="${input}" includes="*.jar"/>
    </jar>

    <unjar src="${output}/test.jar" dest="${output}"/>
    <au:assertFileExists file="${output}/foo"/>
    <au:assertFileExists file="${output}/bar"/>
    <au:assertResourceContains value="Origin: manifest.jar"
                               resource="${output}/META-INF/MANIFEST.MF"/>
    <au:assertResourceContains value="Second-Origin: test.jar"
                               resource="${output}/META-INF/MANIFEST.MF"/>
  </target>
</project>
