Path: blob/master/test/jdk/java/io/File/MacPathTest.java
41149 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.io.File;24import java.io.FileInputStream;25import java.io.FileOutputStream;26import java.text.Normalizer;2728public class MacPathTest {2930public static void main(String args[]) throws Throwable {31String osname = System.getProperty("os.name");32if (!osname.contains("OS X") && !osname.contains("Darwin"))33return;3435// English36test("TestDir_apple", // test dir37"dir_macosx", // dir38"file_macosx"); // file3940// Japanese composite character41test("TestDir_\u30c8\u30a4\u30e4\u30cb\u30ca\u30eb/",42"dir_\u30a4\u30c1\u30b4\u306e\u30b1\u30fc\u30ad",43"file_\u30a4\u30c1\u30b4\u306e\u30b1\u30fc\u30ad");4445// latin-1 supplementory46test("TestDir_K\u00f6rperlich\u00e4\u00df/",47"dir_Entt\u00e4uschung",48"file_Entt\u00e4uschung");4950test("TestDir_K\u00f6rperlich\u00e4\u00df/",51"dir_Entt\u00c4uschung",52"file_Entt\u00c4uschung");5354// Korean syblla55test("TestDir_\uac00\uac01\uac02",56"dir_\uac20\uac21\uac22",57"file_\uacc0\uacc1\uacc2");58}5960private static void removeAll(File file) throws Throwable {61if (file.isDirectory()) {62for (File f : file.listFiles()) {63removeAll(f);64}65}66file.delete();67}6869private static boolean equal(Object x, Object y) {70return x == null ? y == null : x.equals(y);71}7273private static boolean match(File target, File src) {74if (target.equals(src)) {75String fname = target.toString();76System.out.printf(" ->matched : [%s], length=%d%n", fname, fname.length());77return true;78}79return false;80}8182private static void open_read(String what, File file) throws Throwable {83try (FileInputStream fis = new FileInputStream(file)) {84byte[] bytes = new byte[10];85fis.read(bytes);86System.out.printf(" %s:%s%n", what, new String(bytes));87}88}8990private static void test(String testdir, String dname, String fname_nfc)91throws Throwable92{93String fname = null;94String dname_nfd = Normalizer.normalize(dname, Normalizer.Form.NFD);95String fname_nfd = Normalizer.normalize(fname_nfc, Normalizer.Form.NFD);9697System.out.printf("%n%n--------Testing...----------%n");98File base = new File(testdir);99File dir = new File(base, dname);100File dir_nfd = new File(base, dname_nfd);101File file_nfc = new File(base, fname_nfc);102File file_nfd = new File(base, fname_nfd);103104System.out.printf("base :[%s][len=%d]%n", testdir, testdir.length());105System.out.printf("dir :[%s][len=%d]%n", dname, dname.length());106System.out.printf("fname_nfc :[%s][len=%d]%n", fname_nfc, fname_nfc.length());107System.out.printf("fname_nfd :[%s][len=%d]%n", fname_nfd, fname_nfd.length());108109fname = file_nfc.toString();110System.out.printf("file_nfc ->[%s][len=%d]%n", fname, fname.length());111fname = file_nfd.toString();112System.out.printf("file_nfd ->[%s][len=%d]%n%n", fname, fname.length());113114removeAll(base);115dir.mkdirs();116117fname = dir.toString();118System.out.printf(":Directory [%s][len=%d] created%n", fname, fname.length());119120//////////////////////////////////////////////////////////////121if (!dir.isDirectory() || !dir_nfd.isDirectory()) {122throw new RuntimeException("File.isDirectory() failed");123}124125//////////////////////////////////////////////////////////////126// write to via nfd127try (FileOutputStream fos = new FileOutputStream(file_nfd)) {128fos.write('n'); fos.write('f'); fos.write('d');129}130open_read("read in with nfc (from nfd)", file_nfc);131file_nfd.delete();132133//////////////////////////////////////////////////////////////134// write to with nfc135try (FileOutputStream fos = new FileOutputStream(file_nfc)) {136fos.write('n'); fos.write('f'); fos.write('c');137}138open_read("read in with nfd (from nfc)", file_nfd);139//file_nfc.delete();140141//////////////////////////////////////////////////////////////142boolean found_dir = false;143boolean found_file_nfc = false;144boolean found_file_nfd = false;145146for (File f : base.listFiles()) {147fname = f.toString();148System.out.printf("Found : [%s], length=%d%n", fname, fname.length());149found_dir |= match(dir, f);150found_file_nfc |= match(file_nfc, f);151found_file_nfd |= match(file_nfd, f);152}153154if (!found_dir || !found_file_nfc || !found_file_nfc) {155throw new RuntimeException("File.equal() failed");156}157removeAll(base);158}159}160161162