org.springframework.mock.web
Class MockServletContext

java.lang.Object
  extended byorg.springframework.mock.web.MockServletContext
All Implemented Interfaces:
ServletContext

public class MockServletContext
extends Object
implements ServletContext

Mock implementation of the ServletContext interface.

Used for testing the web framework; only rarely necessary for testing application controllers, as long as they don't explicitly access the ServletContext. In the latter case, ClassPathXmlApplicationContext can be used to load them; else, XmlWebApplicationContext needs to be used, possibly with this MockServletContext class.

Since:
1.0.2
Author:
Rod Johnson, Juergen Hoeller

Constructor Summary
MockServletContext()
          Create a new MockServletContext, using no base path and a DefaultResourceLoader (i.e. the classpath root as WAR root).
MockServletContext(String resourceBasePath)
          Create a new MockServletContext, using a DefaultResourceLoader.
MockServletContext(String resourceBasePath, ResourceLoader resourceLoader)
          Create a new MockServletContext.
 
Method Summary
 void addInitParameter(String name, String value)
           
 Object getAttribute(String name)
           
 Enumeration getAttributeNames()
           
 ServletContext getContext(String name)
           
 String getInitParameter(String name)
           
 Enumeration getInitParameterNames()
           
 int getMajorVersion()
           
 String getMimeType(String filePath)
           
 int getMinorVersion()
           
 RequestDispatcher getNamedDispatcher(String path)
           
 String getRealPath(String path)
           
 RequestDispatcher getRequestDispatcher(String path)
           
 URL getResource(String path)
           
 InputStream getResourceAsStream(String path)
           
protected  String getResourceLocation(String path)
          Build a full resource location for the given path, prepending the resource base path of this MockServletContext.
 Set getResourcePaths(String path)
           
 String getServerInfo()
           
 Servlet getServlet(String name)
           
 String getServletContextName()
           
 Enumeration getServletNames()
           
 Enumeration getServlets()
           
 void log(Exception e, String message)
           
 void log(String message)
           
 void log(String message, Throwable t)
           
 void removeAttribute(String name)
           
 void setAttribute(String name, Object value)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MockServletContext

public MockServletContext()
Create a new MockServletContext, using no base path and a DefaultResourceLoader (i.e. the classpath root as WAR root).

See Also:
DefaultResourceLoader

MockServletContext

public MockServletContext(String resourceBasePath)
Create a new MockServletContext, using a DefaultResourceLoader.

Parameters:
resourceBasePath - the WAR root directory (should not end with a /)
See Also:
DefaultResourceLoader

MockServletContext

public MockServletContext(String resourceBasePath,
                          ResourceLoader resourceLoader)
Create a new MockServletContext.

Parameters:
resourceBasePath - the WAR root directory (should not end with a /)
resourceLoader - the ResourceLoader to use
Method Detail

getResourceLocation

protected String getResourceLocation(String path)
Build a full resource location for the given path, prepending the resource base path of this MockServletContext.

Parameters:
path - the path as specified
Returns:
the full resource path

getContext

public ServletContext getContext(String name)
Specified by:
getContext in interface ServletContext

getMajorVersion

public int getMajorVersion()
Specified by:
getMajorVersion in interface ServletContext

getMinorVersion

public int getMinorVersion()
Specified by:
getMinorVersion in interface ServletContext

getMimeType

public String getMimeType(String filePath)
Specified by:
getMimeType in interface ServletContext

getResourcePaths

public Set getResourcePaths(String path)
Specified by:
getResourcePaths in interface ServletContext

getResource

public URL getResource(String path)
                throws MalformedURLException
Specified by:
getResource in interface ServletContext
Throws:
MalformedURLException

getResourceAsStream

public InputStream getResourceAsStream(String path)
Specified by:
getResourceAsStream in interface ServletContext

getRequestDispatcher

public RequestDispatcher getRequestDispatcher(String path)
Specified by:
getRequestDispatcher in interface ServletContext

getNamedDispatcher

public RequestDispatcher getNamedDispatcher(String path)
Specified by:
getNamedDispatcher in interface ServletContext

getServlet

public Servlet getServlet(String name)
Specified by:
getServlet in interface ServletContext

getServlets

public Enumeration getServlets()
Specified by:
getServlets in interface ServletContext

getServletNames

public Enumeration getServletNames()
Specified by:
getServletNames in interface ServletContext

log

public void log(String message)
Specified by:
log in interface ServletContext

log

public void log(Exception e,
                String message)
Specified by:
log in interface ServletContext

log

public void log(String message,
                Throwable t)
Specified by:
log in interface ServletContext

getRealPath

public String getRealPath(String path)
Specified by:
getRealPath in interface ServletContext

getServerInfo

public String getServerInfo()
Specified by:
getServerInfo in interface ServletContext

getInitParameter

public String getInitParameter(String name)
Specified by:
getInitParameter in interface ServletContext

addInitParameter

public void addInitParameter(String name,
                             String value)

getInitParameterNames

public Enumeration getInitParameterNames()
Specified by:
getInitParameterNames in interface ServletContext

getAttribute

public Object getAttribute(String name)
Specified by:
getAttribute in interface ServletContext

getAttributeNames

public Enumeration getAttributeNames()
Specified by:
getAttributeNames in interface ServletContext

setAttribute

public void setAttribute(String name,
                         Object value)
Specified by:
setAttribute in interface ServletContext

removeAttribute

public void removeAttribute(String name)
Specified by:
removeAttribute in interface ServletContext

getServletContextName

public String getServletContextName()
Specified by:
getServletContextName in interface ServletContext


Copyright (C) 2003-2004 The Spring Framework Project.