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.bzip2;
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.assertFalse;
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 BZip2ArchiverTest 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/archiveForbz2.zip"));
56          zipArchiver.createArchive();
57          BZip2Archiver archiver = (BZip2Archiver) lookup(Archiver.class, "bzip2");
58          String[] inputFiles = new String[1];
59          inputFiles[0] = "archiveForbz2.zip";
60          archiver.addDirectory(getTestFile("target/output"), inputFiles, null);
61          archiver.setDestFile(getTestFile("target/output/archive.bz2"));
62          archiver.createArchive();
63      }
64  
65      @Test
66      void testCreateEmptyArchive() throws Exception {
67          BZip2Archiver archiver = (BZip2Archiver) lookup(Archiver.class, "bzip2");
68          archiver.setDestFile(getTestFile("target/output/empty.bz2"));
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 bz2File = new File("target/output/pom.xml.bz2");
81          BZip2Archiver bzip2Archiver = (BZip2Archiver) lookup(Archiver.class, "bzip2");
82          bzip2Archiver.setDestFile(bz2File);
83          bzip2Archiver.addFile(pomFile, "pom.xml");
84          FileUtils.removePath(bz2File.getPath());
85          bzip2Archiver.createArchive();
86  
87          System.out.println("Created: " + bz2File.getAbsolutePath());
88  
89          final File zipFile = new File("target/output/pom.zip");
90          ZipArchiver zipArchiver = (ZipArchiver) lookup(Archiver.class, "zip");
91          zipArchiver.setDestFile(zipFile);
92          zipArchiver.addArchivedFileSet(bz2File, "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         assertTrue(Arrays.equals(IOUtil.toByteArray(pom), IOUtil.toByteArray(archivePom)));
102         archivePom.close();
103         pom.close();
104         juZipFile.close();
105     }
106 
107     /**
108      * Tests the .bzip2 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 testBz2IsForcedBehaviour() throws Exception {
115         BZip2Archiver bZip2Archiver = (BZip2Archiver) createArchiver("bzip2");
116 
117         assertTrue(bZip2Archiver.isSupportingForced());
118         bZip2Archiver.createArchive();
119 
120         final long creationTime = bZip2Archiver.getDestFile().lastModified();
121 
122         waitUntilNewTimestamp(bZip2Archiver.getDestFile(), creationTime);
123 
124         bZip2Archiver = (BZip2Archiver) createArchiver("bzip2");
125 
126         bZip2Archiver.setForced(true);
127         bZip2Archiver.createArchive();
128 
129         final long firstRunTime = bZip2Archiver.getDestFile().lastModified();
130 
131         assertFalse(creationTime == firstRunTime);
132 
133         bZip2Archiver = (BZip2Archiver) createArchiver("bzip2");
134 
135         bZip2Archiver.setForced(false);
136         bZip2Archiver.createArchive();
137 
138         final long secondRunTime = bZip2Archiver.getDestFile().lastModified();
139 
140         assertEquals(firstRunTime, secondRunTime);
141     }
142 }