|
8 | 8 | import java.nio.file.LinkOption;
|
9 | 9 | import java.nio.file.NoSuchFileException;
|
10 | 10 | import java.nio.file.Path;
|
| 11 | +import java.nio.file.StandardOpenOption; |
11 | 12 | import java.nio.file.attribute.PosixFilePermission;
|
12 | 13 | import java.nio.file.attribute.PosixFilePermissions;
|
| 14 | +import java.util.ArrayList; |
| 15 | +import java.util.List; |
13 | 16 | import java.util.Set;
|
14 | 17 |
|
| 18 | +import org.apache.commons.compress.archivers.tar.TarArchiveEntry; |
| 19 | +import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream; |
| 20 | +import org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream; |
15 | 21 | import org.junit.jupiter.api.Test;
|
16 | 22 | import org.junit.jupiter.api.io.TempDir;
|
17 | 23 |
|
@@ -579,4 +585,95 @@ public void testGeneratePermissionString() {
|
579 | 585 |
|
580 | 586 | }
|
581 | 587 |
|
| 588 | + /** |
| 589 | + * Tests if extract was called with disabled extract param, the archive will be moved. |
| 590 | + * |
| 591 | + * @param tempDir temporary directory to use. |
| 592 | + */ |
| 593 | + @Test |
| 594 | + public void testDisabledExtractMovesArchive(@TempDir Path tempDir) { |
| 595 | + // arrange |
| 596 | + IdeContext context = IdeTestContextMock.get(); |
| 597 | + FileAccessImpl fileAccess = new FileAccessImpl(context); |
| 598 | + Path downloadArchive = tempDir.resolve("downloaded.zip"); |
| 599 | + fileAccess.touch(downloadArchive); |
| 600 | + Path installationPath = tempDir.resolve("installation"); |
| 601 | + Path targetPath = installationPath.resolve("downloaded.zip"); |
| 602 | + boolean extract = false; |
| 603 | + // act |
| 604 | + fileAccess.extract(downloadArchive, installationPath, this::postExtract, extract); |
| 605 | + // assert |
| 606 | + assertThat(targetPath).exists(); |
| 607 | + } |
| 608 | + |
| 609 | + /** |
| 610 | + * Tests if a tgz archive with a sub folder can be extracted to a target folder properly. |
| 611 | + * |
| 612 | + * @param tempDir temporary directory to use. |
| 613 | + * @throws IOException when a file could not be created. |
| 614 | + */ |
| 615 | + @Test |
| 616 | + public void testExtractTgzArchive(@TempDir Path tempDir) throws IOException { |
| 617 | + // arrange |
| 618 | + IdeContext context = IdeTestContextMock.get(); |
| 619 | + FileAccessImpl fileAccess = new FileAccessImpl(context); |
| 620 | + Path downloadedTgz = tempDir.resolve("downloaded.tgz"); |
| 621 | + fileAccess.touch(downloadedTgz); |
| 622 | + try (GzipCompressorOutputStream gzipOut = new GzipCompressorOutputStream(Files.newOutputStream(downloadedTgz, StandardOpenOption.WRITE)); |
| 623 | + TarArchiveOutputStream tarOut = new TarArchiveOutputStream(gzipOut)) { |
| 624 | + |
| 625 | + // Create a subfolder entry |
| 626 | + TarArchiveEntry subfolderEntry = new TarArchiveEntry("subfolder/"); |
| 627 | + subfolderEntry.setMode(TarArchiveEntry.DEFAULT_DIR_MODE); |
| 628 | + tarOut.putArchiveEntry(subfolderEntry); |
| 629 | + tarOut.closeArchiveEntry(); |
| 630 | + |
| 631 | + // Add a file to the subfolder |
| 632 | + TarArchiveEntry fileEntry = new TarArchiveEntry("subfolder/testfile2.txt"); |
| 633 | + fileEntry.setSize(12); |
| 634 | + tarOut.putArchiveEntry(fileEntry); |
| 635 | + tarOut.write("Hello World2".getBytes()); |
| 636 | + tarOut.closeArchiveEntry(); |
| 637 | + |
| 638 | + // create a file in the root of the archive |
| 639 | + TarArchiveEntry entry = new TarArchiveEntry("testfile.txt"); |
| 640 | + entry.setSize(11); |
| 641 | + tarOut.putArchiveEntry(entry); |
| 642 | + tarOut.write("Hello World".getBytes()); |
| 643 | + tarOut.closeArchiveEntry(); |
| 644 | + } |
| 645 | + Path installationPath = tempDir.resolve("installation"); |
| 646 | + // act |
| 647 | + fileAccess.extractTar(downloadedTgz, installationPath, TarCompression.GZ); |
| 648 | + // assert |
| 649 | + assertThat(installationPath.resolve("testfile.txt")).exists(); |
| 650 | + assertThat(installationPath.resolve("subfolder").resolve("testfile2.txt")).exists(); |
| 651 | + } |
| 652 | + |
| 653 | + /** |
| 654 | + * Tests if a file can be found within a list of folders. |
| 655 | + * |
| 656 | + * @param tempDir temporary directory to use. |
| 657 | + */ |
| 658 | + @Test |
| 659 | + public void testFindExistingFileInFolders(@TempDir Path tempDir) { |
| 660 | + IdeContext context = IdeTestContextMock.get(); |
| 661 | + FileAccessImpl fileAccess = new FileAccessImpl(context); |
| 662 | + Path subfolder1 = tempDir.resolve("subfolder1"); |
| 663 | + fileAccess.mkdirs(subfolder1); |
| 664 | + fileAccess.touch(subfolder1.resolve("testfile")); |
| 665 | + Path subfolder2 = tempDir.resolve("subfolder2"); |
| 666 | + fileAccess.mkdirs(subfolder2); |
| 667 | + fileAccess.touch(subfolder2.resolve("targetfile")); |
| 668 | + List<Path> pathList = new ArrayList<>(); |
| 669 | + pathList.add(subfolder1); |
| 670 | + pathList.add(subfolder2); |
| 671 | + Path foundFile = fileAccess.findExistingFile("targetfile", pathList); |
| 672 | + assertThat(foundFile).exists(); |
| 673 | + } |
| 674 | + |
| 675 | + private void postExtract(Path path) { |
| 676 | + } |
| 677 | + |
| 678 | + |
582 | 679 | }
|
0 commit comments