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