1 /*
   2  * Copyright (c) 2008, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /* @test
  25  * @bug 7130915
  26  * @summary Tests file path with nfc/nfd forms on MacOSX
  27  * @requires (os.family == "mac")
  28  * @build MacPathTest
  29  * @run main/othervm -Duser.language=en -Duser.country=US -Dfile.encoding=UTF-8 -Dsun.jnu.encoding=UTF-8 MacPathTest
  30  */
  31 
  32 import java.io.File;
  33 import java.io.FileInputStream;
  34 import java.io.FileOutputStream;
  35 import java.text.Normalizer;
  36 
  37 public class MacPathTest {
  38 
  39     public static void main(String args[]) throws Throwable {
  40 
  41         // English
  42         test("TestDir_apple",                                    // test dir
  43              "dir_macosx",                                       // dir
  44              "file_macosx");                                     // file
  45 
  46         // Japanese composite character
  47         test("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");
  50 
  51         // latin-1 supplementory
  52         test("TestDir_K\u00f6rperlich\u00e4\u00df/",
  53              "dir_Entt\u00e4uschung",
  54              "file_Entt\u00e4uschung");
  55 
  56         test("TestDir_K\u00f6rperlich\u00e4\u00df/",
  57              "dir_Entt\u00c4uschung",
  58              "file_Entt\u00c4uschung");
  59 
  60         // Korean syblla
  61         test("TestDir_\uac00\uac01\uac02",
  62              "dir_\uac20\uac21\uac22",
  63              "file_\uacc0\uacc1\uacc2");
  64     }
  65 
  66     private static void removeAll(File file) throws Throwable {
  67         if (file.isDirectory()) {
  68             for (File f : file.listFiles()) {
  69                 removeAll(f);
  70             }
  71         }
  72         file.delete();
  73     }
  74 
  75     private static boolean equal(Object x, Object y) {
  76         return x == null ? y == null : x.equals(y);
  77     }
  78 
  79     private static boolean match(File target, File src) {
  80         if (target.equals(src)) {
  81             String fname = target.toString();
  82             System.out.printf("    ->matched   : [%s], length=%d%n", fname, fname.length());
  83             return true;
  84         }
  85         return false;
  86     }
  87 
  88     private static void open_read(String what, File file) throws Throwable {
  89         try (FileInputStream fis = new FileInputStream(file)) {
  90            byte[] bytes = new byte[10];
  91            fis.read(bytes);
  92            System.out.printf("    %s:%s%n", what, new String(bytes));
  93         }
  94     }
  95 
  96     private static void test(String testdir, String dname, String fname_nfc)
  97         throws Throwable
  98     {
  99         String fname = null;
 100         String dname_nfd = Normalizer.normalize(dname, Normalizer.Form.NFD);
 101         String fname_nfd = Normalizer.normalize(fname_nfc, Normalizer.Form.NFD);
 102 
 103         System.out.printf("%n%n--------Testing...----------%n");
 104         File base = new File(testdir);
 105         File dir  = new File(base, dname);
 106         File dir_nfd =  new File(base, dname_nfd);
 107         File file_nfc = new File(base, fname_nfc);
 108         File file_nfd = new File(base, fname_nfd);
 109 
 110         System.out.printf("base           :[%s][len=%d]%n", testdir, testdir.length());
 111         System.out.printf("dir            :[%s][len=%d]%n", dname, dname.length());
 112         System.out.printf("fname_nfc      :[%s][len=%d]%n", fname_nfc, fname_nfc.length());
 113         System.out.printf("fname_nfd      :[%s][len=%d]%n", fname_nfd, fname_nfd.length());
 114 
 115         fname = file_nfc.toString();
 116         System.out.printf("file_nfc ->[%s][len=%d]%n", fname, fname.length());
 117         fname = file_nfd.toString();
 118         System.out.printf("file_nfd ->[%s][len=%d]%n%n", fname, fname.length());
 119 
 120         removeAll(base);
 121         dir.mkdirs();
 122 
 123         fname = dir.toString();
 124         System.out.printf(":Directory [%s][len=%d] created%n", fname, fname.length());
 125 
 126         //////////////////////////////////////////////////////////////
 127         if (!dir.isDirectory() || !dir_nfd.isDirectory()) {
 128             throw new RuntimeException("File.isDirectory() failed");
 129         }
 130 
 131         //////////////////////////////////////////////////////////////
 132         // write to via nfd
 133         try (FileOutputStream fos = new FileOutputStream(file_nfd)) {
 134            fos.write('n'); fos.write('f'); fos.write('d');
 135         }
 136         open_read("read in with nfc (from nfd)", file_nfc);
 137         file_nfd.delete();
 138 
 139         //////////////////////////////////////////////////////////////
 140         // write to with nfc
 141         try (FileOutputStream fos = new FileOutputStream(file_nfc)) {
 142            fos.write('n'); fos.write('f'); fos.write('c');
 143         }
 144         open_read("read in with nfd      (from nfc)", file_nfd);
 145         //file_nfc.delete();
 146 
 147         //////////////////////////////////////////////////////////////
 148         boolean found_dir = false;
 149         boolean found_file_nfc = false;
 150         boolean found_file_nfd = false;
 151 
 152         for (File f : base.listFiles()) {
 153             fname = f.toString();
 154             System.out.printf("Found   : [%s], length=%d%n", fname, fname.length());
 155             found_dir      |= match(dir, f);
 156             found_file_nfc |= match(file_nfc, f);
 157             found_file_nfd |= match(file_nfd, f);
 158         }
 159 
 160         if (!found_dir || !found_file_nfc || !found_file_nfc) {
 161             throw new RuntimeException("File.equal() failed");
 162         }
 163         removeAll(base);
 164     }
 165 }