1 /* 2 * Copyright (c) 2008, 2010, 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 24 /* @test 25 * @bug 4313887 6838333 26 * @summary Unit test for miscellenous java.nio.file.Path methods 27 * @library .. 28 */ 29 30 import java.nio.file.*; 31 import java.io.*; 32 33 public class Misc { 34 static final boolean isWindows = 35 System.getProperty("os.name").startsWith("Windows"); 36 static boolean supportsLinks; 37 38 public static void main(String[] args) throws IOException { 39 Path dir = TestUtil.createTemporaryDirectory(); 40 try { 41 supportsLinks = TestUtil.supportsLinks(dir); 42 43 // equals and hashCode methods 44 testEqualsAndHashCode(); 45 46 // toFile method 47 testToFile(dir); 48 49 // toRealPath method 50 testToRealPath(dir); 51 52 53 } finally { 54 TestUtil.removeAll(dir); 55 } 56 } 57 58 /** 59 * Exercise equals and hashCode methods 60 */ 61 static void testEqualsAndHashCode() { 62 Path thisFile = Paths.get("this"); 63 Path thatFile = Paths.get("that"); 64 65 assertTrue(thisFile.equals(thisFile)); 66 assertTrue(!thisFile.equals(thatFile)); 67 68 assertTrue(!thisFile.equals(null)); 69 assertTrue(!thisFile.equals(new Object())); 70 71 Path likeThis = Paths.get("This"); 72 if (isWindows) { 73 // case insensitive 74 assertTrue(thisFile.equals(likeThis)); 75 assertTrue(thisFile.hashCode() == likeThis.hashCode()); 76 } else { 77 // case senstive 78 assertTrue(!thisFile.equals(likeThis)); 79 } 80 } 81 82 /** 83 * Exercise toFile method 84 */ 85 static void testToFile(Path dir) throws IOException { 86 File d = dir.toFile(); 87 assertTrue(d.toString().equals(dir.toString())); 88 assertTrue(d.toPath().equals(dir)); 89 } 90 91 /** 92 * Exercise toRealPath method 93 */ 94 static void testToRealPath(Path dir) throws IOException { 95 final Path file = Files.createFile(dir.resolve("foo")); 96 final Path link = dir.resolve("link"); 97 98 /** 99 * Test: totRealPath(true) will access same file as toRealPath(false) 100 */ 101 assertTrue(Files.isSameFile(file.toRealPath(true), file.toRealPath(false))); 102 103 /** 104 * Test: toRealPath should fail if file does not exist 105 */ 106 Path doesNotExist = dir.resolve("DoesNotExist"); 107 try { 108 doesNotExist.toRealPath(true); 109 throw new RuntimeException("IOException expected"); 110 } catch (IOException expected) { 111 } 112 try { 113 doesNotExist.toRealPath(false); 114 throw new RuntimeException("IOException expected"); 115 } catch (IOException expected) { 116 } 117 118 /** 119 * Test: toRealPath(true) should resolve links 120 */ 121 if (supportsLinks) { 122 Files.createSymbolicLink(link, file.toAbsolutePath()); 123 assertTrue(link.toRealPath(true).equals(file.toRealPath(true))); 124 Files.delete(link); 125 } 126 127 /** 128 * Test: toRealPath(false) should not resolve links 129 */ 130 if (supportsLinks) { 131 Files.createSymbolicLink(link, file.toAbsolutePath()); 132 assertTrue(link.toRealPath(false).getFileName().equals(link.getFileName())); 133 Files.delete(link); 134 } 135 136 /** 137 * Test: toRealPath(false) with broken link 138 */ 139 if (supportsLinks) { 140 Path broken = Files.createSymbolicLink(link, doesNotExist); 141 assertTrue(link.toRealPath(false).getFileName().equals(link.getFileName())); 142 Files.delete(link); 143 } 144 145 /** 146 * Test: toRealPath should eliminate "." 147 */ 148 assertTrue(dir.resolve(".").toRealPath(true).equals(dir.toRealPath(true))); 149 assertTrue(dir.resolve(".").toRealPath(false).equals(dir.toRealPath(false))); 150 151 /** 152 * Test: toRealPath should eliminate ".." when it doesn't follow a 153 * symbolic link 154 */ 155 Path subdir = Files.createDirectory(dir.resolve("subdir")); 156 assertTrue(subdir.resolve("..").toRealPath(true).equals(dir.toRealPath(true))); 157 assertTrue(subdir.resolve("..").toRealPath(false).equals(dir.toRealPath(false))); 158 Files.delete(subdir); 159 160 // clean-up 161 Files.delete(file); 162 } 163 164 static void assertTrue(boolean okay) { 165 if (!okay) 166 throw new RuntimeException("Assertion Failed"); 167 } 168 }