View Javadoc
1   /*
2    * Copyright 2016 Plexus developers.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.codehaus.plexus.components.io.attributes;
18  
19  import org.apache.commons.io.FileUtils;
20  import org.junit.Before;
21  import org.junit.Test;
22  
23  import java.io.File;
24  import java.io.IOException;
25  import java.nio.file.Files;
26  
27  import static org.junit.Assert.*;
28  
29  public class SymlinkUtilsTest
30  {
31      File target = new File( "target/symlinkCapabilities" );
32  
33      String expected = "This is a filed that we'll be symlinking to\n";
34  
35      @Before
36      public void setup()
37          throws IOException
38      {
39          FileUtils.deleteDirectory( target );
40          Files.createDirectories( target.toPath() );
41      }
42  
43      @Test
44      public void testName()
45          throws Exception
46      {
47  
48      }
49  
50      @Test
51      public void create_read_symbolic_link_to_file()
52          throws Exception
53      {
54          File symlink = new File( target, "symlinkToTarget" );
55          File relativePath = createTargetFile( target );
56          SymlinkUtils.createSymbolicLink( symlink, relativePath );
57          assertEquals( expected, FileUtils.readFileToString( symlink ) );
58          assertEquals( new File( "actualFile" ),
59                        SymlinkUtils.readSymbolicLink( new File( target, "symlinkToTarget" ) ) );
60      }
61  
62      @Test
63      public void create_read_symbolic_link_to_directory()
64          throws Exception
65      {
66          File subDir = new File( target, "aSubDir" );
67          createTargetFile( subDir );
68          File symlink = new File( target, "symlinkToDir" );
69          SymlinkUtils.createSymbolicLink( symlink, new File( "aSubDir" ) );
70          assertEquals( expected, FileUtils.readFileToString( new File( symlink, "actualFile" ) ) );
71          assertEquals( new File( "aSubDir" ), SymlinkUtils.readSymbolicLink( new File( target, "symlinkToDir" ) ) );
72  
73      }
74  
75      private File createTargetFile( File target )
76          throws IOException
77      {
78          File relativePath = new File( "actualFile" );
79          File actualFile = new File( target, relativePath.getPath() );
80          FileUtils.write( actualFile, expected );
81          return relativePath;
82      }
83  }