You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tuscany.apache.org by rf...@apache.org on 2008/02/07 20:46:44 UTC

svn commit: r619602 - in /incubator/tuscany/java/sca: itest/ modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/ modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/junit/ tools/maven/maven-web-junit/src/main/java/org...

Author: rfeng
Date: Thu Feb  7 11:46:41 2008
New Revision: 619602

URL: http://svn.apache.org/viewvc?rev=619602&view=rev
Log:
Add the support to package test cases under WEB-INF/classes folder and make it the default scheme

Modified:
    incubator/tuscany/java/sca/itest/pom.xml
    incubator/tuscany/java/sca/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/WebAppServletHost.java
    incubator/tuscany/java/sca/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/junit/WebTestRunner.java
    incubator/tuscany/java/sca/tools/maven/maven-web-junit/src/main/java/org/apache/tuscany/tools/sca/web/junit/plugin/WebJUnitGeneratorMojo.java

Modified: incubator/tuscany/java/sca/itest/pom.xml
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/itest/pom.xml?rev=619602&r1=619601&r2=619602&view=diff
==============================================================================
--- incubator/tuscany/java/sca/itest/pom.xml (original)
+++ incubator/tuscany/java/sca/itest/pom.xml Thu Feb  7 11:46:41 2008
@@ -188,6 +188,7 @@
                 <plugins>
 
                     <!-- Create jar containing the jUnit tests -->
+                    <!--
                     <plugin>
                         <groupId>org.apache.maven.plugins</groupId>
                         <artifactId>maven-jar-plugin</artifactId>
@@ -204,6 +205,7 @@
                             </execution>
                         </executions>
                     </plugin>
+                    -->
 
                     <!-- Create war and include jUnit test jar -->
                     <plugin>
@@ -215,6 +217,7 @@
                             <warSourceExcludes>WEB-INF/lib/tuscany-host-tomcat-*.jar,WEB-INF/lib/tuscany-host-jetty-*.jar,servlet-api-*.jar</warSourceExcludes>
                             <webResources>
                                 <!-- Add the tests jar into the WAR -->
+                                <!--
                                 <resource>
                                     <directory>${project.build.directory}</directory>
                                     <includes>
@@ -222,6 +225,13 @@
                                     </includes>
                                     <targetPath>WEB-INF/lib</targetPath>
                                 </resource>
+                                -->
+
+                                <resource>
+                                    <directory>${project.build.directory}/test-classes</directory>
+                                    <targetPath>WEB-INF/classes</targetPath>
+                                </resource>
+
 
                                 <!-- Add the junit -->
                                 <resource>

Modified: incubator/tuscany/java/sca/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/WebAppServletHost.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/WebAppServletHost.java?rev=619602&r1=619601&r2=619602&view=diff
==============================================================================
--- incubator/tuscany/java/sca/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/WebAppServletHost.java (original)
+++ incubator/tuscany/java/sca/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/WebAppServletHost.java Thu Feb  7 11:46:41 2008
@@ -26,7 +26,6 @@
 import java.net.URI;
 import java.net.URL;
 import java.net.UnknownHostException;
-import java.util.Collections;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.logging.Logger;
@@ -49,7 +48,7 @@
  */
 public class WebAppServletHost implements ServletHost {
     private static final Logger logger = Logger.getLogger(WebAppServletHost.class.getName());
-    
+
     private static final String SCA_DOMAIN_ATTRIBUTE = "org.apache.tuscany.sca.SCADomain";
 
     private static final WebAppServletHost instance = new WebAppServletHost();
@@ -62,11 +61,11 @@
     private WebAppServletHost() {
         servlets = new HashMap<String, Servlet>();
     }
-    
+
     public void setDefaultPort(int port) {
         defaultPortNumber = port;
     }
-    
+
     public int getDefaultPort() {
         return defaultPortNumber;
     }
@@ -79,15 +78,15 @@
         if (!suri.startsWith("/")) {
             suri = '/' + suri;
         }
-        
+
         if (!suri.startsWith(contextPath)) {
             suri = contextPath + suri;
-        } 
-                
+        }
+
         // In a webapp just use the given path and ignore the host and port
         // as they are fixed by the Web container
         servlets.put(suri, servlet);
-        
+
         logger.info("Added Servlet mapping: " + suri);
     }
 
@@ -99,7 +98,7 @@
         if (!suri.startsWith("/")) {
             suri = '/' + suri;
         }
-        
+
         if (!suri.startsWith(contextPath)) {
             suri = contextPath + suri;
         }
@@ -113,11 +112,11 @@
         if (!suri.startsWith("/")) {
             suri = '/' + suri;
         }
