View Javadoc
1   package org.codehaus.plexus.archiver;
2   
3   import java.io.File;
4   import java.io.IOException;
5   import java.nio.file.Files;
6   import org.codehaus.plexus.PlexusTestCase;
7   import org.codehaus.plexus.archiver.dir.DirectoryArchiver;
8   import org.codehaus.plexus.archiver.tar.TarArchiver;
9   import org.codehaus.plexus.archiver.tar.TarLongFileMode;
10  import org.codehaus.plexus.archiver.tar.TarUnArchiver;
11  import org.codehaus.plexus.archiver.zip.ZipArchiver;
12  import org.codehaus.plexus.archiver.zip.ZipUnArchiver;
13  import org.codehaus.plexus.util.Os;
14  
15  /**
16   * @author Kristian Rosenvold
17   */
18  public class SymlinkTest
19      extends PlexusTestCase
20  {
21  
22      public void testSymlinkDir()
23          throws IOException
24      {
25          if ( !Os.isFamily( Os.FAMILY_WINDOWS ) )
26          {
27              File dummyContent = getTestFile( "src/test/resources/symlinks/src/symDir" );
28              assertTrue( dummyContent.isDirectory() );
29              assertTrue( Files.isSymbolicLink( dummyContent.toPath() ) );
30          }
31      }
32  
33      public void testSymlinkDirWithSlash()
34          throws IOException
35      {
36          if ( !Os.isFamily( Os.FAMILY_WINDOWS ) )
37          {
38              File dummyContent = getTestFile( "src/test/resources/symlinks/src/symDir/" );
39              assertTrue( dummyContent.isDirectory() );
40              assertTrue( Files.isSymbolicLink( dummyContent.toPath() ) );
41          }
42      }
43  
44      public void testSymlinkFile()
45      {
46          if ( !Os.isFamily( Os.FAMILY_WINDOWS ) )
47          {
48              File dummyContent = getTestFile( "src/test/resources/symlinks/src/symR" );
49              assertFalse( dummyContent.isDirectory() );
50              assertTrue( Files.isSymbolicLink( dummyContent.toPath() ) );
51          }
52      }
53  
54      public void testSymlinkTar()
55          throws Exception
56      {
57          TarArchiver archiver = (TarArchiver) lookup( Archiver.ROLE, "tar" );
58          archiver.setLongfile( TarLongFileMode.posix );
59  
60          File dummyContent = getTestFile( "src/test/resources/symlinks/src" );
61          archiver.addDirectory( dummyContent );
62          final File archiveFile = new File( "target/output/symlinks.tar" );
63          archiver.setDestFile( archiveFile );
64          archiver.createArchive();
65          File output = getTestFile( "target/output/untaredSymlinks" );
66          output.mkdirs();
67          TarUnArchiver unarchiver = (TarUnArchiver) lookup( UnArchiver.ROLE, "tar" );
68          unarchiver.setSourceFile( archiveFile );
69          unarchiver.setDestFile( output );
70          unarchiver.extract();
71      }
72  
73      public void testSymlinkZip()
74          throws Exception
75      {
76          ZipArchiver archiver = (ZipArchiver) lookup( Archiver.ROLE, "zip" );
77  
78          File dummyContent = getTestFile( "src/test/resources/symlinks/src" );
79          archiver.addDirectory( dummyContent );
80          final File archiveFile = new File( "target/output/symlinks.zip" );
81          archiveFile.delete();
82          archiver.setDestFile( archiveFile );
83          archiver.createArchive();
84  
85          File output = getTestFile( "target/output/unzippedSymlinks" );
86          output.mkdirs();
87          ZipUnArchiver unarchiver = (ZipUnArchiver) lookup( UnArchiver.ROLE, "zip" );
88          unarchiver.setSourceFile( archiveFile );
89          unarchiver.setDestFile( output );
90          unarchiver.extract();
91      }
92  
93      public void testSymlinkDirArchiver()
94          throws Exception
95      {
96          if ( !Os.isFamily( Os.FAMILY_WINDOWS ) )
97          {
98              DirectoryArchiver archiver = (DirectoryArchiver) lookup( Archiver.ROLE, "dir" );
99  
100             File dummyContent = getTestFile( "src/test/resources/symlinks/src" );
101             archiver.addDirectory( dummyContent );
102             final File archiveFile = new File( "target/output/dirarchiver-symlink" );
103             archiveFile.mkdirs();
104             archiver.setDestFile( archiveFile );
105             archiver.createArchive();
106 
107             File symbolicLink = new File( "target/output/dirarchiver-symlink/symR" );
108             assertTrue( Files.isSymbolicLink( symbolicLink.toPath() ) );
109 
110             symbolicLink = new File( "target/output/dirarchiver-symlink/aDirWithALink/backOutsideToFileX" );
111             assertTrue( Files.isSymbolicLink( symbolicLink.toPath() ) );
112         }
113     }
114 
115 }