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.zip.ZipEntry;
22  import java.util.zip.ZipFile;
23  
24  import org.codehaus.plexus.archiver.Archiver;
25  import org.codehaus.plexus.archiver.BasePlexusArchiverTest;
26  import org.codehaus.plexus.archiver.exceptions.EmptyArchiveException;
27  import org.codehaus.plexus.archiver.zip.ZipArchiver;
28  import org.codehaus.plexus.util.FileUtils;
29  import org.codehaus.plexus.util.IOUtil;
30  import org.junit.jupiter.api.Test;
31  
32  import static org.junit.jupiter.api.Assertions.assertArrayEquals;
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.assertNotSame;
36  import static org.junit.jupiter.api.Assertions.assertTrue;
37  import static org.junit.jupiter.api.Assertions.fail;
38  
39  class ZstdArchiverTest extends BasePlexusArchiverTest {
40  
41      @Test
42      void createArchive() throws Exception {
43          ZipArchiver zipArchiver = (ZipArchiver) lookup(Archiver.class, "zip");
44          zipArchiver.addDirectory(getTestFile("src"));
45          zipArchiver.setDestFile(getTestFile("target/output/archiveForxz.zip"));
46          zipArchiver.createArchive();
47  
48          ZstdArchiver archiver = (ZstdArchiver) lookup(Archiver.class, "zst");
49          String[] inputFiles = new String[1];
50          inputFiles[0] = "archiveForxz.zip";
51  
52          File targetOutputFile = getTestFile("target/output/archive.zst");
53          if (targetOutputFile.exists()) {
54              FileUtils.fileDelete(targetOutputFile.getPath());
55          }
56          assertFalse(targetOutputFile.exists());
57  
58          archiver.addDirectory(getTestFile("target/output"), inputFiles, null);
59          archiver.setDestFile(targetOutputFile);
60          archiver.createArchive();
61  
62          assertTrue(targetOutputFile.exists());
63      }
64  
65      @Test
66      void createEmptyArchive() throws Exception {
67          ZstdArchiver archiver = (ZstdArchiver) lookup(Archiver.class, "zst");
68          archiver.setDestFile(getTestFile("target/output/empty.zst"));
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 createResourceCollection() throws Exception {
79          final File pomFile = new File("pom.xml");
80          final File zstFile = new File("target/output/pom.xml.zst");
81          ZstdArchiver zstdArchiver = (ZstdArchiver) lookup(Archiver.class, "zst");
82          zstdArchiver.setDestFile(zstFile);
83          zstdArchiver.addFile(pomFile, "pom.xml");
84          FileUtils.removePath(zstFile.getPath());
85          zstdArchiver.createArchive();
86  
87          System.out.println("Created: " + zstFile.getAbsolutePath());
88  
89          final File zipFile = new File("target/output/pomxz.zip");
90          ZipArchiver zipArchiver = (ZipArchiver) lookup(Archiver.class, "zip");
91          zipArchiver.setDestFile(zipFile);
92          zipArchiver.addArchivedFileSet(zstFile, "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         assertArrayEquals(IOUtil.toByteArray(pom), IOUtil.toByteArray(archivePom));
102         archivePom.close();
103         pom.close();
104         juZipFile.close();
105     }
106 
107     /**
108      * Tests the .std 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 zstIsForcedBehaviour() throws Exception {
115         ZstdArchiver zstdArchiver = (ZstdArchiver) createArchiver("zst");
116 
117         assertTrue(zstdArchiver.isSupportingForced());
118         zstdArchiver.createArchive();
119 
120         final long creationTime = zstdArchiver.getDestFile().lastModified();
121 
122         waitUntilNewTimestamp(zstdArchiver.getDestFile(), creationTime);
123 
124         zstdArchiver = (ZstdArchiver) createArchiver("zst");
125 
126         zstdArchiver.setForced(true);
127         zstdArchiver.createArchive();
128 
129         final long firstRunTime = zstdArchiver.getDestFile().lastModified();
130 
131         assertNotSame(creationTime, firstRunTime);
132 
133         zstdArchiver = (ZstdArchiver) createArchiver("zst");
134 
135         zstdArchiver.setForced(false);
136         zstdArchiver.createArchive();
137 
138         final long secondRunTime = zstdArchiver.getDestFile().lastModified();
139 
140         assertEquals(firstRunTime, secondRunTime);
141     }
142 }