Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/nio/file/Path/MacPathTest.java
38828 views
/*1* Copyright (c) 2008, 2012, 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*/2223/* @test24* @bug 713091525* @summary Tests file path with nfc/nfd forms on MacOSX26* @library ../27* @build MacPathTest28* @run shell MacPathTest.sh29*/3031import java.nio.file.*;32import java.nio.file.attribute.*;33import java.text.*;34import java.util.*;35import java.util.regex.*;3637public class MacPathTest {3839public static void main(String args[]) throws Throwable {40String osname = System.getProperty("os.name");41if (!osname.contains("OS X") && !osname.contains("Darwin"))42return;43System.out.printf("sun.jnu.encoding=%s, file.encoding=%s%n",44System.getProperty("file.encoding"),45System.getProperty("sun.jnu.encoding"));46// English47test("TestDir_apple", // test dir48"dir_macosx", // dir49"file_macosx"); // file5051// Japanese composite character52test("TestDir_\u30c8\u30a4\u30e4\u30cb\u30ca\u30eb/",53"dir_\u30a4\u30c1\u30b4\u306e\u30b1\u30fc\u30ad",54"file_\u30a4\u30c1\u30b4\u306e\u30b1\u30fc\u30ad");5556// latin-1 supplementory57test("TestDir_K\u00f6rperlich\u00e4\u00df/",58"dir_Entt\u00e4uschung",59"file_Entt\u00e4uschung");6061test("TestDir_K\u00f6rperlich\u00e4\u00df/",62"dir_Entt\u00c4uschung",63"file_Entt\u00c4uschung");6465// Korean syblla66test("TestDir_\uac00\uac01\uac02",67"dir_\uac20\uac21\uac22",68"file_\uacc0\uacc1\uacc2");69}7071private static boolean equal(Object x, Object y) {72return x == null ? y == null : x.equals(y);73}7475private static boolean match(Path target, Path src) {76String fname = target.toString();77System.out.printf(" --> Trying [%s], length=%d...", fname, fname.length());78if (target.equals(src)) {79System.out.println(" MATCHED!");80return true;81} else {82System.out.println(" NOT MATCHED!");83}84return false;85}8687private static void test(String testdir, String dname, String fname_nfc)88throws Throwable89{90String fname = null;91String dname_nfd = Normalizer.normalize(dname, Normalizer.Form.NFD);92String fname_nfd = Normalizer.normalize(fname_nfc, Normalizer.Form.NFD);9394System.out.printf("%n%n--------Testing...----------%n");95Path bpath = Paths.get(testdir);96Path dpath = Paths.get(testdir, dname);97Path dpath_nfd = Paths.get(testdir, dname_nfd);98Path fpath_nfc = Paths.get(testdir, fname_nfc);99Path fpath_nfd = Paths.get(testdir, fname_nfd);100101if (Files.exists(bpath))102TestUtil.removeAll(bpath);103Files.createDirectories(dpath);104105fname = dpath.toString();106System.out.printf(":Directory [%s][len=%d] created%n", fname, fname.length());107108//////////////////////////////////////////////////////////////109if (!Files.isDirectory(dpath) || !Files.isDirectory(dpath_nfd)) {110throw new RuntimeException("Files.isDirectory(...) failed");111}112113//////////////////////////////////////////////////////////////114// write out with nfd, read in with nfc + case115Files.write(fpath_nfd, new byte[] { 'n', 'f', 'd'});116System.out.println(" read in with nfc (from nfd):" + new String(Files.readAllBytes(fpath_nfc)));117118// check attrs with nfc + case119Set<PosixFilePermission> pfp = Files.getPosixFilePermissions(fpath_nfd);120if (!equal(pfp, Files.getPosixFilePermissions(fpath_nfc)) ) {121throw new RuntimeException("Files.getPosixfilePermission(...) failed");122}123Files.delete(fpath_nfd);124125// write out with nfc, read in with nfd + case126Files.write(fpath_nfc, new byte[] { 'n', 'f', 'c'});127System.out.println(" read in with nfd (from nfc):" + new String(Files.readAllBytes(fpath_nfd)));128129// check attrs with nfc + case130pfp = Files.getPosixFilePermissions(fpath_nfc);131if (!equal(pfp, Files.getPosixFilePermissions(fpath_nfd))) {132throw new RuntimeException("Files.getPosixfilePermission(...) failed");133}134//////////////////////////////////////////////////////////////135boolean found_dir = false;136boolean found_file_nfc = false;137boolean found_file_nfd = false;138try (DirectoryStream<Path> stream = Files.newDirectoryStream(bpath)) {139for (Path path: stream) {140fname = path.toString();141System.out.printf("Found : [%s], length=%d%n", fname, fname.length());142found_dir |= match(dpath, path);143found_file_nfc |= match(fpath_nfc, path);144found_file_nfd |= match(fpath_nfd, path);145}146}147if (!found_dir || !found_file_nfc || !found_file_nfd) {148throw new RuntimeException("File.equal() failed");149}150// glob151String glob = "*" + fname_nfd.substring(2); // remove leading "FI" from "FILE..."152System.out.println("glob=" + glob);153boolean globmatched = false;154try (DirectoryStream<Path> stream = Files.newDirectoryStream(bpath, glob)) {155for (Path path: stream) {156fname = path.toString();157System.out.printf("PathMatch : [%s], length=%d%n", fname, fname.length());158globmatched |= match(fpath_nfc, path);159}160}161if (!globmatched) {162//throw new RuntimeException("path matcher failed");163// it appears we have a regex.anon_eq bug in hangul syllable handling164System.out.printf("pathmatcher failed, glob=[%s]%n", glob);165System.out.printf(" -> fname_nfd.matches(fname_nfc)=%b%n",166Pattern.compile(fname_nfd, Pattern.CANON_EQ)167.matcher(fname_nfc)168.matches());169System.out.printf(" -> fname_nfc.matches(fname_nfd)=%b%n",170Pattern.compile(fname_nfc, Pattern.CANON_EQ)171.matcher(fname_nfd)172.matches());173}174TestUtil.removeAll(bpath);175}176}177178179