IsHidden.java revision 2497:5919f0c72c0b
133965Sjdp/*
278828Sobrien * Copyright (c) 1998, 2007, Oracle and/or its affiliates. All rights reserved.
378828Sobrien * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
478828Sobrien *
578828Sobrien * This code is free software; you can redistribute it and/or modify it
678828Sobrien * under the terms of the GNU General Public License version 2 only, as
778828Sobrien * published by the Free Software Foundation.
878828Sobrien *
978828Sobrien * This code is distributed in the hope that it will be useful, but WITHOUT
1078828Sobrien * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1178828Sobrien * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1278828Sobrien * version 2 for more details (a copy is included in the LICENSE file that
1378828Sobrien * accompanied this code).
1478828Sobrien *
1578828Sobrien * You should have received a copy of the GNU General Public License version
1678828Sobrien * 2 along with this work; if not, write to the Free Software Foundation,
17218822Sdim * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1878828Sobrien *
1933965Sjdp * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2033965Sjdp * or visit www.oracle.com if you need additional information or have any
2133965Sjdp * questions.
2233965Sjdp */
2333965Sjdp
2433965Sjdp/* @test
2533965Sjdp   @bug 4131223 6470354
2633965Sjdp   @summary Basic test for isHidden method
2733965Sjdp */
2833965Sjdp
2933965Sjdpimport java.io.*;
3033965Sjdpimport java.nio.file.attribute.DosFileAttributeView;
3133965Sjdp
3233965Sjdppublic class IsHidden {
3333965Sjdp
3433965Sjdp    private static String dir = System.getProperty("test.dir", ".");
3533965Sjdp
3633965Sjdp    private static void ck(String path, boolean ans) throws Exception {
3733965Sjdp        File f = new File(path);
3833965Sjdp        boolean x = f.isHidden();
3933965Sjdp        if (x != ans)
4033965Sjdp            throw new Exception(path + ": expected " + ans + ", got " + x);
4133965Sjdp        System.err.println(path + " ==> " + x);
4233965Sjdp    }
4333965Sjdp
4433965Sjdp    private static void setHidden(File f, boolean value) throws IOException {
4533965Sjdp        f.toPath().getFileAttributeView(DosFileAttributeView.class).setHidden(value);
4633965Sjdp    }
4733965Sjdp
4833965Sjdp    private static void testWin32() throws Exception {
49        File f = new File(dir, "test");
50        f.deleteOnExit();
51        f.createNewFile();
52        setHidden(f, true);
53        try {
54            ck(f.getPath(), true);
55        } finally {
56            setHidden(f, false);
57        }
58        ck(".foo", false);
59        ck("foo", false);
60    }
61
62    private static void testUnix() throws Exception {
63        ck(dir + "/IsHidden.java", false);
64        ck(dir + "/.", true);
65        ck(".", true);
66        ck("..", true);
67        ck(".foo", true);
68        ck("foo", false);
69        ck("", false);
70    }
71
72    public static void main(String[] args) throws Exception {
73        if (File.separatorChar == '\\') testWin32();
74        if (File.separatorChar == '/') testUnix();
75    }
76
77}
78