org.springframework.mock.web
Class MockServletContext

java.lang.Object
  extended by org.springframework.mock.web.MockServletContext
All Implemented Interfaces:
javax.servlet.ServletContext

public class MockServletContext
extends java.lang.Object
implements javax.servlet.ServletContext

Mock implementation of the ServletContext interface.

Used for testing the Spring web framework; only rarely necessary for testing application controllers. As long as application components don't explicitly access the ServletContext, ClassPathXmlApplicationContext or FileSystemXmlApplicationContext can be used to load the context files for testing, even for DispatcherServlet context definitions.

For setting up a full WebApplicationContext in a test environment, you can use XmlWebApplicationContext (or GenericWebApplicationContext), passing in an appropriate MockServletContext instance. You might want to configure your MockServletContext with a FileSystemResourceLoader in that case, to make your resource paths interpreted as relative file system locations.

A common setup is to point your JVM working directory to the root of your web application directory, in combination with filesystem-based resource loading. This allows to load the context files as used in the web application, with relative paths getting interpreted correctly. Such a setup will work with both FileSystemXmlApplicationContext (which will load straight from the file system) and XmlWebApplicationContext with an underlying MockServletContext (as long as the MockServletContext has been configured with a FileSystemResourceLoader).

Since:
1.0.2
Author:
Rod Johnson, Juergen Hoeller
See Also:
MockServletContext(org.springframework.core.io.ResourceLoader), XmlWebApplicationContext, GenericWebApplicationContext, ClassPathXmlApplicationContext, FileSystemXmlApplicationContext

