Skip to content

Commit dd64a4a

Browse files
author
Brian Burkhalter
committedAug 30, 2023
8315241: (fs) Move toRealPath tests in java/nio/file/Path/Misc.java to separate JUnit 5 test
Reviewed-by: rriggs
1 parent 8e4cda0 commit dd64a4a

File tree

2 files changed

+193
-143
lines changed

2 files changed

+193
-143
lines changed
 

‎test/jdk/java/nio/file/Path/Misc.java

+2-143
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2008, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2008, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -22,7 +22,7 @@
2222
*/
2323

2424
/* @test
25-
* @bug 4313887 6838333 7029979 8295753
25+
* @bug 4313887 6838333 7029979
2626
* @summary Unit test for miscellenous java.nio.file.Path methods
2727
* @library .. /test/lib
2828
* @build jdk.test.lib.Platform
@@ -32,28 +32,17 @@
3232
import java.io.*;
3333
import java.nio.file.*;
3434

35-
import static java.nio.file.LinkOption.*;
36-
3735
import jdk.test.lib.Platform;
3836

3937
public class Misc {
40-
static boolean supportsLinks;
41-
4238
public static void main(String[] args) throws IOException {
4339
Path dir = TestUtil.createTemporaryDirectory();
4440
try {
45-
supportsLinks = TestUtil.supportsLinks(dir);
46-
4741
// equals and hashCode methods
4842
testEqualsAndHashCode();
4943

5044
// toFile method
5145
testToFile(dir);
52-
53-
// toRealPath method
54-
testToRealPath(dir);
55-
56-
5746
} finally {
5847
TestUtil.removeAll(dir);
5948
}
@@ -92,136 +81,6 @@ static void testToFile(Path dir) throws IOException {
9281
assertTrue(d.toPath().equals(dir));
9382
}
9483

95-
/**
96-
* Exercise toRealPath method
97-
*/
98-
static void testToRealPath(Path dir) throws IOException {
99-
final Path file = Files.createFile(dir.resolve("foo"));
100-
final Path link = dir.resolve("link");
101-
102-
/**
103-
* Test: toRealPath() will access same file as toRealPath(NOFOLLOW_LINKS)
104-
*/
105-
assertTrue(Files.isSameFile(file.toRealPath(), file.toRealPath(NOFOLLOW_LINKS)));
106-
107-
/**
108-
* Test: toRealPath should fail if file does not exist
109-
*/
110-
Path doesNotExist = dir.resolve("DoesNotExist");
111-
try {
112-
doesNotExist.toRealPath();
113-
throw new RuntimeException("IOException expected");
114-
} catch (IOException expected) {
115-
}
116-
try {
117-
doesNotExist.toRealPath(NOFOLLOW_LINKS);
118-
throw new RuntimeException("IOException expected");
119-
} catch (IOException expected) {
120-
}
121-
122-
/**
123-
* Test: toRealPath() should resolve links
124-
*/
125-
if (supportsLinks) {
126-
Path resolvedFile = file;
127-
if (Platform.isWindows()) {
128-
// Path::toRealPath does not work with environments using the
129-
// legacy subst mechanism. This is a workaround to keep the
130-
// test working if 'dir' points to a location on a subst drive.
131-
// See JDK-8213216.
132-
//
133-
Path tempLink = dir.resolve("tempLink");
134-
Files.createSymbolicLink(tempLink, dir.toAbsolutePath());
135-
Path resolvedDir = tempLink.toRealPath();
136-
Files.delete(tempLink);
137-
resolvedFile = resolvedDir.resolve(file.getFileName());
138-
}
139-
140-
Files.createSymbolicLink(link, resolvedFile.toAbsolutePath());
141-
assertTrue(link.toRealPath().equals(resolvedFile.toRealPath()));
142-
Files.delete(link);
143-
}
144-
145-
/**
146-
* Test: toRealPath(NOFOLLOW_LINKS) should not resolve links
147-
*/
148-
if (supportsLinks) {
149-
Files.createSymbolicLink(link, file.toAbsolutePath());
150-
assertTrue(link.toRealPath(NOFOLLOW_LINKS).getFileName().equals(link.getFileName()));
151-
Files.delete(link);
152-
}
153-
154-
/**
155-
* Test: toRealPath(NOFOLLOW_LINKS) with broken link
156-
*/
157-
if (supportsLinks) {
158-
Path broken = Files.createSymbolicLink(link, doesNotExist);
159-
assertTrue(link.toRealPath(NOFOLLOW_LINKS).getFileName().equals(link.getFileName()));
160-
Files.delete(link);
161-
}
162-
163-
/**
164-
* Test: toRealPath should eliminate "."
165-
*/
166-
assertTrue(dir.resolve(".").toRealPath().equals(dir.toRealPath()));
167-
assertTrue(dir.resolve(".").toRealPath(NOFOLLOW_LINKS).equals(dir.toRealPath(NOFOLLOW_LINKS)));
168-
169-
/**
170-
* Test: toRealPath should eliminate ".." when it doesn't follow a
171-
* symbolic link
172-
*/
173-
Path subdir = Files.createDirectory(dir.resolve("subdir"));
174-
assertTrue(subdir.resolve("..").toRealPath().equals(dir.toRealPath()));
175-
assertTrue(subdir.resolve("..").toRealPath(NOFOLLOW_LINKS).equals(dir.toRealPath(NOFOLLOW_LINKS)));
176-
177-
/**
178-
* Test: toRealPath yields accurate case of path elements when
179-
* not following links
180-
*/
181-
if (Platform.isOSX()) {
182-
// theTarget = dir/subdir/theTarget
183-
Path theTarget = Path.of(subdir.toString(), "theTarget");
184-
Files.createFile(theTarget);
185-
186-
// dir/theLink -> dir/subdir
187-
Path theLink = Path.of(dir.toString(), "theLink");
188-
Files.createSymbolicLink(theLink, subdir);
189-
190-
// thePath = dir/thelink/thetarget (all lower case)
191-
Path thePath = Path.of(dir.toString(), "thelink", "thetarget");
192-
Path noFollow = thePath.toRealPath(NOFOLLOW_LINKS);
193-
int nc = noFollow.getNameCount();
194-
195-
// Real path should retain case as dir/theLink/theTarget
196-
assertTrue(noFollow.getName(nc - 2).equals(Path.of("theLink")));
197-
assertTrue(noFollow.getName(nc - 1).equals(Path.of("theTarget")));
198-
assertTrue(noFollow.toString().equals(
199-
Path.of(dir.toString(), "theLink", "theTarget").toString()));
200-
201-
// Test where a link is preceded by ".." in the path
202-
Path superBeforeLink =
203-
Path.of(subdir.toString(), "..", "thelink", "thetarget");
204-
noFollow = superBeforeLink.toRealPath(NOFOLLOW_LINKS);
205-
nc = noFollow.getNameCount();
206-
assertTrue(noFollow.getName(nc - 2).equals(Path.of("theLink")));
207-
assertTrue(noFollow.getName(nc - 1).equals(Path.of("theTarget")));
208-
209-
// Test where a link is followed by ".." in the path
210-
Path linkBeforeSuper =
211-
Path.of(dir.toString(), "thelink", "..", "subdir", "thetarget");
212-
noFollow = linkBeforeSuper.toRealPath(NOFOLLOW_LINKS);
213-
nc = noFollow.getNameCount();
214-
assertTrue(noFollow.getName(nc - 4).equals(Path.of("theLink")));
215-
assertTrue(noFollow.getName(nc - 1).equals(Path.of("theTarget")));
216-
217-
Files.delete(theTarget);
218-
}
219-
220-
// clean-up
221-
Files.delete(subdir);
222-
Files.delete(file);
223-
}
224-
22584
static void assertTrue(boolean okay) {
22685
if (!okay)
22786
throw new RuntimeException("Assertion Failed");
+191
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
/*
2+
* Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
/* @test
25+
* @bug 8295753
26+
* @summary Verify correct operation of Path.toRealPath
27+
* @library .. /test/lib
28+
* @build ToRealPath jdk.test.lib.Platform
29+
* @run junit ToRealPath
30+
*/
31+
32+
import java.io.IOException;
33+
import java.io.UncheckedIOException;
34+
import java.nio.file.Files;
35+
import java.nio.file.Path;
36+
37+
import jdk.test.lib.Platform;
38+
39+
import org.junit.jupiter.api.AfterAll;
40+
import org.junit.jupiter.api.Test;
41+
import org.junit.jupiter.api.condition.EnabledIf;
42+
import org.junit.jupiter.api.condition.EnabledOnOs;
43+
import org.junit.jupiter.api.condition.OS;
44+
45+
import static java.nio.file.LinkOption.*;
46+
import static org.junit.jupiter.api.Assertions.*;
47+
48+
public class ToRealPath {
49+
static final boolean SUPPORTS_LINKS;
50+
static final Path DIR;
51+
static final Path SUBDIR;
52+
static final Path FILE;
53+
static final Path LINK;
54+
55+
static {
56+
try {
57+
DIR = TestUtil.createTemporaryDirectory();
58+
SUBDIR = Files.createDirectory(DIR.resolve("subdir"));
59+
FILE = Files.createFile(DIR.resolve("foo"));
60+
LINK = DIR.resolve("link");
61+
SUPPORTS_LINKS = TestUtil.supportsLinks(DIR);
62+
} catch (IOException e) {
63+
throw new UncheckedIOException(e);
64+
}
65+
};
66+
67+
public boolean supportsLinks() {
68+
return SUPPORTS_LINKS;
69+
}
70+
71+
@Test
72+
public void locateSameFile() throws IOException {
73+
assertTrue(Files.isSameFile(FILE.toRealPath(),
74+
FILE.toRealPath(NOFOLLOW_LINKS)));
75+
}
76+
77+
@Test
78+
public void failNotExist() {
79+
Path doesNotExist = DIR.resolve("DoesNotExist");
80+
assertThrows(IOException.class, () -> doesNotExist.toRealPath());
81+
}
82+
83+
@Test
84+
public void failNotExistNoFollow() {
85+
Path doesNotExist = DIR.resolve("DoesNotExist");
86+
assertThrows(IOException.class,
87+
() -> doesNotExist.toRealPath(NOFOLLOW_LINKS));
88+
}
89+
90+
@EnabledIf("supportsLinks")
91+
@Test
92+
public void shouldResolveLinks() throws IOException {
93+
Path resolvedFile = FILE;
94+
if (Platform.isWindows()) {
95+
// Path::toRealPath does not work with environments using the
96+
// legacy subst mechanism. This is a workaround to keep the
97+
// test working if 'dir' points to a location on a subst drive.
98+
// See JDK-8213216.
99+
//
100+
Path tempLink = DIR.resolve("tempLink");
101+
Files.createSymbolicLink(tempLink, DIR.toAbsolutePath());
102+
Path resolvedDir = tempLink.toRealPath();
103+
Files.delete(tempLink);
104+
resolvedFile = resolvedDir.resolve(FILE.getFileName());
105+
}
106+
107+
Files.createSymbolicLink(LINK, resolvedFile.toAbsolutePath());
108+
assertTrue(LINK.toRealPath().equals(resolvedFile.toRealPath()));
109+
Files.delete(LINK);
110+
}
111+
112+
@Test
113+
@EnabledIf("supportsLinks")
114+
public void shouldNotResolveLinks() throws IOException {
115+
Files.createSymbolicLink(LINK, FILE.toAbsolutePath());
116+
assertEquals(LINK.toRealPath(NOFOLLOW_LINKS).getFileName(),
117+
LINK.getFileName());
118+
Files.delete(LINK);
119+
}
120+
121+
@Test
122+
public void eliminateDot() throws IOException {
123+
assertEquals(DIR.resolve(".").toRealPath(),
124+
DIR.toRealPath());
125+
}
126+
127+
@Test
128+
public void eliminateDotNoFollow() throws IOException {
129+
assertEquals(DIR.resolve(".").toRealPath(NOFOLLOW_LINKS),
130+
DIR.toRealPath(NOFOLLOW_LINKS));
131+
}
132+
133+
@Test
134+
public void eliminateDots() throws IOException {
135+
assertEquals(SUBDIR.resolve("..").toRealPath(),
136+
DIR.toRealPath());
137+
}
138+
139+
@Test
140+
public void eliminateDotsNoFollow() throws IOException {
141+
assertEquals(SUBDIR.resolve("..").toRealPath(NOFOLLOW_LINKS),
142+
DIR.toRealPath(NOFOLLOW_LINKS));
143+
}
144+
145+
@Test
146+
@EnabledOnOs(OS.MAC)
147+
public final void macOSTests() throws IOException {
148+
// theTarget = dir/subdir/theTarget
149+
Path theTarget = Path.of(SUBDIR.toString(), "theTarget");
150+
Files.createFile(theTarget);
151+
152+
// dir/theLink -> dir/subdir
153+
Path theLink = Path.of(DIR.toString(), "theLink");
154+
Files.createSymbolicLink(theLink, SUBDIR);
155+
156+
// thePath = dir/thelink/thetarget (all lower case)
157+
Path thePath = Path.of(DIR.toString(), "thelink", "thetarget");
158+
Path noFollow = thePath.toRealPath(NOFOLLOW_LINKS);
159+
int nc = noFollow.getNameCount();
160+
161+
// Real path should retain case as dir/theLink/theTarget
162+
assertEquals(noFollow.getName(nc - 2), Path.of("theLink"));
163+
assertEquals(noFollow.getName(nc - 1), Path.of("theTarget"));
164+
assertEquals(noFollow.toString(),
165+
Path.of(DIR.toString(), "theLink", "theTarget").toString());
166+
167+
// Test where a link is preceded by ".." in the path
168+
Path superBeforeLink =
169+
Path.of(SUBDIR.toString(), "..", "thelink", "thetarget");
170+
noFollow = superBeforeLink.toRealPath(NOFOLLOW_LINKS);
171+
nc = noFollow.getNameCount();
172+
assertEquals(noFollow.getName(nc - 2), Path.of("theLink"));
173+
assertEquals(noFollow.getName(nc - 1), Path.of("theTarget"));
174+
175+
// Test where a link is followed by ".." in the path
176+
Path linkBeforeSuper =
177+
Path.of(DIR.toString(), "thelink", "..", "subdir", "thetarget");
178+
noFollow = linkBeforeSuper.toRealPath(NOFOLLOW_LINKS);
179+
nc = noFollow.getNameCount();
180+
assertEquals(noFollow.getName(nc - 4), Path.of("theLink"));
181+
assertEquals(noFollow.getName(nc - 1), Path.of("theTarget"));
182+
183+
Files.delete(theTarget);
184+
}
185+
186+
@AfterAll
187+
public static void cleanup() throws IOException {
188+
Files.delete(SUBDIR);
189+
Files.delete(FILE);
190+
}
191+
}

0 commit comments

Comments
 (0)