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.tar;
25  
26  import java.io.File;
27  
28  import org.codehaus.plexus.archiver.Archiver;
29  import org.codehaus.plexus.archiver.TestSupport;
30  import org.codehaus.plexus.archiver.UnArchiver;
31  import org.codehaus.plexus.archiver.bzip2.BZip2Archiver;
32  import org.junit.jupiter.api.Test;
33  
34  import static org.junit.jupiter.api.Assertions.assertEquals;
35  import static org.junit.jupiter.api.Assertions.assertTrue;
36  
37  /**
38   * @author Dan Tran
39   */
40  class TarBZip2UnArchiverTest extends TestSupport {
41  
42      @Test
43      void testExtract() throws Exception {
44          TarArchiver tarArchiver = (TarArchiver) lookup(Archiver.class, "tar");
45          tarArchiver.setLongfile(TarLongFileMode.posix);
46  
47          String fileName1 = "TarBZip2UnArchiverTest1.txt";
48          String fileName2 = "TarBZip2UnArchiverTest2.txt";
49          File file1InTar = getTestFile("target/output/" + fileName1);
50          File file2InTar = getTestFile("target/output/" + fileName2);
51          file1InTar.delete();
52          file2InTar.delete();
53  
54          File testBZip2File = getTestFile("target/output/archive.tar.bz2");
55  
56          tarArchiver.addFile(getTestFile("src/test/resources/manifests/manifest1.mf"), fileName1);
57          tarArchiver.addFile(getTestFile("src/test/resources/manifests/manifest2.mf"), fileName2, 0664);
58          tarArchiver.setDestFile(getTestFile("target/output/archive.tar"));
59          tarArchiver.createArchive();
60  
61          BZip2Archiver bzip2Archiver = (BZip2Archiver) lookup(Archiver.class, "bzip2");
62  
63          bzip2Archiver.setDestFile(testBZip2File);
64          bzip2Archiver.addFile(getTestFile("target/output/archive.tar"), "dontcare");
65          bzip2Archiver.createArchive();
66  
67          TarBZip2UnArchiver tarBZip2UnArchiver = (TarBZip2UnArchiver) lookup(UnArchiver.class, "tbz2");
68  
69          tarBZip2UnArchiver.setDestDirectory(getTestFile("target/output"));
70          tarBZip2UnArchiver.setSourceFile(testBZip2File);
71          tarBZip2UnArchiver.extract();
72  
73          assertTrue(file1InTar.exists());
74          assertTrue(file2InTar.exists());
75  
76          // makesure we place the source file back
77          assertEquals(testBZip2File, tarBZip2UnArchiver.getSourceFile());
78      }
79  
80      @Test
81      void testLookup() throws Exception {
82          lookup(UnArchiver.class, "tar.bz2");
83      }
84  }