1/*
2 * Copyright (c) 2004, 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 5016938
26 * @summary Test file operations with Unicode filenames
27 * @author Martin Buchholz
28 */
29
30import java.io.*;
31
32public class Unicode
33{
34    static int fail = 0;
35    static void fail(String msg) {
36        fail++;
37        System.err.println(msg);
38    }
39
40    static boolean creat(File f) throws Exception {
41        try {
42            FileOutputStream out = new FileOutputStream(f);
43            out.write(new byte[]{'a', 'b', 'c'});
44            out.close();
45            // Check that the file we tried to create has the expected name
46            return find(f);
47        } catch (Exception e) {
48            return false;
49        }
50    }
51
52    static boolean find(File f) throws Exception {
53        String fn = f.getPath();
54        String[] fns = new File(".").list();
55        for (int i = 0; i < fns.length; i++)
56            if (fns[i].equals(fn))
57                return true;
58        return false;
59    }
60
61    static void sanityCheck(File f) throws Exception {
62        if (! f.exists())      fail("! f.exists()");
63        if (  f.length() != 3) fail("  f.length() != 3");
64        if (  f.isAbsolute())  fail("  f.isAbsolute()");
65        if (! f.canRead())     fail("! f.canRead()");
66        if (! f.canWrite())    fail("! f.canWrite()");
67        if (  f.isHidden())    fail("  f.isHidden()");
68        if (! f.isFile())      fail("! f.isFile()");
69        if (  f.isDirectory()) fail("  f.isDirectory()");
70    }
71
72    public static void main(String [] args) throws Exception {
73        final File f1 = new File("\u0411.tst");
74        final File f2 = new File("\u0412.tst");
75
76        try {
77            if (! creat(f1))
78                // Couldn't create file with Unicode filename?
79                return;
80
81            System.out.println("This system supports Unicode filenames!");
82            sanityCheck(f1);
83
84            f1.renameTo(f2);
85            sanityCheck(f2);
86            if (! f2.delete()) fail("! f2.delete()");
87            if (  f2.exists()) fail("  f2.exists()");
88            if (  f1.exists()) fail("  f1.exists()");
89            if (  f1.delete()) fail("  f1.delete()");
90
91            if (fail != 0) throw new Exception(fail + " failures");
92        } finally {
93            f1.delete();
94            f2.delete();
95        }
96    }
97}
98