IsHidden.java revision 2497:5919f0c72c0b
1229997Sken/*
2229997Sken * Copyright (c) 1998, 2007, Oracle and/or its affiliates. All rights reserved.
3290776Smav * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4229997Sken *
5229997Sken * This code is free software; you can redistribute it and/or modify it
6229997Sken * under the terms of the GNU General Public License version 2 only, as
7229997Sken * published by the Free Software Foundation.
8229997Sken *
9229997Sken * This code is distributed in the hope that it will be useful, but WITHOUT
10229997Sken * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11229997Sken * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12229997Sken * version 2 for more details (a copy is included in the LICENSE file that
13229997Sken * accompanied this code).
14229997Sken *
15229997Sken * You should have received a copy of the GNU General Public License version
16229997Sken * 2 along with this work; if not, write to the Free Software Foundation,
17229997Sken * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18229997Sken *
19229997Sken * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20229997Sken * or visit www.oracle.com if you need additional information or have any
21229997Sken * questions.
22229997Sken */
23229997Sken
24229997Sken/* @test
25229997Sken   @bug 4131223 6470354
26229997Sken   @summary Basic test for isHidden method
27229997Sken */
28229997Sken
29229997Skenimport java.io.*;
30229997Skenimport java.nio.file.attribute.DosFileAttributeView;
31229997Sken
32229997Skenpublic class IsHidden {
33229997Sken
34229997Sken    private static String dir = System.getProperty("test.dir", ".");
35229997Sken
36229997Sken    private static void ck(String path, boolean ans) throws Exception {
37229997Sken        File f = new File(path);
38229997Sken        boolean x = f.isHidden();
39229997Sken        if (x != ans)
40229997Sken            throw new Exception(path + ": expected " + ans + ", got " + x);
41229997Sken        System.err.println(path + " ==> " + x);
42229997Sken    }
43229997Sken
44229997Sken    private static void setHidden(File f, boolean value) throws IOException {
45229997Sken        f.toPath().getFileAttributeView(DosFileAttributeView.class).setHidden(value);
46229997Sken    }
47229997Sken
48229997Sken    private static void testWin32() throws Exception {
49229997Sken        File f = new File(dir, "test");
50229997Sken        f.deleteOnExit();
51229997Sken        f.createNewFile();
52229997Sken        setHidden(f, true);
53229997Sken        try {
54229997Sken            ck(f.getPath(), true);
55229997Sken        } finally {
56229997Sken            setHidden(f, false);
57229997Sken        }
58229997Sken        ck(".foo", false);
59229997Sken        ck("foo", false);
60229997Sken    }
61229997Sken
62229997Sken    private static void testUnix() throws Exception {
63229997Sken        ck(dir + "/IsHidden.java", false);
64229997Sken        ck(dir + "/.", true);
65229997Sken        ck(".", true);
66229997Sken        ck("..", true);
67229997Sken        ck(".foo", true);
68229997Sken        ck("foo", false);
69229997Sken        ck("", false);
70229997Sken    }
71229997Sken
72229997Sken    public static void main(String[] args) throws Exception {
73229997Sken        if (File.separatorChar == '\\') testWin32();
74229997Sken        if (File.separatorChar == '/') testUnix();
75229997Sken    }
76229997Sken
77290776Smav}
78290776Smav