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.gzip;
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   * @author Emmanuel Venisse
48   */
49  class GZipArchiverTest 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/archiveForGzip.zip"));
56          zipArchiver.createArchive();
57          GZipArchiver archiver = (GZipArchiver) lookup(Archiver.class, "gzip");
58          String[] inputFiles = new String[1];
59          inputFiles[0] = "archiveForGzip.zip";
60          archiver.addDirectory(getTestFile("target/output"), inputFiles, null);
61          archiver.setDestFile(getTestFile("target/output/archive.gzip"));
62          archiver.createArchive();
63      }
64  
65      @Test
66      void testCreateEmptyArchive() throws Exception {
67          GZipArchiver archiver = (GZipArchiver) lookup(Archiver.class, "gzip");
68          archiver.setDestFile(getTestFile("target/output/empty.gz"));
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 gzFile = new File("target/output/pom.xml.gz");
81          GZipArchiver gzipArchiver = (GZipArchiver) lookup(Archiver.class, "gzip");
82          gzipArchiver.setDestFile(gzFile);
83          gzipArchiver.addFile(pomFile, "pom.xml");
84          FileUtils.removePath(gzFile.getPath());
85          gzipArchiver.createArchive();
86  
87          final File zipFile = new File("target/output/pom.zip");
88          ZipArchiver zipArchiver = (ZipArchiver) lookup(Archiver.class, "zip");
89          zipArchiver.setDestFile(zipFile);
90          zipArchiver.addArchivedFileSet(gzFile, "prfx/");
91          FileUtils.removePath(zipFile.getPath());
92          zipArchiver.createArchive();
93  
94          final ZipFile juZipFile = new ZipFile(zipFile);
95          final ZipEntry zipEntry = juZipFile.getEntry("prfx/target/output/pom.xml");
96          final InputStream archivePom = juZipFile.getInputStream(zipEntry);
97          final InputStream pom = Files.newInputStream(pomFile.toPath());
98          assertTrue(Arrays.equals(IOUtil.toByteArray(pom), IOUtil.toByteArray(archivePom)));
99          archivePom.close();
100         pom.close();
101         juZipFile.close();
102     }
103 
104     /**
105      * Tests the .gzip archiver is forced set to true, and after that
106      * tests the behavior when the forced is set to false.
107      *
108      * @throws Exception
109      */
110     @Test
111     void testTarGzIsForcedBehaviour() throws Exception {
112         GZipArchiver gZipArchiver = (GZipArchiver) createArchiver("gzip");
113 
114         assertTrue(gZipArchiver.isSupportingForced());
115         gZipArchiver.createArchive();
116 
117         final long creationTime = gZipArchiver.getDestFile().lastModified();
118 
119         waitUntilNewTimestamp(gZipArchiver.getDestFile(), creationTime);
120 
121         gZipArchiver = (GZipArchiver) createArchiver("gzip");
122 
123         gZipArchiver.setForced(true);
124         gZipArchiver.createArchive();
125 
126         final long firstRunTime = gZipArchiver.getDestFile().lastModified();
127 
128         assertNotEquals(creationTime, firstRunTime);
129 
130         gZipArchiver = (GZipArchiver) createArchiver("gzip");
131 
132         gZipArchiver.setForced(false);
133         gZipArchiver.createArchive();
134 
135         final long secondRunTime = gZipArchiver.getDestFile().lastModified();
136 
137         assertEquals(firstRunTime, secondRunTime);
138     }
139 }