Skip to content

Commit

Permalink
[java] for loop enhance and using of standard java 11 method writeStr…
Browse files Browse the repository at this point in the history
…ing for tests (#14889)

* enhance for loop use

* 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()

* applying format.sh

---------

Co-authored-by: Puja Jagani <[email protected]>
Co-authored-by: Diego Molina <[email protected]>
  • Loading branch information
3 people authored Dec 27, 2024
1 parent 7d5a9e4 commit 66e96fd
Show file tree
Hide file tree
Showing 8 changed files with 14 additions and 20 deletions.
6 changes: 3 additions & 3 deletions java/src/org/openqa/selenium/grid/jmx/MBean.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -454,9 +454,8 @@ private void saveSessionCapabilities(Capabilities sessionRequestCapabilities, St
String capsToJson = new Json().toJson(sessionRequestCapabilities);
try {
Files.createDirectories(Paths.get(path));
Files.write(
Paths.get(path, "sessionCapabilities.json"),
capsToJson.getBytes(Charset.defaultCharset()));
Files.writeString(
Paths.get(path, "sessionCapabilities.json"), capsToJson, Charset.defaultCharset());
} catch (IOException e) {
LOG.log(Level.WARNING, "Failed to save session capabilities", e);
}
Expand Down
3 changes: 1 addition & 2 deletions java/test/org/openqa/selenium/UploadTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -179,7 +178,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);
Expand Down
2 changes: 1 addition & 1 deletion java/test/org/openqa/selenium/edge/EdgeOptionsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -135,7 +134,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());
Expand Down
5 changes: 2 additions & 3 deletions java/test/org/openqa/selenium/grid/node/NodeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -917,7 +916,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);
Expand All @@ -928,7 +927,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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -48,7 +47,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"));
Expand Down Expand Up @@ -90,7 +89,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))));
Expand All @@ -109,7 +108,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, "/"));
Expand Down
3 changes: 1 addition & 2 deletions java/test/org/openqa/selenium/testing/TestUtilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -175,7 +174,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);
Expand Down

0 comments on commit 66e96fd

Please sign in to comment.