Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/nio/file/Path/MacPathTest.java
38828 views
1
/*
2
* Copyright (c) 2008, 2012, 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 7130915
26
* @summary Tests file path with nfc/nfd forms on MacOSX
27
* @library ../
28
* @build MacPathTest
29
* @run shell MacPathTest.sh
30
*/
31
32
import java.nio.file.*;
33
import java.nio.file.attribute.*;
34
import java.text.*;
35
import java.util.*;
36
import java.util.regex.*;
37
38
public class MacPathTest {
39
40
public static void main(String args[]) throws Throwable {
41
String osname = System.getProperty("os.name");
42
if (!osname.contains("OS X") && !osname.contains("Darwin"))
43
return;
44
System.out.printf("sun.jnu.encoding=%s, file.encoding=%s%n",
45
System.getProperty("file.encoding"),
46
System.getProperty("sun.jnu.encoding"));
47
// English
48
test("TestDir_apple", // test dir
49
"dir_macosx", // dir
50
"file_macosx"); // file
51
52
// Japanese composite character
53
test("TestDir_\u30c8\u30a4\u30e4\u30cb\u30ca\u30eb/",
54
"dir_\u30a4\u30c1\u30b4\u306e\u30b1\u30fc\u30ad",
55
"file_\u30a4\u30c1\u30b4\u306e\u30b1\u30fc\u30ad");
56
57
// latin-1 supplementory
58
test("TestDir_K\u00f6rperlich\u00e4\u00df/",
59
"dir_Entt\u00e4uschung",
60
"file_Entt\u00e4uschung");
61
62
test("TestDir_K\u00f6rperlich\u00e4\u00df/",
63
"dir_Entt\u00c4uschung",
64
"file_Entt\u00c4uschung");
65
66
// Korean syblla
67
test("TestDir_\uac00\uac01\uac02",
68
"dir_\uac20\uac21\uac22",
69
"file_\uacc0\uacc1\uacc2");
70
}
71
72
private static boolean equal(Object x, Object y) {
73
return x == null ? y == null : x.equals(y);
74
}
75
76
private static boolean match(Path target, Path src) {
77
String fname = target.toString();
78
System.out.printf(" --> Trying [%s], length=%d...", fname, fname.length());
79
if (target.equals(src)) {
80
System.out.println(" MATCHED!");
81
return true;
82
} else {
83
System.out.println(" NOT MATCHED!");
84
}
85
return false;
86
}
87
88
private static void test(String testdir, String dname, String fname_nfc)
89
throws Throwable
90
{
91
String fname = null;
92
String dname_nfd = Normalizer.normalize(dname, Normalizer.Form.NFD);
93
String fname_nfd = Normalizer.normalize(fname_nfc, Normalizer.Form.NFD);
94
95
System.out.printf("%n%n--------Testing...----------%n");
96
Path bpath = Paths.get(testdir);
97
Path dpath = Paths.get(testdir, dname);
98
Path dpath_nfd = Paths.get(testdir, dname_nfd);
99
Path fpath_nfc = Paths.get(testdir, fname_nfc);
100
Path fpath_nfd = Paths.get(testdir, fname_nfd);
101
102
if (Files.exists(bpath))
103
TestUtil.removeAll(bpath);
104
Files.createDirectories(dpath);
105
106
fname = dpath.toString();
107
System.out.printf(":Directory [%s][len=%d] created%n", fname, fname.length());
108
109
//////////////////////////////////////////////////////////////
110
if (!Files.isDirectory(dpath) || !Files.isDirectory(dpath_nfd)) {
111
throw new RuntimeException("Files.isDirectory(...) failed");
112
}
113
114
//////////////////////////////////////////////////////////////
115
// write out with nfd, read in with nfc + case
116
Files.write(fpath_nfd, new byte[] { 'n', 'f', 'd'});
117
System.out.println(" read in with nfc (from nfd):" + new String(Files.readAllBytes(fpath_nfc)));
118
119
// check attrs with nfc + case
120
Set<PosixFilePermission> pfp = Files.getPosixFilePermissions(fpath_nfd);
121
if (!equal(pfp, Files.getPosixFilePermissions(fpath_nfc)) ) {
122
throw new RuntimeException("Files.getPosixfilePermission(...) failed");
123
}
124
Files.delete(fpath_nfd);
125
126
// write out with nfc, read in with nfd + case
127
Files.write(fpath_nfc, new byte[] { 'n', 'f', 'c'});
128
System.out.println(" read in with nfd (from nfc):" + new String(Files.readAllBytes(fpath_nfd)));
129
130
// check attrs with nfc + case
131
pfp = Files.getPosixFilePermissions(fpath_nfc);
132
if (!equal(pfp, Files.getPosixFilePermissions(fpath_nfd))) {
133
throw new RuntimeException("Files.getPosixfilePermission(...) failed");
134
}
135
//////////////////////////////////////////////////////////////
136
boolean found_dir = false;
137
boolean found_file_nfc = false;
138
boolean found_file_nfd = false;
139
try (DirectoryStream<Path> stream = Files.newDirectoryStream(bpath)) {
140
for (Path path: stream) {
141
fname = path.toString();
142
System.out.printf("Found : [%s], length=%d%n", fname, fname.length());
143
found_dir |= match(dpath, path);
144
found_file_nfc |= match(fpath_nfc, path);
145
found_file_nfd |= match(fpath_nfd, path);
146
}
147
}
148
if (!found_dir || !found_file_nfc || !found_file_nfd) {
149
throw new RuntimeException("File.equal() failed");
150
}
151
// glob
152
String glob = "*" + fname_nfd.substring(2); // remove leading "FI" from "FILE..."
153
System.out.println("glob=" + glob);
154
boolean globmatched = false;
155
try (DirectoryStream<Path> stream = Files.newDirectoryStream(bpath, glob)) {
156
for (Path path: stream) {
157
fname = path.toString();
158
System.out.printf("PathMatch : [%s], length=%d%n", fname, fname.length());
159
globmatched |= match(fpath_nfc, path);
160
}
161
}
162
if (!globmatched) {
163
//throw new RuntimeException("path matcher failed");
164
// it appears we have a regex.anon_eq bug in hangul syllable handling
165
System.out.printf("pathmatcher failed, glob=[%s]%n", glob);
166
System.out.printf(" -> fname_nfd.matches(fname_nfc)=%b%n",
167
Pattern.compile(fname_nfd, Pattern.CANON_EQ)
168
.matcher(fname_nfc)
169
.matches());
170
System.out.printf(" -> fname_nfc.matches(fname_nfd)=%b%n",
171
Pattern.compile(fname_nfc, Pattern.CANON_EQ)
172
.matcher(fname_nfd)
173
.matches());
174
}
175
TestUtil.removeAll(bpath);
176
}
177
}
178
179