Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/io/File/MacPathTest.java
38811 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* @build MacPathTest27* @run shell MacPathTest.sh28*/2930import java.io.*;31import java.text.*;32import java.util.*;3334public class MacPathTest {3536public static void main(String args[]) throws Throwable {37String osname = System.getProperty("os.name");38if (!osname.contains("OS X") && !osname.contains("Darwin"))39return;4041// English42test("TestDir_apple", // test dir43"dir_macosx", // dir44"file_macosx"); // file4546// Japanese composite character47test("TestDir_\u30c8\u30a4\u30e4\u30cb\u30ca\u30eb/",48"dir_\u30a4\u30c1\u30b4\u306e\u30b1\u30fc\u30ad",49"file_\u30a4\u30c1\u30b4\u306e\u30b1\u30fc\u30ad");5051// latin-1 supplementory52test("TestDir_K\u00f6rperlich\u00e4\u00df/",53"dir_Entt\u00e4uschung",54"file_Entt\u00e4uschung");5556test("TestDir_K\u00f6rperlich\u00e4\u00df/",57"dir_Entt\u00c4uschung",58"file_Entt\u00c4uschung");5960// Korean syblla61test("TestDir_\uac00\uac01\uac02",62"dir_\uac20\uac21\uac22",63"file_\uacc0\uacc1\uacc2");64}6566private static void removeAll(File file) throws Throwable {67if (file.isDirectory()) {68for (File f : file.listFiles()) {69removeAll(f);70}71}72file.delete();73}7475private static boolean equal(Object x, Object y) {76return x == null ? y == null : x.equals(y);77}7879private static boolean match(File target, File src) {80if (target.equals(src)) {81String fname = target.toString();82System.out.printf(" ->matched : [%s], length=%d%n", fname, fname.length());83return true;84}85return false;86}8788private static void open_read(String what, File file) throws Throwable {89try (FileInputStream fis = new FileInputStream(file)) {90byte[] bytes = new byte[10];91fis.read(bytes);92System.out.printf(" %s:%s%n", what, new String(bytes));93}94}9596private static void test(String testdir, String dname, String fname_nfc)97throws Throwable98{99String fname = null;100String dname_nfd = Normalizer.normalize(dname, Normalizer.Form.NFD);101String fname_nfd = Normalizer.normalize(fname_nfc, Normalizer.Form.NFD);102103System.out.printf("%n%n--------Testing...----------%n");104File base = new File(testdir);105File dir = new File(base, dname);106File dir_nfd = new File(base, dname_nfd);107File file_nfc = new File(base, fname_nfc);108File file_nfd = new File(base, fname_nfd);109110System.out.printf("base :[%s][len=%d]%n", testdir, testdir.length());111System.out.printf("dir :[%s][len=%d]%n", dname, dname.length());112System.out.printf("fname_nfc :[%s][len=%d]%n", fname_nfc, fname_nfc.length());113System.out.printf("fname_nfd :[%s][len=%d]%n", fname_nfd, fname_nfd.length());114115fname = file_nfc.toString();116System.out.printf("file_nfc ->[%s][len=%d]%n", fname, fname.length());117fname = file_nfd.toString();118System.out.printf("file_nfd ->[%s][len=%d]%n%n", fname, fname.length());119120removeAll(base);121dir.mkdirs();122123fname = dir.toString();124System.out.printf(":Directory [%s][len=%d] created%n", fname, fname.length());125126//////////////////////////////////////////////////////////////127if (!dir.isDirectory() || !dir_nfd.isDirectory()) {128throw new RuntimeException("File.isDirectory() failed");129}130131//////////////////////////////////////////////////////////////132// write to via nfd133try (FileOutputStream fos = new FileOutputStream(file_nfd)) {134fos.write('n'); fos.write('f'); fos.write('d');135}136open_read("read in with nfc (from nfd)", file_nfc);137file_nfd.delete();138139//////////////////////////////////////////////////////////////140// write to with nfc141try (FileOutputStream fos = new FileOutputStream(file_nfc)) {142fos.write('n'); fos.write('f'); fos.write('c');143}144open_read("read in with nfd (from nfc)", file_nfd);145//file_nfc.delete();146147//////////////////////////////////////////////////////////////148boolean found_dir = false;149boolean found_file_nfc = false;150boolean found_file_nfd = false;151152for (File f : base.listFiles()) {153fname = f.toString();154System.out.printf("Found : [%s], length=%d%n", fname, fname.length());155found_dir |= match(dir, f);156found_file_nfc |= match(file_nfc, f);157found_file_nfd |= match(file_nfd, f);158}159160if (!found_dir || !found_file_nfc || !found_file_nfc) {161throw new RuntimeException("File.equal() failed");162}163removeAll(base);164}165}166167168