You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@struts.apache.org by cr...@apache.org on 2005/12/06 01:15:28 UTC

svn commit: r354239 - /struts/shale/trunk/test-framework/src/java/org/apache/shale/test/mock/MockServletContext.java

Author: craigmcc
Date: Mon Dec  5 16:15:23 2005
New Revision: 354239

URL: http://svn.apache.org/viewcvs?rev=354239&view=rev
Log:
Properly implement getResource(), getResourceAsStream(), and getResourcePaths()
in terms of the directory specified by the "documentRoot" property.  This allows
test cases that need access to webapp resources to function as expected.

Modified:
    struts/shale/trunk/test-framework/src/java/org/apache/shale/test/mock/MockServletContext.java

Modified: struts/shale/trunk/test-framework/src/java/org/apache/shale/test/mock/MockServletContext.java
URL: http://svn.apache.org/viewcvs/struts/shale/trunk/test-framework/src/java/org/apache/shale/test/mock/MockServletContext.java?rev=354239&r1=354238&r2=354239&view=diff
==============================================================================
--- struts/shale/trunk/test-framework/src/java/org/apache/shale/test/mock/MockServletContext.java (original)
+++ struts/shale/trunk/test-framework/src/java/org/apache/shale/test/mock/MockServletContext.java Mon Dec  5 16:15:23 2005
@@ -22,6 +22,7 @@
 import java.net.MalformedURLException;
 import java.net.URL;
 import java.util.Enumeration;
+import java.util.HashSet;
 import java.util.Hashtable;
 import java.util.Set;
 
@@ -34,6 +35,13 @@
 /**
  * <p>Mock implementation of <code>ServletContext</code>.</p>
  *
+ * <p><strong>WARNING</strong> - Before you can get meaningful results from
+ * calls to the <code>getResource()</code>, <code>getResourceAsStream()</code>,
+ * <code>getResourcePaths()</code>, or <code>getRealPath()</code> methods,
+ * you must configure the <code>documentRoot</code> property, passing in a
+ * <code>File</code> object pointing at a directory that simulates a
+ * web application structure.</p>
+ *
  * $Id$
  */
 
@@ -168,19 +176,20 @@
 
     public URL getResource(String path) throws MalformedURLException {
 
-        // Return a corresponding class loader resource
-        StringBuffer tmp = new StringBuffer(path);
-        if (tmp.charAt(0) == '/')
-           tmp.delete(0, 1);
-        
-        // Return a corresponding class loader resource
-        ClassLoader classloader = Thread.currentThread()
-                .getContextClassLoader();
-        if (classloader == null)
-            classloader = this.getClass().getClassLoader();
-        
-        URL url = classloader.getResource(tmp.toString());
-        return url;
+        if (documentRoot != null) {
+            if (!path.startsWith("/")) {
+                throw new MalformedURLException("The specified path ('" +
+                        path + "') does not start with a '/' character");
+            }
+            File resolved = new File(documentRoot, path.substring(1));
+            if (resolved.exists()) {
+                return resolved.toURL();
+            } else {
+                return null;
+            }
+        } else {
+            return null;
+        }
 
     }
 
@@ -202,7 +211,62 @@
 
     public Set getResourcePaths(String path) {
 
-        throw new UnsupportedOperationException();
+        if (documentRoot == null) {
+            return null;
+        }
+
+        // Enforce the leading slash restriction
+        if (!path.startsWith("/")) {
+            throw new IllegalArgumentException("The specified path ('" +
+                    path + "') does not start with a '/' character");
+        }
+
+        // Set up to do a recursive walk through our webapp resources
+        Set set = new HashSet();
+        String match = null;
+        int trim = 0;
+        try {
+            match = documentRoot.toURL().toExternalForm();
+            trim = match.length() - 1;
+            match += path.substring(1);
+        } catch (Exception e) {
+            match = "";
+        }
+
+        // Perform a recursive walk through our resources
+        getResourcePaths(documentRoot, match, trim, set);
+        return set;
+
+    }
+
+
+    private void getResourcePaths(File node, String match, int trim, Set set) {
+
+        // Does this resource match?
+        String path = null;
+        try {
+            path = node.toURL().toExternalForm();
+        } catch (Exception e) {
+            path = "";
+        }
+        if (path.startsWith(match)) {
+            set.add(path.substring(trim));
+        }
+
+        // Is this resource a directory?
+        if (!node.isDirectory()) {
+            return;
+        }
+
+        // Recursively process directory contents
+        String files[] = node.list();
+        if (files == null) {
+            return;
+        }
+        for (int i = 0; i < files.length; i++) {
+            File subnode = new File(node, files[i]);
+            getResourcePaths(subnode, match, trim, set);
+        }
 
     }
 



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@struts.apache.org
For additional commands, e-mail: dev-help@struts.apache.org