Overview | Package | Class | Tree | Deprecated | Index | Help
| Spring Framework
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD

## org.springframework.mock.web
Class MockServletContext

    java.lang.Object
      org.springframework.mock.web.MockServletContext

All Implemented Interfaces:
  javax.servlet.ServletContext

    public class MockServletContext

extends java.lang.Object
implements javax.servlet.ServletContext

Mock implementation of the `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).

Since:
  1.0.2
Author:
  Rod Johnson, Juergen Hoeller
See Also:
  `MockServletContext(org.springframework.core.io.ResourceLoader)`, `XmlWebApplicationContext`, `GenericWebApplicationContext`, `ClassPathXmlApplicationContext`, `FileSystemXmlApplicationContext`

Field Summary

Fields inherited from interface javax.servlet.ServletContext
`ORDERED_LIBS, TEMPDIR`

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()`
`javax.servlet.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()`
`javax.servlet.RequestDispatcher` | `getNamedDispatcher(java.lang.String path)`
`java.lang.String` | `getRealPath(java.lang.String path)`
`javax.servlet.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()`
`javax.servlet.Servlet` | `getServlet(java.lang.String name)`
`java.lang.String` | `getServletContextName()`
`java.util.Enumeration<java.lang.String>` | `getServletNames()`
`java.util.Enumeration<javax.servlet.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, javax.servlet.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`

Methods inherited from interface javax.servlet.ServletContext
`addFilter, addFilter, addFilter, addListener, addListener, addListener, addServlet, addServlet, addServlet, createFilter, createListener, createServlet, getDefaultSessionTrackingModes, getEffectiveSessionTrackingModes, getFilterRegistration, getFilterRegistrations, getJspConfigDescriptor, getServletRegistration, getServletRegistrations, getSessionCookieConfig, setSessionTrackingModes`

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(java.lang.String resourceBasePath)

  Create a new MockServletContext, using a DefaultResourceLoader.

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

### MockServletContext

    public MockServletContext(ResourceLoader resourceLoader)

  Create a new MockServletContext, using the specified ResourceLoader and no base path.

  Parameters:
    `resourceLoader` - the ResourceLoader to use (or null for the default)

### MockServletContext

    public MockServletContext(java.lang.String resourceBasePath,
                              ResourceLoader resourceLoader)

  Create a new MockServletContext.

  Parameters:
    `resourceBasePath` - the WAR root directory (should not end with a slash)
    `resourceLoader` - the ResourceLoader to use (or null for the default)

Method Detail

### getResourceLocation

    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.

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

### setContextPath

    public void setContextPath(java.lang.String contextPath)

### getContextPath

    public java.lang.String getContextPath()

  Specified by:
    `getContextPath` in interface `javax.servlet.ServletContext`

### registerContext

    public void registerContext(java.lang.String contextPath,
                                javax.servlet.ServletContext context)

### getContext

    public javax.servlet.ServletContext getContext(java.lang.String contextPath)

  Specified by:
    `getContext` in interface `javax.servlet.ServletContext`

### setMajorVersion

    public void setMajorVersion(int majorVersion)

### getMajorVersion

    public int getMajorVersion()

  Specified by:
    `getMajorVersion` in interface `javax.servlet.ServletContext`

### setMinorVersion

    public void setMinorVersion(int minorVersion)

### getMinorVersion

    public int getMinorVersion()

  Specified by:
    `getMinorVersion` in interface `javax.servlet.ServletContext`

### setEffectiveMajorVersion

    public void setEffectiveMajorVersion(int effectiveMajorVersion)

### getEffectiveMajorVersion

    public int getEffectiveMajorVersion()

  Specified by:
    `getEffectiveMajorVersion` in interface `javax.servlet.ServletContext`

### setEffectiveMinorVersion

    public void setEffectiveMinorVersion(int effectiveMinorVersion)

### getEffectiveMinorVersion

    public int getEffectiveMinorVersion()

  Specified by:
    `getEffectiveMinorVersion` in interface `javax.servlet.ServletContext`

### getMimeType

    public java.lang.String getMimeType(java.lang.String filePath)

  Specified by:
    `getMimeType` in interface `javax.servlet.ServletContext`

