Path: blob/master/test/jdk/java/nio/file/Path/MacPath.java
41153 views
/*1* Copyright (c) 2008, 2017, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.nio.file.DirectoryStream;24import java.nio.file.Files;25import java.nio.file.Path;26import java.nio.file.Paths;27import java.nio.file.attribute.PosixFilePermission;28import java.text.Normalizer;29import java.util.Set;30import java.util.regex.Pattern;3132public class MacPath {3334public static void main(String args[]) throws Throwable {35System.out.printf("sun.jnu.encoding=%s, file.encoding=%s%n",36System.getProperty("file.encoding"),37System.getProperty("sun.jnu.encoding"));38// English39test("TestDir_apple", // test dir40"dir_macosx", // dir41"file_macosx"); // file4243// Japanese composite character44test("TestDir_\u30c8\u30a4\u30e4\u30cb\u30ca\u30eb/",45"dir_\u30a4\u30c1\u30b4\u306e\u30b1\u30fc\u30ad",46"file_\u30a4\u30c1\u30b4\u306e\u30b1\u30fc\u30ad");4748// latin-1 supplementory49test("TestDir_K\u00f6rperlich\u00e4\u00df/",50"dir_Entt\u00e4uschung",51"file_Entt\u00e4uschung");5253test("TestDir_K\u00f6rperlich\u00e4\u00df/",54"dir_Entt\u00c4uschung",55"file_Entt\u00c4uschung");5657// Korean syblla58test("TestDir_\uac00\uac01\uac02",59"dir_\uac20\uac21\uac22",60"file_\uacc0\uacc1\uacc2");61}6263private static boolean equal(Object x, Object y) {64return x == null ? y == null : x.equals(y);65}6667private static boolean match(Path target, Path src) {68String fname = target.toString();69System.out.printf(" --> Trying [%s], length=%d...", fname, fname.length());70if (target.equals(src)) {71System.out.println(" MATCHED!");72return true;73} else {74System.out.println(" NOT MATCHED!");75}76return false;77}7879private static void test(String testdir, String dname, String fname_nfc)80throws Throwable81{82String fname = null;83String dname_nfd = Normalizer.normalize(dname, Normalizer.Form.NFD);84String fname_nfd = Normalizer.normalize(fname_nfc, Normalizer.Form.NFD);8586System.out.printf("%n%n--------Testing...----------%n");87Path bpath = Paths.get(testdir);88Path dpath = Paths.get(testdir, dname);89Path dpath_nfd = Paths.get(testdir, dname_nfd);90Path fpath_nfc = Paths.get(testdir, fname_nfc);91Path fpath_nfd = Paths.get(testdir, fname_nfd);9293if (Files.exists(bpath))94TestUtil.removeAll(bpath);95Files.createDirectories(dpath);9697fname = dpath.toString();98System.out.printf(":Directory [%s][len=%d] created%n", fname, fname.length());99100//////////////////////////////////////////////////////////////101if (!Files.isDirectory(dpath) || !Files.isDirectory(dpath_nfd)) {102throw new RuntimeException("Files.isDirectory(...) failed");103}104105//////////////////////////////////////////////////////////////106// write out with nfd, read in with nfc + case107Files.write(fpath_nfd, new byte[] { 'n', 'f', 'd'});108System.out.println(" read in with nfc (from nfd):" + new String(Files.readAllBytes(fpath_nfc)));109110// check attrs with nfc + case111Set<PosixFilePermission> pfp = Files.getPosixFilePermissions(fpath_nfd);112if (!equal(pfp, Files.getPosixFilePermissions(fpath_nfc)) ) {113throw new RuntimeException("Files.getPosixfilePermission(...) failed");114}115Files.delete(fpath_nfd);116117// write out with nfc, read in with nfd + case118Files.write(fpath_nfc, new byte[] { 'n', 'f', 'c'});119System.out.println(" read in with nfd (from nfc):" + new String(Files.readAllBytes(fpath_nfd)));120121// check attrs with nfc + case122pfp = Files.getPosixFilePermissions(fpath_nfc);123if (!equal(pfp, Files.getPosixFilePermissions(fpath_nfd))) {124throw new RuntimeException("Files.getPosixfilePermission(...) failed");125}126//////////////////////////////////////////////////////////////127boolean found_dir = false;128boolean found_file_nfc = false;129boolean found_file_nfd = false;130try (DirectoryStream<Path> stream = Files.newDirectoryStream(bpath)) {131for (Path path: stream) {132fname = path.toString();133System.out.printf("Found : [%s], length=%d%n", fname, fname.length());134found_dir |= match(dpath, path);135found_file_nfc |= match(fpath_nfc, path);136found_file_nfd |= match(fpath_nfd, path);137}138}139if (!found_dir || !found_file_nfc || !found_file_nfd) {140throw new RuntimeException("File.equal() failed");141}142// glob143String glob = "*" + fname_nfd.substring(2); // remove leading "FI" from "FILE..."144System.out.println("glob=" + glob);145boolean globmatched = false;146try (DirectoryStream<Path> stream = Files.newDirectoryStream(bpath, glob)) {147for (Path path: stream) {148fname = path.toString();149System.out.printf("PathMatch : [%s], length=%d%n", fname, fname.length());150globmatched |= match(fpath_nfc, path);151}152}153if (!globmatched) {154//throw new RuntimeException("path matcher failed");155// it appears we have a regex.anon_eq bug in hangul syllable handling156System.out.printf("pathmatcher failed, glob=[%s]%n", glob);157System.out.printf(" -> fname_nfd.matches(fname_nfc)=%b%n",158Pattern.compile(fname_nfd, Pattern.CANON_EQ)159.matcher(fname_nfc)160.matches());161System.out.printf(" -> fname_nfc.matches(fname_nfd)=%b%n",162Pattern.compile(fname_nfc, Pattern.CANON_EQ)163.matcher(fname_nfd)164.matches());165}166TestUtil.removeAll(bpath);167}168}169170171