1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
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
106
107
108
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 }