You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tapestry.apache.org by hl...@apache.org on 2012/01/05 19:52:06 UTC

svn commit: r1227749 - in /tapestry/tapestry5/trunk/tapestry-test/src/main/java/org/apache/tapestry5/test: SeleniumTestCase.java TapestryTestConfiguration.java

Author: hlship
Date: Thu Jan  5 18:52:06 2012
New Revision: 1227749

URL: http://svn.apache.org/viewvc?rev=1227749&view=rev
Log:
Change port attributes of TapestryTestConfiguration to be ints
Modify SeleniumTestCase to output (to System.err) a summary of its configuration as it starts up

Modified:
    tapestry/tapestry5/trunk/tapestry-test/src/main/java/org/apache/tapestry5/test/SeleniumTestCase.java
    tapestry/tapestry5/trunk/tapestry-test/src/main/java/org/apache/tapestry5/test/TapestryTestConfiguration.java

Modified: tapestry/tapestry5/trunk/tapestry-test/src/main/java/org/apache/tapestry5/test/SeleniumTestCase.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-test/src/main/java/org/apache/tapestry5/test/SeleniumTestCase.java?rev=1227749&r1=1227748&r2=1227749&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-test/src/main/java/org/apache/tapestry5/test/SeleniumTestCase.java (original)
+++ tapestry/tapestry5/trunk/tapestry-test/src/main/java/org/apache/tapestry5/test/SeleniumTestCase.java Thu Jan  5 18:52:06 2012
@@ -14,31 +14,25 @@
 
 package org.apache.tapestry5.test;
 
-import java.io.File;
-import java.lang.reflect.Method;
-
+import com.thoughtworks.selenium.CommandProcessor;
+import com.thoughtworks.selenium.DefaultSelenium;
+import com.thoughtworks.selenium.HttpCommandProcessor;
+import com.thoughtworks.selenium.Selenium;
 import org.openqa.selenium.server.RemoteControlConfiguration;
 import org.openqa.selenium.server.SeleniumServer;
 import org.testng.Assert;
 import org.testng.ITestContext;
-import org.testng.annotations.AfterClass;
-import org.testng.annotations.AfterMethod;
-import org.testng.annotations.AfterTest;
-import org.testng.annotations.BeforeClass;
-import org.testng.annotations.BeforeMethod;
-import org.testng.annotations.BeforeTest;
+import org.testng.annotations.*;
 import org.testng.xml.XmlTest;
 
-import com.thoughtworks.selenium.CommandProcessor;
-import com.thoughtworks.selenium.DefaultSelenium;
-import com.thoughtworks.selenium.HttpCommandProcessor;
-import com.thoughtworks.selenium.Selenium;
+import java.io.File;
+import java.lang.reflect.Method;
 
 /**
  * Base class for creating Selenium-based integration test cases. This class implements all the
  * methods of {@link Selenium} and delegates to an instance (setup once per test by
  * {@link #testStartup(org.testng.ITestContext, org.testng.xml.XmlTest)}.
- * 
+ *
  * @since 5.2.0
  */
 public abstract class SeleniumTestCase extends Assert implements Selenium
@@ -55,7 +49,7 @@ public abstract class SeleniumTestCase e
     /**
      * An XPath expression for locating a submit element (very commonly used
      * with {@link #clickAndWait(String)}.
-     * 
+     *
      * @since 5.3
      */
     public static final String SUBMIT = "//input[@type='submit']";
@@ -65,7 +59,7 @@ public abstract class SeleniumTestCase e
      * this can be useful when attempting to use SeleniumTestCase with a newer version of Selenium which
      * has added some methods to the interface. This field will not be set until the test case instance
      * has gone through its full initialization.
-     * 
+     *
      * @since 5.3
      */
     protected Selenium selenium;
@@ -127,19 +121,18 @@ public abstract class SeleniumTestCase e
      * <td>Command string used to launch the browser, as defined by Selenium</td>
      * </tr>
      * </table>
-     * <p>
+     * <p/>
      * Tests in the <em>beforeStartup</em> group will be run before the start of Selenium. This can be used to
      * programmatically override the above parameter values.
-     * <p>
+     * <p/>
      * This method will be invoked in <em>each</em> subclass, but is set up to only startup the servers once (it checks
      * the {@link ITestContext} to see if the necessary keys are already present).
