View Javadoc
1   package org.eclipse.aether.util;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.IOException;
23  import java.nio.file.FileSystems;
24  import java.nio.file.Files;
25  import java.nio.file.Path;
26  import java.nio.file.Paths;
27  
28  import org.junit.Rule;
29  import org.junit.Test;
30  import org.junit.rules.TestName;
31  
32  import static org.hamcrest.MatcherAssert.assertThat;
33  import static org.hamcrest.Matchers.equalTo;
34  import static org.hamcrest.Matchers.startsWith;
35  import static org.junit.Assume.assumeTrue;
36  
37  public class DirectoryUtilsTest
38  {
39      @Rule
40      public TestName testName = new TestName();
41  
42      @Test
43      public void expectedCasesRelative() throws IOException
44      {
45          // hack for surefire: sets the property but directory may not exist
46          Files.createDirectories( Paths.get ( System.getProperty( "java.io.tmpdir" ) ) );
47  
48          Path tmpDir = Files.createTempDirectory( testName.getMethodName() );
49          Path result;
50  
51          result = DirectoryUtils.resolveDirectory( "foo", tmpDir, false );
52          assertThat( result, equalTo( tmpDir.resolve( "foo" ) ) );
53  
54          result = DirectoryUtils.resolveDirectory( "foo/bar", tmpDir, false );
55          assertThat( result, equalTo( tmpDir.resolve( "foo/bar" ) ) );
56  
57          result = DirectoryUtils.resolveDirectory( "foo/./bar/..", tmpDir, false );
58          assertThat( result, equalTo( tmpDir.resolve( "foo" ) ) );
59      }
60  
61      @Test
62      public void expectedCasesAbsolute() throws IOException
63      {
64          // TODO: this test is skipped on Windows, as it is not clear which drive letter will `new File("/foo")`
65          // path get. According to Windows (and  assuming Java Path does separator change OK), "\foo" file should
66          // get resolved to CWD drive + "\foo" path, but seems Java 17 is different from 11 and 8 in this respect.
67          // This below WORKS on win + Java8 abd win + Java11 but FAILS on win + Java17
68          assumeTrue( !"WindowsFileSystem".equals( FileSystems.getDefault().getClass().getSimpleName() ) );
69  
70          // hack for surefire: sets the property but directory may not exist
71          Files.createDirectories( Paths.get ( System.getProperty( "java.io.tmpdir" ) ) );
72  
73          Path tmpDir = Files.createTempDirectory( testName.getMethodName() );
74          Path result;
75  
76          result = DirectoryUtils.resolveDirectory( "/foo", tmpDir, false );
77          assertThat( result, equalTo( FileSystems.getDefault().getPath( "/foo" ).toAbsolutePath() ) );
78  
79          result = DirectoryUtils.resolveDirectory( "/foo/bar", tmpDir, false );
80          assertThat( result, equalTo( FileSystems.getDefault().getPath( "/foo/bar" ).toAbsolutePath() ) );
81  
82          result = DirectoryUtils.resolveDirectory( "/foo/./bar/..", tmpDir, false );
83          assertThat( result, equalTo( FileSystems.getDefault().getPath( "/foo" ).toAbsolutePath() ) );
84      }
85  
86      @Test
87      public void existsButIsADirectory() throws IOException
88      {
89          // hack for surefire: sets the property but directory may not exist
90          Files.createDirectories( Paths.get ( System.getProperty( "java.io.tmpdir" ) ) );
91  
92          Path tmpDir = Files.createTempDirectory( testName.getMethodName() );
93          Files.createDirectories( tmpDir.resolve( "foo" ) );
94          Path result = DirectoryUtils.resolveDirectory( "foo", tmpDir, false );
95          assertThat( result, equalTo( tmpDir.resolve( "foo" ) ) );
96      }
97  
98      @Test
99      public void existsButNotADirectory() throws IOException
100     {
101         // hack for surefire: sets the property but directory may not exist
102         Files.createDirectories( Paths.get ( System.getProperty( "java.io.tmpdir" ) ) );
103 
104         Path tmpDir = Files.createTempDirectory( testName.getMethodName() );
105         Files.createFile( tmpDir.resolve( "foo" ) );
106         try
107         {
108             DirectoryUtils.resolveDirectory( "foo", tmpDir, false );
109         }
110         catch ( IOException e )
111         {
112             assertThat( e.getMessage(), startsWith( "Path exists, but is not a directory:" ) );
113         }
114     }
115 
116     @Test
117     public void notExistsAndIsCreated() throws IOException
118     {
119         // hack for surefire: sets the property but directory may not exist
120         Files.createDirectories( Paths.get ( System.getProperty( "java.io.tmpdir" ) ) );
121 
122         Path tmpDir = Files.createTempDirectory( testName.getMethodName() );
123         Files.createDirectories( tmpDir.resolve( "foo" ) );
124         Path result = DirectoryUtils.resolveDirectory( "foo", tmpDir, true );
125         assertThat( result, equalTo( tmpDir.resolve( "foo" ) ) );
126         assertThat( Files.isDirectory( tmpDir.resolve( "foo" ) ), equalTo( true ) );
127     }
128 }