From ce57cef3ed5105deb2a29551564474d87be05afd Mon Sep 17 00:00:00 2001 From: SendaoYan Date: Fri, 13 Feb 2026 08:09:40 +0000 Subject: [PATCH] 8371979: Convert java/nio/file/FileStore/Basic.java to JUnit Reviewed-by: alanb, bpb --- test/jdk/java/nio/file/FileStore/Basic.java | 217 ++++++++++++-------- 1 file changed, 126 insertions(+), 91 deletions(-) diff --git a/test/jdk/java/nio/file/FileStore/Basic.java b/test/jdk/java/nio/file/FileStore/Basic.java index 052348c1f02..14450aa525b 100644 --- a/test/jdk/java/nio/file/FileStore/Basic.java +++ b/test/jdk/java/nio/file/FileStore/Basic.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2008, 2025, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2008, 2026, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -28,39 +28,53 @@ * @library .. /test/lib * @build jdk.test.lib.Platform * jdk.test.lib.util.FileUtils - * @run main Basic + * @run junit Basic */ -import java.nio.file.*; -import java.nio.file.attribute.*; import java.io.File; import java.io.IOException; +import java.nio.file.AccessDeniedException; +import java.nio.file.FileStore; +import java.nio.file.FileSystemException; +import java.nio.file.FileSystems; +import java.nio.file.Files; +import java.nio.file.NoSuchFileException; +import java.nio.file.Path; +import java.nio.file.attribute.AclFileAttributeView; +import java.nio.file.attribute.BasicFileAttributeView; +import java.nio.file.attribute.DosFileAttributeView; +import java.nio.file.attribute.FileAttributeView; +import java.nio.file.attribute.PosixFileAttributeView; +import java.nio.file.attribute.UserDefinedFileAttributeView; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import java.util.stream.StreamSupport; + +import org.junit.jupiter.api.condition.EnabledOnOs; +import org.junit.jupiter.api.condition.OS; +import org.junit.jupiter.api.io.TempDir; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; +import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assumptions.assumeTrue; import jdk.test.lib.Platform; import jdk.test.lib.util.FileUtils; -import static jdk.test.lib.Asserts.*; public class Basic { static final long G = 1024L * 1024L * 1024L; - public static void main(String[] args) throws IOException { - Path dir = TestUtil.createTemporaryDirectory(); - try { - doTests(dir); - } finally { - TestUtil.removeAll(dir); - } + static Path[] factory(@TempDir Path tempDir) { + return new Path[] { tempDir }; } static void checkWithin1GB(String space, long expected, long actual) { long diff = Math.abs(actual - expected); - if (diff > G) { - String msg = String.format("%s: |actual %d - expected %d| = %d (%f G)", + assertTrue(diff <= G, () -> String.format("%s: |actual %d - expected %d| = %d (%f G)", space, actual, expected, diff, - (float)diff/G); - throw new RuntimeException(msg); - } + (float)diff/G)); } static void testFileAttributes(Path file, @@ -68,7 +82,7 @@ public class Basic { String viewName) throws IOException { FileStore store = Files.getFileStore(file); boolean supported = store.supportsFileAttributeView(viewClass); - assertTrue(store.supportsFileAttributeView(viewName) == supported); + assertEquals(store.supportsFileAttributeView(viewName), supported); // If the file attribute view is supported by the FileStore then // Files.getFileAttributeView should return that view if (supported) { @@ -76,103 +90,124 @@ public class Basic { } } - static void doTests(Path dir) throws IOException { - /** - * Test: Directory should be on FileStore that is writable - */ - assertTrue(!Files.getFileStore(dir).isReadOnly()); + /* + * Test: Directory should be on FileStore that is writable + */ + @ParameterizedTest + @MethodSource("factory") + void testDirectoryWritable(Path dir) throws IOException { + assertFalse(Files.getFileStore(dir).isReadOnly()); + } - /** - * Test: Two files should have the same FileStore - */ + /* + * Test: Two files should have the same FileStore + */ + @ParameterizedTest + @MethodSource("factory") + void testEquality(Path dir) throws IOException { Path file1 = Files.createFile(dir.resolve("foo")); Path file2 = Files.createFile(dir.resolve("bar")); FileStore store1 = Files.getFileStore(file1); FileStore store2 = Files.getFileStore(file2); - assertTrue(store1.equals(store2)); - assertTrue(store2.equals(store1)); - assertTrue(store1.hashCode() == store2.hashCode()); + assertEquals(store1, store2); + assertEquals(store2, store1); + assertEquals(store1.hashCode(), store2.hashCode()); + } - 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)); - } + /* + * Test: FileStore should not be case sensitive + */ + @ParameterizedTest + @MethodSource("factory") + @EnabledOnOs({OS.WINDOWS}) + void testCaseSensitivity(Path dir) throws IOException { + FileStore upper = Files.getFileStore(Path.of("C:\\")); + FileStore lower = Files.getFileStore(Path.of("c:\\")); + assertEquals(lower, upper); + } - /** - * Test: File and FileStore attributes - */ - assertTrue(store1.supportsFileAttributeView("basic")); + /* + * Test: File and FileStore attributes + */ + @ParameterizedTest + @MethodSource("factory") + void testAttributes(Path dir) throws IOException { + Path file = Files.createFile(dir.resolve("foo")); + FileStore store = Files.getFileStore(file); + assertTrue(store.supportsFileAttributeView("basic")); testFileAttributes(dir, BasicFileAttributeView.class, "basic"); testFileAttributes(dir, PosixFileAttributeView.class, "posix"); testFileAttributes(dir, DosFileAttributeView.class, "dos"); testFileAttributes(dir, AclFileAttributeView.class, "acl"); testFileAttributes(dir, UserDefinedFileAttributeView.class, "user"); + } - /** - * Test: Space atributes - */ - File f = file1.toFile(); + /* + * Test: Space attributes + */ + @ParameterizedTest + @MethodSource("factory") + void testSpaceAttributes(Path dir) throws IOException { + Path file = Files.createFile(dir.resolve("foo")); + FileStore store = Files.getFileStore(file); + File f = file.toFile(); // check values are "close" - checkWithin1GB("total", f.getTotalSpace(), store1.getTotalSpace()); - checkWithin1GB("free", f.getFreeSpace(), store1.getUnallocatedSpace()); - checkWithin1GB("usable", f.getUsableSpace(), store1.getUsableSpace()); + checkWithin1GB("total", f.getTotalSpace(), store.getTotalSpace()); + checkWithin1GB("free", f.getFreeSpace(), store.getUnallocatedSpace()); + checkWithin1GB("usable", f.getUsableSpace(), store.getUsableSpace()); // get values by name checkWithin1GB("total", f.getTotalSpace(), - (Long)store1.getAttribute("totalSpace")); + (Long)store.getAttribute("totalSpace")); checkWithin1GB("free", f.getFreeSpace(), - (Long)store1.getAttribute("unallocatedSpace")); + (Long)store.getAttribute("unallocatedSpace")); checkWithin1GB("usable", f.getUsableSpace(), - (Long)store1.getAttribute("usableSpace")); + (Long)store.getAttribute("usableSpace")); + } - /** - * Test: Enumerate all FileStores - */ - if (FileUtils.areMountPointsAccessibleAndUnique()) { - FileStore prev = null; - for (FileStore store: FileSystems.getDefault().getFileStores()) { - System.out.format("%s (name=%s type=%s)\n", store, store.name(), - store.type()); + /* + * Test: Enumerate all FileStores + */ + @ParameterizedTest + @MethodSource("factory") + void testEnumerateFileStores(Path dir) throws IOException { + assumeTrue(FileUtils.areMountPointsAccessibleAndUnique()); + List stores = StreamSupport.stream(FileSystems.getDefault() + .getFileStores().spliterator(), false) + .toList(); + Set uniqueStores = new HashSet<>(stores); + assertEquals(stores.size(), uniqueStores.size(), "FileStores should be unique"); + for (FileStore store: stores) { + System.err.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); - } catch (FileSystemException fse) { - // On Linux, ignore the FSE if the path is one of the - // /run/user/$UID mounts created by pam_systemd(8) as it - // might be mounted as a fuse.portal filesystem and - // its access attempt might fail with EPERM - if (!Platform.isLinux() || store.toString().indexOf("/run/user") == -1) { - throw new RuntimeException(fse); - } else { - System.err.format("%s error: %s\n", store, fse); - } + // 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); + } catch (FileSystemException fse) { + // On Linux, ignore the FSE if the path is one of the + // /run/user/$UID mounts created by pam_systemd(8) as it + // might be mounted as a fuse.portal filesystem and + // its access attempt might fail with EPERM + if (!Platform.isLinux() || store.toString().indexOf("/run/user") == -1) { + throw new RuntimeException(fse); + } else { + System.err.format("%s error: %s\n", store, fse); } - - // two distinct FileStores should not be equal - assertTrue(!store.equals(prev)); - prev = store; } - } else { - System.err.println - ("Skipping FileStore check due to file system access failure"); } } }