Class MockServletContext
- java.lang.Object
-
- com.jalios.servlet.http.MockServletContext
-
- All Implemented Interfaces:
javax.servlet.ServletContext
public class MockServletContext extends java.lang.Object implements javax.servlet.ServletContext
Mock implementation of theServletContext
interface.
-
-
Field Summary
Fields Modifier and Type Field Description static java.lang.String
TEMP_DIR_CONTEXT_ATTRIBUTE
-
Constructor Summary
Constructors Constructor Description MockServletContext()
Create a newMockServletContext
, using no base path (i.e.MockServletContext(java.lang.String resourceBasePath)
Create a newMockServletContext
using the supplied resource base path.
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description javax.servlet.FilterRegistration.Dynamic
addFilter(java.lang.String filterName, java.lang.Class<? extends javax.servlet.Filter> filterClass)
javax.servlet.FilterRegistration.Dynamic
addFilter(java.lang.String filterName, java.lang.String className)
javax.servlet.FilterRegistration.Dynamic
addFilter(java.lang.String filterName, javax.servlet.Filter filter)
void
addInitParameter(java.lang.String name, java.lang.String value)
javax.servlet.ServletRegistration.Dynamic
addJspFile(java.lang.String arg0, java.lang.String arg1)
void
addListener(java.lang.Class<? extends java.util.EventListener> listenerClass)
void
addListener(java.lang.String className)
<T extends java.util.EventListener>
voidaddListener(T t)
javax.servlet.ServletRegistration.Dynamic
addServlet(java.lang.String servletName, java.lang.Class<? extends javax.servlet.Servlet> servletClass)
javax.servlet.ServletRegistration.Dynamic
addServlet(java.lang.String servletName, java.lang.String className)
javax.servlet.ServletRegistration.Dynamic
addServlet(java.lang.String servletName, javax.servlet.Servlet servlet)
<T extends javax.servlet.Filter>
TcreateFilter(java.lang.Class<T> c)
<T extends java.util.EventListener>
TcreateListener(java.lang.Class<T> c)
<T extends javax.servlet.Servlet>
TcreateServlet(java.lang.Class<T> c)
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()
java.lang.String
getDefaultServletName()
Get the name of the defaultServlet
.java.util.Set<javax.servlet.SessionTrackingMode>
getDefaultSessionTrackingModes()
int
getEffectiveMajorVersion()
int
getEffectiveMinorVersion()
java.util.Set<javax.servlet.SessionTrackingMode>
getEffectiveSessionTrackingModes()
javax.servlet.FilterRegistration
getFilterRegistration(java.lang.String filterName)
This method always returnsnull
.java.util.Map<java.lang.String,? extends javax.servlet.FilterRegistration>
getFilterRegistrations()
This method always returns an empty map.java.lang.String
getInitParameter(java.lang.String name)
java.util.Enumeration<java.lang.String>
getInitParameterNames()
javax.servlet.descriptor.JspConfigDescriptor
getJspConfigDescriptor()
int
getMajorVersion()
java.lang.String
getMimeType(java.lang.String filePath)
This method uses the defaultFileTypeMap
from the Java Activation Framework to resolve MIME types.int
getMinorVersion()
javax.servlet.RequestDispatcher
getNamedDispatcher(java.lang.String path)
java.lang.String
getRealPath(java.lang.String path)
java.lang.String
getRequestCharacterEncoding()
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 thisMockServletContext
.java.util.Set<java.lang.String>
getResourcePaths(java.lang.String path)
java.lang.String
getResponseCharacterEncoding()
java.lang.String
getServerInfo()
javax.servlet.Servlet
getServlet(java.lang.String name)
Deprecated.java.lang.String
getServletContextName()
java.util.Enumeration<java.lang.String>
getServletNames()
Deprecated.javax.servlet.ServletRegistration
getServletRegistration(java.lang.String servletName)
This method always returnsnull
.java.util.Map<java.lang.String,? extends javax.servlet.ServletRegistration>
getServletRegistrations()
This method always returns an empty map.java.util.Enumeration<javax.servlet.Servlet>
getServlets()
Deprecated.javax.servlet.SessionCookieConfig
getSessionCookieConfig()
int
getSessionTimeout()
java.lang.String
getVirtualServerName()
void
log(java.lang.Exception ex, java.lang.String message)
Deprecated.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
registerNamedDispatcher(java.lang.String name, javax.servlet.RequestDispatcher requestDispatcher)
Register aRequestDispatcher
(typically aMockRequestDispatcher
) that acts as a wrapper for the named Servlet.void
removeAttribute(java.lang.String name)
void
setAttribute(java.lang.String name, java.lang.Object value)
void
setContextPath(java.lang.String contextPath)
void
setDefaultServletName(java.lang.String defaultServletName)
Set the name of the defaultServlet
.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
setRequestCharacterEncoding(java.lang.String arg0)
void
setResponseCharacterEncoding(java.lang.String arg0)
void
setServletContextName(java.lang.String servletContextName)
void
setSessionTimeout(int arg0)
void
setSessionTrackingModes(java.util.Set<javax.servlet.SessionTrackingMode> sessionTrackingModes)
void
unregisterNamedDispatcher(java.lang.String name)
Unregister theRequestDispatcher
with the given name.
-
-
-
Field Detail
-
TEMP_DIR_CONTEXT_ATTRIBUTE
public static final java.lang.String TEMP_DIR_CONTEXT_ATTRIBUTE
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
MockServletContext
public MockServletContext()
Create a newMockServletContext
, using no base path (i.e. the classpath root as WAR root).
-
MockServletContext
public MockServletContext(java.lang.String resourceBasePath)
Create a newMockServletContext
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:
registerNamedDispatcher(java.lang.String, javax.servlet.RequestDispatcher)
-
-
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 thisMockServletContext
.- 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 interfacejavax.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 interfacejavax.servlet.ServletContext
-
setMajorVersion
public void setMajorVersion(int majorVersion)
-
getMajorVersion
public int getMajorVersion()
- Specified by:
getMajorVersion
in interfacejavax.servlet.ServletContext
-
setMinorVersion
public void setMinorVersion(int minorVersion)
-
getMinorVersion
public int getMinorVersion()
- Specified by:
getMinorVersion
in interfacejavax.servlet.ServletContext
-
setEffectiveMajorVersion
public void setEffectiveMajorVersion(int effectiveMajorVersion)
-
getEffectiveMajorVersion
public int getEffectiveMajorVersion()
- Specified by:
getEffectiveMajorVersion
in interfacejavax.servlet.ServletContext
-
setEffectiveMinorVersion
public void setEffectiveMinorVersion(int effectiveMinorVersion)
-
getEffectiveMinorVersion
public int getEffectiveMinorVersion()
- Specified by:
getEffectiveMinorVersion
in interfacejavax.servlet.ServletContext
-
getMimeType
public java.lang.String getMimeType(java.lang.String filePath)
This method uses the defaultFileTypeMap
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 returnsnull
). Thus, in order to honor theServletContext.getMimeType(String)
contract, this method returnsnull
if the MIME type is"application/octet-stream"
.MockServletContext
does not provide a direct mechanism for setting a custom MIME type; however, if the defaultFileTypeMap
is an instance ofjavax.activation.MimetypesFileTypeMap
, a custom MIME type namedtext/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 interfacejavax.servlet.ServletContext
-
getResourcePaths
public java.util.Set<java.lang.String> getResourcePaths(java.lang.String path)
- Specified by:
getResourcePaths
in interfacejavax.servlet.ServletContext
-
getResource
public java.net.URL getResource(java.lang.String path) throws java.net.MalformedURLException
- Specified by:
getResource
in interfacejavax.servlet.ServletContext
- Throws:
java.net.MalformedURLException
-
getResourceAsStream
public java.io.InputStream getResourceAsStream(java.lang.String path)
- Specified by:
getResourceAsStream
in interfacejavax.servlet.ServletContext
-
getRequestDispatcher
public javax.servlet.RequestDispatcher getRequestDispatcher(java.lang.String path)
- Specified by:
getRequestDispatcher
in interfacejavax.servlet.ServletContext
-
getNamedDispatcher
public javax.servlet.RequestDispatcher getNamedDispatcher(java.lang.String path)
- Specified by:
getNamedDispatcher
in interfacejavax.servlet.ServletContext
-
registerNamedDispatcher
public void registerNamedDispatcher(java.lang.String name, javax.servlet.RequestDispatcher requestDispatcher)
Register aRequestDispatcher
(typically aMockRequestDispatcher
) that acts as a wrapper for the named Servlet.- Parameters:
name
- the name of the wrapped ServletrequestDispatcher
- the dispatcher that wraps the named Servlet- See Also:
getNamedDispatcher(java.lang.String)
,unregisterNamedDispatcher(java.lang.String)
-
unregisterNamedDispatcher
public void unregisterNamedDispatcher(java.lang.String name)
Unregister theRequestDispatcher
with the given name.- Parameters:
name
- the name of the dispatcher to unregister- See Also:
getNamedDispatcher(java.lang.String)
,registerNamedDispatcher(java.lang.String, javax.servlet.RequestDispatcher)
-
getDefaultServletName
public java.lang.String getDefaultServletName()
Get the name of the defaultServlet
.Defaults to 'default'.
- See Also:
setDefaultServletName(java.lang.String)
-
setDefaultServletName
public void setDefaultServletName(java.lang.String defaultServletName)
Set the name of the defaultServlet
.Also
unregisters
the current defaultRequestDispatcher
andreplaces
it with aMockRequestDispatcher
for the provideddefaultServletName
.- Parameters:
defaultServletName
- the name of the defaultServlet
; nevernull
or empty- See Also:
getDefaultServletName()
-
getServlet
@Deprecated public javax.servlet.Servlet getServlet(java.lang.String name)
Deprecated.- Specified by:
getServlet
in interfacejavax.servlet.ServletContext
-
getServlets
@Deprecated public java.util.Enumeration<javax.servlet.Servlet> getServlets()
Deprecated.- Specified by:
getServlets
in interfacejavax.servlet.ServletContext
-
getServletNames
@Deprecated public java.util.Enumeration<java.lang.String> getServletNames()
Deprecated.- Specified by:
getServletNames
in interfacejavax.servlet.ServletContext
-
log
public void log(java.lang.String message)
- Specified by:
log
in interfacejavax.servlet.ServletContext
-
log
@Deprecated public void log(java.lang.Exception ex, java.lang.String message)
Deprecated.- Specified by:
log
in interfacejavax.servlet.ServletContext
-
log
public void log(java.lang.String message, java.lang.Throwable ex)
- Specified by:
log
in interfacejavax.servlet.ServletContext
-
getRealPath
public java.lang.String getRealPath(java.lang.String path)
- Specified by:
getRealPath
in interfacejavax.servlet.ServletContext
-
getServerInfo
public java.lang.String getServerInfo()
- Specified by:
getServerInfo
in interfacejavax.servlet.ServletContext
-
getInitParameter
public java.lang.String getInitParameter(java.lang.String name)
- Specified by:
getInitParameter
in interfacejavax.servlet.ServletContext
-
getInitParameterNames
public java.util.Enumeration<java.lang.String> getInitParameterNames()
- Specified by:
getInitParameterNames
in interfacejavax.servlet.ServletContext
-
setInitParameter
public boolean setInitParameter(java.lang.String name, java.lang.String value)
- Specified by:
setInitParameter
in interfacejavax.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 interfacejavax.servlet.ServletContext
-
getAttributeNames
public java.util.Enumeration<java.lang.String> getAttributeNames()
- Specified by:
getAttributeNames
in interfacejavax.servlet.ServletContext
-
setAttribute
public void setAttribute(java.lang.String name, java.lang.Object value)
- Specified by:
setAttribute
in interfacejavax.servlet.ServletContext
-
removeAttribute
public void removeAttribute(java.lang.String name)
- Specified by:
removeAttribute
in interfacejavax.servlet.ServletContext
-
setServletContextName
public void setServletContextName(java.lang.String servletContextName)
-
getServletContextName
public java.lang.String getServletContextName()
- Specified by:
getServletContextName
in interfacejavax.servlet.ServletContext
-
getClassLoader
public java.lang.ClassLoader getClassLoader()
- Specified by:
getClassLoader
in interfacejavax.servlet.ServletContext
-
declareRoles
public void declareRoles(java.lang.String... roleNames)
- Specified by:
declareRoles
in interfacejavax.servlet.ServletContext
-
getDeclaredRoles
public java.util.Set<java.lang.String> getDeclaredRoles()
-
setSessionTrackingModes
public void setSessionTrackingModes(java.util.Set<javax.servlet.SessionTrackingMode> sessionTrackingModes) throws java.lang.IllegalStateException, java.lang.IllegalArgumentException
- Specified by:
setSessionTrackingModes
in interfacejavax.servlet.ServletContext
- Throws:
java.lang.IllegalStateException
java.lang.IllegalArgumentException
-
getDefaultSessionTrackingModes
public java.util.Set<javax.servlet.SessionTrackingMode> getDefaultSessionTrackingModes()
- Specified by:
getDefaultSessionTrackingModes
in interfacejavax.servlet.ServletContext
-
getEffectiveSessionTrackingModes
public java.util.Set<javax.servlet.SessionTrackingMode> getEffectiveSessionTrackingModes()
- Specified by:
getEffectiveSessionTrackingModes
in interfacejavax.servlet.ServletContext
-
getSessionCookieConfig
public javax.servlet.SessionCookieConfig getSessionCookieConfig()
- Specified by:
getSessionCookieConfig
in interfacejavax.servlet.ServletContext
-
getJspConfigDescriptor
public javax.servlet.descriptor.JspConfigDescriptor getJspConfigDescriptor()
- Specified by:
getJspConfigDescriptor
in interfacejavax.servlet.ServletContext
-
addServlet
public javax.servlet.ServletRegistration.Dynamic addServlet(java.lang.String servletName, java.lang.String className)
- Specified by:
addServlet
in interfacejavax.servlet.ServletContext
-
addServlet
public javax.servlet.ServletRegistration.Dynamic addServlet(java.lang.String servletName, javax.servlet.Servlet servlet)
- Specified by:
addServlet
in interfacejavax.servlet.ServletContext
-
addServlet
public javax.servlet.ServletRegistration.Dynamic addServlet(java.lang.String servletName, java.lang.Class<? extends javax.servlet.Servlet> servletClass)
- Specified by:
addServlet
in interfacejavax.servlet.ServletContext
-
createServlet
public <T extends javax.servlet.Servlet> T createServlet(java.lang.Class<T> c) throws javax.servlet.ServletException
- Specified by:
createServlet
in interfacejavax.servlet.ServletContext
- Throws:
javax.servlet.ServletException
-
getServletRegistration
public javax.servlet.ServletRegistration getServletRegistration(java.lang.String servletName)
This method always returnsnull
.- Specified by:
getServletRegistration
in interfacejavax.servlet.ServletContext
- See Also:
ServletContext.getServletRegistration(java.lang.String)
-
getServletRegistrations
public java.util.Map<java.lang.String,? extends javax.servlet.ServletRegistration> getServletRegistrations()
This method always returns an empty map.- Specified by:
getServletRegistrations
in interfacejavax.servlet.ServletContext
- See Also:
ServletContext.getServletRegistrations()
-
addFilter
public javax.servlet.FilterRegistration.Dynamic addFilter(java.lang.String filterName, java.lang.String className)
- Specified by:
addFilter
in interfacejavax.servlet.ServletContext
-
addFilter
public javax.servlet.FilterRegistration.Dynamic addFilter(java.lang.String filterName, javax.servlet.Filter filter)
- Specified by:
addFilter
in interfacejavax.servlet.ServletContext
-
addFilter
public javax.servlet.FilterRegistration.Dynamic addFilter(java.lang.String filterName, java.lang.Class<? extends javax.servlet.Filter> filterClass)
- Specified by:
addFilter
in interfacejavax.servlet.ServletContext
-
createFilter
public <T extends javax.servlet.Filter> T createFilter(java.lang.Class<T> c) throws javax.servlet.ServletException
- Specified by:
createFilter
in interfacejavax.servlet.ServletContext
- Throws:
javax.servlet.ServletException
-
getFilterRegistration
public javax.servlet.FilterRegistration getFilterRegistration(java.lang.String filterName)
This method always returnsnull
.- Specified by:
getFilterRegistration
in interfacejavax.servlet.ServletContext
- See Also:
ServletContext.getFilterRegistration(java.lang.String)
-
getFilterRegistrations
public java.util.Map<java.lang.String,? extends javax.servlet.FilterRegistration> getFilterRegistrations()
This method always returns an empty map.- Specified by:
getFilterRegistrations
in interfacejavax.servlet.ServletContext
- See Also:
ServletContext.getFilterRegistrations()
-
addListener
public void addListener(java.lang.Class<? extends java.util.EventListener> listenerClass)
- Specified by:
addListener
in interfacejavax.servlet.ServletContext
-
addListener
public void addListener(java.lang.String className)
- Specified by:
addListener
in interfacejavax.servlet.ServletContext
-
addListener
public <T extends java.util.EventListener> void addListener(T t)
- Specified by:
addListener
in interfacejavax.servlet.ServletContext
-
createListener
public <T extends java.util.EventListener> T createListener(java.lang.Class<T> c) throws javax.servlet.ServletException
- Specified by:
createListener
in interfacejavax.servlet.ServletContext
- Throws:
javax.servlet.ServletException
-
getVirtualServerName
public java.lang.String getVirtualServerName()
- Specified by:
getVirtualServerName
in interfacejavax.servlet.ServletContext
-
addJspFile
public javax.servlet.ServletRegistration.Dynamic addJspFile(java.lang.String arg0, java.lang.String arg1)
- Specified by:
addJspFile
in interfacejavax.servlet.ServletContext
-
getRequestCharacterEncoding
public java.lang.String getRequestCharacterEncoding()
- Specified by:
getRequestCharacterEncoding
in interfacejavax.servlet.ServletContext
-
getResponseCharacterEncoding
public java.lang.String getResponseCharacterEncoding()
- Specified by:
getResponseCharacterEncoding
in interfacejavax.servlet.ServletContext
-
getSessionTimeout
public int getSessionTimeout()
- Specified by:
getSessionTimeout
in interfacejavax.servlet.ServletContext
-
setRequestCharacterEncoding
public void setRequestCharacterEncoding(java.lang.String arg0)
- Specified by:
setRequestCharacterEncoding
in interfacejavax.servlet.ServletContext
-
setResponseCharacterEncoding
public void setResponseCharacterEncoding(java.lang.String arg0)
- Specified by:
setResponseCharacterEncoding
in interfacejavax.servlet.ServletContext
-
setSessionTimeout
public void setSessionTimeout(int arg0)
- Specified by:
setSessionTimeout
in interfacejavax.servlet.ServletContext
-
-