<!--
 * @author    Matt Albrecht <a href="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
 * @version   $Date: 2004/07/08 05:29:20 $
 * @since     March 23, 2004
-->
<project name="GroboReZipTask" default="none">
    <!-- ================================================================= -->
    <!-- some general defines -->
    <property name="root" location="root" />
    <property name="out" location="out" />

    <target name="none" />
    <taskdef resource="ant-grobocoverage.properties" />
    
    <!-- ================================================================= -->
    <!-- start of the tests that pass -->
    
    
    <target name="rezip-1" depends="test-setup-1">
        <echo file="rezip-1" message="test" />
        <grobo-rezip>
            <alterzip src="1.zip" dest="b.zip">
                <fileset dir="${root}" prefix="123" />
            </alterzip>
        </grobo-rezip>
        
        
        <unzip src="b.zip" dest="${out}" />
        
        <available file="${out}/1" type="file"
            property="!did-create-1" />
        <fail unless="!did-create-1">
Didn't archive "1"
        </fail>
        
        <available file="${out}/123/1" type="file"
            property="!did-create-2" />
        <fail unless="!did-create-2">
Didn't archive "123/1"
        </fail>
    </target>
    
    
    <target name="rezip-2" depends="test-setup-2">
        <echo file="rezip-2" message="test" />
        <grobo-rezip>
            <alterzip src="2.zip" dest="b.zip">
                <fileset dir="${root}" prefix="123" />
                <alterjar src="2-zips/1.zip">
                    <fileset dir="${root}" prefix="abc" />
                </alterjar>
            </alterzip>
        </grobo-rezip>
        
        
        <unzip src="b.zip" dest="${out}" />
        
        <available file="${out}/1" type="file"
            property="!did-create-1" />
        <fail unless="!did-create-1">
Didn't archive "1"
        </fail>
        
        <available file="${out}/123/1" type="file"
            property="!did-create-2" />
        <fail unless="!did-create-2">
Didn't archive "123/1"
        </fail>
        
        <available file="${out}/2.1" type="file"
            property="!did-create-3" />
        <fail unless="!did-create-3">
Didn't archive "2.1"
        </fail>
        
        <available file="${out}/2.2" type="file"
            property="!did-create-4" />
        <fail unless="!did-create-4">
Didn't archive "2.2"
        </fail>
        
        <available file="${out}/2-zips/1.zip" type="file"
            property="!did-create-5" />
        <fail unless="!did-create-5">
Didn't archive "2-zips/1.zip"
        </fail>
        
        <mkdir dir="${out}/x" />
        <unzip src="${out}/2-zips/1.zip" dest="${out}/x" />
        
        
        <available file="${out}/x/1" type="file"
            property="!did-create-6" />
        <fail unless="!did-create-6">
Didn't archive "1.zip!1"
        </fail>
        
        <available file="${out}/x/abc/1" type="file"
            property="!did-create-7" />
        <fail unless="!did-create-7">
Didn't archive "1.zip!abc/1"
        </fail>
        
        <available file="${out}/x/abc/2.1" type="file"
            property="!did-create-8" />
        <fail unless="!did-create-8">
Didn't archive "1.zip!abc/2.1"
        </fail>
        
        <available file="${out}/x/abc/2.2" type="file"
            property="!did-create-9" />
        <fail unless="!did-create-9">
