commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From grobme...@apache.org
Subject svn commit: r766455 - in /commons/proper/compress/trunk/src: main/java/org/apache/commons/compress/changes/ test/java/org/apache/commons/compress/changes/
Date Sun, 19 Apr 2009 12:08:30 GMT
Author: grobmeier
Date: Sun Apr 19 12:08:29 2009
New Revision: 766455

URL: http://svn.apache.org/viewvc?rev=766455&view=rev
Log:
added replace feature for Add-Changes

Modified:
    commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/changes/Change.java
    commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/changes/ChangeSet.java
    commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/changes/ChangeSetPerformer.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/Change.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/changes/Change.java?rev=766455&r1=766454&r2=766455&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/changes/Change.java
(original)
+++ commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/changes/Change.java
Sun Apr 19 12:08:29 2009
@@ -31,6 +31,7 @@
 	private final String targetFile; // entry name to delete
 	private final ArchiveEntry entry; // new entry to add
 	private final InputStream input; // source for new entry
+	private final boolean replaceMode; // change should replaceMode existing entries
 	
 	// Type of change
 	private final int type;
@@ -53,6 +54,7 @@
 		this.type = type;
 		this.input = null;
 		this.entry = null;
+		this.replaceMode = true;
 	}
 		
 	/**
@@ -61,7 +63,7 @@
 	 * @param pEntry the entry details
 	 * @param pInput the InputStream for the entry data
 	 */
-	Change(final ArchiveEntry pEntry, final InputStream pInput) {
+	Change(final ArchiveEntry pEntry, final InputStream pInput, boolean replace) {
 		if(pEntry == null || pInput == null) {
 			throw new NullPointerException();
 		}
@@ -69,6 +71,7 @@
 		this.input = pInput;
 		type = TYPE_ADD;
 		targetFile = null;
+		this.replaceMode = replace;
 	}
 	
 	ArchiveEntry getEntry() {
@@ -86,4 +89,8 @@
 	int type() {
 		return type;
 	}
+
+    boolean isReplaceMode() {
+        return replaceMode;
+    }
 }

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=766455&r1=766454&r2=766455&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
Sun Apr 19 12:08:29 2009
@@ -65,7 +65,21 @@
      *            the datastream to add
      */
     public void add(final ArchiveEntry pEntry, final InputStream pInput) {
-        addAddition(new Change(pEntry, pInput));
+        this.add(pEntry, pInput, true);
+    }
+    
+    /**
+     * Adds a new archive entry to the archive.
+     * 
+     * @param pEntry
+     *            the entry to add
+     * @param pInput
+     *            the datastream to add
+     * @param replace
+     *            indicates the this change should replace existing entries            
+     */
+    public void add(final ArchiveEntry pEntry, final InputStream pInput, final boolean replace)
{
+        addAddition(new Change(pEntry, pInput, replace));
     }
 
     /**
@@ -88,8 +102,14 @@
                     ArchiveEntry entry = change.getEntry();
 
                     if(entry.equals(pChange.getEntry())) {
-                        it.remove();
-                        break;
+                        if(pChange.isReplaceMode()) {
+                            it.remove();
+                            changes.add(pChange);
+                            return;
+                        } else {
+                            // do not add this change
+                            return;
+                        }
                     }
                 }
             }

Modified: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/changes/ChangeSetPerformer.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/changes/ChangeSetPerformer.java?rev=766455&r1=766454&r2=766455&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/changes/ChangeSetPerformer.java
(original)
+++ commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/changes/ChangeSetPerformer.java
Sun Apr 19 12:08:29 2009
@@ -70,7 +70,7 @@
         for (Iterator it = workingSet.iterator(); it.hasNext();) {
             Change change = (Change) it.next();
 
-            if (change.type() == Change.TYPE_ADD) {
+            if (change.type() == Change.TYPE_ADD && change.isReplaceMode()) {
                 copyStream(change.getInput(), out, change.getEntry());
                 it.remove();
                 results.addedFromChangeSet(change.getEntry().getName());
@@ -110,6 +110,19 @@
             }
         }
         
+        // Adds files which hasn't been added from the original and do not have replace mode
on
+        for (Iterator it = workingSet.iterator(); it.hasNext();) {
+            Change change = (Change) it.next();
+
+            if (change.type() == Change.TYPE_ADD && 
+                !change.isReplaceMode() && 
+                !results.hasBeenAdded(change.getEntry().getName())) {
+                copyStream(change.getInput(), out, change.getEntry());
+                it.remove();
+                results.addedFromChangeSet(change.getEntry().getName());
+            }
+        }
+        
         return results;
     }
 

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=766455&r1=766454&r2=766455&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
Sun Apr 19 12:08:29 2009
@@ -99,6 +99,38 @@
     }
     
     /**
+     * Adds an ArchiveEntry with the same name two times.
+     * Only the first addition should be found in the ChangeSet,
+     * the second add should never be added since replace = false
+     * 
+     * @throws Exception
+     */
+    public void testAddChangeTwiceWithoutReplace() 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, true);
+            changes.add(e2, in2, false);
+            
+            assertEquals(1, changes.getChanges().size());
+            Change c = (Change)changes.getChanges().iterator().next();
+            assertEquals(in, 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
@@ -1051,4 +1083,91 @@
 
         this.checkArchiveContent(result, archiveList);
     }
