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