From 970557de27ba8097e11ad7d623a5bb30d28feb1b Mon Sep 17 00:00:00 2001 From: Alex Date: Sat, 4 May 2024 06:03:26 +0700 Subject: [PATCH] [Java] Java language level aids (#13834) * Improvements from Java 11 API have been incorporated into the code. * removed unnecessary values boxing * remove unnecessary values unboxing * fix for file java/test/org/openqa/selenium/environment/webserver/Utf8Handler.java format * Format script --------- Co-authored-by: Diego Molina Co-authored-by: Diego Molina --- .../openqa/selenium/ExecutingJavascriptTest.java | 2 +- java/test/org/openqa/selenium/ProxyTest.java | 6 +++--- .../environment/webserver/Utf8Handler.java | 4 +--- .../grid/distributor/AddingNodesTest.java | 8 ++++---- .../org/openqa/selenium/json/JsonOutputTest.java | 2 +- java/test/org/openqa/selenium/json/JsonTest.java | 16 ++++++++-------- .../selenium/remote/DesiredCapabilitiesTest.java | 6 +----- .../codec/w3c/W3CHttpResponseCodecTest.java | 8 ++++---- 8 files changed, 23 insertions(+), 29 deletions(-) diff --git a/java/test/org/openqa/selenium/ExecutingJavascriptTest.java b/java/test/org/openqa/selenium/ExecutingJavascriptTest.java index 22bd5a7804e19..0a80cdd803157 100644 --- a/java/test/org/openqa/selenium/ExecutingJavascriptTest.java +++ b/java/test/org/openqa/selenium/ExecutingJavascriptTest.java @@ -407,7 +407,7 @@ void testShouldBeAbleToExecuteABigChunkOfJavascriptCode() throws IOException { driver.get(pages.javascriptPage); Path jqueryFile = InProject.locate("common/src/web/js/jquery-3.5.1.min.js"); - String jquery = new String(Files.readAllBytes(jqueryFile), US_ASCII); + String jquery = Files.readString(jqueryFile, US_ASCII); assertThat(jquery.length()) .describedAs("The javascript code should be at least 50 KB.") .isGreaterThan(50000); diff --git a/java/test/org/openqa/selenium/ProxyTest.java b/java/test/org/openqa/selenium/ProxyTest.java index 6944be1f3c123..77f016e9d7314 100644 --- a/java/test/org/openqa/selenium/ProxyTest.java +++ b/java/test/org/openqa/selenium/ProxyTest.java @@ -117,7 +117,7 @@ void testManualProxy() { assertThat(proxy.getHttpProxy()).isEqualTo("http.proxy:1234"); assertThat(proxy.getSslProxy()).isEqualTo("ssl.proxy"); assertThat(proxy.getSocksProxy()).isEqualTo("socks.proxy:65555"); - assertThat(proxy.getSocksVersion()).isEqualTo(Integer.valueOf(5)); + assertThat(proxy.getSocksVersion()).isEqualTo(5); assertThat(proxy.getSocksUsername()).isEqualTo("test1"); assertThat(proxy.getSocksPassword()).isEqualTo("test2"); assertThat(proxy.getNoProxy()).isEqualTo("localhost,127.0.0.*"); @@ -184,7 +184,7 @@ void manualProxyFromMap() { assertThat(proxy.getHttpProxy()).isEqualTo("http.proxy:1234"); assertThat(proxy.getSslProxy()).isEqualTo("ssl.proxy"); assertThat(proxy.getSocksProxy()).isEqualTo("socks.proxy:65555"); - assertThat(proxy.getSocksVersion()).isEqualTo(Integer.valueOf(5)); + assertThat(proxy.getSocksVersion()).isEqualTo(5); assertThat(proxy.getSocksUsername()).isEqualTo("test1"); assertThat(proxy.getSocksPassword()).isEqualTo("test2"); assertThat(proxy.getNoProxy()).isEqualTo("localhost,127.0.0.*"); @@ -209,7 +209,7 @@ void longSocksVersionFromMap() { Proxy proxy = new Proxy(proxyData); - assertThat(proxy.getSocksVersion()).isEqualTo(Integer.valueOf(5)); + assertThat(proxy.getSocksVersion()).isEqualTo(5); } @Test diff --git a/java/test/org/openqa/selenium/environment/webserver/Utf8Handler.java b/java/test/org/openqa/selenium/environment/webserver/Utf8Handler.java index 4e0bb13dc527e..1e684874a4760 100644 --- a/java/test/org/openqa/selenium/environment/webserver/Utf8Handler.java +++ b/java/test/org/openqa/selenium/environment/webserver/Utf8Handler.java @@ -17,8 +17,6 @@ package org.openqa.selenium.environment.webserver; -import static java.nio.charset.StandardCharsets.UTF_8; - import java.io.IOException; import java.io.UncheckedIOException; import java.nio.file.Files; @@ -51,7 +49,7 @@ public HttpResponse execute(HttpRequest req) throws UncheckedIOException { return new HttpResponse() .setHeader("Content-Type", "text/html; charset=UTF-8") - .setContent(Contents.utf8String(new String(Files.readAllBytes(target), UTF_8))); + .setContent(Contents.utf8String(Files.readString(target))); } catch (IOException e) { throw new UncheckedIOException(e); } diff --git a/java/test/org/openqa/selenium/grid/distributor/AddingNodesTest.java b/java/test/org/openqa/selenium/grid/distributor/AddingNodesTest.java index 2e1b8755eabc8..482f577d05d22 100644 --- a/java/test/org/openqa/selenium/grid/distributor/AddingNodesTest.java +++ b/java/test/org/openqa/selenium/grid/distributor/AddingNodesTest.java @@ -153,7 +153,7 @@ void shouldBeAbleToRegisterALocalNode() throws URISyntaxException { wait.until(obj -> distributor.getStatus().hasCapacity()); NodeStatus status = getOnlyElement(distributor.getStatus().getNodes()); - assertEquals(1, getStereotypes(status).get(CAPS).intValue()); + assertEquals(1, getStereotypes(status).get(CAPS)); } @Test @@ -192,7 +192,7 @@ void shouldBeAbleToRegisterACustomNode() throws URISyntaxException { wait.until(obj -> distributor.getStatus().hasCapacity()); NodeStatus status = getOnlyElement(distributor.getStatus().getNodes()); - assertEquals(1, getStereotypes(status).get(CAPS).intValue()); + assertEquals(1, getStereotypes(status).get(CAPS)); } } @@ -231,7 +231,7 @@ void shouldBeAbleToRegisterNodesByListeningForEvents() throws URISyntaxException wait.until(obj -> distributor.getStatus().hasCapacity()); NodeStatus status = getOnlyElement(distributor.getStatus().getNodes()); - assertEquals(1, getStereotypes(status).get(CAPS).intValue()); + assertEquals(1, getStereotypes(status).get(CAPS)); } } @@ -323,7 +323,7 @@ void distributorShouldUpdateStateOfExistingNodeWhenNodePublishesStateChange() wait.until(obj -> distributor.getStatus().hasCapacity()); NodeStatus nodeStatus = getOnlyElement(distributor.getStatus().getNodes()); - assertEquals(1, getStereotypes(nodeStatus).get(CAPS).intValue()); + assertEquals(1, getStereotypes(nodeStatus).get(CAPS)); // Craft a status that makes it look like the node is busy, and post it on the bus. NodeStatus status = node.getStatus(); diff --git a/java/test/org/openqa/selenium/json/JsonOutputTest.java b/java/test/org/openqa/selenium/json/JsonOutputTest.java index f85897e35b981..10d246d431b8f 100644 --- a/java/test/org/openqa/selenium/json/JsonOutputTest.java +++ b/java/test/org/openqa/selenium/json/JsonOutputTest.java @@ -419,7 +419,7 @@ void shouldConvertUnhandledAlertException() { @Test void shouldConvertDatesToMillisecondsInUtcTime() { String jsonStr = convert(new Date(0)); - assertThat(valueOf(jsonStr).intValue()).isZero(); + assertThat(valueOf(jsonStr)).isZero(); } @Test diff --git a/java/test/org/openqa/selenium/json/JsonTest.java b/java/test/org/openqa/selenium/json/JsonTest.java index 23e484e237f1d..ad5e0c0ef1007 100644 --- a/java/test/org/openqa/selenium/json/JsonTest.java +++ b/java/test/org/openqa/selenium/json/JsonTest.java @@ -61,9 +61,9 @@ void canReadBooleans() { @Test void canReadANumber() { - assertThat((Number) new Json().toType("42", Number.class)).isEqualTo(Long.valueOf(42)); - assertThat((Integer) new Json().toType("42", Integer.class)).isEqualTo(Integer.valueOf(42)); - assertThat((Double) new Json().toType("42", Double.class)).isEqualTo(Double.valueOf(42)); + assertThat((Number) new Json().toType("42", Number.class)).isEqualTo(42L); + assertThat((Integer) new Json().toType("42", Integer.class)).isEqualTo(42); + assertThat((Double) new Json().toType("42", Double.class)).isEqualTo(42.0); } @Test @@ -285,7 +285,7 @@ void canHandleValueBeingAnArray() { assertThat(response.getSessionId()).isEqualTo("bar"); assertThat(((List) converted.getValue())).hasSize(2); - assertThat(response.getStatus().intValue()).isEqualTo(1512); + assertThat(response.getStatus()).isEqualTo(1512); } @Test @@ -426,7 +426,7 @@ void decodingResponseWithNumbersInValueObject() { void shouldRecognizeNumericStatus() { Response response = new Json().toType("{\"status\":0,\"value\":\"cheese\"}", Response.class); - assertThat(response.getStatus().intValue()).isZero(); + assertThat(response.getStatus()).isZero(); assertThat(response.getState()).isEqualTo(new ErrorCodes().toState(0)); String value = (String) response.getValue(); assertThat(value).isEqualTo("cheese"); @@ -437,7 +437,7 @@ void shouldRecognizeStringStatus() { Response response = new Json().toType("{\"status\":\"success\",\"value\":\"cheese\"}", Response.class); - assertThat(response.getStatus().intValue()).isZero(); + assertThat(response.getStatus()).isZero(); assertThat(response.getState()).isEqualTo(new ErrorCodes().toState(0)); String value = (String) response.getValue(); assertThat(value).isEqualTo("cheese"); @@ -450,7 +450,7 @@ void shouldConvertInvalidSelectorError() { .toType( "{\"state\":\"invalid selector\",\"message\":\"invalid xpath selector\"}", Response.class); - assertThat(response.getStatus().intValue()).isEqualTo(32); + assertThat(response.getStatus()).isEqualTo(32); assertThat(response.getState()).isEqualTo(new ErrorCodes().toState(32)); } @@ -459,7 +459,7 @@ void shouldRecognizeStringState() { Response response = new Json().toType("{\"state\":\"success\",\"value\":\"cheese\"}", Response.class); assertThat(response.getState()).isEqualTo("success"); - assertThat(response.getStatus().intValue()).isZero(); + assertThat(response.getStatus()).isZero(); String value = (String) response.getValue(); assertThat(value).isEqualTo("cheese"); } diff --git a/java/test/org/openqa/selenium/remote/DesiredCapabilitiesTest.java b/java/test/org/openqa/selenium/remote/DesiredCapabilitiesTest.java index 856f273356468..ba401b0a9af5e 100644 --- a/java/test/org/openqa/selenium/remote/DesiredCapabilitiesTest.java +++ b/java/test/org/openqa/selenium/remote/DesiredCapabilitiesTest.java @@ -133,10 +133,6 @@ void canCompareCapabilities() { } private String createString(int length) { - StringBuilder outputBuffer = new StringBuilder(length); - for (int i = 0; i < length; i++) { - outputBuffer.append("x"); - } - return outputBuffer.toString(); + return "x".repeat(Math.max(0, length)); } } diff --git a/java/test/org/openqa/selenium/remote/codec/w3c/W3CHttpResponseCodecTest.java b/java/test/org/openqa/selenium/remote/codec/w3c/W3CHttpResponseCodecTest.java index 11c4007194978..df480c356a99c 100644 --- a/java/test/org/openqa/selenium/remote/codec/w3c/W3CHttpResponseCodecTest.java +++ b/java/test/org/openqa/selenium/remote/codec/w3c/W3CHttpResponseCodecTest.java @@ -78,7 +78,7 @@ void shouldBeAbleToHandleGatewayTimeoutError() { Response decoded = new W3CHttpResponseCodec().decode(response); - assertThat(decoded.getStatus().intValue()).isEqualTo(ErrorCodes.UNHANDLED_ERROR); + assertThat(decoded.getStatus()).isEqualTo(ErrorCodes.UNHANDLED_ERROR); assertThat(decoded.getValue()).isEqualTo(responseString); } @@ -104,7 +104,7 @@ void shouldBeAbleToHandleBadGatewayError() { Response decoded = new W3CHttpResponseCodec().decode(response); - assertThat(decoded.getStatus().intValue()).isEqualTo(ErrorCodes.UNHANDLED_ERROR); + assertThat(decoded.getStatus()).isEqualTo(ErrorCodes.UNHANDLED_ERROR); assertThat(decoded.getValue()).isEqualTo(responseString); } @@ -120,7 +120,7 @@ void decodingAnErrorWithoutAStacktraceIsDecodedProperlyForNonCompliantImplementa Response decoded = new W3CHttpResponseCodec().decode(response); assertThat(decoded.getState()).isEqualTo("unsupported operation"); - assertThat(decoded.getStatus().intValue()).isEqualTo(METHOD_NOT_ALLOWED); + assertThat(decoded.getStatus()).isEqualTo(METHOD_NOT_ALLOWED); assertThat(decoded.getValue()).isInstanceOf(UnsupportedCommandException.class); assertThat(((WebDriverException) decoded.getValue()).getMessage()).contains("I like peas"); @@ -140,7 +140,7 @@ void decodingAnErrorWithoutAStacktraceIsDecodedProperlyForConformingImplementati Response decoded = new W3CHttpResponseCodec().decode(response); assertThat(decoded.getState()).isEqualTo("unsupported operation"); - assertThat(decoded.getStatus().intValue()).isEqualTo(METHOD_NOT_ALLOWED); + assertThat(decoded.getStatus()).isEqualTo(METHOD_NOT_ALLOWED); assertThat(decoded.getValue()).isInstanceOf(UnsupportedCommandException.class); assertThat(((WebDriverException) decoded.getValue()).getMessage()).contains("I like peas");