Skip to content

Commit 3538223

Browse files
committed
Classes in the core webdriver-api package must not depend on GSON
Or guava, or anything else. In addition, the removed fields were used for deserialising Proxy instances correctly. Added a failing, but ignored, test.
1 parent 83e1124 commit 3538223

3 files changed

Lines changed: 48 additions & 27 deletions

File tree

java/client/src/org/openqa/selenium/BUCK

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ java_library(name = 'core',
5656
]),
5757
manifest_file = ':manifest',
5858
deps = [
59+
# This list of dependencies MUST NOT include anything other than code
60+
# from the selenium project. That means no guava and no gson.
5961
':beta',
6062
':exceptions',
6163
':platform',
@@ -64,7 +66,6 @@ java_library(name = 'core',
6466
'//java/client/src/org/openqa/selenium/interactions:exceptions',
6567
'//java/client/src/org/openqa/selenium/logging:api',
6668
'//java/client/src/org/openqa/selenium/security:security',
67-
'//third_party/java/gson:gson',
6869
],
6970
visibility = [
7071
'//java/client/src/org/openqa/selenium/interactions:interactions',

java/client/src/org/openqa/selenium/Proxy.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717

1818
package org.openqa.selenium;
1919

20-
import com.google.gson.Gson;
21-
import com.google.gson.JsonElement;
2220
import java.util.HashMap;
2321
import java.util.Map;
2422

@@ -95,8 +93,9 @@ public Proxy(Map<String, ?> raw) {
9593
}
9694
}
9795

98-
public JsonElement toJson() {
99-
Map<String, String> m = new HashMap<>();
96+
public Map<String, Object> toJson() {
97+
Map<String, Object> m = new HashMap<>();
98+
10099
if (proxyType != ProxyType.UNSPECIFIED) {
101100
m.put("proxyType", proxyType.toString().toLowerCase());
102101
}
@@ -125,9 +124,9 @@ public JsonElement toJson() {
125124
m.put("proxyAutoconfigUrl", proxyAutoconfigUrl);
126125
}
127126
if (autodetect) {
128-
m.put("autodetect", "true");
127+
m.put("autodetect", true);
129128
}
130-
return new Gson().toJsonTree(m);
129+
return m;
131130
}
132131

133132
/**

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

Lines changed: 41 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,21 @@
2323
import static org.junit.Assert.assertNull;
2424
import static org.junit.Assert.assertTrue;
2525
import static org.junit.Assert.fail;
26+
import static org.openqa.selenium.remote.CapabilityType.PROXY;
2627

2728
import com.google.gson.JsonElement;
2829
import com.google.gson.JsonObject;
2930
import java.util.Set;
31+
32+
import org.junit.Ignore;
3033
import org.junit.Test;
3134
import org.junit.runner.RunWith;
3235
import org.junit.runners.JUnit4;
3336
import org.openqa.selenium.Proxy.ProxyType;
37+
import org.openqa.selenium.remote.BeanToJsonConverter;
3438
import org.openqa.selenium.remote.CapabilityType;
3539
import org.openqa.selenium.remote.DesiredCapabilities;
40+
import org.openqa.selenium.remote.JsonToBeanConverter;
3641

3742
import java.util.HashMap;
3843
import java.util.Map;
@@ -245,16 +250,16 @@ public void manualProxyToJson() {
245250
proxy.setSocksUsername("test1");
246251
proxy.setSocksPassword("test2");
247252

248-
JsonObject json = proxy.toJson().getAsJsonObject();
253+
Map<String, Object> json = proxy.toJson();
249254

250-
assertEquals("manual", json.getAsJsonPrimitive("proxyType").getAsString());
251-
assertEquals("ftp.proxy", json.getAsJsonPrimitive("ftpProxy").getAsString());
252-
assertEquals("http.proxy:1234", json.getAsJsonPrimitive("httpProxy").getAsString());
253-
assertEquals("ssl.proxy", json.getAsJsonPrimitive("sslProxy").getAsString());
254-
assertEquals("socks.proxy:65555", json.getAsJsonPrimitive("socksProxy").getAsString());
255-
assertEquals("test1", json.getAsJsonPrimitive("socksUsername").getAsString());
256-
assertEquals("test2", json.getAsJsonPrimitive("socksPassword").getAsString());
257-
assertEquals("localhost,127.0.0.*", json.getAsJsonPrimitive("noProxy").getAsString());
255+
assertEquals("manual", json.get("proxyType"));
256+
assertEquals("ftp.proxy", json.get("ftpProxy"));
257+
assertEquals("http.proxy:1234", json.get("httpProxy"));
258+
assertEquals("ssl.proxy", json.get("sslProxy"));
259+
assertEquals("socks.proxy:65555", json.get("socksProxy"));
260+
assertEquals("test1", json.get("socksUsername"));
261+
assertEquals("test2", json.get("socksPassword"));
262+
assertEquals("localhost,127.0.0.*", json.get("noProxy"));
258263
assertEquals(8, json.entrySet().size());
259264
}
260265

@@ -285,10 +290,10 @@ public void pacProxyToJson() {
285290
proxy.setProxyType(ProxyType.PAC);
286291
proxy.setProxyAutoconfigUrl("http://aaa/bbb.pac");
287292

288-
JsonObject json = proxy.toJson().getAsJsonObject();
293+
Map<String, Object> json = proxy.toJson();
289294

290-
assertEquals("pac", json.getAsJsonPrimitive("proxyType").getAsString());
291-
assertEquals("http://aaa/bbb.pac", json.getAsJsonPrimitive("proxyAutoconfigUrl").getAsString());
295+
assertEquals("pac", json.get("proxyType"));
296+
assertEquals("http://aaa/bbb.pac", json.get("proxyAutoconfigUrl"));
292297
assertEquals(2, json.entrySet().size());
293298
}
294299

@@ -319,10 +324,10 @@ public void autodetectProxyToJson() {
319324
proxy.setProxyType(ProxyType.AUTODETECT);
320325
proxy.setAutodetect(true);
321326

322-
JsonObject json = proxy.toJson().getAsJsonObject();
327+
Map<String, ?> json = proxy.toJson();
323328

324-
assertEquals("autodetect", json.getAsJsonPrimitive("proxyType").getAsString());
325-
assertTrue(json.getAsJsonPrimitive("autodetect").getAsBoolean());
329+
assertEquals("autodetect", json.get("proxyType"));
330+
assertTrue((Boolean) json.get("autodetect"));
326331
assertEquals(2, json.entrySet().size());
327332
}
328333

@@ -351,9 +356,9 @@ public void systemProxyToJson() {
351356
Proxy proxy = new Proxy();
352357
proxy.setProxyType(ProxyType.SYSTEM);
353358

354-
JsonObject json = proxy.toJson().getAsJsonObject();
359+
Map<String, Object> json = proxy.toJson();
355360

356-
assertEquals("system", json.getAsJsonPrimitive("proxyType").getAsString());
361+
assertEquals("system", json.get("proxyType"));
357362
assertEquals(1, json.entrySet().size());
358363
}
359364

@@ -382,9 +387,9 @@ public void directProxyToJson() {
382387
Proxy proxy = new Proxy();
383388
proxy.setProxyType(ProxyType.DIRECT);
384389

385-
JsonObject json = proxy.toJson().getAsJsonObject();
390+
Map<String, Object> json = proxy.toJson();
386391

387-
assertEquals("direct", json.getAsJsonPrimitive("proxyType").getAsString());
392+
assertEquals("direct", json.get("proxyType"));
388393
assertEquals(1, json.entrySet().size());
389394
}
390395

@@ -395,12 +400,28 @@ public void constructingWithNullKeysWorksAsExpected() {
395400
rawProxy.put("httpProxy", "http://www.example.com");
396401
rawProxy.put("autodetect", null);
397402
DesiredCapabilities caps = new DesiredCapabilities();
398-
caps.setCapability(CapabilityType.PROXY, rawProxy);
403+
caps.setCapability(PROXY, rawProxy);
399404

400405
Proxy proxy = Proxy.extractFrom(caps);
401406

402407
assertNull(proxy.getFtpProxy());
403408
assertFalse(proxy.isAutodetect());
404409
assertEquals("http://www.example.com", proxy.getHttpProxy());
405410
}
411+
412+
@Test
413+
@Ignore
414+
public void serialiazesAndDeserializesWithoutError() {
415+
Proxy proxy = new Proxy();
416+
proxy.setProxyAutoconfigUrl("http://www.example.com/config.pac");
417+
418+
DesiredCapabilities caps = new DesiredCapabilities();
419+
caps.setCapability(PROXY, proxy);
420+
421+
String rawJson = new BeanToJsonConverter().convert(caps);
422+
Capabilities converted = new JsonToBeanConverter().convert(Capabilities.class, rawJson);
423+
424+
Object returnedProxy = converted.getCapability(PROXY);
425+
assertTrue(returnedProxy instanceof Proxy);
426+
}
406427
}

0 commit comments

Comments
 (0)