public class MockServletContext
extends java.lang.Object
implements javax.servlet.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(java.lang.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(java.util.Map<java.lang.String,java.lang.String> parameters)
Adds all the values in the supplied Map to the set of init parameters.
|
void |
addFilter(java.lang.Class<? extends javax.servlet.Filter> filterClass,
java.lang.String filterName,
java.util.Map<java.lang.String,java.lang.String> initParams)
Adds a filter to the end of filter chain that will be used to filter requests.
|
void |
addInitParameter(java.lang.String name,
java.lang.String value)
Adds an init parameter to the mock servlet context.
|
java.lang.Object |
getAttribute(java.lang.String name)
Gets an attribute that has been set on the context (i.e.
|
java.util.Enumeration<java.lang.String> |
getAttributeNames()
Returns an enumeration of all the names of attributes in the context.
|
javax.servlet.ServletContext |
getContext(java.lang.String url)
If the url is within this servlet context, returns this.
|
java.lang.String |
getContextPath()
Servlet 2.3 method.
|
java.util.List<javax.servlet.Filter> |
getFilters()
Provides access to the set of filters configured for this context.
|
java.lang.String |
getInitParameter(java.lang.String name)
Gets the value of an init parameter with the specified name, if one exists.
|
java.util.Enumeration<java.lang.String> |
getInitParameterNames()
Returns an enumeration of all the initialization parameters in the context.
|
int |
getMajorVersion()
Always returns 2.
|
java.lang.String |
getMimeType(java.lang.String file)
Always returns null (i.e.
|
int |
getMinorVersion()
Always returns 4.
|
javax.servlet.RequestDispatcher |
getNamedDispatcher(java.lang.String name)
Returns a MockRequestDispatcher for the named servlet provided.
|
java.lang.String |
getRealPath(java.lang.String string)
Always returns null as this is standard behaviour for WAR resources.
|
javax.servlet.RequestDispatcher |
getRequestDispatcher(java.lang.String url)
Returns a MockRequestDispatcher for the url provided.
|
java.net.URL |
getResource(java.lang.String name)
Uses the current classloader to fetch the resource if it can.
|
java.io.InputStream |
getResourceAsStream(java.lang.String name)
Uses the current classloader to fetch the resource if it can.
|
java.util.Set<?> |
getResourcePaths(java.lang.String path)
Always returns null (i.e.
|
java.lang.String |
getServerInfo()
Returns a version string identifying the Mock implementation.
|
javax.servlet.Servlet |
getServlet(java.lang.String string)
Deprecated method always returns null.
|
java.lang.String |
getServletContextName()
Returns the name of the mock context.
|
java.util.Enumeration<?> |
getServletNames()
Deprecated method always returns an empty enumeration.
|
java.util.Enumeration<?> |
getServlets()
Deprecated method always returns an empty enumeration.
|
void |
log(java.lang.Exception exception,
java.lang.String message)
Logs the message and exception to System.out.
|
void |
log(java.lang.String message)
Logs the message to System.out.
|
void |
log(java.lang.String message,
java.lang.Throwable throwable)
Logs the message and exception to System.out.
|
void |
removeAttribute(java.lang.String name)
Removes the named attribute from the context.
|
void |
setAttribute(java.lang.String name,
java.lang.Object value)
Sets the supplied value for the attribute on the context.
|
void |
setServlet(java.lang.Class<? extends javax.servlet.http.HttpServlet> servletClass,
java.lang.String servletName,
java.util.Map<java.lang.String,java.lang.String> initParams)
Sets the servlet that will receive all requests in this servlet context.
|
public MockServletContext(java.lang.String contextName)
public javax.servlet.ServletContext getContext(java.lang.String url)
getContext
in interface javax.servlet.ServletContext
public java.lang.String getContextPath()
getContextPath
in interface javax.servlet.ServletContext
public int getMajorVersion()
getMajorVersion
in interface javax.servlet.ServletContext
public int getMinorVersion()
getMinorVersion
in interface javax.servlet.ServletContext
public java.lang.String getMimeType(java.lang.String file)
getMimeType
in interface javax.servlet.ServletContext
public java.util.Set<?> getResourcePaths(java.lang.String path)
getResourcePaths
in interface javax.servlet.ServletContext
public java.net.URL getResource(java.lang.String name) throws java.net.MalformedURLException
getResource
in interface javax.servlet.ServletContext
java.net.MalformedURLException
public java.io.InputStream getResourceAsStream(java.lang.String name)
getResourceAsStream
in interface javax.servlet.ServletContext
public javax.servlet.RequestDispatcher getRequestDispatcher(java.lang.String url)
getRequestDispatcher
in interface javax.servlet.ServletContext
public javax.servlet.RequestDispatcher getNamedDispatcher(java.lang.String name)
getNamedDispatcher
in interface javax.servlet.ServletContext
public javax.servlet.Servlet getServlet(java.lang.String string) throws javax.servlet.ServletException
getServlet
in interface javax.servlet.ServletContext
javax.servlet.ServletException
public java.util.Enumeration<?> getServlets()
getServlets
in interface javax.servlet.ServletContext
public java.util.Enumeration<?> getServletNames()
getServletNames
in interface javax.servlet.ServletContext
public void log(java.lang.String message)
log
in interface javax.servlet.ServletContext
public void log(java.lang.Exception exception, java.lang.String message)
log
in interface javax.servlet.ServletContext
public void log(java.lang.String message, java.lang.Throwable throwable)
log
in interface javax.servlet.ServletContext
public java.lang.String getRealPath(java.lang.String string)
getRealPath
in interface javax.servlet.ServletContext
public java.lang.String getServerInfo()
getServerInfo
in interface javax.servlet.ServletContext
public void addInitParameter(java.lang.String name, java.lang.String value)
public void addAllInitParameters(java.util.Map<java.lang.String,java.lang.String> parameters)
public java.lang.String getInitParameter(java.lang.String name)
getInitParameter
in interface javax.servlet.ServletContext
public java.util.Enumeration<java.lang.String> getInitParameterNames()
getInitParameterNames
in interface javax.servlet.ServletContext
public java.lang.Object getAttribute(java.lang.String name)
getAttribute
in interface javax.servlet.ServletContext
public java.util.Enumeration<java.lang.String> getAttributeNames()
getAttributeNames
in interface javax.servlet.ServletContext
public void setAttribute(java.lang.String name, java.lang.Object value)
setAttribute
in interface javax.servlet.ServletContext
public void removeAttribute(java.lang.String name)
removeAttribute
in interface javax.servlet.ServletContext
public java.lang.String getServletContextName()
getServletContextName
in interface javax.servlet.ServletContext
public void addFilter(java.lang.Class<? extends javax.servlet.Filter> filterClass, java.lang.String filterName, java.util.Map<java.lang.String,java.lang.String> initParams)
public java.util.List<javax.servlet.Filter> getFilters()
public void setServlet(java.lang.Class<? extends javax.servlet.http.HttpServlet> servletClass, java.lang.String servletName, java.util.Map<java.lang.String,java.lang.String> initParams)
public void acceptRequest(MockHttpServletRequest request, MockHttpServletResponse response) throws java.lang.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.
java.lang.Exception
? Copyright 2005-2006, Stripes Development Team.