893N/A/*
3909N/A * Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved.
893N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
893N/A *
893N/A * This code is free software; you can redistribute it and/or modify it
893N/A * under the terms of the GNU General Public License version 2 only, as
893N/A * published by the Free Software Foundation.
893N/A *
893N/A * This code is distributed in the hope that it will be useful, but WITHOUT
893N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
893N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
893N/A * version 2 for more details (a copy is included in the LICENSE file that
893N/A * accompanied this code).
893N/A *
893N/A * You should have received a copy of the GNU General Public License version
893N/A * 2 along with this work; if not, write to the Free Software Foundation,
893N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
893N/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.
893N/A */
893N/A
893N/A/* @test
5809N/A * @bug 4313887 6838333 8005566
3471N/A * @summary Unit test for miscellenous methods in java.nio.file.Files
893N/A * @library ..
893N/A */
893N/A
893N/Aimport java.nio.file.*;
3471N/Aimport static java.nio.file.Files.*;
3471N/Aimport static java.nio.file.LinkOption.*;
3471N/Aimport java.nio.file.attribute.*;
893N/Aimport java.io.IOException;
893N/Aimport java.util.*;
893N/A
893N/Apublic class Misc {
893N/A
893N/A public static void main(String[] args) throws IOException {
1319N/A Path dir = TestUtil.createTemporaryDirectory();
1319N/A try {
3471N/A testCreateDirectories(dir);
3471N/A testIsHidden(dir);
3471N/A testIsSameFile(dir);
3471N/A testFileTypeMethods(dir);
3471N/A testAccessMethods(dir);
3471N/A } finally {
3471N/A TestUtil.removeAll(dir);
3471N/A }
3471N/A }
1319N/A
3471N/A /**
3471N/A * Tests createDirectories
3471N/A */
3471N/A static void testCreateDirectories(Path tmpdir) throws IOException {
3471N/A // a no-op
3471N/A createDirectories(tmpdir);
3471N/A
3471N/A // create one directory
3471N/A Path subdir = tmpdir.resolve("a");
3471N/A createDirectories(subdir);
3471N/A assertTrue(exists(subdir));
1319N/A
3471N/A // create parents
3471N/A subdir = subdir.resolve("b/c/d");
3471N/A createDirectories(subdir);
3471N/A assertTrue(exists(subdir));
1319N/A
3471N/A // existing file is not a directory
3471N/A Path file = createFile(tmpdir.resolve("x"));
3471N/A try {
3471N/A createDirectories(file);
3471N/A throw new RuntimeException("failure expected");
3471N/A } catch (FileAlreadyExistsException x) { }
3471N/A try {
3471N/A createDirectories(file.resolve("y"));
3471N/A throw new RuntimeException("failure expected");
3471N/A } catch (IOException x) { }
3471N/A }
3471N/A
3471N/A /**
3471N/A * Tests isHidden
3471N/A */
3471N/A static void testIsHidden(Path tmpdir) throws IOException {
3471N/A assertTrue(!isHidden(tmpdir));
3471N/A
3471N/A Path file = tmpdir.resolve(".foo");
3471N/A if (System.getProperty("os.name").startsWith("Windows")) {
3471N/A createFile(file);
1319N/A try {
3471N/A setAttribute(file, "dos:hidden", true);
3471N/A try {
3471N/A assertTrue(isHidden(file));
3471N/A } finally {
3471N/A setAttribute(file, "dos:hidden", false);
3471N/A }
3471N/A } finally {
3471N/A delete(file);
3471N/A }
3471N/A } else {
3471N/A assertTrue(isHidden(file));
3471N/A }
3471N/A }
1319N/A
3471N/A /**
3471N/A * Tests isSameFile
3471N/A */
3471N/A static void testIsSameFile(Path tmpdir) throws IOException {
3471N/A Path thisFile = tmpdir.resolve("thisFile");
3471N/A Path thatFile = tmpdir.resolve("thatFile");
1319N/A
3471N/A /**
3471N/A * Test: isSameFile for self
3471N/A */
3471N/A assertTrue(isSameFile(thisFile, thisFile));
1319N/A
3471N/A /**
3471N/A * Test: Neither files exist
3471N/A */
893N/A try {
3471N/A isSameFile(thisFile, thatFile);
3471N/A throw new RuntimeException("IOException not thrown");
3471N/A } catch (IOException x) {
893N/A }
893N/A try {
3471N/A isSameFile(thatFile, thisFile);
3471N/A throw new RuntimeException("IOException not thrown");
3471N/A } catch (IOException x) {
893N/A }
3471N/A
3471N/A createFile(thisFile);
893N/A try {
3471N/A /**
3471N/A * Test: One file exists
3471N/A */
3471N/A try {
3471N/A isSameFile(thisFile, thatFile);
3471N/A throw new RuntimeException("IOException not thrown");
3471N/A } catch (IOException x) {
3471N/A }
3471N/A try {
3471N/A isSameFile(thatFile, thisFile);
3471N/A throw new RuntimeException("IOException not thrown");
3471N/A } catch (IOException x) {
3471N/A }
3471N/A
3471N/A /**
3471N/A * Test: Both file exists
3471N/A */
3471N/A createFile(thatFile);
3471N/A try {
3471N/A assertTrue(!isSameFile(thisFile, thatFile));
3471N/A assertTrue(!isSameFile(thatFile, thisFile));
3471N/A } finally {
3471N/A delete(thatFile);
3471N/A }
3471N/A
3471N/A /**
3471N/A * Test: Symbolic links
3471N/A */
3471N/A if (TestUtil.supportsLinks(tmpdir)) {
3471N/A createSymbolicLink(thatFile, thisFile);
3471N/A try {
3471N/A assertTrue(isSameFile(thisFile, thatFile));
3471N/A assertTrue(isSameFile(thatFile, thisFile));
3471N/A } finally {
3471N/A TestUtil.deleteUnchecked(thatFile);
3471N/A }
3471N/A }
3471N/A } finally {
3471N/A delete(thisFile);
893N/A }
1541N/A
3471N/A // nulls
3471N/A try {
3471N/A isSameFile(thisFile, null);
3471N/A throw new RuntimeException("NullPointerException expected");
3471N/A } catch (NullPointerException ignore) { }
3471N/A try {
3471N/A isSameFile(null, thatFile);
3471N/A throw new RuntimeException("NullPointerException expected");
3471N/A } catch (NullPointerException ignore) { }
3471N/A }
3471N/A
3471N/A /**
3471N/A * Exercise isRegularFile, isDirectory, isSymbolicLink
3471N/A */
3471N/A static void testFileTypeMethods(Path tmpdir) throws IOException {
3471N/A assertTrue(!isRegularFile(tmpdir));
3471N/A assertTrue(!isRegularFile(tmpdir, NOFOLLOW_LINKS));
3471N/A assertTrue(isDirectory(tmpdir));
3471N/A assertTrue(isDirectory(tmpdir, NOFOLLOW_LINKS));
3471N/A assertTrue(!isSymbolicLink(tmpdir));
3471N/A
3471N/A Path file = createFile(tmpdir.resolve("foo"));
3471N/A try {
3471N/A assertTrue(isRegularFile(file));
3471N/A assertTrue(isRegularFile(file, NOFOLLOW_LINKS));
3471N/A assertTrue(!isDirectory(file));
3471N/A assertTrue(!isDirectory(file, NOFOLLOW_LINKS));
3471N/A assertTrue(!isSymbolicLink(file));
3471N/A
3471N/A if (TestUtil.supportsLinks(tmpdir)) {
3471N/A Path link = tmpdir.resolve("link");
3471N/A
3471N/A createSymbolicLink(link, tmpdir);
3471N/A try {
3471N/A assertTrue(!isRegularFile(link));
3471N/A assertTrue(!isRegularFile(link, NOFOLLOW_LINKS));
3471N/A assertTrue(isDirectory(link));
3471N/A assertTrue(!isDirectory(link, NOFOLLOW_LINKS));
3471N/A assertTrue(isSymbolicLink(link));
3471N/A } finally {
3471N/A delete(link);
3471N/A }
3471N/A
3471N/A createSymbolicLink(link, file);
3471N/A try {
3471N/A assertTrue(isRegularFile(link));
3471N/A assertTrue(!isRegularFile(link, NOFOLLOW_LINKS));
3471N/A assertTrue(!isDirectory(link));
3471N/A assertTrue(!isDirectory(link, NOFOLLOW_LINKS));
3471N/A assertTrue(isSymbolicLink(link));
3471N/A } finally {
3471N/A delete(link);
3471N/A }
3471N/A
3471N/A createLink(link, file);
3471N/A try {
3471N/A assertTrue(isRegularFile(link));
3471N/A assertTrue(isRegularFile(link, NOFOLLOW_LINKS));
3471N/A assertTrue(!isDirectory(link));
3471N/A assertTrue(!isDirectory(link, NOFOLLOW_LINKS));
3471N/A assertTrue(!isSymbolicLink(link));
3471N/A } finally {
3471N/A delete(link);
3471N/A }
3471N/A }
3471N/A
3471N/A } finally {
3471N/A delete(file);
3471N/A }
3471N/A }
3471N/A
3471N/A /**
3471N/A * Exercise isReadbale, isWritable, isExecutable, exists, notExists
3471N/A */
3471N/A static void testAccessMethods(Path tmpdir) throws IOException {
3471N/A // should return false when file does not exist
3471N/A Path doesNotExist = tmpdir.resolve("doesNotExist");
3471N/A assertTrue(!isReadable(doesNotExist));
3471N/A assertTrue(!isWritable(doesNotExist));
3471N/A assertTrue(!isExecutable(doesNotExist));
3471N/A assertTrue(!exists(doesNotExist));
3471N/A assertTrue(notExists(doesNotExist));
2826N/A
3471N/A Path file = createFile(tmpdir.resolve("foo"));
3471N/A try {
3471N/A // files exist
3471N/A assertTrue(isReadable(file));
3471N/A assertTrue(isWritable(file));
3471N/A assertTrue(exists(file));
3471N/A assertTrue(!notExists(file));
3471N/A assertTrue(isReadable(tmpdir));
3471N/A assertTrue(isWritable(tmpdir));
3471N/A assertTrue(exists(tmpdir));
3471N/A assertTrue(!notExists(tmpdir));
3471N/A
3471N/A
3471N/A // sym link exists
3471N/A if (TestUtil.supportsLinks(tmpdir)) {
3471N/A Path link = tmpdir.resolve("link");
3471N/A
3471N/A createSymbolicLink(link, file);
3471N/A try {
3471N/A assertTrue(isReadable(link));
3471N/A assertTrue(isWritable(link));
3471N/A assertTrue(exists(link));
3471N/A assertTrue(!notExists(link));
3471N/A } finally {
3471N/A delete(link);
3471N/A }
3471N/A
3471N/A createSymbolicLink(link, doesNotExist);
3471N/A try {
3471N/A assertTrue(!isReadable(link));
3471N/A assertTrue(!isWritable(link));
3471N/A assertTrue(!exists(link));
3471N/A assertTrue(exists(link, NOFOLLOW_LINKS));
3471N/A assertTrue(notExists(link));
3471N/A assertTrue(!notExists(link, NOFOLLOW_LINKS));
3471N/A } finally {
3471N/A delete(link);
3471N/A }
3471N/A }
3471N/A
3471N/A /**
3471N/A * Test: Edit ACL to deny WRITE and EXECUTE
3471N/A */
3471N/A if (getFileStore(file).supportsFileAttributeView("acl")) {
3471N/A AclFileAttributeView view =
3471N/A getFileAttributeView(file, AclFileAttributeView.class);
3471N/A UserPrincipal owner = view.getOwner();
3471N/A List<AclEntry> acl = view.getAcl();
3471N/A
3471N/A // Insert entry to deny WRITE and EXECUTE
3471N/A AclEntry entry = AclEntry.newBuilder()
3471N/A .setType(AclEntryType.DENY)
3471N/A .setPrincipal(owner)
3471N/A .setPermissions(AclEntryPermission.WRITE_DATA,
3471N/A AclEntryPermission.EXECUTE)
3471N/A .build();
3471N/A acl.add(0, entry);
3471N/A view.setAcl(acl);
3471N/A try {
3471N/A assertTrue(!isWritable(file));
3471N/A assertTrue(!isExecutable(file));
3471N/A } finally {
3471N/A // Restore ACL
3471N/A acl.remove(0);
3471N/A view.setAcl(acl);
3471N/A }
3471N/A }
3471N/A
3471N/A /**
3471N/A * Test: Windows DOS read-only attribute
3471N/A */
3471N/A if (System.getProperty("os.name").startsWith("Windows")) {
3471N/A setAttribute(file, "dos:readonly", true);
3471N/A try {
3471N/A assertTrue(!isWritable(file));
3471N/A } finally {
3471N/A setAttribute(file, "dos:readonly", false);
3471N/A }
3471N/A
3471N/A // Read-only attribute does not make direcory read-only
3471N/A DosFileAttributeView view =
3471N/A getFileAttributeView(tmpdir, DosFileAttributeView.class);
3471N/A boolean save = view.readAttributes().isReadOnly();
3471N/A view.setReadOnly(true);
3471N/A try {
3471N/A assertTrue(isWritable(file));
3471N/A } finally {
3471N/A view.setReadOnly(save);
3471N/A }
3471N/A }
3471N/A } finally {
3471N/A delete(file);
3471N/A }
3471N/A }
3471N/A
3471N/A static void assertTrue(boolean okay) {
3471N/A if (!okay)
3471N/A throw new RuntimeException("Assertion Failed");
893N/A }
893N/A}