类 MockServletContext
- java.lang.Object
- org.springframework.mock.web.MockServletContext
- 所有已实现的接口:
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
).- 从以下版本开始:
- 1.0.2
- 作者:
- Rod Johnson, Juergen Hoeller, Sam Brannen
- 另请参阅:
MockServletContext(org.springframework.core.io.ResourceLoader)
,AnnotationConfigWebApplicationContext
,XmlWebApplicationContext
,GenericWebApplicationContext
,ClassPathXmlApplicationContext
,FileSystemXmlApplicationContext
字段概要
从接口继承的字段 javax.servlet.ServletContext
ORDERED_LIBS, TEMPDIR
构造器概要
构造器 构造器 说明 MockServletContext()
Create a newMockServletContext
, using no base path and aDefaultResourceLoader
(i.e. the classpath root as WAR root).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.
方法概要
从类继承的方法 java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
从接口继承的方法 javax.servlet.ServletContext
getVirtualServerName
构造器详细资料
MockServletContext
public MockServletContext()
Create a newMockServletContext
, using no base path and aDefaultResourceLoader
(i.e. the classpath root as WAR root).
MockServletContext
public MockServletContext(String resourceBasePath)
Create a newMockServletContext
, using aDefaultResourceLoader
.- 参数:
resourceBasePath
- the root directory of the WAR (should not end with a slash)- 另请参阅:
DefaultResourceLoader
MockServletContext
public MockServletContext(ResourceLoader resourceLoader)
Create a newMockServletContext
, using the specifiedResourceLoader
and no base path.- 参数:
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'.- 参数:
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)
方法详细资料
getResourceLocation
protected String getResourceLocation(String path)
Build a full resource location for the given path, prepending the resource base path of thisMockServletContext
.- 参数:
path
- the path as specified- 返回:
- the full resource path
setContextPath
public void setContextPath(String contextPath)
getContextPath
public String getContextPath()
- 指定者:
getContextPath
在接口中ServletContext
registerContext
public void registerContext(String contextPath, ServletContext context)
getContext
public ServletContext getContext(String contextPath)
- 指定者:
getContext
在接口中ServletContext
setMajorVersion
public void setMajorVersion(int majorVersion)
getMajorVersion
public int getMajorVersion()
- 指定者:
getMajorVersion
在接口中ServletContext
setMinorVersion
public void setMinorVersion(int minorVersion)
getMinorVersion
public int getMinorVersion()
- 指定者:
getMinorVersion
在接口中ServletContext
setEffectiveMajorVersion
public void setEffectiveMajorVersion(int effectiveMajorVersion)
getEffectiveMajorVersion
public int getEffectiveMajorVersion()
- 指定者:
getEffectiveMajorVersion
在接口中ServletContext
setEffectiveMinorVersion
public void setEffectiveMinorVersion(int effectiveMinorVersion)
getEffectiveMinorVersion
public int getEffectiveMinorVersion()
- 指定者:
getEffectiveMinorVersion
在接口中ServletContext
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");
- 指定者:
getMimeType
在接口中ServletContext
getResourcePaths
public Set<String> getResourcePaths(String path)
- 指定者:
getResourcePaths
在接口中ServletContext
getResource
public URL getResource(String path) throws MalformedURLException
- 指定者:
getResource
在接口中ServletContext
- 抛出:
MalformedURLException
getResourceAsStream
public InputStream getResourceAsStream(String path)
- 指定者:
getResourceAsStream
在接口中ServletContext
getRequestDispatcher
public RequestDispatcher getRequestDispatcher(String path)
- 指定者:
getRequestDispatcher
在接口中ServletContext
getNamedDispatcher
public RequestDispatcher getNamedDispatcher(String path)
- 指定者:
getNamedDispatcher
在接口中ServletContext
registerNamedDispatcher
public void registerNamedDispatcher(String name, RequestDispatcher requestDispatcher)
Register aRequestDispatcher
(typically aMockRequestDispatcher
) that acts as a wrapper for the named Servlet.- 参数:
name
- the name of the wrapped ServletrequestDispatcher
- the dispatcher that wraps the named Servlet- 另请参阅:
getNamedDispatcher(java.lang.String)
,unregisterNamedDispatcher(java.lang.String)
unregisterNamedDispatcher
public void unregisterNamedDispatcher(String name)
Unregister theRequestDispatcher
with the given name.- 参数:
name
- the name of the dispatcher to unregister- 另请参阅:
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
.- 参数:
defaultServletName
- the name of the defaultServlet
; nevernull
or empty- 另请参阅:
getDefaultServletName()
getServlet
@Deprecated public Servlet getServlet(String name)
已过时。- 指定者:
getServlet
在接口中ServletContext
getServlets
@Deprecated public Enumeration<Servlet> getServlets()
已过时。- 指定者:
getServlets
在接口中ServletContext
getServletNames
@Deprecated public Enumeration<String> getServletNames()
已过时。- 指定者:
getServletNames
在接口中ServletContext
log
public void log(String message)
- 指定者:
log
在接口中ServletContext
log
@Deprecated public void log(Exception ex, String message)
已过时。- 指定者:
log
在接口中ServletContext
log
public void log(String message, Throwable ex)
- 指定者:
log
在接口中ServletContext
getRealPath
public String getRealPath(String path)
- 指定者:
getRealPath
在接口中ServletContext
getServerInfo
public String getServerInfo()
- 指定者:
getServerInfo
在接口中ServletContext
getInitParameter
public String getInitParameter(String name)
- 指定者:
getInitParameter
在接口中ServletContext
getInitParameterNames
public Enumeration<String> getInitParameterNames()
- 指定者:
getInitParameterNames
在接口中ServletContext
setInitParameter
public boolean setInitParameter(String name, String value)
- 指定者:
setInitParameter
在接口中ServletContext
addInitParameter
public void addInitParameter(String name, String value)
getAttribute
public Object getAttribute(String name)
- 指定者:
getAttribute
在接口中ServletContext
getAttributeNames
public Enumeration<String> getAttributeNames()
- 指定者:
getAttributeNames
在接口中ServletContext
setAttribute
public void setAttribute(String name, Object value)
- 指定者:
setAttribute
在接口中ServletContext
removeAttribute
public void removeAttribute(String name)
- 指定者:
removeAttribute
在接口中ServletContext
setServletContextName
public void setServletContextName(String servletContextName)
getServletContextName
public String getServletContextName()
- 指定者:
getServletContextName
在接口中ServletContext
getClassLoader
public ClassLoader getClassLoader()
- 指定者:
getClassLoader
在接口中ServletContext
declareRoles
public void declareRoles(String... roleNames)
- 指定者:
declareRoles
在接口中ServletContext
getDeclaredRoles
public Set<String> getDeclaredRoles()
setSessionTrackingModes
public void setSessionTrackingModes(Set<SessionTrackingMode> sessionTrackingModes) throws IllegalStateException, IllegalArgumentException
getDefaultSessionTrackingModes
public Set<SessionTrackingMode> getDefaultSessionTrackingModes()
getEffectiveSessionTrackingModes
public Set<SessionTrackingMode> getEffectiveSessionTrackingModes()
getSessionCookieConfig
public SessionCookieConfig getSessionCookieConfig()
- 指定者:
getSessionCookieConfig
在接口中ServletContext
getJspConfigDescriptor
public JspConfigDescriptor getJspConfigDescriptor()
- 指定者:
getJspConfigDescriptor
在接口中ServletContext
addServlet
public ServletRegistration.Dynamic addServlet(String servletName, String className)
- 指定者:
addServlet
在接口中ServletContext
addServlet
public ServletRegistration.Dynamic addServlet(String servletName, Servlet servlet)
- 指定者:
addServlet
在接口中ServletContext
addServlet
public ServletRegistration.Dynamic addServlet(String servletName, Class<? extends Servlet> servletClass)
- 指定者:
addServlet
在接口中ServletContext
createServlet
public <T extends Servlet> T createServlet(Class<T> c) throws ServletException
- 指定者:
createServlet
在接口中ServletContext
- 抛出:
ServletException
getServletRegistration
public ServletRegistration getServletRegistration(String servletName)
This method always returnsnull
.
getServletRegistrations
public Map<String,? extends ServletRegistration> getServletRegistrations()
This method always returns an empty map.
addFilter
public FilterRegistration.Dynamic addFilter(String filterName, String className)
- 指定者:
addFilter
在接口中ServletContext
addFilter
public FilterRegistration.Dynamic addFilter(String filterName, Filter filter)
- 指定者:
addFilter
在接口中ServletContext
addFilter
public FilterRegistration.Dynamic addFilter(String filterName, Class<? extends Filter> filterClass)
- 指定者:
addFilter
在接口中ServletContext
createFilter
public <T extends Filter> T createFilter(Class<T> c) throws ServletException
- 指定者:
createFilter
在接口中ServletContext
- 抛出:
ServletException
getFilterRegistration
public FilterRegistration getFilterRegistration(String filterName)
This method always returnsnull
.
getFilterRegistrations
public Map<String,? extends FilterRegistration> getFilterRegistrations()
This method always returns an empty map.
addListener
public void addListener(Class<? extends EventListener> listenerClass)
- 指定者:
addListener
在接口中ServletContext
addListener
public void addListener(String className)
- 指定者:
addListener
在接口中ServletContext
addListener
public <T extends EventListener> void addListener(T t)
- 指定者:
addListener
在接口中ServletContext
createListener
public <T extends EventListener> T createListener(Class<T> c) throws ServletException
- 指定者:
createListener
在接口中ServletContext
- 抛出:
ServletException