### getResourcePaths

    public java.util.Set<java.lang.String> getResourcePaths(java.lang.String path)

  Specified by:
    `getResourcePaths` in interface `javax.servlet.ServletContext`

### getResource

    public java.net.URL getResource(java.lang.String path)
                             throws java.net.MalformedURLException

  Specified by:
    `getResource` in interface `javax.servlet.ServletContext`
  Throws:
    `java.net.MalformedURLException`

### getResourceAsStream

    public java.io.InputStream getResourceAsStream(java.lang.String path)

  Specified by:
    `getResourceAsStream` in interface `javax.servlet.ServletContext`

### getRequestDispatcher

    public javax.servlet.RequestDispatcher getRequestDispatcher(java.lang.String path)

  Specified by:
    `getRequestDispatcher` in interface `javax.servlet.ServletContext`

### getNamedDispatcher

    public javax.servlet.RequestDispatcher getNamedDispatcher(java.lang.String path)

  Specified by:
    `getNamedDispatcher` in interface `javax.servlet.ServletContext`

### getServlet

    public javax.servlet.Servlet getServlet(java.lang.String name)

  Specified by:
    `getServlet` in interface `javax.servlet.ServletContext`

### getServlets

    public java.util.Enumeration<javax.servlet.Servlet> getServlets()

  Specified by:
    `getServlets` in interface `javax.servlet.ServletContext`

### getServletNames

    public java.util.Enumeration<java.lang.String> getServletNames()

  Specified by:
    `getServletNames` in interface `javax.servlet.ServletContext`

### log

    public void log(java.lang.String message)

  Specified by:
    `log` in interface `javax.servlet.ServletContext`

### log

    public void log(java.lang.Exception ex,
                    java.lang.String message)

  Specified by:
    `log` in interface `javax.servlet.ServletContext`

### log

    public void log(java.lang.String message,
                    java.lang.Throwable ex)

  Specified by:
    `log` in interface `javax.servlet.ServletContext`

### getRealPath

    public java.lang.String getRealPath(java.lang.String path)

  Specified by:
    `getRealPath` in interface `javax.servlet.ServletContext`

### getServerInfo

    public java.lang.String getServerInfo()

  Specified by:
    `getServerInfo` in interface `javax.servlet.ServletContext`

### getInitParameter

    public java.lang.String getInitParameter(java.lang.String name)

  Specified by:
    `getInitParameter` in interface `javax.servlet.ServletContext`

### getInitParameterNames

    public java.util.Enumeration<java.lang.String> getInitParameterNames()

  Specified by:
    `getInitParameterNames` in interface `javax.servlet.ServletContext`

### setInitParameter

    public boolean setInitParameter(java.lang.String name,
                                    java.lang.String value)

  Specified by:
    `setInitParameter` in interface `javax.servlet.ServletContext`

### addInitParameter

    public void addInitParameter(java.lang.String name,
                                 java.lang.String value)

### getAttribute

    public java.lang.Object getAttribute(java.lang.String name)

  Specified by:
    `getAttribute` in interface `javax.servlet.ServletContext`

### getAttributeNames

    public java.util.Enumeration<java.lang.String> getAttributeNames()

  Specified by:
    `getAttributeNames` in interface `javax.servlet.ServletContext`

### setAttribute

    public void setAttribute(java.lang.String name,
                             java.lang.Object value)

  Specified by:
    `setAttribute` in interface `javax.servlet.ServletContext`

### removeAttribute

    public void removeAttribute(java.lang.String name)

  Specified by:
    `removeAttribute` in interface `javax.servlet.ServletContext`

### setServletContextName

    public void setServletContextName(java.lang.String servletContextName)

### getServletContextName

    public java.lang.String getServletContextName()

  Specified by:
    `getServletContextName` in interface `javax.servlet.ServletContext`

### getClassLoader

    public java.lang.ClassLoader getClassLoader()

  Specified by:
    `getClassLoader` in interface `javax.servlet.ServletContext`

### declareRoles

    public void declareRoles(java.lang.String... roleNames)

  Specified by:
    `declareRoles` in interface `javax.servlet.ServletContext`

### getDeclaredRoles

    public java.util.Set<java.lang.String> getDeclaredRoles()

Overview | Package | Class | Tree | Deprecated | Index | Help
| Spring Framework
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD