Path: blob/master/test/jdk/java/nio/file/Path/Misc.java
41153 views
/*1* Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24* @bug 4313887 6838333 702997925* @summary Unit test for miscellenous java.nio.file.Path methods26* @library ..27*/2829import java.nio.file.*;30import static java.nio.file.LinkOption.*;31import java.io.*;3233public class Misc {34static final boolean isWindows =35System.getProperty("os.name").startsWith("Windows");36static boolean supportsLinks;3738public static void main(String[] args) throws IOException {39Path dir = TestUtil.createTemporaryDirectory();40try {41supportsLinks = TestUtil.supportsLinks(dir);4243// equals and hashCode methods44testEqualsAndHashCode();4546// toFile method47testToFile(dir);4849// toRealPath method50testToRealPath(dir);515253} finally {54TestUtil.removeAll(dir);55}56}5758/**59* Exercise equals and hashCode methods60*/61static void testEqualsAndHashCode() {62Path thisFile = Paths.get("this");63Path thatFile = Paths.get("that");6465assertTrue(thisFile.equals(thisFile));66assertTrue(!thisFile.equals(thatFile));6768assertTrue(!thisFile.equals(null));69assertTrue(!thisFile.equals(new Object()));7071Path likeThis = Paths.get("This");72if (isWindows) {73// case insensitive74assertTrue(thisFile.equals(likeThis));75assertTrue(thisFile.hashCode() == likeThis.hashCode());76} else {77// case senstive78assertTrue(!thisFile.equals(likeThis));79}80}8182/**83* Exercise toFile method84*/85static void testToFile(Path dir) throws IOException {86File d = dir.toFile();87assertTrue(d.toString().equals(dir.toString()));88assertTrue(d.toPath().equals(dir));89}9091/**92* Exercise toRealPath method93*/94static void testToRealPath(Path dir) throws IOException {95final Path file = Files.createFile(dir.resolve("foo"));96final Path link = dir.resolve("link");9798/**99* Test: totRealPath() will access same file as toRealPath(NOFOLLOW_LINKS)100*/101assertTrue(Files.isSameFile(file.toRealPath(), file.toRealPath(NOFOLLOW_LINKS)));102103/**104* Test: toRealPath should fail if file does not exist105*/106Path doesNotExist = dir.resolve("DoesNotExist");107try {108doesNotExist.toRealPath();109throw new RuntimeException("IOException expected");110} catch (IOException expected) {111}112try {113doesNotExist.toRealPath(NOFOLLOW_LINKS);114throw new RuntimeException("IOException expected");115} catch (IOException expected) {116}117118/**119* Test: toRealPath() should resolve links120*/121if (supportsLinks) {122Path resolvedFile = file;123if (isWindows) {124// Path::toRealPath does not work with environments using the125// legacy subst mechanism. This is a workaround to keep the126// test working if 'dir' points to a location on a subst drive.127// See JDK-8213216.128//129Path tempLink = dir.resolve("tempLink");130Files.createSymbolicLink(tempLink, dir.toAbsolutePath());131Path resolvedDir = tempLink.toRealPath();132Files.delete(tempLink);133resolvedFile = resolvedDir.resolve(file.getFileName());134}135136Files.createSymbolicLink(link, resolvedFile.toAbsolutePath());137assertTrue(link.toRealPath().equals(resolvedFile.toRealPath()));138Files.delete(link);139}140141/**142* Test: toRealPath(NOFOLLOW_LINKS) should not resolve links143*/144if (supportsLinks) {145Files.createSymbolicLink(link, file.toAbsolutePath());146assertTrue(link.toRealPath(NOFOLLOW_LINKS).getFileName().equals(link.getFileName()));147Files.delete(link);148}149150/**151* Test: toRealPath(NOFOLLOW_LINKS) with broken link152*/153if (supportsLinks) {154Path broken = Files.createSymbolicLink(link, doesNotExist);155assertTrue(link.toRealPath(NOFOLLOW_LINKS).getFileName().equals(link.getFileName()));156Files.delete(link);157}158159/**160* Test: toRealPath should eliminate "."161*/162assertTrue(dir.resolve(".").toRealPath().equals(dir.toRealPath()));163assertTrue(dir.resolve(".").toRealPath(NOFOLLOW_LINKS).equals(dir.toRealPath(NOFOLLOW_LINKS)));164165/**166* Test: toRealPath should eliminate ".." when it doesn't follow a167* symbolic link168*/169Path subdir = Files.createDirectory(dir.resolve("subdir"));170assertTrue(subdir.resolve("..").toRealPath().equals(dir.toRealPath()));171assertTrue(subdir.resolve("..").toRealPath(NOFOLLOW_LINKS).equals(dir.toRealPath(NOFOLLOW_LINKS)));172Files.delete(subdir);173174// clean-up175Files.delete(file);176}177178static void assertTrue(boolean okay) {179if (!okay)180throw new RuntimeException("Assertion Failed");181}182}183184185