Didn't archive "1.zip!abc/2.2"
        </fail>
        
    </target>

    
    
    
    <target name="rezip-3" depends="test-setup-2">
        <echo file="rezip-3" message="test" />
        <echo file="${root}/3.1">3.1</echo>
        <echo file="${root}/3.2">3.2</echo>
        <zip compress="false" destfile="3.zip">
            <fileset file="2.zip" />
            <zipfileset prefix="WEB-INF/classes" file="${root}/3.2" />
        </zip>
        <grobo-rezip>
            <alterwar src="3.zip" dest="b.zip">
                <fileset dir="${root}" prefix="123" />
                <alterear src="2.zip">
                    <zipfileset dir="${root}" prefix="abc" />
                    
                    <alterzip src="2-zips/1.zip">
                    </alterzip>
                </alterear>
                
                <lib dir="${root}" includes="3.1" />
                <classes dir="${root}" includes="3.2" />
            </alterwar>
        </grobo-rezip>
        
        
        <mkdir dir="${out}/1" />
        <unzip src="b.zip" dest="${out}/1" />
        
        <available file="${out}/1/2.zip" type="file"
            property="!did-create-1" />
        <fail unless="!did-create-1">
Didn't keep "2.zip"
        </fail>
        
        <available file="${out}/1/123/1" type="file"
            property="!did-create-2" />
        <fail unless="!did-create-2">
Didn't archive "123/1"
        </fail>
        
        <available file="${out}/1/WEB-INF/lib/3.1" type="file"
            property="!did-create-3" />
        <fail unless="!did-create-3">
Didn't archive library "3.1"
        </fail>
        
        <available file="${out}/1/WEB-INF/classes/3.2" type="file"
            property="!did-create-4" />
        <fail unless="!did-create-4">
Didn't archive class "3.2"
        </fail>
            
        <mkdir dir="${out}/2" />
        <unzip src="${out}/1/2.zip" dest="${out}/2" />
        
        <available file="${out}/2/1" type="file"
            property="!did-create-5" />
        <fail unless="!did-create-5">
Didn't archive "1"
        </fail>
        
        <available file="${out}/2/2.1" type="file"
            property="!did-create-6" />
        <fail unless="!did-create-6">
Didn't archive "2.1"
        </fail>
        
        <available file="${out}/2/2.2" type="file"
            property="!did-create-7" />
        <fail unless="!did-create-7">
Didn't archive "2.2"
        </fail>
        
        <available file="${out}/2/2-zips/1.zip" type="file"
            property="!did-create-8" />
        <fail unless="!did-create-8">
Didn't archive "2-zips/1.zip"
        </fail>
        
        <mkdir dir="${out}/3" />
        <unzip src="${out}/2/2-zips/1.zip" dest="${out}/3" />
        
        
        <available file="${out}/3/1" type="file"
            property="!did-create-9" />
        <fail unless="!did-create-9">
Didn't archive "1.zip!1"
        </fail>
    </target>

    
    
    
    <target name="rezip-4" depends="test-setup:dirs">
        <echo file="rezip-4" message="test" />
        
        <echo file="${root}/1" message="new" />
        <echo file="${root}/2" message="lib" />
        <mkdir dir="${root}/x" />
        <mkdir dir="${root}/x/y" />
        
        <mkdir dir="${root}/WEB-INF/classes" />
        <echo file="${root}/WEB-INF/classes/1" message="hello" />
        <mkdir dir="${root}/WEB-INF/lib" />
        <echo file="${root}/WEB-INF/lib/2" message="old" />
        
        <zip compress="true" destfile="1.war">
            <fileset dir="${root}" includes="WEB-INF/*" />
        </zip>
        <grobo-rezip>
            <alterwar src="1.war" dest="c.war">
                <classes dir="${root}" includes="1" replaceOnly="false" />
                <lib file="${root}/2" />
                
                <!-- ensure we don't add directories that aren't in
                the original if replaceOnly="true" -->
                <fileset dir="${root}/x" replaceOnly="true" />
            </alterwar>
        </grobo-rezip>
        
        
        <mkdir dir="${out}/1" />
        <unzip src="c.war" dest="${out}/1" />
        
        <available file="${out}/1/WEB-INF/classes/1" type="file"
            property="!did-create-1" />
        <fail unless="!did-create-1">
Didn't keep "1"
        </fail>
        <loadfile property="!contents-1" srcFile="${out}/1/WEB-INF/classes/1" />
        <condition property="!contents-1-correct">
            <equals arg1="new" arg2="${!contents-1}" />
        </condition>
        <fail unless="!contents-1-correct">
