net.sourceforge.stripes.mock
Class MockServletContext

java.lang.Object
  extended by net.sourceforge.stripes.mock.MockServletContext
All Implemented Interfaces:
ServletContext

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:

Since:
Stripes 1.1.1
Author:
Tim Fennell

Constructor Summary
MockServletContext(String contextName)
          Simple constructor that creates a new mock ServletContext with the supplied context name.
 
Method Summary
 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.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MockServletContext

public MockServletContext(String contextName)
Simple constructor that creates a new mock ServletContext with the supplied context name.

Method Detail

getContext

public ServletContext getContext(String url)
If the url is within this servlet context, returns this. Otherwise returns null.

Specified by:
getContext in interface ServletContext

getContextPath

public String getContextPath()
Servlet 2.3 method. Returns the context name with a leading slash.

Specified by:
getContextPath in interface ServletContext

getMajorVersion

public int getMajorVersion()
Always returns 2.

Specified by:
getMajorVersion in interface ServletContext

getMinorVersion

public int getMinorVersion()
Always returns 4.

Specified by:
getMinorVersion in interface ServletContext

getMimeType

public String getMimeType(String file)
Always returns null (i.e. don't know).

Specified by:
getMimeType in interface ServletContext

getResourcePaths

public Set<?> getResourcePaths(String path)
Always returns null (i.e. there are no resources under this path).

Specified by:
getResourcePaths in interface ServletContext

getResource

public URL getResource(String name)
                throws MalformedURLException
Uses the current classloader to fetch the resource if it can.

Specified by:
getResource in interface ServletContext
Throws:
MalformedURLException

getResourceAsStream

public InputStream getResourceAsStream(String name)
Uses the current classloader to fetch the resource if it can.

Specified by:
getResourceAsStream in interface ServletContext

getRequestDispatcher

public RequestDispatcher getRequestDispatcher(String url)
Returns a MockRequestDispatcher for the url provided.

Specified by:
getRequestDispatcher in interface ServletContext

getNamedDispatcher

public RequestDispatcher getNamedDispatcher(String name)
Returns a MockRequestDispatcher for the named servlet provided.

Specified by:
getNamedDispatcher in interface ServletContext

getServlet

public Servlet getServlet(String string)
                   throws ServletException
Deprecated method always returns null.

Specified by:
getServlet in interface ServletContext
Throws:
ServletException

getServlets

public Enumeration<?> getServlets()
Deprecated method always returns an empty enumeration.

Specified by:
getServlets in interface ServletContext

getServletNames

public Enumeration<?> getServletNames()
Deprecated method always returns an empty enumeration.

Specified by:
getServletNames in interface ServletContext

log

public void log(String message)
Logs the message to System.out.

Specified by:
log in interface ServletContext

log

public void log(Exception exception,
                String message)
Logs the message and exception to System.out.

Specified by:
log in interface ServletContext

log

public void log(String message,
                Throwable throwable)
Logs the message and exception to System.out.

Specified by:
log in interface ServletContext

getRealPath

public String getRealPath(String string)
Always returns null as this is standard behaviour for WAR resources.

Specified by:
getRealPath in interface ServletContext

getServerInfo

public String getServerInfo()
Returns a version string identifying the Mock implementation.

Specified by:
getServerInfo in interface ServletContext

addInitParameter

public void addInitParameter(String name,
                             String value)
Adds an init parameter to the mock servlet context.


addAllInitParameters

public void addAllInitParameters(Map<String,String> parameters)
Adds all the values in the supplied Map to the set of init parameters.


getInitParameter

public String getInitParameter(String name)
Gets the value of an init parameter with the specified name, if one exists.

Specified by:
getInitParameter in interface ServletContext

getInitParameterNames

public Enumeration<String> getInitParameterNames()
Returns an enumeration of all the initialization parameters in the context.

Specified by:
getInitParameterNames in interface ServletContext

getAttribute

public Object getAttribute(String name)
Gets an attribute that has been set on the context (i.e. application) scope.

Specified by:
getAttribute in interface ServletContext

getAttributeNames

public Enumeration<String> getAttributeNames()
Returns an enumeration of all the names of attributes in the context.

Specified by:
getAttributeNames in interface ServletContext

setAttribute

public void setAttribute(String name,
                         Object value)
Sets the supplied value for the attribute on the context.

Specified by:
setAttribute in interface ServletContext

removeAttribute

public void removeAttribute(String name)
Removes the named attribute from the context.

Specified by:
removeAttribute in interface ServletContext

getServletContextName

public String getServletContextName()
Returns the name of the mock context.

Specified by:
getServletContextName in interface ServletContext

addFilter

public 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.


getFilters

public List<Filter> getFilters()
Provides access to the set of filters configured for this context.


setServlet

public void setServlet(Class<? extends HttpServlet> servletClass,
                       String servletName,
                       Map<String,String> initParams)
Sets the servlet that will receive all requests in this servlet context.


acceptRequest

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.

Throws:
Exception

copyCookies

public void copyCookies(MockHttpServletRequest request,
                        MockHttpServletResponse response)
Copies cookies from the request to the response.

Parameters:
request - The request.
response - The response.


© Copyright 2005-2006, Stripes Development Team.