org.springframework.mock.web
Class MockServletContext

java.lang.Object
  extended by org.springframework.mock.web.MockServletContext

public class MockServletContext
extends java.lang.Object

Mock implementation of the javax.servlet.ServletContext interface.

Compatible with Servlet 2.5 and partially with Servlet 3.0. Can be configured to expose a specific version through setMajorVersion(int)/setMinorVersion(int); default is 2.5. Note that Servlet 3.0 support is limited: servlet, filter and listener registration methods are not supported; neither is cookie or JSP configuration. We generally do not recommend to unit-test your ServletContainerInitializers and WebApplicationInitializers which is where those registration methods would be used.

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

Nested Class Summary
private static class MockServletContext.MimeTypeResolver
          Inner factory class used to just introduce a Java Activation Framework dependency when actually asked to resolve a MIME type.
 
Field Summary
private  java.util.Map<java.lang.String,java.lang.Object> attributes
           
private  java.lang.String contextPath
           
private  java.util.Map<java.lang.String,ServletContext> contexts
           
private  java.util.Set<java.lang.String> declaredRoles
           
private  int effectiveMajorVersion
           
private  int effectiveMinorVersion
           
private  java.util.Map<java.lang.String,java.lang.String> initParameters
           
private  Log logger
           
private  int majorVersion
           
private  int minorVersion
           
private  java.lang.String resourceBasePath
           
private  ResourceLoader resourceLoader
           
private  java.lang.String servletContextName
           
private static java.lang.String TEMP_DIR_SYSTEM_PROPERTY
           
 
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)
           
 void declareRoles(java.lang.String... roleNames)
           
 java.lang.Object getAttribute(java.lang.String name)
           
 java.util.Enumeration<java.lang.String> getAttributeNames()
           
 java.lang.ClassLoader getClassLoader()
           
 ServletContext getContext(java.lang.String contextPath)
           
 java.lang.String getContextPath()
           
 java.util.Set<java.lang.String> getDeclaredRoles()
           
 int getEffectiveMajorVersion()
           
 int getEffectiveMinorVersion()
           
 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()
           
 RequestDispatcher getNamedDispatcher(java.lang.String path)
           
 java.lang.String getRealPath(java.lang.String path)
           
 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()
           
 Servlet getServlet(java.lang.String name)
           
 java.lang.String getServletContextName()
           
 java.util.Enumeration<java.lang.String> getServletNames()
           
 java.util.Enumeration<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, 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 setEffectiveMajorVersion(int effectiveMajorVersion)
           
 void setEffectiveMinorVersion(int effectiveMinorVersion)
           
 boolean setInitParameter(java.lang.String name, java.lang.String value)
           
 void setMajorVersion(int majorVersion)
           
 void setMinorVersion(int minorVersion)
           
 void setServletContextName(java.lang.String servletContextName)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

TEMP_DIR_SYSTEM_PROPERTY

private static final java.lang.String TEMP_DIR_SYSTEM_PROPERTY
See Also:
Constant Field Values

logger

private final Log logger

resourceLoader

private final ResourceLoader resourceLoader

resourceBasePath

private final java.lang.String resourceBasePath

contextPath

private java.lang.String contextPath

majorVersion

private int majorVersion

minorVersion

private int minorVersion

effectiveMajorVersion

private int effectiveMajorVersion

effectiveMinorVersion

private int effectiveMinorVersion

contexts

private final java.util.Map<java.lang.String,ServletContext> contexts

initParameters

private final java.util.Map<java.lang.String,java.lang.String> initParameters

attributes

private final java.util.Map<java.lang.String,java.lang.Object> attributes

servletContextName

private java.lang.String servletContextName

declaredRoles

private final java.util.Set<java.lang.String> declaredRoles
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()

registerContext

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

getContext

public ServletContext getContext(java.lang.String contextPath)

setMajorVersion

public void setMajorVersion(int majorVersion)

getMajorVersion

public int getMajorVersion()

setMinorVersion

public void setMinorVersion(int minorVersion)

getMinorVersion

public int getMinorVersion()

setEffectiveMajorVersion

public void setEffectiveMajorVersion(int effectiveMajorVersion)

getEffectiveMajorVersion

public int getEffectiveMajorVersion()

setEffectiveMinorVersion

public void setEffectiveMinorVersion(int effectiveMinorVersion)

getEffectiveMinorVersion

public int getEffectiveMinorVersion()

getMimeType

public java.lang.String getMimeType(java.lang.String filePath)

getResourcePaths

public java.util.Set<java.lang.String> getResourcePaths(java.lang.String path)

getResource

public java.net.URL getResource(java.lang.String path)
                         throws java.net.MalformedURLException
Throws:
java.net.MalformedURLException

getResourceAsStream

public java.io.InputStream getResourceAsStream(java.lang.String path)

getRequestDispatcher

public RequestDispatcher getRequestDispatcher(java.lang.String path)

getNamedDispatcher

public RequestDispatcher getNamedDispatcher(java.lang.String path)

getServlet

public Servlet getServlet(java.lang.String name)

getServlets

public java.util.Enumeration<Servlet> getServlets()

getServletNames

public java.util.Enumeration<java.lang.String> getServletNames()

log

public void log(java.lang.String message)

log

public void log(java.lang.Exception ex,
                java.lang.String message)

log

public void log(java.lang.String message,
                java.lang.Throwable ex)

getRealPath

public java.lang.String getRealPath(java.lang.String path)

getServerInfo

public java.lang.String getServerInfo()

getInitParameter

public java.lang.String getInitParameter(java.lang.String name)

getInitParameterNames

public java.util.Enumeration<java.lang.String> getInitParameterNames()

setInitParameter

public boolean setInitParameter(java.lang.String name,
                                java.lang.String value)

addInitParameter

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

getAttribute

public java.lang.Object getAttribute(java.lang.String name)

getAttributeNames

public java.util.Enumeration<java.lang.String> getAttributeNames()

setAttribute

public void setAttribute(java.lang.String name,
                         java.lang.Object value)

removeAttribute

public void removeAttribute(java.lang.String name)

setServletContextName

public void setServletContextName(java.lang.String servletContextName)

getServletContextName

public java.lang.String getServletContextName()

getClassLoader

public java.lang.ClassLoader getClassLoader()

declareRoles

public void declareRoles(java.lang.String... roleNames)

getDeclaredRoles

public java.util.Set<java.lang.String> getDeclaredRoles()