Skip to content

Commit

Permalink
[java] Moving test utility classes to the only tests where they are used
Browse files Browse the repository at this point in the history
  • Loading branch information
barancev committed Sep 19, 2018
1 parent c24ac7c commit 46627dc
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 152 deletions.
1 change: 1 addition & 0 deletions java/client/test/org/openqa/selenium/BUCK
Expand Up @@ -90,6 +90,7 @@ java_library(name = 'tests',
'//third_party/java/assertj:assertj',
'//third_party/java/jetty:jetty',
'//third_party/java/junit:junit',
'//third_party/java/littleshoot:littleproxy',
'//third_party/java/mockito:mockito-core',
'//third_party/java/servlet:javax.servlet-api',
],
Expand Down
73 changes: 72 additions & 1 deletion java/client/test/org/openqa/selenium/ProxySettingTest.java
Expand Up @@ -29,18 +29,28 @@
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ErrorCollector;
import org.littleshoot.proxy.HttpFilters;
import org.littleshoot.proxy.HttpFiltersAdapter;
import org.littleshoot.proxy.HttpFiltersSourceAdapter;
import org.littleshoot.proxy.HttpProxyServer;
import org.littleshoot.proxy.impl.DefaultHttpProxyServer;
import org.openqa.selenium.net.NetworkUtils;
import org.openqa.selenium.net.PortProber;
import org.openqa.selenium.testing.Ignore;
import org.openqa.selenium.testing.JUnit4TestBase;
import org.openqa.selenium.testing.NeedsLocalEnvironment;
import org.openqa.selenium.testing.ProxyServer;
import org.openqa.selenium.testing.drivers.WebDriverBuilder;
import org.seleniumhq.jetty9.server.Handler;
import org.seleniumhq.jetty9.server.Request;
import org.seleniumhq.jetty9.server.Server;
import org.seleniumhq.jetty9.server.ServerConnector;
import org.seleniumhq.jetty9.server.handler.AbstractHandler;

import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.HttpObject;
import io.netty.handler.codec.http.HttpRequest;
import io.netty.handler.codec.http.HttpResponse;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -228,4 +238,65 @@ private Server createServer(Handler handler) {
private static HostAndPort getHostAndPort(Server server) {
return HostAndPort.fromParts(server.getURI().getHost(), server.getURI().getPort());
}

public static class ProxyServer {
private HttpProxyServer proxyServer;
private final String baseUrl;
private final List<String> uris = new ArrayList<>();

public ProxyServer() {
int port = PortProber.findFreePort();

String address = new NetworkUtils().getPrivateLocalAddress();
baseUrl = String.format("%s:%d", address, port);

proxyServer = DefaultHttpProxyServer.bootstrap().withAllowLocalOnly(false).withPort(port)
.withFiltersSource(new HttpFiltersSourceAdapter() {
public HttpFilters filterRequest(HttpRequest originalRequest, ChannelHandlerContext ctx) {
return new HttpFiltersAdapter(originalRequest) {
public HttpResponse clientToProxyRequest(HttpObject httpObject) {
String uri = originalRequest.uri();
String[] parts = uri.split("/");
if (parts.length == 0) {
return null;
}
String finalPart = parts[parts.length - 1];
uris.add(finalPart);
return null;
}

@Override
public HttpObject serverToProxyResponse(HttpObject httpObject) {
return httpObject;
}
};
}
})
.start();
}

public String getBaseUrl() {
return baseUrl;
}

/**
* Checks if a resource has been requested using the short name of the resource.
*
* @param resourceName The short name of the resource to check.
* @return true if the resource has been called.
*/
public boolean hasBeenCalled(String resourceName) {
return uris.contains(resourceName);
}

public void destroy() {
proxyServer.stop();
}

public Proxy asProxy() {
Proxy proxy = new Proxy();
proxy.setHttpProxy(baseUrl);
return proxy;
}
}
}
Expand Up @@ -34,6 +34,7 @@
import static org.openqa.selenium.remote.CapabilityType.ACCEPT_INSECURE_CERTS;
import static org.openqa.selenium.remote.CapabilityType.PAGE_LOAD_STRATEGY;

import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;

Expand All @@ -44,7 +45,6 @@
import org.openqa.selenium.PageLoadStrategy;
import org.openqa.selenium.Platform;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.testing.JreSystemProperty;
import org.openqa.selenium.testing.TestUtilities;

