public class MockServletContext extends Object implements ServletContext
ServletContext
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
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 AnnotationConfigWebApplicationContext
,
XmlWebApplicationContext
, or GenericWebApplicationContext
,
passing in an appropriate MockServletContext
instance. You might want
to configure your MockServletContext
with a FileSystemResourceLoader
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) and XmlWebApplicationContext
with an underlying
MockServletContext
(as long as the MockServletContext
has been
configured with a FileSystemResourceLoader
).
MockServletContext(org.springframework.core.io.ResourceLoader)
,
AnnotationConfigWebApplicationContext
,
XmlWebApplicationContext
,
GenericWebApplicationContext
,
ClassPathXmlApplicationContext
,
FileSystemXmlApplicationContext
ORDERED_LIBS, TEMPDIR
Constructor and Description |
---|
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(String resourceBasePath)
Create a new
MockServletContext , using a DefaultResourceLoader . |
MockServletContext(String resourceBasePath,
ResourceLoader resourceLoader)
Create a new
MockServletContext using the supplied resource base
path and resource loader. |
public MockServletContext()
MockServletContext
, using no base path and a
DefaultResourceLoader
(i.e. the classpath root as WAR root).DefaultResourceLoader
public MockServletContext(String resourceBasePath)
MockServletContext
, using a DefaultResourceLoader
.resourceBasePath
- the root directory of the WAR (should not end with a slash)DefaultResourceLoader
public MockServletContext(ResourceLoader resourceLoader)
MockServletContext
, using the specified ResourceLoader
and no base path.resourceLoader
- the ResourceLoader to use (or null for the default)public MockServletContext(String resourceBasePath, ResourceLoader resourceLoader)
MockServletContext
using the supplied resource base
path and resource loader.
Registers a MockRequestDispatcher
for the Servlet named
'default'.
resourceBasePath
- the root directory of the WAR (should not end with a slash)resourceLoader
- the ResourceLoader to use (or null for the default)registerNamedDispatcher(java.lang.String, javax.servlet.RequestDispatcher)
protected String getResourceLocation(String path)
MockServletContext
.path
- the path as specifiedpublic void setContextPath(String contextPath)
public String getContextPath()
getContextPath
in interface ServletContext
public void registerContext(String contextPath, ServletContext context)
public ServletContext getContext(String contextPath)
getContext
in interface ServletContext
public void setMajorVersion(int majorVersion)
public int getMajorVersion()
getMajorVersion
in interface ServletContext
public void setMinorVersion(int minorVersion)
public int getMinorVersion()
getMinorVersion
in interface ServletContext
public void setEffectiveMajorVersion(int effectiveMajorVersion)
public int getEffectiveMajorVersion()
getEffectiveMajorVersion
in interface ServletContext
public void setEffectiveMinorVersion(int effectiveMinorVersion)
public int getEffectiveMinorVersion()
getEffectiveMinorVersion
in interface ServletContext
public String getMimeType(String filePath)
FileTypeMap
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 returns null
). Thus, in
order to honor the ServletContext.getMimeType(String)
contract,
this method returns null
if the MIME type is
"application/octet-stream"
.
MockServletContext
does not provide a direct mechanism for
setting a custom MIME type; however, if the default FileTypeMap
is an instance of javax.activation.MimetypesFileTypeMap
, a custom
MIME type named text/enigma
can be registered for a custom
.puzzle
file extension in the following manner:
MimetypesFileTypeMap mimetypesFileTypeMap = (MimetypesFileTypeMap) FileTypeMap.getDefaultFileTypeMap(); mimetypesFileTypeMap.addMimeTypes("text/enigma puzzle");
getMimeType
in interface ServletContext
public Set<String> getResourcePaths(String path)
getResourcePaths
in interface ServletContext
public URL getResource(String path) throws MalformedURLException
getResource
in interface ServletContext
MalformedURLException
public InputStream getResourceAsStream(String path)
getResourceAsStream
in interface ServletContext
public RequestDispatcher getRequestDispatcher(String path)
getRequestDispatcher
in interface ServletContext
public RequestDispatcher getNamedDispatcher(String path)
getNamedDispatcher
in interface ServletContext
public void registerNamedDispatcher(String name, RequestDispatcher requestDispatcher)
RequestDispatcher
(typically a MockRequestDispatcher
)
that acts as a wrapper for the named Servlet.name
- the name of the wrapped ServletrequestDispatcher
- the dispatcher that wraps the named ServletgetNamedDispatcher(java.lang.String)
,
unregisterNamedDispatcher(java.lang.String)
public void unregisterNamedDispatcher(String name)
RequestDispatcher
with the given name.name
- the name of the dispatcher to unregistergetNamedDispatcher(java.lang.String)
,
registerNamedDispatcher(java.lang.String, javax.servlet.RequestDispatcher)
public String getDefaultServletName()
Servlet
.
Defaults to 'default'.
setDefaultServletName(java.lang.String)
public void setDefaultServletName(String defaultServletName)
Servlet
.
Also unregisters
the current default
RequestDispatcher
and replaces
it with a MockRequestDispatcher
for the provided
defaultServletName
.
defaultServletName
- the name of the default Servlet
;
never null
or emptygetDefaultServletName()
@Deprecated public Servlet getServlet(String name)
getServlet
in interface ServletContext
@Deprecated public Enumeration<Servlet> getServlets()
getServlets
in interface ServletContext
@Deprecated public Enumeration<String> getServletNames()
getServletNames
in interface ServletContext
public void log(String message)
log
in interface ServletContext
@Deprecated public void log(Exception ex, String message)
log
in interface ServletContext
public void log(String message, Throwable ex)
log
in interface ServletContext
public String getRealPath(String path)
getRealPath
in interface ServletContext
public String getServerInfo()
getServerInfo
in interface ServletContext
public String getInitParameter(String name)
getInitParameter
in interface ServletContext
public Enumeration<String> getInitParameterNames()
getInitParameterNames
in interface ServletContext
public boolean setInitParameter(String name, String value)
setInitParameter
in interface ServletContext
public Object getAttribute(String name)
getAttribute
in interface ServletContext
public Enumeration<String> getAttributeNames()
getAttributeNames
in interface ServletContext
public void setAttribute(String name, Object value)
setAttribute
in interface ServletContext
public void removeAttribute(String name)
removeAttribute
in interface ServletContext
public void setServletContextName(String servletContextName)
public String getServletContextName()
getServletContextName
in interface ServletContext
public ClassLoader getClassLoader()
getClassLoader
in interface ServletContext
public void declareRoles(String... roleNames)
declareRoles
in interface ServletContext
public void setSessionTrackingModes(Set<SessionTrackingMode> sessionTrackingModes) throws IllegalStateException, IllegalArgumentException
setSessionTrackingModes
in interface ServletContext
IllegalStateException
IllegalArgumentException
public Set<SessionTrackingMode> getDefaultSessionTrackingModes()
getDefaultSessionTrackingModes
in interface ServletContext
public Set<SessionTrackingMode> getEffectiveSessionTrackingModes()
getEffectiveSessionTrackingModes
in interface ServletContext
public SessionCookieConfig getSessionCookieConfig()
getSessionCookieConfig
in interface ServletContext
public JspConfigDescriptor getJspConfigDescriptor()
getJspConfigDescriptor
in interface ServletContext
public ServletRegistration.Dynamic addServlet(String servletName, String className)
addServlet
in interface ServletContext
public ServletRegistration.Dynamic addServlet(String servletName, Servlet servlet)
addServlet
in interface ServletContext
public ServletRegistration.Dynamic addServlet(String servletName, Class<? extends Servlet> servletClass)
addServlet
in interface ServletContext
public <T extends Servlet> T createServlet(Class<T> c) throws ServletException
createServlet
in interface ServletContext
ServletException
public ServletRegistration getServletRegistration(String servletName)
getServletRegistration
in interface ServletContext
public Map<String,? extends ServletRegistration> getServletRegistrations()
getServletRegistrations
in interface ServletContext
public FilterRegistration.Dynamic addFilter(String filterName, String className)
addFilter
in interface ServletContext
public FilterRegistration.Dynamic addFilter(String filterName, Filter filter)
addFilter
in interface ServletContext
public FilterRegistration.Dynamic addFilter(String filterName, Class<? extends Filter> filterClass)
addFilter
in interface ServletContext
public <T extends Filter> T createFilter(Class<T> c) throws ServletException
createFilter
in interface ServletContext
ServletException
public FilterRegistration getFilterRegistration(String filterName)
getFilterRegistration
in interface ServletContext
public Map<String,? extends FilterRegistration> getFilterRegistrations()
getFilterRegistrations
in interface ServletContext
public void addListener(Class<? extends EventListener> listenerClass)
addListener
in interface ServletContext
public void addListener(String className)
addListener
in interface ServletContext
public <T extends EventListener> void addListener(T t)
addListener
in interface ServletContext
public <T extends EventListener> T createListener(Class<T> c) throws ServletException
createListener
in interface ServletContext
ServletException