1 package org.eclipse.aether.util;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
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
65
66
67
68 assumeTrue( !"WindowsFileSystem".equals( FileSystems.getDefault().getClass().getSimpleName() ) );
69
70
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
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
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
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 }