Constructor Summary
MockServletContext()
          Create a new MockServletContext, using no base path and a DefaultResourceLoader (i.e.
MockServletContext(ResourceLoader resourceLoader)
          Create a new MockServletContext, using the specified ResourceLoader and no base path.
MockServletContext(java.lang.String resourceBasePath)
          Create a new MockServletContext, using a DefaultResourceLoader.
MockServletContext(java.lang.String resourceBasePath, ResourceLoader resourceLoader)
          Create a new MockServletContext.
 
Method Summary
 void addInitParameter(java.lang.String name, java.lang.String value)
           
 java.lang.Object getAttribute(java.lang.String name)
           
 java.util.Enumeration<java.lang.String> getAttributeNames()
           
 javax.servlet.ServletContext getContext(java.lang.String contextPath)
           
 java.lang.String getContextPath()
           
 java.lang.String getInitParameter(java.lang.String name)
           
 java.util.Enumeration<java.lang.String> getInitParameterNames()
           
 int getMajorVersion()
           
 java.lang.String getMimeType(java.lang.String filePath)
           
 int getMinorVersion()
           
 javax.servlet.RequestDispatcher getNamedDispatcher(java.lang.String path)
           
 java.lang.String getRealPath(java.lang.String path)
           
 javax.servlet.RequestDispatcher getRequestDispatcher(java.lang.String path)
           
 java.net.URL getResource(java.lang.String path)
           
 java.io.InputStream getResourceAsStream(java.lang.String path)
           
protected  java.lang.String getResourceLocation(java.lang.String path)
          Build a full resource location for the given path, prepending the resource base path of this MockServletContext.
 java.util.Set<java.lang.String> getResourcePaths(java.lang.String path)
           
 java.lang.String getServerInfo()
           
 javax.servlet.Servlet getServlet(java.lang.String name)
           
 java.lang.String getServletContextName()
           
 java.util.Enumeration<java.lang.String> getServletNames()
           
 java.util.Enumeration<javax.servlet.Servlet> getServlets()
           
 void log(java.lang.Exception ex, java.lang.String message)
           
 void log(java.lang.String message)
           
 void log(java.lang.String message, java.lang.Throwable ex)
           
 void registerContext(java.lang.String contextPath, javax.servlet.ServletContext context)
           
 void removeAttribute(java.lang.String name)
           
 void setAttribute(java.lang.String name, java.lang.Object value)
           
 void setContextPath(java.lang.String contextPath)
           
 void setServletContextName(java.lang.String servletContextName)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MockServletContext

public MockServletContext()
Create a new MockServletContext, using no base path and a DefaultResourceLoader (i.e. the classpath root as WAR root).

See Also:
DefaultResourceLoader

MockServletContext

public MockServletContext(java.lang.String resourceBasePath)
Create a new MockServletContext, using a DefaultResourceLoader.

Parameters:
resourceBasePath - the WAR root directory (should not end with a slash)
See Also:
DefaultResourceLoader

MockServletContext

public MockServletContext(ResourceLoader resourceLoader)
Create a new MockServletContext, using the specified ResourceLoader and no base path.

Parameters:
resourceLoader - the ResourceLoader to use (or null for the default)

MockServletContext

public MockServletContext(java.lang.String resourceBasePath,
                          ResourceLoader resourceLoader)
Create a new MockServletContext.

Parameters:
resourceBasePath - the WAR root directory (should not end with a slash)
resourceLoader - the ResourceLoader to use (or null for the default)
Method Detail

getResourceLocation

protected java.lang.String getResourceLocation(java.lang.String path)
Build a full resource location for the given path, prepending the resource base path of this MockServletContext.

Parameters:
path - the path as specified
Returns:
the full resource path

setContextPath

public void setContextPath(java.lang.String contextPath)

getContextPath

public java.lang.String getContextPath()
Specified by:
getContextPath in interface javax.servlet.ServletContext

registerContext

public void registerContext(java.lang.String contextPath,
                            javax.servlet.ServletContext context)

getContext

public javax.servlet.ServletContext getContext(java.lang.String contextPath)
Specified by:
getContext in interface javax.servlet.ServletContext

getMajorVersion

public int getMajorVersion()
Specified by:
getMajorVersion in interface javax.servlet.ServletContext

getMinorVersion

public int getMinorVersion()
Specified by:
getMinorVersion in interface javax.servlet.ServletContext

getMimeType

public java.lang.String getMimeType(java.lang.String filePath)
Specified by:
getMimeType in interface javax.servlet.ServletContext

getResourcePaths

public java.util.Set<java.lang.String> getResourcePaths(java.lang.String path)
Specified by:
getResourcePaths in interface javax.servlet.ServletContext

getResource

public java.net.URL getResource(java.lang.String path)
                         throws java.net.MalformedURLException
Specified by:
getResource in interface javax.servlet.ServletContext
Throws:
java.net.MalformedURLException

getResourceAsStream

public java.io.InputStream getResourceAsStream(java.lang.String path)
Specified by:
getResourceAsStream in interface javax.servlet.ServletContext

getRequestDispatcher

public javax.servlet.RequestDispatcher getRequestDispatcher(java.lang.String path)
Specified by:
getRequestDispatcher in interface javax.servlet.ServletContext

getNamedDispatcher

public javax.servlet.RequestDispatcher getNamedDispatcher(java.lang.String path)
Specified by:
getNamedDispatcher in interface javax.servlet.ServletContext

getServlet

public javax.servlet.Servlet getServlet(java.lang.String name)
Specified by:
getServlet in interface javax.servlet.ServletContext

getServlets

public java.util.Enumeration<javax.servlet.Servlet> getServlets()
Specified by:
getServlets in interface javax.servlet.ServletContext

getServletNames

public java.util.Enumeration<java.lang.String> getServletNames()
Specified by:
getServletNames in interface javax.servlet.ServletContext

log

public void log(java.lang.String message)
Specified by:
log in interface javax.servlet.ServletContext

log

public void log(java.lang.Exception ex,
                java.lang.String message)
Specified by:
log in interface javax.servlet.ServletContext

log

public void log(java.lang.String message,
                java.lang.Throwable ex)
Specified by:
log in interface javax.servlet.ServletContext

getRealPath

public java.lang.String getRealPath(java.lang.String path)
Specified by:
getRealPath in interface javax.servlet.ServletContext

getServerInfo

public java.lang.String getServerInfo()
Specified by:
getServerInfo in interface javax.servlet.ServletContext

getInitParameter

public java.lang.String getInitParameter(java.lang.String name)
Specified by:
getInitParameter in interface javax.servlet.ServletContext

addInitParameter

public void addInitParameter(java.lang.String name,
                             java.lang.String value)

getInitParameterNames

public java.util.Enumeration<java.lang.String> getInitParameterNames()
Specified by:
getInitParameterNames in interface javax.servlet.ServletContext

getAttribute

public java.lang.Object getAttribute(java.lang.String name)
Specified by:
getAttribute in interface javax.servlet.ServletContext

getAttributeNames

public java.util.Enumeration<java.lang.String> getAttributeNames()
Specified by:
getAttributeNames in interface javax.servlet.ServletContext

setAttribute

public void setAttribute(java.lang.String name,
                         java.lang.Object value)
Specified by:
setAttribute in interface javax.servlet.ServletContext

removeAttribute

public void removeAttribute(java.lang.String name)
Specified by:
removeAttribute in interface javax.servlet.ServletContext

setServletContextName

public void setServletContextName(java.lang.String servletContextName)

getServletContextName

public java.lang.String getServletContextName()
Specified by:
getServletContextName in interface javax.servlet.ServletContext