-        
+
         if (!suri.startsWith(contextPath)) {
             suri = contextPath + suri;
-        } 
-        
+        }
+
         // Get the servlet mapped to the given path
         Servlet servlet = servlets.get(suri);
         return servlet;
@@ -135,7 +134,7 @@
         if (portNumber == -1) {
             portNumber = defaultPortNumber;
         }
-        
+
         // Get the host
         String host;
         try {
@@ -143,17 +142,17 @@
         } catch (UnknownHostException e) {
             host = "localhost";
         }
-        
+
         // Construct the URL
         String path = uri.getPath();
         if (!path.startsWith("/")) {
             path = '/' + path;
         }
-        
+
         if (!path.startsWith(contextPath)) {
             path = contextPath + path;
         }
-        
+
         URL url;
         try {
             url = new URL(scheme, host, portNumber, path);
@@ -162,26 +161,26 @@
         }
         return url;
     }
-        
+
     public RequestDispatcher getRequestDispatcher(String suri) throws ServletMappingException {
 
         // Make sure that the path starts with a /
         if (!suri.startsWith("/")) {
             suri = '/' + suri;
         }
-        
+
         suri = contextPath + suri;
-        
+
         // Get the servlet mapped to the given path
         Servlet servlet = servlets.get(suri);
         if (servlet != null) {
             return new WebAppRequestDispatcher(suri, servlet);
         }
-        
+
         for (Map.Entry<String, Servlet> entry : servlets.entrySet()) {
             String servletPath = entry.getKey();
             if (servletPath.endsWith("*")) {
-                servletPath = servletPath.substring(0, servletPath.length() -1);
+                servletPath = servletPath.substring(0, servletPath.length() - 1);
                 if (suri.startsWith(servletPath)) {
                     return new WebAppRequestDispatcher(entry.getKey(), entry.getValue());
                 } else {
@@ -191,11 +190,11 @@
                 }
             }
         }
-        
+
         // No servlet found
         return null;
     }
-    
+
     public static WebAppServletHost getInstance() {
         return instance;
     }
@@ -206,7 +205,7 @@
 
         // Create an SCA domain object
         ServletContext servletContext = config.getServletContext();
-        String domainURI = "http://localhost/" + servletContext.getServletContextName().replace(' ', '.');
+        String domainURI = "http://localhost/" + contextPath;
         String contributionRoot = null;
         try {
             URL rootURL = servletContext.getResource("/");
@@ -216,16 +215,16 @@
                 contributionRoot = warRootFile.toURL().toString();
             } else {
                 //this is jetty case
-                contributionRoot  = rootURL.toString();
+                contributionRoot = rootURL.toString();
             }
-        } catch(MalformedURLException mf) {
+        } catch (MalformedURLException mf) {
             //ignore, pass null
         }
         scaDomain = SCADomain.newInstance(domainURI, contributionRoot);
-        
+
         // Store the SCA domain in the servlet context
         servletContext.setAttribute(SCA_DOMAIN_ATTRIBUTE, scaDomain);
