Misc.java revision 1319
893N/A/*
893N/A * Copyright 2008-2009 Sun Microsystems, Inc. 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 *
893N/A * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
893N/A * CA 95054 USA or visit www.sun.com if you need additional information or
893N/A * have any questions.
893N/A */
893N/A
893N/A/* @test
1319N/A * @bug 4313887 6838333
893N/A * @summary Unit test for java.nio.file.Path for miscellenous methods not
893N/A * covered by other tests
893N/A * @library ..
893N/A */
893N/A
893N/Aimport java.nio.file.*;
893N/Aimport static java.nio.file.LinkOption.*;
893N/Aimport java.nio.file.attribute.*;
893N/Aimport java.io.*;
893N/Aimport java.util.*;
893N/A
893N/Apublic class Misc {
893N/A static final boolean isWindows =
893N/A System.getProperty("os.name").startsWith("Windows");
893N/A static boolean supportsLinks;
893N/A
893N/A public static void main(String[] args) throws IOException {
893N/A Path dir = TestUtil.createTemporaryDirectory();
893N/A try {
893N/A supportsLinks = TestUtil.supportsLinks(dir);
893N/A
893N/A // equals and hashCode methods
893N/A equalsAndHashCode();
893N/A
893N/A // checkAccess method
893N/A checkAccessTests(dir);
893N/A
893N/A // getFileAttributeView methods
893N/A getFileAttributeViewTests(dir);
893N/A
893N/A // toRealPath method
893N/A toRealPathTests(dir);
893N/A
893N/A // isSameFile method
893N/A isSameFileTests(dir);
893N/A
893N/A // isHidden method
893N/A isHiddenTests(dir);
893N/A
893N/A } finally {
893N/A TestUtil.removeAll(dir);
893N/A }
893N/A }
893N/A
893N/A /**
893N/A * Exercise equals and hashCode methods
893N/A */
893N/A static void equalsAndHashCode() {
893N/A
893N/A Path thisFile = Paths.get("this");
893N/A Path thatFile = Paths.get("that");
893N/A
893N/A assertTrue(thisFile.equals(thisFile));
893N/A assertTrue(!thisFile.equals(thatFile));
893N/A
893N/A assertTrue(!thisFile.equals(null));
893N/A assertTrue(!thisFile.equals(new Object()));
893N/A
893N/A Path likeThis = Paths.get("This");
893N/A if (isWindows) {
893N/A // case insensitive
893N/A assertTrue(thisFile.equals(likeThis));
893N/A assertTrue(thisFile.hashCode() == likeThis.hashCode());
893N/A } else {
893N/A // case senstive
893N/A assertTrue(!thisFile.equals(likeThis));
893N/A }
893N/A }
893N/A
893N/A /**
893N/A * Exercise checkAccess method
893N/A */
893N/A static void checkAccessTests(Path dir) throws IOException {
893N/A final Path file = dir.resolve("foo").createFile();
893N/A
893N/A /**
893N/A * Test: This directory should readable and writable
893N/A */
893N/A dir.checkAccess();
893N/A dir.checkAccess(AccessMode.READ);
893N/A dir.checkAccess(AccessMode.WRITE);
893N/A dir.checkAccess(AccessMode.READ, AccessMode.WRITE);
893N/A
893N/A /**
893N/A * Test: File does not exist
893N/A */
893N/A Path doesNotExist = dir.resolve("thisDoesNotExists");
893N/A try {
893N/A doesNotExist.checkAccess();
893N/A throw new RuntimeException("NoSuchFileException expected");
893N/A } catch (NoSuchFileException x) {
893N/A }
893N/A try {
893N/A doesNotExist.checkAccess(AccessMode.READ);
893N/A throw new RuntimeException("NoSuchFileException expected");
893N/A } catch (NoSuchFileException x) {
893N/A }
893N/A try {
893N/A doesNotExist.checkAccess(AccessMode.WRITE);
893N/A throw new RuntimeException("NoSuchFileException expected");
893N/A } catch (NoSuchFileException x) {
893N/A }
893N/A try {
893N/A doesNotExist.checkAccess(AccessMode.EXECUTE);
893N/A throw new RuntimeException("NoSuchFileException expected");
893N/A } catch (NoSuchFileException x) {
893N/A }
893N/A
893N/A /**
893N/A * Test: Edit ACL to deny WRITE and EXECUTE
893N/A */
893N/A AclFileAttributeView view = file
893N/A .getFileAttributeView(AclFileAttributeView.class);
893N/A if (view != null &&
893N/A file.getFileStore().supportsFileAttributeView("acl"))
893N/A {
893N/A UserPrincipal owner = view.getOwner();
893N/A List<AclEntry> acl = view.getAcl();
893N/A
893N/A // Insert entry to deny WRITE and EXECUTE
893N/A AclEntry entry = AclEntry.newBuilder()
893N/A .setType(AclEntryType.DENY)
893N/A .setPrincipal(owner)
893N/A .setPermissions(AclEntryPermission.WRITE_DATA,
893N/A AclEntryPermission.EXECUTE)
893N/A .build();
893N/A acl.add(0, entry);
893N/A view.setAcl(acl);
893N/A
893N/A try {
893N/A file.checkAccess(AccessMode.WRITE);
893N/A throw new RuntimeException("AccessDeniedException expected");
893N/A } catch (AccessDeniedException x) {
893N/A }
893N/A
893N/A try {
893N/A file.checkAccess(AccessMode.EXECUTE);
893N/A throw new RuntimeException("AccessDeniedException expected");
893N/A } catch (AccessDeniedException x) {
893N/A }
893N/A
893N/A
893N/A // Restore ACL
893N/A acl.remove(0);
893N/A view.setAcl(acl);
893N/A }
893N/A
893N/A /**
893N/A * Test: Windows DOS read-only attribute
893N/A */
893N/A if (isWindows) {
893N/A DosFileAttributeView dview =
893N/A file.getFileAttributeView(DosFileAttributeView.class);
893N/A dview.setReadOnly(true);
893N/A try {
893N/A file.checkAccess(AccessMode.WRITE);
893N/A throw new RuntimeException("AccessDeniedException expected");
893N/A } catch (AccessDeniedException x) {
893N/A }
893N/A dview.setReadOnly(false);
893N/A
893N/A // Read-only attribute does not make direcory read-only
893N/A dview = dir.getFileAttributeView(DosFileAttributeView.class);
893N/A boolean save = dview.readAttributes().isReadOnly();
893N/A dview.setReadOnly(true);
893N/A dir.checkAccess(AccessMode.WRITE);
893N/A dview.setReadOnly(save);
893N/A }
893N/A
893N/A /**
893N/A * Test: null
893N/A */
893N/A try {
893N/A file.checkAccess((AccessMode)null);
893N/A throw new RuntimeException("NullPointerException expected");
893N/A } catch (NullPointerException ignore) { }
893N/A
893N/A // clean-up
893N/A file.delete();
893N/A }
893N/A
893N/A /**
893N/A * Exercise getFileAttributeFile methods
893N/A */
893N/A static void getFileAttributeViewTests(Path dir) {
893N/A assertTrue(dir.getFileAttributeView(BasicFileAttributeView.class)
893N/A instanceof BasicFileAttributeView);
893N/A assertTrue(dir.getFileAttributeView(BasicFileAttributeView.class, NOFOLLOW_LINKS)
893N/A instanceof BasicFileAttributeView);
893N/A assertTrue(dir.getFileAttributeView(BogusFileAttributeView.class) == null);
893N/A try {
893N/A dir.getFileAttributeView((Class<FileAttributeView>)null);
893N/A } catch (NullPointerException ignore) { }
893N/A try {
893N/A dir.getFileAttributeView(BasicFileAttributeView.class, (LinkOption[])null);
893N/A } catch (NullPointerException ignore) { }
893N/A try {
893N/A dir.getFileAttributeView(BasicFileAttributeView.class, (LinkOption)null);
893N/A } catch (NullPointerException ignore) { }
893N/A
893N/A }
893N/A interface BogusFileAttributeView extends FileAttributeView { }
893N/A
893N/A /**
893N/A * Exercise toRealPath method
893N/A */
893N/A static void toRealPathTests(Path dir) throws IOException {
893N/A final Path file = dir.resolve("foo").createFile();
893N/A final Path link = dir.resolve("link");
893N/A
893N/A /**
893N/A * Test: toRealPath(true) will access same file as toRealPath(false)
893N/A */
893N/A assertTrue(file.toRealPath(true).isSameFile(file.toRealPath(false)));
893N/A
893N/A /**
893N/A * Test: toRealPath(true) should resolve links
893N/A */
893N/A if (supportsLinks) {
893N/A link.createSymbolicLink(file.toAbsolutePath());
893N/A assertTrue(link.toRealPath(true).equals(file.toRealPath(true)));
893N/A link.delete();
893N/A }
893N/A
893N/A
893N/A /**
893N/A * Test: toRealPath(false) should not resolve links
893N/A */
893N/A if (supportsLinks) {
893N/A link.createSymbolicLink(file.toAbsolutePath());
893N/A assertTrue(link.toRealPath(false).getName().equals(link.getName()));
893N/A link.delete();
893N/A }
893N/A
893N/A /**
1319N/A * Test: toRealPath(false) with broken link
1319N/A */
1319N/A if (supportsLinks) {
1319N/A Path broken = dir.resolve("doesNotExist");
1319N/A link.createSymbolicLink(broken);
1319N/A assertTrue(link.toRealPath(false).getName().equals(link.getName()));
1319N/A link.delete();
1319N/A }
1319N/A
1319N/A /**
893N/A * Test: toRealPath should eliminate "."
893N/A */
893N/A assertTrue(dir.resolve(".").toRealPath(true).equals(dir.toRealPath(true)));
893N/A assertTrue(dir.resolve(".").toRealPath(false).equals(dir.toRealPath(false)));
893N/A
893N/A /**
893N/A * Test: toRealPath should eliminate ".." when it doesn't follow a
893N/A * symbolic link
893N/A */
893N/A Path subdir = dir.resolve("subdir").createDirectory();
893N/A assertTrue(subdir.resolve("..").toRealPath(true).equals(dir.toRealPath(true)));
893N/A assertTrue(subdir.resolve("..").toRealPath(false).equals(dir.toRealPath(false)));
893N/A subdir.delete();
893N/A
893N/A // clean-up
893N/A file.delete();
893N/A }
893N/A
893N/A /**
893N/A * Exercise isSameFile method
893N/A */
893N/A static void isSameFileTests(Path dir) throws IOException {
893N/A Path thisFile = dir.resolve("thisFile");
893N/A Path thatFile = dir.resolve("thatFile");
893N/A
893N/A /**
893N/A * Test: isSameFile for self and null
893N/A */
893N/A assertTrue(thisFile.isSameFile(thisFile));
893N/A assertTrue(!thisFile.isSameFile(null));
893N/A
893N/A /**
893N/A * Test: Neither files exist
893N/A */
893N/A try {
893N/A thisFile.isSameFile(thatFile);
893N/A throw new RuntimeException("IOException not thrown");
893N/A } catch (IOException x) {
893N/A }
893N/A try {
893N/A thatFile.isSameFile(thisFile);
893N/A throw new RuntimeException("IOException not thrown");
893N/A } catch (IOException x) {
893N/A }
893N/A
893N/A thisFile.createFile();
893N/A try {
893N/A /**
893N/A * Test: One file exists
893N/A */
893N/A try {
893N/A thisFile.isSameFile(thatFile);
893N/A throw new RuntimeException("IOException not thrown");
893N/A } catch (IOException x) {
893N/A }
893N/A try {
893N/A thatFile.isSameFile(thisFile);
893N/A throw new RuntimeException("IOException not thrown");
893N/A } catch (IOException x) {
893N/A }
893N/A
893N/A thatFile.createFile();
893N/A
893N/A /**
893N/A * Test: Both file exists
893N/A */
893N/A try {
893N/A assertTrue(!thisFile.isSameFile(thatFile));
893N/A assertTrue(!thatFile.isSameFile(thisFile));
893N/A } finally {
893N/A TestUtil.deleteUnchecked(thatFile);
893N/A }
893N/A
893N/A /**
893N/A * Test: Symbolic links
893N/A */
893N/A if (supportsLinks) {
893N/A thatFile.createSymbolicLink(thisFile);
893N/A try {
893N/A assertTrue(thisFile.isSameFile(thatFile));
893N/A assertTrue(thatFile.isSameFile(thisFile));
893N/A } finally {
893N/A TestUtil.deleteUnchecked(thatFile);
893N/A }
893N/A }
893N/A } finally {
1319N/A thisFile.delete();
893N/A }
893N/A }
893N/A
893N/A /**
893N/A * Exercise isHidden method
893N/A */
893N/A static void isHiddenTests(Path dir) throws IOException {
893N/A assertTrue(!dir.isHidden());
893N/A
893N/A Path file = dir.resolve(".foo");
893N/A if (isWindows) {
893N/A file.createFile();
893N/A try {
1319N/A file.setAttribute("dos:hidden", true);
893N/A assertTrue(file.isHidden());
893N/A } finally {
893N/A file.delete();
893N/A }
893N/A } else {
893N/A assertTrue(file.isHidden());
893N/A }
893N/A }
893N/A
893N/A static void assertTrue(boolean okay) {
893N/A if (!okay)
893N/A throw new RuntimeException("Assertion Failed");
893N/A }
893N/A}