+    
+    /**
+     * Adds a file with the same filename as an existing file from the stream.
+     * Should lead to a replacement.
+     * 
+     * @throws Exception
+     */
+    public void testAddAllreadyExistingWithReplaceTrue() throws Exception {
+        final String archivename = "zip";
+        File input = this.createArchive(archivename);
+
+        ArchiveOutputStream out = null;
+        ArchiveInputStream ais = null;
+        File result = File.createTempFile("test", "."+archivename);
+        result.deleteOnExit();
+        try {
+
+            final InputStream is = new FileInputStream(input);
+            ais = factory.createArchiveInputStream(archivename, is);
+            out = factory.createArchiveOutputStream(archivename,
+                    new FileOutputStream(result));
+
+            ChangeSet changes = new ChangeSet();
+
+            final File file1 = getFile("test.txt");
+            ArchiveEntry entry = new ZipArchiveEntry("testdata/test1.xml");
+            changes.add(entry, new FileInputStream(file1), true);
+            
+            ChangeSetPerformer performer = new ChangeSetPerformer(changes);
+            ChangeSetResults results = performer.perform(ais, out);
+            assertTrue(results.getAddedFromChangeSet().contains("testdata/test1.xml"));
+            is.close();
+
+        } finally {
+            if (out != null)
+                out.close();
+            if (ais != null)
+                ais.close();
+        }
+
+        this.checkArchiveContent(result, archiveList);
+    }
+    
+    /**
+     * Adds a file with the same filename as an existing file from the stream.
+     * Should lead to a replacement.
+     * 
+     * @throws Exception
+     */
+    public void testAddAllreadyExistingWithReplaceFalse() throws Exception {
+        final String archivename = "zip";
+        File input = this.createArchive(archivename);
+
+        ArchiveOutputStream out = null;
+        ArchiveInputStream ais = null;
+        File result = File.createTempFile("test", "."+archivename);
+        result.deleteOnExit();
+        try {
+
+            final InputStream is = new FileInputStream(input);
+            ais = factory.createArchiveInputStream(archivename, is);
+            out = factory.createArchiveOutputStream(archivename,
+                    new FileOutputStream(result));
+
+            ChangeSet changes = new ChangeSet();
+
+            final File file1 = getFile("test.txt");
+            ArchiveEntry entry = new ZipArchiveEntry("testdata/test1.xml");
+            changes.add(entry, new FileInputStream(file1), false);
+            
+            ChangeSetPerformer performer = new ChangeSetPerformer(changes);
+            ChangeSetResults results = performer.perform(ais, out);
+            assertTrue(results.getAddedFromStream().contains("testdata/test1.xml"));
+            assertTrue(results.getAddedFromChangeSet().size() == 0);
+            assertTrue(results.getDeleted().size() == 0);
+            is.close();
+
+        } finally {
+            if (out != null)
+                out.close();
+            if (ais != null)
+                ais.close();
+        }
+
+        this.checkArchiveContent(result, archiveList);
+    }
+    
 }



Mime
View raw message