PageRenderTime 39ms CodeModel.GetById 11ms app.highlight 24ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://bitbucket.org/mmeinhold/amps
Java | 513 lines | 398 code | 106 blank | 9 comment | 22 complexity | 7f727918c98a3f0a50074a55b5e89720 MD5 | raw file
  1package com.atlassian.maven.plugins.amps.util;
  2
  3import org.apache.commons.io.FileUtils;
  4import org.junit.After;
  5import org.junit.Assume;
  6import org.junit.Before;
  7import org.junit.Test;
  8
  9import com.google.common.collect.Lists;
 10
 11import java.io.File;
 12import java.io.IOException;
 13import java.net.URISyntaxException;
 14import java.net.URL;
 15import java.util.Enumeration;
 16import java.util.List;
 17import java.util.UUID;
 18import java.util.zip.ZipEntry;
 19import java.util.zip.ZipFile;
 20
 21import static org.hamcrest.core.IsEqual.equalTo;
 22import static org.hamcrest.core.IsNot.not;
 23import static org.junit.Assert.assertEquals;
 24import static org.junit.Assert.assertThat;
 25import static org.junit.Assert.assertTrue;
 26
 27public class TestZipUtils
 28{
 29    public static final int NUM_FILES = 2;
 30    public static final int NUM_FOLDERS = 4;
 31    public static final int NUM_FOLDERS_NESTED_PREFIX = NUM_FOLDERS + 1;
 32
 33    public static final String ROOT_DIR = "test-zip-dir";
 34    public static final String FIRST_PREFIX = "prefix1";
 35    public static final String SECOND_PREFIX = "prefix2";
 36    public static final String NESTED_PREFIX = FIRST_PREFIX + "/" + SECOND_PREFIX;
 37
 38    private File tempDir;
 39    private File sourceZipDir;
 40    private ZipFile zip;
 41
 42    @Before
 43    public void ensureDirsExist() throws IOException
 44    {
 45
 46        // Create the temp dir
 47        final File sysTempDir = new File(System.getProperty("java.io.tmpdir"));
 48        String dirName = UUID.randomUUID().toString();
 49        tempDir = new File(sysTempDir, dirName);
 50        tempDir.mkdirs();
 51
 52        // Create our test source tree
 53        sourceZipDir = new File(tempDir, ROOT_DIR);
 54        File level2sub1 = new File(sourceZipDir, "level2sub1");
 55        File level2sub2 = new File(sourceZipDir, "level2sub2");
 56        File level3sub1 = new File(level2sub2, "level3sub1");
 57
 58        File level2TextFile = new File(level2sub2, "level2sub2.txt");
 59        File level3TextFile = new File(level3sub1, "level3sub1.txt");
 60
 61        level2sub1.mkdirs();
 62        level3sub1.mkdirs();
 63
 64        FileUtils.writeStringToFile(level2TextFile, "level2sub2");
 65        FileUtils.writeStringToFile(level3TextFile, "level3sub1");
 66    }
 67
 68    @After
 69    public void removeTempDir() throws IOException
 70    {
 71        // make sure zip is closed, else delete fails on windows
 72        if (zip != null)
 73        {
 74            try
 75            {
 76                zip.close();
 77            }
 78            catch (IOException e)
 79            {
 80                // ignore
 81            }
 82            zip = null;
 83        }
 84        FileUtils.deleteDirectory(tempDir);
 85    }
 86
 87    @Test
 88    public void zipContainsSinglePrefix() throws IOException
 89    {
 90        File zipFile = new File(tempDir, "zip-with-prefix.zip");
 91        ZipUtils.zipDir(zipFile, sourceZipDir, FIRST_PREFIX);
 92
 93        zip = new ZipFile(zipFile);
 94        final Enumeration<? extends ZipEntry> entries = zip.entries();
 95
 96        while (entries.hasMoreElements())
 97        {
 98            final ZipEntry zipEntry = entries.nextElement();
 99            String zipPath = zipEntry.getName();
100            String testPrefix = zipPath.substring(0, zipPath.indexOf("/"));
101
102            assertEquals(FIRST_PREFIX, testPrefix);
103        }
104    }
105
106    @Test
107    public void zipContainsNestedPrefix() throws IOException
108    {
109        File zipFile = new File(tempDir, "zip-nested-prefix.zip");
110        ZipUtils.zipDir(zipFile, sourceZipDir, NESTED_PREFIX);
111
112        zip = new ZipFile(zipFile);
113        final Enumeration<? extends ZipEntry> entries = zip.entries();
114
115        while (entries.hasMoreElements())
116        {
117            final ZipEntry zipEntry = entries.nextElement();
118            String zipPath = zipEntry.getName();
119            String[] segments = zipPath.split("/");
120            if (segments.length > 1)
121            {
122                String testPrefix = segments[0] + "/" + segments[1];
123
124                assertEquals(NESTED_PREFIX, testPrefix);
125            }
126        }
127    }
128
129    @Test
130    public void prefixedZipDoesNotContainRootDir() throws IOException
131    {
132        File zipFile = new File(tempDir, "zip-with-prefix-no-root.zip");
133        ZipUtils.zipDir(zipFile, sourceZipDir, FIRST_PREFIX);
134
135        zip = new ZipFile(zipFile);
136        final Enumeration<? extends ZipEntry> entries = zip.entries();
137
138        while (entries.hasMoreElements())
139        {
140            final ZipEntry zipEntry = entries.nextElement();
141            String zipPath = zipEntry.getName();
142            String[] segments = zipPath.split("/");
143            if (segments.length > 1)
144            {
145                String rootPath = segments[1];
146
147                assertThat(rootPath, not(equalTo(ROOT_DIR)));
148            }
149        }
150    }
151
152    @Test
153    public void nestedPrefixedZipDoesNotContainRootDir() throws IOException
154    {
155        File zipFile = new File(tempDir, "zip-nested-prefix-no-root.zip");
156        ZipUtils.zipDir(zipFile, sourceZipDir, NESTED_PREFIX);
157
158        zip = new ZipFile(zipFile);
159        final Enumeration<? extends ZipEntry> entries = zip.entries();
160
161        while (entries.hasMoreElements())
162        {
163            final ZipEntry zipEntry = entries.nextElement();
164            String zipPath = zipEntry.getName();
165            String[] segments = zipPath.split("/");
166            if (segments.length > 2)
167            {
168                String rootPath = segments[2];
169
170                assertThat(rootPath, not(equalTo(ROOT_DIR)));
171            }
172        }
173    }
174
175    @Test
176    public void emptyPrefixedZipContainsRootDir() throws IOException
177    {
178        File zipFile = new File(tempDir, "zip-empty-prefix.zip");
179        ZipUtils.zipDir(zipFile, sourceZipDir, "");
180
181        zip = new ZipFile(zipFile);
182        final Enumeration<? extends ZipEntry> entries = zip.entries();
183
184        while (entries.hasMoreElements())
185        {
186            final ZipEntry zipEntry = entries.nextElement();
187            String zipPath = zipEntry.getName();
188            String rootPath = zipPath.substring(0, zipPath.indexOf("/"));
189
190            assertEquals(ROOT_DIR, rootPath);
191        }
192    }
193
194    @Test
195    public void nullPrefixedZipContainsRootDir() throws IOException
196    {
197        File zipFile = new File(tempDir, "zip-null-prefix.zip");
198        ZipUtils.zipDir(zipFile, sourceZipDir, null);
199
200        zip = new ZipFile(zipFile);
201        final Enumeration<? extends ZipEntry> entries = zip.entries();
202
203        while (entries.hasMoreElements())
204        {
205            final ZipEntry zipEntry = entries.nextElement();
206            String zipPath = zipEntry.getName();
207            String rootPath = zipPath.substring(0, zipPath.indexOf("/"));
208
209            assertEquals(ROOT_DIR, rootPath);
210        }
211    }
212
213    @Test
214    public void emptyPrefixedZipFolderCountMatches() throws IOException
215    {
216        File zipFile = new File(tempDir, "zip-empty-prefix.zip");
217        ZipUtils.zipDir(zipFile, sourceZipDir, "");
218
219        zip = new ZipFile(zipFile);
220        final Enumeration<? extends ZipEntry> entries = zip.entries();
221
222        int numFolders = 0;
223
224        while (entries.hasMoreElements())
225        {
226            final ZipEntry zipEntry = entries.nextElement();
227            if (zipEntry.isDirectory())
228            {
229                numFolders++;
230            }
231        }
232
233        assertEquals(NUM_FOLDERS, numFolders);
234    }
235
236    @Test
237    public void singlePrefixedZipFolderCountMatches() throws IOException
238    {
239        File zipFile = new File(tempDir, "zip-single-prefix.zip");
240        ZipUtils.zipDir(zipFile, sourceZipDir, FIRST_PREFIX);
241
242        zip = new ZipFile(zipFile);
243        final Enumeration<? extends ZipEntry> entries = zip.entries();
244
245        int numFolders = 0;
246
247        while (entries.hasMoreElements())
248        {
249            final ZipEntry zipEntry = entries.nextElement();
250            if (zipEntry.isDirectory())
251            {
252                numFolders++;
253            }
254        }
255
256        assertEquals(NUM_FOLDERS, numFolders);
257    }
258
259    @Test
260    public void nestedPrefixedZipFolderCountMatches() throws IOException
261    {
262        File zipFile = new File(tempDir, "zip-nested-prefix.zip");
263        ZipUtils.zipDir(zipFile, sourceZipDir, NESTED_PREFIX);
264
265        zip = new ZipFile(zipFile);
266        final Enumeration<? extends ZipEntry> entries = zip.entries();
267
268        int numFolders = 0;
269
270        while (entries.hasMoreElements())
271        {
272            final ZipEntry zipEntry = entries.nextElement();
273            if (zipEntry.isDirectory())
274            {
275                numFolders++;
276            }
277        }
278
279        assertEquals(NUM_FOLDERS_NESTED_PREFIX, numFolders);
280    }
281
282    @Test
283    public void zipFileCountMatches() throws IOException
284    {
285        File zipFile = new File(tempDir, "zip-single-prefix.zip");
286        ZipUtils.zipDir(zipFile, sourceZipDir, FIRST_PREFIX);
287
288        zip = new ZipFile(zipFile);
289        final Enumeration<? extends ZipEntry> entries = zip.entries();
290
291        int numFiles = 0;
292
293        while (entries.hasMoreElements())
294        {
295            final ZipEntry zipEntry = entries.nextElement();
296            if (!zipEntry.isDirectory())
297            {
298                numFiles++;
299            }
300        }
301
302        assertEquals(NUM_FILES, numFiles);
303    }
304
305    @Test
306    public void unzipNonPrefixed() throws IOException
307    {
308        File zipFile = new File(tempDir, "zip-empty-prefix.zip");
309        ZipUtils.zipDir(zipFile, sourceZipDir, "");
310
311        File unzipDir = new File(tempDir, "unzip-empty-prefix");
312        ZipUtils.unzip(zipFile, unzipDir.getAbsolutePath());
313
314        File rootUnzip = new File(unzipDir, ROOT_DIR);
315
316        assertTrue("root folder in zip was not unzipped", (rootUnzip.exists() && rootUnzip.isDirectory()));
317    }
318
319    @Test
320    public void unzipSinglePrefix() throws IOException
321    {
322        File zipFile = new File(tempDir, "zip-single-prefix.zip");
323        ZipUtils.zipDir(zipFile, sourceZipDir, FIRST_PREFIX);
324
325        File unzipDir = new File(tempDir, "unzip-single-prefix");
326        ZipUtils.unzip(zipFile, unzipDir.getAbsolutePath());
327
328        File rootUnzip = new File(unzipDir, FIRST_PREFIX);
329
330        assertTrue("single prefix folder in zip was not unzipped", (rootUnzip.exists() && rootUnzip.isDirectory()));
331    }
332
333    @Test
334    public void unzipNestedPrefix() throws IOException
335    {
336        File zipFile = new File(tempDir, "zip-nested-prefix.zip");
337        ZipUtils.zipDir(zipFile, sourceZipDir, NESTED_PREFIX);
338
339        File unzipDir = new File(tempDir, "unzip-nested-prefix");
340        ZipUtils.unzip(zipFile, unzipDir.getAbsolutePath());
341
342        File rootUnzip = new File(unzipDir, FIRST_PREFIX);
343        File nestedUnzip = new File(rootUnzip, SECOND_PREFIX);
344
345        assertTrue("nested prefix folder in zip was not unzipped", (nestedUnzip.exists() && nestedUnzip.isDirectory()));
346    }
347
348    @Test
349    public void detectPrefix() throws IOException
350    {
351        File zipFile = new File(tempDir, "zip-single-prefix.zip");
352        // zipDir will use the foldername as a prefix
353        ZipUtils.zipDir(zipFile, sourceZipDir, "");
354
355        int nestedRoots = ZipUtils.countNestingLevel(zipFile);
356
357        assertEquals("One level of nesting should be detected", 1, nestedRoots);
358    }
359
360    @Test
361    public void detectDoublePrefix() throws IOException
362    {
363        File zipFile = new File(tempDir, "zip-double-prefix.zip");
364        ZipUtils.zipDir(zipFile, sourceZipDir, NESTED_PREFIX);
365
366        int nestedRoots = ZipUtils.countNestingLevel(zipFile);
367
368        assertEquals("Two levels of nesting should be detected", 2, nestedRoots);
369    }
370
371    @Test
372    public void detectNoPrefix() throws IOException, URISyntaxException
373    {
374        // zip-no-root.zip is a zip with no root folder.
375        // We can't use ZipUtils#zipDir() to create it (zipDir() always puts a root folder),
376        // so we need to provide one in src/test/resources.
377        URL zipPath = TestZipUtils.class.getResource("zip-no-root.zip");
378        File zipFile = new File(zipPath.toURI());
379
380        int nestedRoots = ZipUtils.countNestingLevel(zipFile);
381
382        assertEquals("No nesting should be detected", 0, nestedRoots);
383    }
384
385    @Test
386    public void unzipSinglePrefixTrimmed() throws IOException
387    {
388        File zipFile = new File(tempDir, "zip-single-prefix.zip");
389        ZipUtils.zipDir(zipFile, sourceZipDir, FIRST_PREFIX);
390
391        File unzipDir = new File(tempDir, "unzip-single-prefix");
392        ZipUtils.unzip(zipFile, unzipDir.getAbsolutePath(), 1);
393
394        File rootUnzip = new File(unzipDir, FIRST_PREFIX);
395
396        assertTrue("single prefix folder in zip should have been trimmed", !rootUnzip.exists());
397    }
398
399    @Test
400    public void unzipNestedPrefixTrimmed() throws IOException
401    {
402        File zipFile = new File(tempDir, "zip-nested-prefix.zip");
403        ZipUtils.zipDir(zipFile, sourceZipDir, NESTED_PREFIX);
404
405        File unzipDir = new File(tempDir, "unzip-nested-prefix");
406        ZipUtils.unzip(zipFile, unzipDir.getAbsolutePath(), 2);
407
408        File nestedUnzip = new File(unzipDir, SECOND_PREFIX);
409
410        assertTrue("nested prefix folder in zip should have been trimmed", !nestedUnzip.exists());
411    }
412
413    @Test
414    public void unzipExecutable() throws IOException
415    {
416        File zipFile = new File(tempDir, "zip-executable.zip");
417        File executable = new File(sourceZipDir, "executable.sh");
418        executable.createNewFile();
419
420        // This won't work under Windows - not much we can but ignore this test
421        Assume.assumeTrue(executable.setExecutable(true));
422
423        ZipUtils.zipDir(zipFile, sourceZipDir, "");
424
425        File unzipDir = new File(tempDir, "unzip-executable");
426        ZipUtils.unzip(zipFile, unzipDir.getAbsolutePath(), 1);
427
428        File nestedUnzip = new File(unzipDir, "executable.sh");
429
430        assertTrue("Zip/Unzip should preserve executable permissions", nestedUnzip.canExecute());
431    }
432
433    @Test
434    public void countNoNestingLevel()
435    {
436        List<String> filenames = Lists.newArrayList(
437                "file1.txt",
438                "file2.txt");
439
440        int nestedRoots = ZipUtils.countNestingLevel(filenames);
441        assertEquals("The number of nested roots should be detected", 0, nestedRoots);
442    }
443
444    @Test
445    public void countOneNestingLevel()
446    {
447        List<String> filenames = Lists.newArrayList(
448                "root/folder1/file.txt",
449                "root/folder2/file.txt");
450
451        int nestedRoots = ZipUtils.countNestingLevel(filenames);
452        assertEquals("The number of nested roots should be detected", 1, nestedRoots);
453    }
454
455    @Test
456    public void countNestingLevelWithEmptyList()
457    {
458        List<String> filenames = Lists.newArrayList();
459
460        int nestedRoots = ZipUtils.countNestingLevel(filenames);
461        assertEquals("Should work with an empty list", 0, nestedRoots);
462    }
463
464    @Test
465    public void countTwoNestingLevel()
466    {
467        List<String> filenames = Lists.newArrayList(
468                "root/otherRoot/file1.txt",
469                "root/otherRoot/file2.txt");
470        int nestedRoots = ZipUtils.countNestingLevel(filenames);
471        assertEquals("The number of nested roots should be detected", 2, nestedRoots);
472    }
473
474    @Test
475    public void countTwoNestingLevelWithEmptyDirs()
476    {
477        List<String> filenames = Lists.newArrayList(
478                "root/",
479                "root/otherRoot/",
480                "root/otherRoot/file1.txt",
481                "root/otherRoot/file2.txt");
482
483        int nestedRoots = ZipUtils.countNestingLevel(filenames);
484        assertEquals("The number of nested roots should be detected", 2, nestedRoots);
485    }
486
487    @Test
488    public void countTwoNestingLevelWithEmptyDirsInReversedOrder()
489    {
490        List<String> filenames = Lists.newArrayList(
491                "root/otherRoot/file1.txt",
492                "root/otherRoot/file2.txt",
493                "root/otherRoot/",
494                "root/");
495
496        int nestedRoots = ZipUtils.countNestingLevel(filenames);
497        assertEquals("The number of nested roots should be detected", 2, nestedRoots);
498    }
499
500    @Test
501    public void countOneNestingLevelWithEmptyDirs()
502    {
503        List<String> filenames = Lists.newArrayList(
504                "root/folder1/file1.txt",
505                "root/folder1/file2.txt",
506                "root/folder1/",
507                "root/folder2/",
508                "root/");
509
510        int nestedRoots = ZipUtils.countNestingLevel(filenames);
511        assertEquals("The number of nested roots should be detected", 1, nestedRoots);
512    }
513}