Skip to content

Commit f3724e6

Browse files
iampopovichdiemol
andauthored
[java] Consistent UTF-8 Encoding and Code Enhancements (#14218)
* made the use of UTF8 encoding consistent throughout * replaced for loop with enchanced for in DockerOptions * removed unnecessary unboxing in test noErrorNoCry * applied format.sh --------- Co-authored-by: Diego Molina <[email protected]>
1 parent 72e3a61 commit f3724e6

File tree

9 files changed

+29
-56
lines changed

9 files changed

+29
-56
lines changed

java/src/org/openqa/selenium/grid/distributor/local/LocalDistributor.java

+1-5
Original file line numberDiff line numberDiff line change
@@ -878,11 +878,7 @@ protected Node getNodeFromURI(URI uri) {
878878
model.getSnapshot().stream()
879879
.filter(node -> node.getExternalUri().equals(uri))
880880
.findFirst();
881-
if (nodeStatus.isPresent()) {
882-
return nodes.get(nodeStatus.get().getNodeId());
883-
} else {
884-
return null;
885-
}
881+
return nodeStatus.map(status -> nodes.get(status.getNodeId())).orElse(null);
886882
} finally {
887883
readLock.unlock();
888884
}

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -207,8 +207,8 @@ protected List<Device> getDevicesMapping() {
207207
config.getAll(DOCKER_SECTION, "devices").orElseGet(Collections::emptyList);
208208

209209
List<Device> deviceMapping = new ArrayList<>();
210-
for (int i = 0; i < devices.size(); i++) {
211-
String deviceMappingDefined = devices.get(i).trim();
210+
for (String device : devices) {
211+
String deviceMappingDefined = device.trim();
212212
Matcher matcher =
213213
linuxDeviceMappingWithDefaultPermissionsPattern.matcher(deviceMappingDefined);
214214

java/src/org/openqa/selenium/net/Urls.java

+2-6
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@
1919

2020
import java.io.IOException;
2121
import java.io.UncheckedIOException;
22-
import java.io.UnsupportedEncodingException;
2322
import java.net.MalformedURLException;
2423
import java.net.URI;
2524
import java.net.URISyntaxException;
2625
import java.net.URL;
2726
import java.net.URLEncoder;
27+
import java.nio.charset.StandardCharsets;
2828
import java.util.regex.Pattern;
2929
import org.openqa.selenium.internal.Require;
3030

@@ -42,11 +42,7 @@ private Urls() {
4242
* @see URLEncoder#encode(java.lang.String, java.lang.String)
4343
*/
4444
public static String urlEncode(String value) {
45-
try {
46-
return URLEncoder.encode(value, "UTF-8");
47-
} catch (UnsupportedEncodingException e) {
48-
throw new UncheckedIOException(e);
49-
}
45+
return URLEncoder.encode(value, StandardCharsets.UTF_8);
5046
}
5147

5248
public static URL fromUri(URI uri) {

java/src/org/openqa/selenium/remote/http/FormEncodedData.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,6 @@ private static String read(Reader reader, Charset charSet, char delimiter, Atomi
9090
builder.append(c);
9191
}
9292

93-
return URLDecoder.decode(builder.toString(), charSet.toString());
93+
return URLDecoder.decode(builder.toString(), charSet);
9494
}
9595
}

java/test/org/openqa/selenium/ReferrerTest.java

+1-6
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
import com.google.common.net.HostAndPort;
3232
import java.io.IOException;
3333
import java.io.UncheckedIOException;
34-
import java.io.UnsupportedEncodingException;
3534
import java.net.URI;
3635
import java.net.URLEncoder;
3736
import java.nio.file.Files;
@@ -169,11 +168,7 @@ void basicHistoryNavigationWithADirectProxy() {
169168
}
170169

171170
private static String encode(String url) {
172-
try {
173-
return URLEncoder.encode(url, UTF_8.name());
174-
} catch (UnsupportedEncodingException e) {
175-
throw new RuntimeException("UTF-8 should always be supported!", e);
176-
}
171+
return URLEncoder.encode(url, UTF_8);
177172
}
178173

179174
private void performNavigation(WebDriver driver, String firstUrl) {

java/test/org/openqa/selenium/docker/v1_41/ListImagesTest.java

+15-19
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
import static org.openqa.selenium.json.Json.MAP_TYPE;
2222
import static org.openqa.selenium.remote.http.Contents.utf8String;
2323

24-
import java.io.UnsupportedEncodingException;
2524
import java.net.URLDecoder;
25+
import java.nio.charset.StandardCharsets;
2626
import java.util.Map;
2727
import java.util.Set;
2828
import org.junit.jupiter.api.Test;
@@ -41,26 +41,22 @@ void shouldReturnImageIfTagIsPresent() {
4141
HttpHandler handler =
4242
req -> {
4343
String filters = req.getQueryParameter("filters");
44-
try {
45-
String decoded = URLDecoder.decode(filters, "UTF-8");
46-
Map<String, Object> raw = new Json().toType(decoded, MAP_TYPE);
44+
String decoded = URLDecoder.decode(filters, StandardCharsets.UTF_8);
45+
Map<String, Object> raw = new Json().toType(decoded, MAP_TYPE);
4746

48-
Map<?, ?> rawRef = (Map<?, ?>) raw.get("reference");
49-
assertThat(rawRef.get("selenium/standalone-firefox:latest")).isEqualTo(true);
47+
Map<?, ?> rawRef = (Map<?, ?>) raw.get("reference");
48+
assertThat(rawRef.get("selenium/standalone-firefox:latest")).isEqualTo(true);
5049

51-
return new HttpResponse()
52-
.addHeader("Content-Type", "application/json")
53-
.setContent(
54-
utf8String(
55-
"[{\"Containers\":-1,\"Created\":1581716253,"
56-
+ "\"Id\":\"sha256:bc24341497a00a3afbf04c518cb4bf98834d933ae331d1c5d3cd6f52c079049e\","
57-
+ "\"Labels\":{\"authors\":\"SeleniumHQ\"},\"ParentId\":\"\","
58-
+ "\"RepoDigests\":null,"
59-
+ "\"RepoTags\":[\"selenium/standalone-firefox:latest\"],"
60-
+ "\"SharedSize\":-1,\"Size\":765131593,\"VirtualSize\":765131593}]"));
61-
} catch (UnsupportedEncodingException ignore) {
62-
return null;
63-
}
50+
return new HttpResponse()
51+
.addHeader("Content-Type", "application/json")
52+
.setContent(
53+
utf8String(
54+
"[{\"Containers\":-1,\"Created\":1581716253,"
55+
+ "\"Id\":\"sha256:bc24341497a00a3afbf04c518cb4bf98834d933ae331d1c5d3cd6f52c079049e\","
56+
+ "\"Labels\":{\"authors\":\"SeleniumHQ\"},\"ParentId\":\"\","
57+
+ "\"RepoDigests\":null,"
58+
+ "\"RepoTags\":[\"selenium/standalone-firefox:latest\"],"
59+
+ "\"SharedSize\":-1,\"Size\":765131593,\"VirtualSize\":765131593}]"));
6460
};
6561

6662
Reference reference = Reference.parse("selenium/standalone-firefox:latest");

java/test/org/openqa/selenium/grid/MainTest.java

+1-6
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121

2222
import java.io.ByteArrayOutputStream;
2323
import java.io.PrintStream;
24-
import java.io.UnsupportedEncodingException;
2524
import java.nio.charset.StandardCharsets;
2625
import org.junit.jupiter.api.BeforeEach;
2726
import org.junit.jupiter.api.Test;
@@ -38,11 +37,7 @@ public void init() {
3837
}
3938

4039
private PrintStream toPrintStream(ByteArrayOutputStream baos) {
41-
try {
42-
return new PrintStream(baos, true, StandardCharsets.UTF_8.name());
43-
} catch (UnsupportedEncodingException e) {
44-
throw new RuntimeException(e);
45-
}
40+
return new PrintStream(baos, true, StandardCharsets.UTF_8);
4641
}
4742

4843
@Test

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ void noErrorNoCry() {
5252

5353
Response decoded = new W3CHttpResponseCodec().decode(response);
5454

55-
assertThat(decoded.getStatus().intValue()).isEqualTo(ErrorCodes.SUCCESS);
55+
assertThat(decoded.getStatus()).isEqualTo(ErrorCodes.SUCCESS);
5656
assertThat(decoded.getState()).isEqualTo("success");
5757
assertThat(decoded.getValue()).isEqualTo("cheese");
5858
}

java/test/org/openqa/selenium/remote/http/FormEncodedDataTest.java

+5-10
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727

2828
import com.google.common.collect.ImmutableMap;
2929
import com.google.common.net.MediaType;
30-
import java.io.UnsupportedEncodingException;
3130
import java.net.URLEncoder;
3231
import java.util.ArrayList;
3332
import java.util.Arrays;
@@ -133,15 +132,11 @@ private HttpRequest createRequest(String key, String value, String... others) {
133132
if (!isFirst) {
134133
content.append("&");
135134
}
136-
try {
137-
content.append(URLEncoder.encode(iterator.next(), UTF_8.toString()));
138-
139-
String next = iterator.next();
140-
if (next != null) {
141-
content.append("=").append(URLEncoder.encode(next, UTF_8.toString()));
142-
}
143-
} catch (UnsupportedEncodingException e) {
144-
fail(e.getMessage());
135+
content.append(URLEncoder.encode(iterator.next(), UTF_8));
136+
137+
String next = iterator.next();
138+
if (next != null) {
139+
content.append("=").append(URLEncoder.encode(next, UTF_8));
145140
}
146141
if (isFirst) {
147142
isFirst = false;

0 commit comments

Comments
 (0)