Skip to content

Commit

Permalink
[java] Moving utility methods to the only class that uses them
Browse files Browse the repository at this point in the history
  • Loading branch information
barancev committed Jan 17, 2018
1 parent ce39406 commit 20c12db
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 176 deletions.
33 changes: 29 additions & 4 deletions java/server/src/org/openqa/grid/selenium/GridLauncherV3.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import org.openqa.grid.internal.utils.configuration.GridHubConfiguration;
import org.openqa.grid.internal.utils.configuration.GridNodeConfiguration;
import org.openqa.grid.internal.utils.configuration.StandaloneConfiguration;
import org.openqa.grid.shared.CliUtils;
import org.openqa.grid.web.Hub;
import org.openqa.grid.web.servlet.DisplayHelpServlet;
import org.openqa.selenium.internal.BuildInfo;
Expand All @@ -38,6 +37,7 @@

import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -154,11 +154,11 @@ private static GridItemLauncher buildLauncher(String[] args) {

private static void printInfoAboutRoles(String roleCommandLineArg) {
if (roleCommandLineArg != null) {
CliUtils.printWrappedLine(
printWrappedLine(
"",
"Error: the role '" + roleCommandLineArg + "' does not match a recognized server role: node/hub/standalone\n");
} else {
CliUtils.printWrappedLine(
printWrappedLine(
"",
"Error: -role option needs to be followed by the value that defines role of this component in the grid\n");
}
Expand All @@ -169,12 +169,37 @@ private static void printInfoAboutRoles(String roleCommandLineArg) {
" standalone as a standalone server not being a part of a grid\n" +
"\n" +
"If -role option is omitted the server runs standalone\n");
CliUtils.printWrappedLine(
printWrappedLine(
"",
"To get help on the options available for a specific role run the server"
+ " with -help option and the corresponding -role option value");
}

private static void printWrappedLine(String prefix, String msg) {
printWrappedLine(System.out, prefix, msg, true);
}

private static void printWrappedLine(PrintStream output, String prefix, String msg, boolean first) {
output.print(prefix);
if (!first) {
output.print(" ");
}
int defaultWrap = 70;
int wrap = defaultWrap - prefix.length();
if (wrap > msg.length()) {
output.println(msg);
return;
}
String lineRaw = msg.substring(0, wrap);
int spaceIndex = lineRaw.lastIndexOf(' ');
if (spaceIndex == -1) {
spaceIndex = lineRaw.length();
}
String line = lineRaw.substring(0, spaceIndex);
output.println(line);
printWrappedLine(output, prefix, msg.substring(spaceIndex + 1), false);
}

private static void configureLogging(StandaloneConfiguration configuration) {
Level logLevel =
configuration.debug
Expand Down
52 changes: 0 additions & 52 deletions java/server/src/org/openqa/grid/shared/CliUtils.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,14 @@

package org.openqa.grid.web.servlet.beta;

import org.openqa.grid.internal.GridRegistry;
import org.openqa.grid.internal.RemoteProxy;
import org.openqa.grid.internal.TestSlot;
import org.openqa.grid.web.utils.BrowserNameUtils;
import org.openqa.selenium.remote.BrowserType;
import org.openqa.selenium.remote.DesiredCapabilities;

import java.io.InputStream;


/**
* the browser on the console will be organized per browserName and version only.
Expand All @@ -46,8 +49,66 @@ public String getVersion() {
}

public String getIcon() {
return BrowserNameUtils.getConsoleIconPath(new DesiredCapabilities(capabilities),
proxy.getRegistry());
return getConsoleIconPath(new DesiredCapabilities(capabilities), proxy.getRegistry());
}

/**
* get the icon representing the browser for the grid. If the icon cannot be located, returns
* null.
*
* @param cap - Capability
* @param registry - GridRegistry
* @return String with path to icon image file. Can be <i>null</i> if no icon
* file if available.
*/
private String getConsoleIconPath(DesiredCapabilities cap, GridRegistry registry) {
String name = consoleIconName(cap, registry);
String path = "org/openqa/grid/images/";
InputStream in =
Thread.currentThread().getContextClassLoader()
.getResourceAsStream(path + name + ".png");
if (in == null) {
return null;
}
return "/grid/resources/" + path + name + ".png";
}

private String consoleIconName(DesiredCapabilities cap, GridRegistry registry) {
String browserString = cap.getBrowserName();
if (browserString == null || "".equals(browserString)) {
return "missingBrowserName";
}

String ret = browserString;

// Map browser environments to icon names.
if (browserString.contains("iexplore") || browserString.startsWith("*iehta")) {
ret = BrowserType.IE;
} else if (browserString.contains("firefox") || browserString.startsWith("*chrome")) {
if (cap.getVersion() != null && cap.getVersion().toLowerCase().equals("beta") ||
cap.getBrowserName().toLowerCase().contains("beta")) {
ret = "firefoxbeta";
} else if (cap.getVersion() != null && cap.getVersion().toLowerCase().equals("aurora") ||
cap.getBrowserName().toLowerCase().contains("aurora")) {
ret = "aurora";
} else if (cap.getVersion() != null && cap.getVersion().toLowerCase().equals("nightly") ||
cap.getBrowserName().toLowerCase().contains("nightly")) {
ret = "nightly";
} else {
ret = BrowserType.FIREFOX;
}

} else if (browserString.startsWith("*safari")) {
ret = BrowserType.SAFARI;
} else if (browserString.startsWith("*googlechrome")) {
ret = BrowserType.CHROME;
} else if (browserString.startsWith("opera")) {
ret = BrowserType.OPERA;
} else if (browserString.toLowerCase().contains("edge")) {
ret = BrowserType.EDGE;
}

return ret.replace(" ", "_");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@

package org.openqa.grid.web.servlet.handler;

import com.google.common.collect.Maps;

import org.openqa.grid.internal.ExternalSessionKey;
import org.openqa.grid.internal.GridRegistry;
import org.openqa.grid.web.utils.BrowserNameUtils;
import org.openqa.selenium.Platform;
import org.openqa.selenium.remote.CapabilityType;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
Expand Down Expand Up @@ -81,7 +84,7 @@ public Map<String, Object> extractDesiredCapability() {
Map<String, Object> cap = new HashMap<>();
// TODO freynaud : more splitting, like trying to guess the
// platform or version ?
cap.putAll(BrowserNameUtils.parseGrid2Environment(envt));
cap.putAll(parseGrid2Environment(envt));

return cap;
}
Expand All @@ -90,6 +93,25 @@ public Map<String, Object> extractDesiredCapability() {
throw new RuntimeException("Error");
}

private Map<String, Object> parseGrid2Environment(String environment) {
Map<String, Object> ret = Maps.newHashMap();

String[] details = environment.split(" ");
if (details.length == 1) {
// simple browser string
ret.put(CapabilityType.BROWSER_NAME, details[0]);
} else {
// more complex. Only case handled so far = X on Y
// where X is the browser string, Y the OS
ret.put(CapabilityType.BROWSER_NAME, details[0]);
if (details.length == 3) {
ret.put(CapabilityType.PLATFORM, Platform.extractFromSysProperty(details[2]));
}
}

return ret;
}

@Override
public String getBody() {
String postBody = super.getBody();
Expand Down
115 changes: 0 additions & 115 deletions java/server/src/org/openqa/grid/web/utils/BrowserNameUtils.java

This file was deleted.

0 comments on commit 20c12db

Please sign in to comment.