Skip to content

Commit 970557d

Browse files
iampopovichdiemol
andauthored
[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 <[email protected]> Co-authored-by: Diego Molina <[email protected]>
1 parent 5fe3362 commit 970557d

File tree

8 files changed

+23
-29
lines changed

8 files changed

+23
-29
lines changed

java/test/org/openqa/selenium/ExecutingJavascriptTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ void testShouldBeAbleToExecuteABigChunkOfJavascriptCode() throws IOException {
407407
driver.get(pages.javascriptPage);
408408

409409
Path jqueryFile = InProject.locate("common/src/web/js/jquery-3.5.1.min.js");
410-
String jquery = new String(Files.readAllBytes(jqueryFile), US_ASCII);
410+
String jquery = Files.readString(jqueryFile, US_ASCII);
411411
assertThat(jquery.length())
412412
.describedAs("The javascript code should be at least 50 KB.")
413413
.isGreaterThan(50000);

java/test/org/openqa/selenium/ProxyTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ void testManualProxy() {
117117
assertThat(proxy.getHttpProxy()).isEqualTo("http.proxy:1234");
118118
assertThat(proxy.getSslProxy()).isEqualTo("ssl.proxy");
119119
assertThat(proxy.getSocksProxy()).isEqualTo("socks.proxy:65555");
120-
assertThat(proxy.getSocksVersion()).isEqualTo(Integer.valueOf(5));
120+
assertThat(proxy.getSocksVersion()).isEqualTo(5);
121121
assertThat(proxy.getSocksUsername()).isEqualTo("test1");
122122
assertThat(proxy.getSocksPassword()).isEqualTo("test2");
123123
assertThat(proxy.getNoProxy()).isEqualTo("localhost,127.0.0.*");
@@ -184,7 +184,7 @@ void manualProxyFromMap() {
184184
assertThat(proxy.getHttpProxy()).isEqualTo("http.proxy:1234");
185185
assertThat(proxy.getSslProxy()).isEqualTo("ssl.proxy");
186186
assertThat(proxy.getSocksProxy()).isEqualTo("socks.proxy:65555");
187-
assertThat(proxy.getSocksVersion()).isEqualTo(Integer.valueOf(5));
187+
assertThat(proxy.getSocksVersion()).isEqualTo(5);
188188
assertThat(proxy.getSocksUsername()).isEqualTo("test1");
189189
assertThat(proxy.getSocksPassword()).isEqualTo("test2");
190190
assertThat(proxy.getNoProxy()).isEqualTo("localhost,127.0.0.*");
@@ -209,7 +209,7 @@ void longSocksVersionFromMap() {
209209

210210
Proxy proxy = new Proxy(proxyData);
211211

212-
assertThat(proxy.getSocksVersion()).isEqualTo(Integer.valueOf(5));
212+
assertThat(proxy.getSocksVersion()).isEqualTo(5);
213213
}
214214

215215
@Test

java/test/org/openqa/selenium/environment/webserver/Utf8Handler.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717

1818
package org.openqa.selenium.environment.webserver;
1919

20-
import static java.nio.charset.StandardCharsets.UTF_8;
21-
2220
import java.io.IOException;
2321
import java.io.UncheckedIOException;
2422
import java.nio.file.Files;
@@ -51,7 +49,7 @@ public HttpResponse execute(HttpRequest req) throws UncheckedIOException {
5149

5250
return new HttpResponse()
5351
.setHeader("Content-Type", "text/html; charset=UTF-8")
54-
.setContent(Contents.utf8String(new String(Files.readAllBytes(target), UTF_8)));
52+
.setContent(Contents.utf8String(Files.readString(target)));
5553
} catch (IOException e) {
5654
throw new UncheckedIOException(e);
5755
}

java/test/org/openqa/selenium/grid/distributor/AddingNodesTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ void shouldBeAbleToRegisterALocalNode() throws URISyntaxException {
153153
wait.until(obj -> distributor.getStatus().hasCapacity());
154154

155155
NodeStatus status = getOnlyElement(distributor.getStatus().getNodes());
156-
assertEquals(1, getStereotypes(status).get(CAPS).intValue());
156+
assertEquals(1, getStereotypes(status).get(CAPS));
157157
}
158158

159159
@Test
@@ -192,7 +192,7 @@ void shouldBeAbleToRegisterACustomNode() throws URISyntaxException {
192192
wait.until(obj -> distributor.getStatus().hasCapacity());
193193

194194
NodeStatus status = getOnlyElement(distributor.getStatus().getNodes());
195-
assertEquals(1, getStereotypes(status).get(CAPS).intValue());
195+
assertEquals(1, getStereotypes(status).get(CAPS));
196196
}
197197
}
198198

@@ -231,7 +231,7 @@ void shouldBeAbleToRegisterNodesByListeningForEvents() throws URISyntaxException
231231
wait.until(obj -> distributor.getStatus().hasCapacity());
232232

233233
NodeStatus status = getOnlyElement(distributor.getStatus().getNodes());
234-
assertEquals(1, getStereotypes(status).get(CAPS).intValue());
234+
assertEquals(1, getStereotypes(status).get(CAPS));
235235
}
236236
}
237237

@@ -323,7 +323,7 @@ void distributorShouldUpdateStateOfExistingNodeWhenNodePublishesStateChange()
323323
wait.until(obj -> distributor.getStatus().hasCapacity());
324324

325325
NodeStatus nodeStatus = getOnlyElement(distributor.getStatus().getNodes());
326-
assertEquals(1, getStereotypes(nodeStatus).get(CAPS).intValue());
326+
assertEquals(1, getStereotypes(nodeStatus).get(CAPS));
327327

328328
// Craft a status that makes it look like the node is busy, and post it on the bus.
329329
NodeStatus status = node.getStatus();

java/test/org/openqa/selenium/json/JsonOutputTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ void shouldConvertUnhandledAlertException() {
419419
@Test
420420
void shouldConvertDatesToMillisecondsInUtcTime() {
421421
String jsonStr = convert(new Date(0));
422-
assertThat(valueOf(jsonStr).intValue()).isZero();
422+
assertThat(valueOf(jsonStr)).isZero();
423423
}
424424

425425
@Test

java/test/org/openqa/selenium/json/JsonTest.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,9 @@ void canReadBooleans() {
6161

6262
@Test
6363
void canReadANumber() {
64-
assertThat((Number) new Json().toType("42", Number.class)).isEqualTo(Long.valueOf(42));
65-
assertThat((Integer) new Json().toType("42", Integer.class)).isEqualTo(Integer.valueOf(42));
66-
assertThat((Double) new Json().toType("42", Double.class)).isEqualTo(Double.valueOf(42));
64+
assertThat((Number) new Json().toType("42", Number.class)).isEqualTo(42L);
65+
assertThat((Integer) new Json().toType("42", Integer.class)).isEqualTo(42);
66+
assertThat((Double) new Json().toType("42", Double.class)).isEqualTo(42.0);
6767
}
6868

6969
@Test
@@ -285,7 +285,7 @@ void canHandleValueBeingAnArray() {
285285

286286
assertThat(response.getSessionId()).isEqualTo("bar");
287287
assertThat(((List<?>) converted.getValue())).hasSize(2);
288-
assertThat(response.getStatus().intValue()).isEqualTo(1512);
288+
assertThat(response.getStatus()).isEqualTo(1512);
289289
}
290290

291291
@Test
@@ -426,7 +426,7 @@ void decodingResponseWithNumbersInValueObject() {
426426
void shouldRecognizeNumericStatus() {
427427
Response response = new Json().toType("{\"status\":0,\"value\":\"cheese\"}", Response.class);
428428

429-
assertThat(response.getStatus().intValue()).isZero();
429+
assertThat(response.getStatus()).isZero();
430430
assertThat(response.getState()).isEqualTo(new ErrorCodes().toState(0));
431431
String value = (String) response.getValue();
432432
assertThat(value).isEqualTo("cheese");
@@ -437,7 +437,7 @@ void shouldRecognizeStringStatus() {
437437
Response response =
438438
new Json().toType("{\"status\":\"success\",\"value\":\"cheese\"}", Response.class);
439439

440-
assertThat(response.getStatus().intValue()).isZero();
440+
assertThat(response.getStatus()).isZero();
441441
assertThat(response.getState()).isEqualTo(new ErrorCodes().toState(0));
442442
String value = (String) response.getValue();
443443
assertThat(value).isEqualTo("cheese");
@@ -450,7 +450,7 @@ void shouldConvertInvalidSelectorError() {
450450
.toType(
451451
"{\"state\":\"invalid selector\",\"message\":\"invalid xpath selector\"}",
452452
Response.class);
453-
assertThat(response.getStatus().intValue()).isEqualTo(32);
453+
assertThat(response.getStatus()).isEqualTo(32);
454454
assertThat(response.getState()).isEqualTo(new ErrorCodes().toState(32));
455455
}
456456

@@ -459,7 +459,7 @@ void shouldRecognizeStringState() {
459459
Response response =
460460
new Json().toType("{\"state\":\"success\",\"value\":\"cheese\"}", Response.class);
461461
assertThat(response.getState()).isEqualTo("success");
462-
assertThat(response.getStatus().intValue()).isZero();
462+
assertThat(response.getStatus()).isZero();
463463
String value = (String) response.getValue();
464464
assertThat(value).isEqualTo("cheese");
465465
}

java/test/org/openqa/selenium/remote/DesiredCapabilitiesTest.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -133,10 +133,6 @@ void canCompareCapabilities() {
133133
}
134134

135135
private String createString(int length) {
136-
StringBuilder outputBuffer = new StringBuilder(length);
137-
for (int i = 0; i < length; i++) {
138-
outputBuffer.append("x");
139-
}
140-
return outputBuffer.toString();
136+
return "x".repeat(Math.max(0, length));
141137
}
142138
}

java/test/org/openqa/selenium/remote/codec/w3c/W3CHttpResponseCodecTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ void shouldBeAbleToHandleGatewayTimeoutError() {
7878

7979
Response decoded = new W3CHttpResponseCodec().decode(response);
8080

81-
assertThat(decoded.getStatus().intValue()).isEqualTo(ErrorCodes.UNHANDLED_ERROR);
81+
assertThat(decoded.getStatus()).isEqualTo(ErrorCodes.UNHANDLED_ERROR);
8282
assertThat(decoded.getValue()).isEqualTo(responseString);
8383
}
8484

@@ -104,7 +104,7 @@ void shouldBeAbleToHandleBadGatewayError() {
104104

105105
Response decoded = new W3CHttpResponseCodec().decode(response);
106106

107-
assertThat(decoded.getStatus().intValue()).isEqualTo(ErrorCodes.UNHANDLED_ERROR);
107+
assertThat(decoded.getStatus()).isEqualTo(ErrorCodes.UNHANDLED_ERROR);
108108
assertThat(decoded.getValue()).isEqualTo(responseString);
109109
}
110110

@@ -120,7 +120,7 @@ void decodingAnErrorWithoutAStacktraceIsDecodedProperlyForNonCompliantImplementa
120120
Response decoded = new W3CHttpResponseCodec().decode(response);
121121

122122
assertThat(decoded.getState()).isEqualTo("unsupported operation");
123-
assertThat(decoded.getStatus().intValue()).isEqualTo(METHOD_NOT_ALLOWED);
123+
assertThat(decoded.getStatus()).isEqualTo(METHOD_NOT_ALLOWED);
124124

125125
assertThat(decoded.getValue()).isInstanceOf(UnsupportedCommandException.class);
126126
assertThat(((WebDriverException) decoded.getValue()).getMessage()).contains("I like peas");
@@ -140,7 +140,7 @@ void decodingAnErrorWithoutAStacktraceIsDecodedProperlyForConformingImplementati
140140
Response decoded = new W3CHttpResponseCodec().decode(response);
141141

142142
assertThat(decoded.getState()).isEqualTo("unsupported operation");
143-
assertThat(decoded.getStatus().intValue()).isEqualTo(METHOD_NOT_ALLOWED);
143+
assertThat(decoded.getStatus()).isEqualTo(METHOD_NOT_ALLOWED);
144144

145145
assertThat(decoded.getValue()).isInstanceOf(UnsupportedCommandException.class);
146146
assertThat(((WebDriverException) decoded.getValue()).getMessage()).contains("I like peas");

0 commit comments

Comments
 (0)