Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/nio/file/FileStore/Basic.java
38828 views
/*1* Copyright (c) 2008, 2019, 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 702051725* @summary Unit test for java.nio.file.FileStore26* @key intermittent27* @library .. /lib/testlibrary28* @build jdk.testlibrary.FileUtils29* @run main Basic30*/3132import java.nio.file.*;33import java.nio.file.attribute.*;34import java.io.File;35import java.io.IOException;36import java.util.*;37import jdk.testlibrary.FileUtils;3839public class Basic {4041static final long G = 1024L * 1024L * 1024L;4243public static void main(String[] args) throws IOException {44Path dir = TestUtil.createTemporaryDirectory();45try {46doTests(dir);47} finally {48TestUtil.removeAll(dir);49}50}5152static void assertTrue(boolean okay) {53if (!okay)54throw new RuntimeException("Assertion failed");55}5657static void checkWithin1GB(long value1, long value2) {58long diff = Math.abs(value1 - value2);59if (diff > G)60throw new RuntimeException("values differ by more than 1GB");61}6263static void doTests(Path dir) throws IOException {64/**65* Test: Directory should be on FileStore that is writable66*/67assertTrue(!Files.getFileStore(dir).isReadOnly());6869/**70* Test: Two files should have the same FileStore71*/72Path file1 = Files.createFile(dir.resolve("foo"));73Path file2 = Files.createFile(dir.resolve("bar"));74FileStore store1 = Files.getFileStore(file1);75FileStore store2 = Files.getFileStore(file2);76assertTrue(store1.equals(store2));77assertTrue(store2.equals(store1));78assertTrue(store1.hashCode() == store2.hashCode());7980/**81* Test: File and FileStore attributes82*/83assertTrue(store1.supportsFileAttributeView("basic"));84assertTrue(store1.supportsFileAttributeView(BasicFileAttributeView.class));85assertTrue(store1.supportsFileAttributeView("posix") ==86store1.supportsFileAttributeView(PosixFileAttributeView.class));87assertTrue(store1.supportsFileAttributeView("dos") ==88store1.supportsFileAttributeView(DosFileAttributeView.class));89assertTrue(store1.supportsFileAttributeView("acl") ==90store1.supportsFileAttributeView(AclFileAttributeView.class));91assertTrue(store1.supportsFileAttributeView("user") ==92store1.supportsFileAttributeView(UserDefinedFileAttributeView.class));9394/**95* Test: Space atributes96*/97File f = file1.toFile();98long total = f.getTotalSpace();99long free = f.getFreeSpace();100long usable = f.getUsableSpace();101102// check values are "close"103checkWithin1GB(total, store1.getTotalSpace());104checkWithin1GB(free, store1.getUnallocatedSpace());105checkWithin1GB(usable, store1.getUsableSpace());106107// get values by name108checkWithin1GB(total, (Long)store1.getAttribute("totalSpace"));109checkWithin1GB(free, (Long)store1.getAttribute("unallocatedSpace"));110checkWithin1GB(usable, (Long)store1.getAttribute("usableSpace"));111112/**113* Test: Enumerate all FileStores114*/115if (FileUtils.areFileSystemsAccessible()) {116FileStore prev = null;117for (FileStore store: FileSystems.getDefault().getFileStores()) {118System.out.format("%s (name=%s type=%s)\n", store, store.name(),119store.type());120121// check space attributes are accessible122try {123store.getTotalSpace();124store.getUnallocatedSpace();125store.getUsableSpace();126} catch (NoSuchFileException nsfe) {127// ignore exception as the store could have been128// deleted since the iterator was instantiated129System.err.format("%s was not found\n", store);130} catch (AccessDeniedException ade) {131// ignore exception as the lack of ability to access the132// store due to lack of file permission or similar does not133// reflect whether the space attributes would be accessible134// were access to be permitted135System.err.format("%s is inaccessible\n", store);136}137138// two distinct FileStores should not be equal139assertTrue(!store.equals(prev));140prev = store;141}142} else {143System.err.println144("Skipping FileStore check due to file system access failure");145}146}147}148149150