Class MockServletContext
- java.lang.Object
- org.springframework.mock.web.MockServletContext
- All Implemented Interfaces:
ServletContext
public class MockServletContext extends Object implements ServletContext
Mock implementation of theServletContext
interface.As of Spring 4.0, this set of mocks is designed on a Servlet 3.0 baseline.
Compatible with Servlet 3.0 but can be configured to expose a specific version through
setMajorVersion(int)
/setMinorVersion(int)
; default is 3.0. Note that Servlet 3.0 support is limited: servlet, filter and listener registration methods are not supported; neither is 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
orFileSystemXmlApplicationContext
can be used to load the context files for testing, even forDispatcherServlet
context definitions.For setting up a full
WebApplicationContext
in a test environment, you can useAnnotationConfigWebApplicationContext
,XmlWebApplicationContext
, orGenericWebApplicationContext
, passing in an appropriateMockServletContext
instance. You might want to configure yourMockServletContext
with aFileSystemResourceLoader
in that case to ensure that resource paths are interpreted as relative filesystem 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 filesystem) andXmlWebApplicationContext
with an underlyingMockServletContext
(as long as theMockServletContext
has been configured with aFileSystemResourceLoader
).- Since:
- 1.0.2
- Author:
- Rod Johnson, Juergen Hoeller, Sam Brannen
- See Also:
MockServletContext(org.springframework.core.io.ResourceLoader)
,AnnotationConfigWebApplicationContext
,XmlWebApplicationContext
,GenericWebApplicationContext
,ClassPathXmlApplicationContext
,FileSystemXmlApplicationContext
Field Summary
Fields inherited from interface javax.servlet.ServletContext
ORDERED_LIBS, TEMPDIR
Constructor Summary
Constructors Constructor Description MockServletContext()
Create a newMockServletContext
, using no base path and aDefaultResourceLoader
(i.e.MockServletContext(String resourceBasePath)
Create a newMockServletContext
, using aDefaultResourceLoader
.MockServletContext(String resourceBasePath, ResourceLoader resourceLoader)
Create a newMockServletContext
using the supplied resource base path and resource loader.MockServletContext(ResourceLoader resourceLoader)
Create a newMockServletContext
, using the specifiedResourceLoader
and no base path.
Method Summary
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface javax.servlet.ServletContext
getVirtualServerName
Constructor Detail
MockServletContext
public MockServletContext()
Create a newMockServletContext
, using no base path and aDefaultResourceLoader
(i.e. the classpath root as WAR root).- See Also:
DefaultResourceLoader
MockServletContext
public MockServletContext(String resourceBasePath)
Create a newMockServletContext
, using aDefaultResourceLoader
.- Parameters:
resourceBasePath
- the root directory of the WAR (should not end with a slash)- See Also:
DefaultResourceLoader
MockServletContext
public MockServletContext(ResourceLoader resourceLoader)
Create a newMockServletContext
, using the specifiedResourceLoader
and no base path.- Parameters:
resourceLoader
- the ResourceLoader to use (or null for the default)
MockServletContext
public MockServletContext(String resourceBasePath, ResourceLoader resourceLoader)
Create a newMockServletContext
using the supplied resource base path and resource loader.Registers a
MockRequestDispatcher
for the Servlet named 'default'.- Parameters:
resourceBasePath
- the root directory of the WAR (should not end with a slash)resourceLoader
- the ResourceLoader to use (or null for the default)- See Also:
registerNamedDispatcher(java.lang.String, javax.servlet.RequestDispatcher)
Method Detail
getResourceLocation
protected String getResourceLocation(String path)
Build a full resource location for the given path, prepending the resource base path of thisMockServletContext
.- Parameters:
path
- the path as specified- Returns:
- the full resource path
setContextPath
public void setContextPath(String contextPath)
getContextPath
public String getContextPath()
- Specified by:
getContextPath
in interfaceServletContext
registerContext
public void registerContext(String contextPath, ServletContext context)
getContext
public ServletContext getContext(String contextPath)
- Specified by:
getContext
in interfaceServletContext
setMajorVersion
public void setMajorVersion(int majorVersion)
getMajorVersion
public int getMajorVersion()
- Specified by:
getMajorVersion
in interfaceServletContext
setMinorVersion
public void setMinorVersion(int minorVersion)
getMinorVersion
public int getMinorVersion()
- Specified by:
getMinorVersion
in interfaceServletContext
setEffectiveMajorVersion
public void setEffectiveMajorVersion(int effectiveMajorVersion)
getEffectiveMajorVersion
public int getEffectiveMajorVersion()
- Specified by:
getEffectiveMajorVersion
in interfaceServletContext
setEffectiveMinorVersion
public void setEffectiveMinorVersion(int effectiveMinorVersion)
getEffectiveMinorVersion
public int getEffectiveMinorVersion()
- Specified by:
getEffectiveMinorVersion
in interfaceServletContext
getMimeType
public String getMimeType(String filePath)
This method uses the defaultFileTypeMap
from the Java Activation Framework to resolve MIME types.The Java Activation Framework returns
"application/octet-stream"
if the MIME type is unknown (i.e., it never returnsnull
). Thus, in order to honor theServletContext.getMimeType(String)
contract, this method returnsnull
if the MIME type is"application/octet-stream"
.MockServletContext
does not provide a direct mechanism for setting a custom MIME type; however, if the defaultFileTypeMap
is an instance ofjavax.activation.MimetypesFileTypeMap
, a custom MIME type namedtext/enigma
can be registered for a custom.puzzle
file extension in the following manner:MimetypesFileTypeMap mimetypesFileTypeMap = (MimetypesFileTypeMap) FileTypeMap.getDefaultFileTypeMap(); mimetypesFileTypeMap.addMimeTypes("text/enigma puzzle");
- Specified by:
getMimeType
in interfaceServletContext
getResourcePaths
public Set<String> getResourcePaths(String path)
- Specified by:
getResourcePaths
in interfaceServletContext
getResource
public URL getResource(String path) throws MalformedURLException
- Specified by:
getResource
in interfaceServletContext
- Throws:
MalformedURLException
getResourceAsStream
public InputStream getResourceAsStream(String path)
- Specified by:
getResourceAsStream
in interfaceServletContext
getRequestDispatcher
public RequestDispatcher getRequestDispatcher(String path)
- Specified by:
getRequestDispatcher
in interfaceServletContext
getNamedDispatcher
public RequestDispatcher getNamedDispatcher(String path)
- Specified by:
getNamedDispatcher
in interfaceServletContext
registerNamedDispatcher
public void registerNamedDispatcher(String name, RequestDispatcher requestDispatcher)
Register aRequestDispatcher
(typically aMockRequestDispatcher
) that acts as a wrapper for the named Servlet.- Parameters:
name
- the name of the wrapped ServletrequestDispatcher
- the dispatcher that wraps the named Servlet- See Also:
getNamedDispatcher(java.lang.String)
,unregisterNamedDispatcher(java.lang.String)
unregisterNamedDispatcher
public void unregisterNamedDispatcher(String name)
Unregister theRequestDispatcher
with the given name.- Parameters:
name
- the name of the dispatcher to unregister- See Also:
getNamedDispatcher(java.lang.String)
,registerNamedDispatcher(java.lang.String, javax.servlet.RequestDispatcher)
getDefaultServletName
public String getDefaultServletName()
Get the name of the defaultServlet
.Defaults to 'default'.
setDefaultServletName
public void setDefaultServletName(String defaultServletName)
Set the name of the defaultServlet
.Also
unregisters
the current defaultRequestDispatcher
andreplaces
it with aMockRequestDispatcher
for the provideddefaultServletName
.- Parameters:
defaultServletName
- the name of the defaultServlet
; nevernull
or empty- See Also:
getDefaultServletName()
getServlet
@Deprecated public Servlet getServlet(String name)
Deprecated.- Specified by:
getServlet
in interfaceServletContext
getServlets
@Deprecated public Enumeration<Servlet> getServlets()
Deprecated.- Specified by:
getServlets
in interfaceServletContext
getServletNames
@Deprecated public Enumeration<String> getServletNames()
Deprecated.- Specified by:
getServletNames
in interfaceServletContext
log
public void log(String message)
- Specified by:
log
in interfaceServletContext
log
@Deprecated public void log(Exception ex, String message)
Deprecated.- Specified by:
log
in interfaceServletContext
log
public void log(String message, Throwable ex)
- Specified by:
log
in interfaceServletContext
getRealPath
public String getRealPath(String path)
- Specified by:
getRealPath
in interfaceServletContext
getServerInfo
public String getServerInfo()
- Specified by:
getServerInfo
in interfaceServletContext
getInitParameter
public String getInitParameter(String name)
- Specified by:
getInitParameter
in interfaceServletContext
getInitParameterNames
public Enumeration<String> getInitParameterNames()
- Specified by:
getInitParameterNames
in interfaceServletContext
setInitParameter
public boolean setInitParameter(String name, String value)
- Specified by:
setInitParameter
in interfaceServletContext
addInitParameter
public void addInitParameter(String name, String value)
getAttribute
public Object getAttribute(String name)
- Specified by:
getAttribute
in interfaceServletContext
getAttributeNames
public Enumeration<String> getAttributeNames()
- Specified by:
getAttributeNames
in interfaceServletContext
setAttribute
public void setAttribute(String name, Object value)
- Specified by:
setAttribute
in interfaceServletContext
removeAttribute
public void removeAttribute(String name)
- Specified by:
removeAttribute
in interfaceServletContext
setServletContextName
public void setServletContextName(String servletContextName)
getServletContextName
public String getServletContextName()
- Specified by:
getServletContextName
in interfaceServletContext
getClassLoader
public ClassLoader getClassLoader()
- Specified by:
getClassLoader
in interfaceServletContext
declareRoles
public void declareRoles(String... roleNames)
- Specified by:
declareRoles
in interfaceServletContext
getDeclaredRoles
public Set<String> getDeclaredRoles()
setSessionTrackingModes
public void setSessionTrackingModes(Set<SessionTrackingMode> sessionTrackingModes) throws IllegalStateException, IllegalArgumentException
- Specified by:
setSessionTrackingModes
in interfaceServletContext
- Throws:
IllegalStateException
IllegalArgumentException
getDefaultSessionTrackingModes
public Set<SessionTrackingMode> getDefaultSessionTrackingModes()
- Specified by:
getDefaultSessionTrackingModes
in interfaceServletContext
getEffectiveSessionTrackingModes
public Set<SessionTrackingMode> getEffectiveSessionTrackingModes()
- Specified by:
getEffectiveSessionTrackingModes
in interfaceServletContext
getSessionCookieConfig
public SessionCookieConfig getSessionCookieConfig()
- Specified by:
getSessionCookieConfig
in interfaceServletContext
getJspConfigDescriptor
public JspConfigDescriptor getJspConfigDescriptor()
- Specified by:
getJspConfigDescriptor
in interfaceServletContext
addServlet
public ServletRegistration.Dynamic addServlet(String servletName, String className)
- Specified by:
addServlet
in interfaceServletContext
addServlet
public ServletRegistration.Dynamic addServlet(String servletName, Servlet servlet)
- Specified by:
addServlet
in interfaceServletContext
addServlet
public ServletRegistration.Dynamic addServlet(String servletName, Class<? extends Servlet> servletClass)
- Specified by:
addServlet
in interfaceServletContext
createServlet
public <T extends Servlet> T createServlet(Class<T> c) throws ServletException
- Specified by:
createServlet
in interfaceServletContext
- Throws:
ServletException
getServletRegistration
public ServletRegistration getServletRegistration(String servletName)
This method always returnsnull
.- Specified by:
getServletRegistration
in interfaceServletContext
- See Also:
ServletContext.getServletRegistration(java.lang.String)
getServletRegistrations
public Map<String,? extends ServletRegistration> getServletRegistrations()
This method always returns an empty map.- Specified by:
getServletRegistrations
in interfaceServletContext
- See Also:
ServletContext.getServletRegistrations()
addFilter
public FilterRegistration.Dynamic addFilter(String filterName, String className)
- Specified by:
addFilter
in interfaceServletContext
addFilter
public FilterRegistration.Dynamic addFilter(String filterName, Filter filter)
- Specified by:
addFilter
in interfaceServletContext
addFilter
public FilterRegistration.Dynamic addFilter(String filterName, Class<? extends Filter> filterClass)
- Specified by:
addFilter
in interfaceServletContext
createFilter
public <T extends Filter> T createFilter(Class<T> c) throws ServletException
- Specified by:
createFilter
in interfaceServletContext
- Throws:
ServletException
getFilterRegistration
public FilterRegistration getFilterRegistration(String filterName)
This method always returnsnull
.- Specified by:
getFilterRegistration
in interfaceServletContext
- See Also:
ServletContext.getFilterRegistration(java.lang.String)
getFilterRegistrations
public Map<String,? extends FilterRegistration> getFilterRegistrations()
This method always returns an empty map.- Specified by:
getFilterRegistrations
in interfaceServletContext
- See Also:
ServletContext.getFilterRegistrations()
addListener
public void addListener(Class<? extends EventListener> listenerClass)
- Specified by:
addListener
in interfaceServletContext
addListener
public void addListener(String className)
- Specified by:
addListener
in interfaceServletContext
addListener
public <T extends EventListener> void addListener(T t)
- Specified by:
addListener
in interfaceServletContext
createListener
public <T extends EventListener> T createListener(Class<T> c) throws ServletException
- Specified by:
createListener
in interfaceServletContext
- Throws:
ServletException