• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 package test.java.nio.file.Path;
24 
25 /* @test
26  * @bug 4313887 6838333 7029979
27  * @summary Unit test for miscellenous java.nio.file.Path methods
28  * @library ..
29  */
30 
31 import java.nio.file.*;
32 import static java.nio.file.LinkOption.*;
33 import java.io.*;
34 import org.testng.annotations.Test;
35 import static org.testng.Assert.assertTrue;
36 
37 public class Misc {
38     static boolean supportsLinks;
39 
40     /**
41      * Exercise equals and hashCode methods
42      */
43     @Test
testEqualsAndHashCode()44     public void testEqualsAndHashCode() {
45         Path thisFile = Paths.get("this");
46         Path thatFile = Paths.get("that");
47 
48         assertTrue(thisFile.equals(thisFile));
49         assertTrue(!thisFile.equals(thatFile));
50 
51         assertTrue(!thisFile.equals(null));
52         assertTrue(!thisFile.equals(new Object()));
53 
54         Path likeThis = Paths.get("This");
55         assertTrue(!thisFile.equals(likeThis));
56     }
57 
58     /**
59      * Exercise toFile method
60      */
61     @Test
testToFile()62     public void testToFile() throws IOException {
63         Path dir = Files.createTempDirectory("tempdir");
64         File d = dir.toFile();
65         assertTrue(d.toString().equals(dir.toString()));
66         assertTrue(d.toPath().equals(dir));
67     }
68 
69     /**
70      * Exercise toRealPath method
71      */
72     @Test
testToRealPath()73     public void testToRealPath() throws IOException {
74         Path dir = Files.createTempDirectory("tempdir");
75         final Path file = Files.createFile(dir.resolve("foo"));
76         final Path link = dir.resolve("link");
77 
78         /**
79          * Test: totRealPath() will access same file as toRealPath(NOFOLLOW_LINKS)
80          */
81         assertTrue(Files.isSameFile(file.toRealPath(), file.toRealPath(NOFOLLOW_LINKS)));
82 
83         /**
84          * Test: toRealPath should fail if file does not exist
85          */
86         Path doesNotExist = dir.resolve("DoesNotExist");
87         try {
88             doesNotExist.toRealPath();
89             throw new RuntimeException("IOException expected");
90         } catch (IOException expected) {
91         }
92         try {
93             doesNotExist.toRealPath(NOFOLLOW_LINKS);
94             throw new RuntimeException("IOException expected");
95         } catch (IOException expected) {
96         }
97 
98         /**
99          * Test: toRealPath() should resolve links
100          */
101         if (supportsLinks) {
102             Path resolvedFile = file;
103             Files.createSymbolicLink(link, resolvedFile.toAbsolutePath());
104             assertTrue(link.toRealPath().equals(resolvedFile.toRealPath()));
105             Files.delete(link);
106         }
107 
108         /**
109          * Test: toRealPath(NOFOLLOW_LINKS) should not resolve links
110          */
111         if (supportsLinks) {
112             Files.createSymbolicLink(link, file.toAbsolutePath());
113             assertTrue(link.toRealPath(NOFOLLOW_LINKS).getFileName().equals(link.getFileName()));
114             Files.delete(link);
115         }
116 
117         /**
118          * Test: toRealPath(NOFOLLOW_LINKS) with broken link
119          */
120         if (supportsLinks) {
121             Path broken = Files.createSymbolicLink(link, doesNotExist);
122             assertTrue(link.toRealPath(NOFOLLOW_LINKS).getFileName().equals(link.getFileName()));
123             Files.delete(link);
124         }
125 
126         /**
127          * Test: toRealPath should eliminate "."
128          */
129         assertTrue(dir.resolve(".").toRealPath().equals(dir.toRealPath()));
130         assertTrue(dir.resolve(".").toRealPath(NOFOLLOW_LINKS).equals(dir.toRealPath(NOFOLLOW_LINKS)));
131 
132         /**
133          * Test: toRealPath should eliminate ".." when it doesn't follow a
134          *       symbolic link
135          */
136         Path subdir = Files.createDirectory(dir.resolve("subdir"));
137         assertTrue(subdir.resolve("..").toRealPath().equals(dir.toRealPath()));
138         assertTrue(subdir.resolve("..").toRealPath(NOFOLLOW_LINKS).equals(dir.toRealPath(NOFOLLOW_LINKS)));
139         Files.delete(subdir);
140 
141         // clean-up
142         Files.delete(file);
143     }
144 }