View Javadoc
1   package org.codehaus.plexus.archiver.tar;
2   
3   import java.io.File;
4   import java.io.FileInputStream;
5   import java.io.IOException;
6   import java.io.InputStream;
7   import java.util.Arrays;
8   import java.util.Enumeration;
9   import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
10  import org.codehaus.plexus.PlexusTestCase;
11  import org.codehaus.plexus.archiver.Archiver;
12  import org.codehaus.plexus.archiver.bzip2.BZip2Compressor;
13  import org.codehaus.plexus.archiver.gzip.GZipCompressor;
14  import org.codehaus.plexus.archiver.util.Compressor;
15  import org.codehaus.plexus.util.FileUtils;
16  import org.codehaus.plexus.util.IOUtil;
17  import static org.codehaus.plexus.components.io.resources.ResourceFactory.createResource;
18  
19  /**
20   * Test case for {@link TarFile}.
21   */
22  public class TarFileTest
23      extends PlexusTestCase
24  {
25  
26      private interface TarFileCreator
27      {
28  
29          TarFile newTarFile( File file ) throws IOException;
30  
31      }
32  
33      /**
34       * Test for the uncompressed tar file, {@link TarFile}.
35       */
36      public void testTarFile() throws Exception
37      {
38          testTarFile( null, null, new TarFileCreator()
39          {
40  
41              @Override
42              public TarFile newTarFile( File file )
43                  throws IOException
44              {
45                  return new TarFile( file );
46              }
47  
48          } );
49      }
50  
51      /**
52       * Test for the gzip compressed tar file, {@link GZipTarFile}.
53       */
54      public void testGZipTarFile() throws Exception
55      {
56          final GZipCompressor compressor = new GZipCompressor();
57          testTarFile( compressor, ".gz", new TarFileCreator()
58          {
59  
60              @Override
61              public TarFile newTarFile( File file )
62                  throws IOException
63              {
64                  return new GZipTarFile( file );
65              }
66  
67          } );
68      }
69  
70      /**
71       * Test for the bzip2 compressed tar file, {@link BZip2TarFile}.
72       */
73      public void testBZip2TarFile() throws Exception
74      {
75          final BZip2Compressor compressor = new BZip2Compressor();
76          testTarFile( compressor, ".bz2", new TarFileCreator()
77          {
78  
79              @Override
80              public TarFile newTarFile( File file )
81                  throws IOException
82              {
83                  return new BZip2TarFile( file );
84              }
85  
86          } );
87      }
88  
89      private void testTarFile( Compressor compressor, String extension,
90                                TarFileCreator tarFileCreator )
91          throws Exception
92      {
93          File file = new File( "target/output/TarFileTest.tar" );
94          final TarArchiver archiver = (TarArchiver) lookup( Archiver.ROLE, "tar" );
95          archiver.setLongfile( TarLongFileMode.posix );
96          archiver.setDestFile( file );
97          archiver.addDirectory( new File( "src" ) );
98          FileUtils.removePath( file.getPath() );
99          archiver.createArchive();
100         if ( compressor != null )
101         {
102             final File compressedFile = new File( file.getPath() + extension );
103             compressor.setSource( createResource( file, file.getName() ) );
104             compressor.setDestFile( compressedFile );
105             compressor.compress();
106             compressor.close();
107             file = compressedFile;
108         }
109         final TarFile tarFile = tarFileCreator.newTarFile( file );
110 
111         for ( Enumeration en = tarFile.getEntries(); en.hasMoreElements(); )
112         {
113             final TarArchiveEntry te = (TarArchiveEntry) en.nextElement();
114             if ( te.isDirectory() || te.isSymbolicLink() )
115             {
116                 continue;
117             }
118             final File teFile = new File( "src", te.getName() );
119             final InputStream teStream = tarFile.getInputStream( te );
120             final InputStream fileStream = new FileInputStream( teFile );
121             assertTrue( Arrays.equals( IOUtil.toByteArray( teStream ), IOUtil.toByteArray( fileStream ) ) );
122             teStream.close();
123             fileStream.close();
124         }
125 
126         tarFile.close();
127     }
128 
129 }