Revision: 6888 Author: [email protected] Date: Fri Nov 13 06:33:42 2009 Log: tr...@6852 was merged into this branch Make RunStyleSelenium more extensible. svn merge --ignore-ancestry -c6852 http://google-web-toolkit.googlecode.com/svn/trunk/ .
Patch by: jlabanca http://code.google.com/p/google-web-toolkit/source/detail?r=6888 Modified: /releases/2.0/branch-info.txt /releases/2.0/user/src/com/google/gwt/junit/JUnitShell.java /releases/2.0/user/src/com/google/gwt/junit/RunStyle.java /releases/2.0/user/src/com/google/gwt/junit/RunStyleSelenium.java ======================================= --- /releases/2.0/branch-info.txt Thu Nov 12 14:52:16 2009 +++ /releases/2.0/branch-info.txt Fri Nov 13 06:33:42 2009 @@ -622,3 +622,7 @@ Fixed stupid style error in Mail's stack panel headers. svn merge --ignore-ancestry -c6883 http://google-web-toolkit.googlecode.com/svn/trunk/ . +tr...@6852 was merged into this branch + Make RunStyleSelenium more extensible. + svn merge --ignore-ancestry -c6852 http://google-web-toolkit.googlecode.com/svn/trunk/ . + ======================================= --- /releases/2.0/user/src/com/google/gwt/junit/JUnitShell.java Wed Nov 11 17:18:27 2009 +++ /releases/2.0/user/src/com/google/gwt/junit/JUnitShell.java Fri Nov 13 06:33:42 2009 @@ -55,8 +55,6 @@ import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; -import java.net.InetAddress; -import java.net.UnknownHostException; import java.util.ArrayList; import java.util.Arrays; import java.util.EnumSet; @@ -752,20 +750,15 @@ } public String getModuleUrl(String moduleName) { - try { - String localhost = InetAddress.getLocalHost().getHostAddress(); - String url = "http://" + localhost + ":" + getPort() + "/" - + moduleName - + (standardsMode ? "/junit-standards.html" : "/junit.html"); - if (developmentMode) { - // CHECKSTYLE_OFF - url += "?gwt.hosted=" + localhost + ":" + codeServerPort; - // CHECKSTYLE_ON - } - return url; - } catch (UnknownHostException e) { - throw new RuntimeException("Unable to determine my ip address", e); - } + String localhost = runStyle.getLocalHostName(); + String url = "http://" + localhost + ":" + getPort() + "/" + moduleName + + (standardsMode ? "/junit-standards.html" : "/junit.html"); + if (developmentMode) { + // CHECKSTYLE_OFF + url += "?gwt.hosted=" + localhost + ":" + codeServerPort; + // CHECKSTYLE_ON + } + return url; } /** ======================================= --- /releases/2.0/user/src/com/google/gwt/junit/RunStyle.java Fri Nov 6 10:10:17 2009 +++ /releases/2.0/user/src/com/google/gwt/junit/RunStyle.java Fri Nov 13 06:33:42 2009 @@ -18,6 +18,9 @@ import com.google.gwt.core.ext.TreeLogger; import com.google.gwt.core.ext.UnableToCompleteException; +import java.net.InetAddress; +import java.net.UnknownHostException; + /** * An abstract class that handles the details of launching a browser. */ @@ -49,6 +52,21 @@ public String[] getInterruptedHosts() { return null; } + + /** + * Get the host name of the local system to use in URLs. This method returns + * the host address instead of the host name in case the test target cannot + * resolve the host name. + * + * @return the host name of the local system + */ + public String getLocalHostName() { + try { + return InetAddress.getLocalHost().getHostAddress(); + } catch (UnknownHostException e) { + throw new RuntimeException("Unable to determine my ip address", e); + } + } /** * Returns the number of times this test should be tried to run. A test @@ -111,5 +129,4 @@ protected TreeLogger getLogger() { return shell.getTopLogger(); } - -} +} ======================================= --- /releases/2.0/user/src/com/google/gwt/junit/RunStyleSelenium.java Thu Nov 5 11:47:39 2009 +++ /releases/2.0/user/src/com/google/gwt/junit/RunStyleSelenium.java Fri Nov 13 06:33:42 2009 @@ -16,58 +16,73 @@ package com.google.gwt.junit; import com.google.gwt.core.ext.TreeLogger; -import com.google.gwt.core.ext.UnableToCompleteException; import com.thoughtworks.selenium.DefaultSelenium; import com.thoughtworks.selenium.Selenium; import com.thoughtworks.selenium.SeleniumException; -import java.net.InetAddress; -import java.net.UnknownHostException; import java.util.HashSet; import java.util.Set; import java.util.regex.Matcher; import java.util.regex.Pattern; /** - * Runs in web mode via browsers managed by Selenium. + * Runs via browsers managed by Selenium. */ public class RunStyleSelenium extends RunStyle { - - private static class RCSelenium { - final String browser; - final String host; - final int port; - Selenium selenium; - - public RCSelenium(String browser, String host, int port) { - this.browser = browser; - this.host = host; - this.port = port; + /** + * Wraps a Selenium instance. + */ + protected static interface SeleniumWrapper { + void createSelenium(String domain); + Selenium getSelenium(); + String getSpecifier(); + } + + /** + * Implements SeleniumWrapper using DefaultSelenium. + */ + private static class RCSelenium implements SeleniumWrapper { + + private static final Pattern PATTERN = + Pattern.compile("([\\w\\.-]+):([\\d]+)/([\\w\\s\\*(/\\w+)*]+)"); + + private String browser; + private String host; + private int port; + private Selenium selenium; + private final String specifier; + + public RCSelenium(String specifier) { + this.specifier = specifier; + parseSpecifier(); } public void createSelenium(String domain) { this.selenium = new DefaultSelenium(host, port, browser, domain); } - public String getBrowser() { - return browser; + public Selenium getSelenium() { + return selenium; } - public String getHost() { - return host; + public String getSpecifier() { + return specifier; } - public int getPort() { - return port; - } - - public Selenium getSelenium() { - return selenium; + private void parseSpecifier() { + Matcher matcher = PATTERN.matcher(specifier); + if (!matcher.matches()) { + throw new IllegalArgumentException("Unable to parse Selenium target " + + specifier + " (expected format is [host]:[port]/[browser])"); + } + this.browser = matcher.group(3); + this.host = matcher.group(1); + this.port = Integer.parseInt(matcher.group(2)); } } - private RCSelenium remotes[]; + private SeleniumWrapper remotes[]; /** * The list of hosts that were interrupted. @@ -106,19 +121,15 @@ return false; } String[] targetsIn = args.split(","); - RCSelenium targets[] = new RCSelenium[targetsIn.length]; - - Pattern pattern = Pattern.compile("([\\w\\.-]+):([\\d]+)/([\\w\\s\\*(/\\w+)*]+)"); + SeleniumWrapper targets[] = new SeleniumWrapper[targetsIn.length]; + for (int i = 0; i < targets.length; ++i) { - Matcher matcher = pattern.matcher(targetsIn[i]); - if (!matcher.matches()) { - getLogger().log(TreeLogger.ERROR, "Unable to parse Selenium target " - + targetsIn[i] + " (expected format is [host]:[port]/[browser])"); + try { + targets[i] = createSeleniumWrapper(targetsIn[i]); + } catch (IllegalArgumentException e) { + getLogger().log(TreeLogger.ERROR, e.getMessage()); return false; } - RCSelenium instance = new RCSelenium(matcher.group(3), matcher.group(1), - Integer.parseInt(matcher.group(2))); - targets[i] = instance; } this.remotes = targets; @@ -129,7 +140,7 @@ Runtime.getRuntime().addShutdownHook(new Thread() { @Override public void run() { - for (RCSelenium remote : remotes) { + for (SeleniumWrapper remote : remotes) { if (remote.getSelenium() != null) { try { remote.getSelenium().stop(); @@ -146,18 +157,13 @@ } @Override - public synchronized void launchModule(String moduleName) throws UnableToCompleteException { + public synchronized void launchModule(String moduleName) { // Get the localhost address. - String domain; - try { - String localhost = InetAddress.getLocalHost().getHostAddress(); - domain = "http://" + localhost + ":" + shell.getPort() + "/"; - } catch (UnknownHostException e) { - throw new RuntimeException("Unable to determine my ip address", e); - } + String domain = "http://" + getLocalHostName() + ":" + shell.getPort() + + "/"; // Startup all the selenia and point them at the module url. - for (RCSelenium remote : remotes) { + for (SeleniumWrapper remote : remotes) { try { String url = shell.getModuleUrl(moduleName); shell.getTopLogger().log(TreeLogger.TRACE, @@ -166,11 +172,23 @@ remote.getSelenium().start(); remote.getSelenium().open(url); } catch (Exception e) { - shell.getTopLogger().log(TreeLogger.ERROR, - "Error launching browser via Selenium-RC at " + remote.getHost(), e); + shell.getTopLogger().log( + TreeLogger.ERROR, + "Error launching browser via Selenium-RC at " + + remote.getSpecifier(), e); } } } + + /** + * Factory method for {...@link SeleniumWrapper}. + * + * @param seleniumSpecifier Specifies the Selenium instance to create + * @return an instance of {...@link SeleniumWrapper} + */ + protected SeleniumWrapper createSeleniumWrapper(String seleniumSpecifier) { + return new RCSelenium(seleniumSpecifier); + } /** * Create the keep-alive thread. @@ -195,7 +213,7 @@ private synchronized boolean doKeepAlives() { if (remotes != null) { - for (RCSelenium remote : remotes) { + for (SeleniumWrapper remote : remotes) { // Use getTitle() as a cheap way to see if the Selenium server's still // responding (Selenium seems to provide no way to check the server // status directly). @@ -208,8 +226,7 @@ if (interruptedHosts == null) { interruptedHosts = new HashSet<String>(); } - interruptedHosts.add(remote.getHost() + ":" + remote.getPort() - + "/" + remote.getBrowser()); + interruptedHosts.add(remote.getSpecifier()); } } } -- http://groups.google.com/group/Google-Web-Toolkit-Contributors