-        
+
         // Initialize the registered servlets
         for (Servlet servlet : servlets.values()) {
             servlet.init(config);
@@ -239,27 +238,30 @@
      */
     @SuppressWarnings("unchecked")
     public void initContextPath(ServletConfig config) {
-        if (Collections.list(config.getInitParameterNames()).contains("contextPath")) {
-            contextPath = config.getInitParameter("contextPath");
-        } else {
-            ServletContext context = config.getServletContext();
+        ServletContext context = config.getServletContext();
+        int version = context.getMajorVersion() * 100 + context.getMinorVersion();
+        if (version >= 205) {
+            // The getContextPath() is introduced since Servlet 2.5
             Method m;
             try {
-                m = context.getClass().getMethod("getContextPath", new Class[]{});
+                m = context.getClass().getMethod("getContextPath", new Class[] {});
                 try {
-                    contextPath = (String)m.invoke(context, new Object[]{});
+                    contextPath = (String)m.invoke(context, new Object[] {});
                 } catch (Exception e) {
                     throw new RuntimeException(e);
                 }
             } catch (NoSuchMethodException e) {
-                throw new IllegalStateException("'contextPath' init parameter must be set for pre-2.5 servlet container");
+                throw new IllegalStateException(
+                                                "'contextPath' init parameter must be set for pre-2.5 servlet container");
             }
+        } else {
+            contextPath = config.getInitParameter("contextPath");
         }
         logger.info("initContextPath: " + contextPath);
     }
 
     void destroy() {
-        
+
         // Destroy the registered servlets
         for (Servlet servlet : servlets.values()) {
             servlet.destroy();
@@ -276,9 +278,9 @@
     }
 
     public void setContextPath(String path) {
-//        if (!contextPath.equals(path)) {
-//            throw new IllegalArgumentException("invalid context path for webapp, existing context path: " + contextPath + " new contextPath: " + path);
-//        }
+        //        if (!contextPath.equals(path)) {
+        //            throw new IllegalArgumentException("invalid context path for webapp, existing context path: " + contextPath + " new contextPath: " + path);
+        //        }
     }
 
     /**

Modified: incubator/tuscany/java/sca/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/junit/WebTestRunner.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/junit/WebTestRunner.java?rev=619602&r1=619601&r2=619602&view=diff
==============================================================================
--- incubator/tuscany/java/sca/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/junit/WebTestRunner.java (original)
+++ incubator/tuscany/java/sca/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/junit/WebTestRunner.java Thu Feb  7 11:46:41 2008
@@ -25,9 +25,9 @@
 import java.io.PrintStream;
 import java.net.URL;
 import java.net.URLClassLoader;
-import java.util.ArrayList;
 import java.util.Arrays;
-import java.util.List;
+import java.util.HashSet;
+import java.util.Set;
 import java.util.jar.JarEntry;
 import java.util.jar.JarInputStream;
 import java.util.regex.Pattern;
@@ -35,6 +35,7 @@
 import javax.servlet.Filter;
 import javax.servlet.FilterChain;
 import javax.servlet.FilterConfig;
+import javax.servlet.ServletContext;
 import javax.servlet.ServletException;
 import javax.servlet.ServletRequest;
 import javax.servlet.ServletResponse;
@@ -50,7 +51,7 @@
  */
 public class WebTestRunner implements Filter {
     private static final String JUNIT_TESTS_PATTERN = "junit.tests.pattern";
-    private static final String JUNIT_TESTS_JAR = "junit.tests.jar";
+    private static final String JUNIT_TESTS_PATH = "junit.tests.path";
     private static final String JUNIT_ENABLED = "junit.enabled";
     private static final String TESTCASE_PATTERN = ".*TestCase";
     private static final String TESTS_JAR = "/WEB-INF/test-lib/junit-tests.jar";
@@ -58,14 +59,25 @@
     private FilterConfig config;
     private boolean junitEnabled = true;
 
-    private List<String> findTestCases(String testJarPath) throws IOException {
-        String filter = config.getInitParameter(JUNIT_TESTS_PATTERN);
-        if (filter == null) {
-            filter = TESTCASE_PATTERN;
+    private Set<String> findTestCases(String testJarPath) throws IOException {
+        Pattern pattern = getTestCasePattern();
+        if (testJarPath.endsWith(".jar")) {
+            return findTestCasesInJar(testJarPath, pattern);
+        } else {
+            return findTestCasesInDir(testJarPath, pattern);
         }
-        Pattern pattern = Pattern.compile(filter);
+    }
+
+    /**
+     * Search test cases in a JAR
+     * @param testJarPath
+     * @param pattern
+     * @return
+     * @throws IOException
+     */
+    private Set<String> findTestCasesInJar(String testJarPath, Pattern pattern) throws IOException {
         InputStream in = config.getServletContext().getResourceAsStream(testJarPath);
-        List<String> tests = new ArrayList<String>();
+        Set<String> tests = new HashSet<String>();
         if (in != null) {
             JarInputStream jar = new JarInputStream(in);
             try {
@@ -95,29 +107,69 @@
         return tests;
     }
 
+    private Pattern getTestCasePattern() {
+        String filter = config.getInitParameter(JUNIT_TESTS_PATTERN);
+        if (filter == null) {
+            filter = TESTCASE_PATTERN;
+        }
+        Pattern pattern = Pattern.compile(filter);
+        return pattern;
+    }
+
     public void destroy() {
     }
 
-    private List<String> allTestCases;
+    private Set<String> allTestCases;
     private ClassLoader testClassLoader;
 
     private void init() throws IOException {
         testClassLoader = Thread.currentThread().getContextClassLoader();
-        allTestCases = new ArrayList<String>();
-        String testsJar = config.getInitParameter(JUNIT_TESTS_JAR);
-        if (testsJar == null) {
-            testsJar = TESTS_JAR;
+        allTestCases = new HashSet<String>();
+        String testsPath = config.getInitParameter(JUNIT_TESTS_PATH);
+        if (testsPath == null) {
+            testsPath = TESTS_JAR;
         }
-        URL url = config.getServletContext().getResource(testsJar);
+        URL url = config.getServletContext().getResource(testsPath);
         if (url != null) {
-            allTestCases = findTestCases(testsJar);
-            if (!testsJar.startsWith("/WEB-INF/lib/")) {
+            allTestCases = findTestCases(testsPath);
+            if (!(testsPath.startsWith("/WEB-INF/lib/") || testsPath.startsWith("/WEB-INF/classes/"))) {
                 // Create a new classloader to load the test jar
                 testClassLoader = new URLClassLoader(new URL[] {url}, testClassLoader);
             }
         }
     }
 
+    /**
+     * Search test cases in a directory
+     * @param classesPath
+     * @param pattern
+     * @return
+     */
+    private Set<String> findTestCasesInDir(String classesPath, Pattern pattern) {
+        ServletContext context = config.getServletContext();
+        Set<String> tests = new HashSet<String>();
+        String dir = classesPath;
+        findResources(context, pattern, tests, classesPath, dir);
+        return tests;
+    }
+
+    private void findResources(ServletContext context, Pattern pattern, Set<String> tests, String root, String dir) {
+        Set<String> paths = context.getResourcePaths(dir);
+        if (paths != null) {
+            for (String name : paths) {
+                if (name.endsWith("/")) {
+                    findResources(context, pattern, tests, root, name);
+                }
+                if (name.endsWith(".class")) {
+                    String className = name.substring(root.length(), name.length() - 6).replace('/', '.');
+                    if (pattern.matcher(className).matches()) {
+                        tests.add(className);
+                    }
+                }
+            }
+        }
+    }
+
     public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,
         ServletException {
 
@@ -125,13 +177,13 @@
         String query = req.getQueryString();
         PrintStream ps = new PrintStream(response.getOutputStream());
 
-        List<String> testCases = null;
+        Set<String> testCases = null;
         // ClassLoader cl = Thread.currentThread().getContextClassLoader();
         if (query == null || "ALL".equals(query)) {
             testCases = this.allTestCases;
         } else {
             String[] tests = query.split(",");
-            testCases = Arrays.asList(tests);
+            testCases = new HashSet<String>(Arrays.asList(tests));
         }
 
         int errors = 0;

Modified: incubator/tuscany/java/sca/tools/maven/maven-web-junit/src/main/java/org/apache/tuscany/tools/sca/web/junit/plugin/WebJUnitGeneratorMojo.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/tools/maven/maven-web-junit/src/main/java/org/apache/tuscany/tools/sca/web/junit/plugin/WebJUnitGeneratorMojo.java?rev=619602&r1=619601&r2=619602&view=diff
==============================================================================
--- incubator/tuscany/java/sca/tools/maven/maven-web-junit/src/main/java/org/apache/tuscany/tools/sca/web/junit/plugin/WebJUnitGeneratorMojo.java (original)
+++ incubator/tuscany/java/sca/tools/maven/maven-web-junit/src/main/java/org/apache/tuscany/tools/sca/web/junit/plugin/WebJUnitGeneratorMojo.java Thu Feb  7 11:46:41 2008
@@ -77,8 +77,8 @@
             + "\n           <filter-name>tuscany</filter-name>"
             + "\n           <filter-class>org.apache.tuscany.sca.host.webapp.TuscanyServletFilter</filter-class>"
             + "\n           <init-param>"
-            + "\n               <param-name>junit.tests.jar</param-name>"
-            + "\n               <param-value>${junit.tests.jar}</param-value>"
+            + "\n               <param-name>junit.tests.path</param-name>"
+            + "\n               <param-value>${junit.tests.path}</param-value>"
             + "\n           </init-param>"
             + "\n       </filter>"
             + "\n       <filter-mapping>"
@@ -90,7 +90,7 @@
     /**
      * @parameter
      */
-    private String testsJar;
+    private String testsPath;
 
     /**
      * @parameter
@@ -115,12 +115,17 @@
         base.mkdirs();
         File webxml = new File(base, "web.xml");
         getLog().info("Generating " + webxml.toString());
-        String content = setParameter(WEB_XML, "display-name", project.getName());
+        
+        String name = project.getName();
+        if (name == null) {
+            name = project.getGroupId() + "-" + project.getArtifactId();
+        }
+        String content = setParameter(WEB_XML, "display-name", name);
 
-        if (testsJar == null) {
-            testsJar = "/WEB-INF/lib/junit-tests.jar";
+        if (testsPath == null) {
+            testsPath = "/WEB-INF/classes/";
         }
-        content = setParameter(content, "junit.tests.jar", testsJar);
+        content = setParameter(content, "junit.tests.path", testsPath);
 
         try {
             FileWriter writer = new FileWriter(webxml);



---------------------------------------------------------------------
To unsubscribe, e-mail: tuscany-commits-unsubscribe@ws.apache.org
For additional commands, e-mail: tuscany-commits-help@ws.apache.org