Path: blob/master/test/jdk/java/nio/file/FileStore/Basic.java
41153 views
/*1* Copyright (c) 2008, 2021, 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 6873621 6979526 7006126 7020517 826440025* @summary Unit test for java.nio.file.FileStore26* @key intermittent27* @library .. /test/lib28* @build jdk.test.lib.Platform29* jdk.test.lib.util.FileUtils30* @run main Basic31*/3233import java.nio.file.*;34import java.nio.file.attribute.*;35import java.io.File;36import java.io.IOException;3738import jdk.test.lib.Platform;39import jdk.test.lib.util.FileUtils;4041public class Basic {4243static final long G = 1024L * 1024L * 1024L;4445public static void main(String[] args) throws IOException {46Path dir = TestUtil.createTemporaryDirectory();47try {48doTests(dir);49} finally {50TestUtil.removeAll(dir);51}52}5354static void assertTrue(boolean okay) {55if (!okay)56throw new RuntimeException("Assertion failed");57}5859static void checkWithin1GB(long value1, long value2) {60long diff = Math.abs(value1 - value2);61if (diff > G)62throw new RuntimeException("values differ by more than 1GB");63}6465static void doTests(Path dir) throws IOException {66/**67* Test: Directory should be on FileStore that is writable68*/69assertTrue(!Files.getFileStore(dir).isReadOnly());7071/**72* Test: Two files should have the same FileStore73*/74Path file1 = Files.createFile(dir.resolve("foo"));75Path file2 = Files.createFile(dir.resolve("bar"));76FileStore store1 = Files.getFileStore(file1);77FileStore store2 = Files.getFileStore(file2);78assertTrue(store1.equals(store2));79assertTrue(store2.equals(store1));80assertTrue(store1.hashCode() == store2.hashCode());8182if (Platform.isWindows()) {83/**84* Test: FileStore.equals() should not be case sensitive85*/86FileStore upper = Files.getFileStore(Path.of("C:\\"));87FileStore lower = Files.getFileStore(Path.of("c:\\"));88assertTrue(lower.equals(upper));89}9091/**92* Test: File and FileStore attributes93*/94assertTrue(store1.supportsFileAttributeView("basic"));95assertTrue(store1.supportsFileAttributeView(BasicFileAttributeView.class));96assertTrue(store1.supportsFileAttributeView("posix") ==97store1.supportsFileAttributeView(PosixFileAttributeView.class));98assertTrue(store1.supportsFileAttributeView("dos") ==99store1.supportsFileAttributeView(DosFileAttributeView.class));100assertTrue(store1.supportsFileAttributeView("acl") ==101store1.supportsFileAttributeView(AclFileAttributeView.class));102assertTrue(store1.supportsFileAttributeView("user") ==103store1.supportsFileAttributeView(UserDefinedFileAttributeView.class));104105/**106* Test: Space atributes107*/108File f = file1.toFile();109long total = f.getTotalSpace();110long free = f.getFreeSpace();111long usable = f.getUsableSpace();112113// check values are "close"114checkWithin1GB(total, store1.getTotalSpace());115checkWithin1GB(free, store1.getUnallocatedSpace());116checkWithin1GB(usable, store1.getUsableSpace());117118// get values by name119checkWithin1GB(total, (Long)store1.getAttribute("totalSpace"));120checkWithin1GB(free, (Long)store1.getAttribute("unallocatedSpace"));121checkWithin1GB(usable, (Long)store1.getAttribute("usableSpace"));122123/**124* Test: Enumerate all FileStores125*/126if (FileUtils.areMountPointsAccessibleAndUnique()) {127FileStore prev = null;128for (FileStore store: FileSystems.getDefault().getFileStores()) {129System.out.format("%s (name=%s type=%s)\n", store, store.name(),130store.type());131132// check space attributes are accessible133try {134store.getTotalSpace();135store.getUnallocatedSpace();136store.getUsableSpace();137} catch (NoSuchFileException nsfe) {138// ignore exception as the store could have been139// deleted since the iterator was instantiated140System.err.format("%s was not found\n", store);141} catch (AccessDeniedException ade) {142// ignore exception as the lack of ability to access the143// store due to lack of file permission or similar does not144// reflect whether the space attributes would be accessible145// were access to be permitted146System.err.format("%s is inaccessible\n", store);147}148149// two distinct FileStores should not be equal150assertTrue(!store.equals(prev));151prev = store;152}153} else {154System.err.println155("Skipping FileStore check due to file system access failure");156}157}158}159160161