View Javadoc
1   /*
2    * The MIT License
3    *
4    * Copyright (c) 2004, The Codehaus
5    *
6    * Permission is hereby granted, free of charge, to any person obtaining a copy of
7    * this software and associated documentation files (the "Software"), to deal in
8    * the Software without restriction, including without limitation the rights to
9    * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
10   * of the Software, and to permit persons to whom the Software is furnished to do
11   * so, subject to the following conditions:
12   *
13   * The above copyright notice and this permission notice shall be included in all
14   * copies or substantial portions of the Software.
15   *
16   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22   * SOFTWARE.
23   */
24  package org.codehaus.plexus.archiver.snappy;
25  
26  import java.io.File;
27  import java.io.InputStream;
28  import java.nio.file.Files;
29  import java.util.Arrays;
30  import java.util.zip.ZipEntry;
31  import java.util.zip.ZipFile;
32  
33  import org.codehaus.plexus.archiver.Archiver;
34  import org.codehaus.plexus.archiver.BasePlexusArchiverTest;
35  import org.codehaus.plexus.archiver.exceptions.EmptyArchiveException;
36  import org.codehaus.plexus.archiver.zip.ZipArchiver;
37  import org.codehaus.plexus.util.FileUtils;
38  import org.codehaus.plexus.util.IOUtil;
39  import org.junit.jupiter.api.Test;
40  
41  import static org.junit.jupiter.api.Assertions.assertEquals;
42  import static org.junit.jupiter.api.Assertions.assertNotEquals;
43  import static org.junit.jupiter.api.Assertions.assertTrue;
44  import static org.junit.jupiter.api.Assertions.fail;
45  
46  /**
47   * Tests for the snappy archiver
48   */
49  class SnappyArchiverTest extends BasePlexusArchiverTest {
50  
51      @Test
52      void testCreateArchive() throws Exception {
53          ZipArchiver zipArchiver = (ZipArchiver) lookup(Archiver.class, "zip");
54          zipArchiver.addDirectory(getTestFile("src"));
55          zipArchiver.setDestFile(getTestFile("target/output/archiveForSnappy.zip"));
56          zipArchiver.createArchive();
57          SnappyArchiver archiver = (SnappyArchiver) lookup(Archiver.class, "snappy");
58          String[] inputFiles = new String[1];
59          inputFiles[0] = "archiveForSnappy.zip";
60          archiver.addDirectory(getTestFile("target/output"), inputFiles, null);
61          archiver.setDestFile(getTestFile("target/output/archive.snappy"));
62          archiver.createArchive();
63      }
64  
65      @Test
66      void testCreateEmptyArchive() throws Exception {
67          SnappyArchiver archiver = (SnappyArchiver) lookup(Archiver.class, "snappy");
68          archiver.setDestFile(getTestFile("target/output/empty.snappy"));
69          try {
70              archiver.createArchive();
71  
72              fail("Creating empty archive should throw EmptyArchiveException");
73          } catch (EmptyArchiveException ignore) {
74          }
75      }
76  
77      @Test
78      void testCreateResourceCollection() throws Exception {
79          final File pomFile = new File("pom.xml");
80          final File snappyFile = new File("target/output/pom.xml.snappy");
81          SnappyArchiver SnappyArchiver = (SnappyArchiver) lookup(Archiver.class, "snappy");
82          SnappyArchiver.setDestFile(snappyFile);
83          SnappyArchiver.addFile(pomFile, "pom.xml");
84          FileUtils.removePath(snappyFile.getPath());
85          SnappyArchiver.createArchive();
86  
87          System.out.println("Created: " + snappyFile.getAbsolutePath());
88  
89          final File zipFile = new File("target/output/pom.zip");
90          ZipArchiver zipArchiver = (ZipArchiver) lookup(Archiver.class, "zip");
91          zipArchiver.setDestFile(zipFile);
92          zipArchiver.addArchivedFileSet(snappyFile, "prfx/");
93          FileUtils.removePath(zipFile.getPath());
94          zipArchiver.createArchive();
95  
96          final ZipFile juZipFile = new ZipFile(zipFile);
97          final ZipEntry zipEntry = juZipFile.getEntry("prfx/target/output/pom.xml");
98          final InputStream archivePom = juZipFile.getInputStream(zipEntry);
99          final InputStream pom = Files.newInputStream(pomFile.toPath());
100 
101         assertTrue(Arrays.equals(IOUtil.toByteArray(pom), IOUtil.toByteArray(archivePom)));
102         archivePom.close();
103         pom.close();
104         juZipFile.close();
105     }
106 
107     /**
108      * Tests the Snappy archiver is forced set to true, and after that
109      * tests the behavior when the forced is set to false.
110      *
111      * @throws Exception
112      */
113     @Test
114     void testsnappyIsForcedBehaviour() throws Exception {
115         SnappyArchiver SnappyArchiver = (SnappyArchiver) createArchiver("snappy");
116 
117         assertTrue(SnappyArchiver.isSupportingForced());
118         SnappyArchiver.createArchive();
119 
120         final long creationTime = SnappyArchiver.getDestFile().lastModified();
121 
122         waitUntilNewTimestamp(SnappyArchiver.getDestFile(), creationTime);
123 
124         SnappyArchiver = (SnappyArchiver) createArchiver("snappy");
125 
126         SnappyArchiver.setForced(true);
127         SnappyArchiver.createArchive();
128 
129         final long firstRunTime = SnappyArchiver.getDestFile().lastModified();
130 
131         assertNotEquals(creationTime, firstRunTime);
132 
133         SnappyArchiver = (SnappyArchiver) createArchiver("snappy");
134 
135         SnappyArchiver.setForced(false);
136         SnappyArchiver.createArchive();
137 
138         final long secondRunTime = SnappyArchiver.getDestFile().lastModified();
139 
140         assertEquals(firstRunTime, secondRunTime);
141     }
142 }