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.jupiter.api.Test;
28  import org.junit.jupiter.api.TestInfo;
29  
30  import static org.junit.jupiter.api.Assertions.*;
31  import static org.junit.jupiter.api.Assumptions.assumeTrue;
32  
33  public class DirectoryUtilsTest {
34      @Test
35      void expectedCasesRelative(TestInfo testInfo) throws IOException {
36          // hack for surefire: sets the property but directory may not exist
37          Files.createDirectories(Paths.get(System.getProperty("java.io.tmpdir")));
38  
39          Path tmpDir = Files.createTempDirectory(testInfo.getDisplayName());
40          Path result;
41  
42          result = DirectoryUtils.resolveDirectory("foo", tmpDir, false);
43          assertEquals(result, tmpDir.resolve("foo"));
44  
45          result = DirectoryUtils.resolveDirectory("foo/bar", tmpDir, false);
46          assertEquals(result, tmpDir.resolve("foo/bar"));
47  
48          result = DirectoryUtils.resolveDirectory("foo/./bar/..", tmpDir, false);
49          assertEquals(result, tmpDir.resolve("foo"));
50      }
51  
52      @Test
53      void expectedCasesAbsolute(TestInfo testInfo) throws IOException {
54          // TODO: this test is skipped on Windows, as it is not clear which drive letter will `new File("/foo")`
55          // path get. According to Windows (and  assuming Java Path does separator change OK), "\foo" file should
56          // get resolved to CWD drive + "\foo" path, but seems Java 17 is different from 11 and 8 in this respect.
57          // This below WORKS on win + Java8 abd win + Java11 but FAILS on win + Java17
58          assumeTrue(
59                  !"WindowsFileSystem".equals(FileSystems.getDefault().getClass().getSimpleName()));
60  
61          // hack for surefire: sets the property but directory may not exist
62          Files.createDirectories(Paths.get(System.getProperty("java.io.tmpdir")));
63  
64          Path tmpDir = Files.createTempDirectory(testInfo.getDisplayName());
65          Path result;
66  
67          result = DirectoryUtils.resolveDirectory("/foo", tmpDir, false);
68          assertEquals(result, FileSystems.getDefault().getPath("/foo").toAbsolutePath());
69  
70          result = DirectoryUtils.resolveDirectory("/foo/bar", tmpDir, false);
71          assertEquals(result, FileSystems.getDefault().getPath("/foo/bar").toAbsolutePath());
72  
73          result = DirectoryUtils.resolveDirectory("/foo/./bar/..", tmpDir, false);
74          assertEquals(result, FileSystems.getDefault().getPath("/foo").toAbsolutePath());
75      }
76  
77      @Test
78      void existsButIsADirectory(TestInfo testInfo) throws IOException {
79          // hack for surefire: sets the property but directory may not exist
80          Files.createDirectories(Paths.get(System.getProperty("java.io.tmpdir")));
81  
82          Path tmpDir = Files.createTempDirectory(testInfo.getDisplayName());
83          Files.createDirectories(tmpDir.resolve("foo"));
84          Path result = DirectoryUtils.resolveDirectory("foo", tmpDir, false);
85          assertEquals(result, tmpDir.resolve("foo"));
86      }
87  
88      @Test
89      void existsButNotADirectory(TestInfo testInfo) throws IOException {
90          // hack for surefire: sets the property but directory may not exist
91          Files.createDirectories(Paths.get(System.getProperty("java.io.tmpdir")));
92  
93          Path tmpDir = Files.createTempDirectory(testInfo.getDisplayName());
94          Files.createFile(tmpDir.resolve("foo"));
95          try {
96              DirectoryUtils.resolveDirectory("foo", tmpDir, false);
97          } catch (IOException e) {
98              assertTrue(e.getMessage().startsWith("Path exists, but is not a directory:"), e.getMessage());
99          }
100     }
101 
102     @Test
103     void notExistsAndIsCreated(TestInfo testInfo) throws IOException {
104         // hack for surefire: sets the property but directory may not exist
105         Files.createDirectories(Paths.get(System.getProperty("java.io.tmpdir")));
106 
107         Path tmpDir = Files.createTempDirectory(testInfo.getDisplayName());
108         Files.createDirectories(tmpDir.resolve("foo"));
109         Path result = DirectoryUtils.resolveDirectory("foo", tmpDir, true);
110         assertEquals(result, tmpDir.resolve("foo"));
111         assumeTrue(Files.isDirectory(tmpDir.resolve("foo")));
112     }
113 }