View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.commons.compress.changes;
20  
21  import java.io.BufferedInputStream;
22  import java.io.BufferedReader;
23  import java.io.File;
24  import java.io.FileInputStream;
25  import java.io.FileOutputStream;
26  import java.io.FileReader;
27  import java.io.InputStream;
28  import java.util.ArrayList;
29  import java.util.Iterator;
30  import java.util.List;
31  
32  import org.apache.commons.compress.AbstractTestCase;
33  import org.apache.commons.compress.archivers.ArchiveEntry;
34  import org.apache.commons.compress.archivers.ArchiveInputStream;
35  import org.apache.commons.compress.archivers.ArchiveOutputStream;
36  import org.apache.commons.compress.archivers.ar.ArArchiveEntry;
37  import org.apache.commons.compress.archivers.cpio.CpioArchiveEntry;
38  import org.apache.commons.compress.archivers.jar.JarArchiveEntry;
39  import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
40  import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
41  import org.apache.commons.compress.archivers.zip.ZipFile;
42  
43  /**
44   * Checks several ChangeSet business logics.
45   */
46  public final class ChangeSetTestCase extends AbstractTestCase {
47  
48      // Delete a directory tree
49      private void archiveListDeleteDir(String prefix){
50          Iterator<String> it = archiveList.iterator();
51          while(it.hasNext()){
52              String entry = it.next();
53              if (entry.startsWith(prefix+"/")){ // TODO won't work with folders
54                  it.remove();
55              }
56          }
57      }
58  
59      // Delete a single file
60      private void archiveListDelete(String prefix){
61          Iterator<String> it = archiveList.iterator();
62          while(it.hasNext()){
63              String entry = it.next();
64              if (entry.equals(prefix)){
65                  it.remove();
66              }
67          }
68      }
69  
70      /**
71       * Adds an ArchiveEntry with the same name two times.
72       * Only the latest addition should be found in the ChangeSet,
73       * the first add should be replaced.
74       * 
75       * @throws Exception
76       */
77      public void testAddChangeTwice() throws Exception {
78          InputStream in = null;
79          InputStream in2 = null;
80          try {
81              in = new FileInputStream(getFile("test.txt"));
82              in2 = new FileInputStream(getFile("test2.xml"));
83  
84              ArchiveEntry e = new ZipArchiveEntry("test.txt");
85              ArchiveEntry e2 = new ZipArchiveEntry("test.txt");
86  
87              ChangeSet changes = new ChangeSet();
88              changes.add(e, in);
89              changes.add(e2, in2);
90  
91              assertEquals(1, changes.getChanges().size());
92              Change c = changes.getChanges().iterator().next();
93              assertEquals(in2, c.getInput());
94          } finally {
95              if (in != null) {
96                  in.close();
97              }
98              if (in2 != null) {
99                  in2.close();
100             }
101         }
102     }
103 
104     /**
105      * Adds an ArchiveEntry with the same name two times.
106      * Only the first addition should be found in the ChangeSet,
107      * the second add should never be added since replace = false
108      * 
109      * @throws Exception
110      */
111     public void testAddChangeTwiceWithoutReplace() throws Exception {
112         InputStream in = null;
113         InputStream in2 = null;
114         try {
115             in = new FileInputStream(getFile("test.txt"));
116             in2 = new FileInputStream(getFile("test2.xml"));
117 
118             ArchiveEntry e = new ZipArchiveEntry("test.txt");
119             ArchiveEntry e2 = new ZipArchiveEntry("test.txt");
120 
121             ChangeSet changes = new ChangeSet();
122             changes.add(e, in, true);
123             changes.add(e2, in2, false);
124 
125             assertEquals(1, changes.getChanges().size());
126             Change c = changes.getChanges().iterator().next();
127             assertEquals(in, c.getInput());
128         } finally {
129             if (in != null) {
130                 in.close();
131             }
132             if (in2 != null) {
133                 in2.close();
134             }
135         }
136     }
137 
138     /**
139      * Tries to delete the folder "bla" from an archive file. This should result in
140      * the deletion of bla/*, which actually means bla/test4.xml should be
141      * removed from the archive. The file something/bla (without ending, named
142      * like the folder) should not be deleted.
143      * 
144      * @throws Exception
145      */
146     public void testDeleteDir() throws Exception {
147         final String archivename = "cpio";
148         File input = this.createArchive(archivename);
149 
150         ArchiveOutputStream out = null;
151         ArchiveInputStream ais = null;
152         File result = File.createTempFile("test", "."+archivename);
153         result.deleteOnExit();
154         try {
155 
156             final InputStream is = new FileInputStream(input);
157             ais = factory.createArchiveInputStream(archivename, is);
158 
159             out = factory.createArchiveOutputStream(archivename,
160                     new FileOutputStream(result));
161 
162             ChangeSet changes = new ChangeSet();
163             changes.deleteDir("bla");
164             archiveListDeleteDir("bla");
165             ChangeSetPerformer performer = new ChangeSetPerformer(changes);
166             performer.perform(ais, out);
167             is.close();
168 
169         } finally {
170             if (out != null) {
171                 out.close();
172             }
173             if (ais != null) {
174                 ais.close();
175             }
176         }
177 
178         this.checkArchiveContent(result, archiveList);
179     }
180 
181     /**
182      * Tries to delete the folder "la" from an archive file. This should result in
183      * the deletion of la/*, which should not match any files/folders.
184      * 
185      * @throws Exception
186      */
187     public void testDeleteDir2() throws Exception {
188         final String archivename = "cpio";
189         File input = this.createArchive(archivename);
190 
191         ArchiveOutputStream out = null;
192         ArchiveInputStream ais = null;
193         File result = File.createTempFile("test", "."+archivename);
194         result.deleteOnExit();
195         try {
196 
197             final InputStream is = new FileInputStream(input);
198             ais = factory.createArchiveInputStream(archivename, is);
199 
200             out = factory.createArchiveOutputStream(archivename,
201                     new FileOutputStream(result));
202 
203             ChangeSet changes = new ChangeSet();
204             changes.deleteDir("la");
205             archiveListDeleteDir("la");
206             ChangeSetPerformer performer = new ChangeSetPerformer(changes);
207             performer.perform(ais, out);
208             is.close();
209 
210         } finally {
211             if (out != null) {
212                 out.close();
213             }
214             if (ais != null) {
215                 ais.close();
216             }
217         }
218 
219         this.checkArchiveContent(result, archiveList);
220     }
221 
222     /**
223      * Tries to delete the folder "test.txt" from an archive file.
224      * This should not match any files/folders.
225      * 
226      * @throws Exception
227      */
228     public void testDeleteDir3() throws Exception {
229         final String archivename = "cpio";
230         File input = this.createArchive(archivename);
231 
232         ArchiveOutputStream out = null;
233         ArchiveInputStream ais = null;
234         File result = File.createTempFile("test", "."+archivename);
235         result.deleteOnExit();
236         try {
237 
238             final InputStream is = new FileInputStream(input);
239             ais = factory.createArchiveInputStream(archivename, is);
240 
241             out = factory.createArchiveOutputStream(archivename,
242                     new FileOutputStream(result));
243 
244             ChangeSet changes = new ChangeSet();
245             changes.deleteDir("test.txt");
246             archiveListDeleteDir("test.txt");
247             ChangeSetPerformer performer = new ChangeSetPerformer(changes);
248             performer.perform(ais, out);
249             is.close();
250 
251         } finally {
252             if (out != null) {
253                 out.close();
254             }
255             if (ais != null) {
256                 ais.close();
257             }
258         }
259 
260         this.checkArchiveContent(result, archiveList);
261     }
262 
263     /**
264      * Tries to delete the file "bla/test5.xml" from an archive. This should
265      * result in the deletion of "bla/test5.xml".
266      * 
267      * @throws Exception
268      */
269     public void testDeleteFile() throws Exception {
270         final String archivename = "cpio";
271         File input = this.createArchive(archivename);
272 
273         ArchiveOutputStream out = null;
274         ArchiveInputStream ais = null;
275         File result = File.createTempFile("test", "."+archivename);
276         result.deleteOnExit();
277         try {
278 
279             final InputStream is = new FileInputStream(input);
280             ais = factory.createArchiveInputStream(archivename, is);
281 
282             out = factory.createArchiveOutputStream(archivename,
283                     new FileOutputStream(result));
284 
285             ChangeSet changes = new ChangeSet();
286             changes.delete("bla/test5.xml");
287             archiveListDelete("bla/test5.xml");
288 
289             ChangeSetPerformer performer = new ChangeSetPerformer(changes);
290             performer.perform(ais, out);
291             is.close();
292 
293         } finally {
294             if (out != null) {
295                 out.close();
296             }
297             if (ais != null) {
298                 ais.close();
299             }
300         }
301 
302         this.checkArchiveContent(result, archiveList);
303     }
304 
305     /**
306      * Tries to delete the file "bla" from an archive. This should
307      * result in the deletion of nothing.
308      * 
309      * @throws Exception
310      */
311     public void testDeleteFile2() throws Exception {
312         final String archivename = "cpio";
313         File input = this.createArchive(archivename);
314 
315         ArchiveOutputStream out = null;
316         ArchiveInputStream ais = null;
317         File result = File.createTempFile("test", "."+archivename);
318         result.deleteOnExit();
319         try {
320 
321             final InputStream is = new FileInputStream(input);
322             ais = factory.createArchiveInputStream(archivename, is);
323 
324             out = factory.createArchiveOutputStream(archivename,
325                     new FileOutputStream(result));
326 
327             ChangeSet changes = new ChangeSet();
328             changes.delete("bla");
329             //archiveListDelete("bla");
330 
331             ChangeSetPerformer performer = new ChangeSetPerformer(changes);
332             performer.perform(ais, out);
333             is.close();
334 
335         } finally {
336             if (out != null) {
337                 out.close();
338             }
339             if (ais != null) {
340                 ais.close();
341             }
342         }
343 
344         this.checkArchiveContent(result, archiveList);
345     }
346 
347     /**
348      * Tries to delete and then add a file with the same name.
349      * Should delete test/test3.xml and adds test.txt with the name
350      * test/test3.xml
351      * 
352      * @throws Exception
353      */
354     public void testDeletePlusAddSame() throws Exception {
355         final String archivename = "zip";
356         File input = this.createArchive(archivename);
357 
358         ArchiveOutputStream out = null;
359         ArchiveInputStream ais = null;
360         File result = File.createTempFile("test", "."+archivename);
361         result.deleteOnExit();
362 
363         File testtxt = null;
364         try {
365 
366             final InputStream is = new FileInputStream(input);
367             ais = factory.createArchiveInputStream(archivename, is);
368             out = factory.createArchiveOutputStream(archivename,
369                     new FileOutputStream(result));
370 
371             ChangeSet changes = new ChangeSet();
372             changes.delete("test/test3.xml");
373             archiveListDelete("test/test3.xml");
374 
375             // Add a file
376             testtxt = getFile("test.txt");
377             ArchiveEntry entry = out.createArchiveEntry(testtxt, "test/test3.xml");
378             changes.add(entry, new FileInputStream(testtxt));
379             archiveList.add("test/test3.xml");
380 
381             ChangeSetPerformer performer = new ChangeSetPerformer(changes);
382             performer.perform(ais, out);
383             is.close();
384 
385         } finally {
386             if (out != null) {
387                 out.close();
388             }
389             if (ais != null) {
390                 ais.close();
391             }
392         }
393 
394         // Checks
395         ArchiveInputStream in = null;
396         File check = null;
397         try {
398             final InputStream is = new FileInputStream(result);
399             final BufferedInputStream buf = new BufferedInputStream(is);
400             in = factory.createArchiveInputStream(buf);
401             check = this.checkArchiveContent(in, archiveList, false);
402             File test3xml = new File(check,"result/test/test3.xml");
403             assertEquals(testtxt.length(), test3xml.length());
404 
405             BufferedReader reader = new BufferedReader(new FileReader(test3xml));
406             String str;
407             while ((str = reader.readLine()) != null) {
408                 // All lines look like this
409                 "111111111111111111111111111000101011".equals(str);
410             }
411             reader.close();
412         } finally {
413             if (in != null) {
414                 in.close();
415             }
416             rmdir(check);
417         } 
418     }
419 
420     /**
421      * Checks for the correct ChangeSetResults
422      * 
423      * @throws Exception
424      */
425     public void testChangeSetResults() throws Exception {
426         final String archivename = "cpio";
427         File input = this.createArchive(archivename);
428 
429         ArchiveOutputStream out = null;
430         ArchiveInputStream ais = null;
431         File result = File.createTempFile("test", "."+archivename);
432         result.deleteOnExit();
433         try {
434 
435             final InputStream is = new FileInputStream(input);
436             ais = factory.createArchiveInputStream(archivename, is);
437             out = factory.createArchiveOutputStream(archivename,
438                     new FileOutputStream(result));
439 
440             ChangeSet changes = new ChangeSet();
441             changes.deleteDir("bla");
442             archiveListDeleteDir("bla");
443 
444             // Add a file
445             final File file1 = getFile("test.txt");
446             ArchiveEntry entry = out.createArchiveEntry(file1, "bla/test.txt");
447             changes.add(entry, new FileInputStream(file1));
448             archiveList.add("bla/test.txt");
449 
450             ChangeSetPerformer performer = new ChangeSetPerformer(changes);
451             ChangeSetResults results = performer.perform(ais, out);
452             is.close();
453 
454             // Checks
455             assertEquals(1,results.getAddedFromChangeSet().size());
456             assertEquals("bla/test.txt",results.getAddedFromChangeSet().iterator().next());
457             assertEquals(3,results.getDeleted().size());
458             assertTrue(results.getDeleted().contains("bla/test4.xml"));
459             assertTrue(results.getDeleted().contains("bla/test5.xml"));
460             assertTrue(results.getDeleted().contains("bla/blubber/test6.xml"));
461 
462             assertTrue(results.getAddedFromStream().contains("testdata/test1.xml"));
463             assertTrue(results.getAddedFromStream().contains("testdata/test2.xml"));
464             assertTrue(results.getAddedFromStream().contains("test/test3.xml"));
465             assertTrue(results.getAddedFromStream().contains("test.txt"));
466             assertTrue(results.getAddedFromStream().contains("something/bla"));
467             assertTrue(results.getAddedFromStream().contains("test with spaces.txt"));
468             assertEquals(6,results.getAddedFromStream().size());
469         } finally {
470             if (out != null) {
471                 out.close();
472             }
473             if (ais != null) {
474                 ais.close();
475             }
476         }
477 
478         this.checkArchiveContent(result, archiveList);
479     }
480 
481     /**
482      * Tries to delete a directory with a file and adds a new directory with a
483      * new file and with the same name. Should delete dir1/* and add
484      * dir1/test.txt at the end
485      * 
486      * @throws Exception
487      */
488     public void testDeletePlusAdd() throws Exception {
489         final String archivename = "cpio";
490         File input = this.createArchive(archivename);
491 
492         ArchiveOutputStream out = null;
493         ArchiveInputStream ais = null;
494         File result = File.createTempFile("test", "."+archivename);
495         result.deleteOnExit();
496         try {
497 
498             final InputStream is = new FileInputStream(input);
499             ais = factory.createArchiveInputStream(archivename, is);
500             out = factory.createArchiveOutputStream(archivename,
501                     new FileOutputStream(result));
502 
503             ChangeSet changes = new ChangeSet();
504             changes.deleteDir("bla");
505             archiveListDeleteDir("bla");
506 
507             // Add a file
508             final File file1 = getFile("test.txt");
509             ArchiveEntry entry = out.createArchiveEntry(file1, "bla/test.txt");
510             changes.add(entry, new FileInputStream(file1));
511             archiveList.add("bla/test.txt");
512 
513             ChangeSetPerformer performer = new ChangeSetPerformer(changes);
514             performer.perform(ais, out);
515             is.close();
516 
517         } finally {
518             if (out != null) {
519                 out.close();
520             }
521             if (ais != null) {
522                 ais.close();
523             }
524         }
525 
526         this.checkArchiveContent(result, archiveList);
527     }
528 
529     /**
530      * Adds a file to a zip archive. Deletes an other file.
531      * 
532      * @throws Exception
533      */
534     public void testDeleteFromAndAddToZip() throws Exception {
535         final String archivename = "zip";
536         File input = this.createArchive(archivename);
537 
538         ArchiveOutputStream out = null;
539         ArchiveInputStream ais = null;
540         File result = File.createTempFile("test", "."+archivename);
541         result.deleteOnExit();
542         try {
543 
544             final InputStream is = new FileInputStream(input);
545             ais = factory.createArchiveInputStream(archivename, is);
546             out = factory.createArchiveOutputStream(archivename,
547                     new FileOutputStream(result));
548 
549             ChangeSet changes = new ChangeSet();
550 
551             final File file1 = getFile("test.txt");
552             ArchiveEntry entry = new ZipArchiveEntry("blub/test.txt");
553             changes.add(entry, new FileInputStream(file1));
554             archiveList.add("blub/test.txt");
555 
556             changes.delete("testdata/test1.xml");
557             archiveListDelete("testdata/test1.xml");
558 
559             ChangeSetPerformer performer = new ChangeSetPerformer(changes);
560             performer.perform(ais, out);
561             is.close();
562 
563         } finally {
564             if (out != null) {
565                 out.close();
566             }
567             if (ais != null) {
568                 ais.close();
569             }
570         }
571 
572         this.checkArchiveContent(result, archiveList);
573     }
574 
575     /**
576      * Adds a file to a zip archive. Deletes an other file.
577      * 
578      * @throws Exception
579      */
580     public void testDeleteFromAndAddToZipUsingZipFilePerform() throws Exception {
581         final String archivename = "zip";
582         File input = this.createArchive(archivename);
583 
584         ArchiveOutputStream out = null;
585         ZipFile ais = null;
586         File result = File.createTempFile("test", "."+archivename);
587         result.deleteOnExit();
588         try {
589 
590             ais = new ZipFile(input);
591             out = factory.createArchiveOutputStream(archivename,
592                     new FileOutputStream(result));
593 
594             ChangeSet changes = new ChangeSet();
595 
596             final File file1 = getFile("test.txt");
597             ArchiveEntry entry = new ZipArchiveEntry("blub/test.txt");
598             changes.add(entry, new FileInputStream(file1));
599             archiveList.add("blub/test.txt");
600 
601             changes.delete("testdata/test1.xml");
602             archiveListDelete("testdata/test1.xml");
603 
604             ChangeSetPerformer performer = new ChangeSetPerformer(changes);
605             performer.perform(ais, out);
606 
607         } finally {
608             if (out != null) {
609                 out.close();
610             }
611             if (ais != null) {
612                 ais.close();
613             }
614         }
615 
616         this.checkArchiveContent(result, archiveList);
617     }
618 
619     /**
620      * add blub/test.txt + delete blub Should add blub/test.txt and delete it
621      * afterwards. In this example, the archive should stay untouched.
622      * 
623      * @throws Exception
624      */
625     public void testAddDeleteAdd() throws Exception {
626         final String archivename = "cpio";
627         File input = this.createArchive(archivename);
628 
629         ArchiveOutputStream out = null;
630         ArchiveInputStream ais = null;
631         File result = File.createTempFile("test", "."+archivename);
632         result.deleteOnExit();
633         try {
634 
635             final InputStream is = new FileInputStream(input);
636             ais = factory.createArchiveInputStream(archivename, is);
637             out = factory.createArchiveOutputStream(archivename,
638                     new FileOutputStream(result));
639 
640             ChangeSet changes = new ChangeSet();
641 
642             final File file1 = getFile("test.txt");
643             ArchiveEntry entry = new CpioArchiveEntry("blub/test.txt");
644             changes.add(entry, new FileInputStream(file1));
645             archiveList.add("blub/test.txt");
646 
647             changes.deleteDir("blub");
648             archiveListDeleteDir("blub");
649 
650             ChangeSetPerformer performer = new ChangeSetPerformer(changes);
651             performer.perform(ais, out);
652 
653             is.close();
654 
655         } finally {
656             if (out != null) {
657                 out.close();
658             }
659             if (ais != null) {
660                 ais.close();
661             }
662         }
663 
664         this.checkArchiveContent(result, archiveList);
665     }
666 
667     /**
668      * delete bla + add bla/test.txt + delete bla Deletes dir1/* first, then
669      * suppresses the add of bla.txt because there is a delete operation later.
670      * 
671      * @throws Exception
672      */
673     public void testDeleteAddDelete() throws Exception {
674         final String archivename = "cpio";
675         File input = this.createArchive(archivename);
676 
677         ArchiveOutputStream out = null;
678         ArchiveInputStream ais = null;
679         File result = File.createTempFile("test", "."+archivename);
680         result.deleteOnExit();
681         try {
682 
683             final InputStream is = new FileInputStream(input);
684             ais = factory.createArchiveInputStream(archivename, is);
685             out = factory.createArchiveOutputStream(archivename,
686                     new FileOutputStream(result));
687 
688             ChangeSet changes = new ChangeSet();
689 
690             changes.deleteDir("bla");
691 
692             final File file1 = getFile("test.txt");
693             ArchiveEntry entry = new CpioArchiveEntry("bla/test.txt");
694             changes.add(entry, new FileInputStream(file1));
695             archiveList.add("bla/test.txt");
696 
697             changes.deleteDir("bla");
698             archiveListDeleteDir("bla");
699 
700             ChangeSetPerformer performer = new ChangeSetPerformer(changes);
701             performer.perform(ais, out);
702 
703             is.close();
704 
705         } finally {
706             if (out != null) {
707                 out.close();
708             }
709             if (ais != null) {
710                 ais.close();
711             }
712         }
713 
714         this.checkArchiveContent(result, archiveList);
715     }
716 
717     /**
718      * Simple Delete from a zip file.
719      * 
720      * @throws Exception
721      */
722     public void testDeleteFromZip() throws Exception {
723         ArchiveOutputStream out = null;
724         ArchiveInputStream ais = null;
725         File temp = null;
726         try {
727             ChangeSet changes = new ChangeSet();
728             changes.delete("test2.xml");
729 
730             final File input = getFile("bla.zip");
731             final InputStream is = new FileInputStream(input);
732             ais = factory.createArchiveInputStream("zip", is);
733 
734             temp = File.createTempFile("test", ".zip");
735             temp.deleteOnExit();
736             out = factory.createArchiveOutputStream("zip",
737                     new FileOutputStream(temp));
738 
739             ChangeSetPerformer performer = new ChangeSetPerformer(changes);
740             performer.perform(ais, out);
741 
742         } finally {
743             if (out != null) {
744                 out.close();
745             }
746             if (ais != null) {
747                 ais.close();
748             }
749         }
750 
751         List<String> expected = new ArrayList<String>();
752         expected.add("test1.xml");
753 
754         this.checkArchiveContent(temp, expected);
755     }
756 
757     /**
758      * Simple delete from a tar file
759      * 
760      * @throws Exception
761      */
762     public void testDeleteFromTar() throws Exception {
763         ArchiveOutputStream out = null;
764         ArchiveInputStream ais = null;
765         File temp = null;
766         try {
767             ChangeSet changes = new ChangeSet();
768             changes.delete("test2.xml");
769 
770             final File input = getFile("bla.tar");
771             final InputStream is = new FileInputStream(input);
772             ais = factory.createArchiveInputStream("tar", is);
773 
774             temp = new File(dir, "bla.tar");
775             out = factory.createArchiveOutputStream("tar",
776                     new FileOutputStream(temp));
777 
778             ChangeSetPerformer performer = new ChangeSetPerformer(changes);
779             performer.perform(ais, out);
780 
781         } finally {
782             if (out != null) {
783                 out.close();
784             }
785             if (ais != null) {
786                 ais.close();
787             }
788         }
789         List<String> expected = new ArrayList<String>();
790         expected.add("test1.xml");
791         this.checkArchiveContent(temp, expected);
792     }
793 
794     /**
795      * Simple delete from a jar file
796      * 
797      * @throws Exception
798      */
799     public void testDeleteFromJar() throws Exception {
800         ArchiveOutputStream out = null;
801         ArchiveInputStream ais = null;
802         File temp = null;
803         try {
804             ChangeSet changes = new ChangeSet();
805             changes.delete("test2.xml");
806             changes.deleteDir("META-INF");
807             changes.delete(".classpath");
808             changes.delete(".project");
809 
810             final File input = getFile("bla.jar");
811             final InputStream is = new FileInputStream(input);
812             ais = factory.createArchiveInputStream("jar", is);
813 
814             temp = new File(dir, "bla.jar");
815             out = factory.createArchiveOutputStream("jar",
816                     new FileOutputStream(temp));
817 
818             ChangeSetPerformer performer = new ChangeSetPerformer(changes);
819             performer.perform(ais, out);
820 
821         } finally {
822             if (out != null) {
823                 out.close();
824             }
825             if (ais != null) {
826                 ais.close();
827             }
828         }
829         List<String> expected = new ArrayList<String>();
830         expected.add("test1.xml");
831         this.checkArchiveContent(temp, expected);
832     }
833 
834     public void testDeleteFromAndAddToTar() throws Exception {
835         ArchiveOutputStream out = null;
836         ArchiveInputStream ais = null;
837         File temp = null;
838         try {
839             ChangeSet changes = new ChangeSet();
840             changes.delete("test2.xml");
841 
842             final File file1 = getFile("test.txt");
843 
844             final TarArchiveEntry entry = new TarArchiveEntry(
845                     "testdata/test.txt");
846             entry.setModTime(0);
847             entry.setSize(file1.length());
848             entry.setUserId(0);
849             entry.setGroupId(0);
850             entry.setUserName("avalon");
851             entry.setGroupName("excalibur");
852             entry.setMode(0100000);
853 
854             changes.add(entry, new FileInputStream(file1));
855 
856             final File input = getFile("bla.tar");
857             final InputStream is = new FileInputStream(input);
858             ais = factory.createArchiveInputStream("tar", is);
859 
860             temp = new File(dir, "bla.tar");
861             out = factory.createArchiveOutputStream("tar",
862                     new FileOutputStream(temp));
863 
864             ChangeSetPerformer performer = new ChangeSetPerformer(changes);
865             performer.perform(ais, out);
866 
867         } finally {
868             if (out != null) {
869                 out.close();
870             }
871             if (ais != null) {
872                 ais.close();
873             }
874         }
875         List<String> expected = new ArrayList<String>();
876         expected.add("test1.xml");
877         expected.add("testdata/test.txt");
878         final ArchiveInputStream in = factory.createArchiveInputStream("tar", new FileInputStream(temp));
879         this.checkArchiveContent(in, expected);
880     }
881 
882     /**
883      * Delete from a jar file and add another file
884      * 
885      * @throws Exception
886      */
887     public void testDeleteFromAndAddToJar() throws Exception {
888         ArchiveOutputStream out = null;
889         ArchiveInputStream ais = null;
890         File temp = null;
891         try {
892             ChangeSet changes = new ChangeSet();
893             changes.delete("test2.xml");
894             changes.deleteDir("META-INF");
895             changes.delete(".classpath");
896             changes.delete(".project");
897 
898             final File file1 = getFile("test.txt");
899             JarArchiveEntry entry = new JarArchiveEntry("testdata/test.txt");
900             changes.add(entry, new FileInputStream(file1));
901 
902             final File input = getFile("bla.jar");
903             final InputStream is = new FileInputStream(input);
904             ais = factory.createArchiveInputStream("jar", is);
905 
906             temp = new File(dir, "bla.jar");
907             out = factory.createArchiveOutputStream("jar",
908                     new FileOutputStream(temp));
909 
910             ChangeSetPerformer performer = new ChangeSetPerformer(changes);
911             performer.perform(ais, out);
912 
913         } finally {
914             if (out != null) {
915                 out.close();
916             }
917             if (ais != null) {
918                 ais.close();
919             }
920         }
921         List<String> expected = new ArrayList<String>();
922         expected.add("test1.xml");
923         expected.add("testdata/test.txt");
924         this.checkArchiveContent(temp, expected);
925     }
926 
927     /**
928      * Simple delete from an ar file
929      * 
930      * @throws Exception
931      */
932     public void testDeleteFromAr() throws Exception {
933         ArchiveOutputStream out = null;
934         ArchiveInputStream ais = null;
935         File temp = null;
936         try {
937             ChangeSet changes = new ChangeSet();
938             changes.delete("test2.xml");
939 
940             final File input = getFile("bla.ar");
941             final InputStream is = new FileInputStream(input);
942             ais = factory.createArchiveInputStream("ar", is);
943 
944             temp = new File(dir, "bla.ar");
945             out = factory.createArchiveOutputStream("ar",
946                     new FileOutputStream(temp));
947 
948             ChangeSetPerformer performer = new ChangeSetPerformer(changes);
949             performer.perform(ais, out);
950 
951         } finally {
952             if (out != null) {
953                 out.close();
954             }
955             if (ais != null) {
956                 ais.close();
957             }
958         }
959 
960         List<String> expected = new ArrayList<String>();
961         expected.add("test1.xml");
962         this.checkArchiveContent(temp, expected);
963     }
964 
965     /**
966      * Deletes a file from an AR-archive and adds another
967      * 
968      * @throws Exception
969      */
970     public void testDeleteFromAndAddToAr() throws Exception {
971         ArchiveOutputStream out = null;
972         ArchiveInputStream ais = null;
973         File temp = null;
974         try {
975             ChangeSet changes = new ChangeSet();
976             changes.delete("test2.xml");
977 
978             final File file1 = getFile("test.txt");
979 
980             final ArArchiveEntry entry = new ArArchiveEntry("test.txt", file1
981                     .length());
982 
983             changes.add(entry, new FileInputStream(file1));
984 
985             final File input = getFile("bla.ar");
986             final InputStream is = new FileInputStream(input);
987             ais = factory.createArchiveInputStream("ar", is);
988 
989             temp = new File(dir, "bla.ar");
990             out = factory.createArchiveOutputStream("ar",
991                     new FileOutputStream(temp));
992 
993             ChangeSetPerformer performer = new ChangeSetPerformer(changes);
994             performer.perform(ais, out);
995 
996         } finally {
997             if (out != null) {
998                 out.close();
999             }
1000             if (ais != null) {
1001                 ais.close();
1002             }
1003         }
1004         List<String> expected = new ArrayList<String>();
1005         expected.add("test1.xml");
1006         expected.add("test.txt");
1007         this.checkArchiveContent(temp, expected);
1008     }
1009 
1010     /**
1011      * TODO: Move operations are not supported currently
1012      * 
1013      * mv dir1/test.text dir2/test.txt + delete dir1 Moves the file to dir2 and
1014      * deletes everything in dir1
1015      * 
1016      * @throws Exception
1017      */
1018     public void testRenameAndDelete() throws Exception {
1019     }
1020 
1021     /**
1022      * TODO: Move operations are not supported currently
1023      * 
1024      * add dir1/bla.txt + mv dir1/test.text dir2/test.txt + delete dir1
1025      * 
1026      * Add dir1/bla.txt should be surpressed. All other dir1 files will be
1027      * deleted, except dir1/test.text will be moved
1028      * 
1029      * @throws Exception
1030      */
1031     public void testAddMoveDelete() throws Exception {
1032     }
1033 
1034     /**
1035      * Check can add a file to an empty archive.
1036      * 
1037      * @throws Exception
1038      */
1039     public void testAddToEmptyArchive() throws Exception {
1040         final String archivename = "zip";
1041         File input = this.createEmptyArchive(archivename);
1042 
1043         ArchiveOutputStream out = null;
1044         ArchiveInputStream ais = null;
1045         InputStream is = null;
1046         File result = File.createTempFile("test", "."+archivename);
1047         result.deleteOnExit();
1048         ChangeSet changes = new ChangeSet();
1049         try {
1050 
1051             is = new FileInputStream(input);
1052             ais = factory.createArchiveInputStream(archivename, is);
1053 
1054             out = factory.createArchiveOutputStream(archivename,
1055                     new FileOutputStream(result));
1056 
1057             final File file1 = getFile("test.txt");
1058             ArchiveEntry entry = new ZipArchiveEntry("bla/test.txt");
1059             changes.add(entry, new FileInputStream(file1));
1060             archiveList.add("bla/test.txt");
1061             ChangeSetPerformer performer = new ChangeSetPerformer(changes);
1062             performer.perform(ais, out);
1063             is.close();
1064 
1065         } finally {
1066             if (out != null) {
1067                 out.close();
1068             }
1069             if (ais != null) {
1070                 ais.close(); // will close is 
1071             } else if (is != null){
1072                 is.close();
1073             }
1074         }
1075 
1076         this.checkArchiveContent(result, archiveList);
1077     }
1078 
1079     /**
1080      * Check can delete and add a file to an archive with a single file
1081      * 
1082      * @throws Exception
1083      */
1084     public void testDeleteAddToOneFileArchive() throws Exception {
1085         final String archivename = "zip";
1086         File input = this.createSingleEntryArchive(archivename);
1087 
1088         ArchiveOutputStream out = null;
1089         ArchiveInputStream ais = null;
1090         InputStream is = null;
1091         File result = File.createTempFile("test", "."+archivename);
1092         result.deleteOnExit();
1093         ChangeSet changes = new ChangeSet();
1094         try {
1095 
1096             is = new FileInputStream(input);
1097             ais = factory.createArchiveInputStream(archivename, is);
1098 
1099             out = factory.createArchiveOutputStream(archivename,
1100                     new FileOutputStream(result));
1101             changes.delete("test1.xml");
1102             archiveListDelete("test1.xml");
1103 
1104             final File file = getFile("test.txt");
1105             ArchiveEntry entry = out.createArchiveEntry(file,"bla/test.txt");
1106             changes.add(entry, new FileInputStream(file));
1107             archiveList.add("bla/test.txt");
1108 
1109             ChangeSetPerformer performer = new ChangeSetPerformer(changes);
1110             performer.perform(ais, out);
1111             is.close();
1112 
1113         } finally {
1114             if (out != null) {
1115                 out.close();
1116             }
1117             if (ais != null) {
1118                 ais.close(); // will close is 
1119             } else if (is != null){
1120                 is.close();
1121             }
1122         }
1123 
1124         this.checkArchiveContent(result, archiveList);
1125     }
1126 
1127     /**
1128      * Check can add and delete a file to an archive with a single file
1129      * 
1130      * @throws Exception
1131      */
1132     public void testAddDeleteToOneFileArchive() throws Exception {
1133         final String archivename = "cpio";
1134         File input = this.createSingleEntryArchive(archivename);
1135 
1136         ArchiveOutputStream out = null;
1137         ArchiveInputStream ais = null;
1138         InputStream is = null;
1139         File result = File.createTempFile("test", "."+archivename);
1140         result.deleteOnExit();
1141         ChangeSet changes = new ChangeSet();
1142         try {
1143 
1144             is = new FileInputStream(input);
1145             ais = factory.createArchiveInputStream(archivename, is);
1146 
1147             out = factory.createArchiveOutputStream(archivename,
1148                     new FileOutputStream(result));
1149             final File file = getFile("test.txt");
1150             ArchiveEntry entry = out.createArchiveEntry(file,"bla/test.txt"); 
1151             changes.add(entry, new FileInputStream(file));
1152             archiveList.add("bla/test.txt");
1153 
1154             changes.delete("test1.xml");
1155             archiveListDelete("test1.xml");
1156 
1157             ChangeSetPerformer performer = new ChangeSetPerformer(changes);
1158             performer.perform(ais, out);
1159             is.close();
1160 
1161         } finally {
1162             if (out != null) {
1163                 out.close();
1164             }
1165             if (ais != null) {
1166                 ais.close(); // will close is 
1167             } else if (is != null){
1168                 is.close();
1169             }
1170         }
1171 
1172         this.checkArchiveContent(result, archiveList);
1173     }
1174 
1175     /**
1176      * Adds a file with the same filename as an existing file from the stream.
1177      * Should lead to a replacement.
1178      * 
1179      * @throws Exception
1180      */
1181     public void testAddAllreadyExistingWithReplaceTrue() throws Exception {
1182         final String archivename = "zip";
1183         File input = this.createArchive(archivename);
1184 
1185         ArchiveOutputStream out = null;
1186         ArchiveInputStream ais = null;
1187         File result = File.createTempFile("test", "."+archivename);
1188         result.deleteOnExit();
1189         try {
1190 
1191             final InputStream is = new FileInputStream(input);
1192             ais = factory.createArchiveInputStream(archivename, is);
1193             out = factory.createArchiveOutputStream(archivename,
1194                     new FileOutputStream(result));
1195 
1196             ChangeSet changes = new ChangeSet();
1197 
1198             final File file1 = getFile("test.txt");
1199             ArchiveEntry entry = new ZipArchiveEntry("testdata/test1.xml");
1200             changes.add(entry, new FileInputStream(file1), true);
1201 
1202             ChangeSetPerformer performer = new ChangeSetPerformer(changes);
1203             ChangeSetResults results = performer.perform(ais, out);
1204             assertTrue(results.getAddedFromChangeSet().contains("testdata/test1.xml"));
1205             is.close();
1206 
1207         } finally {
1208             if (out != null) {
1209                 out.close();
1210             }
1211             if (ais != null) {
1212                 ais.close();
1213             }
1214         }
1215 
1216         this.checkArchiveContent(result, archiveList);
1217     }
1218 
1219     /**
1220      * Adds a file with the same filename as an existing file from the stream.
1221      * Should lead to a replacement.
1222      * 
1223      * @throws Exception
1224      */
1225     public void testAddAllreadyExistingWithReplaceFalse() throws Exception {
1226         final String archivename = "zip";
1227         File input = this.createArchive(archivename);
1228 
1229         ArchiveOutputStream out = null;
1230         ArchiveInputStream ais = null;
1231         File result = File.createTempFile("test", "."+archivename);
1232         result.deleteOnExit();
1233         try {
1234 
1235             final InputStream is = new FileInputStream(input);
1236             ais = factory.createArchiveInputStream(archivename, is);
1237             out = factory.createArchiveOutputStream(archivename,
1238                     new FileOutputStream(result));
1239 
1240             ChangeSet changes = new ChangeSet();
1241 
1242             final File file1 = getFile("test.txt");
1243             ArchiveEntry entry = new ZipArchiveEntry("testdata/test1.xml");
1244             changes.add(entry, new FileInputStream(file1), false);
1245 
1246             ChangeSetPerformer performer = new ChangeSetPerformer(changes);
1247             ChangeSetResults results = performer.perform(ais, out);
1248             assertTrue(results.getAddedFromStream().contains("testdata/test1.xml"));
1249             assertTrue(results.getAddedFromChangeSet().isEmpty());
1250             assertTrue(results.getDeleted().isEmpty());
1251             is.close();
1252 
1253         } finally {
1254             if (out != null) {
1255                 out.close();
1256             }
1257             if (ais != null) {
1258                 ais.close();
1259             }
1260         }
1261 
1262         this.checkArchiveContent(result, archiveList);
1263     }
1264 
1265 }