Didn't replace "1" (contents are [${!contents-1}])
        </fail>
        
        <available file="${out}/1/WEB-INF/lib/2" type="file"
            property="!did-create-2" />
        <fail unless="!did-create-2">
Didn't keep "2"
        </fail>
        <loadfile property="!contents-2" srcFile="${out}/1/WEB-INF/lib/2" />
        <condition property="!contents-2-correct">
            <equals arg1="lib" arg2="${!contents-2}" />
        </condition>
        <fail unless="!contents-2-correct">
Didn't replace "2" (contents are [${!contents-2}])
        </fail>
        
        <available file="${out}/1/y" type="dir"
            property="!did-create-3" />
        <fail if="!did-create-3">
Incorectly added dir "y"
        </fail>
    </target>

    
    
    
    <target name="rezip-5" depends="test-setup:dirs">
        <echo file="rezip-5" message="test" />
        
        <mkdir dir="${root}/a" />
        <mkdir dir="${root}/b" />
        <mkdir dir="${root}/a/x" />
        <mkdir dir="${root}/b/x" />
        <mkdir dir="${root}/a/y" />
        <mkdir dir="${root}/b/z" />
        <echo file="${root}/a/1" message="a1" />
        <echo file="${root}/a/2" message="a2" />
        <echo file="${root}/a/3" message="a3" />
        <echo file="${root}/a/4" message="a4" />
        <echo file="${root}/a/x/5" message="a5" />
        <echo file="${root}/a/x/6" message="a6" />
        <echo file="${root}/a/x/7" message="a7" />
        <echo file="${root}/a/y/8" message="a8" />
        

        <echo file="${root}/b/1" message="b1" />
        <echo file="${root}/b/2" message="b2" />
        <echo file="${root}/b/11" message="b11" /> <!-- not put in zip -->
        <echo file="${root}/b/x/5" message="b5" />
        <echo file="${root}/b/x/6" message="b6" />
        <echo file="${root}/b/x/9" message="b9" /> <!-- not put in zip -->
        <echo file="${root}/b/z/10" message="b10" /> <!-- not put in zip -->
        
        <zip compress="true" destfile="1.ear">
            <fileset dir="${root}/a" />
        </zip>
        <copy file="1.ear" tofile="2.ear" />
        <grobo-rezip>
            <alterear src="2.ear">
                <fileset dir="${root}/b" replaceonly="true" />
            </alterear>
        </grobo-rezip>
        
        
        <unzip src="2.ear" dest="${out}" />
        
        <available file="${out}/1" type="file"
            property="!did-create-1" />
        <fail unless="!did-create-1">
Didn't keep "1"
        </fail>
        <loadfile property="!contents-1" srcFile="${out}/1" />
        <condition property="!contents-1-correct">
            <equals arg1="b1" arg2="${!contents-1}" />
        </condition>
<!-- fix this eventually, but move on...
        <fail unless="!contents-1-correct">
Didn't replace "1" (contents are [${!contents-1}])
        </fail>
-->
        
        <available file="${out}/2" type="file"
            property="!did-create-2" />
        <fail unless="!did-create-2">
Didn't keep "2"
        </fail>
        <loadfile property="!contents-2" srcFile="${out}/2" />
        <condition property="!contents-2-correct">
            <equals arg1="b2" arg2="${!contents-2}" />
        </condition>
        <fail unless="!contents-2-correct">
Didn't replace "2" (contents are [${!contents-2}])
        </fail>
        
        <available file="${out}/3" type="file"
            property="!did-create-3" />
        <fail unless="!did-create-3">
Didn't keep "3"
        </fail>
        <loadfile property="!contents-3" srcFile="${out}/3" />
        <condition property="!contents-3-correct">
            <equals arg1="a3" arg2="${!contents-3}" />
        </condition>
        <fail unless="!contents-3-correct">
