You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gr...@apache.org on 2009/04/17 07:38:38 UTC

svn commit: r765850 - in /commons/proper/compress/trunk/src: main/java/org/apache/commons/compress/changes/ChangeSet.java test/java/org/apache/commons/compress/changes/ChangeSetTestCase.java

Author: grobmeier
Date: Fri Apr 17 05:38:38 2009
New Revision: 765850

URL: http://svn.apache.org/viewvc?rev=765850&view=rev
Log:
prevent adding an ArchiveEntry with same name twice to an ChangeSet

Modified:
    commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/changes/ChangeSet.java
    commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/changes/ChangeSetTestCase.java

Modified: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/changes/ChangeSet.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/changes/ChangeSet.java?rev=765850&r1=765849&r2=765850&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/changes/ChangeSet.java (original)
+++ commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/changes/ChangeSet.java Fri Apr 17 05:38:38 2009
@@ -65,10 +65,39 @@
      *            the datastream to add
      */
     public void add(final ArchiveEntry pEntry, final InputStream pInput) {
-        changes.add(new Change(pEntry, pInput));
+        addAddition(new Change(pEntry, pInput));
     }
 
     /**
+     * Adds an addition change.
+     * 
+     * @param pChange
+     *            the change which should result in an addition
+     */
+    private void addAddition(Change pChange) {
+        if (Change.TYPE_ADD != pChange.type() ||    
+            pChange.getInput() == null) {
+            return;
+        }
+
+        if (!changes.isEmpty()) {
+            for (Iterator it = changes.iterator(); it.hasNext();) {
+                Change change = (Change) it.next();
+                if (change.type() == Change.TYPE_ADD
+                        && change.getEntry() != null) {
+                    ArchiveEntry entry = change.getEntry();
+
+                    if(entry.equals(pChange.getEntry())) {
+                        it.remove();
+                        break;
+                    }
+                }
+            }
+        }
+        changes.add(pChange);
+    }
+    
+    /**
      * Adds an delete change.
      * 
      * @param pChange

Modified: commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/changes/ChangeSetTestCase.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/changes/ChangeSetTestCase.java?rev=765850&r1=765849&r2=765850&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/changes/ChangeSetTestCase.java (original)
+++ commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/changes/ChangeSetTestCase.java Fri Apr 17 05:38:38 2009
@@ -67,6 +67,38 @@
     }
 
     /**
+     * Adds an ArchiveEntry with the same name two times.
+     * Only the latest addition should be found in the ChangeSet,
+     * the first add should be replaced.
+     * 
+     * @throws Exception
+     */
+    public void testAddChangeTwice() throws Exception {
+        InputStream in = null;
+        InputStream in2 = null;
+        try {
+            in = new FileInputStream(getFile("test.txt"));
+            in2 = new FileInputStream(getFile("test2.xml"));
+       
+            ArchiveEntry e = new ZipArchiveEntry("test.txt");
+            ArchiveEntry e2 = new ZipArchiveEntry("test.txt");
+            
+            ChangeSet changes = new ChangeSet();
+            changes.add(e, in);
+            changes.add(e2, in2);
+            
+            assertEquals(1, changes.getChanges().size());
+            Change c = (Change)changes.getChanges().iterator().next();
+            assertEquals(in2, c.getInput());
+        } finally {
+            if (in != null)
+                in.close();
+            if (in2 != null)
+                in2.close();
+        }
+    }
+    
+    /**
      * Tries to delete the folder "bla" from an archive file. This should result in
      * the deletion of bla/*, which actually means bla/test4.xml should be
      * removed from the archive. The file something/bla (without ending, named