View Javadoc
1   /*
2    * Copyright 2016 Codehaus.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.codehaus.plexus.archiver.tar;
17  
18  import java.io.File;
19  import org.codehaus.plexus.PlexusTestCase;
20  import org.codehaus.plexus.archiver.Archiver;
21  import org.codehaus.plexus.archiver.UnArchiver;
22  import org.codehaus.plexus.archiver.xz.XZArchiver;
23  import org.codehaus.plexus.util.FileUtils;
24  import static junit.framework.TestCase.assertEquals;
25  import static junit.framework.TestCase.assertTrue;
26  import static org.codehaus.plexus.PlexusTestCase.getTestFile;
27  
28  /**
29   * @author philip.lourandos
30   * @since 3.3
31   */
32  public class TarXzUnArchiverTest extends PlexusTestCase
33  {
34  
35      public void testExtract()
36          throws Exception
37      {
38          TarArchiver tarArchiver = (TarArchiver) lookup( Archiver.ROLE, "tar" );
39          tarArchiver.setLongfile( TarLongFileMode.posix );
40  
41          String fileName1 = "TarBZip2UnArchiverTest1.txt";
42          String fileName2 = "TarBZip2UnArchiverTest2.txt";
43          File file1InTar = getTestFile( "target/output/" + fileName1 );
44          File file2InTar = getTestFile( "target/output/" + fileName2 );
45          file1InTar.delete();
46          file2InTar.delete();
47  
48          assertFalse( file1InTar.exists() );
49          assertFalse( file2InTar.exists() );
50  
51          File testXZFile = getTestFile( "target/output/archive.tar.xz" );
52          if ( testXZFile.exists() )
53          {
54              FileUtils.fileDelete( testXZFile.getPath() );
55          }
56          assertFalse( testXZFile.exists() );
57  
58          tarArchiver.addFile( getTestFile( "src/test/resources/manifests/manifest1.mf" ), fileName1 );
59          tarArchiver.addFile( getTestFile( "src/test/resources/manifests/manifest2.mf" ), fileName2, 0664 );
60          tarArchiver.setDestFile( getTestFile( "target/output/archive.tar" ) );
61          tarArchiver.createArchive();
62  
63          XZArchiver xzArchiver = (XZArchiver) lookup( Archiver.ROLE, "xz" );
64  
65          xzArchiver.setDestFile( testXZFile );
66          xzArchiver.addFile( getTestFile( "target/output/archive.tar" ), "dontcare" );
67          xzArchiver.createArchive();
68  
69          assertTrue( testXZFile.exists() );
70  
71          TarXZUnArchiver tarXZUnArchiver = (TarXZUnArchiver) lookup( UnArchiver.ROLE, "tar.xz" );
72  
73          tarXZUnArchiver.setDestDirectory( getTestFile( "target/output" ) );
74          tarXZUnArchiver.setSourceFile( testXZFile );
75          tarXZUnArchiver.extract();
76  
77          assertTrue( file1InTar.exists() );
78          assertTrue( file2InTar.exists() );
79  
80          assertEquals( testXZFile, tarXZUnArchiver.getSourceFile() );
81      }
82  
83      public void testLookup() throws Exception
84      {
85          assertNotNull( lookup( UnArchiver.ROLE, "tar.xz" ) );
86      }
87  
88  }