From 2a7ddf1fdeb724bb5f4bd6cd64867894aa485add Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Sautter?= Date: Thu, 20 Jun 2024 19:41:56 +0200 Subject: [PATCH] [java] use java.nio to check files #14088 --- .../selenium/chromium/ChromiumOptions.java | 2 +- .../org/openqa/selenium/internal/Require.java | 71 ++++++++++++++++++- .../selenium/remote/service/DriverFinder.java | 6 +- .../selenium/javascript/TestFileLocator.java | 2 +- 4 files changed, 75 insertions(+), 6 deletions(-) diff --git a/java/src/org/openqa/selenium/chromium/ChromiumOptions.java b/java/src/org/openqa/selenium/chromium/ChromiumOptions.java index 019e8c301cc33..2af0a72c93bf8 100644 --- a/java/src/org/openqa/selenium/chromium/ChromiumOptions.java +++ b/java/src/org/openqa/selenium/chromium/ChromiumOptions.java @@ -158,7 +158,7 @@ public T addExtensions(File... paths) { * @param paths Paths to the extensions to install. */ public T addExtensions(List paths) { - paths.forEach(path -> Require.argument("Extension", path).isFile()); + paths.forEach(path -> Require.argument("Extension", path.toPath()).isFile()); extensionFiles.addAll(paths); return (T) this; } diff --git a/java/src/org/openqa/selenium/internal/Require.java b/java/src/org/openqa/selenium/internal/Require.java index 24fcd9311334b..14ed1600004f7 100644 --- a/java/src/org/openqa/selenium/internal/Require.java +++ b/java/src/org/openqa/selenium/internal/Require.java @@ -164,10 +164,15 @@ public static IntChecker argument(String argName, Integer number) { return new IntChecker(argName, number); } + @Deprecated(forRemoval = true) public static FileChecker argument(String argName, File file) { return new FileChecker(argName, file); } + public static PathChecker argument(String argName, Path path) { + return new PathChecker(argName, path); + } + public static void stateCondition(boolean state, String message, Object... args) { if (!state) { throw new IllegalStateException(String.format(message, args)); @@ -178,6 +183,7 @@ public static StateChecker state(String name, T state) { return new StateChecker<>(name, state); } + @Deprecated(forRemoval = true) public static FileStateChecker state(String name, File file) { return new FileStateChecker(name, file); } @@ -252,6 +258,7 @@ public int greaterThan(int max, String message) { } } + @Deprecated(forRemoval = true) public static class FileChecker { private final String argName; @@ -293,6 +300,47 @@ public File isDirectory() { } } + public static class PathChecker { + + private final String argName; + private final Path path; + + PathChecker(String argName, Path path) { + this.argName = argName; + this.path = path; + } + + public Path isFile() { + if (path == null) { + throw new IllegalArgumentException(String.format(MUST_BE_SET, argName)); + } + if (!Files.exists(path)) { + throw new IllegalArgumentException( + String.format(MUST_EXIST, argName, path.toAbsolutePath())); + } + if (!Files.isRegularFile(path)) { + throw new IllegalArgumentException( + String.format(MUST_BE_FILE, argName, path.toAbsolutePath())); + } + return path; + } + + public Path isDirectory() { + if (path == null) { + throw new IllegalArgumentException(String.format(MUST_BE_SET, argName)); + } + if (!Files.exists(path)) { + throw new IllegalArgumentException( + String.format(MUST_EXIST, argName, path.toAbsolutePath())); + } + if (!Files.isDirectory(path)) { + throw new IllegalArgumentException( + String.format(MUST_BE_DIR, argName, path.toAbsolutePath())); + } + return path; + } + } + public static class StateChecker { private final String name; @@ -328,6 +376,7 @@ public T instanceOf(Class cls) { } } + @Deprecated(forRemoval = true) public static class FileStateChecker { private final String name; @@ -365,7 +414,12 @@ public File isDirectory() { } public File isExecutable() { - isFile(); + if (file == null) { + throw new IllegalStateException(String.format(MUST_BE_SET, name)); + } + if (!file.exists()) { + throw new IllegalStateException(String.format(MUST_EXIST, name, file.getAbsolutePath())); + } if (!file.canExecute()) { throw new IllegalStateException( String.format(MUST_BE_EXECUTABLE, name, file.getAbsolutePath())); @@ -409,5 +463,20 @@ public Path isDirectory() { } return path; } + + public Path isExecutable() { + if (path == null) { + throw new IllegalStateException(String.format(MUST_BE_SET, name)); + } + if (!Files.exists(path)) { + throw new IllegalStateException(String.format(MUST_EXIST, name, path)); + } + // do not check for isRegularFile here, there are executable none regular files e.g. Windows + // app execution aliases + if (!Files.isExecutable(path)) { + throw new IllegalStateException(String.format(MUST_BE_EXECUTABLE, name, path)); + } + return path; + } } } diff --git a/java/src/org/openqa/selenium/remote/service/DriverFinder.java b/java/src/org/openqa/selenium/remote/service/DriverFinder.java index 35d20505a07a9..de9a911b983c7 100644 --- a/java/src/org/openqa/selenium/remote/service/DriverFinder.java +++ b/java/src/org/openqa/selenium/remote/service/DriverFinder.java @@ -17,7 +17,7 @@ package org.openqa.selenium.remote.service; -import java.io.File; +import java.nio.file.Path; import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -100,7 +100,7 @@ private Result getBinaryPaths() { if (result.getDriverPath() == null) { List arguments = toArguments(); result = seleniumManager.getBinaryPaths(arguments); - Require.state(options.getBrowserName(), new File(result.getBrowserPath())) + Require.state(options.getBrowserName(), Path.of(result.getBrowserPath())) .isExecutable(); } else { LOG.fine( @@ -115,7 +115,7 @@ private Result getBinaryPaths() { driverName, result.getDriverPath())); } - Require.state(driverName, new File(result.getDriverPath())).isExecutable(); + Require.state(driverName, Path.of(result.getDriverPath())).isExecutable(); } catch (RuntimeException e) { throw new NoSuchDriverException( String.format( diff --git a/java/test/org/openqa/selenium/javascript/TestFileLocator.java b/java/test/org/openqa/selenium/javascript/TestFileLocator.java index 3d66472c52927..0c4e0fd035871 100644 --- a/java/test/org/openqa/selenium/javascript/TestFileLocator.java +++ b/java/test/org/openqa/selenium/javascript/TestFileLocator.java @@ -78,7 +78,7 @@ private static Path getTestDirectory() { testDir = InProject.locate(testDirName); } - Require.state("Test directory", testDir.toFile()).isDirectory(); + Require.state("Test directory", testDir).isDirectory(); return testDir; }