Skip to content

Commit

Permalink
Classes in the core webdriver-api package must not depend on GSON
Browse files Browse the repository at this point in the history
Or guava, or anything else. In addition, the removed fields
were used for deserialising Proxy instances correctly. Added
a failing, but ignored, test.
  • Loading branch information
shs96c committed Mar 29, 2017
1 parent 83e1124 commit 3538223
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 27 deletions.
3 changes: 2 additions & 1 deletion java/client/src/org/openqa/selenium/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ java_library(name = 'core',
]),
manifest_file = ':manifest',
deps = [
# This list of dependencies MUST NOT include anything other than code
# from the selenium project. That means no guava and no gson.
':beta',
':exceptions',
':platform',
Expand All @@ -64,7 +66,6 @@ java_library(name = 'core',
'//java/client/src/org/openqa/selenium/interactions:exceptions',
'//java/client/src/org/openqa/selenium/logging:api',
'//java/client/src/org/openqa/selenium/security:security',
'//third_party/java/gson:gson',
],
visibility = [
'//java/client/src/org/openqa/selenium/interactions:interactions',
Expand Down
11 changes: 5 additions & 6 deletions java/client/src/org/openqa/selenium/Proxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@

package org.openqa.selenium;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import java.util.HashMap;
import java.util.Map;

Expand Down Expand Up @@ -95,8 +93,9 @@ public Proxy(Map<String, ?> raw) {
}
}

public JsonElement toJson() {
Map<String, String> m = new HashMap<>();
public Map<String, Object> toJson() {
Map<String, Object> m = new HashMap<>();

if (proxyType != ProxyType.UNSPECIFIED) {
m.put("proxyType", proxyType.toString().toLowerCase());
}
Expand Down Expand Up @@ -125,9 +124,9 @@ public JsonElement toJson() {
m.put("proxyAutoconfigUrl", proxyAutoconfigUrl);
}
if (autodetect) {
m.put("autodetect", "true");
m.put("autodetect", true);
}
return new Gson().toJsonTree(m);
return m;
}

/**
Expand Down
61 changes: 41 additions & 20 deletions java/client/test/org/openqa/selenium/ProxyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,21 @@
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.openqa.selenium.remote.CapabilityType.PROXY;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import java.util.Set;

import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.openqa.selenium.Proxy.ProxyType;
import org.openqa.selenium.remote.BeanToJsonConverter;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.JsonToBeanConverter;

import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -245,16 +250,16 @@ public void manualProxyToJson() {
proxy.setSocksUsername("test1");
proxy.setSocksPassword("test2");

JsonObject json = proxy.toJson().getAsJsonObject();
Map<String, Object> json = proxy.toJson();

assertEquals("manual", json.getAsJsonPrimitive("proxyType").getAsString());
assertEquals("ftp.proxy", json.getAsJsonPrimitive("ftpProxy").getAsString());
assertEquals("http.proxy:1234", json.getAsJsonPrimitive("httpProxy").getAsString());
assertEquals("ssl.proxy", json.getAsJsonPrimitive("sslProxy").getAsString());
assertEquals("socks.proxy:65555", json.getAsJsonPrimitive("socksProxy").getAsString());
assertEquals("test1", json.getAsJsonPrimitive("socksUsername").getAsString());
assertEquals("test2", json.getAsJsonPrimitive("socksPassword").getAsString());
assertEquals("localhost,127.0.0.*", json.getAsJsonPrimitive("noProxy").getAsString());
assertEquals("manual", json.get("proxyType"));
assertEquals("ftp.proxy", json.get("ftpProxy"));
assertEquals("http.proxy:1234", json.get("httpProxy"));
assertEquals("ssl.proxy", json.get("sslProxy"));
assertEquals("socks.proxy:65555", json.get("socksProxy"));
assertEquals("test1", json.get("socksUsername"));
assertEquals("test2", json.get("socksPassword"));
assertEquals("localhost,127.0.0.*", json.get("noProxy"));
assertEquals(8, json.entrySet().size());
}

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

JsonObject json = proxy.toJson().getAsJsonObject();
Map<String, Object> json = proxy.toJson();

assertEquals("pac", json.getAsJsonPrimitive("proxyType").getAsString());
assertEquals("http://aaa/bbb.pac", json.getAsJsonPrimitive("proxyAutoconfigUrl").getAsString());
assertEquals("pac", json.get("proxyType"));
assertEquals("http://aaa/bbb.pac", json.get("proxyAutoconfigUrl"));
assertEquals(2, json.entrySet().size());
}

Expand Down Expand Up @@ -319,10 +324,10 @@ public void autodetectProxyToJson() {
proxy.setProxyType(ProxyType.AUTODETECT);
proxy.setAutodetect(true);

JsonObject json = proxy.toJson().getAsJsonObject();
Map<String, ?> json = proxy.toJson();

assertEquals("autodetect", json.getAsJsonPrimitive("proxyType").getAsString());
assertTrue(json.getAsJsonPrimitive("autodetect").getAsBoolean());
assertEquals("autodetect", json.get("proxyType"));
assertTrue((Boolean) json.get("autodetect"));
assertEquals(2, json.entrySet().size());
}

Expand Down Expand Up @@ -351,9 +356,9 @@ public void systemProxyToJson() {
Proxy proxy = new Proxy();
proxy.setProxyType(ProxyType.SYSTEM);

JsonObject json = proxy.toJson().getAsJsonObject();
Map<String, Object> json = proxy.toJson();

assertEquals("system", json.getAsJsonPrimitive("proxyType").getAsString());
assertEquals("system", json.get("proxyType"));
assertEquals(1, json.entrySet().size());
}

Expand Down Expand Up @@ -382,9 +387,9 @@ public void directProxyToJson() {
Proxy proxy = new Proxy();
proxy.setProxyType(ProxyType.DIRECT);

JsonObject json = proxy.toJson().getAsJsonObject();
Map<String, Object> json = proxy.toJson();

assertEquals("direct", json.getAsJsonPrimitive("proxyType").getAsString());
assertEquals("direct", json.get("proxyType"));
assertEquals(1, json.entrySet().size());
}

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

Proxy proxy = Proxy.extractFrom(caps);

assertNull(proxy.getFtpProxy());
assertFalse(proxy.isAutodetect());
assertEquals("http://www.example.com", proxy.getHttpProxy());
}

@Test
@Ignore
public void serialiazesAndDeserializesWithoutError() {
Proxy proxy = new Proxy();
proxy.setProxyAutoconfigUrl("http://www.example.com/config.pac");

DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability(PROXY, proxy);

String rawJson = new BeanToJsonConverter().convert(caps);
Capabilities converted = new JsonToBeanConverter().convert(Capabilities.class, rawJson);

Object returnedProxy = converted.getCapability(PROXY);
assertTrue(returnedProxy instanceof Proxy);
}
}

0 comments on commit 3538223

Please sign in to comment.