Author: joshcanfield
Date: Wed Jan 19 05:50:19 2011
New Revision: 1060687

URL: http://svn.apache.org/viewvc?rev=1060687&view=rev
Log:
Fixed TAP5-1419: Created Tomcat6Runner and added servlet-container test 
parameter.

Added:
    
tapestry/tapestry5/trunk/tapestry-test/src/main/java/org/apache/tapestry5/test/ServletContainerRunner.java
    
tapestry/tapestry5/trunk/tapestry-test/src/main/java/org/apache/tapestry5/test/Tomcat6Runner.java
Modified:
    tapestry/tapestry5/trunk/tapestry-test/pom.xml
    
tapestry/tapestry5/trunk/tapestry-test/src/main/java/org/apache/tapestry5/test/Jetty7Runner.java
    
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/TapestryTestConstants.java

Modified: tapestry/tapestry5/trunk/tapestry-test/pom.xml
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-test/pom.xml?rev=1060687&r1=1060686&r2=1060687&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-test/pom.xml (original)
+++ tapestry/tapestry5/trunk/tapestry-test/pom.xml Wed Jan 19 05:50:19 2011
@@ -56,6 +56,20 @@
       <version>5.1.12</version>
       <scope>compile</scope>
     </dependency>
+
+    <dependency>
+        <groupId>org.apache.tomcat</groupId>
+        <artifactId>catalina</artifactId>
+        <version>6.0.30</version>
+        <scope>compile</scope>
+    </dependency>
+    <dependency>
+        <groupId>org.apache.tomcat</groupId>
+        <artifactId>coyote</artifactId>
+        <version>6.0.30</version>
+        <scope>compile</scope>
+    </dependency>
+
   </dependencies>
   <build>
     <plugins>

Modified: 
tapestry/tapestry5/trunk/tapestry-test/src/main/java/org/apache/tapestry5/test/Jetty7Runner.java
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-test/src/main/java/org/apache/tapestry5/test/Jetty7Runner.java?rev=1060687&r1=1060686&r2=1060687&view=diff
==============================================================================
--- 
tapestry/tapestry5/trunk/tapestry-test/src/main/java/org/apache/tapestry5/test/Jetty7Runner.java
 (original)
+++ 
tapestry/tapestry5/trunk/tapestry-test/src/main/java/org/apache/tapestry5/test/Jetty7Runner.java
 Wed Jan 19 05:50:19 2011
@@ -23,7 +23,7 @@ import org.eclipse.jetty.webapp.WebAppCo
 /**
  * Launches an instance of Jetty7.
  */
