|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.springframework.mock.web.MockServletContext
public class MockServletContext
Mock implementation of the javax.servlet.ServletContext
interface.
Compatible with Servlet 2.5 and partially with Servlet 3.0. Can be configured to
expose a specific version through setMajorVersion(int)
/setMinorVersion(int)
;
default is 2.5. Note that Servlet 3.0 support is limited: servlet, filter and listener
registration methods are not supported; neither is cookie or 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 XmlWebApplicationContext (or GenericWebApplicationContext), passing in an appropriate MockServletContext instance. You might want to configure your MockServletContext with a FileSystemResourceLoader in that case, to make your resource paths interpreted as relative file system 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 file system) and XmlWebApplicationContext with an underlying MockServletContext (as long as the MockServletContext has been configured with a FileSystemResourceLoader).
MockServletContext(org.springframework.core.io.ResourceLoader)
,
XmlWebApplicationContext
,
GenericWebApplicationContext
,
ClassPathXmlApplicationContext
,
FileSystemXmlApplicationContext
Nested Class Summary | |
---|---|
private static class |
MockServletContext.MimeTypeResolver
Inner factory class used to just introduce a Java Activation Framework dependency when actually asked to resolve a MIME type. |
Field Summary | |
---|---|
private java.util.Map<java.lang.String,java.lang.Object> |
attributes
|
private java.lang.String |
contextPath
|
private java.util.Map<java.lang.String,ServletContext> |
contexts
|
private java.util.Set<java.lang.String> |
declaredRoles
|
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.lang.String |
resourceBasePath
|
private ResourceLoader |
resourceLoader
|
private java.lang.String |
servletContextName
|
private static java.lang.String |
TEMP_DIR_SYSTEM_PROPERTY
|
Constructor Summary | |
---|---|
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. |
Method Summary | |
---|---|
void |
addInitParameter(java.lang.String name,
java.lang.String value)
|
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()
|
int |
getEffectiveMajorVersion()
|
int |
getEffectiveMinorVersion()
|
java.lang.String |
getInitParameter(java.lang.String name)
|
java.util.Enumeration<java.lang.String> |
getInitParameterNames()
|
int |
getMajorVersion()
|
java.lang.String |
getMimeType(java.lang.String filePath)
|
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)
|
java.lang.String |
getServletContextName()
|
java.util.Enumeration<java.lang.String> |
getServletNames()
|
java.util.Enumeration<Servlet> |
getServlets()
|
void |
log(java.lang.Exception ex,
java.lang.String message)
|
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 |
removeAttribute(java.lang.String name)
|
void |
setAttribute(java.lang.String name,
java.lang.Object value)
|
void |
setContextPath(java.lang.String contextPath)
|
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)
|
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
private static final java.lang.String TEMP_DIR_SYSTEM_PROPERTY
private final Log logger
private final ResourceLoader resourceLoader
private final java.lang.String resourceBasePath
private java.lang.String contextPath
private int majorVersion
private int minorVersion
private int effectiveMajorVersion
private int effectiveMinorVersion
private final java.util.Map<java.lang.String,ServletContext> contexts
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
Constructor Detail |
---|
public MockServletContext()
DefaultResourceLoader
public MockServletContext(java.lang.String resourceBasePath)
resourceBasePath
- the WAR root directory (should not end with a slash)DefaultResourceLoader
public MockServletContext(ResourceLoader resourceLoader)
resourceLoader
- the ResourceLoader to use (or null for the default)public MockServletContext(java.lang.String resourceBasePath, ResourceLoader resourceLoader)
resourceBasePath
- the WAR root directory (should not end with a slash)resourceLoader
- the ResourceLoader to use (or null for the default)Method Detail |
---|
protected java.lang.String getResourceLocation(java.lang.String path)
path
- the path as specified
public 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)
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 Servlet getServlet(java.lang.String name)
public java.util.Enumeration<Servlet> getServlets()
public java.util.Enumeration<java.lang.String> getServletNames()
public void log(java.lang.String message)
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()
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |