View Javadoc
1   /*
2    * Copyright 2016 Codehaus.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.codehaus.plexus.archiver.xz;
17  
18  import java.io.File;
19  import java.io.InputStream;
20  import java.nio.file.Files;
21  import java.util.Arrays;
22  import java.util.zip.ZipEntry;
23  import java.util.zip.ZipFile;
24  
25  import org.codehaus.plexus.archiver.Archiver;
26  import org.codehaus.plexus.archiver.BasePlexusArchiverTest;
27  import org.codehaus.plexus.archiver.exceptions.EmptyArchiveException;
28  import org.codehaus.plexus.archiver.zip.ZipArchiver;
29  import org.codehaus.plexus.util.FileUtils;
30  import org.codehaus.plexus.util.IOUtil;
31  import org.junit.jupiter.api.Test;
32  
33  import static org.junit.jupiter.api.Assertions.assertEquals;
34  import static org.junit.jupiter.api.Assertions.assertFalse;
35  import static org.junit.jupiter.api.Assertions.assertTrue;
36  import static org.junit.jupiter.api.Assertions.fail;
37  
38  /**
39   * @author philip.lourandos
40   * @since 3.3
41   */
42  class XzArchiverTest extends BasePlexusArchiverTest {
43  
44      @Test
45      void testCreateArchive() throws Exception {
46          ZipArchiver zipArchiver = (ZipArchiver) lookup(Archiver.class, "zip");
47          zipArchiver.addDirectory(getTestFile("src"));
48          zipArchiver.setDestFile(getTestFile("target/output/archiveForxz.zip"));
49          zipArchiver.createArchive();
50  
51          XZArchiver archiver = (XZArchiver) lookup(Archiver.class, "xz");
52          String[] inputFiles = new String[1];
53          inputFiles[0] = "archiveForxz.zip";
54  
55          File targetOutputFile = getTestFile("target/output/archive.xz");
56          if (targetOutputFile.exists()) {
57              FileUtils.fileDelete(targetOutputFile.getPath());
58          }
59          assertFalse(targetOutputFile.exists());
60  
61          archiver.addDirectory(getTestFile("target/output"), inputFiles, null);
62          archiver.setDestFile(targetOutputFile);
63          archiver.createArchive();
64  
65          assertTrue(targetOutputFile.exists());
66      }
67  
68      @Test
69      void testCreateEmptyArchive() throws Exception {
70          XZArchiver archiver = (XZArchiver) lookup(Archiver.class, "xz");
71          archiver.setDestFile(getTestFile("target/output/empty.xz"));
72          try {
73              archiver.createArchive();
74  
75              fail("Creating empty archive should throw EmptyArchiveException");
76          } catch (EmptyArchiveException ignore) {
77          }
78      }
79  
80      @Test
81      void testCreateResourceCollection() throws Exception {
82          final File pomFile = new File("pom.xml");
83          final File xzFile = new File("target/output/pom.xml.xz");
84          XZArchiver xzArchiver = (XZArchiver) lookup(Archiver.class, "xz");
85          xzArchiver.setDestFile(xzFile);
86          xzArchiver.addFile(pomFile, "pom.xml");
87          FileUtils.removePath(xzFile.getPath());
88          xzArchiver.createArchive();
89  
90          System.out.println("Created: " + xzFile.getAbsolutePath());
91  
92          final File zipFile = new File("target/output/pomxz.zip");
93          ZipArchiver zipArchiver = (ZipArchiver) lookup(Archiver.class, "zip");
94          zipArchiver.setDestFile(zipFile);
95          zipArchiver.addArchivedFileSet(xzFile, "prfx/");
96          FileUtils.removePath(zipFile.getPath());
97          zipArchiver.createArchive();
98  
99          final ZipFile juZipFile = new ZipFile(zipFile);
100         final ZipEntry zipEntry = juZipFile.getEntry("prfx/target/output/pom.xml");
101         final InputStream archivePom = juZipFile.getInputStream(zipEntry);
102         final InputStream pom = Files.newInputStream(pomFile.toPath());
103 
104         assertTrue(Arrays.equals(IOUtil.toByteArray(pom), IOUtil.toByteArray(archivePom)));
105         archivePom.close();
106         pom.close();
107         juZipFile.close();
108     }
109 
110     /**
111      * Tests the .xz archiver is forced set to true, and after that
112      * tests the behavior when the forced is set to false.
113      *
114      * @throws Exception
115      */
116     @Test
117     void testXzIsForcedBehaviour() throws Exception {
118         XZArchiver xzArchiver = (XZArchiver) createArchiver("xz");
119 
120         assertTrue(xzArchiver.isSupportingForced());
121         xzArchiver.createArchive();
122 
123         final long creationTime = xzArchiver.getDestFile().lastModified();
124 
125         waitUntilNewTimestamp(xzArchiver.getDestFile(), creationTime);
126 
127         xzArchiver = (XZArchiver) createArchiver("xz");
128 
129         xzArchiver.setForced(true);
130         xzArchiver.createArchive();
131 
132         final long firstRunTime = xzArchiver.getDestFile().lastModified();
133 
134         assertFalse(creationTime == firstRunTime);
135 
136         xzArchiver = (XZArchiver) createArchiver("xz");
137 
138         xzArchiver.setForced(false);
139         xzArchiver.createArchive();
140 
141         final long secondRunTime = xzArchiver.getDestFile().lastModified();
142 
143         assertEquals(firstRunTime, secondRunTime);
144     }
145 }