ant-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bode...@apache.org
Subject svn commit: r807875 - in /ant/sandbox/antlibs/compress/trunk/src: main/org/apache/ant/compress/taskdefs/ArchiveBase.java tests/antunit/ar-test.xml tests/antunit/cpio-test.xml tests/antunit/tar-test.xml tests/antunit/zip-test.xml
Date Wed, 26 Aug 2009 04:30:14 GMT
Author: bodewig
Date: Wed Aug 26 04:30:13 2009
New Revision: 807875

URL: http://svn.apache.org/viewvc?rev=807875&view=rev
Log:
port duplicate attribute from <zip>

Modified:
    ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/compress/taskdefs/ArchiveBase.java
    ant/sandbox/antlibs/compress/trunk/src/tests/antunit/ar-test.xml
    ant/sandbox/antlibs/compress/trunk/src/tests/antunit/cpio-test.xml
    ant/sandbox/antlibs/compress/trunk/src/tests/antunit/tar-test.xml
    ant/sandbox/antlibs/compress/trunk/src/tests/antunit/zip-test.xml

Modified: ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/compress/taskdefs/ArchiveBase.java
URL: http://svn.apache.org/viewvc/ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/compress/taskdefs/ArchiveBase.java?rev=807875&r1=807874&r2=807875&view=diff
==============================================================================
--- ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/compress/taskdefs/ArchiveBase.java
(original)
+++ ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/compress/taskdefs/ArchiveBase.java
Wed Aug 26 04:30:13 2009
@@ -76,6 +76,7 @@
     private boolean preserve0permissions = false;
     private boolean roundUp = true;
     private boolean preserveLeadingSlashes = false;
+    private Duplicate duplicate = new Duplicate();
 
     protected ArchiveBase() {}
 
@@ -171,6 +172,19 @@
         this.preserveLeadingSlashes = b;
     }
 
