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
24import java.io.File;
25import java.io.FileInputStream;
26import java.io.FileOutputStream;
27import java.text.Normalizer;
28
29public class MacPathTest {
30
31    public static void main(String args[]) throws Throwable {
32        String osname = System.getProperty("os.name");
33        if (!osname.contains("OS X") && !osname.contains("Darwin"))
34            return;
35
36        // English
37        test("TestDir_apple",                                    // test dir
38             "dir_macosx",                                       // dir
39             "file_macosx");                                     // file
40
41        // Japanese composite character
42        test("TestDir_\u30c8\u30a4\u30e4\u30cb\u30ca\u30eb/",
43             "dir_\u30a4\u30c1\u30b4\u306e\u30b1\u30fc\u30ad",
44             "file_\u30a4\u30c1\u30b4\u306e\u30b1\u30fc\u30ad");
45
46        // latin-1 supplementory
47        test("TestDir_K\u00f6rperlich\u00e4\u00df/",
48             "dir_Entt\u00e4uschung",
49             "file_Entt\u00e4uschung");
50
51        test("TestDir_K\u00f6rperlich\u00e4\u00df/",
52             "dir_Entt\u00c4uschung",
53             "file_Entt\u00c4uschung");
54
55        // Korean syblla
56        test("TestDir_\uac00\uac01\uac02",
57             "dir_\uac20\uac21\uac22",
58             "file_\uacc0\uacc1\uacc2");
59    }
60
61    private static void removeAll(File file) throws Throwable {
62        if (file.isDirectory()) {
63            for (File f : file.listFiles()) {
64                removeAll(f);
65            }
66        }
67        file.delete();
68    }
69
70    private static boolean equal(Object x, Object y) {
71        return x == null ? y == null : x.equals(y);
72    }
73
74    private static boolean match(File target, File src) {
75        if (target.equals(src)) {
76            String fname = target.toString();
77            System.out.printf("    ->matched   : [%s], length=%d%n", fname, fname.length());
78            return true;
79        }
80        return false;
81    }
82
83    private static void open_read(String what, File file) throws Throwable {
84        try (FileInputStream fis = new FileInputStream(file)) {
85           byte[] bytes = new byte[10];
86           fis.read(bytes);
87           System.out.printf("    %s:%s%n", what, new String(bytes));
88        }
89    }
90
91    private static void test(String testdir, String dname, String fname_nfc)
92        throws Throwable
93    {
94        String fname = null;
95        String dname_nfd = Normalizer.normalize(dname, Normalizer.Form.NFD);
96        String fname_nfd = Normalizer.normalize(fname_nfc, Normalizer.Form.NFD);
97
98        System.out.printf("%n%n--------Testing...----------%n");
99        File base = new File(testdir);
100        File dir  = new File(base, dname);
101        File dir_nfd =  new File(base, dname_nfd);
102        File file_nfc = new File(base, fname_nfc);
103        File file_nfd = new File(base, fname_nfd);
104
105        System.out.printf("base           :[%s][len=%d]%n", testdir, testdir.length());
106        System.out.printf("dir            :[%s][len=%d]%n", dname, dname.length());
107        System.out.printf("fname_nfc      :[%s][len=%d]%n", fname_nfc, fname_nfc.length());
108        System.out.printf("fname_nfd      :[%s][len=%d]%n", fname_nfd, fname_nfd.length());
109
110        fname = file_nfc.toString();
111        System.out.printf("file_nfc ->[%s][len=%d]%n", fname, fname.length());
112        fname = file_nfd.toString();
113        System.out.printf("file_nfd ->[%s][len=%d]%n%n", fname, fname.length());
114
115        removeAll(base);
116        dir.mkdirs();
117
118        fname = dir.toString();
119        System.out.printf(":Directory [%s][len=%d] created%n", fname, fname.length());
120
121        //////////////////////////////////////////////////////////////
122        if (!dir.isDirectory() || !dir_nfd.isDirectory()) {
123            throw new RuntimeException("File.isDirectory() failed");
124        }
125
126        //////////////////////////////////////////////////////////////
127        // write to via nfd
128        try (FileOutputStream fos = new FileOutputStream(file_nfd)) {
129           fos.write('n'); fos.write('f'); fos.write('d');
130        }
131        open_read("read in with nfc (from nfd)", file_nfc);
132        file_nfd.delete();
133
134        //////////////////////////////////////////////////////////////
135        // write to with nfc
136        try (FileOutputStream fos = new FileOutputStream(file_nfc)) {
137           fos.write('n'); fos.write('f'); fos.write('c');
138        }
139        open_read("read in with nfd      (from nfc)", file_nfd);
140        //file_nfc.delete();
141
142        //////////////////////////////////////////////////////////////
143        boolean found_dir = false;
144        boolean found_file_nfc = false;
145        boolean found_file_nfd = false;
146
147        for (File f : base.listFiles()) {
148            fname = f.toString();
149            System.out.printf("Found   : [%s], length=%d%n", fname, fname.length());
150            found_dir      |= match(dir, f);
151            found_file_nfc |= match(file_nfc, f);
152            found_file_nfd |= match(file_nfd, f);
153        }
154
155        if (!found_dir || !found_file_nfc || !found_file_nfc) {
156            throw new RuntimeException("File.equal() failed");
157        }
158        removeAll(base);
159    }
160}
161