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