Path: blob/master/test/jdk/java/nio/file/Files/Links.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 686386425* @summary Unit test for java.nio.file.Files createSymbolicLink,26* readSymbolicLink, and createLink methods27* @library ..28* @build Links29* @run main/othervm Links30*/3132import java.nio.file.*;33import java.nio.file.attribute.*;34import java.io.*;3536public class Links {3738static final boolean isWindows =39System.getProperty("os.name").startsWith("Windows");4041static void assertTrue(boolean okay) {42if (!okay)43throw new RuntimeException("Assertion failed");44}4546/**47* Exercise createSymbolicLink and readLink methods48*/49static void testSymLinks(Path dir) throws IOException {50final Path link = dir.resolve("link");5152// Check if sym links are supported53try {54Files.createSymbolicLink(link, Paths.get("foo"));55Files.delete(link);56} catch (UnsupportedOperationException x) {57// sym links not supported58return;59} catch (IOException x) {60// probably insufficient privileges to create sym links (Windows)61return;62}6364// Test links to various targets65String[] windowsTargets =66{ "foo", "C:\\foo", "\\foo", "\\\\server\\share\\foo" };67String[] otherTargets = { "relative", "/absolute" };6869String[] targets = (isWindows) ? windowsTargets : otherTargets;70for (String s: targets) {71Path target = Paths.get(s);72Files.createSymbolicLink(link, target);73try {74assertTrue(Files.readSymbolicLink(link).equals(target));75} finally {76Files.delete(link);77}78}7980// Test links to directory81Path mydir = dir.resolve("mydir");82Path myfile = mydir.resolve("myfile");83try {84Files.createDirectory(mydir);85Files.createFile(myfile);8687// link -> "mydir"88Files.createSymbolicLink(link, mydir.getFileName());89assertTrue(Files.readSymbolicLink(link).equals(mydir.getFileName()));9091// Test access to directory via link92try (DirectoryStream<Path> stream = Files.newDirectoryStream(link)) {93boolean found = false;94for (Path entry: stream) {95if (entry.getFileName().equals(myfile.getFileName())) {96found = true;97break;98}99}100assertTrue(found);101}102103// Test link2 -> link -> mydir104final Path link2 = dir.resolve("link2");105Path target2 = link.getFileName();106Files.createSymbolicLink(link2, target2);107try {108assertTrue(Files.readSymbolicLink(link2).equals(target2));109Files.newDirectoryStream(link2).close();110} finally {111Files.delete(link2);112}113114// Remove mydir and re-create link2 before re-creating mydir115// (This is a useful test on Windows to ensure that creating a116// sym link to a directory sym link creates the right type of link).117Files.delete(myfile);118Files.delete(mydir);119Files.createSymbolicLink(link2, target2);120try {121assertTrue(Files.readSymbolicLink(link2).equals(target2));122Files.createDirectory(mydir);123Files.newDirectoryStream(link2).close();124} finally {125Files.delete(link2);126}127128} finally {129Files.deleteIfExists(myfile);130Files.deleteIfExists(mydir);131Files.deleteIfExists(link);132}133}134135/**136* Exercise createLink method137*/138static void testHardLinks(Path dir) throws IOException {139Path foo = dir.resolve("foo");140Files.createFile(foo);141try {142Path bar = dir.resolve("bar");143try {144Files.createLink(bar, foo);145} catch (UnsupportedOperationException x) {146return;147} catch (IOException x) {148// probably insufficient privileges (Windows)149return;150}151try {152Object key1 = Files.readAttributes(foo, BasicFileAttributes.class).fileKey();153Object key2 = Files.readAttributes(bar, BasicFileAttributes.class).fileKey();154assertTrue((key1 == null) || (key1.equals(key2)));155} finally {156Files.delete(bar);157}158159160} finally {161Files.delete(foo);162}163}164165public static void main(String[] args) throws IOException {166Path dir = TestUtil.createTemporaryDirectory();167try {168testSymLinks(dir);169testHardLinks(dir);170171// repeat tests on Windows with long path172if (isWindows) {173Path dirWithLongPath = null;174try {175dirWithLongPath = TestUtil.createDirectoryWithLongPath(dir);176} catch (IOException x) {177System.out.println("Unable to create long path: " + x);178}179if (dirWithLongPath != null) {180System.out.println("");181System.out.println("** REPEAT TESTS WITH LONG PATH **");182testSymLinks(dirWithLongPath);183testHardLinks(dirWithLongPath);184}185}186} finally {187TestUtil.removeAll(dir);188}189}190}191192193