View Javadoc
1   package org.codehaus.plexus.archiver.tar;
2   
3   import java.io.File;
4   import java.io.FileWriter;
5   import java.io.IOException;
6   import java.util.ArrayList;
7   import java.util.List;
8   import org.codehaus.plexus.PlexusTestCase;
9   import org.codehaus.plexus.archiver.Archiver;
10  import org.codehaus.plexus.archiver.UnArchiver;
11  import org.codehaus.plexus.archiver.util.DefaultArchivedFileSet;
12  import org.codehaus.plexus.components.io.attributes.PlexusIoResourceAttributeUtils;
13  import org.codehaus.plexus.components.io.attributes.PlexusIoResourceAttributes;
14  import org.codehaus.plexus.util.FileUtils;
15  import org.codehaus.plexus.util.IOUtil;
16  import org.codehaus.plexus.util.Os;
17  
18  @SuppressWarnings( "ResultOfMethodCallIgnored" )
19  public class TarFileAttributesTest
20      extends PlexusTestCase
21  {
22  
23      private final List<File> toDelete = new ArrayList<File>();
24  
25      @Override
26      public void setUp()
27          throws Exception
28      {
29          super.setUp();
30  
31          System.out.println( "Octal 0660 is decimal " + 0660 );
32          System.out.println( "Octal 0644 is decimal " + 0644 );
33          System.out.println( "Octal 0440 is decimal " + 0440 );
34      }
35  
36      @Override
37      public void tearDown()
38          throws Exception
39      {
40          super.tearDown();
41  
42          if ( !toDelete.isEmpty() )
43          {
44              for ( File f : toDelete )
45              {
46                  System.out.println( "Deleting: " + f );
47                  if ( f.isDirectory() )
48                  {
49                      try
50                      {
51                          FileUtils.deleteDirectory( f );
52                      }
53                      catch ( IOException e )
54                      {
55                          System.out.println( "Error deleting test directory: " + f );
56                      }
57                  }
58                  else
59                  {
60                      f.delete();
61                      f.deleteOnExit();
62                  }
63              }
64          }
65      }
66  
67      private void printTestHeader()
68      {
69          StackTraceElement e = new Throwable().getStackTrace()[1];
70          System.out.println( "\n\nRunning: " + e.getMethodName() + "\n\n" );
71      }
72  
73      public void testUseAttributesFromTarArchiveInputInTarArchiverOutput()
74          throws Exception
75      {
76          printTestHeader();
77          if ( checkForWindows() )
78          {
79              System.out.println( "This test cannot run on windows. Aborting." );
80              return;
81          }
82  
83          File tempFile = File.createTempFile( "tar-file-attributes.", ".tmp" );
84          toDelete.add( tempFile );
85  
86          FileWriter writer = null;
87          try
88          {
89              writer = new FileWriter( tempFile );
90              writer.write( "This is a test file." );
91              writer.close();
92              writer = null;
93          }
94          finally
95          {
96              IOUtil.close( writer );
97          }
98  
99          int result = Runtime.getRuntime().exec( "chmod 440 " + tempFile.getAbsolutePath() ).waitFor();
100         assertEquals( 0, result );
101 
102         TarArchiver tarArchiver = getPosixCompliantTarArchiver();
103 
104         File tempTarFile = File.createTempFile( "tar-file.", ".tar" );
105         toDelete.add( tempTarFile );
106 
107         tarArchiver.setDestFile( tempTarFile );
108         tarArchiver.addFile( tempFile, tempFile.getName(), 0660 );
109 
110         tarArchiver.createArchive();
111 
112         TarArchiver tarArchiver2 = getPosixCompliantTarArchiver();
113 
114         File tempTarFile2 = File.createTempFile( "tar-file.", ".tar" );
115         toDelete.add( tempTarFile2 );
116 
117         tarArchiver2.setDestFile( tempTarFile2 );
118 
119         DefaultArchivedFileSet afs = new DefaultArchivedFileSet( tempTarFile );
120 
121         System.out.println( "Adding tar archive to new archiver: " + tempTarFile );
122         tarArchiver2.addArchivedFileSet( afs );
123 
124         tarArchiver2.createArchive();
125 
126         // Cut from here, and feed it into a new tar archiver...then unarchive THAT.
127         TarUnArchiver tarUnArchiver = (TarUnArchiver) lookup( UnArchiver.ROLE, "tar" );
128 
129         File tempTarDir = File.createTempFile( "tar-test.", ".dir" );
130         tempTarDir.delete();
131         tempTarDir.mkdirs();
132 
133         toDelete.add( tempTarDir );
134 
135         tarUnArchiver.setDestDirectory( tempTarDir );
136         tarUnArchiver.setSourceFile( tempTarFile2 );
137 
138         tarUnArchiver.extract();
139 
140         PlexusIoResourceAttributes fileAttributes =
141             PlexusIoResourceAttributeUtils.getFileAttributes( new File( tempTarDir, tempFile.getName() ) );
142 
143         assertEquals( 0660, fileAttributes.getOctalMode() );
144 
145     }
146 
147     public void testUseDetectedFileAttributes()
148         throws Exception
149     {
150         printTestHeader();
151         if ( checkForWindows() )
152         {
153             System.out.println( "This test cannot run on windows. Aborting." );
154             return;
155         }
156 
157         File tempFile = File.createTempFile( "tar-file-attributes.", ".tmp" );
158         toDelete.add( tempFile );
159 
160         FileWriter writer = null;
161         try
162         {
163             writer = new FileWriter( tempFile );
164             writer.write( "This is a test file." );
165             writer.close();
166             writer = null;
167         }
168         finally
169         {
170             IOUtil.close( writer );
171         }
172 
173         int result = Runtime.getRuntime().exec( "chmod 440 " + tempFile.getAbsolutePath() ).waitFor();
174 
175         assertEquals( 0, result );
176 
177         PlexusIoResourceAttributes fileAttributes = PlexusIoResourceAttributeUtils.getFileAttributes( tempFile );
178 
179         assertEquals( 0440, fileAttributes.getOctalMode() );
180 
181         TarArchiver tarArchiver = getPosixCompliantTarArchiver();
182 
183         File tempTarFile = File.createTempFile( "tar-file.", ".tar" );
184         toDelete.add( tempTarFile );
185 
186         tarArchiver.setDestFile( tempTarFile );
187         tarArchiver.addFile( tempFile, tempFile.getName() );
188 
189         tarArchiver.createArchive();
190 
191         TarUnArchiver tarUnArchiver = (TarUnArchiver) lookup( UnArchiver.ROLE, "tar" );
192 
193         File tempTarDir = File.createTempFile( "tar-test.", ".dir" );
194         tempTarDir.delete();
195         tempTarDir.mkdirs();
196 
197         toDelete.add( tempTarDir );
198 
199         tarUnArchiver.setDestDirectory( tempTarDir );
200         tarUnArchiver.setSourceFile( tempTarFile );
201 
202         tarUnArchiver.extract();
203 
204         fileAttributes = PlexusIoResourceAttributeUtils.getFileAttributes( new File( tempTarDir, tempFile.getName() ) );
205 
206         assertEquals( 0440, fileAttributes.getOctalMode() );
207 
208     }
209 
210     private boolean checkForWindows()
211     {
212         return Os.isFamily( Os.FAMILY_WINDOWS );
213     }
214 
215     public void testOverrideDetectedFileAttributes()
216         throws Exception
217     {
218         printTestHeader();
219 
220         if ( checkForWindows() )
221         {
222             System.out.println( "This test cannot run on windows. Aborting." );
223             return;
224         }
225 
226         File tempFile = File.createTempFile( "tar-file-attributes.", ".tmp" );
227         toDelete.add( tempFile );
228 
229         FileWriter writer = null;
230         try
231         {
232             writer = new FileWriter( tempFile );
233             writer.write( "This is a test file." );
234             writer.close();
235             writer = null;
236         }
237         finally
238         {
239             IOUtil.close( writer );
240         }
241 
242         int result = Runtime.getRuntime().exec( "chmod 440 " + tempFile.getAbsolutePath() ).waitFor();
243         assertEquals( 0, result );
244 
245         TarArchiver tarArchiver = getPosixCompliantTarArchiver();
246 
247         File tempTarFile = File.createTempFile( "tar-file.", ".tar" );
248         toDelete.add( tempTarFile );
249 
250         tarArchiver.setDestFile( tempTarFile );
251         tarArchiver.addFile( tempFile, tempFile.getName(), 0660 );
252 
253         tarArchiver.createArchive();
254 
255         TarUnArchiver tarUnArchiver = (TarUnArchiver) lookup( UnArchiver.ROLE, "tar" );
256 
257         File tempTarDir = File.createTempFile( "tar-test.", ".dir" );
258         tempTarDir.delete();
259         tempTarDir.mkdirs();
260 
261         toDelete.add( tempTarDir );
262 
263         tarUnArchiver.setDestDirectory( tempTarDir );
264         tarUnArchiver.setSourceFile( tempTarFile );
265 
266         tarUnArchiver.extract();
267 
268         PlexusIoResourceAttributes fileAttributes =
269             PlexusIoResourceAttributeUtils.getFileAttributes( new File( tempTarDir, tempFile.getName() ) );
270 
271         assertEquals( 0660, fileAttributes.getOctalMode() );
272 
273     }
274 
275     private TarArchiver getPosixCompliantTarArchiver() throws Exception
276     {
277         TarArchiver tarArchiver = (TarArchiver) lookup( Archiver.ROLE, "tar" );
278         tarArchiver.setLongfile( TarLongFileMode.posix );
279         return tarArchiver;
280     }
281 
282     public void testOverrideDetectedFileAttributesUsingFileMode()
283         throws Exception
284     {
285         printTestHeader();
286         if ( checkForWindows() )
287         {
288             System.out.println( "This test cannot run on windows. Aborting." );
289             return;
290         }
291 
292         File tempFile = File.createTempFile( "tar-file-attributes.", ".tmp" );
293         toDelete.add( tempFile );
294 
295         FileWriter writer = null;
296         try
297         {
298             writer = new FileWriter( tempFile );
299             writer.write( "This is a test file." );
300             writer.close();
301             writer = null;
302         }
303         finally
304         {
305             IOUtil.close( writer );
306         }
307 
308         int result = Runtime.getRuntime().exec( "chmod 440 " + tempFile.getAbsolutePath() ).waitFor();
309         assertEquals( 0, result );
310 
311         TarArchiver tarArchiver = getPosixCompliantTarArchiver();
312 
313         File tempTarFile = File.createTempFile( "tar-file.", ".tar" );
314         toDelete.add( tempTarFile );
315 
316         tarArchiver.setDestFile( tempTarFile );
317         tarArchiver.setFileMode( 0660 );
318         tarArchiver.addFile( tempFile, tempFile.getName() );
319 
320         tarArchiver.createArchive();
321 
322         TarUnArchiver tarUnArchiver = (TarUnArchiver) lookup( UnArchiver.ROLE, "tar" );
323 
324         File tempTarDir = File.createTempFile( "tar-test.", ".dir" );
325         tempTarDir.delete();
326         tempTarDir.mkdirs();
327 
328         toDelete.add( tempTarDir );
329 
330         tarUnArchiver.setDestDirectory( tempTarDir );
331         tarUnArchiver.setSourceFile( tempTarFile );
332 
333         tarUnArchiver.extract();
334 
335         PlexusIoResourceAttributes fileAttributes =
336             PlexusIoResourceAttributeUtils.getFileAttributes( new File( tempTarDir, tempFile.getName() ) );
337 
338         assertEquals( 0660, fileAttributes.getOctalMode() );
339 
340     }
341 
342 }