Skip to content

Commit 66e96fd

Browse files
iampopovichpujaganidiemol
authored
[java] for loop enhance and using of standard java 11 method writeString 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]>
1 parent 7d5a9e4 commit 66e96fd

File tree

8 files changed

+14
-20
lines changed

8 files changed

+14
-20
lines changed

java/src/org/openqa/selenium/grid/jmx/MBean.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -248,9 +248,9 @@ public AttributeList getAttributes(String[] attributes) {
248248
// if attributeNames is empty, return an empty result list
249249
if (attributes == null || attributes.length == 0) return resultList;
250250

251-
for (int i = 0; i < attributes.length; i++) {
252-
Object value = getAttribute(attributes[i]);
253-
resultList.add(new Attribute(attributes[i], value));
251+
for (String attribute : attributes) {
252+
Object value = getAttribute(attribute);
253+
resultList.add(new Attribute(attribute, value));
254254
}
255255

256256
return resultList;

java/src/org/openqa/selenium/grid/node/docker/DockerSessionFactory.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -454,9 +454,8 @@ private void saveSessionCapabilities(Capabilities sessionRequestCapabilities, St
454454
String capsToJson = new Json().toJson(sessionRequestCapabilities);
455455
try {
456456
Files.createDirectories(Paths.get(path));
457-
Files.write(
458-
Paths.get(path, "sessionCapabilities.json"),
459-
capsToJson.getBytes(Charset.defaultCharset()));
457+
Files.writeString(
458+
Paths.get(path, "sessionCapabilities.json"), capsToJson, Charset.defaultCharset());
460459
} catch (IOException e) {
461460
LOG.log(Level.WARNING, "Failed to save session capabilities", e);
462461
}

java/test/org/openqa/selenium/UploadTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import java.io.File;
3030
import java.io.IOException;
3131
import java.io.UncheckedIOException;
32-
import java.nio.charset.StandardCharsets;
3332
import java.nio.file.Files;
3433
import java.util.Arrays;
3534
import java.util.List;
@@ -179,7 +178,7 @@ private File createTmpFile(String content) {
179178
try {
180179
File f = File.createTempFile("webdriver", "tmp");
181180
f.deleteOnExit();
182-
Files.write(f.toPath(), content.getBytes(StandardCharsets.UTF_8));
181+
Files.writeString(f.toPath(), content);
183182
return f;
184183
} catch (IOException e) {
185184
throw new UncheckedIOException(e);

java/test/org/openqa/selenium/edge/EdgeOptionsTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ private void checkCommonStructure(EdgeOptions options) {
242242
private File createTempFile(Path tmpDir, String content) {
243243
try {
244244
Path file = Files.createTempFile(tmpDir, "tmp", "ext");
245-
Files.write(file, content.getBytes(Charset.defaultCharset()));
245+
Files.writeString(file, content, Charset.defaultCharset());
246246
return file.toFile();
247247
} catch (IOException e) {
248248
throw new UncheckedIOException(e);

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

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

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

20-
import static java.nio.charset.StandardCharsets.UTF_8;
2120
import static org.junit.jupiter.api.Assertions.assertEquals;
2221
import static org.junit.jupiter.api.Assertions.assertTrue;
2322
import static org.openqa.selenium.remote.http.Contents.string;
@@ -135,7 +134,7 @@ void uploadsFile() throws Throwable {
135134
String FILE_CONTENTS = "Uploaded file";
136135
File testFile = File.createTempFile("webdriver", "tmp");
137136
testFile.deleteOnExit();
138-
Files.write(testFile.toPath(), FILE_CONTENTS.getBytes(UTF_8));
137+
Files.writeString(testFile.toPath(), FILE_CONTENTS);
139138

140139
driver.get(server.whereIs("upload.html"));
141140
driver.findElement(By.id("upload")).sendKeys(testFile.getAbsolutePath());

java/test/org/openqa/selenium/grid/node/NodeTest.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
import java.io.UncheckedIOException;
3737
import java.net.URI;
3838
import java.net.URISyntaxException;
39-
import java.nio.charset.StandardCharsets;
4039
import java.nio.file.Files;
4140
import java.nio.file.Path;
4241
import java.time.Clock;
@@ -917,7 +916,7 @@ private File createFile(String content, File directory) {
917916
try {
918917
File f = new File(directory.getAbsolutePath(), UUID.randomUUID().toString());
919918
f.deleteOnExit();
920-
Files.write(directory.toPath(), content.getBytes(StandardCharsets.UTF_8));
919+
Files.writeString(directory.toPath(), content);
921920
return f;
922921
} catch (IOException e) {
923922
throw new RuntimeException(e);
@@ -928,7 +927,7 @@ private File createTmpFile(String content) {
928927
try {
929928
File f = File.createTempFile("webdriver", "tmp");
930929
f.deleteOnExit();
931-
Files.write(f.toPath(), content.getBytes(StandardCharsets.UTF_8));
930+
Files.writeString(f.toPath(), content);
932931
return f;
933932
} catch (IOException e) {
934933
throw new UncheckedIOException(e);

java/test/org/openqa/selenium/grid/web/ResourceHandlerTest.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
import static java.net.HttpURLConnection.HTTP_MOVED_TEMP;
2121
import static java.net.HttpURLConnection.HTTP_OK;
22-
import static java.nio.charset.StandardCharsets.UTF_8;
2322
import static org.assertj.core.api.Assertions.assertThat;
2423
import static org.openqa.selenium.remote.http.HttpMethod.GET;
2524

@@ -48,7 +47,7 @@ public void getPath() throws IOException {
4847

4948
@Test
5049
void shouldLoadContent() throws IOException {
51-
Files.write(base.resolve("content.txt"), "I like cheese".getBytes(UTF_8));
50+
Files.writeString(base.resolve("content.txt"), "I like cheese");
5251

5352
HttpHandler handler = new ResourceHandler(new PathResource(base));
5453
HttpResponse res = handler.execute(new HttpRequest(GET, "/content.txt"));
@@ -90,7 +89,7 @@ void canBeNestedWithinARoute() throws IOException {
9089
Path contents = base.resolve("cheese").resolve("cake.txt");
9190

9291
Files.createDirectories(contents.getParent());
93-
Files.write(contents, "delicious".getBytes(UTF_8));
92+
Files.writeString(contents, "delicious");
9493

9594
HttpHandler handler =
9695
Route.prefix("/peas").to(Route.combine(new ResourceHandler(new PathResource(base))));
@@ -109,7 +108,7 @@ void canBeNestedWithinARoute() throws IOException {
109108
@Test
110109
void shouldRedirectToIndexPageIfOneExists() throws IOException {
111110
Path index = base.resolve("index.html");
112-
Files.write(index, "Cheese".getBytes(UTF_8));
111+
Files.writeString(index, "Cheese");
113112

114113
ResourceHandler handler = new ResourceHandler(new PathResource(base));
115114
HttpResponse res = handler.execute(new HttpRequest(GET, "/"));

java/test/org/openqa/selenium/testing/TestUtilities.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import java.io.File;
2121
import java.io.IOException;
2222
import java.io.UncheckedIOException;
23-
import java.nio.charset.StandardCharsets;
2423
import java.nio.file.Files;
2524
import java.util.Map;
2625
import java.util.regex.Matcher;
@@ -175,7 +174,7 @@ public static File createTmpFile(String content) {
175174
try {
176175
File f = File.createTempFile("webdriver", "tmp");
177176
f.deleteOnExit();
178-
Files.write(f.toPath(), content.getBytes(StandardCharsets.UTF_8));
177+
Files.writeString(f.toPath(), content);
179178
return f;
180179
} catch (IOException e) {
181180
throw new UncheckedIOException(e);

0 commit comments

Comments
 (0)