Class MockServletContext
- All Implemented Interfaces:
- jakarta.servlet.ServletContext
 Mock implementation of ServletContext.
 
This implementation supports all of the standard context methods except for request dispatching which just indicates what is being dispatched to, rather than performing an actual dispatch.
The context can be configured with a path parameter that should point to an directory location that represents the place where the contents of the WAR bundle are located. The path can either be an absolute or relative path. If the path is not found, the classpath will be checked for such a directory. Setting this value allows all of the resource location functionality to work as in a fully functioning web application. If this value is not set then not resource location functionality will work and instead null will always be returned.
This class was adapted from Apache Wicket.
- Since:
- 17.10.0
- 
Field SummaryFieldsModifier and TypeFieldDescriptionstatic final StringThe servlet context default context path, "/mock".Fields inherited from interface jakarta.servlet.ServletContextORDERED_LIBS, TEMPDIR
- 
Constructor SummaryConstructorsConstructorDescriptionDefault constructor for this mock object.MockServletContext(String contextPath, String webappPath) Create the mock object.MockServletContext(String contextPath, String webappPath, String tempPath) Create the mock object.
- 
Method SummaryModifier and TypeMethodDescriptionjakarta.servlet.FilterRegistration.Dynamicjakarta.servlet.FilterRegistration.Dynamicjakarta.servlet.FilterRegistration.DynamicvoidaddInitParameter(String name, String value) Add an init parameter.voidaddInitParameters(Map<String, String> initParameters) Add the map of init parameters.jakarta.servlet.ServletRegistration.DynamicaddJspFile(String a, String b) voidaddListener(Class<? extends EventListener> listenerClass) voidaddListener(String className) <T extends EventListener>
 voidaddListener(T t) voidaddMimeType(String fileExtension, String mimeType) Add a new recognized mime type.jakarta.servlet.ServletRegistration.DynamicaddServlet(String servletName, jakarta.servlet.Servlet servlet) jakarta.servlet.ServletRegistration.DynamicaddServlet(String servletName, Class<? extends jakarta.servlet.Servlet> servletClass) jakarta.servlet.ServletRegistration.DynamicaddServlet(String servletName, String className) <T extends jakarta.servlet.Filter>
 TcreateFilter(Class<T> clazz) <T extends EventListener>
 TcreateListener(Class<T> clazz) <T extends jakarta.servlet.Servlet>
 TcreateServlet(Class<T> clazz) voidCreates a temporary directory as specified bygetTempPath().voidCreates the web application root FilegetWebappRoot().voiddeclareRoles(String... roleNames) getAttribute(String name) Get an attribute with the given name.Get all of the attribute names.jakarta.servlet.ServletContextgetContext(String name) Get the context for the given URL path.Return the servlet context path.Set<jakarta.servlet.SessionTrackingMode>intintSet<jakarta.servlet.SessionTrackingMode>jakarta.servlet.FilterRegistrationgetFilterRegistration(String filterName) getInitParameter(String name) Get the init parameter with the given name.Get the name of all of the init parameters.jakarta.servlet.descriptor.JspConfigDescriptorintReturn the major version of the Servlet spec that this package supports, defaults to 2.getMimeType(String name) Get the mime type for the given file.intReturn the minor version of the Servlet spec that this package supports, defaults to 3.jakarta.servlet.RequestDispatchergetNamedDispatcher(String name) Returns a RequestDispatcher for the specified name.getRealPath(String name) Get the real file path of the given resource name.jakarta.servlet.RequestDispatchergetRequestDispatcher(String path) Returns a RequestDispatcher for the specified path.getResource(String name) Get the URL for a particular resource that is relative to the web app root directory.getResourceAsStream(String name) Get an input stream for a particular resource that is relative to the web app root directory or the current classpath.getResourcePaths(String name) Get the resource paths starting from the web app root directory and then relative to the given name.Get the server info.jakarta.servlet.ServletgetServlet(String name) NOT USED - Servlet Spec requires that this always returns null.Return the name of the servlet context.NOT USED - Servlet spec requires that this always returns null.jakarta.servlet.ServletRegistrationgetServletRegistration(String servletName) Enumeration<jakarta.servlet.Servlet>NOT USED - Servlet spec requires that this always returns null.jakarta.servlet.SessionCookieConfigintReturn the temporary path where files are stored during test runs.Return the web application path where resources like javascript, css and images can be picked up.Return the web application root File where resources like javascript, css and images can be picked up.voidLog the exception to System.err and the message to System.out.voidLog the message to System.out.voidLog the cause to System.err and the message to System.out.voidremoveAttribute(String name) Remove an attribute with the given name.voidsetAttribute(String name, Object o) Set an attribute.voidsetContextPath(String contextPath) Sets the servlet context path.booleansetInitParameter(String name, String value) voidsetRequestCharacterEncoding(String encoding) voidsetResponseCharacterEncoding(String encoding) voidsetServletContextName(String servletContextName) Set the servlet context name to the specified value.voidsetSessionTimeout(int time) voidsetSessionTrackingModes(Set<jakarta.servlet.SessionTrackingMode> sessionTrackingModes) voidsetTempPath(String tempPath) Set the temporary path where files are stored during test runs.voidsetWebappPath(String webappPath) Sets the mock web application path to the specified webappPath.
- 
Field Details- 
DEFAULT_CONTEXT_PATHThe servlet context default context path, "/mock".- See Also:
 
 
- 
- 
Constructor Details- 
MockServletContextpublic MockServletContext()Default constructor for this mock object. The servlet context name is set to 'mock'. The web content root and temporary work direcotry are set to null.
- 
MockServletContextCreate the mock object. As part of the creation, the context sets the root directory where web application content is stored. This must be an ABSOLUTE directory relative to where the tests are being executed. For example: System.getProperty("user.dir") + "/src/webapp"In addition to setting the web root directory, this constructor also sets up a temporary work directory for things like file uploads. Note this temporary work directory is set as the value of the ServletContext attribute 'jakarta.servlet.context.tempdir'. The temporary work directory defaults to System.getProperty("java.io.tmpdir"). - Parameters:
- contextPath- the servlet context path
- webappPath- The path to the root of the web application
 
- 
MockServletContextCreate the mock object. As part of the creation, the context sets the root directory where web application content is stored. This must be an ABSOLUTE directory relative to where the tests are being executed. For example: System.getProperty("user.dir") + "/src/webapp"In addition to setting the web root directory, this constructor also sets up a temporary work directory for things like file uploads. Note this temporary work directory is set as the value of the ServletContext attribute 'jakarta.servlet.context.tempdir'. - Parameters:
- contextPath- the servlet context path
- webappPath- the path to the root of the web application
- tempPath- the temporary work directory
 
 
- 
- 
Method Details- 
createWebappRootpublic void createWebappRoot()Creates the web application root FilegetWebappRoot().
- 
createTempDirpublic void createTempDir()Creates a temporary directory as specified bygetTempPath().
- 
setServletContextNameSet the servlet context name to the specified value.- Parameters:
- servletContextName- the servlet context name
 
- 
getTempPathReturn the temporary path where files are stored during test runs.- Returns:
- the temporary path where files are stored during test runs
 
- 
setTempPathSet the temporary path where files are stored during test runs.- Parameters:
- tempPath- the temporary path where files are stored during test runs
 
- 
getWebappPathReturn the web application path where resources like javascript, css and images can be picked up.- Returns:
- the web application path
 
- 
getWebappRootReturn the web application root File where resources like javascript, css and images can be picked up.- Returns:
- the web application root File
 
- 
setWebappPathSets the mock web application path to the specified webappPath. Note: this method will also set the web application's temporary directory to the value getTempPath(). IfgetTempPath()is not set, this method will default tempPath to: System.getProperty("java.io.tmpdir").- Parameters:
- webappPath- set the context web application path
 
- 
addInitParameterAdd an init parameter.- Parameters:
- name- The parameter name
- value- The parameter value
 
- 
addInitParametersAdd the map of init parameters.- Parameters:
- initParameters- A map of init parameters
 
- 
addMimeTypeAdd a new recognized mime type.- Parameters:
- fileExtension- The file extension (e.g. "jpg")
- mimeType- The mime type (e.g. "image/jpeg")
 
- 
getAttributeGet an attribute with the given name.- Specified by:
- getAttributein interface- jakarta.servlet.ServletContext
- Parameters:
- name- The attribute name
- Returns:
- The value, or null
 
- 
getAttributeNamesGet all of the attribute names.- Specified by:
- getAttributeNamesin interface- jakarta.servlet.ServletContext
- Returns:
- The attribute names
 
- 
getContextGet the context for the given URL path.- Specified by:
- getContextin interface- jakarta.servlet.ServletContext
- Parameters:
- name- The url path
- Returns:
- Always returns this
 
- 
getContextPathReturn the servlet context path.- Specified by:
- getContextPathin interface- jakarta.servlet.ServletContext
- Returns:
- the servletContext path
 
- 
setContextPathSets the servlet context path.- Parameters:
- contextPath- the servlet context path
 
- 
getInitParameterGet the init parameter with the given name.- Specified by:
- getInitParameterin interface- jakarta.servlet.ServletContext
- Parameters:
- name- The name
- Returns:
- The parameter, or null if no such parameter
 
- 
getInitParameterNamesGet the name of all of the init parameters.- Specified by:
- getInitParameterNamesin interface- jakarta.servlet.ServletContext
- Returns:
- The init parameter names
 
- 
getMajorVersionpublic int getMajorVersion()Return the major version of the Servlet spec that this package supports, defaults to 2.- Specified by:
- getMajorVersionin interface- jakarta.servlet.ServletContext
- Returns:
- the major version of the Servlet spec that this package supports, defaults to 2.
 
- 
getMimeTypeGet the mime type for the given file. Uses a hardcoded map of mime types set at initialization time. If the mime type was not explicitly set, this method will fallback to- Specified by:
- getMimeTypein interface- jakarta.servlet.ServletContext
- Parameters:
- name- The name to get the mime type for
- Returns:
- The mime type
 
- 
getMinorVersionpublic int getMinorVersion()Return the minor version of the Servlet spec that this package supports, defaults to 3.- Specified by:
- getMinorVersionin interface- jakarta.servlet.ServletContext
- Returns:
- the minor version of the Servlet spec that this package supports, defaults to 3.
 
- 
getRealPathGet the real file path of the given resource name.- Specified by:
- getRealPathin interface- jakarta.servlet.ServletContext
- Parameters:
- name- The name
- Returns:
- The real path or null
 
- 
getRequestDispatcherReturns a RequestDispatcher for the specified path. The dispatcher will not dispatch to the resource. It only records the specified path so that one can test if the correct path was dispatched to.- Specified by:
- getRequestDispatcherin interface- jakarta.servlet.ServletContext
- Parameters:
- path- a String specifying the pathname to the resource
- Returns:
- a dispatcher for the specified path
 
- 
getNamedDispatcherReturns a RequestDispatcher for the specified name. The dispatcher will not dispatch to the resource. It only records the specified name so that one can test if the correct name was dispatched to.- Specified by:
- getNamedDispatcherin interface- jakarta.servlet.ServletContext
- Parameters:
- name- a String specifying the name of a servlet to wrap
- Returns:
- a dispatcher for the specified name
 
- 
getResourceGet the URL for a particular resource that is relative to the web app root directory.- Specified by:
- getResourcein interface- jakarta.servlet.ServletContext
- Parameters:
- name- The name of the resource to get
- Returns:
- The resource, or null if resource not found
- Throws:
- MalformedURLException- If the URL is invalid
 
- 
getResourceAsStreamGet an input stream for a particular resource that is relative to the web app root directory or the current classpath. If the webappRoot is not set, this method will try and load the resource from the classpath.- Specified by:
- getResourceAsStreamin interface- jakarta.servlet.ServletContext
- Parameters:
- name- The name of the resource to get
- Returns:
- The input stream for the resource, or null if resource is not found
 
- 
getResourcePathsGet the resource paths starting from the web app root directory and then relative to the given name.- Specified by:
- getResourcePathsin interface- jakarta.servlet.ServletContext
- Parameters:
- name- The starting name
- Returns:
- The set of resource paths at this location
 
- 
getServerInfoGet the server info.- Specified by:
- getServerInfoin interface- jakarta.servlet.ServletContext
- Returns:
- The server info
 
- 
getServletNOT USED - Servlet Spec requires that this always returns null.- Parameters:
- name- Not used
- Returns:
- null
 
- 
getServletContextNameReturn the name of the servlet context.- Specified by:
- getServletContextNamein interface- jakarta.servlet.ServletContext
- Returns:
- The name
 
- 
getServletNamesNOT USED - Servlet spec requires that this always returns null.- Returns:
- null
 
- 
getServletsNOT USED - Servlet spec requires that this always returns null.- Returns:
- null
 
- 
logLog the exception to System.err and the message to System.out.- Parameters:
- e- The exception to log
- msg- The message to log
 
- 
logLog the message to System.out.- Specified by:
- login interface- jakarta.servlet.ServletContext
- Parameters:
- msg- The message to log
 
- 
logLog the cause to System.err and the message to System.out.- Specified by:
- login interface- jakarta.servlet.ServletContext
- Parameters:
- msg- The message to log
- cause- The cause exception
 
- 
removeAttributeRemove an attribute with the given name.- Specified by:
- removeAttributein interface- jakarta.servlet.ServletContext
- Parameters:
- name- The name
 
- 
setAttributeSet an attribute.- Specified by:
- setAttributein interface- jakarta.servlet.ServletContext
- Parameters:
- name- The name of the attribute
- o- The value
 
- 
setResponseCharacterEncoding- Specified by:
- setResponseCharacterEncodingin interface- jakarta.servlet.ServletContext
 
- 
getResponseCharacterEncoding- Specified by:
- getResponseCharacterEncodingin interface- jakarta.servlet.ServletContext
 
