5389N/A/*
5389N/A * Copyright (c) 2008, 2012, Oracle and/or its affiliates. All rights reserved.
5389N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5389N/A *
5389N/A * This code is free software; you can redistribute it and/or modify it
5389N/A * under the terms of the GNU General Public License version 2 only, as
5389N/A * published by the Free Software Foundation.
5389N/A *
5389N/A * This code is distributed in the hope that it will be useful, but WITHOUT
5389N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
5389N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
5389N/A * version 2 for more details (a copy is included in the LICENSE file that
5389N/A * accompanied this code).
5389N/A *
5389N/A * You should have received a copy of the GNU General Public License version
5389N/A * 2 along with this work; if not, write to the Free Software Foundation,
5389N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
5389N/A *
5389N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
5389N/A * or visit www.oracle.com if you need additional information or have any
5389N/A * questions.
5389N/A */
5389N/A
5389N/A/* @test
5389N/A * @bug 7130915
5389N/A * @summary Tests file path with nfc/nfd forms on MacOSX
5389N/A * @build MacPathTest
5389N/A * @run shell MacPathTest.sh
5389N/A */
5389N/A
5389N/Aimport java.io.*;
5389N/Aimport java.text.*;
5389N/Aimport java.util.*;
5389N/A
5389N/Apublic class MacPathTest {
5389N/A
5389N/A public static void main(String args[]) throws Throwable {
5389N/A String osname = System.getProperty("os.name");
5389N/A if (!osname.contains("OS X") && !osname.contains("Darwin"))
5389N/A return;
5389N/A
5389N/A // English
5389N/A test("TestDir_apple", // test dir
5389N/A "dir_macosx", // dir
5389N/A "file_macosx"); // file
5389N/A
5389N/A // Japanese composite character
5389N/A test("TestDir_\u30c8\u30a4\u30e4\u30cb\u30ca\u30eb/",
5389N/A "dir_\u30a4\u30c1\u30b4\u306e\u30b1\u30fc\u30ad",
5389N/A "file_\u30a4\u30c1\u30b4\u306e\u30b1\u30fc\u30ad");
5389N/A
5389N/A // latin-1 supplementory
5389N/A test("TestDir_K\u00f6rperlich\u00e4\u00df/",
5389N/A "dir_Entt\u00e4uschung",
5389N/A "file_Entt\u00e4uschung");
5389N/A
5389N/A test("TestDir_K\u00f6rperlich\u00e4\u00df/",
5389N/A "dir_Entt\u00c4uschung",
5389N/A "file_Entt\u00c4uschung");
5389N/A
5389N/A // Korean syblla
5389N/A test("TestDir_\uac00\uac01\uac02",
5389N/A "dir_\uac20\uac21\uac22",
5389N/A "file_\uacc0\uacc1\uacc2");
5389N/A }
5389N/A
5389N/A private static void removeAll(File file) throws Throwable {
5389N/A if (file.isDirectory()) {
5389N/A for (File f : file.listFiles()) {
5389N/A removeAll(f);
5389N/A }
5389N/A }
5389N/A file.delete();
5389N/A }
5389N/A
5389N/A private static boolean equal(Object x, Object y) {
5389N/A return x == null ? y == null : x.equals(y);
5389N/A }
5389N/A
5389N/A private static boolean match(File target, File src) {
5389N/A if (target.equals(src)) {
5389N/A String fname = target.toString();
5389N/A System.out.printf(" ->matched : [%s], length=%d%n", fname, fname.length());
5389N/A return true;
5389N/A }
5389N/A return false;
5389N/A }
5389N/A
5389N/A private static void open_read(String what, File file) throws Throwable {
5389N/A try (FileInputStream fis = new FileInputStream(file)) {
5389N/A byte[] bytes = new byte[10];
5389N/A fis.read(bytes);
5389N/A System.out.printf(" %s:%s%n", what, new String(bytes));
5389N/A }
5389N/A }
5389N/A
5389N/A private static void test(String testdir, String dname, String fname_nfc)
5389N/A throws Throwable
5389N/A {
5389N/A String fname = null;
5389N/A String dname_nfd = Normalizer.normalize(dname, Normalizer.Form.NFD);
5389N/A String fname_nfd = Normalizer.normalize(fname_nfc, Normalizer.Form.NFD);
5389N/A
5389N/A System.out.printf("%n%n--------Testing...----------%n");
5389N/A File base = new File(testdir);
5389N/A File dir = new File(base, dname);
5389N/A File dir_nfd = new File(base, dname_nfd);
5389N/A File file_nfc = new File(base, fname_nfc);
5389N/A File file_nfd = new File(base, fname_nfd);
5389N/A
5389N/A System.out.printf("base :[%s][len=%d]%n", testdir, testdir.length());
5389N/A System.out.printf("dir :[%s][len=%d]%n", dname, dname.length());
5389N/A System.out.printf("fname_nfc :[%s][len=%d]%n", fname_nfc, fname_nfc.length());
5389N/A System.out.printf("fname_nfd :[%s][len=%d]%n", fname_nfd, fname_nfd.length());
5389N/A
5389N/A fname = file_nfc.toString();
5389N/A System.out.printf("file_nfc ->[%s][len=%d]%n", fname, fname.length());
5389N/A fname = file_nfd.toString();
5389N/A System.out.printf("file_nfd ->[%s][len=%d]%n%n", fname, fname.length());
5389N/A
5389N/A removeAll(base);
5389N/A dir.mkdirs();
5389N/A
5389N/A fname = dir.toString();
5389N/A System.out.printf(":Directory [%s][len=%d] created%n", fname, fname.length());
5389N/A
5389N/A //////////////////////////////////////////////////////////////
5389N/A if (!dir.isDirectory() || !dir_nfd.isDirectory()) {
5389N/A throw new RuntimeException("File.isDirectory() failed");
5389N/A }
5389N/A
5389N/A //////////////////////////////////////////////////////////////
5389N/A // write to via nfd
5389N/A try (FileOutputStream fos = new FileOutputStream(file_nfd)) {
5389N/A fos.write('n'); fos.write('f'); fos.write('d');
5389N/A }
5389N/A open_read("read in with nfc (from nfd)", file_nfc);
5389N/A file_nfd.delete();
5389N/A
5389N/A //////////////////////////////////////////////////////////////
5389N/A // write to with nfc
5389N/A try (FileOutputStream fos = new FileOutputStream(file_nfc)) {
5389N/A fos.write('n'); fos.write('f'); fos.write('c');
5389N/A }
5389N/A open_read("read in with nfd (from nfc)", file_nfd);
5389N/A //file_nfc.delete();
5389N/A
5389N/A //////////////////////////////////////////////////////////////
5389N/A boolean found_dir = false;
5389N/A boolean found_file_nfc = false;
5389N/A boolean found_file_nfd = false;
5389N/A
5389N/A for (File f : base.listFiles()) {
5389N/A fname = f.toString();
5389N/A System.out.printf("Found : [%s], length=%d%n", fname, fname.length());
5389N/A found_dir |= match(dir, f);
5389N/A found_file_nfc |= match(file_nfc, f);
5389N/A found_file_nfd |= match(file_nfd, f);
5389N/A }
5389N/A
5389N/A if (!found_dir || !found_file_nfc || !found_file_nfc) {
5389N/A throw new RuntimeException("File.equal() failed");
5389N/A }
5389N/A removeAll(base);
5389N/A }
5389N/A}