Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions java/src/org/openqa/selenium/events/Event.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.Objects;
import java.util.StringJoiner;
import java.util.UUID;
import org.jspecify.annotations.Nullable;
import org.openqa.selenium.internal.Require;
import org.openqa.selenium.json.Json;
import org.openqa.selenium.json.JsonOutput;
Expand All @@ -31,11 +32,11 @@ public class Event {
private final EventName eventName;
private final String data;

public Event(EventName eventName, Object data) {
public Event(EventName eventName, @Nullable Object data) {
this(UUID.randomUUID(), eventName, data);
}

public Event(UUID id, EventName eventName, Object data) {
public Event(UUID id, EventName eventName, @Nullable Object data) {
this.id = Require.nonNull("Message id", id);
this.eventName = Require.nonNull("Event type", eventName);

Expand Down
18 changes: 12 additions & 6 deletions java/src/org/openqa/selenium/grid/Bootstrap.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.ArrayList;
Expand Down Expand Up @@ -90,16 +93,19 @@ private static ClassLoader createExtendedClassLoader(String ext) {
continue;
}

if (file.isDirectory()) {
File[] files = file.listFiles();
if (files == null) {
LOG.warning("Cannot list files in directory: " + file);
} else {
for (File subdirFile : files) {
Path dir = file.toPath();

if (Files.isDirectory(dir)) {
try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir)) {
for (Path path : stream) {
File subdirFile = path.toFile();
if (subdirFile.isFile() && subdirFile.getName().endsWith(".jar")) {
jars.add(subdirFile);
}
}
} catch (IOException e) {
LOG.warning(
() -> String.format("Cannot list files in directory: %s due to: %s", file, e));
}
} else {
jars.add(file);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public RemoteDistributor add(Node node) {

HttpResponse response = client.with(addSecret).execute(request);

Values.get(response, Void.class);
Values.parse(response);

LOG.info(String.format("Added node %s.", node.getId()));

Expand All @@ -91,7 +91,7 @@ public boolean drain(NodeId nodeId) {

HttpResponse response = client.with(addSecret).execute(request);

return Values.get(response, Boolean.class);
return Values.parseBoolean(response);
}

@Override
Expand All @@ -102,7 +102,7 @@ public void remove(NodeId nodeId) {

HttpResponse response = client.with(addSecret).execute(request);

Values.get(response, Void.class);
Values.parse(response);
}

@Override
Expand All @@ -112,7 +112,7 @@ public DistributorStatus getStatus() {

HttpResponse response = client.execute(request);

return Values.get(response, DistributorStatus.class);
return Values.parse(response, DistributorStatus.class);
}

@Override
Expand All @@ -125,7 +125,7 @@ public Either<SessionNotCreatedException, CreateSessionResponse> newSession(
HttpResponse res = client.execute(req);

if (res.isSuccessful()) {
return Either.right(Values.get(res, CreateSessionResponse.class));
return Either.right(Values.parse(res, CreateSessionResponse.class));
} else {
return Either.left(Values.get(res, SessionNotCreatedException.class));
}
Expand Down
10 changes: 5 additions & 5 deletions java/src/org/openqa/selenium/grid/node/remote/RemoteNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ public boolean isSessionOwner(SessionId id) {

HttpResponse res = client.with(addSecret).execute(req);

return Boolean.TRUE.equals(Values.get(res, Boolean.class));
return Values.parseBoolean(res);
}

@Override
Expand All @@ -219,7 +219,7 @@ public boolean tryAcquireConnection(SessionId id) {

HttpResponse res = client.with(addSecret).execute(req);

return Boolean.TRUE.equals(Values.get(res, Boolean.class));
return Values.parseBoolean(res);
}

@Override
Expand All @@ -231,7 +231,7 @@ public void releaseConnection(SessionId id) {

HttpResponse res = client.with(addSecret).execute(req);

Values.get(res, Void.class);
Values.parse(res);
}

@Override
Expand All @@ -243,7 +243,7 @@ public Session getSession(SessionId id) throws NoSuchSessionException {

HttpResponse res = client.with(addSecret).execute(req);

return Require.nonNull("Session", Values.get(res, Session.class));
return Values.parse(res, Session.class);
}

@Override
Expand Down Expand Up @@ -274,7 +274,7 @@ public void stop(SessionId id) throws NoSuchSessionException {

HttpResponse res = client.with(addSecret).execute(req);

Values.get(res, Void.class);
Values.parse(res);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import static org.openqa.selenium.remote.http.HttpMethod.POST;

import java.io.UncheckedIOException;
import java.lang.reflect.Type;
import java.net.MalformedURLException;
import java.net.URI;
import org.jspecify.annotations.Nullable;
Expand Down Expand Up @@ -114,7 +113,7 @@ public void remove(SessionId id) {
}

@Nullable
private <T> T makeRequest(HttpRequest request, Type typeOfT) {
private <T> T makeRequest(HttpRequest request, Class<T> typeOfT) {
HttpTracing.inject(tracer, tracer.getCurrentContext(), request);

HttpResponse response = client.execute(request);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public boolean retryAddToQueue(SessionRequest request) {
HttpTracing.inject(tracer, tracer.getCurrentContext(), upstream);
upstream.setContent(Contents.asJson(request));
HttpResponse response = client.with(addSecret).execute(upstream);
return Values.get(response, Boolean.class);
return Values.parseBoolean(response);
}

@Override
Expand Down Expand Up @@ -156,7 +156,7 @@ public List<SessionRequest> getNextAvailable(Map<Capabilities, Long> stereotypes
HttpTracing.inject(tracer, tracer.getCurrentContext(), upstream);
HttpResponse response = client.with(addSecret).execute(upstream);

return Values.get(response, SESSION_REQUEST_TYPE);
return Require.nonNull("Next session request", Values.get(response, SESSION_REQUEST_TYPE));
}

@Override
Expand All @@ -178,7 +178,7 @@ public boolean complete(

HttpTracing.inject(tracer, tracer.getCurrentContext(), upstream);
HttpResponse response = client.with(addSecret).execute(upstream);
return Values.get(response, Boolean.class);
return Values.parseBoolean(response);
}

@Override
Expand All @@ -187,7 +187,7 @@ public int clearQueue() {
HttpTracing.inject(tracer, tracer.getCurrentContext(), upstream);
HttpResponse response = client.with(addSecret).execute(upstream);

return Values.get(response, Integer.class);
return Values.parse(response, Integer.class);
}

@Override
Expand All @@ -196,7 +196,7 @@ public List<SessionRequestCapability> getQueueContents() {
HttpTracing.inject(tracer, tracer.getCurrentContext(), upstream);
HttpResponse response = client.execute(upstream);

return Values.get(response, QUEUE_CONTENTS_TYPE);
return Require.nonNull("Queue contents", Values.get(response, QUEUE_CONTENTS_TYPE));
}

@Override
Expand Down
15 changes: 15 additions & 0 deletions java/src/org/openqa/selenium/grid/web/Values.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import java.io.Reader;
import java.io.UncheckedIOException;
import java.lang.reflect.Type;
import org.jspecify.annotations.Nullable;
import org.openqa.selenium.internal.Require;
import org.openqa.selenium.json.Json;
import org.openqa.selenium.json.JsonInput;
import org.openqa.selenium.remote.ErrorCodec;
Expand All @@ -36,6 +38,19 @@ public class Values {
private static final Json JSON = new Json();
private static final ErrorCodec ERRORS = ErrorCodec.createDefault();

public static <T> T parse(HttpResponse response, Class<T> classOfT) {
return Require.nonNull(classOfT.getSimpleName(), get(response, classOfT));
}

public static void parse(HttpResponse response) {
get(response, Void.class);
}

public static boolean parseBoolean(HttpResponse response) {
return Boolean.TRUE.equals(get(response, Boolean.class));
}

@Nullable
public static <T> T get(HttpResponse response, Type typeOfT) {
try (Reader reader = reader(response);
JsonInput input = JSON.newInput(reader)) {
Expand Down
16 changes: 11 additions & 5 deletions java/src/org/openqa/selenium/io/Zip.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.FileTime;
import java.util.Base64;
import java.util.logging.Level;
Expand Down Expand Up @@ -54,12 +57,15 @@ public static String zip(File input) throws IOException {

private static void addToZip(String basePath, ZipOutputStream zos, File toAdd)
throws IOException {
if (toAdd.isDirectory()) {
File[] files = toAdd.listFiles();
if (files != null) {
for (File file : files) {
addToZip(basePath, zos, file);
Path dirPath = toAdd.toPath();

if (Files.isDirectory(dirPath)) {
try (DirectoryStream<Path> stream = Files.newDirectoryStream(dirPath)) {
for (Path path : stream) {
addToZip(basePath, zos, path.toFile());
}
} catch (IOException e) {
LOG.warning(() -> String.format("Failed to read directory %s for zipping: %s", toAdd, e));
}
} else {
try (FileInputStream fis = new FileInputStream(toAdd)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,10 @@ protected void defineCommand(String name, CommandSpec spec) {
}

private String buildUri(
String commandName, SessionId sessionId, Map<String, ?> parameters, CommandSpec spec) {
String commandName,
@Nullable SessionId sessionId,
Map<String, ?> parameters,
CommandSpec spec) {
StringBuilder builder = new StringBuilder();
for (String part : spec.pathSegments) {
if (part.isEmpty()) {
Expand All @@ -340,10 +343,12 @@ private String buildUri(
}

private String getParameter(
String parameterName, String commandName, SessionId sessionId, Map<String, ?> parameters) {
String parameterName,
String commandName,
@Nullable SessionId sessionId,
Map<String, ?> parameters) {
if ("sessionId".equals(parameterName)) {
Require.argument("Session id", sessionId)
.nonNull("Session ID may not be null for command %s", commandName);
Require.nonNull("Session id", sessionId, "may not be null for command %s", commandName);
return sessionId.toString();
}

Expand Down
7 changes: 5 additions & 2 deletions java/test/org/openqa/selenium/AlertsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatCode;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.openqa.selenium.WaitingConditions.newWindowIsOpened;
import static org.openqa.selenium.support.ui.ExpectedConditions.alertIsPresent;
import static org.openqa.selenium.support.ui.ExpectedConditions.presenceOfElementLocated;
Expand Down Expand Up @@ -117,8 +118,10 @@ void testShouldThrowIllegalArgumentExceptionWhenKeysNull() {
driver.findElement(By.id("alert")).click();
Alert alert = wait.until(alertIsPresent());

assertThatExceptionOfType(IllegalArgumentException.class)
.isThrownBy(() -> alert.sendKeys(null));
//noinspection DataFlowIssue
assertThatThrownBy(() -> alert.sendKeys(null))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("Keys to send should be a not null CharSequence");
}

@Test
Expand Down
4 changes: 2 additions & 2 deletions java/test/org/openqa/selenium/ClickScrollingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ void testShouldNotScrollOverflowElementsWhichAreVisible() {
WebElement list = driver.findElement(By.tagName("ul"));
WebElement item = list.findElement(By.id("desired"));
item.click();
long yOffset =
Long yOffset =
(Long) ((JavascriptExecutor) driver).executeScript("return arguments[0].scrollTop;", list);
assertThat(yOffset).describedAs("Should not have scrolled").isZero();
}
Expand Down Expand Up @@ -235,7 +235,7 @@ void testShouldNotScrollWhenGettingElementSize() {
assertThat(getScrollTop()).isEqualTo(scrollTop);
}

private long getScrollTop() {
private Long getScrollTop() {
wait.until(presenceOfElementLocated(By.tagName("body")));
return (Long) ((JavascriptExecutor) driver).executeScript("return document.body.scrollTop;");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import org.openqa.selenium.chromium.HasCdp;
import org.openqa.selenium.chromium.HasNetworkConditions;
import org.openqa.selenium.chromium.HasPermissions;
import org.openqa.selenium.internal.Require;
import org.openqa.selenium.net.PortProber;
import org.openqa.selenium.remote.RemoteWebDriverBuilder;
import org.openqa.selenium.remote.http.ClientConfig;
Expand Down Expand Up @@ -126,7 +127,7 @@ public String checkPermission(WebDriver driver, String permission) {
+ "name: arguments[0]"
+ "}));",
permission);
return result.get("state").toString();
return Require.nonNull("Result", result).get("state").toString();
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package org.openqa.selenium.environment.webserver;

import static java.util.Objects.requireNonNull;
import static java.util.stream.StreamSupport.stream;
import static org.assertj.core.api.Assertions.assertThat;
import static org.openqa.selenium.By.id;
Expand Down Expand Up @@ -46,17 +47,19 @@
public abstract class AppServerTestBase {
private static final String APPCACHE_MIME_TYPE = "text/cache-manifest";
private AppServer server;
private static @Nullable WebDriver driver = null;
private static @Nullable WebDriver cachedDriver = null;
private WebDriver driver;

@BeforeAll
public static void startDriver() {
driver = new WebDriverBuilder().get();
cachedDriver = new WebDriverBuilder().get();
}

@BeforeEach
public void startServer() {
server = createAppServer();
server.start();
driver = requireNonNull(cachedDriver, "Driver is not initialized");
}

protected abstract AppServer createAppServer();
Expand All @@ -68,9 +71,9 @@ public void stopServer() {

@AfterAll
public static void quitDriver() {
if (driver != null) {
driver.quit();
driver = null;
if (cachedDriver != null) {
cachedDriver.quit();
cachedDriver = null;
}
}

Expand Down
Loading
Loading