IsHidden.java revision 6153
1088N/A/*
3909N/A * Copyright (c) 1998, 2013, Oracle and/or its affiliates. All rights reserved.
1088N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1088N/A *
1088N/A * This code is free software; you can redistribute it and/or modify it
1088N/A * under the terms of the GNU General Public License version 2 only, as
1088N/A * published by the Free Software Foundation.
1088N/A *
1088N/A * This code is distributed in the hope that it will be useful, but WITHOUT
1088N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1088N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1088N/A * version 2 for more details (a copy is included in the LICENSE file that
1088N/A * accompanied this code).
1088N/A *
1088N/A * You should have received a copy of the GNU General Public License version
1088N/A * 2 along with this work; if not, write to the Free Software Foundation,
1088N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1088N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
1088N/A */
1088N/A
1088N/A/* @test
1088N/A @bug 4131223 6470354
1088N/A @summary Basic test for isHidden method
1088N/A */
1088N/A
1088N/Aimport java.io.*;
1088N/Aimport java.nio.file.Files;
3564N/Aimport java.nio.file.attribute.DosFileAttributeView;
1088N/A
1088N/Apublic class IsHidden {
1088N/A
1088N/A private static String dir = System.getProperty("test.dir", ".");
1088N/A
1088N/A private static void ck(String path, boolean ans) throws Exception {
1088N/A File f = new File(path);
1088N/A boolean x = f.isHidden();
1088N/A if (x != ans)
1088N/A throw new Exception(path + ": expected " + ans + ", got " + x);
1088N/A System.err.println(path + " ==> " + x);
1088N/A }
1088N/A
1088N/A private static void setHidden(File f, boolean value) throws IOException {
1088N/A Files.getFileAttributeView(f.toPath(), DosFileAttributeView.class).setHidden(value);
1088N/A }
1088N/A
1088N/A private static void checkHidden(File f) {
1088N/A if (!f.isHidden())
1088N/A throw new RuntimeException(f + " should be hidden");
1088N/A }
1088N/A
1088N/A private static void testWin32() throws Exception {
1088N/A File f = new File(dir, "test");
1088N/A f.deleteOnExit();
1088N/A f.createNewFile();
1088N/A setHidden(f, true);
1088N/A try {
1088N/A ck(f.getPath(), true);
1088N/A } finally {
1088N/A setHidden(f, false);
1088N/A }
1088N/A ck(".foo", false);
1088N/A ck("foo", false);
1088N/A
1088N/A File pagefile = new File("C:\\pagefile.sys");
1088N/A File hiberfil = new File("C:\\hiberfil.sys");
1088N/A if (pagefile.exists()) checkHidden(pagefile);
1088N/A if (hiberfil.exists()) checkHidden(hiberfil);
1088N/A }
3564N/A
3564N/A private static void testUnix() throws Exception {
3564N/A ck(dir + "/IsHidden.java", false);
3564N/A ck(dir + "/.", true);
3564N/A ck(".", true);
3564N/A ck("..", true);
3564N/A ck(".foo", true);
3564N/A ck("foo", false);
3564N/A ck("", false);
3564N/A }
3564N/A
3564N/A public static void main(String[] args) throws Exception {
1088N/A if (File.separatorChar == '\\') testWin32();
1088N/A if (File.separatorChar == '/') testUnix();
3564N/A }
1088N/A
3564N/A}
1088N/A