Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/nio/file/Path/Misc.java
38828 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) {122Files.createSymbolicLink(link, file.toAbsolutePath());123assertTrue(link.toRealPath().equals(file.toRealPath()));124Files.delete(link);125}126127/**128* Test: toRealPath(NOFOLLOW_LINKS) should not resolve links129*/130if (supportsLinks) {131Files.createSymbolicLink(link, file.toAbsolutePath());132assertTrue(link.toRealPath(NOFOLLOW_LINKS).getFileName().equals(link.getFileName()));133Files.delete(link);134}135136/**137* Test: toRealPath(NOFOLLOW_LINKS) with broken link138*/139if (supportsLinks) {140Path broken = Files.createSymbolicLink(link, doesNotExist);141assertTrue(link.toRealPath(NOFOLLOW_LINKS).getFileName().equals(link.getFileName()));142Files.delete(link);143}144145/**146* Test: toRealPath should eliminate "."147*/148assertTrue(dir.resolve(".").toRealPath().equals(dir.toRealPath()));149assertTrue(dir.resolve(".").toRealPath(NOFOLLOW_LINKS).equals(dir.toRealPath(NOFOLLOW_LINKS)));150151/**152* Test: toRealPath should eliminate ".." when it doesn't follow a153* symbolic link154*/155Path subdir = Files.createDirectory(dir.resolve("subdir"));156assertTrue(subdir.resolve("..").toRealPath().equals(dir.toRealPath()));157assertTrue(subdir.resolve("..").toRealPath(NOFOLLOW_LINKS).equals(dir.toRealPath(NOFOLLOW_LINKS)));158Files.delete(subdir);159160// clean-up161Files.delete(file);162}163164static void assertTrue(boolean okay) {165if (!okay)166throw new RuntimeException("Assertion Failed");167}168}169170171