import java.io.File;
Expand Down Expand Up @@ -156,7 +156,6 @@ public void pathBasedBinaryRemainsAbsoluteIfSetAsAbsolute() {
@Test
public void shouldPickUpBinaryFromSystemPropertyIfSet() throws IOException {
JreSystemProperty property = new JreSystemProperty(BROWSER_BINARY);
String resetValue = property.get();

Path binary = Files.createTempFile("firefox", ".exe");
try (OutputStream ignored = Files.newOutputStream(binary, DELETE_ON_CLOSE)) {
Expand All @@ -172,14 +171,13 @@ public void shouldPickUpBinaryFromSystemPropertyIfSet() throws IOException {

assertThat(firefoxBinary.getPath()).isEqualTo(binary.toString());
} finally {
property.set(resetValue);
property.reset();
}
}

@Test
public void shouldPickUpLegacyValueFromSystemProperty() {
JreSystemProperty property = new JreSystemProperty(DRIVER_USE_MARIONETTE);
String resetValue = property.get();

try {
// No value should default to using Marionette
Expand All @@ -195,14 +193,13 @@ public void shouldPickUpLegacyValueFromSystemProperty() {
options = new FirefoxOptions();
assertThat(options.isLegacy()).isFalse();
} finally {
property.set(resetValue);
property.reset();
}
}

@Test
public void settingMarionetteToFalseAsASystemPropertyDoesNotPrecedence() {
JreSystemProperty property = new JreSystemProperty(DRIVER_USE_MARIONETTE);
String resetValue = property.get();

try {
Capabilities caps = new ImmutableCapabilities(MARIONETTE, true);
Expand All @@ -211,7 +208,7 @@ public void settingMarionetteToFalseAsASystemPropertyDoesNotPrecedence() {
FirefoxOptions options = new FirefoxOptions().merge(caps);
assertThat(options.isLegacy()).isFalse();
} finally {
property.set(resetValue);
property.reset();
}
}

Expand All @@ -221,15 +218,14 @@ public void shouldPickUpProfileFromSystemProperty() {
assumeThat(defaultProfile).isNotNull();

JreSystemProperty property = new JreSystemProperty(BROWSER_PROFILE);
String resetValue = property.get();
try {
property.set("default");
FirefoxOptions options = new FirefoxOptions();
FirefoxProfile profile = options.getProfile();

assertThat(profile).isNotNull();
} finally {
property.set(resetValue);
property.reset();
}
}

Expand All @@ -240,13 +236,12 @@ public void shouldThrowAnExceptionIfSystemPropertyProfileDoesNotExist() {
assumeThat(foundProfile).isNull();

JreSystemProperty property = new JreSystemProperty(BROWSER_PROFILE);
String resetValue = property.get();
try {
property.set(unlikelyProfileName);
assertThatExceptionOfType(WebDriverException.class)
.isThrownBy(FirefoxOptions::new);
} finally {
property.set(resetValue);
property.reset();
}
}

Expand Down Expand Up @@ -317,4 +312,30 @@ public void roundTrippingToCapabilitiesAndBackWorks() {
assertThat(seen).isEqualTo(expected);
}

private static class JreSystemProperty {

private final String name;
private final String originalValue;

public JreSystemProperty(String name) {
this.name = Preconditions.checkNotNull(name);
this.originalValue = System.getProperty(name);
}

public String get() {
return System.getProperty(name);
}

public void set(String value) {
if (value == null) {
System.clearProperty(name);
} else {
System.setProperty(name, value);
}
}

public void reset() {
set(originalValue);
}
}
}
2 changes: 0 additions & 2 deletions java/client/test/org/openqa/selenium/testing/BUCK
Expand Up @@ -23,7 +23,6 @@ java_library(name = 'helpers',
srcs = [
'DevMode.java',
'InProject.java',
'JreSystemProperty.java',
],
deps = [
'//java/client/src/org/openqa/selenium:core',
Expand All @@ -38,7 +37,6 @@ java_library(name = 'helpers',
java_library(name = 'test-base',
srcs = [
'JUnit4TestBase.java',
'ProxyServer.java',
'SeleniumTestRunner.java',
'TestUtilities.java',
],
Expand Down

This file was deleted.

96 changes: 0 additions & 96 deletions java/client/test/org/openqa/selenium/testing/ProxyServer.java

This file was deleted.

2 changes: 1 addition & 1 deletion third_party/java/littleshoot/BUCK
Expand Up @@ -10,7 +10,7 @@ prebuilt_jar(
'//third_party/java/udt:barchart-udt-bundle'
],
visibility = [
'//java/client/test/org/openqa/selenium/testing:test-base',
'//java/client/test/org/openqa/selenium:tests',
],
)

0 comments on commit 46627dc

Please sign in to comment.