PageRenderTime 30ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 1ms

/amps-maven-plugin/src/test/java/com/atlassian/maven/plugins/amps/util/TestZipUtils.java

https://bitbucket.org/atlassian/amps
Java | 513 lines | 393 code | 112 blank | 8 comment | 20 complexity | 851c60b84a8c11c0277d5d02538d8d77 MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause
  1. package com.atlassian.maven.plugins.amps.util;
  2. import com.google.common.collect.Lists;
  3. import org.apache.commons.io.FileUtils;
  4. import org.junit.Before;
  5. import org.junit.Rule;
  6. import org.junit.Test;
  7. import org.junit.rules.TemporaryFolder;
  8. import java.io.File;
  9. import java.io.IOException;
  10. import java.net.URISyntaxException;
  11. import java.net.URL;
  12. import java.nio.charset.StandardCharsets;
  13. import java.util.Collections;
  14. import java.util.Enumeration;
  15. import java.util.List;
  16. import java.util.regex.Pattern;
  17. import java.util.zip.ZipEntry;
  18. import java.util.zip.ZipFile;
  19. import static java.util.stream.Collectors.toList;
  20. import static org.hamcrest.Matchers.allOf;
  21. import static org.hamcrest.Matchers.equalTo;
  22. import static org.hamcrest.Matchers.hasItems;
  23. import static org.hamcrest.Matchers.iterableWithSize;
  24. import static org.hamcrest.Matchers.not;
  25. import static org.junit.Assert.assertEquals;
  26. import static org.junit.Assert.assertFalse;
  27. import static org.junit.Assert.assertThat;
  28. import static org.junit.Assert.assertTrue;
  29. import static org.junit.Assume.assumeTrue;
  30. @SuppressWarnings("Duplicates")
  31. public class TestZipUtils {
  32. private static final int NUM_FILES = 2;
  33. private static final int NUM_FOLDERS = 4;
  34. private static final int NUM_FOLDERS_NESTED_PREFIX = NUM_FOLDERS + 1;
  35. private static final String ROOT_DIR = "test-zip-dir";
  36. private static final String FIRST_PREFIX = "prefix1";
  37. private static final String SECOND_PREFIX = "prefix2";
  38. private static final String NESTED_PREFIX = FIRST_PREFIX + "/" + SECOND_PREFIX;
  39. @Rule
  40. public final TemporaryFolder tempDir = new TemporaryFolder();
  41. private File sourceZipDir;
  42. @Before
  43. public void ensureDirsExist() throws IOException {
  44. // Create our test source tree
  45. sourceZipDir = tempDir.newFolder(ROOT_DIR);
  46. tempDir.newFolder(ROOT_DIR, "level2sub1");
  47. File level2sub2 = tempDir.newFolder(ROOT_DIR, "level2sub2");
  48. File level2TextFile = new File(level2sub2, "level2sub2.txt");
  49. FileUtils.writeStringToFile(level2TextFile, "level2sub2", StandardCharsets.UTF_8);
  50. File level3sub1 = tempDir.newFolder(ROOT_DIR, "level2sub2", "level3sub1");
  51. File level3TextFile = new File(level3sub1, "level3sub1.txt");
  52. FileUtils.writeStringToFile(level3TextFile, "level3sub1", StandardCharsets.UTF_8);
  53. }
  54. @Test
  55. public void zipChildrenHasNoPrefix() throws IOException {
  56. File zipFile = tempDir.newFile("zip-children.zip");
  57. ZipUtils.zipChildren(zipFile, sourceZipDir);
  58. try (ZipFile zip = new ZipFile(zipFile)) {
  59. List<String> entries = Collections.list(zip.entries()).stream()
  60. .map(ZipEntry::getName)
  61. .collect(toList());
  62. // - Directories should have explicit entries
  63. // - Entries should be relative to _without including_ sourceZipDir (test-zip-dir)
  64. assertThat(entries, allOf(
  65. iterableWithSize(5),
  66. hasItems("level2sub1/", "level2sub2/", "level2sub2/level2sub2.txt",
  67. "level2sub2/level3sub1/", "level2sub2/level3sub1/level3sub1.txt")
  68. ));
  69. }
  70. }
  71. @Test
  72. public void zipContainsSinglePrefix() throws IOException {
  73. File zipFile = tempDir.newFile("zip-with-prefix.zip");
  74. ZipUtils.zipDir(zipFile, sourceZipDir, FIRST_PREFIX);
  75. try (ZipFile zip = new ZipFile(zipFile)) {
  76. final Enumeration<? extends ZipEntry> entries = zip.entries();
  77. while (entries.hasMoreElements()) {
  78. final ZipEntry zipEntry = entries.nextElement();
  79. String zipPath = zipEntry.getName();
  80. String testPrefix = zipPath.substring(0, zipPath.indexOf("/"));
  81. assertEquals(FIRST_PREFIX, testPrefix);
  82. }
  83. }
  84. }
  85. @Test
  86. public void zipContainsNestedPrefix() throws IOException {
  87. File zipFile = tempDir.newFile("zip-nested-prefix.zip");
  88. ZipUtils.zipDir(zipFile, sourceZipDir, NESTED_PREFIX);
  89. try (ZipFile zip = new ZipFile(zipFile)) {
  90. final Enumeration<? extends ZipEntry> entries = zip.entries();
  91. while (entries.hasMoreElements()) {
  92. final ZipEntry zipEntry = entries.nextElement();
  93. String zipPath = zipEntry.getName();
  94. String[] segments = zipPath.split("/");
  95. if (segments.length > 1) {
  96. String testPrefix = segments[0] + "/" + segments[1];
  97. assertEquals(NESTED_PREFIX, testPrefix);
  98. }
  99. }
  100. }
  101. }
  102. @Test
  103. public void prefixedZipDoesNotContainRootDir() throws IOException {
  104. File zipFile = tempDir.newFile("zip-with-prefix-no-root.zip");
  105. ZipUtils.zipDir(zipFile, sourceZipDir, FIRST_PREFIX);
  106. try (ZipFile zip = new ZipFile(zipFile)) {
  107. final Enumeration<? extends ZipEntry> entries = zip.entries();
  108. while (entries.hasMoreElements()) {
  109. final ZipEntry zipEntry = entries.nextElement();
  110. String zipPath = zipEntry.getName();
  111. String[] segments = zipPath.split("/");
  112. if (segments.length > 1) {
  113. String rootPath = segments[1];
  114. assertThat(rootPath, not(equalTo(ROOT_DIR)));
  115. }
  116. }
  117. }
  118. }
  119. @Test
  120. public void nestedPrefixedZipDoesNotContainRootDir() throws IOException {
  121. File zipFile = tempDir.newFile("zip-nested-prefix-no-root.zip");
  122. ZipUtils.zipDir(zipFile, sourceZipDir, NESTED_PREFIX);
  123. try (ZipFile zip = new ZipFile(zipFile)) {
  124. final Enumeration<? extends ZipEntry> entries = zip.entries();
  125. while (entries.hasMoreElements()) {
  126. final ZipEntry zipEntry = entries.nextElement();
  127. String zipPath = zipEntry.getName();
  128. String[] segments = zipPath.split("/");
  129. if (segments.length > 2) {
  130. String rootPath = segments[2];
  131. assertThat(rootPath, not(equalTo(ROOT_DIR)));
  132. }
  133. }
  134. }
  135. }
  136. @Test
  137. public void emptyPrefixedZipContainsRootDir() throws IOException {
  138. File zipFile = tempDir.newFile("zip-empty-prefix.zip");
  139. ZipUtils.zipDir(zipFile, sourceZipDir, "");
  140. try (ZipFile zip = new ZipFile(zipFile)) {
  141. final Enumeration<? extends ZipEntry> entries = zip.entries();
  142. while (entries.hasMoreElements()) {
  143. final ZipEntry zipEntry = entries.nextElement();
  144. String zipPath = zipEntry.getName();
  145. String rootPath = zipPath.substring(0, zipPath.indexOf("/"));
  146. assertEquals(ROOT_DIR, rootPath);
  147. }
  148. }
  149. }
  150. @Test
  151. public void nullPrefixedZipContainsRootDir() throws IOException {
  152. File zipFile = tempDir.newFile("zip-null-prefix.zip");
  153. ZipUtils.zipDir(zipFile, sourceZipDir, null);
  154. try (ZipFile zip = new ZipFile(zipFile)) {
  155. final Enumeration<? extends ZipEntry> entries = zip.entries();
  156. while (entries.hasMoreElements()) {
  157. final ZipEntry zipEntry = entries.nextElement();
  158. String zipPath = zipEntry.getName();
  159. String rootPath = zipPath.substring(0, zipPath.indexOf("/"));
  160. assertEquals(ROOT_DIR, rootPath);
  161. }
  162. }
  163. }
  164. @Test
  165. public void emptyPrefixedZipFolderCountMatches() throws IOException {
  166. File zipFile = tempDir.newFile("zip-empty-prefix.zip");
  167. ZipUtils.zipDir(zipFile, sourceZipDir, "");
  168. try (ZipFile zip = new ZipFile(zipFile)) {
  169. final Enumeration<? extends ZipEntry> entries = zip.entries();
  170. int numFolders = 0;
  171. while (entries.hasMoreElements()) {
  172. final ZipEntry zipEntry = entries.nextElement();
  173. if (zipEntry.isDirectory()) {
  174. numFolders++;
  175. }
  176. }
  177. assertEquals(NUM_FOLDERS, numFolders);
  178. }
  179. }
  180. @Test
  181. public void singlePrefixedZipFolderCountMatches() throws IOException {
  182. File zipFile = tempDir.newFile("zip-single-prefix.zip");
  183. ZipUtils.zipDir(zipFile, sourceZipDir, FIRST_PREFIX);
  184. try (ZipFile zip = new ZipFile(zipFile)) {
  185. final Enumeration<? extends ZipEntry> entries = zip.entries();
  186. int numFolders = 0;
  187. while (entries.hasMoreElements()) {
  188. final ZipEntry zipEntry = entries.nextElement();
  189. if (zipEntry.isDirectory()) {
  190. numFolders++;
  191. }
  192. }
  193. assertEquals(NUM_FOLDERS, numFolders);
  194. }
  195. }
  196. @Test
  197. public void nestedPrefixedZipFolderCountMatches() throws IOException {
  198. File zipFile = tempDir.newFile("zip-nested-prefix.zip");
  199. ZipUtils.zipDir(zipFile, sourceZipDir, NESTED_PREFIX);
  200. try (ZipFile zip = new ZipFile(zipFile)) {
  201. final Enumeration<? extends ZipEntry> entries = zip.entries();
  202. int numFolders = 0;
  203. while (entries.hasMoreElements()) {
  204. final ZipEntry zipEntry = entries.nextElement();
  205. if (zipEntry.isDirectory()) {
  206. numFolders++;
  207. }
  208. }
  209. assertEquals(NUM_FOLDERS_NESTED_PREFIX, numFolders);
  210. }
  211. }
  212. @Test
  213. public void zipFileCountMatches() throws IOException {
  214. File zipFile = tempDir.newFile("zip-single-prefix.zip");
  215. ZipUtils.zipDir(zipFile, sourceZipDir, FIRST_PREFIX);
  216. try (ZipFile zip = new ZipFile(zipFile)) {
  217. final Enumeration<? extends ZipEntry> entries = zip.entries();
  218. int numFiles = 0;
  219. while (entries.hasMoreElements()) {
  220. final ZipEntry zipEntry = entries.nextElement();
  221. if (!zipEntry.isDirectory()) {
  222. numFiles++;
  223. }
  224. }
  225. assertEquals(NUM_FILES, numFiles);
  226. }
  227. }
  228. @Test
  229. public void unzipNonPrefixed() throws IOException {
  230. File zipFile = tempDir.newFile("zip-empty-prefix.zip");
  231. ZipUtils.zipDir(zipFile, sourceZipDir, "");
  232. File unzipDir = tempDir.newFolder("unzip-empty-prefix");
  233. ZipUtils.unzip(zipFile, unzipDir.getAbsolutePath());
  234. File rootUnzip = new File(unzipDir, ROOT_DIR);
  235. assertTrue("root folder in zip was not unzipped", (rootUnzip.exists() && rootUnzip.isDirectory()));
  236. }
  237. @Test
  238. public void unzipSinglePrefix() throws IOException {
  239. File zipFile = tempDir.newFile("zip-single-prefix.zip");
  240. ZipUtils.zipDir(zipFile, sourceZipDir, FIRST_PREFIX);
  241. File unzipDir = tempDir.newFolder("unzip-single-prefix");
  242. ZipUtils.unzip(zipFile, unzipDir.getAbsolutePath());
  243. File rootUnzip = new File(unzipDir, FIRST_PREFIX);
  244. assertTrue("single prefix folder in zip was not unzipped", (rootUnzip.exists() && rootUnzip.isDirectory()));
  245. }
  246. @Test
  247. public void unzipNestedPrefix() throws IOException {
  248. File zipFile = tempDir.newFile("zip-nested-prefix.zip");
  249. ZipUtils.zipDir(zipFile, sourceZipDir, NESTED_PREFIX);
  250. File unzipDir = tempDir.newFolder("unzip-nested-prefix");
  251. ZipUtils.unzip(zipFile, unzipDir.getAbsolutePath());
  252. File rootUnzip = new File(unzipDir, FIRST_PREFIX);
  253. File nestedUnzip = new File(rootUnzip, SECOND_PREFIX);
  254. assertTrue("nested prefix folder in zip was not unzipped", (nestedUnzip.exists() && nestedUnzip.isDirectory()));
  255. }
  256. @Test
  257. public void detectPrefix() throws IOException {
  258. File zipFile = tempDir.newFile("zip-single-prefix.zip");
  259. // zipDir will use the foldername as a prefix
  260. ZipUtils.zipDir(zipFile, sourceZipDir, "");
  261. int nestedRoots = ZipUtils.countNestingLevel(zipFile);
  262. assertEquals("One level of nesting should be detected", 1, nestedRoots);
  263. }
  264. @Test
  265. public void detectDoublePrefix() throws IOException {
  266. File zipFile = tempDir.newFile("zip-double-prefix.zip");
  267. ZipUtils.zipDir(zipFile, sourceZipDir, NESTED_PREFIX);
  268. int nestedRoots = ZipUtils.countNestingLevel(zipFile);
  269. assertEquals("Two levels of nesting should be detected", 2, nestedRoots);
  270. }
  271. @Test
  272. public void detectNoPrefix() throws IOException, URISyntaxException {
  273. // zip-no-root.zip is a zip with no root folder.
  274. // We can't use ZipUtils#zipDir() to create it (zipDir() always puts a root folder),
  275. // so we need to provide one in src/test/resources.
  276. URL zipPath = TestZipUtils.class.getResource("zip-no-root.zip");
  277. File zipFile = new File(zipPath.toURI());
  278. int nestedRoots = ZipUtils.countNestingLevel(zipFile);
  279. assertEquals("No nesting should be detected", 0, nestedRoots);
  280. }
  281. @Test
  282. public void unzipSinglePrefixTrimmed() throws IOException {
  283. File zipFile = tempDir.newFile("zip-single-prefix.zip");
  284. ZipUtils.zipDir(zipFile, sourceZipDir, FIRST_PREFIX);
  285. File unzipDir = tempDir.newFolder("unzip-single-prefix");
  286. ZipUtils.unzip(zipFile, unzipDir.getAbsolutePath(), 1);
  287. File rootUnzip = new File(unzipDir, FIRST_PREFIX);
  288. assertTrue("single prefix folder in zip should have been trimmed", !rootUnzip.exists());
  289. }
  290. @Test
  291. public void unzipNestedPrefixTrimmed() throws IOException {
  292. File zipFile = tempDir.newFile("zip-nested-prefix.zip");
  293. ZipUtils.zipDir(zipFile, sourceZipDir, NESTED_PREFIX);
  294. File unzipDir = tempDir.newFolder("unzip-nested-prefix");
  295. ZipUtils.unzip(zipFile, unzipDir.getAbsolutePath(), 2);
  296. File nestedUnzip = new File(unzipDir, SECOND_PREFIX);
  297. assertTrue("nested prefix folder in zip should have been trimmed", !nestedUnzip.exists());
  298. }
  299. @Test
  300. public void unzipAndFlatten() throws IOException {
  301. File zipFile = tempDir.newFile("zip-flatten.zip");
  302. ZipUtils.zipDir(zipFile, sourceZipDir, "");
  303. File unzipDir = tempDir.newFolder("unzip-flatten");
  304. ZipUtils.unzip(zipFile, unzipDir.getAbsolutePath(), 0, true, null);
  305. File level2TextFile = new File(unzipDir, "level2sub2.txt");
  306. File level3TextFile = new File(unzipDir, "level3sub1.txt");
  307. assertTrue(level2TextFile.exists());
  308. assertTrue(level3TextFile.exists());
  309. }
  310. @Test
  311. public void unzipPatternShouldMatch() throws IOException {
  312. File zipFile = tempDir.newFile("zip-pattern.zip");
  313. ZipUtils.zipDir(zipFile, sourceZipDir, "");
  314. File unzipDir = tempDir.newFolder("unzip-pattern");
  315. ZipUtils.unzip(zipFile, unzipDir.getAbsolutePath(), 0, false, null);
  316. File level2TextFile = new File(unzipDir, "test-zip-dir/level2sub2/level2sub2.txt");
  317. File level3TextFile = new File(unzipDir, "test-zip-dir/level2sub2/level3sub1/level3sub1.txt");
  318. assertTrue(level2TextFile.exists());
  319. assertTrue(level3TextFile.exists());
  320. unzipDir = tempDir.newFolder("unzip-pattern-2");
  321. ZipUtils.unzip(zipFile, unzipDir.getAbsolutePath(), 0, false, Pattern.compile(".+level2sub2.txt"));
  322. level2TextFile = new File(unzipDir, "test-zip-dir/level2sub2/level2sub2.txt");
  323. level3TextFile = new File(unzipDir, "test-zip-dir/level2sub2/level3sub1/level3sub1.txt");
  324. assertTrue(level2TextFile.exists());
  325. assertFalse(level3TextFile.exists());
  326. }
  327. @Test
  328. public void unzipExecutable() throws IOException {
  329. File zipFile = tempDir.newFile("zip-executable.zip");
  330. File executable = new File(sourceZipDir, "executable.sh");
  331. assertTrue(executable.createNewFile());
  332. // This won't work under Windows - not much we can do but ignore this test
  333. assumeTrue(executable.setExecutable(true));
  334. ZipUtils.zipDir(zipFile, sourceZipDir, "");
  335. File unzipDir = tempDir.newFolder("unzip-executable");
  336. ZipUtils.unzip(zipFile, unzipDir.getAbsolutePath(), 1);
  337. File nestedUnzip = new File(unzipDir, "executable.sh");
  338. assertTrue("Zip/Unzip should preserve executable permissions", nestedUnzip.canExecute());
  339. }
  340. @Test
  341. public void countNoNestingLevel() {
  342. List<String> filenames = Lists.newArrayList(
  343. "file1.txt",
  344. "file2.txt");
  345. int nestedRoots = ZipUtils.countNestingLevel(filenames);
  346. assertEquals("The number of nested roots should be detected", 0, nestedRoots);
  347. }
  348. @Test
  349. public void countOneNestingLevel() {
  350. List<String> filenames = Lists.newArrayList(
  351. "root/folder1/file.txt",
  352. "root/folder2/file.txt");
  353. int nestedRoots = ZipUtils.countNestingLevel(filenames);
  354. assertEquals("The number of nested roots should be detected", 1, nestedRoots);
  355. }
  356. @Test
  357. public void countNestingLevelWithEmptyList() {
  358. List<String> filenames = Lists.newArrayList();
  359. int nestedRoots = ZipUtils.countNestingLevel(filenames);
  360. assertEquals("Should work with an empty list", 0, nestedRoots);
  361. }
  362. @Test
  363. public void countTwoNestingLevel() {
  364. List<String> filenames = Lists.newArrayList(
  365. "root/otherRoot/file1.txt",
  366. "root/otherRoot/file2.txt");
  367. int nestedRoots = ZipUtils.countNestingLevel(filenames);
  368. assertEquals("The number of nested roots should be detected", 2, nestedRoots);
  369. }
  370. @Test
  371. public void countTwoNestingLevelWithEmptyDirs() {
  372. List<String> filenames = Lists.newArrayList(
  373. "root/",
  374. "root/otherRoot/",
  375. "root/otherRoot/file1.txt",
  376. "root/otherRoot/file2.txt");
  377. int nestedRoots = ZipUtils.countNestingLevel(filenames);
  378. assertEquals("The number of nested roots should be detected", 2, nestedRoots);
  379. }
  380. @Test
  381. public void countTwoNestingLevelWithEmptyDirsInReversedOrder() {
  382. List<String> filenames = Lists.newArrayList(
  383. "root/otherRoot/file1.txt",
  384. "root/otherRoot/file2.txt",
  385. "root/otherRoot/",
  386. "root/");
  387. int nestedRoots = ZipUtils.countNestingLevel(filenames);
  388. assertEquals("The number of nested roots should be detected", 2, nestedRoots);
  389. }
  390. @Test
  391. public void countOneNestingLevelWithEmptyDirs() {
  392. List<String> filenames = Lists.newArrayList(
  393. "root/folder1/file1.txt",
  394. "root/folder1/file2.txt",
  395. "root/folder1/",
  396. "root/folder2/",
  397. "root/");
  398. int nestedRoots = ZipUtils.countNestingLevel(filenames);
  399. assertEquals("The number of nested roots should be detected", 1, nestedRoots);
  400. }
  401. }