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.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
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
61
62
63
64 assumeTrue(
65 !"WindowsFileSystem".equals(FileSystems.getDefault().getClass().getSimpleName()));
66
67
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
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
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
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 }