Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/nio/file/Path/Misc.java
41153 views
1
/*
2
* Copyright (c) 2008, 2011, 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 4313887 6838333 7029979
26
* @summary Unit test for miscellenous java.nio.file.Path methods
27
* @library ..
28
*/
29
30
import java.nio.file.*;
31
import static java.nio.file.LinkOption.*;
32
import java.io.*;
33
34
public class Misc {
35
static final boolean isWindows =
36
System.getProperty("os.name").startsWith("Windows");
37
static boolean supportsLinks;
38
39
public static void main(String[] args) throws IOException {
40
Path dir = TestUtil.createTemporaryDirectory();
41
try {
42
supportsLinks = TestUtil.supportsLinks(dir);
43
44
// equals and hashCode methods
45
testEqualsAndHashCode();
46
47
// toFile method
48
testToFile(dir);
49
50
// toRealPath method
51
testToRealPath(dir);
52
53
54
} finally {
55
TestUtil.removeAll(dir);
56
}
57
}
58
59
/**
60
* Exercise equals and hashCode methods
61
*/
62
static void testEqualsAndHashCode() {
63
Path thisFile = Paths.get("this");
64
Path thatFile = Paths.get("that");
65
66
assertTrue(thisFile.equals(thisFile));
67
assertTrue(!thisFile.equals(thatFile));
68
69
assertTrue(!thisFile.equals(null));
70
assertTrue(!thisFile.equals(new Object()));
71
72
Path likeThis = Paths.get("This");
73
if (isWindows) {
74
// case insensitive
75
assertTrue(thisFile.equals(likeThis));
76
assertTrue(thisFile.hashCode() == likeThis.hashCode());
77
} else {
78
// case senstive
79
assertTrue(!thisFile.equals(likeThis));
80
}
81
}
82
83
/**
84
* Exercise toFile method
85
*/
86
static void testToFile(Path dir) throws IOException {
87
File d = dir.toFile();
88
assertTrue(d.toString().equals(dir.toString()));
89
assertTrue(d.toPath().equals(dir));
90
}
91
92
/**
93
* Exercise toRealPath method
94
*/
95
static void testToRealPath(Path dir) throws IOException {
96
final Path file = Files.createFile(dir.resolve("foo"));
97
final Path link = dir.resolve("link");
98
99
/**
100
* Test: totRealPath() will access same file as toRealPath(NOFOLLOW_LINKS)
101
*/
102
assertTrue(Files.isSameFile(file.toRealPath(), file.toRealPath(NOFOLLOW_LINKS)));
103
104
/**
105
* Test: toRealPath should fail if file does not exist
106
*/
107
Path doesNotExist = dir.resolve("DoesNotExist");
108
try {
109
doesNotExist.toRealPath();
110
throw new RuntimeException("IOException expected");
111
} catch (IOException expected) {
112
}
113
try {
114
doesNotExist.toRealPath(NOFOLLOW_LINKS);
115
throw new RuntimeException("IOException expected");
116
} catch (IOException expected) {
117
}
118
119
/**
120
* Test: toRealPath() should resolve links
121
*/
122
if (supportsLinks) {
123
Path resolvedFile = file;
124
if (isWindows) {
125
// Path::toRealPath does not work with environments using the
126
// legacy subst mechanism. This is a workaround to keep the
127
// test working if 'dir' points to a location on a subst drive.
128
// See JDK-8213216.
129
//
130
Path tempLink = dir.resolve("tempLink");
131
Files.createSymbolicLink(tempLink, dir.toAbsolutePath());
132
Path resolvedDir = tempLink.toRealPath();
133
Files.delete(tempLink);
134
resolvedFile = resolvedDir.resolve(file.getFileName());
135
}
136
137
Files.createSymbolicLink(link, resolvedFile.toAbsolutePath());
138
assertTrue(link.toRealPath().equals(resolvedFile.toRealPath()));
139
Files.delete(link);
140
}
141
142
/**
143
* Test: toRealPath(NOFOLLOW_LINKS) should not resolve links
144
*/
145
if (supportsLinks) {
146
Files.createSymbolicLink(link, file.toAbsolutePath());
147
assertTrue(link.toRealPath(NOFOLLOW_LINKS).getFileName().equals(link.getFileName()));
148
Files.delete(link);
149
}
150
151
/**
152
* Test: toRealPath(NOFOLLOW_LINKS) with broken link
153
*/
154
if (supportsLinks) {
155
Path broken = Files.createSymbolicLink(link, doesNotExist);
156
assertTrue(link.toRealPath(NOFOLLOW_LINKS).getFileName().equals(link.getFileName()));
157
Files.delete(link);
158
}
159
160
/**
161
* Test: toRealPath should eliminate "."
162
*/
163
assertTrue(dir.resolve(".").toRealPath().equals(dir.toRealPath()));
164
assertTrue(dir.resolve(".").toRealPath(NOFOLLOW_LINKS).equals(dir.toRealPath(NOFOLLOW_LINKS)));
165
166
/**
167
* Test: toRealPath should eliminate ".." when it doesn't follow a
168
* symbolic link
169
*/
170
Path subdir = Files.createDirectory(dir.resolve("subdir"));
171
assertTrue(subdir.resolve("..").toRealPath().equals(dir.toRealPath()));
172
assertTrue(subdir.resolve("..").toRealPath(NOFOLLOW_LINKS).equals(dir.toRealPath(NOFOLLOW_LINKS)));
173
Files.delete(subdir);
174
175
// clean-up
176
Files.delete(file);
177
}
178
179
static void assertTrue(boolean okay) {
180
if (!okay)
181
throw new RuntimeException("Assertion Failed");
182
}
183
}
184
185