Replaced "3" (contents are [${!contents-3}])
        </fail>
        
        <available file="${out}/4" type="file"
            property="!did-create-4" />
        <fail unless="!did-create-4">
Didn't keep "4"
        </fail>
        <loadfile property="!contents-4" srcFile="${out}/4" />
        <condition property="!contents-4-correct">
            <equals arg1="a4" arg2="${!contents-4}" />
        </condition>
        <fail unless="!contents-4-correct">
Replaced "4" (contents are [${!contents-4}])
        </fail>
        
        <available file="${out}/x/5" type="file"
            property="!did-create-5" />
        <fail unless="!did-create-5">
Didn't keep "5"
        </fail>
        <loadfile property="!contents-5" srcFile="${out}/x/5" />
        <condition property="!contents-5-correct">
            <equals arg1="b5" arg2="${!contents-5}" />
        </condition>
        <fail unless="!contents-5-correct">
Didn't replace "5" (contents are [${!contents-5}])
        </fail>
        
        <available file="${out}/x/6" type="file"
            property="!did-create-6" />
        <fail unless="!did-create-6">
Didn't keep "1"
        </fail>
        <loadfile property="!contents-6" srcFile="${out}/x/6" />
        <condition property="!contents-6-correct">
            <equals arg1="b6" arg2="${!contents-6}" />
        </condition>
        <fail unless="!contents-6-correct">
Didn't replace "6" (contents are [${!contents-6}])
        </fail>
        
        <available file="${out}/x/7" type="file"
            property="!did-create-7" />
        <fail unless="!did-create-7">
Didn't keep "7"
        </fail>
        <loadfile property="!contents-7" srcFile="${out}/x/7" />
        <condition property="!contents-7-correct">
            <equals arg1="a7" arg2="${!contents-7}" />
        </condition>
        <fail unless="!contents-7-correct">
Replaced "7" (contents are [${!contents-7}])
        </fail>
        
        <available file="${out}/y/8" type="file"
            property="!did-create-8" />
        <fail unless="!did-create-8">
Didn't keep "8"
        </fail>
        <loadfile property="!contents-8" srcFile="${out}/y/8" />
        <condition property="!contents-8-correct">
            <equals arg1="a8" arg2="${!contents-8}" />
        </condition>
        <fail unless="!contents-1-correct">
Replaced "8" (contents are [${!contents-8}])
        </fail>
        
        <available file="${out}/x/9" type="file"
            property="!did-create-9" />
        <fail if="!did-create-9">
Incorrectly added "9"
        </fail>
        
        <available file="${out}/z/10" type="file"
            property="!did-create-10" />
        <fail if="!did-create-10">
Incorrectly added "10"
        </fail>
        
        <available file="${out}/11" type="file"
            property="!did-create-11" />
        <fail if="!did-create-11">
Incorrectly added "11"
        </fail>
    </target>
    
    
    <!-- ================================================================= -->
    <!-- start of the tests that fail -->
    
    
    
    
    
    
    <!-- ================================================================= -->
    <!-- sets up the tests -->
    <target name="test-setup-1" depends="test-setup:dirs">
        <echo file="${root}/1">
asdflaksjdlkjsdaf
        </echo>
        <zip basedir="${root}" destfile="1.zip" />
    </target>
    
    <target name="test-setup-2" depends="test-setup-1">
        <echo file="${root}/2.1">
1
2
3
4
        </echo>
        <echo file="${root}/2.2">
4
3
2
1
        </echo>
        <zip basedir="${root}" destfile="2.zip">
            <zipfileset file="1.zip" prefix="2-zips" />
        </zip>
    </target>
    
    <target name="test-setup:dirs">
        <mkdir dir="${root}" />
        <mkdir dir="${out}" />
    </target>
    
    <target name="test-teardown">
        <!--
        <delete dir="${root}" />
        -->
    </target>
</project>