Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/nio/file/Files/Misc.java
38828 views
/*1* Copyright (c) 2008, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24* @bug 4313887 6838333 8005566 803222025* @summary Unit test for miscellenous methods in java.nio.file.Files26* @library ..27*/2829import java.nio.file.*;30import static java.nio.file.Files.*;31import static java.nio.file.LinkOption.*;32import java.nio.file.attribute.*;33import java.io.IOException;34import java.util.*;3536public class Misc {3738public static void main(String[] args) throws IOException {39Path dir = TestUtil.createTemporaryDirectory();40try {41testCreateDirectories(dir);42testIsHidden(dir);43testIsSameFile(dir);44testFileTypeMethods(dir);45testAccessMethods(dir);46} finally {47TestUtil.removeAll(dir);48}49}5051/**52* Tests createDirectories53*/54static void testCreateDirectories(Path tmpdir) throws IOException {55// a no-op56createDirectories(tmpdir);5758// create one directory59Path subdir = tmpdir.resolve("a");60createDirectories(subdir);61assertTrue(exists(subdir));6263// create parents64subdir = subdir.resolve("b/c/d");65createDirectories(subdir);66assertTrue(exists(subdir));6768// existing file is not a directory69Path file = createFile(tmpdir.resolve("x"));70try {71createDirectories(file);72throw new RuntimeException("failure expected");73} catch (FileAlreadyExistsException x) { }74try {75createDirectories(file.resolve("y"));76throw new RuntimeException("failure expected");77} catch (IOException x) { }7879// the root directory always exists80Path root = Paths.get("/");81Files.createDirectories(root);82Files.createDirectories(root.toAbsolutePath());83}8485/**86* Tests isHidden87*/88static void testIsHidden(Path tmpdir) throws IOException {89assertTrue(!isHidden(tmpdir));9091Path file = tmpdir.resolve(".foo");92if (System.getProperty("os.name").startsWith("Windows")) {93createFile(file);94try {95setAttribute(file, "dos:hidden", true);96try {97assertTrue(isHidden(file));98} finally {99setAttribute(file, "dos:hidden", false);100}101} finally {102delete(file);103}104} else {105assertTrue(isHidden(file));106}107}108109/**110* Tests isSameFile111*/112static void testIsSameFile(Path tmpdir) throws IOException {113Path thisFile = tmpdir.resolve("thisFile");114Path thatFile = tmpdir.resolve("thatFile");115116/**117* Test: isSameFile for self118*/119assertTrue(isSameFile(thisFile, thisFile));120121/**122* Test: Neither files exist123*/124try {125isSameFile(thisFile, thatFile);126throw new RuntimeException("IOException not thrown");127} catch (IOException x) {128}129try {130isSameFile(thatFile, thisFile);131throw new RuntimeException("IOException not thrown");132} catch (IOException x) {133}134135createFile(thisFile);136try {137/**138* Test: One file exists139*/140try {141isSameFile(thisFile, thatFile);142throw new RuntimeException("IOException not thrown");143} catch (IOException x) {144}145try {146isSameFile(thatFile, thisFile);147throw new RuntimeException("IOException not thrown");148} catch (IOException x) {149}150151/**152* Test: Both file exists153*/154createFile(thatFile);155try {156assertTrue(!isSameFile(thisFile, thatFile));157assertTrue(!isSameFile(thatFile, thisFile));158} finally {159delete(thatFile);160}161162/**163* Test: Symbolic links164*/165if (TestUtil.supportsLinks(tmpdir)) {166createSymbolicLink(thatFile, thisFile);167try {168assertTrue(isSameFile(thisFile, thatFile));169assertTrue(isSameFile(thatFile, thisFile));170} finally {171TestUtil.deleteUnchecked(thatFile);172}173}174} finally {175delete(thisFile);176}177178// nulls179try {180isSameFile(thisFile, null);181throw new RuntimeException("NullPointerException expected");182} catch (NullPointerException ignore) { }183try {184isSameFile(null, thatFile);185throw new RuntimeException("NullPointerException expected");186} catch (NullPointerException ignore) { }187}188189/**190* Exercise isRegularFile, isDirectory, isSymbolicLink191*/192static void testFileTypeMethods(Path tmpdir) throws IOException {193assertTrue(!isRegularFile(tmpdir));194assertTrue(!isRegularFile(tmpdir, NOFOLLOW_LINKS));195assertTrue(isDirectory(tmpdir));196assertTrue(isDirectory(tmpdir, NOFOLLOW_LINKS));197assertTrue(!isSymbolicLink(tmpdir));198199Path file = createFile(tmpdir.resolve("foo"));200try {201assertTrue(isRegularFile(file));202assertTrue(isRegularFile(file, NOFOLLOW_LINKS));203assertTrue(!isDirectory(file));204assertTrue(!isDirectory(file, NOFOLLOW_LINKS));205assertTrue(!isSymbolicLink(file));206207if (TestUtil.supportsLinks(tmpdir)) {208Path link = tmpdir.resolve("link");209210createSymbolicLink(link, tmpdir);211try {212assertTrue(!isRegularFile(link));213assertTrue(!isRegularFile(link, NOFOLLOW_LINKS));214assertTrue(isDirectory(link));215assertTrue(!isDirectory(link, NOFOLLOW_LINKS));216assertTrue(isSymbolicLink(link));217} finally {218delete(link);219}220221createSymbolicLink(link, file);222try {223assertTrue(isRegularFile(link));224assertTrue(!isRegularFile(link, NOFOLLOW_LINKS));225assertTrue(!isDirectory(link));226assertTrue(!isDirectory(link, NOFOLLOW_LINKS));227assertTrue(isSymbolicLink(link));228} finally {229delete(link);230}231232createLink(link, file);233try {234assertTrue(isRegularFile(link));235assertTrue(isRegularFile(link, NOFOLLOW_LINKS));236assertTrue(!isDirectory(link));237assertTrue(!isDirectory(link, NOFOLLOW_LINKS));238assertTrue(!isSymbolicLink(link));239} finally {240delete(link);241}242}243244} finally {245delete(file);246}247}248249/**250* Exercise isReadbale, isWritable, isExecutable, exists, notExists251*/252static void testAccessMethods(Path tmpdir) throws IOException {253// should return false when file does not exist254Path doesNotExist = tmpdir.resolve("doesNotExist");255assertTrue(!isReadable(doesNotExist));256assertTrue(!isWritable(doesNotExist));257assertTrue(!isExecutable(doesNotExist));258assertTrue(!exists(doesNotExist));259assertTrue(notExists(doesNotExist));260261Path file = createFile(tmpdir.resolve("foo"));262try {263// files exist264assertTrue(isReadable(file));265assertTrue(isWritable(file));266assertTrue(exists(file));267assertTrue(!notExists(file));268assertTrue(isReadable(tmpdir));269assertTrue(isWritable(tmpdir));270assertTrue(exists(tmpdir));271assertTrue(!notExists(tmpdir));272273274// sym link exists275if (TestUtil.supportsLinks(tmpdir)) {276Path link = tmpdir.resolve("link");277278createSymbolicLink(link, file);279try {280assertTrue(isReadable(link));281assertTrue(isWritable(link));282assertTrue(exists(link));283assertTrue(!notExists(link));284} finally {285delete(link);286}287288createSymbolicLink(link, doesNotExist);289try {290assertTrue(!isReadable(link));291assertTrue(!isWritable(link));292assertTrue(!exists(link));293assertTrue(exists(link, NOFOLLOW_LINKS));294assertTrue(notExists(link));295assertTrue(!notExists(link, NOFOLLOW_LINKS));296} finally {297delete(link);298}299}300301/**302* Test: Edit ACL to deny WRITE and EXECUTE303*/304if (getFileStore(file).supportsFileAttributeView("acl")) {305AclFileAttributeView view =306getFileAttributeView(file, AclFileAttributeView.class);307UserPrincipal owner = view.getOwner();308List<AclEntry> acl = view.getAcl();309310// Insert entry to deny WRITE and EXECUTE311AclEntry entry = AclEntry.newBuilder()312.setType(AclEntryType.DENY)313.setPrincipal(owner)314.setPermissions(AclEntryPermission.WRITE_DATA,315AclEntryPermission.EXECUTE)316.build();317acl.add(0, entry);318view.setAcl(acl);319try {320if (isRoot()) {321// root has all permissions322assertTrue(isWritable(file));323assertTrue(isExecutable(file));324} else {325assertTrue(!isWritable(file));326assertTrue(!isExecutable(file));327}328} finally {329// Restore ACL330acl.remove(0);331view.setAcl(acl);332}333}334335/**336* Test: Windows DOS read-only attribute337*/338if (System.getProperty("os.name").startsWith("Windows")) {339setAttribute(file, "dos:readonly", true);340try {341assertTrue(!isWritable(file));342} finally {343setAttribute(file, "dos:readonly", false);344}345346// Read-only attribute does not make direcory read-only347DosFileAttributeView view =348getFileAttributeView(tmpdir, DosFileAttributeView.class);349boolean save = view.readAttributes().isReadOnly();350view.setReadOnly(true);351try {352assertTrue(isWritable(file));353} finally {354view.setReadOnly(save);355}356}357} finally {358delete(file);359}360}361362static void assertTrue(boolean okay) {363if (!okay)364throw new RuntimeException("Assertion Failed");365}366367private static boolean isRoot() {368if (System.getProperty("os.name").startsWith("Windows"))369return false;370371Path passwd = Paths.get("/etc/passwd");372return Files.isWritable(passwd);373}374}375376377