2009-02-15 12:25:54 +00:00
|
|
|
/*
|
2021-04-09 15:41:52 +00:00
|
|
|
* Copyright (c) 2008, 2021, Oracle and/or its affiliates. All rights reserved.
|
2009-02-15 12:25:54 +00:00
|
|
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
|
|
*
|
|
|
|
* This code is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License version 2 only, as
|
|
|
|
* published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
* version 2 for more details (a copy is included in the LICENSE file that
|
|
|
|
* accompanied this code).
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License version
|
|
|
|
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
|
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*
|
2010-05-25 22:58:33 +00:00
|
|
|
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
|
|
* or visit www.oracle.com if you need additional information or have any
|
|
|
|
* questions.
|
2009-02-15 12:25:54 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* @test
|
2021-04-09 15:41:52 +00:00
|
|
|
* @bug 4313887 6873621 6979526 7006126 7020517 8264400
|
2009-02-15 12:25:54 +00:00
|
|
|
* @summary Unit test for java.nio.file.FileStore
|
2015-03-20 01:38:26 +00:00
|
|
|
* @key intermittent
|
2017-05-30 22:05:33 +00:00
|
|
|
* @library .. /test/lib
|
2017-06-12 19:43:26 +00:00
|
|
|
* @build jdk.test.lib.Platform
|
|
|
|
* jdk.test.lib.util.FileUtils
|
2017-03-09 16:58:58 +00:00
|
|
|
* @run main Basic
|
2009-02-15 12:25:54 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
import java.nio.file.*;
|
|
|
|
import java.nio.file.attribute.*;
|
2011-01-28 09:28:43 +00:00
|
|
|
import java.io.File;
|
2009-02-15 12:25:54 +00:00
|
|
|
import java.io.IOException;
|
2017-05-30 22:05:33 +00:00
|
|
|
|
2021-04-09 15:41:52 +00:00
|
|
|
import jdk.test.lib.Platform;
|
2017-05-30 22:05:33 +00:00
|
|
|
import jdk.test.lib.util.FileUtils;
|
2017-03-09 16:58:58 +00:00
|
|
|
|
2009-02-15 12:25:54 +00:00
|
|
|
public class Basic {
|
|
|
|
|
2011-01-28 09:28:43 +00:00
|
|
|
static final long G = 1024L * 1024L * 1024L;
|
|
|
|
|
2009-02-15 12:25:54 +00:00
|
|
|
public static void main(String[] args) throws IOException {
|
|
|
|
Path dir = TestUtil.createTemporaryDirectory();
|
|
|
|
try {
|
|
|
|
doTests(dir);
|
|
|
|
} finally {
|
|
|
|
TestUtil.removeAll(dir);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void assertTrue(boolean okay) {
|
|
|
|
if (!okay)
|
|
|
|
throw new RuntimeException("Assertion failed");
|
|
|
|
}
|
|
|
|
|
2011-01-28 09:28:43 +00:00
|
|
|
static void checkWithin1GB(long value1, long value2) {
|
|
|
|
long diff = Math.abs(value1 - value2);
|
|
|
|
if (diff > G)
|
|
|
|
throw new RuntimeException("values differ by more than 1GB");
|
|
|
|
}
|
|
|
|
|
2009-02-15 12:25:54 +00:00
|
|
|
static void doTests(Path dir) throws IOException {
|
|
|
|
/**
|
|
|
|
* Test: Directory should be on FileStore that is writable
|
|
|
|
*/
|
2011-01-28 09:28:43 +00:00
|
|
|
assertTrue(!Files.getFileStore(dir).isReadOnly());
|
2009-02-15 12:25:54 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test: Two files should have the same FileStore
|
|
|
|
*/
|
2011-01-28 09:28:43 +00:00
|
|
|
Path file1 = Files.createFile(dir.resolve("foo"));
|
|
|
|
Path file2 = Files.createFile(dir.resolve("bar"));
|
|
|
|
FileStore store1 = Files.getFileStore(file1);
|
|
|
|
FileStore store2 = Files.getFileStore(file2);
|
2009-02-15 12:25:54 +00:00
|
|
|
assertTrue(store1.equals(store2));
|
|
|
|
assertTrue(store2.equals(store1));
|
|
|
|
assertTrue(store1.hashCode() == store2.hashCode());
|
|
|
|
|
2021-04-09 15:41:52 +00:00
|
|
|
if (Platform.isWindows()) {
|
|
|
|
/**
|
|
|
|
* Test: FileStore.equals() should not be case sensitive
|
|
|
|
*/
|
|
|
|
FileStore upper = Files.getFileStore(Path.of("C:\\"));
|
|
|
|
FileStore lower = Files.getFileStore(Path.of("c:\\"));
|
|
|
|
assertTrue(lower.equals(upper));
|
|
|
|
}
|
|
|
|
|
2009-02-15 12:25:54 +00:00
|
|
|
/**
|
|
|
|
* Test: File and FileStore attributes
|
|
|
|
*/
|
|
|
|
assertTrue(store1.supportsFileAttributeView("basic"));
|
2009-09-04 17:15:50 +00:00
|
|
|
assertTrue(store1.supportsFileAttributeView(BasicFileAttributeView.class));
|
|
|
|
assertTrue(store1.supportsFileAttributeView("posix") ==
|
|
|
|
store1.supportsFileAttributeView(PosixFileAttributeView.class));
|
|
|
|
assertTrue(store1.supportsFileAttributeView("dos") ==
|
|
|
|
store1.supportsFileAttributeView(DosFileAttributeView.class));
|
|
|
|
assertTrue(store1.supportsFileAttributeView("acl") ==
|
|
|
|
store1.supportsFileAttributeView(AclFileAttributeView.class));
|
|
|
|
assertTrue(store1.supportsFileAttributeView("user") ==
|
|
|
|
store1.supportsFileAttributeView(UserDefinedFileAttributeView.class));
|
2009-02-15 12:25:54 +00:00
|
|
|
|
2011-01-28 09:28:43 +00:00
|
|
|
/**
|
|
|
|
* Test: Space atributes
|
|
|
|
*/
|
|
|
|
File f = file1.toFile();
|
|
|
|
long total = f.getTotalSpace();
|
|
|
|
long free = f.getFreeSpace();
|
|
|
|
long usable = f.getUsableSpace();
|
|
|
|
|
|
|
|
// check values are "close"
|
|
|
|
checkWithin1GB(total, store1.getTotalSpace());
|
|
|
|
checkWithin1GB(free, store1.getUnallocatedSpace());
|
|
|
|
checkWithin1GB(usable, store1.getUsableSpace());
|
|
|
|
|
|
|
|
// get values by name
|
|
|
|
checkWithin1GB(total, (Long)store1.getAttribute("totalSpace"));
|
|
|
|
checkWithin1GB(free, (Long)store1.getAttribute("unallocatedSpace"));
|
|
|
|
checkWithin1GB(usable, (Long)store1.getAttribute("usableSpace"));
|
|
|
|
|
2009-02-15 12:25:54 +00:00
|
|
|
/**
|
|
|
|
* Test: Enumerate all FileStores
|
|
|
|
*/
|
2020-01-23 18:19:32 +00:00
|
|
|
if (FileUtils.areMountPointsAccessibleAndUnique()) {
|
2017-03-09 16:58:58 +00:00
|
|
|
FileStore prev = null;
|
|
|
|
for (FileStore store: FileSystems.getDefault().getFileStores()) {
|
|
|
|
System.out.format("%s (name=%s type=%s)\n", store, store.name(),
|
|
|
|
store.type());
|
|
|
|
|
|
|
|
// check space attributes are accessible
|
|
|
|
try {
|
|
|
|
store.getTotalSpace();
|
|
|
|
store.getUnallocatedSpace();
|
|
|
|
store.getUsableSpace();
|
|
|
|
} catch (NoSuchFileException nsfe) {
|
|
|
|
// ignore exception as the store could have been
|
|
|
|
// deleted since the iterator was instantiated
|
|
|
|
System.err.format("%s was not found\n", store);
|
|
|
|
} catch (AccessDeniedException ade) {
|
|
|
|
// ignore exception as the lack of ability to access the
|
|
|
|
// store due to lack of file permission or similar does not
|
|
|
|
// reflect whether the space attributes would be accessible
|
|
|
|
// were access to be permitted
|
|
|
|
System.err.format("%s is inaccessible\n", store);
|
|
|
|
}
|
|
|
|
|
|
|
|
// two distinct FileStores should not be equal
|
|
|
|
assertTrue(!store.equals(prev));
|
|
|
|
prev = store;
|
2015-07-31 14:15:59 +00:00
|
|
|
}
|
2017-03-09 16:58:58 +00:00
|
|
|
} else {
|
|
|
|
System.err.println
|
|
|
|
("Skipping FileStore check due to file system access failure");
|
2009-02-15 12:25:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|