8327002: (fs) java/nio/file/Files/CopyMoveVariations.java should be able to test across file systems

Reviewed-by: alanb
This commit is contained in:
Brian Burkhalter 2024-04-02 17:13:04 +00:00
parent 6ae1cf12ce
commit dd5d7d0770

View File

@ -34,6 +34,7 @@ import java.nio.file.AccessDeniedException;
import java.nio.file.CopyOption; import java.nio.file.CopyOption;
import java.nio.file.FileAlreadyExistsException; import java.nio.file.FileAlreadyExistsException;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.FileStore;
import java.nio.file.LinkOption; import java.nio.file.LinkOption;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.attribute.PosixFilePermission; import java.nio.file.attribute.PosixFilePermission;
@ -68,19 +69,21 @@ public class CopyMoveVariations {
} }
private static final boolean SUPPORTS_POSIX_PERMISSIONS; private static final boolean SUPPORTS_POSIX_PERMISSIONS;
private static final String TMP_DIR =
System.getProperty("copymove.tmp.dir", ".");
static { static {
Path currentDir = null; Path tempFile = null;
try { try {
currentDir = Files.createTempFile(Path.of("."), "this", "that"); tempFile = Files.createTempFile(Path.of(TMP_DIR), "this", "that");
SUPPORTS_POSIX_PERMISSIONS = SUPPORTS_POSIX_PERMISSIONS =
Files.getFileStore(currentDir).supportsFileAttributeView("posix"); Files.getFileStore(tempFile).supportsFileAttributeView("posix");
} catch (IOException cause) { } catch (IOException cause) {
throw new UncheckedIOException(cause); throw new UncheckedIOException(cause);
} finally { } finally {
if (currentDir != null) { if (tempFile != null) {
try { try {
Files.delete(currentDir); Files.delete(tempFile);
} catch (IOException ignore) { } catch (IOException ignore) {
} }
} }
@ -91,6 +94,13 @@ public class CopyMoveVariations {
return SUPPORTS_POSIX_PERMISSIONS; return SUPPORTS_POSIX_PERMISSIONS;
} }
private static boolean isSameFileStore(Path p1, Path p2)
throws IOException {
FileStore fs1 = p1.getFileSystem().provider().getFileStore(p1);
FileStore fs2 = p2.getFileSystem().provider().getFileStore(p2);
return fs1.equals(fs2);
}
private static Stream<Arguments> params() { private static Stream<Arguments> params() {
List<Arguments> list = new ArrayList<Arguments>(); List<Arguments> list = new ArrayList<Arguments>();
@ -142,15 +152,15 @@ public class CopyMoveVariations {
Path source = null; Path source = null;
Path target = null; Path target = null;
Path linkTarget = null; Path linkTarget = null;
Path currentDir = Path.of("."); Path tmpDir = Path.of(TMP_DIR);
try { try {
switch (type) { switch (type) {
case FILE -> case FILE ->
source = Files.createTempFile(currentDir, "file", "dat"); source = Files.createTempFile(tmpDir, "file", "dat");
case DIR -> case DIR ->
source = Files.createTempDirectory(currentDir, "dir"); source = Files.createTempDirectory(tmpDir, "dir");
case LINK -> { case LINK -> {
linkTarget = Files.createTempFile(currentDir, "link", "target"); linkTarget = Files.createTempFile(tmpDir, "link", "target");
Path link = Path.of("link"); Path link = Path.of("link");
source = Files.createSymbolicLink(link, linkTarget); source = Files.createSymbolicLink(link, linkTarget);
} }
@ -164,7 +174,7 @@ public class CopyMoveVariations {
Files.setPosixFilePermissions(source, perms); Files.setPosixFilePermissions(source, perms);
if (targetExists) if (targetExists)
target = Files.createTempFile(currentDir, "file", "target"); target = Files.createTempFile(tmpDir, "file", "target");
else else
target = Path.of("target"); target = Path.of("target");
@ -193,7 +203,11 @@ public class CopyMoveVariations {
assertThrows(FileAlreadyExistsException.class, assertThrows(FileAlreadyExistsException.class,
() -> Files.move(src, dst, options)); () -> Files.move(src, dst, options));
} else { } else {
Files.move(source, target, options); try {
Files.move(source, target, options);
} catch (AccessDeniedException ade) {
assertTrue(mode.charAt(0) != 'r');
}
assert Files.exists(target); assert Files.exists(target);
} }
} else if (type == PathType.DIR) { } else if (type == PathType.DIR) {
@ -213,7 +227,20 @@ public class CopyMoveVariations {
Files.move(source, target, options); Files.move(source, target, options);
assert Files.exists(target); assert Files.exists(target);
} catch (AccessDeniedException ade) { } catch (AccessDeniedException ade) {
assertTrue(mode.charAt(1) != 'w'); Path other = target.getParent();
if (other == null)
other = Path.of(System.getProperty("user.dir"));
if (isSameFileStore(source, other)) {
// directories on same store should be renamed
assertTrue(mode.charAt(1) != 'w');
} else {
// directories on different stores will likely be
// moved by a copy which requires read permission
if (mode.charAt(1) == 'w')
assertTrue(mode.charAt(0) != 'r');
else
assertTrue(mode.charAt(1) != 'w');
}
} catch (FileAlreadyExistsException faee) { } catch (FileAlreadyExistsException faee) {
assertTrue(targetExists && !replaceExisting); assertTrue(targetExists && !replaceExisting);
} }