Skip to content

Commit 2a7ddf1

Browse files
committed
[java] use java.nio to check files #14088
1 parent 00c4f60 commit 2a7ddf1

File tree

4 files changed

+75
-6
lines changed

4 files changed

+75
-6
lines changed

java/src/org/openqa/selenium/chromium/ChromiumOptions.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ public T addExtensions(File... paths) {
158158
* @param paths Paths to the extensions to install.
159159
*/
160160
public T addExtensions(List<File> paths) {
161-
paths.forEach(path -> Require.argument("Extension", path).isFile());
161+
paths.forEach(path -> Require.argument("Extension", path.toPath()).isFile());
162162
extensionFiles.addAll(paths);
163163
return (T) this;
164164
}

java/src/org/openqa/selenium/internal/Require.java

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,10 +164,15 @@ public static IntChecker argument(String argName, Integer number) {
164164
return new IntChecker(argName, number);
165165
}
166166

167+
@Deprecated(forRemoval = true)
167168
public static FileChecker argument(String argName, File file) {
168169
return new FileChecker(argName, file);
169170
}
170171

172+
public static PathChecker argument(String argName, Path path) {
173+
return new PathChecker(argName, path);
174+
}
175+
171176
public static void stateCondition(boolean state, String message, Object... args) {
172177
if (!state) {
173178
throw new IllegalStateException(String.format(message, args));
@@ -178,6 +183,7 @@ public static <T> StateChecker<T> state(String name, T state) {
178183
return new StateChecker<>(name, state);
179184
}
180185

186+
@Deprecated(forRemoval = true)
181187
public static FileStateChecker state(String name, File file) {
182188
return new FileStateChecker(name, file);
183189
}
@@ -252,6 +258,7 @@ public int greaterThan(int max, String message) {
252258
}
253259
}
254260

261+
@Deprecated(forRemoval = true)
255262
public static class FileChecker {
256263

257264
private final String argName;
@@ -293,6 +300,47 @@ public File isDirectory() {
293300
}
294301
}
295302

303+
public static class PathChecker {
304+
305+
private final String argName;
306+
private final Path path;
307+
308+
PathChecker(String argName, Path path) {
309+
this.argName = argName;
310+
this.path = path;
311+
}
312+
313+
public Path isFile() {
314+
if (path == null) {
315+
throw new IllegalArgumentException(String.format(MUST_BE_SET, argName));
316+
}
317+
if (!Files.exists(path)) {
318+
throw new IllegalArgumentException(
319+
String.format(MUST_EXIST, argName, path.toAbsolutePath()));
320+
}
321+
if (!Files.isRegularFile(path)) {
322+
throw new IllegalArgumentException(
323+
String.format(MUST_BE_FILE, argName, path.toAbsolutePath()));
324+
}
325+
return path;
326+
}
327+
328+
public Path isDirectory() {
329+
if (path == null) {
330+
throw new IllegalArgumentException(String.format(MUST_BE_SET, argName));
331+
}
332+
if (!Files.exists(path)) {
333+
throw new IllegalArgumentException(
334+
String.format(MUST_EXIST, argName, path.toAbsolutePath()));
335+
}
336+
if (!Files.isDirectory(path)) {
337+
throw new IllegalArgumentException(
338+
String.format(MUST_BE_DIR, argName, path.toAbsolutePath()));
339+
}
340+
return path;
341+
}
342+
}
343+
296344
public static class StateChecker<T> {
297345

298346
private final String name;
@@ -328,6 +376,7 @@ public T instanceOf(Class<?> cls) {
328376
}
329377
}
330378

379+
@Deprecated(forRemoval = true)
331380
public static class FileStateChecker {
332381

333382
private final String name;
@@ -365,7 +414,12 @@ public File isDirectory() {
365414
}
366415

367416
public File isExecutable() {
368-
isFile();
417+
if (file == null) {
418+
throw new IllegalStateException(String.format(MUST_BE_SET, name));
419+
}
420+
if (!file.exists()) {
421+
throw new IllegalStateException(String.format(MUST_EXIST, name, file.getAbsolutePath()));
422+
}
369423
if (!file.canExecute()) {
370424
throw new IllegalStateException(
371425
String.format(MUST_BE_EXECUTABLE, name, file.getAbsolutePath()));
@@ -409,5 +463,20 @@ public Path isDirectory() {
409463
}
410464
return path;
411465
}
466+
467+
public Path isExecutable() {
468+
if (path == null) {
469+
throw new IllegalStateException(String.format(MUST_BE_SET, name));
470+
}
471+
if (!Files.exists(path)) {
472+
throw new IllegalStateException(String.format(MUST_EXIST, name, path));
473+
}
474+
// do not check for isRegularFile here, there are executable none regular files e.g. Windows
475+
// app execution aliases
476+
if (!Files.isExecutable(path)) {
477+
throw new IllegalStateException(String.format(MUST_BE_EXECUTABLE, name, path));
478+
}
479+
return path;
480+
}
412481
}
413482
}

java/src/org/openqa/selenium/remote/service/DriverFinder.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
package org.openqa.selenium.remote.service;
1919

20-
import java.io.File;
20+
import java.nio.file.Path;
2121
import java.util.ArrayList;
2222
import java.util.Arrays;
2323
import java.util.List;
@@ -100,7 +100,7 @@ private Result getBinaryPaths() {
100100
if (result.getDriverPath() == null) {
101101
List<String> arguments = toArguments();
102102
result = seleniumManager.getBinaryPaths(arguments);
103-
Require.state(options.getBrowserName(), new File(result.getBrowserPath()))
103+
Require.state(options.getBrowserName(), Path.of(result.getBrowserPath()))
104104
.isExecutable();
105105
} else {
106106
LOG.fine(
@@ -115,7 +115,7 @@ private Result getBinaryPaths() {
115115
driverName, result.getDriverPath()));
116116
}
117117

118-
Require.state(driverName, new File(result.getDriverPath())).isExecutable();
118+
Require.state(driverName, Path.of(result.getDriverPath())).isExecutable();
119119
} catch (RuntimeException e) {
120120
throw new NoSuchDriverException(
121121
String.format(

java/test/org/openqa/selenium/javascript/TestFileLocator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ private static Path getTestDirectory() {
7878
testDir = InProject.locate(testDirName);
7979
}
8080

81-
Require.state("Test directory", testDir.toFile()).isDirectory();
81+
Require.state("Test directory", testDir).isDirectory();
8282

8383
return testDir;
8484
}

0 commit comments

Comments
 (0)