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
3899N/A * @bug 4313887 6838333 7029979
3471N/A * @summary Unit test for miscellenous java.nio.file.Path methods
893N/A * @library ..
893N/A */
893N/A
893N/Aimport java.nio.file.*;
3899N/Aimport static java.nio.file.LinkOption.*;
893N/Aimport java.io.*;
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
3471N/A testEqualsAndHashCode();
893N/A
3471N/A // toFile method
3471N/A testToFile(dir);
893N/A
893N/A // toRealPath method
3471N/A testToRealPath(dir);
893N/A
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 */
3471N/A static void testEqualsAndHashCode() {
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 /**
3471N/A * Exercise toFile method
893N/A */
3471N/A static void testToFile(Path dir) throws IOException {
3471N/A File d = dir.toFile();
3471N/A assertTrue(d.toString().equals(dir.toString()));
3471N/A assertTrue(d.toPath().equals(dir));
893N/A }
893N/A
893N/A /**
893N/A * Exercise toRealPath method
893N/A */
3471N/A static void testToRealPath(Path dir) throws IOException {
3471N/A final Path file = Files.createFile(dir.resolve("foo"));
893N/A final Path link = dir.resolve("link");
893N/A
893N/A /**
3899N/A * Test: totRealPath() will access same file as toRealPath(NOFOLLOW_LINKS)
893N/A */
3899N/A assertTrue(Files.isSameFile(file.toRealPath(), file.toRealPath(NOFOLLOW_LINKS)));
893N/A
893N/A /**
2672N/A * Test: toRealPath should fail if file does not exist
2672N/A */
2672N/A Path doesNotExist = dir.resolve("DoesNotExist");
2672N/A try {
3899N/A doesNotExist.toRealPath();
2672N/A throw new RuntimeException("IOException expected");
2672N/A } catch (IOException expected) {
2672N/A }
2672N/A try {
3899N/A doesNotExist.toRealPath(NOFOLLOW_LINKS);
2672N/A throw new RuntimeException("IOException expected");
2672N/A } catch (IOException expected) {
2672N/A }
2672N/A
2672N/A /**
3899N/A * Test: toRealPath() should resolve links
893N/A */
893N/A if (supportsLinks) {
3471N/A Files.createSymbolicLink(link, file.toAbsolutePath());
3899N/A assertTrue(link.toRealPath().equals(file.toRealPath()));
3471N/A Files.delete(link);
893N/A }
893N/A
893N/A /**
3899N/A * Test: toRealPath(NOFOLLOW_LINKS) should not resolve links
893N/A */
893N/A if (supportsLinks) {
3471N/A Files.createSymbolicLink(link, file.toAbsolutePath());
3899N/A assertTrue(link.toRealPath(NOFOLLOW_LINKS).getFileName().equals(link.getFileName()));
3471N/A Files.delete(link);
893N/A }
893N/A
893N/A /**
3899N/A * Test: toRealPath(NOFOLLOW_LINKS) with broken link
1319N/A */
1319N/A if (supportsLinks) {
3471N/A Path broken = Files.createSymbolicLink(link, doesNotExist);
3899N/A assertTrue(link.toRealPath(NOFOLLOW_LINKS).getFileName().equals(link.getFileName()));
3471N/A Files.delete(link);
1319N/A }
1319N/A
1319N/A /**
893N/A * Test: toRealPath should eliminate "."
893N/A */
3899N/A assertTrue(dir.resolve(".").toRealPath().equals(dir.toRealPath()));
3899N/A assertTrue(dir.resolve(".").toRealPath(NOFOLLOW_LINKS).equals(dir.toRealPath(NOFOLLOW_LINKS)));
893N/A
893N/A /**
893N/A * Test: toRealPath should eliminate ".." when it doesn't follow a
893N/A * symbolic link
893N/A */
3471N/A Path subdir = Files.createDirectory(dir.resolve("subdir"));
3899N/A assertTrue(subdir.resolve("..").toRealPath().equals(dir.toRealPath()));
3899N/A assertTrue(subdir.resolve("..").toRealPath(NOFOLLOW_LINKS).equals(dir.toRealPath(NOFOLLOW_LINKS)));
3471N/A Files.delete(subdir);
893N/A
893N/A // clean-up
3471N/A Files.delete(file);
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}