|
| 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 ru.olegcherednik.zip4jvm; |
| 20 | + |
| 21 | +import ru.olegcherednik.zip4jvm.model.Encryption; |
| 22 | +import ru.olegcherednik.zip4jvm.model.settings.ZipEntrySettings; |
| 23 | +import ru.olegcherednik.zip4jvm.model.settings.ZipEntrySettingsProvider; |
| 24 | +import ru.olegcherednik.zip4jvm.model.settings.ZipSettings; |
| 25 | + |
| 26 | +import org.testng.annotations.Test; |
| 27 | + |
| 28 | +import java.io.IOException; |
| 29 | +import java.nio.file.Path; |
| 30 | + |
| 31 | +import static ru.olegcherednik.zip4jvm.TestData.dirCars; |
| 32 | +import static ru.olegcherednik.zip4jvm.TestData.fileBentley; |
| 33 | +import static ru.olegcherednik.zip4jvm.TestData.fileNameBentley; |
| 34 | +import static ru.olegcherednik.zip4jvm.TestDataAssert.dirCarsAssert; |
| 35 | +import static ru.olegcherednik.zip4jvm.TestDataAssert.fileBentleyAssert; |
| 36 | +import static ru.olegcherednik.zip4jvm.assertj.Zip4jvmAssertions.assertThatDirectory; |
| 37 | +import static ru.olegcherednik.zip4jvm.assertj.Zip4jvmAssertions.assertThatZipFile; |
| 38 | + |
| 39 | +/** |
| 40 | + * @author Oleg Cherednik |
| 41 | + * @since 20.10.2024 |
| 42 | + */ |
| 43 | +@Test |
| 44 | +@SuppressWarnings("FieldNamingConvention") |
| 45 | +public class ZipSpecialTest { |
| 46 | + |
| 47 | + private static final Path rootDir = Zip4jvmSuite.generateSubDirNameWithTime(ZipSpecialTest.class); |
| 48 | + |
| 49 | + public void shouldAddRegularFileWhenSameNameAndDifferentDestPath() throws IOException { |
| 50 | + final char[] one = "1".toCharArray(); |
| 51 | + final char[] two = "2".toCharArray(); |
| 52 | + |
| 53 | + final String oneEntryName = "one/" + fileNameBentley; |
| 54 | + final String twoEntryName = "two/" + fileNameBentley; |
| 55 | + final String threeEntryName = "three/" + fileNameBentley; |
| 56 | + |
| 57 | + ZipSettings settings = ZipSettings.builder() |
| 58 | + .entrySettingsProvider(ZipEntrySettingsProvider.of(entryName -> { |
| 59 | + if (entryName.equals(oneEntryName)) |
| 60 | + return ZipEntrySettings.of(Encryption.AES_256, one); |
| 61 | + if (entryName.equals(twoEntryName)) |
| 62 | + return ZipEntrySettings.of(Encryption.AES_256, two); |
| 63 | + return null; |
| 64 | + })).build(); |
| 65 | + |
| 66 | + Path zip = Zip4jvmSuite.subDirNameAsMethodName(rootDir).resolve("src.zip"); |
| 67 | + |
| 68 | + try (ZipFile.Writer zipFile = ZipFile.writer(zip, settings)) { |
| 69 | + zipFile.addWithMove(fileBentley, "one"); |
| 70 | + zipFile.addWithMove(fileBentley, "two"); |
| 71 | + zipFile.addWithMove(fileBentley, "three"); |
| 72 | + } |
| 73 | + |
| 74 | + assertThatDirectory(zip.getParent()).exists().hasOnlyRegularFiles(1); |
| 75 | + assertThatZipFile(zip).root().hasEntries(3).hasDirectories(3).hasRegularFiles(0); |
| 76 | + assertThatZipFile(zip, one).regularFile(oneEntryName).matches(fileBentleyAssert); |
| 77 | + assertThatZipFile(zip, two).regularFile(twoEntryName).matches(fileBentleyAssert); |
| 78 | + assertThatZipFile(zip).regularFile(threeEntryName).matches(fileBentleyAssert); |
| 79 | + } |
| 80 | + |
| 81 | + public void shouldAddDirectoryWhenSameNameAndDifferentDestPath() throws IOException { |
| 82 | + final char[] one = "1".toCharArray(); |
| 83 | + final char[] two = "2".toCharArray(); |
| 84 | + |
| 85 | + ZipSettings settings = ZipSettings.builder() |
| 86 | + .entrySettingsProvider(ZipEntrySettingsProvider.of(entryName -> { |
| 87 | + if (entryName.startsWith("one/")) |
| 88 | + return ZipEntrySettings.of(Encryption.AES_256, one); |
| 89 | + if (entryName.startsWith("two/")) |
| 90 | + return ZipEntrySettings.of(Encryption.AES_256, two); |
| 91 | + return null; |
| 92 | + })) |
| 93 | + .removeRootDir(true) |
| 94 | + .build(); |
| 95 | + |
| 96 | + Path zip = Zip4jvmSuite.subDirNameAsMethodName(rootDir).resolve("src.zip"); |
| 97 | + |
| 98 | + try (ZipFile.Writer zipFile = ZipFile.writer(zip, settings)) { |
| 99 | + zipFile.addWithMove(dirCars, "one"); |
| 100 | + zipFile.addWithMove(dirCars, "two"); |
| 101 | + zipFile.addWithMove(dirCars, "three"); |
| 102 | + } |
| 103 | + |
| 104 | + assertThatDirectory(zip.getParent()).exists().hasOnlyRegularFiles(1); |
| 105 | + assertThatZipFile(zip).root().hasEntries(3).hasDirectories(3).hasRegularFiles(0); |
| 106 | + assertThatZipFile(zip, one).directory("one").matches(dirCarsAssert); |
| 107 | + assertThatZipFile(zip, two).directory("two").matches(dirCarsAssert); |
| 108 | + assertThatZipFile(zip).directory("three").matches(dirCarsAssert); |
| 109 | + } |
| 110 | + |
| 111 | +} |
0 commit comments