test/java/nio/file/Files/Links.java

Print this page

        

*** 21,31 **** * questions. */ /* @test * @bug 4313887 6838333 6863864 ! * @summary Unit test for java.nio.file.Path createSymbolicLink, * readSymbolicLink, and createLink methods * @library .. * @build Links * @run main/othervm Links */ --- 21,31 ---- * questions. */ /* @test * @bug 4313887 6838333 6863864 ! * @summary Unit test for java.nio.file.Files createSymbolicLink, * readSymbolicLink, and createLink methods * @library .. * @build Links * @run main/othervm Links */
*** 50,61 **** static void testSymLinks(Path dir) throws IOException { final Path link = dir.resolve("link"); // Check if sym links are supported try { ! link.createSymbolicLink(Paths.get("foo")); ! link.delete(); } catch (UnsupportedOperationException x) { // sym links not supported return; } catch (IOException x) { // probably insufficient privileges to create sym links (Windows) --- 50,61 ---- static void testSymLinks(Path dir) throws IOException { final Path link = dir.resolve("link"); // Check if sym links are supported try { ! Files.createSymbolicLink(link, Paths.get("foo")); ! Files.delete(link); } catch (UnsupportedOperationException x) { // sym links not supported return; } catch (IOException x) { // probably insufficient privileges to create sym links (Windows)
*** 68,171 **** String[] otherTargets = { "relative", "/absolute" }; String[] targets = (isWindows) ? windowsTargets : otherTargets; for (String s: targets) { Path target = Paths.get(s); ! link.createSymbolicLink(target); try { ! assertTrue(link.readSymbolicLink().equals(target)); } finally { ! link.delete(); } } // Test links to directory Path mydir = dir.resolve("mydir"); Path myfile = mydir.resolve("myfile"); try { ! mydir.createDirectory(); ! myfile.createFile(); // link -> "mydir" ! link.createSymbolicLink(mydir.getName()); ! assertTrue(link.readSymbolicLink().equals(mydir.getName())); // Test access to directory via link ! DirectoryStream<Path> stream = link.newDirectoryStream(); ! try { boolean found = false; for (Path entry: stream) { ! if (entry.getName().equals(myfile.getName())) { found = true; break; } } assertTrue(found); - } finally { - stream.close(); } // Test link2 -> link -> mydir final Path link2 = dir.resolve("link2"); ! Path target2 = link.getName(); ! link2.createSymbolicLink(target2); try { ! assertTrue(link2.readSymbolicLink().equals(target2)); ! link2.newDirectoryStream().close(); } finally { ! link2.delete(); } // Remove mydir and re-create link2 before re-creating mydir // (This is a useful test on Windows to ensure that creating a // sym link to a directory sym link creates the right type of link). ! myfile.delete(); ! mydir.delete(); ! link2.createSymbolicLink(target2); try { ! assertTrue(link2.readSymbolicLink().equals(target2)); ! mydir.createDirectory(); ! link2.newDirectoryStream().close(); } finally { ! link2.delete(); } } finally { ! myfile.deleteIfExists(); ! mydir.deleteIfExists(); ! link.deleteIfExists(); } } /** * Exercise createLink method */ static void testHardLinks(Path dir) throws IOException { ! Path foo = dir.resolve("foo").createFile(); try { ! Path bar; try { ! bar = dir.resolve("bar").createLink(foo); } catch (UnsupportedOperationException x) { return; } catch (IOException x) { // probably insufficient privileges (Windows) return; } try { ! Object key1 = Attributes ! .readBasicFileAttributes(foo).fileKey(); ! Object key2 = Attributes ! .readBasicFileAttributes(bar).fileKey(); assertTrue((key1 == null) || (key1.equals(key2))); } finally { ! bar.delete(); } } finally { ! foo.delete(); } } public static void main(String[] args) throws IOException { Path dir = TestUtil.createTemporaryDirectory(); --- 68,167 ---- String[] otherTargets = { "relative", "/absolute" }; String[] targets = (isWindows) ? windowsTargets : otherTargets; for (String s: targets) { Path target = Paths.get(s); ! Files.createSymbolicLink(link, target); try { ! assertTrue(Files.readSymbolicLink(link).equals(target)); } finally { ! Files.delete(link); } } // Test links to directory Path mydir = dir.resolve("mydir"); Path myfile = mydir.resolve("myfile"); try { ! Files.createDirectory(mydir); ! Files.createFile(myfile); // link -> "mydir" ! Files.createSymbolicLink(link, mydir.getFileName()); ! assertTrue(Files.readSymbolicLink(link).equals(mydir.getFileName())); // Test access to directory via link ! try (DirectoryStream<Path> stream = Files.newDirectoryStream(link)) { boolean found = false; for (Path entry: stream) { ! if (entry.getFileName().equals(myfile.getFileName())) { found = true; break; } } assertTrue(found); } // Test link2 -> link -> mydir final Path link2 = dir.resolve("link2"); ! Path target2 = link.getFileName(); ! Files.createSymbolicLink(link2, target2); try { ! assertTrue(Files.readSymbolicLink(link2).equals(target2)); ! Files.newDirectoryStream(link2).close(); } finally { ! Files.delete(link2); } // Remove mydir and re-create link2 before re-creating mydir // (This is a useful test on Windows to ensure that creating a // sym link to a directory sym link creates the right type of link). ! Files.delete(myfile); ! Files.delete(mydir); ! Files.createSymbolicLink(link2, target2); try { ! assertTrue(Files.readSymbolicLink(link2).equals(target2)); ! Files.createDirectory(mydir); ! Files.newDirectoryStream(link2).close(); } finally { ! Files.delete(link2); } } finally { ! Files.deleteIfExists(myfile); ! Files.deleteIfExists(mydir); ! Files.deleteIfExists(link); } } /** * Exercise createLink method */ static void testHardLinks(Path dir) throws IOException { ! Path foo = dir.resolve("foo"); ! Files.createFile(foo); try { ! Path bar = dir.resolve("bar"); try { ! Files.createLink(bar, foo); } catch (UnsupportedOperationException x) { return; } catch (IOException x) { // probably insufficient privileges (Windows) return; } try { ! Object key1 = Files.readAttributes(foo, BasicFileAttributes.class).fileKey(); ! Object key2 = Files.readAttributes(bar, BasicFileAttributes.class).fileKey(); assertTrue((key1 == null) || (key1.equals(key2))); } finally { ! Files.delete(bar); } } finally { ! Files.delete(foo); } } public static void main(String[] args) throws IOException { Path dir = TestUtil.createTemporaryDirectory();