jdk/test/java/nio/file/Files/Links.java
author ohair
Wed, 06 Apr 2011 22:06:11 -0700
changeset 9035 1255eb81cc2f
parent 8158 77d9c0f1c19f
permissions -rw-r--r--
7033660: Update copyright year to 2011 on any files changed in 2011 Reviewed-by: dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
     1
/*
9035
1255eb81cc2f 7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents: 8158
diff changeset
     2
 * Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved.
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
     4
 *
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
     7
 * published by the Free Software Foundation.
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
     8
 *
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    13
 * accompanied this code).
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    14
 *
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4234
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4234
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4234
diff changeset
    21
 * questions.
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    22
 */
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    23
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    24
/* @test
3326
31a5302fe6d4 6863864: (fs) Path.createSymbolicLink doesn't set directory flag when creating sym link to directory (win)
alanb
parents: 3065
diff changeset
    25
 * @bug 4313887 6838333 6863864
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 5506
diff changeset
    26
 * @summary Unit test for java.nio.file.Files createSymbolicLink,
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    27
 *     readSymbolicLink, and createLink methods
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    28
 * @library ..
4234
d9720475ab15 6900234: Several NIO tests should be removed from test/ProblemList.txt
alanb
parents: 3326
diff changeset
    29
 * @build Links
d9720475ab15 6900234: Several NIO tests should be removed from test/ProblemList.txt
alanb
parents: 3326
diff changeset
    30
 * @run main/othervm Links
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    31
 */
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    32
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    33
import java.nio.file.*;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    34
import java.nio.file.attribute.*;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    35
import java.io.*;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    36
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    37
public class Links {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    38
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    39
    static final boolean isWindows =
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    40
        System.getProperty("os.name").startsWith("Windows");
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    41
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    42
    static void assertTrue(boolean okay) {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    43
        if (!okay)
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    44
            throw new RuntimeException("Assertion failed");
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    45
    }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    46
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    47
    /**
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    48
     * Exercise createSymbolicLink and readLink methods
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    49
     */
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    50
    static void testSymLinks(Path dir) throws IOException {
3326
31a5302fe6d4 6863864: (fs) Path.createSymbolicLink doesn't set directory flag when creating sym link to directory (win)
alanb
parents: 3065
diff changeset
    51
        final Path link = dir.resolve("link");
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    52
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    53
        // Check if sym links are supported
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    54
        try {
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 5506
diff changeset
    55
            Files.createSymbolicLink(link, Paths.get("foo"));
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 5506
diff changeset
    56
            Files.delete(link);
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    57
        } catch (UnsupportedOperationException x) {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    58
            // sym links not supported
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    59
            return;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    60
        } catch (IOException x) {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    61
            // probably insufficient privileges to create sym links (Windows)
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    62
            return;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    63
        }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    64
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    65
        // Test links to various targets
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    66
        String[] windowsTargets =
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    67
            { "foo", "C:\\foo", "\\foo", "\\\\server\\share\\foo" };
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    68
        String[] otherTargets = { "relative", "/absolute" };
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    69
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    70
        String[] targets = (isWindows) ? windowsTargets : otherTargets;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    71
        for (String s: targets) {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    72
            Path target = Paths.get(s);
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 5506
diff changeset
    73
            Files.createSymbolicLink(link, target);
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    74
            try {
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 5506
diff changeset
    75
                assertTrue(Files.readSymbolicLink(link).equals(target));
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    76
            } finally {
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 5506
diff changeset
    77
                Files.delete(link);
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    78
            }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    79
        }
3326
31a5302fe6d4 6863864: (fs) Path.createSymbolicLink doesn't set directory flag when creating sym link to directory (win)
alanb
parents: 3065
diff changeset
    80
31a5302fe6d4 6863864: (fs) Path.createSymbolicLink doesn't set directory flag when creating sym link to directory (win)
alanb
parents: 3065
diff changeset
    81
        // Test links to directory
31a5302fe6d4 6863864: (fs) Path.createSymbolicLink doesn't set directory flag when creating sym link to directory (win)
alanb
parents: 3065
diff changeset
    82
        Path mydir = dir.resolve("mydir");
31a5302fe6d4 6863864: (fs) Path.createSymbolicLink doesn't set directory flag when creating sym link to directory (win)
alanb
parents: 3065
diff changeset
    83
        Path myfile = mydir.resolve("myfile");
31a5302fe6d4 6863864: (fs) Path.createSymbolicLink doesn't set directory flag when creating sym link to directory (win)
alanb
parents: 3065
diff changeset
    84
        try {
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 5506
diff changeset
    85
            Files.createDirectory(mydir);
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 5506
diff changeset
    86
            Files.createFile(myfile);
3326
31a5302fe6d4 6863864: (fs) Path.createSymbolicLink doesn't set directory flag when creating sym link to directory (win)
alanb
parents: 3065
diff changeset
    87
31a5302fe6d4 6863864: (fs) Path.createSymbolicLink doesn't set directory flag when creating sym link to directory (win)
alanb
parents: 3065
diff changeset
    88
            // link -> "mydir"
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 5506
diff changeset
    89
            Files.createSymbolicLink(link, mydir.getFileName());
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 5506
diff changeset
    90
            assertTrue(Files.readSymbolicLink(link).equals(mydir.getFileName()));
3326
31a5302fe6d4 6863864: (fs) Path.createSymbolicLink doesn't set directory flag when creating sym link to directory (win)
alanb
parents: 3065
diff changeset
    91
31a5302fe6d4 6863864: (fs) Path.createSymbolicLink doesn't set directory flag when creating sym link to directory (win)
alanb
parents: 3065
diff changeset
    92
            // Test access to directory via link
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 5506
diff changeset
    93
            try (DirectoryStream<Path> stream = Files.newDirectoryStream(link)) {
3326
31a5302fe6d4 6863864: (fs) Path.createSymbolicLink doesn't set directory flag when creating sym link to directory (win)
alanb
parents: 3065
diff changeset
    94
                boolean found = false;
31a5302fe6d4 6863864: (fs) Path.createSymbolicLink doesn't set directory flag when creating sym link to directory (win)
alanb
parents: 3065
diff changeset
    95
                for (Path entry: stream) {
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 5506
diff changeset
    96
                    if (entry.getFileName().equals(myfile.getFileName())) {
3326
31a5302fe6d4 6863864: (fs) Path.createSymbolicLink doesn't set directory flag when creating sym link to directory (win)
alanb
parents: 3065
diff changeset
    97
                        found = true;
31a5302fe6d4 6863864: (fs) Path.createSymbolicLink doesn't set directory flag when creating sym link to directory (win)
alanb
parents: 3065
diff changeset
    98
                        break;
31a5302fe6d4 6863864: (fs) Path.createSymbolicLink doesn't set directory flag when creating sym link to directory (win)
alanb
parents: 3065
diff changeset
    99
                    }
31a5302fe6d4 6863864: (fs) Path.createSymbolicLink doesn't set directory flag when creating sym link to directory (win)
alanb
parents: 3065
diff changeset
   100
                }
31a5302fe6d4 6863864: (fs) Path.createSymbolicLink doesn't set directory flag when creating sym link to directory (win)
alanb
parents: 3065
diff changeset
   101
                assertTrue(found);
31a5302fe6d4 6863864: (fs) Path.createSymbolicLink doesn't set directory flag when creating sym link to directory (win)
alanb
parents: 3065
diff changeset
   102
            }
31a5302fe6d4 6863864: (fs) Path.createSymbolicLink doesn't set directory flag when creating sym link to directory (win)
alanb
parents: 3065
diff changeset
   103
31a5302fe6d4 6863864: (fs) Path.createSymbolicLink doesn't set directory flag when creating sym link to directory (win)
alanb
parents: 3065
diff changeset
   104
            // Test link2 -> link -> mydir
31a5302fe6d4 6863864: (fs) Path.createSymbolicLink doesn't set directory flag when creating sym link to directory (win)
alanb
parents: 3065
diff changeset
   105
            final Path link2 = dir.resolve("link2");
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 5506
diff changeset
   106
            Path target2 = link.getFileName();
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 5506
diff changeset
   107
            Files.createSymbolicLink(link2, target2);
3326
31a5302fe6d4 6863864: (fs) Path.createSymbolicLink doesn't set directory flag when creating sym link to directory (win)
alanb
parents: 3065
diff changeset
   108
            try {
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 5506
diff changeset
   109
                assertTrue(Files.readSymbolicLink(link2).equals(target2));
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 5506
diff changeset
   110
                Files.newDirectoryStream(link2).close();
3326
31a5302fe6d4 6863864: (fs) Path.createSymbolicLink doesn't set directory flag when creating sym link to directory (win)
alanb
parents: 3065
diff changeset
   111
            } finally {
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 5506
diff changeset
   112
                Files.delete(link2);
3326
31a5302fe6d4 6863864: (fs) Path.createSymbolicLink doesn't set directory flag when creating sym link to directory (win)
alanb
parents: 3065
diff changeset
   113
            }
31a5302fe6d4 6863864: (fs) Path.createSymbolicLink doesn't set directory flag when creating sym link to directory (win)
alanb
parents: 3065
diff changeset
   114
31a5302fe6d4 6863864: (fs) Path.createSymbolicLink doesn't set directory flag when creating sym link to directory (win)
alanb
parents: 3065
diff changeset
   115
            // Remove mydir and re-create link2 before re-creating mydir
31a5302fe6d4 6863864: (fs) Path.createSymbolicLink doesn't set directory flag when creating sym link to directory (win)
alanb
parents: 3065
diff changeset
   116
            // (This is a useful test on Windows to ensure that creating a
31a5302fe6d4 6863864: (fs) Path.createSymbolicLink doesn't set directory flag when creating sym link to directory (win)
alanb
parents: 3065
diff changeset
   117
            // sym link to a directory sym link creates the right type of link).
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 5506
diff changeset
   118
            Files.delete(myfile);
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 5506
diff changeset
   119
            Files.delete(mydir);
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 5506
diff changeset
   120
            Files.createSymbolicLink(link2, target2);
3326
31a5302fe6d4 6863864: (fs) Path.createSymbolicLink doesn't set directory flag when creating sym link to directory (win)
alanb
parents: 3065
diff changeset
   121
            try {
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 5506
diff changeset
   122
                assertTrue(Files.readSymbolicLink(link2).equals(target2));
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 5506
diff changeset
   123
                Files.createDirectory(mydir);
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 5506
diff changeset
   124
                Files.newDirectoryStream(link2).close();
3326
31a5302fe6d4 6863864: (fs) Path.createSymbolicLink doesn't set directory flag when creating sym link to directory (win)
alanb
parents: 3065
diff changeset
   125
            } finally {
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 5506
diff changeset
   126
                Files.delete(link2);
3326
31a5302fe6d4 6863864: (fs) Path.createSymbolicLink doesn't set directory flag when creating sym link to directory (win)
alanb
parents: 3065
diff changeset
   127
            }
31a5302fe6d4 6863864: (fs) Path.createSymbolicLink doesn't set directory flag when creating sym link to directory (win)
alanb
parents: 3065
diff changeset
   128
31a5302fe6d4 6863864: (fs) Path.createSymbolicLink doesn't set directory flag when creating sym link to directory (win)
alanb
parents: 3065
diff changeset
   129
        } finally {
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 5506
diff changeset
   130
            Files.deleteIfExists(myfile);
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 5506
diff changeset
   131
            Files.deleteIfExists(mydir);
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 5506
diff changeset
   132
            Files.deleteIfExists(link);
3326
31a5302fe6d4 6863864: (fs) Path.createSymbolicLink doesn't set directory flag when creating sym link to directory (win)
alanb
parents: 3065
diff changeset
   133
        }
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   134
    }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   135
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   136
    /**
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   137
     * Exercise createLink method
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   138
     */
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   139
    static void testHardLinks(Path dir) throws IOException {
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 5506
diff changeset
   140
        Path foo = dir.resolve("foo");
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 5506
diff changeset
   141
        Files.createFile(foo);
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   142
        try {
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 5506
diff changeset
   143
            Path bar = dir.resolve("bar");
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   144
            try {
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 5506
diff changeset
   145
                Files.createLink(bar, foo);
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   146
            } catch (UnsupportedOperationException x) {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   147
                return;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   148
            } catch (IOException x) {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   149
                // probably insufficient privileges (Windows)
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   150
                return;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   151
            }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   152
            try {
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 5506
diff changeset
   153
                Object key1 = Files.readAttributes(foo, BasicFileAttributes.class).fileKey();
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 5506
diff changeset
   154
                Object key2 = Files.readAttributes(bar, BasicFileAttributes.class).fileKey();
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   155
                assertTrue((key1 == null) || (key1.equals(key2)));
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   156
            } finally {
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 5506
diff changeset
   157
                Files.delete(bar);
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   158
            }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   159
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   160
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   161
        } finally {
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 5506
diff changeset
   162
            Files.delete(foo);
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   163
        }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   164
    }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   165
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   166
    public static void main(String[] args) throws IOException {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   167
        Path dir = TestUtil.createTemporaryDirectory();
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   168
        try {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   169
            testSymLinks(dir);
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   170
            testHardLinks(dir);
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   171
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   172
            // repeat tests on Windows with long path
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   173
            if (isWindows) {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   174
                Path dirWithLongPath = null;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   175
                try {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   176
                    dirWithLongPath = TestUtil.createDirectoryWithLongPath(dir);
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   177
                } catch (IOException x) {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   178
                    System.out.println("Unable to create long path: " + x);
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   179
                }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   180
                if (dirWithLongPath != null) {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   181
                    System.out.println("");
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   182
                    System.out.println("** REPEAT TESTS WITH LONG PATH **");
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   183
                    testSymLinks(dirWithLongPath);
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   184
                    testHardLinks(dirWithLongPath);
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   185
                }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   186
            }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   187
        } finally {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   188
            TestUtil.removeAll(dir);
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   189
        }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   190
    }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   191
}