View Javadoc
1   /*
2    * Copyright 2022 The Apache Software Foundation
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.zstd;
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  class ZstdArchiverTest extends BasePlexusArchiverTest {
39  
40      @Test
41      void testCreateArchive() throws Exception {
42          ZipArchiver zipArchiver = (ZipArchiver) lookup(Archiver.class, "zip");
43          zipArchiver.addDirectory(getTestFile("src"));
44          zipArchiver.setDestFile(getTestFile("target/output/archiveForxz.zip"));
45          zipArchiver.createArchive();
46  
47          ZstdArchiver archiver = (ZstdArchiver) lookup(Archiver.class, "zst");
48          String[] inputFiles = new String[1];
49          inputFiles[0] = "archiveForxz.zip";
50  
51          File targetOutputFile = getTestFile("target/output/archive.zst");
52          if (targetOutputFile.exists()) {
53              FileUtils.fileDelete(targetOutputFile.getPath());
54          }
55          assertFalse(targetOutputFile.exists());
56  
57          archiver.addDirectory(getTestFile("target/output"), inputFiles, null);
58          archiver.setDestFile(targetOutputFile);
59          archiver.createArchive();
60  
61          assertTrue(targetOutputFile.exists());
62      }
63  
64      @Test
65      void testCreateEmptyArchive() throws Exception {
66          ZstdArchiver archiver = (ZstdArchiver) lookup(Archiver.class, "zst");
67          archiver.setDestFile(getTestFile("target/output/empty.zst"));
68          try {
69              archiver.createArchive();
70  
71              fail("Creating empty archive should throw EmptyArchiveException");
72          } catch (EmptyArchiveException ignore) {
73          }
74      }
75  
76      @Test
77      void testCreateResourceCollection() throws Exception {
78          final File pomFile = new File("pom.xml");
79          final File zstFile = new File("target/output/pom.xml.zst");
80          ZstdArchiver zstdArchiver = (ZstdArchiver) lookup(Archiver.class, "zst");
81          zstdArchiver.setDestFile(zstFile);
82          zstdArchiver.addFile(pomFile, "pom.xml");
83          FileUtils.removePath(zstFile.getPath());
84          zstdArchiver.createArchive();
85  
86          System.out.println("Created: " + zstFile.getAbsolutePath());
87  
88          final File zipFile = new File("target/output/pomxz.zip");
89          ZipArchiver zipArchiver = (ZipArchiver) lookup(Archiver.class, "zip");
90          zipArchiver.setDestFile(zipFile);
91          zipArchiver.addArchivedFileSet(zstFile, "prfx/");
92          FileUtils.removePath(zipFile.getPath());
93          zipArchiver.createArchive();
94  
95          final ZipFile juZipFile = new ZipFile(zipFile);
96          final ZipEntry zipEntry = juZipFile.getEntry("prfx/target/output/pom.xml");
97          final InputStream archivePom = juZipFile.getInputStream(zipEntry);
98          final InputStream pom = Files.newInputStream(pomFile.toPath());
99  
100         assertTrue(Arrays.equals(IOUtil.toByteArray(pom), IOUtil.toByteArray(archivePom)));
101         archivePom.close();
102         pom.close();
103         juZipFile.close();
104     }
105 
106     /**
107      * Tests the .std archiver is forced set to true, and after that
108      * tests the behavior when the forced is set to false.
109      *
110      * @throws Exception
111      */
112     @Test
113     void testZstIsForcedBehaviour() throws Exception {
114         ZstdArchiver zstdArchiver = (ZstdArchiver) createArchiver("zst");
115 
116         assertTrue(zstdArchiver.isSupportingForced());
117         zstdArchiver.createArchive();
118 
119         final long creationTime = zstdArchiver.getDestFile().lastModified();
120 
121         waitUntilNewTimestamp(zstdArchiver.getDestFile(), creationTime);
122 
123         zstdArchiver = (ZstdArchiver) createArchiver("zst");
124 
125         zstdArchiver.setForced(true);
126         zstdArchiver.createArchive();
127 
128         final long firstRunTime = zstdArchiver.getDestFile().lastModified();
129 
130         assertFalse(creationTime == firstRunTime);
131 
132         zstdArchiver = (ZstdArchiver) createArchiver("zst");
133 
134         zstdArchiver.setForced(false);
135         zstdArchiver.createArchive();
136 
137         final long secondRunTime = zstdArchiver.getDestFile().lastModified();
138 
139         assertEquals(firstRunTime, secondRunTime);
140     }
141 }