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.gzip.GZipArchiver;
31  
32  /**
33   * @author Dan Tran
34   */
35  public class TarGZipUnArchiverTest
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 = "TarGZipUnArchiverTest1.txt";
46          String fileName2 = "TarGZipUnArchiverTest2.txt";
47          File file1InTar = getTestFile( "target/output/" + fileName1 );
48          File file2InTar = getTestFile( "target/output/" + fileName2 );
49          file1InTar.delete();
50          file2InTar.delete();
51  
52          tarArchiver.addFile( getTestFile( "src/test/resources/manifests/manifest1.mf" ), fileName1 );
53          tarArchiver.addFile( getTestFile( "src/test/resources/manifests/manifest2.mf" ), fileName2, 0664 );
54          tarArchiver.setDestFile( getTestFile( "target/output/archive.tar" ) );
55          tarArchiver.createArchive();
56  
57          GZipArchiver gzipArchiver = (GZipArchiver) lookup( Archiver.ROLE, "gzip" );
58  
59          File testGZipFile = getTestFile( "target/output/archive.tar.gz" );
60          gzipArchiver.setDestFile( testGZipFile );
61          gzipArchiver.addFile( getTestFile( "target/output/archive.tar" ), "dontcare" );
62          gzipArchiver.createArchive();
63  
64          TarGZipUnArchiver tarGZipUnArchiver = (TarGZipUnArchiver) lookup( UnArchiver.ROLE, "tgz" );
65          tarGZipUnArchiver.setDestDirectory( getTestFile( "target/output" ) );
66          tarGZipUnArchiver.setSourceFile( testGZipFile );
67          tarGZipUnArchiver.extract();
68  
69          assertTrue( file1InTar.exists() );
70          assertTrue( file2InTar.exists() );
71  
72          //make sure we place the source file back
73          assertEquals( testGZipFile, tarGZipUnArchiver.getSourceFile() );
74  
75      }
76  
77      public void testLookup()
78          throws Exception
79      {
80          lookup( UnArchiver.ROLE, "tar.gz" );
81      }
82  
83  }