-     * 
-     * @param testContext
-     *            Used to share objects between the launcher and the test suites
+     *
+     * @param testContext Used to share objects between the launcher and the test suites
      * @throws Exception
      */
     @BeforeTest(dependsOnGroups =
-    { "beforeStartup" })
+            {"beforeStartup"})
     public void testStartup(final ITestContext testContext, XmlTest xmlTest) throws Exception
     {
         // This is not actually necessary, because TestNG will only invoke this method once
@@ -147,7 +140,9 @@ public abstract class SeleniumTestCase e
         // just invokes it on the "first" TestCase instance it has test methods for.
 
         if (testContext.getAttribute(TapestryTestConstants.SHUTDOWN_ATTRIBUTE) != null)
+        {
             return;
+        }
 
         // If a parameter is overridden in another test method, TestNG won't pass the
         // updated value via a parameter, but still passes the original (coming from testng.xml or the default).
@@ -156,10 +151,14 @@ public abstract class SeleniumTestCase e
         // Map<String, String> testParameters = xmlTest.getParameters();
 
         TapestryTestConfiguration annotation = this.getClass().getAnnotation(TapestryTestConfiguration.class);
-        if(annotation == null)
+        if (annotation == null)
         {
-            @TapestryTestConfiguration final class defaults{};
-            annotation = defaults.class.getAnnotation(TapestryTestConfiguration.class);
+            @TapestryTestConfiguration
+            final class EmptyInnerClass
+            {
+            }
+
+            annotation = EmptyInnerClass.class.getAnnotation(TapestryTestConfiguration.class);
         }
 
         String webAppFolder = getParameter(xmlTest, TapestryTestConstants.WEB_APP_FOLDER_PARAMETER,
@@ -168,12 +167,22 @@ public abstract class SeleniumTestCase e
                 annotation.container());
         String contextPath = getParameter(xmlTest, TapestryTestConstants.CONTEXT_PATH_PARAMETER,
                 annotation.contextPath());
-        int port = Integer.parseInt(getParameter(xmlTest, TapestryTestConstants.PORT_PARAMETER, annotation.port()));
-        int sslPort = Integer.parseInt(getParameter(xmlTest, TapestryTestConstants.SSL_PORT_PARAMETER,
-                annotation.sslPort()));
+        int port = getIntParameter(xmlTest, TapestryTestConstants.PORT_PARAMETER, annotation.port());
+        int sslPort = getIntParameter(xmlTest, TapestryTestConstants.SSL_PORT_PARAMETER, annotation.sslPort());
         String browserStartCommand = getParameter(xmlTest, TapestryTestConstants.BROWSER_START_COMMAND_PARAMETER,
                 annotation.browserStartCommand());
 
+        String baseURL = String.format("http://localhost:%d%s/", port, contextPath);
+
+        System.err.println("Starting SeleniumTestCase:");
+        System.err.println("    currentDir: " + System.getProperty("user.dir"));
+        System.err.println("  webAppFolder: " + webAppFolder);
+        System.err.println("     container: " + container);
+        System.err.println("   contextPath: " + contextPath);
+        System.err.printf("         ports: %d / %d%n", port, sslPort);
+        System.err.println("  browserStart: " + browserStartCommand);
+        System.err.println("       baseURL: " + baseURL);
+
         final Runnable stopWebServer = launchWebServer(container, webAppFolder, contextPath, port, sslPort);
 
         final SeleniumServer seleniumServer = new SeleniumServer();
@@ -181,11 +190,12 @@ public abstract class SeleniumTestCase e
         File ffProfileTemplate = new File(TapestryTestConstants.MODULE_BASE_DIR, "src/test/conf/ff_profile_template");
 
         if (ffProfileTemplate.isDirectory())
+        {
             seleniumServer.getConfiguration().setFirefoxProfileTemplate(ffProfileTemplate);
+        }
 
         seleniumServer.start();
 
-        String baseURL = String.format("http://localhost:%d%s/", port, contextPath);
 
         CommandProcessor httpCommandProcessor = new HttpCommandProcessor("localhost",
                 RemoteControlConfiguration.DEFAULT_PORT, browserStartCommand, baseURL);
@@ -220,8 +230,7 @@ public abstract class SeleniumTestCase e
                     // builds.
 
                     errorReporter.writeOutputPaths();
-                }
-                finally
+                } finally
                 {
                     testContext.removeAttribute(TapestryTestConstants.BASE_URL_ATTRIBUTE);
                     testContext.removeAttribute(TapestryTestConstants.SELENIUM_ATTRIBUTE);
@@ -240,6 +249,13 @@ public abstract class SeleniumTestCase e
         return value != null ? value : defaultValue;
     }
 
+    private final int getIntParameter(XmlTest xmlTest, String key, int defaultValue)
+    {
+        String value = xmlTest.getParameter(key);
+
+        return value != null ? Integer.parseInt(value) : defaultValue;
+    }
+
     /**
      * Like {@link #testStartup(org.testng.ITestContext, org.testng.xml.XmlTest)} , this may
      * be called multiple times against multiple instances, but only does work the first time.
@@ -254,42 +270,28 @@ public abstract class SeleniumTestCase e
         // and the runnable is the last thing it puts into the test context.
 
         if (r != null)
+        {
             r.run();
+        }
     }
 
     /**
      * Invoked from {@link #testStartup(org.testng.ITestContext, org.testng.xml.XmlTest)} to launch the web
-     * server to be
-     * tested. The return value is a Runnable that will shut down the launched server at the end of
-     * the test (it is coded this way so that the default Jetty web server can be more easily
-     * replaced).
-     * 
-     * @param webAppFolder
-     *            path to the web application context
-     * @param contextPath
-     *            the path the context is mapped to, usually the empty string
-     * @param port
-     *            the port number the server should handle
-     * @param sslPort
-     *            the port number on which the server should handle secure requests
+     * server to be tested. The return value is a Runnable that can be invoked later to cleanly shut down the launched
+     * server at the end of the test.
+     *
+     * @param container    identifies which web server should be launched
+     * @param webAppFolder path to the web application context
+     * @param contextPath  the path the context is mapped to, usually the empty string
+     * @param port         the port number the server should handle
+     * @param sslPort      the port number on which the server should handle secure requests
      * @return Runnable used to shut down the server
      * @throws Exception
      */
-    protected Runnable launchWebServer(String webAppFolder, String contextPath, int port, int sslPort) throws Exception
-    {
-        return launchWebServer(TOMCAT_6, webAppFolder, contextPath, port, sslPort);
-    }
-
     protected Runnable launchWebServer(String container, String webAppFolder, String contextPath, int port, int sslPort)
             throws Exception
     {
-        final ServletContainerRunner runner;
-        if (TOMCAT_6.equals(container))
-            runner = new Tomcat6Runner(webAppFolder, contextPath, port, sslPort);
-        else if (JETTY_7.equals(container))
-            runner = new Jetty7Runner(webAppFolder, contextPath, port, sslPort);
-        else
-            throw new RuntimeException("Unknown servlet container: " + container);
+        final ServletContainerRunner runner = createWebServer(container, webAppFolder, contextPath, port, sslPort);
 
         return new Runnable()
         {
@@ -300,6 +302,21 @@ public abstract class SeleniumTestCase e
         };
     }
 
+    private ServletContainerRunner createWebServer(String container, String webAppFolder, String contextPath, int port, int sslPort) throws Exception
+    {
+        if (TOMCAT_6.equals(container))
+        {
+            return new Tomcat6Runner(webAppFolder, contextPath, port, sslPort);
+        }
+
+        if (JETTY_7.equals(container))
+        {
+            return new Jetty7Runner(webAppFolder, contextPath, port, sslPort);
+        }
+
+        throw new RuntimeException("Unknown servlet container: " + container);
+    }
+
     @BeforeClass
     public void setup(ITestContext context)
     {
@@ -331,7 +348,7 @@ public abstract class SeleniumTestCase e
     /**
      * Returns the base URL for the application. This is of the typically <code>http://localhost:9999/</code> (i.e., it
      * includes a trailing slash).
-     * <p>
+     * <p/>
      * Generally, you should use {@link #openLinks(String...)} to start from your application's home page.
      */
     public String getBaseURL()
@@ -1145,7 +1162,9 @@ public abstract class SeleniumTestCase e
         throw new AssertionError("This statement should not be reachable.");
     }
 
-    /** Open the {@linkplain #getBaseURL()}, and waits for the page to load. */
+    /**
+     * Open the {@linkplain #getBaseURL()}, and waits for the page to load.
+     */
     protected final void openBaseURL()
     {
         open(baseURL);
@@ -1155,11 +1174,9 @@ public abstract class SeleniumTestCase e
 
     /**
      * Asserts the text of an element, identified by the locator.
-     * 
-     * @param locator
-     *            identifies the element whose text value is to be asserted
-     * @param expected
-     *            expected value for the element's text
+     *
+     * @param locator  identifies the element whose text value is to be asserted
+     * @param expected expected value for the element's text
      */
     protected final void assertText(String locator, String expected)
     {
@@ -1168,8 +1185,7 @@ public abstract class SeleniumTestCase e
         try
         {
             actual = getText(locator);
-        }
-        catch (RuntimeException ex)
+        } catch (RuntimeException ex)
         {
             System.err.printf("Error accessing %s: %s, in:\n\n%s\n\n", locator, ex.getMessage(), getHtmlSource());
 
@@ -1199,9 +1215,8 @@ public abstract class SeleniumTestCase e
 
     /**
      * Assets that each string provided is present somewhere in the current document.
-     * 
-     * @param expected
-     *            string expected to be present
+     *
+     * @param expected string expected to be present
      */
     protected final void assertSourcePresent(String... expected)
     {
@@ -1221,9 +1236,8 @@ public abstract class SeleniumTestCase e
     /**
      * Click a link identified by a locator, then wait for the resulting page to load.
      * This is not useful for Ajax updates, just normal full-page refreshes.
-     * 
-     * @param locator
-     *            identifies the link to click
+     *
+     * @param locator identifies the link to click
      */
     protected final void clickAndWait(String locator)
     {
@@ -1243,11 +1257,9 @@ public abstract class SeleniumTestCase e
 
     /**
      * Used when the locator identifies an attribute, not an element.
-     * 
-     * @param locator
-     *            identifies the attribute whose value is to be asserted
-     * @param expected
-     *            expected value for the attribute
+     *
+     * @param locator  identifies the attribute whose value is to be asserted
+     * @param expected expected value for the attribute
      */
     protected final void assertAttribute(String locator, String expected)
     {
@@ -1256,8 +1268,7 @@ public abstract class SeleniumTestCase e
         try
         {
             actual = getAttribute(locator);
-        }
-        catch (RuntimeException ex)
+        } catch (RuntimeException ex)
         {
             System.err.printf("Error accessing %s: %s", locator, ex.getMessage());
 
@@ -1276,11 +1287,9 @@ public abstract class SeleniumTestCase e
 
     /**
      * Assets that the value in the field matches the expectation
-     * 
-     * @param locator
-     *            identifies the field
-     * @param expected
-     *            expected value for the field
+     *
+     * @param locator  identifies the field
+     * @param expected expected value for the field
      * @since 5.3
      */
     protected final void assertFieldValue(String locator, String expected)
@@ -1288,8 +1297,7 @@ public abstract class SeleniumTestCase e
         try
         {
             assertEquals(getValue(locator), expected);
-        }
-        catch (AssertionError ex)
+        } catch (AssertionError ex)
         {
             writeErrorReport();
 
@@ -1300,7 +1308,7 @@ public abstract class SeleniumTestCase e
     /**
      * Opens the base URL, then clicks through a series of links to get to a desired application
      * state.
-     * 
+     *
      * @since 5.3
      */
     protected final void openLinks(String... linkText)
@@ -1315,7 +1323,7 @@ public abstract class SeleniumTestCase e
 
     /**
      * Sleeps for the indicated number of seconds.
-     * 
+     *
      * @since 5.3
      */
     protected final void sleep(long millis)
@@ -1323,8 +1331,7 @@ public abstract class SeleniumTestCase e
         try
         {
             Thread.sleep(millis);
-        }
-        catch (InterruptedException ex)
+        } catch (InterruptedException ex)
         {
             // Ignore.
         }
@@ -1333,9 +1340,8 @@ public abstract class SeleniumTestCase e
     /**
      * Waits, up to the page load limit for an element (identified by a CSS rule) to exist
      * (it is not assured that the element will be visible).
-     * 
-     * @param cssRule
-     *            used to locate the element
+     *
+     * @param cssRule used to locate the element
      * @since 5.3
      */
     protected void waitForCSSSelectedElementToAppear(String cssRule)
@@ -1348,9 +1354,8 @@ public abstract class SeleniumTestCase e
     /**
      * Waits for the element with the given client-side id to be present in the DOM (
      * does not assure that the element is visible).
-     * 
-     * @param elementId
-     *            identifies the element
+     *
+     * @param elementId identifies the element
      * @since 5.3
      */
     protected final void waitForElementToAppear(String elementId)
@@ -1363,9 +1368,8 @@ public abstract class SeleniumTestCase e
 
     /**
      * Waits for the element to be removed from the DOM.
-     * 
-     * @param elementId
-     *            client-side id of element
+     *
+     * @param elementId client-side id of element
      * @since 5.3
      */
     protected final void waitForElementToDisappear(String elementId)
@@ -1379,8 +1383,8 @@ public abstract class SeleniumTestCase e
      * Waits for the element specified by the selector to become visible
      * Note that waitForElementToAppear waits for the element to be present in the dom, visible or not. waitForVisible
      * waits for an element that already exists in the dom to become visible.
-     * @param selector
-     *              element selector
+     *
+     * @param selector element selector
      * @since 5.3
      */
     protected final void waitForVisible(String selector)
@@ -1394,8 +1398,8 @@ public abstract class SeleniumTestCase e
      * Waits for the element specified by the selector to become invisible
      * Note that waitForElementToDisappear waits for the element to be absent from the dom, visible or not. waitForInvisible
      * waits for an existing element to become invisible.
-     * @param selector
-     *              element selector
+     *
+     * @param selector element selector
      * @since 5.3
      */
     protected final void waitForInvisible(String selector)
@@ -1404,20 +1408,19 @@ public abstract class SeleniumTestCase e
 
         waitForCondition(condition, PAGE_LOAD_TIMEOUT);
     }
+
     /**
      * Asserts that the current page's title matches the expected value.
-     * 
+     *
+     * @param expected value for title
      * @since 5.3
-     * @param expected
-     *            value for title
      */
     protected final void assertTitle(String expected)
     {
         try
         {
             assertEquals(getTitle(), expected);
-        }
-        catch (AssertionError ex)
+        } catch (AssertionError ex)
         {
             writeErrorReport();
 
@@ -1428,16 +1431,16 @@ public abstract class SeleniumTestCase e
     /**
      * Waits until all active XHR requests are completed.
      *
-     * @since 5.3
-     *
      * @param timeout timeout to wait for
+     * @since 5.3
      */
     protected final void waitForAjaxRequestsToComplete(String timeout)
     {
         waitForCondition("selenium.browserbot.getCurrentWindow().Ajax.activeRequestCount == 0", timeout);
     }
 
-    public Number getCssCount(String str) {
+    public Number getCssCount(String str)
+    {
         return selenium.getCssCount(str);
     }
 }

Modified: tapestry/tapestry5/trunk/tapestry-test/src/main/java/org/apache/tapestry5/test/TapestryTestConfiguration.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-test/src/main/java/org/apache/tapestry5/test/TapestryTestConfiguration.java?rev=1227749&r1=1227748&r2=1227749&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-test/src/main/java/org/apache/tapestry5/test/TapestryTestConfiguration.java (original)
+++ tapestry/tapestry5/trunk/tapestry-test/src/main/java/org/apache/tapestry5/test/TapestryTestConfiguration.java Thu Jan  5 18:52:06 2012
@@ -26,6 +26,7 @@ import java.lang.annotation.*;
  * <b>Parameters coming from a TestNG XML configuration file take precedence over those supplied with the annotation.</b>
  *
  * Configures the container to be started for the tests and the browser to be used.
+ * @since 5.4
  */
 @Target(ElementType.TYPE)
 @Retention(RetentionPolicy.RUNTIME)
@@ -51,12 +52,12 @@ public @interface TapestryTestConfigurat
     /**
      * The port to listen on for HTTP requests. Defaults to "9090".
      */
-    String port() default "9090";
+    int port() default 9090;
 
     /**
      * The port to listen on fot HTTPS requests. Defaults to "8443".
      */
-    String sslPort() default "8443";
+    int sslPort() default 8443;
 
     /**
      * The browser start command to use with Selenium. Defaults to "*firefox".