1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
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
55
56
57
58 assumeTrue(
59 !"WindowsFileSystem".equals(FileSystems.getDefault().getClass().getSimpleName()));
60
61
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
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
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
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 }