+    /**
+     * Sets behavior for when a duplicate file is about to be added -
+     * one of <code>add</code>, <code>preserve</code> or <code>fail</code>.
+     * Possible values are: <code>add</code> (keep both
+     * of the files); <code>preserve</code> (keep the first version
+     * of the file found); <code>fail</code> halt a problem
+     * Default for is <code>add</code>
+     * @param df a <code>Duplicate</code> enumerated value
+     */
+    public void setDuplicate(Duplicate df) {
+        duplicate = df;
+    }
+
     public void execute() {
         validate();
         if (!dest.isExists()) {
@@ -219,7 +233,8 @@
      * the archive.
      */
     protected ResourceWithFlags[] findSources() throws IOException {
-        ArrayList l = new ArrayList();
+        List/*<ResourceWithFlags>*/ l = new ArrayList/*<ResourceWithFlags>*/();
+        Set/*<String>*/ addedNames = new HashSet/*<String>*/();
         for (Iterator rcs = sources.iterator(); rcs.hasNext(); ) {
             ResourceCollection rc = (ResourceCollection) rcs.next();
             ResourceCollectionFlags rcFlags = getFlags(rc);
@@ -228,9 +243,12 @@
                 if (!filesOnly || !r.isDirectory()) {
                     ResourceWithFlags rwf =
                         new ResourceWithFlags(r, rcFlags, getFlags(r));
-                    if (!"".equals(rwf.getName())
-                        && !"/".equals(rwf.getName())) {
-                        l.add(rwf);
+                    String name = rwf.getName();
+                    if (!"".equals(name) && !"/".equals(name)) {
+                        boolean isDup = !addedNames.add(name);
+                        if (!isDup || addDuplicate(name)) {
+                            l.add(rwf);
+                        }
                     }
                 }
             }
@@ -509,6 +527,24 @@
     }
 
     /**
+     * Is invoked if a duplicate entry is found, decides whether the
+     * entry shall be added regardless.
+     */
+    protected boolean addDuplicate(String name) {
+        if (duplicate.getValue().equals(Duplicate.PRESERVE)) {
+            log(name + " already added, skipping.", Project.MSG_INFO);
+            return false;
+        } else if (duplicate.getValue().equals(Duplicate.FAIL)) {
+            throw new BuildException("Duplicate entry " + name
+                                     + " was found and the duplicate "
+                                     + "attribute is 'fail'.");
+        }
+        // duplicate equal to add, so we continue
+        log("duplicate entry " + name + " found, adding.", Project.MSG_VERBOSE);
+        return true;
+    }
+
+    /**
      * Valid Modes for create/update/replace.
      */
     public static final class Mode extends EnumeratedAttribute {
@@ -539,6 +575,28 @@
     }
 
     /**
+     * Possible behaviors when a duplicate file is added:
+     * "add", "preserve" or "fail"
+     */
+    public static class Duplicate extends EnumeratedAttribute {
+        private static String ADD = "add";
+        private static String PRESERVE = "preserve";
+        private static String FAIL = "fail";
+
+        public Duplicate() {
+            setValue(ADD);
+        }
+
+        /**
+         * @see EnumeratedAttribute#getValues()
+         */
+        /** {@inheritDoc} */
+        public String[] getValues() {
+            return new String[] {ADD, PRESERVE, FAIL};
+        }
+    }
+
+    /**
      * Various flags a (archive) resource may hold in addition to
      * being a plain resource.
      */

Modified: ant/sandbox/antlibs/compress/trunk/src/tests/antunit/ar-test.xml
URL: http://svn.apache.org/viewvc/ant/sandbox/antlibs/compress/trunk/src/tests/antunit/ar-test.xml?rev=807875&r1=807874&r2=807875&view=diff
==============================================================================
--- ant/sandbox/antlibs/compress/trunk/src/tests/antunit/ar-test.xml (original)
+++ ant/sandbox/antlibs/compress/trunk/src/tests/antunit/ar-test.xml Wed Aug 26 04:30:13 2009
@@ -326,4 +326,31 @@
     </assertResourceDoesntExist>
   </target>
 
+  <target name="testAddDuplicates" depends="setUp">
+    <cmp:ar destfile="${dest}">
+      <cmp:arfileset src="../resources/asf-logo.gif.ar"/>
+      <cmp:zipfileset src="../resources/asf-logo.gif.zip"/>
+    </cmp:ar>
+    <au:assertLogContains text="duplicate entry asf-logo.gif found, adding."
+                          level="verbose"/>
+  </target>
+
+  <target name="testPreserveDuplicates" depends="setUp">
+    <cmp:ar destfile="${dest}" duplicate="preserve">
+      <cmp:arfileset src="../resources/asf-logo.gif.ar"/>
+      <cmp:zipfileset src="../resources/asf-logo.gif.zip"/>
+    </cmp:ar>
+    <au:assertLogContains text="asf-logo.gif already added, skipping."
+                          level="verbose"/>
+  </target>
+
+  <target name="testFailDuplicates" depends="setUp">
+    <au:expectfailure
+       message="Duplicate entry asf-logo.gif was found and the duplicate attribute is 'fail'.">
+      <cmp:ar destfile="${dest}" duplicate="fail">
+        <cmp:arfileset src="../resources/asf-logo.gif.ar"/>
+        <cmp:zipfileset src="../resources/asf-logo.gif.zip"/>
+      </cmp:ar>
+    </au:expectfailure>
+  </target>
 </project>

Modified: ant/sandbox/antlibs/compress/trunk/src/tests/antunit/cpio-test.xml
URL: http://svn.apache.org/viewvc/ant/sandbox/antlibs/compress/trunk/src/tests/antunit/cpio-test.xml?rev=807875&r1=807874&r2=807875&view=diff
==============================================================================
--- ant/sandbox/antlibs/compress/trunk/src/tests/antunit/cpio-test.xml (original)
+++ ant/sandbox/antlibs/compress/trunk/src/tests/antunit/cpio-test.xml Wed Aug 26 04:30:13
2009
@@ -357,4 +357,33 @@
       </cmp:cpioentry>
     </assertResourceDoesntExist>
   </target>
+
+  <!-- ACC 1.0 CPIO prevents duplicate entries -->
+  <target name="fail-testAddDuplicates" depends="setUp">
+    <cmp:cpio destfile="${dest}">
+      <cmp:arfileset src="../resources/asf-logo.gif.ar"/>
+      <cmp:zipfileset src="../resources/asf-logo.gif.zip"/>
+    </cmp:cpio>
+    <au:assertLogContains text="duplicate entry asf-logo.gif found, adding."
+                          level="verbose"/>
+  </target>
+
+  <target name="testPreserveDuplicates" depends="setUp">
+    <cmp:cpio destfile="${dest}" duplicate="preserve">
+      <cmp:arfileset src="../resources/asf-logo.gif.ar"/>
+      <cmp:zipfileset src="../resources/asf-logo.gif.zip"/>
+    </cmp:cpio>
+    <au:assertLogContains text="asf-logo.gif already added, skipping."
+                          level="verbose"/>
+  </target>
+
+  <target name="testFailDuplicates" depends="setUp">
+    <au:expectfailure
+       message="Duplicate entry asf-logo.gif was found and the duplicate attribute is 'fail'.">
+      <cmp:cpio destfile="${dest}" duplicate="fail">
+        <cmp:arfileset src="../resources/asf-logo.gif.ar"/>
+        <cmp:zipfileset src="../resources/asf-logo.gif.zip"/>
+      </cmp:cpio>
+    </au:expectfailure>
+  </target>
 </project>

Modified: ant/sandbox/antlibs/compress/trunk/src/tests/antunit/tar-test.xml
URL: http://svn.apache.org/viewvc/ant/sandbox/antlibs/compress/trunk/src/tests/antunit/tar-test.xml?rev=807875&r1=807874&r2=807875&view=diff
==============================================================================
--- ant/sandbox/antlibs/compress/trunk/src/tests/antunit/tar-test.xml (original)
+++ ant/sandbox/antlibs/compress/trunk/src/tests/antunit/tar-test.xml Wed Aug 26 04:30:13
2009
@@ -375,4 +375,32 @@
       </cmp:tarentry>
     </assertResourceDoesntExist>
   </target>
+
+  <target name="testAddDuplicates" depends="setUp">
+    <cmp:tar destfile="${dest}">
+      <cmp:arfileset src="../resources/asf-logo.gif.ar"/>
+      <cmp:zipfileset src="../resources/asf-logo.gif.zip"/>
+    </cmp:tar>
+    <au:assertLogContains text="duplicate entry asf-logo.gif found, adding."
+                          level="verbose"/>
+  </target>
+
+  <target name="testPreserveDuplicates" depends="setUp">
+    <cmp:tar destfile="${dest}" duplicate="preserve">
+      <cmp:arfileset src="../resources/asf-logo.gif.ar"/>
+      <cmp:zipfileset src="../resources/asf-logo.gif.zip"/>
+    </cmp:tar>
+    <au:assertLogContains text="asf-logo.gif already added, skipping."
+                          level="verbose"/>
+  </target>
+
+  <target name="testFailDuplicates" depends="setUp">
+    <au:expectfailure
+       message="Duplicate entry asf-logo.gif was found and the duplicate attribute is 'fail'.">
+      <cmp:tar destfile="${dest}" duplicate="fail">
+        <cmp:arfileset src="../resources/asf-logo.gif.ar"/>
+        <cmp:zipfileset src="../resources/asf-logo.gif.zip"/>
+      </cmp:tar>
+    </au:expectfailure>
+  </target>
 </project>

Modified: ant/sandbox/antlibs/compress/trunk/src/tests/antunit/zip-test.xml
URL: http://svn.apache.org/viewvc/ant/sandbox/antlibs/compress/trunk/src/tests/antunit/zip-test.xml?rev=807875&r1=807874&r2=807875&view=diff
==============================================================================
--- ant/sandbox/antlibs/compress/trunk/src/tests/antunit/zip-test.xml (original)
+++ ant/sandbox/antlibs/compress/trunk/src/tests/antunit/zip-test.xml Wed Aug 26 04:30:13
2009
@@ -384,4 +384,31 @@
          with command line zip -->
   </target>
 
+  <target name="testAddDuplicates" depends="setUp">
+    <cmp:zip destfile="${dest}">
+      <cmp:arfileset src="../resources/asf-logo.gif.ar"/>
+      <cmp:zipfileset src="../resources/asf-logo.gif.zip"/>
+    </cmp:zip>
+    <au:assertLogContains text="duplicate entry asf-logo.gif found, adding."
+                          level="verbose"/>
+  </target>
+
+  <target name="testPreserveDuplicates" depends="setUp">
+    <cmp:zip destfile="${dest}" duplicate="preserve">
+      <cmp:arfileset src="../resources/asf-logo.gif.ar"/>
+      <cmp:zipfileset src="../resources/asf-logo.gif.zip"/>
+    </cmp:zip>
+    <au:assertLogContains text="asf-logo.gif already added, skipping."
+                          level="verbose"/>
+  </target>
+
+  <target name="testFailDuplicates" depends="setUp">
+    <au:expectfailure
+       message="Duplicate entry asf-logo.gif was found and the duplicate attribute is 'fail'.">
+      <cmp:zip destfile="${dest}" duplicate="fail">
+        <cmp:arfileset src="../resources/asf-logo.gif.ar"/>
+        <cmp:zipfileset src="../resources/asf-logo.gif.zip"/>
+      </cmp:zip>
+    </au:expectfailure>
+  </target>
 </project>



Mime
View raw message