MockServletContext.getResource() doesn't seem to work
-----------------------------------------------------

                 Key: STS-376
                 URL: http://mc4j.org/jira/browse/STS-376
             Project: Stripes
          Issue Type: Bug
          Components: Context Management
    Affects Versions: Release 1.4.3
         Environment: OS X 104.9 + Eclipse 3.2
            Reporter: Andrew Jaquith
         Assigned To: Tim Fennell


I have a snippet of code in my unit tests that attemps to load a resource 
relative to the webroot. Calling MockServletContext.getResource to try to 
obtain this resource fails when the resource name begins with a slash (/), 
which the servlet specification says it must.

I determined by trial and error that the following code snippet, in which the 
servlet context is in fact a MockServletContext, works as expected:

            url = m_engine.getServletContext().getResource( "WEB-INF/web.xml" );

...although it is *wrong*. From the ServletContext.getResource(String) JavaDoc 
(J2EE 1.3):

"Returns a URL to the resource that is mapped to a specified path. The path 
must begin with a "/" and is interpreted as relative to the current context 
root."

The code that SHOULD work, but does not, is this:

            url = m_engine.getServletContext().getResource( "/WEB-INF/web.xml" 
);

The root cause of this issue appears to be how MockServletContext actually 
looks up resources. The source code looks like this:

    /** Uses the current classloader to fetch the resource if it can. */
    public URL getResource(String name) throws MalformedURLException {
        return Thread.currentThread().getContextClassLoader().getResource(name);
    }

I believe what is happening is that the servlet context expects a leading 
slash, and if one is supplied it is retained and passed on to the classloader's 
getResource() method. This will cause the file name to be interepreted as an 
absolute path.

Recommended fix: inside MockServletContext.getResource, trim the leading slash 
if one was passed.

-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators: 
http://mc4j.org/jira/secure/Administrators.jspa
-
For more information on JIRA, see: http://www.atlassian.com/software/jira

        

-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/
_______________________________________________
Stripes-development mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/stripes-development

Reply via email to