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