View Javadoc
1   package org.codehaus.plexus.archiver.tar;
2   
3   import java.io.File;
4   import org.codehaus.plexus.PlexusTestCase;
5   import org.codehaus.plexus.archiver.UnArchiver;
6   import org.codehaus.plexus.components.io.fileselectors.FileSelector;
7   import org.codehaus.plexus.components.io.fileselectors.IncludeExcludeFileSelector;
8   import org.codehaus.plexus.util.FileUtils;
9   
10  /**
11   * @author <a href="mailto:viktor@jv-ration.com">Viktor Sadovnikov</a>
12   */
13  public class TarUnArchiverTest extends PlexusTestCase
14  {
15  
16      private void runUnarchiver( FileSelector[] selectors, boolean[] results )
17          throws Exception
18      {
19          String s = "target/tar-unarchiver-tests";
20  
21          File testJar = new File( getBasedir(), "src/test/jars/test.tar.gz" );
22  
23          File outputDirectory = new File( getBasedir(), s );
24  
25          TarUnArchiver tarUn = (TarUnArchiver) lookup( UnArchiver.ROLE, "tar.gz" );
26          tarUn.setSourceFile( testJar );
27          tarUn.setDestDirectory( outputDirectory );
28          tarUn.setFileSelectors( selectors );
29  
30          FileUtils.deleteDirectory( outputDirectory );
31  
32          tarUn.extract( testJar.getAbsolutePath(), outputDirectory );
33  
34          assertFileExistance( s, "/resources/artifactId/test.properties", results[0] );
35          assertFileExistance( s, "/resources/artifactId/directory/test.properties", results[1] );
36          assertFileExistance( s, "/META-INF/MANIFEST.MF", results[2] );
37  
38      }
39  
40      private void assertFileExistance( String s, String file, boolean exists )
41      {
42          File f0 = new File( getBasedir(), s + file );
43          assertEquals( String.format( "Did %s expect to find %s file", exists ? "" : "NOT", f0.getAbsoluteFile() ),
44                        exists, f0.exists() );
45      }
46  
47      public void testExtractingADirectory() throws Exception
48      {
49          runUnarchiver( null,
50                         new boolean[]
51                         {
52                             true, true, true
53                         } );
54  
55      }
56  
57      public void testSelectors()
58          throws Exception
59      {
60          IncludeExcludeFileSelector fileSelector = new IncludeExcludeFileSelector();
61          runUnarchiver( new FileSelector[]
62          {
63              fileSelector
64          },
65                         new boolean[]
66                         {
67                             true, true, true
68                         } );
69  
70          fileSelector.setExcludes( new String[]
71          {
72              "**/test.properties"
73          } );
74          runUnarchiver( new FileSelector[]
75          {
76              fileSelector
77          },
78                         new boolean[]
79                         {
80                             false, false, true
81                         } );
82  
83          fileSelector.setIncludes( new String[]
84          {
85              "**/test.properties"
86          } );
87          fileSelector.setExcludes( null );
88          runUnarchiver( new FileSelector[]
89          {
90              fileSelector
91          },
92                         new boolean[]
93                         {
94                             true, true, false
95                         } );
96  
97          fileSelector.setExcludes( new String[]
98          {
99              "resources/artifactId/directory/test.properties"
100         } );
101         runUnarchiver( new FileSelector[]
102         {
103             fileSelector
104         },
105                        new boolean[]
106                        {
107                            true, false, false
108                        } );
109     }
110 
111 }