public class MockServletContext extends Object implements ServletContext
Mock implementation of a ServletContext. Provides implementation the most commonly used methods, namely those to manipulate init parameters and attributes. Additional methods are provided to allow the setting of initialization parameters etc.
This mock implementation is meant only for testing purposes. As such there are certain limitations:
| Constructor and Description |
|---|
MockServletContext(String contextName)
Simple constructor that creates a new mock ServletContext with the supplied context name.
|
| Modifier and Type | Method and Description |
|---|---|
void |
acceptRequest(MockHttpServletRequest request,
MockHttpServletResponse response)
Takes a request and response and runs them through the set of filters using a
MockFilterChain, which if everything goes well, will eventually execute the servlet
that is registered with this context.
|
void |
addAllInitParameters(Map<String,String> parameters)
Adds all the values in the supplied Map to the set of init parameters.
|
void |
addFilter(Class<? extends Filter> filterClass,
String filterName,
Map<String,String> initParams)
Adds a filter to the end of filter chain that will be used to filter requests.
|
void |
addInitParameter(String name,
String value)
Adds an init parameter to the mock servlet context.
|
void |
copyCookies(MockHttpServletRequest request,
MockHttpServletResponse response)
Copies cookies from the request to the response.
|
Object |
getAttribute(String name)
Gets an attribute that has been set on the context (i.e.
|
Enumeration<String> |
getAttributeNames()
Returns an enumeration of all the names of attributes in the context.
|
ServletContext |
getContext(String url)
If the url is within this servlet context, returns this.
|
String |
getContextPath()
Servlet 2.3 method.
|
List<Filter> |
getFilters()
Provides access to the set of filters configured for this context.
|
String |
getInitParameter(String name)
Gets the value of an init parameter with the specified name, if one exists.
|
Enumeration<String> |
getInitParameterNames()
Returns an enumeration of all the initialization parameters in the context.
|
int |
getMajorVersion()
Always returns 2.
|
String |
getMimeType(String file)
Always returns null (i.e.
|
int |
getMinorVersion()
Always returns 4.
|
RequestDispatcher |
getNamedDispatcher(String name)
Returns a MockRequestDispatcher for the named servlet provided.
|
String |
getRealPath(String string)
Always returns null as this is standard behaviour for WAR resources.
|
RequestDispatcher |
getRequestDispatcher(String url)
Returns a MockRequestDispatcher for the url provided.
|
URL |
getResource(String name)
Uses the current classloader to fetch the resource if it can.
|
InputStream |
getResourceAsStream(String name)
Uses the current classloader to fetch the resource if it can.
|
Set<?> |
getResourcePaths(String path)
Always returns null (i.e.
|
String |
getServerInfo()
Returns a version string identifying the Mock implementation.
|
Servlet |
getServlet(String string)
Deprecated method always returns null.
|
String |
getServletContextName()
Returns the name of the mock context.
|
Enumeration<?> |
getServletNames()
Deprecated method always returns an empty enumeration.
|
Enumeration<?> |
getServlets()
Deprecated method always returns an empty enumeration.
|
void |
log(Exception exception,
String message)
Logs the message and exception to System.out.
|
void |
log(String message)
Logs the message to System.out.
|
void |
log(String message,
Throwable throwable)
Logs the message and exception to System.out.
|
void |
removeAttribute(String name)
Removes the named attribute from the context.
|
void |
setAttribute(String name,
Object value)
Sets the supplied value for the attribute on the context.
|
void |
setServlet(Class<? extends HttpServlet> servletClass,
String servletName,
Map<String,String> initParams)
Sets the servlet that will receive all requests in this servlet context.
|
public MockServletContext(String contextName)
public ServletContext getContext(String url)
getContext in interface ServletContextpublic String getContextPath()
getContextPath in interface ServletContextpublic int getMajorVersion()
getMajorVersion in interface ServletContextpublic int getMinorVersion()
getMinorVersion in interface ServletContextpublic String getMimeType(String file)
getMimeType in interface ServletContextpublic Set<?> getResourcePaths(String path)
getResourcePaths in interface ServletContextpublic URL getResource(String name) throws MalformedURLException
getResource in interface ServletContextMalformedURLExceptionpublic InputStream getResourceAsStream(String name)
getResourceAsStream in interface ServletContextpublic RequestDispatcher getRequestDispatcher(String url)
getRequestDispatcher in interface ServletContextpublic RequestDispatcher getNamedDispatcher(String name)
getNamedDispatcher in interface ServletContextpublic Servlet getServlet(String string) throws ServletException
getServlet in interface ServletContextServletExceptionpublic Enumeration<?> getServlets()
getServlets in interface ServletContextpublic Enumeration<?> getServletNames()
getServletNames in interface ServletContextpublic void log(String message)
log in interface ServletContextpublic void log(Exception exception, String message)
log in interface ServletContextpublic void log(String message, Throwable throwable)
log in interface ServletContextpublic String getRealPath(String string)
getRealPath in interface ServletContextpublic String getServerInfo()
getServerInfo in interface ServletContextpublic void addInitParameter(String name, String value)
public void addAllInitParameters(Map<String,String> parameters)
public String getInitParameter(String name)
getInitParameter in interface ServletContextpublic Enumeration<String> getInitParameterNames()
getInitParameterNames in interface ServletContextpublic Object getAttribute(String name)
getAttribute in interface ServletContextpublic Enumeration<String> getAttributeNames()
getAttributeNames in interface ServletContextpublic void setAttribute(String name, Object value)
setAttribute in interface ServletContextpublic void removeAttribute(String name)
removeAttribute in interface ServletContextpublic String getServletContextName()
getServletContextName in interface ServletContextpublic void addFilter(Class<? extends Filter> filterClass, String filterName, Map<String,String> initParams)
public List<Filter> getFilters()
public void setServlet(Class<? extends HttpServlet> servletClass, String servletName, Map<String,String> initParams)
public void acceptRequest(MockHttpServletRequest request, MockHttpServletResponse response) throws Exception
Takes a request and response and runs them through the set of filters using a MockFilterChain, which if everything goes well, will eventually execute the servlet that is registered with this context.
Any exceptions that are raised during the processing of the request are simply passed through to the caller. I.e. they will be thrown from this method.
Exceptionpublic void copyCookies(MockHttpServletRequest request, MockHttpServletResponse response)
request - The request.response - The response.© Copyright 2005-2006, Stripes Development Team.