- 
setRequestCharacterEncoding- Specified by:
- setRequestCharacterEncodingin interface- jakarta.servlet.ServletContext
 
- 
getRequestCharacterEncoding- Specified by:
- getRequestCharacterEncodingin interface- jakarta.servlet.ServletContext
 
- 
setSessionTimeoutpublic void setSessionTimeout(int time) - Specified by:
- setSessionTimeoutin interface- jakarta.servlet.ServletContext
 
- 
getSessionTimeoutpublic int getSessionTimeout()- Specified by:
- getSessionTimeoutin interface- jakarta.servlet.ServletContext
 
- 
addJspFile- Specified by:
- addJspFilein interface- jakarta.servlet.ServletContext
 
- 
getEffectiveMajorVersionpublic int getEffectiveMajorVersion()- Specified by:
- getEffectiveMajorVersionin interface- jakarta.servlet.ServletContext
 
- 
getEffectiveMinorVersionpublic int getEffectiveMinorVersion()- Specified by:
- getEffectiveMinorVersionin interface- jakarta.servlet.ServletContext
 
- 
setInitParameter- Specified by:
- setInitParameterin interface- jakarta.servlet.ServletContext
 
- 
addServlet- Specified by:
- addServletin interface- jakarta.servlet.ServletContext
 
- 
addServletpublic jakarta.servlet.ServletRegistration.Dynamic addServlet(String servletName, jakarta.servlet.Servlet servlet) - Specified by:
- addServletin interface- jakarta.servlet.ServletContext
 
- 
addServletpublic jakarta.servlet.ServletRegistration.Dynamic addServlet(String servletName, Class<? extends jakarta.servlet.Servlet> servletClass) - Specified by:
- addServletin interface- jakarta.servlet.ServletContext
 
- 
createServlet- Specified by:
- createServletin interface- jakarta.servlet.ServletContext
 
- 
getServletRegistration- Specified by:
- getServletRegistrationin interface- jakarta.servlet.ServletContext
 
- 
getServletRegistrations- Specified by:
- getServletRegistrationsin interface- jakarta.servlet.ServletContext
 
- 
addFilter- Specified by:
- addFilterin interface- jakarta.servlet.ServletContext
 
- 
addFilterpublic jakarta.servlet.FilterRegistration.Dynamic addFilter(String filterName, jakarta.servlet.Filter filter) - Specified by:
- addFilterin interface- jakarta.servlet.ServletContext
 
- 
addFilterpublic jakarta.servlet.FilterRegistration.Dynamic addFilter(String filterName, Class<? extends jakarta.servlet.Filter> filterClass) - Specified by:
- addFilterin interface- jakarta.servlet.ServletContext
 
- 
createFilter- Specified by:
- createFilterin interface- jakarta.servlet.ServletContext
 
- 
getFilterRegistration- Specified by:
- getFilterRegistrationin interface- jakarta.servlet.ServletContext
 
- 
getFilterRegistrations- Specified by:
- getFilterRegistrationsin interface- jakarta.servlet.ServletContext
 
- 
getSessionCookieConfigpublic jakarta.servlet.SessionCookieConfig getSessionCookieConfig()- Specified by:
- getSessionCookieConfigin interface- jakarta.servlet.ServletContext
 
- 
setSessionTrackingModes- Specified by:
- setSessionTrackingModesin interface- jakarta.servlet.ServletContext
 
- 
getDefaultSessionTrackingModes- Specified by:
- getDefaultSessionTrackingModesin interface- jakarta.servlet.ServletContext
 
- 
getEffectiveSessionTrackingModes- Specified by:
- getEffectiveSessionTrackingModesin interface- jakarta.servlet.ServletContext
 
- 
addListener- Specified by:
- addListenerin interface- jakarta.servlet.ServletContext
 
- 
addListener- Specified by:
- addListenerin interface- jakarta.servlet.ServletContext
 
- 
addListener- Specified by:
- addListenerin interface- jakarta.servlet.ServletContext
 
- 
createListener- Specified by:
- createListenerin interface- jakarta.servlet.ServletContext
 
- 
getJspConfigDescriptorpublic jakarta.servlet.descriptor.JspConfigDescriptor getJspConfigDescriptor()- Specified by:
- getJspConfigDescriptorin interface- jakarta.servlet.ServletContext
 
- 
getClassLoader- Specified by:
- getClassLoaderin interface- jakarta.servlet.ServletContext
 
- 
declareRoles- Specified by:
- declareRolesin interface- jakarta.servlet.ServletContext
 
- 
getVirtualServerName- Specified by:
- getVirtualServerNamein interface- jakarta.servlet.ServletContext
 
 
-