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 6865748 26 * @summary Unit test for java.nio.file.Files for miscellenous cases not 27 * covered by other tests 28 * @library .. 29 */ 30 31 import java.nio.file.*; 32 import java.nio.file.attribute.Attributes; 33 import java.nio.file.attribute.BasicFileAttributes; 34 import java.io.IOException; 35 import java.util.*; 36 37 public class Misc { 38 39 static void npeExpected() { 40 throw new RuntimeException("NullPointerException expected"); 41 } 42 43 public static void main(String[] args) throws IOException { 44 45 // -- Files.createDirectories -- 46 47 Path dir = TestUtil.createTemporaryDirectory(); 48 try { 49 // no-op 50 Files.createDirectories(dir); 51 52 // create one directory 53 Path subdir = dir.resolve("a"); 54 Files.createDirectories(subdir); 55 if (!subdir.exists()) 56 throw new RuntimeException("directory not created"); 57 58 // create parents 59 subdir = subdir.resolve("b/c/d"); 60 Files.createDirectories(subdir); 61 if (!subdir.exists()) 62 throw new RuntimeException("directory not created"); 63 64 // existing file is not a directory 65 Path file = dir.resolve("x").createFile(); 66 try { 67 Files.createDirectories(file); 68 throw new RuntimeException("failure expected"); 69 } catch (FileAlreadyExistsException x) { } 70 try { 71 Files.createDirectories(file.resolve("y")); 72 throw new RuntimeException("failure expected"); 73 } catch (IOException x) { } 74 75 } finally { 76 TestUtil.removeAll(dir); 77 } 78 79 // --- NullPointerException -- 80 81 try { 82 Files.probeContentType(null); 83 npeExpected(); 84 } catch (NullPointerException e) { 85 } 86 try { 87 Files.walkFileTree(null, EnumSet.noneOf(FileVisitOption.class), 88 Integer.MAX_VALUE, new SimpleFileVisitor<Path>(){}); 89 npeExpected(); 90 } catch (NullPointerException e) { 91 } 92 try { 93 Files.walkFileTree(Paths.get("."), null, Integer.MAX_VALUE, 94 new SimpleFileVisitor<Path>(){}); 95 npeExpected(); 96 } catch (NullPointerException e) { 97 } 98 try { 99 Files.walkFileTree(Paths.get("."), EnumSet.noneOf(FileVisitOption.class), 100 -1, new SimpleFileVisitor<Path>(){}); 101 throw new RuntimeException("IllegalArgumentExpected expected"); 102 } catch (IllegalArgumentException e) { 103 } 104 try { 105 Set<FileVisitOption> opts = new HashSet<FileVisitOption>(1); 106 opts.add(null); 107 Files.walkFileTree(Paths.get("."), opts, Integer.MAX_VALUE, 108 new SimpleFileVisitor<Path>(){}); 109 npeExpected(); 110 } catch (NullPointerException e) { 111 } 112 try { 113 Files.walkFileTree(Paths.get("."), EnumSet.noneOf(FileVisitOption.class), 114 Integer.MAX_VALUE, null); 115 npeExpected(); 116 } catch (NullPointerException e) { 117 } 118 119 SimpleFileVisitor<Path> visitor = new SimpleFileVisitor<Path>() { }; 120 boolean ranTheGauntlet = false; 121 BasicFileAttributes attrs = Attributes.readBasicFileAttributes(Paths.get(".")); 122 123 try { visitor.preVisitDirectory(null, attrs); 124 } catch (NullPointerException x0) { 125 try { visitor.preVisitDirectory(dir, null); 126 } catch (NullPointerException x1) { 127 try { visitor.visitFile(null, attrs); 128 } catch (NullPointerException x2) { 129 try { visitor.visitFile(dir, null); 130 } catch (NullPointerException x3) { 131 try { visitor.visitFileFailed(null, new IOException()); 132 } catch (NullPointerException x4) { 133 try { visitor.visitFileFailed(dir, null); 134 } catch (NullPointerException x5) { 135 try { visitor.postVisitDirectory(null, new IOException()); 136 } catch (NullPointerException x6) { 137 // if we get here then all visit* methods threw NPE as expected 138 ranTheGauntlet = true; 139 }}}}}}} 140 if (!ranTheGauntlet) 141 throw new RuntimeException("A visit method did not throw NPE"); 142 } 143 } | 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 methods in java.nio.file.Files 27 * @library .. 28 */ 29 30 import java.nio.file.*; 31 import static java.nio.file.Files.*; 32 import static java.nio.file.LinkOption.*; 33 import java.nio.file.attribute.*; 34 import java.io.IOException; 35 import java.util.*; 36 37 public class Misc { 38 39 public static void main(String[] args) throws IOException { 40 Path dir = TestUtil.createTemporaryDirectory(); 41 try { 42 testCreateDirectories(dir); 43 testIsHidden(dir); 44 testIsSameFile(dir); 45 testFileTypeMethods(dir); 46 testAccessMethods(dir); 47 } finally { 48 TestUtil.removeAll(dir); 49 } 50 } 51 52 /** 53 * Tests createDirectories 54 */ 55 static void testCreateDirectories(Path tmpdir) throws IOException { 56 // a no-op 57 createDirectories(tmpdir); 58 59 // create one directory 60 Path subdir = tmpdir.resolve("a"); 61 createDirectories(subdir); 62 assertTrue(exists(subdir)); 63 64 // create parents 65 subdir = subdir.resolve("b/c/d"); 66 createDirectories(subdir); 67 assertTrue(exists(subdir)); 68 69 // existing file is not a directory 70 Path file = createFile(tmpdir.resolve("x")); 71 try { 72 createDirectories(file); 73 throw new RuntimeException("failure expected"); 74 } catch (FileAlreadyExistsException x) { } 75 try { 76 createDirectories(file.resolve("y")); 77 throw new RuntimeException("failure expected"); 78 } catch (IOException x) { } 79 } 80 81 /** 82 * Tests isHidden 83 */ 84 static void testIsHidden(Path tmpdir) throws IOException { 85 assertTrue(!isHidden(tmpdir)); 86 87 Path file = tmpdir.resolve(".foo"); 88 if (System.getProperty("os.name").startsWith("Windows")) { 89 createFile(file); 90 try { 91 setAttribute(file, "dos:hidden", true); 92 try { 93 assertTrue(isHidden(file)); 94 } finally { 95 setAttribute(file, "dos:hidden", false); 96 } 97 } finally { 98 delete(file); 99 } 100 } else { 101 assertTrue(isHidden(file)); 102 } 103 } 104 105 /** 106 * Tests isSameFile 107 */ 108 static void testIsSameFile(Path tmpdir) throws IOException { 109 Path thisFile = tmpdir.resolve("thisFile"); 110 Path thatFile = tmpdir.resolve("thatFile"); 111 112 /** 113 * Test: isSameFile for self 114 */ 115 assertTrue(isSameFile(thisFile, thisFile)); 116 117 /** 118 * Test: Neither files exist 119 */ 120 try { 121 isSameFile(thisFile, thatFile); 122 throw new RuntimeException("IOException not thrown"); 123 } catch (IOException x) { 124 } 125 try { 126 isSameFile(thatFile, thisFile); 127 throw new RuntimeException("IOException not thrown"); 128 } catch (IOException x) { 129 } 130 131 createFile(thisFile); 132 try { 133 /** 134 * Test: One file exists 135 */ 136 try { 137 isSameFile(thisFile, thatFile); 138 throw new RuntimeException("IOException not thrown"); 139 } catch (IOException x) { 140 } 141 try { 142 isSameFile(thatFile, thisFile); 143 throw new RuntimeException("IOException not thrown"); 144 } catch (IOException x) { 145 } 146 147 /** 148 * Test: Both file exists 149 */ 150 createFile(thatFile); 151 try { 152 assertTrue(!isSameFile(thisFile, thatFile)); 153 assertTrue(!isSameFile(thatFile, thisFile)); 154 } finally { 155 delete(thatFile); 156 } 157 158 /** 159 * Test: Symbolic links 160 */ 161 if (TestUtil.supportsLinks(tmpdir)) { 162 createSymbolicLink(thatFile, thisFile); 163 try { 164 assertTrue(isSameFile(thisFile, thatFile)); 165 assertTrue(isSameFile(thatFile, thisFile)); 166 } finally { 167 TestUtil.deleteUnchecked(thatFile); 168 } 169 } 170 } finally { 171 delete(thisFile); 172 } 173 174 // nulls 175 try { 176 isSameFile(thisFile, null); 177 throw new RuntimeException("NullPointerException expected"); 178 } catch (NullPointerException ignore) { } 179 try { 180 isSameFile(null, thatFile); 181 throw new RuntimeException("NullPointerException expected"); 182 } catch (NullPointerException ignore) { } 183 } 184 185 /** 186 * Exercise isRegularFile, isDirectory, isSymbolicLink 187 */ 188 static void testFileTypeMethods(Path tmpdir) throws IOException { 189 assertTrue(!isRegularFile(tmpdir)); 190 assertTrue(!isRegularFile(tmpdir, NOFOLLOW_LINKS)); 191 assertTrue(isDirectory(tmpdir)); 192 assertTrue(isDirectory(tmpdir, NOFOLLOW_LINKS)); 193 assertTrue(!isSymbolicLink(tmpdir)); 194 195 Path file = createFile(tmpdir.resolve("foo")); 196 try { 197 assertTrue(isRegularFile(file)); 198 assertTrue(isRegularFile(file, NOFOLLOW_LINKS)); 199 assertTrue(!isDirectory(file)); 200 assertTrue(!isDirectory(file, NOFOLLOW_LINKS)); 201 assertTrue(!isSymbolicLink(file)); 202 203 if (TestUtil.supportsLinks(tmpdir)) { 204 Path link = tmpdir.resolve("link"); 205 206 createSymbolicLink(link, tmpdir); 207 try { 208 assertTrue(!isRegularFile(link)); 209 assertTrue(!isRegularFile(link, NOFOLLOW_LINKS)); 210 assertTrue(isDirectory(link)); 211 assertTrue(!isDirectory(link, NOFOLLOW_LINKS)); 212 assertTrue(isSymbolicLink(link)); 213 } finally { 214 delete(link); 215 } 216 217 createSymbolicLink(link, file); 218 try { 219 assertTrue(isRegularFile(link)); 220 assertTrue(!isRegularFile(link, NOFOLLOW_LINKS)); 221 assertTrue(!isDirectory(link)); 222 assertTrue(!isDirectory(link, NOFOLLOW_LINKS)); 223 assertTrue(isSymbolicLink(link)); 224 } finally { 225 delete(link); 226 } 227 228 createLink(link, file); 229 try { 230 assertTrue(isRegularFile(link)); 231 assertTrue(isRegularFile(link, NOFOLLOW_LINKS)); 232 assertTrue(!isDirectory(link)); 233 assertTrue(!isDirectory(link, NOFOLLOW_LINKS)); 234 assertTrue(!isSymbolicLink(link)); 235 } finally { 236 delete(link); 237 } 238 } 239 240 } finally { 241 delete(file); 242 } 243 } 244 245 /** 246 * Exercise isReadbale, isWritable, isExecutable, exists, notExists 247 */ 248 static void testAccessMethods(Path tmpdir) throws IOException { 249 // should return false when file does not exist 250 Path doesNotExist = tmpdir.resolve("doesNotExist"); 251 assertTrue(!isReadable(doesNotExist)); 252 assertTrue(!isWritable(doesNotExist)); 253 assertTrue(!isExecutable(doesNotExist)); 254 assertTrue(!exists(doesNotExist)); 255 assertTrue(notExists(doesNotExist)); 256 257 Path file = createFile(tmpdir.resolve("foo")); 258 try { 259 // files exist 260 assertTrue(isReadable(file)); 261 assertTrue(isWritable(file)); 262 assertTrue(exists(file)); 263 assertTrue(!notExists(file)); 264 assertTrue(isReadable(tmpdir)); 265 assertTrue(isWritable(tmpdir)); 266 assertTrue(exists(tmpdir)); 267 assertTrue(!notExists(tmpdir)); 268 269 270 // sym link exists 271 if (TestUtil.supportsLinks(tmpdir)) { 272 Path link = tmpdir.resolve("link"); 273 274 createSymbolicLink(link, file); 275 try { 276 assertTrue(isReadable(link)); 277 assertTrue(isWritable(link)); 278 assertTrue(exists(link)); 279 assertTrue(!notExists(link)); 280 } finally { 281 delete(link); 282 } 283 284 createSymbolicLink(link, doesNotExist); 285 try { 286 assertTrue(!isReadable(link)); 287 assertTrue(!isWritable(link)); 288 assertTrue(!exists(link)); 289 assertTrue(exists(link, NOFOLLOW_LINKS)); 290 assertTrue(notExists(link)); 291 assertTrue(!notExists(link, NOFOLLOW_LINKS)); 292 } finally { 293 delete(link); 294 } 295 } 296 297 /** 298 * Test: Edit ACL to deny WRITE and EXECUTE 299 */ 300 if (getFileStore(file).supportsFileAttributeView("acl")) { 301 AclFileAttributeView view = 302 getFileAttributeView(file, AclFileAttributeView.class); 303 UserPrincipal owner = view.getOwner(); 304 List<AclEntry> acl = view.getAcl(); 305 306 // Insert entry to deny WRITE and EXECUTE 307 AclEntry entry = AclEntry.newBuilder() 308 .setType(AclEntryType.DENY) 309 .setPrincipal(owner) 310 .setPermissions(AclEntryPermission.WRITE_DATA, 311 AclEntryPermission.EXECUTE) 312 .build(); 313 acl.add(0, entry); 314 view.setAcl(acl); 315 try { 316 assertTrue(!isWritable(file)); 317 assertTrue(!isExecutable(file)); 318 } finally { 319 // Restore ACL 320 acl.remove(0); 321 view.setAcl(acl); 322 } 323 } 324 325 /** 326 * Test: Windows DOS read-only attribute 327 */ 328 if (System.getProperty("os.name").startsWith("Windows")) { 329 setAttribute(file, "dos:readonly", true); 330 try { 331 assertTrue(!isWritable(file)); 332 } finally { 333 setAttribute(file, "dos:readonly", false); 334 } 335 336 // Read-only attribute does not make direcory read-only 337 DosFileAttributeView view = 338 getFileAttributeView(tmpdir, DosFileAttributeView.class); 339 boolean save = view.readAttributes().isReadOnly(); 340 view.setReadOnly(true); 341 try { 342 assertTrue(isWritable(file)); 343 } finally { 344 view.setReadOnly(save); 345 } 346 } 347 } finally { 348 delete(file); 349 } 350 } 351 352 static void assertTrue(boolean okay) { 353 if (!okay) 354 throw new RuntimeException("Assertion Failed"); 355 } 356 } |