-public class Jetty7Runner
+public class Jetty7Runner implements ServletContainerRunner
 {
     private final Server jettyServer;
 

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=1060687&r1=1060686&r2=1060687&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
 Wed Jan 19 05:50:19 2011
@@ -37,7 +37,7 @@ import com.thoughtworks.selenium.Seleniu
 /**
  * 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(String, String, int, int, String, ITestContext, 
XmlTest)}.
+ * {@link #testStartup(org.testng.ITestContext, org.testng.xml.XmlTest)}.
  * 
  * @since 5.2.0
  */
@@ -47,6 +47,8 @@ public class SeleniumTestCase extends As
      * 15 seconds
      */
     public static final String PAGE_LOAD_TIMEOUT = "15000";
+    public static final String TOMCAT_6 = "tomcat6";
+    public static final String JETTY_7 = "jetty7";
 
     private Selenium delegate;
 
@@ -70,6 +72,12 @@ public class SeleniumTestCase extends As
      * <th>Description</th>
      * </tr>
      * <tr>
+     * <td>container</td>
+     * <td>tapestry.servlet-container</td>
+     * <td>JETTY_7</td>
+     * <td>The Servlet container to use for the tests. Currently {@link 
#JETTY_7} or {@link #TOMCAT_6}</td>
+     * </tr>
+     * <tr>
      * <td>webAppFolder</td>
      * <td>tapestry.web-app-folder</td>
      * <td>src/main/webapp</td>
@@ -131,13 +139,14 @@ public class SeleniumTestCase extends As
         // Map<String, String> testParameters = xmlTest.getParameters();
 
         String webAppFolder = getParameter(xmlTest, 
TapestryTestConstants.WEB_APP_FOLDER_PARAMETER, "src/main/webapp");
+        String container = getParameter(xmlTest, 
TapestryTestConstants.SERVLET_CONTAINER_PARAMETER, JETTY_7);
         String contextPath = getParameter(xmlTest, 
TapestryTestConstants.CONTEXT_PATH_PARAMETER, "");
         int port = Integer.parseInt(getParameter(xmlTest, 
TapestryTestConstants.PORT_PARAMETER, "9090"));
         int sslPort = Integer.parseInt(getParameter(xmlTest, 
TapestryTestConstants.SSL_PORT_PARAMETER, "8443"));
         String browserStartCommand = getParameter(xmlTest, 
TapestryTestConstants.BROWSER_START_COMMAND_PARAMETER,
                 "*firefox");
 
-        final Runnable stopWebServer = launchWebServer(webAppFolder, 
contextPath, port, sslPort);
+        final Runnable stopWebServer = launchWebServer(container, 
webAppFolder, contextPath, port, sslPort);
 
         final SeleniumServer seleniumServer = new SeleniumServer();
 
@@ -197,7 +206,7 @@ public class SeleniumTestCase extends As
     }
 
     /**
-     * Like {@link #testStartup(String, String, int, int, String, 
ITestContext, XmlTest)}, this may
+     * 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.
      */
     @AfterTest
@@ -214,7 +223,7 @@ public class SeleniumTestCase extends As
     }
 
     /**
-     * Invoked from {@link #testStartup(String, String, int, String, 
ITestContext)} to launch the web
+     * 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
@@ -233,7 +242,18 @@ public class SeleniumTestCase extends As
      */
     protected Runnable launchWebServer(String webAppFolder, String 
contextPath, int port, int sslPort) throws Exception
     {
-        final Jetty7Runner runner = new Jetty7Runner(webAppFolder, 
contextPath, port, sslPort);
+        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);
 
         return new Runnable()
         {

Added: 
tapestry/tapestry5/trunk/tapestry-test/src/main/java/org/apache/tapestry5/test/ServletContainerRunner.java
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-test/src/main/java/org/apache/tapestry5/test/ServletContainerRunner.java?rev=1060687&view=auto
==============================================================================
--- 
tapestry/tapestry5/trunk/tapestry-test/src/main/java/org/apache/tapestry5/test/ServletContainerRunner.java
 (added)
+++ 
tapestry/tapestry5/trunk/tapestry-test/src/main/java/org/apache/tapestry5/test/ServletContainerRunner.java
 Wed Jan 19 05:50:19 2011
@@ -0,0 +1,9 @@
+package org.apache.tapestry5.test;
+
+public interface ServletContainerRunner
+{
+    /**
+     * Stop the container immediately
+     */
+    public void stop();
+}

Modified: 
tapestry/tapestry5/trunk/tapestry-test/src/main/java/org/apache/tapestry5/test/TapestryTestConstants.java
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-test/src/main/java/org/apache/tapestry5/test/TapestryTestConstants.java?rev=1060687&r1=1060686&r2=1060687&view=diff
==============================================================================
--- 
tapestry/tapestry5/trunk/tapestry-test/src/main/java/org/apache/tapestry5/test/TapestryTestConstants.java
 (original)
+++ 
tapestry/tapestry5/trunk/tapestry-test/src/main/java/org/apache/tapestry5/test/TapestryTestConstants.java
 Wed Jan 19 05:50:19 2011
@@ -29,6 +29,7 @@ public class TapestryTestConstants
      * The current working directory (i.e., property "user.dir").
      */
     public static final String CURRENT_DIR_PATH = 
System.getProperty("user.dir");
+
     /**
      * The Surefire plugin sets basedir but DOES NOT change the current 
working directory. When
      * building across modules, basedir changes for each module, but user.dir 
does not. This value should be used when
@@ -45,7 +46,7 @@ public class TapestryTestConstants
     /**
      * {@link ITestContext} attribute holding an instance of {@link Selenium}.
      * 
-     * @see SeleniumLauncher#startup(String, String, int, String, ITestContext)
+     * @see SeleniumTestCase#testStartup(org.testng.ITestContext, 
org.testng.xml.XmlTest)
      * @since 5.2.0
      */
     public static final String SELENIUM_ATTRIBUTE = "tapestry.selenium";
@@ -53,7 +54,7 @@ public class TapestryTestConstants
     /**
      * {@link ITestContext} attribute holding an instance of {@link 
ErrorReporter}.
      * 
-     * @see SeleniumLauncher#startup(String, String, int, String, ITestContext)
+     * @see SeleniumTestCase#testStartup(org.testng.ITestContext, 
org.testng.xml.XmlTest)
      * @since 5.2.0
      */
     public static final String ERROR_REPORTER_ATTRIBUTE = 
"tapestry.error-reporter";
@@ -77,8 +78,6 @@ public class TapestryTestConstants
 
     /**
      * {@link ITestContext} attribute holding the application's base URL.
-     * 
-     * @see SeleniumLauncher#startup(String, String, int, String, ITestContext)
      * @since 5.2.0
      */
     public static final String BASE_URL_ATTRIBUTE = "tapestry.base-url";
@@ -114,4 +113,10 @@ public class TapestryTestConstants
      * {@link XmlTest} parameter holding the browser command to pass to 
Selenium.
      */
     public static final String BROWSER_START_COMMAND_PARAMETER = 
"tapestry.browser-start-command";
+
+    /**
+     * {@link XmlTest} parameter holding the servlet container to run for the 
integration tests.
+     * @since 5.3.0
+     */
+    public static final String SERVLET_CONTAINER_PARAMETER = 
"tapestry.servlet-container";
 }

