public class MockServletContext
extends java.lang.Object
javax.servlet.ServletContext
interface.
As of Spring 5.0, this set of mocks is designed on a Servlet 4.0 baseline.
Compatible with Servlet 3.1 but can be configured to expose a specific version
through setMajorVersion(int)
/setMinorVersion(int)
; default is 3.1.
Note that Servlet 3.1 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.
For setting up a full WebApplicationContext
in a test environment, you can
use AnnotationConfigWebApplicationContext
, XmlWebApplicationContext
,
or GenericWebApplicationContext
, passing in a corresponding
MockServletContext
instance. Consider configuring your
MockServletContext
with a FileSystemResourceLoader
in order to
interpret resource paths as relative filesystem locations.
MockServletContext(org.springframework.core.io.ResourceLoader)
,
AnnotationConfigWebApplicationContext
,
XmlWebApplicationContext
,
GenericWebApplicationContext
Modifier and Type | Field and Description |
---|---|
private java.util.Map<java.lang.String,java.lang.Object> |
attributes |
private static java.lang.String |
COMMON_DEFAULT_SERVLET_NAME
Default Servlet name used by Tomcat, Jetty, JBoss, and GlassFish: "default"
|
private java.lang.String |
contextPath |
private java.util.Map<java.lang.String,ServletContext> |
contexts |
private java.util.Set<java.lang.String> |
declaredRoles |
private static java.util.Set<SessionTrackingMode> |
DEFAULT_SESSION_TRACKING_MODES |
private java.lang.String |
defaultServletName |
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.util.Map<java.lang.String,RequestDispatcher> |
namedRequestDispatchers |
private java.lang.String |
resourceBasePath |
private ResourceLoader |
resourceLoader |
private java.lang.String |
servletContextName |
private SessionCookieConfig |
sessionCookieConfig |
private int |
sessionTimeout |
private java.util.Set<SessionTrackingMode> |
sessionTrackingModes |
private static java.lang.String |
TEMP_DIR_SYSTEM_PROPERTY |
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(java.lang.String resourceBasePath)
Create a new
MockServletContext , using a DefaultResourceLoader . |
MockServletContext(java.lang.String resourceBasePath,
ResourceLoader resourceLoader)
Create a new
MockServletContext using the supplied resource base
path and resource loader. |
Modifier and Type | Method and Description |
---|---|
FilterRegistration.Dynamic |
addFilter(java.lang.String filterName,
java.lang.Class<? extends Filter> filterClass) |
FilterRegistration.Dynamic |
addFilter(java.lang.String filterName,
Filter filter) |
FilterRegistration.Dynamic |
addFilter(java.lang.String filterName,
java.lang.String className) |
void |
addInitParameter(java.lang.String name,
java.lang.String value) |
ServletRegistration.Dynamic |
addJspFile(java.lang.String servletName,
java.lang.String jspFile) |
void |
addListener(java.lang.Class<? extends java.util.EventListener> listenerClass) |
void |
addListener(java.lang.String className) |
<T extends java.util.EventListener> |
addListener(T t) |
ServletRegistration.Dynamic |
addServlet(java.lang.String servletName,
java.lang.Class<? extends Servlet> servletClass) |
ServletRegistration.Dynamic |
addServlet(java.lang.String servletName,
Servlet servlet) |
ServletRegistration.Dynamic |
addServlet(java.lang.String servletName,
java.lang.String className) |
<T extends Filter> |
createFilter(java.lang.Class<T> c) |
<T extends java.util.EventListener> |
createListener(java.lang.Class<T> c) |
<T extends Servlet> |
createServlet(java.lang.Class<T> c) |
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() |
java.lang.String |
getDefaultServletName()
Get the name of the default
Servlet . |
java.util.Set<SessionTrackingMode> |
getDefaultSessionTrackingModes() |
int |
getEffectiveMajorVersion() |
int |
getEffectiveMinorVersion() |
java.util.Set<SessionTrackingMode> |
getEffectiveSessionTrackingModes() |
FilterRegistration |
getFilterRegistration(java.lang.String filterName)
This method always returns
null . |
java.util.Map<java.lang.String,? extends FilterRegistration> |
getFilterRegistrations()
This method always returns an empty map.
|
java.lang.String |
getInitParameter(java.lang.String name) |
java.util.Enumeration<java.lang.String> |
getInitParameterNames() |
JspConfigDescriptor |
getJspConfigDescriptor() |
int |
getMajorVersion() |
java.lang.String |
getMimeType(java.lang.String filePath)
This method uses the default
FileTypeMap
from the Java Activation Framework to resolve MIME types. |
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)
Deprecated.
|
java.lang.String |
getServletContextName() |
java.util.Enumeration<java.lang.String> |
getServletNames()
Deprecated.
|
ServletRegistration |
getServletRegistration(java.lang.String servletName)
This method always returns
null . |
java.util.Map<java.lang.String,? extends ServletRegistration> |
getServletRegistrations()
This method always returns an empty map.
|
java.util.Enumeration<Servlet> |
getServlets()
Deprecated.
|
SessionCookieConfig |
getSessionCookieConfig() |
int |
getSessionTimeout() |
java.lang.String |
getVirtualServerName() |
void |
log(java.lang.Exception ex,
java.lang.String message)
Deprecated.
|
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 |
registerNamedDispatcher(java.lang.String name,
RequestDispatcher requestDispatcher)
Register a
RequestDispatcher (typically a MockRequestDispatcher )
that acts as a wrapper for the named Servlet. |
void |
removeAttribute(java.lang.String name) |
void |
setAttribute(java.lang.String name,
java.lang.Object value) |
void |
setContextPath(java.lang.String contextPath) |
void |
setDefaultServletName(java.lang.String defaultServletName)
Set the name of the default
Servlet . |
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) |
void |
setSessionTimeout(int sessionTimeout) |
void |
setSessionTrackingModes(java.util.Set<SessionTrackingMode> sessionTrackingModes) |
void |
unregisterNamedDispatcher(java.lang.String name)
Unregister the
RequestDispatcher with the given name. |
private static final java.lang.String COMMON_DEFAULT_SERVLET_NAME
private static final java.lang.String TEMP_DIR_SYSTEM_PROPERTY
private static final java.util.Set<SessionTrackingMode> DEFAULT_SESSION_TRACKING_MODES
private final Log logger
private final ResourceLoader resourceLoader
private final java.lang.String resourceBasePath
private java.lang.String contextPath
private final java.util.Map<java.lang.String,ServletContext> contexts
private int majorVersion
private int minorVersion
private int effectiveMajorVersion
private int effectiveMinorVersion
private final java.util.Map<java.lang.String,RequestDispatcher> namedRequestDispatchers
private java.lang.String defaultServletName
private final java.util.Map<java.lang.String,java.lang.String> initParameters
private final java.util.Map<java.lang.String,java.lang.Object> attributes
private java.lang.String servletContextName
private final java.util.Set<java.lang.String> declaredRoles
private java.util.Set<SessionTrackingMode> sessionTrackingModes
private final SessionCookieConfig sessionCookieConfig
private int sessionTimeout
public MockServletContext()
MockServletContext
, using no base path and a
DefaultResourceLoader
(i.e. the classpath root as WAR root).DefaultResourceLoader
public MockServletContext(java.lang.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(java.lang.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, RequestDispatcher)
protected java.lang.String getResourceLocation(java.lang.String path)
MockServletContext
.path
- the path as specifiedpublic void setContextPath(java.lang.String contextPath)
public java.lang.String getContextPath()
public void registerContext(java.lang.String contextPath, ServletContext context)
public ServletContext getContext(java.lang.String contextPath)
public void setMajorVersion(int majorVersion)
public int getMajorVersion()
public void setMinorVersion(int minorVersion)
public int getMinorVersion()
public void setEffectiveMajorVersion(int effectiveMajorVersion)
public int getEffectiveMajorVersion()
public void setEffectiveMinorVersion(int effectiveMinorVersion)
public int getEffectiveMinorVersion()
public java.lang.String getMimeType(java.lang.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");
public java.util.Set<java.lang.String> getResourcePaths(java.lang.String path)
public java.net.URL getResource(java.lang.String path) throws java.net.MalformedURLException
java.net.MalformedURLException
public java.io.InputStream getResourceAsStream(java.lang.String path)
public RequestDispatcher getRequestDispatcher(java.lang.String path)
public RequestDispatcher getNamedDispatcher(java.lang.String path)
public void registerNamedDispatcher(java.lang.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(java.lang.String name)
RequestDispatcher
with the given name.name
- the name of the dispatcher to unregistergetNamedDispatcher(java.lang.String)
,
registerNamedDispatcher(java.lang.String, RequestDispatcher)
public java.lang.String getDefaultServletName()
Servlet
.
Defaults to 'default'.
setDefaultServletName(java.lang.String)
public void setDefaultServletName(java.lang.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(java.lang.String name)
@Deprecated public java.util.Enumeration<Servlet> getServlets()
@Deprecated public java.util.Enumeration<java.lang.String> getServletNames()
public void log(java.lang.String message)
@Deprecated public void log(java.lang.Exception ex, java.lang.String message)
public void log(java.lang.String message, java.lang.Throwable ex)
public java.lang.String getRealPath(java.lang.String path)
public java.lang.String getServerInfo()
public java.lang.String getInitParameter(java.lang.String name)
public java.util.Enumeration<java.lang.String> getInitParameterNames()
public boolean setInitParameter(java.lang.String name, java.lang.String value)
public void addInitParameter(java.lang.String name, java.lang.String value)
public java.lang.Object getAttribute(java.lang.String name)
public java.util.Enumeration<java.lang.String> getAttributeNames()
public void setAttribute(java.lang.String name, java.lang.Object value)
public void removeAttribute(java.lang.String name)
public void setServletContextName(java.lang.String servletContextName)
public java.lang.String getServletContextName()
public java.lang.ClassLoader getClassLoader()
public void declareRoles(java.lang.String... roleNames)
public java.util.Set<java.lang.String> getDeclaredRoles()
public void setSessionTrackingModes(java.util.Set<SessionTrackingMode> sessionTrackingModes) throws java.lang.IllegalStateException, java.lang.IllegalArgumentException
java.lang.IllegalStateException
java.lang.IllegalArgumentException
public java.util.Set<SessionTrackingMode> getDefaultSessionTrackingModes()
public java.util.Set<SessionTrackingMode> getEffectiveSessionTrackingModes()
public SessionCookieConfig getSessionCookieConfig()
public void setSessionTimeout(int sessionTimeout)
public int getSessionTimeout()
public JspConfigDescriptor getJspConfigDescriptor()
public ServletRegistration.Dynamic addJspFile(java.lang.String servletName, java.lang.String jspFile)
public ServletRegistration.Dynamic addServlet(java.lang.String servletName, java.lang.String className)
public ServletRegistration.Dynamic addServlet(java.lang.String servletName, Servlet servlet)
public ServletRegistration.Dynamic addServlet(java.lang.String servletName, java.lang.Class<? extends Servlet> servletClass)
public <T extends Servlet> T createServlet(java.lang.Class<T> c) throws ServletException
ServletException
public ServletRegistration getServletRegistration(java.lang.String servletName)
null
.javax.servlet.ServletContext#getServletRegistration(java.lang.String)
public java.util.Map<java.lang.String,? extends ServletRegistration> getServletRegistrations()
javax.servlet.ServletContext#getServletRegistrations()
public FilterRegistration.Dynamic addFilter(java.lang.String filterName, java.lang.String className)
public FilterRegistration.Dynamic addFilter(java.lang.String filterName, Filter filter)
public FilterRegistration.Dynamic addFilter(java.lang.String filterName, java.lang.Class<? extends Filter> filterClass)
public <T extends Filter> T createFilter(java.lang.Class<T> c) throws ServletException
ServletException
public FilterRegistration getFilterRegistration(java.lang.String filterName)
null
.javax.servlet.ServletContext#getFilterRegistration(java.lang.String)
public java.util.Map<java.lang.String,? extends FilterRegistration> getFilterRegistrations()
javax.servlet.ServletContext#getFilterRegistrations()
public void addListener(java.lang.Class<? extends java.util.EventListener> listenerClass)
public void addListener(java.lang.String className)
public <T extends java.util.EventListener> void addListener(T t)
public <T extends java.util.EventListener> T createListener(java.lang.Class<T> c) throws ServletException
ServletException
public java.lang.String getVirtualServerName()