Class MockServletContext
- java.lang.Object
- org.springframework.mock.web.MockServletContext
- All Implemented Interfaces:
ServletContext
public class MockServletContext extends Object implements ServletContext
Mock implementation of theServletContextinterface.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,ClassPathXmlApplicationContextorFileSystemXmlApplicationContextcan be used to load the context files for testing, even forDispatcherServletcontext definitions.For setting up a full
WebApplicationContextin a test environment, you can useAnnotationConfigWebApplicationContext,XmlWebApplicationContext, orGenericWebApplicationContext, passing in an appropriateMockServletContextinstance. You might want to configure yourMockServletContextwith aFileSystemResourceLoaderin 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) andXmlWebApplicationContextwith an underlyingMockServletContext(as long as theMockServletContexthas 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 newMockServletContextusing the supplied resource base path and resource loader.MockServletContext(ResourceLoader resourceLoader)Create a newMockServletContext, using the specifiedResourceLoaderand 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 specifiedResourceLoaderand no base path.- Parameters:
resourceLoader- the ResourceLoader to use (or null for the default)
MockServletContext
public MockServletContext(String resourceBasePath, ResourceLoader resourceLoader)
Create a newMockServletContextusing the supplied resource base path and resource loader.Registers a
MockRequestDispatcherfor 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:
getContextPathin interfaceServletContext
registerContext
public void registerContext(String contextPath, ServletContext context)
getContext
public ServletContext getContext(String contextPath)
- Specified by:
getContextin interfaceServletContext
setMajorVersion
public void setMajorVersion(int majorVersion)
getMajorVersion
public int getMajorVersion()
- Specified by:
getMajorVersionin interfaceServletContext
setMinorVersion
public void setMinorVersion(int minorVersion)
getMinorVersion
public int getMinorVersion()
- Specified by:
getMinorVersionin interfaceServletContext
setEffectiveMajorVersion
public void setEffectiveMajorVersion(int effectiveMajorVersion)
getEffectiveMajorVersion
public int getEffectiveMajorVersion()
- Specified by:
getEffectiveMajorVersionin interfaceServletContext
setEffectiveMinorVersion
public void setEffectiveMinorVersion(int effectiveMinorVersion)
getEffectiveMinorVersion
public int getEffectiveMinorVersion()
- Specified by:
getEffectiveMinorVersionin interfaceServletContext
getMimeType
public String getMimeType(String filePath)
This method uses the defaultFileTypeMapfrom 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 returnsnullif the MIME type is"application/octet-stream".MockServletContextdoes not provide a direct mechanism for setting a custom MIME type; however, if the defaultFileTypeMapis an instance ofjavax.activation.MimetypesFileTypeMap, a custom MIME type namedtext/enigmacan be registered for a custom.puzzlefile extension in the following manner:MimetypesFileTypeMap mimetypesFileTypeMap = (MimetypesFileTypeMap) FileTypeMap.getDefaultFileTypeMap(); mimetypesFileTypeMap.addMimeTypes("text/enigma puzzle");- Specified by:
getMimeTypein interfaceServletContext
getResourcePaths
public Set<String> getResourcePaths(String path)
- Specified by:
getResourcePathsin interfaceServletContext
getResource
public URL getResource(String path) throws MalformedURLException
- Specified by:
getResourcein interfaceServletContext- Throws:
MalformedURLException
getResourceAsStream
public InputStream getResourceAsStream(String path)
- Specified by:
getResourceAsStreamin interfaceServletContext
getRequestDispatcher
public RequestDispatcher getRequestDispatcher(String path)
- Specified by:
getRequestDispatcherin interfaceServletContext
getNamedDispatcher
public RequestDispatcher getNamedDispatcher(String path)
- Specified by:
getNamedDispatcherin 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 theRequestDispatcherwith 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
unregistersthe current defaultRequestDispatcherandreplacesit with aMockRequestDispatcherfor the provideddefaultServletName.- Parameters:
defaultServletName- the name of the defaultServlet; nevernullor empty- See Also:
getDefaultServletName()
getServlet
@Deprecated public Servlet getServlet(String name)
Deprecated.- Specified by:
getServletin interfaceServletContext
getServlets
@Deprecated public Enumeration<Servlet> getServlets()
Deprecated.- Specified by:
getServletsin interfaceServletContext
getServletNames
@Deprecated public Enumeration<String> getServletNames()
Deprecated.- Specified by:
getServletNamesin interfaceServletContext
log
public void log(String message)
- Specified by:
login interfaceServletContext
log
@Deprecated public void log(Exception ex, String message)
Deprecated.- Specified by:
login interfaceServletContext
log
public void log(String message, Throwable ex)
- Specified by:
login interfaceServletContext
getRealPath
public String getRealPath(String path)
- Specified by:
getRealPathin interfaceServletContext
getServerInfo
public String getServerInfo()
- Specified by:
getServerInfoin interfaceServletContext
getInitParameter
public String getInitParameter(String name)
- Specified by:
getInitParameterin interfaceServletContext
getInitParameterNames
public Enumeration<String> getInitParameterNames()
- Specified by:
getInitParameterNamesin interfaceServletContext
setInitParameter
public boolean setInitParameter(String name, String value)
- Specified by:
setInitParameterin interfaceServletContext
addInitParameter
public void addInitParameter(String name, String value)
getAttribute
public Object getAttribute(String name)
- Specified by:
getAttributein interfaceServletContext
getAttributeNames
public Enumeration<String> getAttributeNames()
- Specified by:
getAttributeNamesin interfaceServletContext
setAttribute
public void setAttribute(String name, Object value)
- Specified by:
setAttributein interfaceServletContext
removeAttribute
public void removeAttribute(String name)
- Specified by:
removeAttributein interfaceServletContext
setServletContextName
public void setServletContextName(String servletContextName)
getServletContextName
public String getServletContextName()
- Specified by:
getServletContextNamein interfaceServletContext
getClassLoader
public ClassLoader getClassLoader()
- Specified by:
getClassLoaderin interfaceServletContext
declareRoles
public void declareRoles(String... roleNames)
- Specified by:
declareRolesin interfaceServletContext
getDeclaredRoles
public Set<String> getDeclaredRoles()
setSessionTrackingModes
public void setSessionTrackingModes(Set<SessionTrackingMode> sessionTrackingModes) throws IllegalStateException, IllegalArgumentException
- Specified by:
setSessionTrackingModesin interfaceServletContext- Throws:
IllegalStateExceptionIllegalArgumentException
getDefaultSessionTrackingModes
public Set<SessionTrackingMode> getDefaultSessionTrackingModes()
- Specified by:
getDefaultSessionTrackingModesin interfaceServletContext
getEffectiveSessionTrackingModes
public Set<SessionTrackingMode> getEffectiveSessionTrackingModes()
- Specified by:
getEffectiveSessionTrackingModesin interfaceServletContext
getSessionCookieConfig
public SessionCookieConfig getSessionCookieConfig()
- Specified by:
getSessionCookieConfigin interfaceServletContext
getJspConfigDescriptor
public JspConfigDescriptor getJspConfigDescriptor()
- Specified by:
getJspConfigDescriptorin interfaceServletContext
addServlet
public ServletRegistration.Dynamic addServlet(String servletName, String className)
- Specified by:
addServletin interfaceServletContext
addServlet
public ServletRegistration.Dynamic addServlet(String servletName, Servlet servlet)
- Specified by:
addServletin interfaceServletContext
addServlet
public ServletRegistration.Dynamic addServlet(String servletName, Class<? extends Servlet> servletClass)
- Specified by:
addServletin interfaceServletContext
createServlet
public <T extends Servlet> T createServlet(Class<T> c) throws ServletException
- Specified by:
createServletin interfaceServletContext- Throws:
ServletException
getServletRegistration
public ServletRegistration getServletRegistration(String servletName)
This method always returnsnull.- Specified by:
getServletRegistrationin interfaceServletContext- See Also:
ServletContext.getServletRegistration(java.lang.String)
getServletRegistrations
public Map<String,? extends ServletRegistration> getServletRegistrations()
This method always returns an empty map.- Specified by:
getServletRegistrationsin interfaceServletContext- See Also:
ServletContext.getServletRegistrations()
addFilter
public FilterRegistration.Dynamic addFilter(String filterName, String className)
- Specified by:
addFilterin interfaceServletContext
addFilter
public FilterRegistration.Dynamic addFilter(String filterName, Filter filter)
- Specified by:
addFilterin interfaceServletContext
addFilter
public FilterRegistration.Dynamic addFilter(String filterName, Class<? extends Filter> filterClass)
- Specified by:
addFilterin interfaceServletContext
createFilter
public <T extends Filter> T createFilter(Class<T> c) throws ServletException
- Specified by:
createFilterin interfaceServletContext- Throws:
ServletException
getFilterRegistration
public FilterRegistration getFilterRegistration(String filterName)
This method always returnsnull.- Specified by:
getFilterRegistrationin interfaceServletContext- See Also:
ServletContext.getFilterRegistration(java.lang.String)
getFilterRegistrations
public Map<String,? extends FilterRegistration> getFilterRegistrations()
This method always returns an empty map.- Specified by:
getFilterRegistrationsin interfaceServletContext- See Also:
ServletContext.getFilterRegistrations()
addListener
public void addListener(Class<? extends EventListener> listenerClass)
- Specified by:
addListenerin interfaceServletContext
addListener
public void addListener(String className)
- Specified by:
addListenerin interfaceServletContext
addListener
public <T extends EventListener> void addListener(T t)
- Specified by:
addListenerin interfaceServletContext
createListener
public <T extends EventListener> T createListener(Class<T> c) throws ServletException
- Specified by:
createListenerin interfaceServletContext- Throws:
ServletException