Skip to content

Commit

Permalink
[java] use java.nio to check files #14088
Browse files Browse the repository at this point in the history
  • Loading branch information
joerg1985 committed Jun 20, 2024
1 parent 00c4f60 commit 2a7ddf1
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 6 deletions.
2 changes: 1 addition & 1 deletion java/src/org/openqa/selenium/chromium/ChromiumOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ public T addExtensions(File... paths) {
* @param paths Paths to the extensions to install.
*/
public T addExtensions(List<File> paths) {
paths.forEach(path -> Require.argument("Extension", path).isFile());
paths.forEach(path -> Require.argument("Extension", path.toPath()).isFile());
extensionFiles.addAll(paths);
return (T) this;
}
Expand Down
71 changes: 70 additions & 1 deletion java/src/org/openqa/selenium/internal/Require.java
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -178,6 +183,7 @@ public static <T> StateChecker<T> 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);
}
Expand Down Expand Up @@ -252,6 +258,7 @@ public int greaterThan(int max, String message) {
}
}

@Deprecated(forRemoval = true)
public static class FileChecker {

private final String argName;
Expand Down Expand Up @@ -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<T> {

private final String name;
Expand Down Expand Up @@ -328,6 +376,7 @@ public T instanceOf(Class<?> cls) {
}
}

@Deprecated(forRemoval = true)
public static class FileStateChecker {

private final String name;
Expand Down Expand Up @@ -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()));
Expand Down Expand Up @@ -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;
}
}
}
6 changes: 3 additions & 3 deletions java/src/org/openqa/selenium/remote/service/DriverFinder.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -100,7 +100,7 @@ private Result getBinaryPaths() {
if (result.getDriverPath() == null) {
List<String> 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(
Expand All @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit 2a7ddf1

Please sign in to comment.