View Javadoc
1   package org.codehaus.plexus.archiver;
2   
3   import java.io.BufferedInputStream;
4   import java.io.BufferedReader;
5   import java.io.File;
6   import java.io.FileInputStream;
7   import java.io.FileReader;
8   import java.util.Enumeration;
9   import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
10  import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
11  import org.codehaus.plexus.PlexusTestCase;
12  import org.codehaus.plexus.archiver.tar.TarArchiver;
13  import org.codehaus.plexus.archiver.tar.TarLongFileMode;
14  import org.codehaus.plexus.logging.Logger;
15  import org.codehaus.plexus.util.FileUtils;
16  
17  /**
18   * @author Erik Engstrom
19   */
20  public class DuplicateFilesTest
21      extends PlexusTestCase
22  {
23  
24      private static final File file1 = getTestFile( "src/test/resources/group-writable/foo.txt" );
25  
26      private static final File file2 = getTestFile( "src/test/resources/world-writable/foo.txt" );
27  
28      private static final File destination = getTestFile( "target/output/duplicateFiles" );
29  
30      public void setUp()
31          throws Exception
32      {
33          super.setUp();
34          getContainer().getLoggerManager().setThreshold( Logger.LEVEL_DEBUG );
35      }
36  
37      public void testZipArchiver()
38          throws Exception
39      {
40          Archiver archiver = (Archiver) lookup( Archiver.ROLE, "zip" );
41          archiver.setDuplicateBehavior( Archiver.DUPLICATES_SKIP );
42  
43          File archive = createArchive( archiver, "zip" );
44  
45          org.apache.commons.compress.archivers.zip.ZipFile zf =
46              new org.apache.commons.compress.archivers.zip.ZipFile( archive );
47  
48          Enumeration e = zf.getEntries();
49          int entryCount = 0;
50          while ( e.hasMoreElements() )
51          {
52              ZipArchiveEntry entry = (ZipArchiveEntry) e.nextElement();
53              System.out.println( entry.getName() );
54              entryCount++;
55          }
56          // Zip file should have 2 entries, 1 for the directory and one for foo.txt
57          assertEquals( 2, entryCount );
58          testArchive( archive, "zip" );
59      }
60  
61      public void testDirArchiver()
62          throws Exception
63      {
64          Archiver archiver = (Archiver) lookup( Archiver.ROLE, "dir" );
65          createArchive( archiver, "dir" );
66          testFinalFile( "target/output/duplicateFiles.dir/duplicateFiles/foo.txt" );
67  
68      }
69  
70      public void testTarArchiver()
71          throws Exception
72      {
73          TarArchiver archiver = (TarArchiver) lookup( Archiver.ROLE, "tar" );
74          archiver.setLongfile( TarLongFileMode.posix );
75          archiver.setDuplicateBehavior( Archiver.DUPLICATES_SKIP );
76  
77          File archive = createArchive( archiver, "tar" );
78          TarArchiveInputStream tis;
79  
80          tis = new TarArchiveInputStream( new BufferedInputStream( new FileInputStream( archive ) ) );
81          int entryCount = 0;
82          while ( ( tis.getNextEntry() ) != null )
83          {
84              entryCount++;
85          }
86          assertEquals( 1, entryCount );
87          testArchive( archive, "tar" );
88          tis.close();
89      }
90  
91      private File createArchive( Archiver archiver, String outputFileExt )
92          throws Exception
93      {
94          archiver.addFile( file1, "duplicateFiles/foo.txt" );
95          archiver.addFile( file2, "duplicateFiles/foo.txt" );
96  
97          // delete it if it exists to ensure it is actually empty
98          if ( destination.exists() )
99          {
100             destination.delete();
101         }
102 
103         File archive = getTestFile( "target/output/duplicateFiles." + outputFileExt );
104         if ( archive.exists() )
105         {
106             if ( archive.isDirectory() )
107             {
108                 FileUtils.deleteDirectory( archive );
109             }
110             else
111             {
112                 archive.delete();
113             }
114         }
115 
116         archiver.setDestFile( archive );
117         archiver.createArchive();
118         return archive;
119     }
120 
121     private void testArchive( File archive, String role )
122         throws Exception
123     {
124         // Check the content of the archive by extracting it
125 
126         UnArchiver unArchiver = (UnArchiver) lookup( UnArchiver.ROLE, role );
127         unArchiver.setSourceFile( archive );
128 
129         unArchiver.setDestDirectory( getTestFile( "target/output/" ) );
130         unArchiver.extract();
131 
132         assertTrue( destination.exists() );
133         assertTrue( destination.isDirectory() );
134         testFinalFile( "target/output/duplicateFiles/foo.txt" );
135     }
136 
137     private void testFinalFile( String path )
138         throws Exception
139     {
140         File outputFile = getTestFile( path );
141         assertTrue( outputFile.exists() );
142         BufferedReader reader = new BufferedReader( new FileReader( outputFile ) );
143         String firstLine = reader.readLine();
144         reader.close();
145         reader = new BufferedReader( new FileReader( file2 ) );
146         String expectedFirstLine = reader.readLine();
147         reader.close();
148         assertEquals( expectedFirstLine, firstLine );
149     }
150 
151 }