From 05bb32fec88f04d320498a7370ac3a5919bad755 Mon Sep 17 00:00:00 2001 From: Alex Date: Thu, 12 Dec 2024 01:16:57 +0700 Subject: [PATCH 1/3] enhance for loop use --- java/src/org/openqa/selenium/grid/jmx/MBean.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/java/src/org/openqa/selenium/grid/jmx/MBean.java b/java/src/org/openqa/selenium/grid/jmx/MBean.java index 3bdd3111dc21f..501cd254d27d5 100644 --- a/java/src/org/openqa/selenium/grid/jmx/MBean.java +++ b/java/src/org/openqa/selenium/grid/jmx/MBean.java @@ -248,9 +248,9 @@ public AttributeList getAttributes(String[] attributes) { // if attributeNames is empty, return an empty result list if (attributes == null || attributes.length == 0) return resultList; - for (int i = 0; i < attributes.length; i++) { - Object value = getAttribute(attributes[i]); - resultList.add(new Attribute(attributes[i], value)); + for (String attribute : attributes) { + Object value = getAttribute(attribute); + resultList.add(new Attribute(attribute, value)); } return resultList; From 3f9b9e05ec6210a9f1590119ca54fd2370e2c802 Mon Sep 17 00:00:00 2001 From: Alex Date: Thu, 12 Dec 2024 01:23:42 +0700 Subject: [PATCH 2/3] method calls that read or write a String as bytes using java. nio. file. Files. Such calls can be replaced with a call to a Files. readString() or Files. writeString() --- .../selenium/grid/node/docker/DockerSessionFactory.java | 4 ++-- java/test/org/openqa/selenium/UploadTest.java | 2 +- java/test/org/openqa/selenium/edge/EdgeOptionsTest.java | 2 +- .../selenium/environment/webserver/AppServerTestBase.java | 2 +- java/test/org/openqa/selenium/grid/node/NodeTest.java | 4 ++-- .../org/openqa/selenium/grid/web/ResourceHandlerTest.java | 6 +++--- java/test/org/openqa/selenium/testing/TestUtilities.java | 2 +- 7 files changed, 11 insertions(+), 11 deletions(-) diff --git a/java/src/org/openqa/selenium/grid/node/docker/DockerSessionFactory.java b/java/src/org/openqa/selenium/grid/node/docker/DockerSessionFactory.java index 48f567f576268..bd7292166248b 100644 --- a/java/src/org/openqa/selenium/grid/node/docker/DockerSessionFactory.java +++ b/java/src/org/openqa/selenium/grid/node/docker/DockerSessionFactory.java @@ -454,9 +454,9 @@ private void saveSessionCapabilities(Capabilities sessionRequestCapabilities, St String capsToJson = new Json().toJson(sessionRequestCapabilities); try { Files.createDirectories(Paths.get(path)); - Files.write( + Files.writeString( Paths.get(path, "sessionCapabilities.json"), - capsToJson.getBytes(Charset.defaultCharset())); + capsToJson, Charset.defaultCharset()); } catch (IOException e) { LOG.log(Level.WARNING, "Failed to save session capabilities", e); } diff --git a/java/test/org/openqa/selenium/UploadTest.java b/java/test/org/openqa/selenium/UploadTest.java index ae67b01aa3613..264324c9bf04a 100644 --- a/java/test/org/openqa/selenium/UploadTest.java +++ b/java/test/org/openqa/selenium/UploadTest.java @@ -179,7 +179,7 @@ private File createTmpFile(String content) { try { File f = File.createTempFile("webdriver", "tmp"); f.deleteOnExit(); - Files.write(f.toPath(), content.getBytes(StandardCharsets.UTF_8)); + Files.writeString(f.toPath(), content); return f; } catch (IOException e) { throw new UncheckedIOException(e); diff --git a/java/test/org/openqa/selenium/edge/EdgeOptionsTest.java b/java/test/org/openqa/selenium/edge/EdgeOptionsTest.java index b0e552108c604..158eee1180a3d 100644 --- a/java/test/org/openqa/selenium/edge/EdgeOptionsTest.java +++ b/java/test/org/openqa/selenium/edge/EdgeOptionsTest.java @@ -242,7 +242,7 @@ private void checkCommonStructure(EdgeOptions options) { private File createTempFile(Path tmpDir, String content) { try { Path file = Files.createTempFile(tmpDir, "tmp", "ext"); - Files.write(file, content.getBytes(Charset.defaultCharset())); + Files.writeString(file, content, Charset.defaultCharset()); return file.toFile(); } catch (IOException e) { throw new UncheckedIOException(e); diff --git a/java/test/org/openqa/selenium/environment/webserver/AppServerTestBase.java b/java/test/org/openqa/selenium/environment/webserver/AppServerTestBase.java index 8244ff07c94b2..0e2d6a0c35bbd 100644 --- a/java/test/org/openqa/selenium/environment/webserver/AppServerTestBase.java +++ b/java/test/org/openqa/selenium/environment/webserver/AppServerTestBase.java @@ -135,7 +135,7 @@ void uploadsFile() throws Throwable { String FILE_CONTENTS = "Uploaded file"; File testFile = File.createTempFile("webdriver", "tmp"); testFile.deleteOnExit(); - Files.write(testFile.toPath(), FILE_CONTENTS.getBytes(UTF_8)); + Files.writeString(testFile.toPath(), FILE_CONTENTS); driver.get(server.whereIs("upload.html")); driver.findElement(By.id("upload")).sendKeys(testFile.getAbsolutePath()); diff --git a/java/test/org/openqa/selenium/grid/node/NodeTest.java b/java/test/org/openqa/selenium/grid/node/NodeTest.java index b9e8fcc7399de..107d94de72f9e 100644 --- a/java/test/org/openqa/selenium/grid/node/NodeTest.java +++ b/java/test/org/openqa/selenium/grid/node/NodeTest.java @@ -917,7 +917,7 @@ private File createFile(String content, File directory) { try { File f = new File(directory.getAbsolutePath(), UUID.randomUUID().toString()); f.deleteOnExit(); - Files.write(directory.toPath(), content.getBytes(StandardCharsets.UTF_8)); + Files.writeString(directory.toPath(), content); return f; } catch (IOException e) { throw new RuntimeException(e); @@ -928,7 +928,7 @@ private File createTmpFile(String content) { try { File f = File.createTempFile("webdriver", "tmp"); f.deleteOnExit(); - Files.write(f.toPath(), content.getBytes(StandardCharsets.UTF_8)); + Files.writeString(f.toPath(), content); return f; } catch (IOException e) { throw new UncheckedIOException(e); diff --git a/java/test/org/openqa/selenium/grid/web/ResourceHandlerTest.java b/java/test/org/openqa/selenium/grid/web/ResourceHandlerTest.java index 32ce041aa7435..4e9594745f4a1 100644 --- a/java/test/org/openqa/selenium/grid/web/ResourceHandlerTest.java +++ b/java/test/org/openqa/selenium/grid/web/ResourceHandlerTest.java @@ -48,7 +48,7 @@ public void getPath() throws IOException { @Test void shouldLoadContent() throws IOException { - Files.write(base.resolve("content.txt"), "I like cheese".getBytes(UTF_8)); + Files.writeString(base.resolve("content.txt"), "I like cheese"); HttpHandler handler = new ResourceHandler(new PathResource(base)); HttpResponse res = handler.execute(new HttpRequest(GET, "/content.txt")); @@ -90,7 +90,7 @@ void canBeNestedWithinARoute() throws IOException { Path contents = base.resolve("cheese").resolve("cake.txt"); Files.createDirectories(contents.getParent()); - Files.write(contents, "delicious".getBytes(UTF_8)); + Files.writeString(contents, "delicious"); HttpHandler handler = Route.prefix("/peas").to(Route.combine(new ResourceHandler(new PathResource(base)))); @@ -109,7 +109,7 @@ void canBeNestedWithinARoute() throws IOException { @Test void shouldRedirectToIndexPageIfOneExists() throws IOException { Path index = base.resolve("index.html"); - Files.write(index, "Cheese".getBytes(UTF_8)); + Files.writeString(index, "Cheese"); ResourceHandler handler = new ResourceHandler(new PathResource(base)); HttpResponse res = handler.execute(new HttpRequest(GET, "/")); diff --git a/java/test/org/openqa/selenium/testing/TestUtilities.java b/java/test/org/openqa/selenium/testing/TestUtilities.java index 3cbe9971a7526..8f13291fae6d3 100644 --- a/java/test/org/openqa/selenium/testing/TestUtilities.java +++ b/java/test/org/openqa/selenium/testing/TestUtilities.java @@ -175,7 +175,7 @@ public static File createTmpFile(String content) { try { File f = File.createTempFile("webdriver", "tmp"); f.deleteOnExit(); - Files.write(f.toPath(), content.getBytes(StandardCharsets.UTF_8)); + Files.writeString(f.toPath(), content); return f; } catch (IOException e) { throw new UncheckedIOException(e); From 4f5178245685ec6128f23a52b8712aca6743701c Mon Sep 17 00:00:00 2001 From: Alex Popov Date: Wed, 11 Dec 2024 18:41:20 +0000 Subject: [PATCH 3/3] applying format.sh --- .../openqa/selenium/grid/node/docker/DockerSessionFactory.java | 3 +-- java/test/org/openqa/selenium/UploadTest.java | 1 - .../selenium/environment/webserver/AppServerTestBase.java | 1 - java/test/org/openqa/selenium/grid/node/NodeTest.java | 1 - .../test/org/openqa/selenium/grid/web/ResourceHandlerTest.java | 1 - java/test/org/openqa/selenium/testing/TestUtilities.java | 1 - 6 files changed, 1 insertion(+), 7 deletions(-) diff --git a/java/src/org/openqa/selenium/grid/node/docker/DockerSessionFactory.java b/java/src/org/openqa/selenium/grid/node/docker/DockerSessionFactory.java index bd7292166248b..3cc1d4ab68911 100644 --- a/java/src/org/openqa/selenium/grid/node/docker/DockerSessionFactory.java +++ b/java/src/org/openqa/selenium/grid/node/docker/DockerSessionFactory.java @@ -455,8 +455,7 @@ private void saveSessionCapabilities(Capabilities sessionRequestCapabilities, St try { Files.createDirectories(Paths.get(path)); Files.writeString( - Paths.get(path, "sessionCapabilities.json"), - capsToJson, Charset.defaultCharset()); + Paths.get(path, "sessionCapabilities.json"), capsToJson, Charset.defaultCharset()); } catch (IOException e) { LOG.log(Level.WARNING, "Failed to save session capabilities", e); } diff --git a/java/test/org/openqa/selenium/UploadTest.java b/java/test/org/openqa/selenium/UploadTest.java index 264324c9bf04a..308ca7a4d1e96 100644 --- a/java/test/org/openqa/selenium/UploadTest.java +++ b/java/test/org/openqa/selenium/UploadTest.java @@ -29,7 +29,6 @@ import java.io.File; import java.io.IOException; import java.io.UncheckedIOException; -import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.util.Arrays; import java.util.List; diff --git a/java/test/org/openqa/selenium/environment/webserver/AppServerTestBase.java b/java/test/org/openqa/selenium/environment/webserver/AppServerTestBase.java index 0e2d6a0c35bbd..b174fa2b95700 100644 --- a/java/test/org/openqa/selenium/environment/webserver/AppServerTestBase.java +++ b/java/test/org/openqa/selenium/environment/webserver/AppServerTestBase.java @@ -17,7 +17,6 @@ package org.openqa.selenium.environment.webserver; -import static java.nio.charset.StandardCharsets.UTF_8; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.openqa.selenium.remote.http.Contents.string; diff --git a/java/test/org/openqa/selenium/grid/node/NodeTest.java b/java/test/org/openqa/selenium/grid/node/NodeTest.java index 107d94de72f9e..4ce9e7e019ff6 100644 --- a/java/test/org/openqa/selenium/grid/node/NodeTest.java +++ b/java/test/org/openqa/selenium/grid/node/NodeTest.java @@ -36,7 +36,6 @@ import java.io.UncheckedIOException; import java.net.URI; import java.net.URISyntaxException; -import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; import java.time.Clock; diff --git a/java/test/org/openqa/selenium/grid/web/ResourceHandlerTest.java b/java/test/org/openqa/selenium/grid/web/ResourceHandlerTest.java index 4e9594745f4a1..8c76ac80cf4dc 100644 --- a/java/test/org/openqa/selenium/grid/web/ResourceHandlerTest.java +++ b/java/test/org/openqa/selenium/grid/web/ResourceHandlerTest.java @@ -19,7 +19,6 @@ import static java.net.HttpURLConnection.HTTP_MOVED_TEMP; import static java.net.HttpURLConnection.HTTP_OK; -import static java.nio.charset.StandardCharsets.UTF_8; import static org.assertj.core.api.Assertions.assertThat; import static org.openqa.selenium.remote.http.HttpMethod.GET; diff --git a/java/test/org/openqa/selenium/testing/TestUtilities.java b/java/test/org/openqa/selenium/testing/TestUtilities.java index 8f13291fae6d3..21abfa50b8e77 100644 --- a/java/test/org/openqa/selenium/testing/TestUtilities.java +++ b/java/test/org/openqa/selenium/testing/TestUtilities.java @@ -20,7 +20,6 @@ import java.io.File; import java.io.IOException; import java.io.UncheckedIOException; -import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.util.Map; import java.util.regex.Matcher;