Class MockServletContext

java.lang.Object
com.jalios.servlet.http.MockServletContext
All Implemented Interfaces:
javax.servlet.ServletContext

public class MockServletContext extends Object implements javax.servlet.ServletContext
Mock implementation of the ServletContext interface.
  • Field Details

  • Constructor Details

    • MockServletContext

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

      public MockServletContext(String resourceBasePath)
      Create a new MockServletContext using the supplied resource base path.

      Registers a MockRequestDispatcher for the Servlet named 'default'.

      Parameters:
      resourceBasePath - the root directory of the WAR (should not end with a slash)
      See Also:
  • Method Details

    • 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
    • setContextPath

      public void setContextPath(String contextPath)
    • getContextPath

      public String getContextPath()
      Specified by:
      getContextPath in interface javax.servlet.ServletContext
    • registerContext

      public void registerContext(String contextPath, javax.servlet.ServletContext context)
    • getContext

      public javax.servlet.ServletContext getContext(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 String getMimeType(String filePath)
      This method uses the default FileTypeMap from the Java Activation Framework to resolve MIME types.

      The Java Activation Framework returns "application/octet-stream" if the MIME type is unknown (i.e., it never returns null). Thus, in order to honor the ServletContext.getMimeType(String) contract, this method returns null if the MIME type is "application/octet-stream".

      MockServletContext does not provide a direct mechanism for setting a custom MIME type; however, if the default FileTypeMap is an instance of javax.activation.MimetypesFileTypeMap, a custom MIME type named text/enigma can be registered for a custom .puzzle file extension in the following manner:

       MimetypesFileTypeMap mimetypesFileTypeMap = (MimetypesFileTypeMap) FileTypeMap.getDefaultFileTypeMap();
       mimetypesFileTypeMap.addMimeTypes("text/enigma    puzzle");
       
      Specified by:
      getMimeType in interface javax.servlet.ServletContext
    • getResourcePaths

      public Set<String> getResourcePaths(String path)
      Specified by:
      getResourcePaths in interface javax.servlet.ServletContext
    • getResource

      public URL getResource(String path) throws MalformedURLException
      Specified by:
      getResource in interface javax.servlet.ServletContext
      Throws:
      MalformedURLException
    • getResourceAsStream

      public InputStream getResourceAsStream(String path)
      Specified by:
      getResourceAsStream in interface javax.servlet.ServletContext
    • getRequestDispatcher

      public javax.servlet.RequestDispatcher getRequestDispatcher(String path)
      Specified by:
      getRequestDispatcher in interface javax.servlet.ServletContext
    • getNamedDispatcher

      public javax.servlet.RequestDispatcher getNamedDispatcher(String path)
      Specified by:
      getNamedDispatcher in interface javax.servlet.ServletContext
    • registerNamedDispatcher

      public void registerNamedDispatcher(String name, javax.servlet.RequestDispatcher requestDispatcher)
      Register a RequestDispatcher (typically a MockRequestDispatcher) that acts as a wrapper for the named Servlet.
      Parameters:
      name - the name of the wrapped Servlet
      requestDispatcher - the dispatcher that wraps the named Servlet
      See Also:
    • unregisterNamedDispatcher

      public void unregisterNamedDispatcher(String name)
      Unregister the RequestDispatcher with the given name.
      Parameters:
      name - the name of the dispatcher to unregister
      See Also:
    • getDefaultServletName

      public String getDefaultServletName()
      Get the name of the default Servlet.

      Defaults to 'default'.

      See Also:
    • setDefaultServletName

      public void setDefaultServletName(String defaultServletName)
      Set the name of the default Servlet.

      Also unregisters the current default RequestDispatcher and replaces it with a MockRequestDispatcher for the provided defaultServletName.

      Parameters:
      defaultServletName - the name of the default Servlet; never null or empty
      See Also:
    • getServlet

      @Deprecated public javax.servlet.Servlet getServlet(String name)
      Deprecated.
      Specified by:
      getServlet in interface javax.servlet.ServletContext
    • getServlets

      @Deprecated public Enumeration<javax.servlet.Servlet> getServlets()
      Deprecated.
      Specified by:
      getServlets in interface javax.servlet.ServletContext
    • getServletNames

      @Deprecated public Enumeration<String> getServletNames()
      Deprecated.
      Specified by:
      getServletNames in interface javax.servlet.ServletContext
    • log

      public void log(String message)
      Specified by:
      log in interface javax.servlet.ServletContext
    • log

      @Deprecated public void log(Exception ex, String message)
      Deprecated.
      Specified by:
      log in interface javax.servlet.ServletContext
    • log

      public void log(String message, Throwable ex)
      Specified by:
      log in interface javax.servlet.ServletContext
    • getRealPath

      public String getRealPath(String path)
      Specified by:
      getRealPath in interface javax.servlet.ServletContext
    • getServerInfo

      public String getServerInfo()
      Specified by:
      getServerInfo in interface javax.servlet.ServletContext
    • getInitParameter

      public String getInitParameter(String name)
      Specified by:
      getInitParameter in interface javax.servlet.ServletContext
    • getInitParameterNames

      public Enumeration<String> getInitParameterNames()
      Specified by:
      getInitParameterNames in interface javax.servlet.ServletContext
    • setInitParameter

      public boolean setInitParameter(String name, String value)
      Specified by:
      setInitParameter in interface javax.servlet.ServletContext
    • addInitParameter

      public void addInitParameter(String name, String value)
    • getAttribute

      public Object getAttribute(String name)
      Specified by:
      getAttribute in interface javax.servlet.ServletContext
    • getAttributeNames

      public Enumeration<String> getAttributeNames()
      Specified by:
      getAttributeNames in interface javax.servlet.ServletContext
    • setAttribute

      public void setAttribute(String name, Object value)
      Specified by:
      setAttribute in interface javax.servlet.ServletContext
    • removeAttribute

      public void removeAttribute(String name)
      Specified by:
      removeAttribute in interface javax.servlet.ServletContext
    • setServletContextName

      public void setServletContextName(String servletContextName)
    • getServletContextName

      public String getServletContextName()
      Specified by:
      getServletContextName in interface javax.servlet.ServletContext
    • getClassLoader

      public ClassLoader getClassLoader()
      Specified by:
      getClassLoader in interface javax.servlet.ServletContext
    • declareRoles

      public void declareRoles(String... roleNames)
      Specified by:
      declareRoles in interface javax.servlet.ServletContext
    • getDeclaredRoles

      public Set<String> getDeclaredRoles()
    • setSessionTrackingModes

      public void setSessionTrackingModes(Set<javax.servlet.SessionTrackingMode> sessionTrackingModes) throws IllegalStateException, IllegalArgumentException
      Specified by:
      setSessionTrackingModes in interface javax.servlet.ServletContext
      Throws:
      IllegalStateException
      IllegalArgumentException
    • getDefaultSessionTrackingModes

      public Set<javax.servlet.SessionTrackingMode> getDefaultSessionTrackingModes()
      Specified by:
      getDefaultSessionTrackingModes in interface javax.servlet.ServletContext
    • getEffectiveSessionTrackingModes

      public Set<javax.servlet.SessionTrackingMode> getEffectiveSessionTrackingModes()
      Specified by:
      getEffectiveSessionTrackingModes in interface javax.servlet.ServletContext
    • getSessionCookieConfig

      public javax.servlet.SessionCookieConfig getSessionCookieConfig()
      Specified by:
      getSessionCookieConfig in interface javax.servlet.ServletContext
    • getJspConfigDescriptor

      public javax.servlet.descriptor.JspConfigDescriptor getJspConfigDescriptor()
      Specified by:
      getJspConfigDescriptor in interface javax.servlet.ServletContext
    • addServlet

      public javax.servlet.ServletRegistration.Dynamic addServlet(String servletName, String className)
      Specified by:
      addServlet in interface javax.servlet.ServletContext
    • addServlet

      public javax.servlet.ServletRegistration.Dynamic addServlet(String servletName, javax.servlet.Servlet servlet)
      Specified by:
      addServlet in interface javax.servlet.ServletContext
    • addServlet

      public javax.servlet.ServletRegistration.Dynamic addServlet(String servletName, Class<? extends javax.servlet.Servlet> servletClass)
      Specified by:
      addServlet in interface javax.servlet.ServletContext
    • createServlet

      public <T extends javax.servlet.Servlet> T createServlet(Class<T> c) throws javax.servlet.ServletException
      Specified by:
      createServlet in interface javax.servlet.ServletContext
      Throws:
      javax.servlet.ServletException
    • getServletRegistration

      public javax.servlet.ServletRegistration getServletRegistration(String servletName)
      This method always returns null.
      Specified by:
      getServletRegistration in interface javax.servlet.ServletContext
      See Also:
      • ServletContext.getServletRegistration(java.lang.String)
    • getServletRegistrations

      public Map<String,? extends javax.servlet.ServletRegistration> getServletRegistrations()
      This method always returns an empty map.
      Specified by:
      getServletRegistrations in interface javax.servlet.ServletContext
      See Also:
      • ServletContext.getServletRegistrations()
    • addFilter

      public javax.servlet.FilterRegistration.Dynamic addFilter(String filterName, String className)
      Specified by:
      addFilter in interface javax.servlet.ServletContext
    • addFilter

      public javax.servlet.FilterRegistration.Dynamic addFilter(String filterName, javax.servlet.Filter filter)
      Specified by:
      addFilter in interface javax.servlet.ServletContext
    • addFilter

      public javax.servlet.FilterRegistration.Dynamic addFilter(String filterName, Class<? extends javax.servlet.Filter> filterClass)
      Specified by:
      addFilter in interface javax.servlet.ServletContext
    • createFilter

      public <T extends javax.servlet.Filter> T createFilter(Class<T> c) throws javax.servlet.ServletException
      Specified by:
      createFilter in interface javax.servlet.ServletContext
      Throws:
      javax.servlet.ServletException
    • getFilterRegistration

      public javax.servlet.FilterRegistration getFilterRegistration(String filterName)
      This method always returns null.
      Specified by:
      getFilterRegistration in interface javax.servlet.ServletContext
      See Also:
      • ServletContext.getFilterRegistration(java.lang.String)
    • getFilterRegistrations

      public Map<String,? extends javax.servlet.FilterRegistration> getFilterRegistrations()
      This method always returns an empty map.
      Specified by:
      getFilterRegistrations in interface javax.servlet.ServletContext
      See Also:
      • ServletContext.getFilterRegistrations()
    • addListener

      public void addListener(Class<? extends EventListener> listenerClass)
      Specified by:
      addListener in interface javax.servlet.ServletContext
    • addListener

      public void addListener(String className)
      Specified by:
      addListener in interface javax.servlet.ServletContext
    • addListener

      public <T extends EventListener> void addListener(T t)
      Specified by:
      addListener in interface javax.servlet.ServletContext
    • createListener

      public <T extends EventListener> T createListener(Class<T> c) throws javax.servlet.ServletException
      Specified by:
      createListener in interface javax.servlet.ServletContext
      Throws:
      javax.servlet.ServletException
    • getVirtualServerName

      public String getVirtualServerName()
      Specified by:
      getVirtualServerName in interface javax.servlet.ServletContext
    • addJspFile

      public javax.servlet.ServletRegistration.Dynamic addJspFile(String arg0, String arg1)
      Specified by:
      addJspFile in interface javax.servlet.ServletContext
    • getRequestCharacterEncoding

      public String getRequestCharacterEncoding()
      Specified by:
      getRequestCharacterEncoding in interface javax.servlet.ServletContext
    • getResponseCharacterEncoding

      public String getResponseCharacterEncoding()
      Specified by:
      getResponseCharacterEncoding in interface javax.servlet.ServletContext
    • getSessionTimeout

      public int getSessionTimeout()
      Specified by:
      getSessionTimeout in interface javax.servlet.ServletContext
    • setRequestCharacterEncoding

      public void setRequestCharacterEncoding(String arg0)
      Specified by:
      setRequestCharacterEncoding in interface javax.servlet.ServletContext
    • setResponseCharacterEncoding

      public void setResponseCharacterEncoding(String arg0)
      Specified by:
      setResponseCharacterEncoding in interface javax.servlet.ServletContext
    • setSessionTimeout

      public void setSessionTimeout(int arg0)
      Specified by:
      setSessionTimeout in interface javax.servlet.ServletContext