Added: 
tapestry/tapestry5/trunk/tapestry-test/src/main/java/org/apache/tapestry5/test/Tomcat6Runner.java
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-test/src/main/java/org/apache/tapestry5/test/Tomcat6Runner.java?rev=1060687&view=auto
==============================================================================
--- 
tapestry/tapestry5/trunk/tapestry-test/src/main/java/org/apache/tapestry5/test/Tomcat6Runner.java
 (added)
+++ 
tapestry/tapestry5/trunk/tapestry-test/src/main/java/org/apache/tapestry5/test/Tomcat6Runner.java
 Wed Jan 19 05:50:19 2011
@@ -0,0 +1,143 @@
+// Copyright 2009, 2010 The Apache Software Foundation
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package org.apache.tapestry5.test;
+
+import org.apache.catalina.Context;
+import org.apache.catalina.Engine;
+import org.apache.catalina.Host;
+import org.apache.catalina.Wrapper;
+import org.apache.catalina.connector.Connector;
+import org.apache.catalina.loader.WebappLoader;
+import org.apache.catalina.servlets.DefaultServlet;
+import org.apache.catalina.startup.Embedded;
+
+import java.io.File;
+
+/**
+ * Launches an instance of Tomcat 6.
+ * @since 5.3.0
+ */
+public class Tomcat6Runner implements ServletContainerRunner
+{
+    private final String description;
+    private final int port;
+    private final int sslPort;
+    private Embedded tomcatServer;
+
+    public Tomcat6Runner(String webappFolder, String contextPath, int port, 
int sslPort) throws Exception
+    {
+        this.port = port;
+
+        this.sslPort = sslPort;
+
+        final String expandedPath = expand(webappFolder);
+
+        description = String.format("<Tomcat6Runner:%s:%s/%s (%s)", 
contextPath, port, sslPort, expandedPath);
+
+        tomcatServer = new Embedded();
+
+        // Tomcat creates a folder, try to put it in an OS agnostic tmp dir
+        String tmpDir = System.getProperty("java.io.tmpdir");
+        String fileSeparator = System.getProperty("file.separator");
+        if (!tmpDir.endsWith(fileSeparator)) tmpDir = tmpDir + fileSeparator;
+        tomcatServer.setCatalinaHome(tmpDir + "tomcat");
+
+        final Engine engine = tomcatServer.createEngine();
+        engine.setDefaultHost("localhost");
+
+        final Host host = tomcatServer.createHost("localhost", expandedPath);
+        engine.addChild(host);
+
+        final Context context = tomcatServer.createContext(contextPath, 
expandedPath);
+
+        // Without a servlet the filter will not get run.
+        final Wrapper wrapper = context.createWrapper();
+        final String name = "DefaultServlet";
+        wrapper.setName(name);
+        wrapper.setServletClass(DefaultServlet.class.getName());
+        context.addChild(wrapper);
+        context.addServletMapping("/", name);
+
+        context.setLoader(new WebappLoader(this.getClass().getClassLoader()));
+
+        host.addChild(context);
+
+        tomcatServer.addEngine(engine);
+
+        Connector http = tomcatServer.createConnector("localhost", port, 
false);
+        http.setAllowTrace(true);
+        tomcatServer.addConnector(http);
+
+        // SSL support
+        final File keystoreFile = new 
File(TapestryTestConstants.MODULE_BASE_DIR, "src/test/conf/keystore");
+
+        if (keystoreFile.exists())
+        {
+            final Connector https = tomcatServer.createConnector("localhost", 
sslPort, true);
+            https.setProperty("keystore", keystoreFile.getPath());
+            https.setProperty("keypass", "tapestry");
+            tomcatServer.addConnector(https);
+        }
+
+        tomcatServer.start();
+    }
+
+    /**
+     * Immediately shuts down the server instance.
+     */
+    public void stop()
+    {
+        System.out.printf("Stopping Tomcat instance on port %d/%d\n", port, 
sslPort);
+
+        try
+        {
+            // Stop immediately and not gracefully.
+            tomcatServer.stop();
+        } catch (Exception ex)
+        {
+            throw new RuntimeException("Error stopping Tomcat6 instance: " + 
ex.toString(), ex);
+        }
+
+        System.out.println("Tomcat instance has stopped.");
+    }
+
+    @Override
+    public String toString()
+    {
+        return description;
+    }
+
+    /**
+     * Needed inside Maven multi-projects to expand a path relative to the 
module to a complete
+     * path. If the path already is absolute and points to an existing 
directory, it will be used
+     * unchanged.
+     *
+     * @param moduleLocalPath
+     * @return expanded path
+     * @see TapestryTestConstants#MODULE_BASE_DIR
+     */
+    protected String expand(String moduleLocalPath)
+    {
+        File path = new File(moduleLocalPath);
+
+        // Don't expand if the path provided already exists.
+        if (path.isAbsolute() && path.isDirectory())
+            return moduleLocalPath;
+
+        return new File(TapestryTestConstants.MODULE_BASE_DIR, 
moduleLocalPath).getPath();
+    }
+
+
+}


Reply via email to