Class MockServletContext
- All Implemented Interfaces:
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 Summary
Modifier and TypeFieldDescriptionstatic final String
The servlet context default context path, "/mock".Fields inherited from interface javax.servlet.ServletContext
ORDERED_LIBS, TEMPDIR
-
Constructor Summary
ConstructorDescriptionDefault 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 Summary
Modifier and TypeMethodDescriptionvoid
addInitParameter
(String name, String value) Add an init parameter.void
addInitParameters
(Map<String, String> initParameters) Add the map of init parameters.addJspFile
(String a, String b) void
addListener
(Class<? extends EventListener> listenerClass) void
addListener
(String className) <T extends EventListener>
voidaddListener
(T t) void
addMimeType
(String fileExtension, String mimeType) Add a new recognized mime type.addServlet
(String servletName, Class<? extends Servlet> servletClass) addServlet
(String servletName, String className) addServlet
(String servletName, Servlet servlet) <T extends Filter>
TcreateFilter
(Class<T> clazz) <T extends EventListener>
TcreateListener
(Class<T> clazz) <T extends Servlet>
TcreateServlet
(Class<T> clazz) void
Creates a temporary directory as specified bygetTempPath()
.void
Creates the web application root FilegetWebappRoot()
.void
declareRoles
(String... roleNames) getAttribute
(String name) Get an attribute with the given name.Get all of the attribute names.getContext
(String name) Get the context for the given URL path.Return the servlet context path.int
int
getFilterRegistration
(String filterName) Map<String,
? extends FilterRegistration> getInitParameter
(String name) Get the init parameter with the given name.Get the name of all of the init parameters.int
Return 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.int
Return the minor version of the Servlet spec that this package supports, defaults to 3.getNamedDispatcher
(String name) Returns a RequestDispatcher for the specified name.getRealPath
(String name) Get the real file path of the given resource name.getRequestDispatcher
(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.getServlet
(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.getServletRegistration
(String servletName) Map<String,
? extends ServletRegistration> NOT USED - Servlet spec requires that this always returns null.int
Return 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.void
Log the exception to System.err and the message to System.out.void
Log the message to System.out.void
Log the cause to System.err and the message to System.out.void
removeAttribute
(String name) Remove an attribute with the given name.void
setAttribute
(String name, Object o) Set an attribute.void
setContextPath
(String contextPath) Sets the servlet context path.boolean
setInitParameter
(String name, String value) void
setRequestCharacterEncoding
(String encoding) void
setResponseCharacterEncoding
(String encoding) void
setServletContextName
(String servletContextName) Set the servlet context name to the specified value.void
setSessionTimeout
(int time) void
setSessionTrackingModes
(Set<SessionTrackingMode> sessionTrackingModes) void
setTempPath
(String tempPath) Set the temporary path where files are stored during test runs.void
setWebappPath
(String webappPath) Sets the mock web application path to the specified webappPath.
-
Field Details
-
DEFAULT_CONTEXT_PATH
The servlet context default context path, "/mock".- See Also:
-
-
Constructor Details
-
MockServletContext
public 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. -
MockServletContext
Create 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 'javax.servlet.context.tempdir'.
The temporary work directory defaults to System.getProperty("java.io.tmpdir").
- Parameters:
contextPath
- the servlet context pathwebappPath
- The path to the root of the web application
-
MockServletContext
Create 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 'javax.servlet.context.tempdir'.
- Parameters:
contextPath
- the servlet context pathwebappPath
- the path to the root of the web applicationtempPath
- the temporary work directory
-
-
Method Details
-
createWebappRoot
public void createWebappRoot()Creates the web application root FilegetWebappRoot()
. -
createTempDir
public void createTempDir()Creates a temporary directory as specified bygetTempPath()
. -
setServletContextName
Set the servlet context name to the specified value.- Parameters:
servletContextName
- the servlet context name
-
getTempPath
Return the temporary path where files are stored during test runs.- Returns:
- the temporary path where files are stored during test runs
-
setTempPath
Set the temporary path where files are stored during test runs.- Parameters:
tempPath
- the temporary path where files are stored during test runs
-
getWebappPath
Return the web application path where resources like javascript, css and images can be picked up.- Returns:
- the web application path
-
getWebappRoot
Return the web application root File where resources like javascript, css and images can be picked up.- Returns:
- the web application root File
-
setWebappPath
Sets 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
-
addInitParameter
Add an init parameter.- Parameters:
name
- The parameter namevalue
- The parameter value
-
addInitParameters
Add the map of init parameters.- Parameters:
initParameters
- A map of init parameters
-
addMimeType
Add a new recognized mime type.- Parameters:
fileExtension
- The file extension (e.g. "jpg")mimeType
- The mime type (e.g. "image/jpeg")
-
getAttribute
Get an attribute with the given name.- Specified by:
getAttribute
in interfaceServletContext
- Parameters:
name
- The attribute name- Returns:
- The value, or null
-
getAttributeNames
Get all of the attribute names.- Specified by:
getAttributeNames
in interfaceServletContext
- Returns:
- The attribute names
-
getContext
Get the context for the given URL path.- Specified by:
getContext
in interfaceServletContext
- Parameters:
name
- The url path- Returns:
- Always returns this
-
getContextPath
Return the servlet context path.- Specified by:
getContextPath
in interfaceServletContext
- Returns:
- the servletContext path
-
setContextPath
Sets the servlet context path.- Parameters:
contextPath
- the servlet context path
-
getInitParameter
Get the init parameter with the given name.- Specified by:
getInitParameter
in interfaceServletContext
- Parameters:
name
- The name- Returns:
- The parameter, or null if no such parameter
-
getInitParameterNames
Get the name of all of the init parameters.- Specified by:
getInitParameterNames
in interfaceServletContext
- Returns:
- The init parameter names
-
getMajorVersion
public int getMajorVersion()Return the major version of the Servlet spec that this package supports, defaults to 2.- Specified by:
getMajorVersion
in interfaceServletContext
- Returns:
- the major version of the Servlet spec that this package supports, defaults to 2.
-
getMimeType
Get 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:
getMimeType
in interfaceServletContext
- Parameters:
name
- The name to get the mime type for- Returns:
- The mime type
-
getMinorVersion
public int getMinorVersion()Return the minor version of the Servlet spec that this package supports, defaults to 3.- Specified by:
getMinorVersion
in interfaceServletContext
- Returns:
- the minor version of the Servlet spec that this package supports, defaults to 3.
-
getRealPath
Get the real file path of the given resource name.- Specified by:
getRealPath
in interfaceServletContext
- Parameters:
name
- The name- Returns:
- The real path or null
-
getRequestDispatcher
Returns 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:
getRequestDispatcher
in interfaceServletContext
- Parameters:
path
- a String specifying the pathname to the resource- Returns:
- a dispatcher for the specified path
-
getNamedDispatcher
Returns 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:
getNamedDispatcher
in interfaceServletContext
- Parameters:
name
- a String specifying the name of a servlet to wrap- Returns:
- a dispatcher for the specified name
-
getResource
Get the URL for a particular resource that is relative to the web app root directory.- Specified by:
getResource
in interfaceServletContext
- 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
-
getResourceAsStream
Get 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:
getResourceAsStream
in interfaceServletContext
- Parameters:
name
- The name of the resource to get- Returns:
- The input stream for the resource, or null if resource is not found
-
getResourcePaths
Get the resource paths starting from the web app root directory and then relative to the given name.- Specified by:
getResourcePaths
in interfaceServletContext
- Parameters:
name
- The starting name- Returns:
- The set of resource paths at this location
-
getServerInfo
Get the server info.- Specified by:
getServerInfo
in interfaceServletContext
- Returns:
- The server info
-
getServlet
NOT USED - Servlet Spec requires that this always returns null.- Specified by:
getServlet
in interfaceServletContext
- Parameters:
name
- Not used- Returns:
- null
-
getServletContextName
Return the name of the servlet context.- Specified by:
getServletContextName
in interfaceServletContext
- Returns:
- The name
-
getServletNames
NOT USED - Servlet spec requires that this always returns null.- Specified by:
getServletNames
in interfaceServletContext
- Returns:
- null
-
getServlets
NOT USED - Servlet spec requires that this always returns null.- Specified by:
getServlets
in interfaceServletContext
- Returns:
- null
-
log
Log the exception to System.err and the message to System.out.- Specified by:
log
in interfaceServletContext
- Parameters:
e
- The exception to logmsg
- The message to log
-
log
Log the message to System.out.- Specified by:
log
in interfaceServletContext
- Parameters:
msg
- The message to log
-
log
Log the cause to System.err and the message to System.out.- Specified by:
log
in interfaceServletContext
- Parameters:
msg
- The message to logcause
- The cause exception
-
removeAttribute
Remove an attribute with the given name.- Specified by:
removeAttribute
in interfaceServletContext
- Parameters:
name
- The name
-
setAttribute
Set an attribute.- Specified by:
setAttribute
in interfaceServletContext
- Parameters:
name
- The name of the attributeo
- The value
-
setResponseCharacterEncoding
- Specified by:
setResponseCharacterEncoding
in interfaceServletContext
-
getResponseCharacterEncoding
- Specified by:
getResponseCharacterEncoding
in interfaceServletContext
-
setRequestCharacterEncoding
- Specified by:
setRequestCharacterEncoding
in interfaceServletContext
-
getRequestCharacterEncoding
- Specified by:
getRequestCharacterEncoding
in interfaceServletContext
-
setSessionTimeout
public void setSessionTimeout(int time) - Specified by:
setSessionTimeout
in interfaceServletContext
-
getSessionTimeout
public int getSessionTimeout()- Specified by:
getSessionTimeout
in interfaceServletContext
-
addJspFile
- Specified by:
addJspFile
in interfaceServletContext
-
getEffectiveMajorVersion
public int getEffectiveMajorVersion()- Specified by:
getEffectiveMajorVersion
in interfaceServletContext
-
getEffectiveMinorVersion
public int getEffectiveMinorVersion()- Specified by:
getEffectiveMinorVersion
in interfaceServletContext
-
setInitParameter
- Specified by:
setInitParameter
in interfaceServletContext
-
addServlet
- Specified by:
addServlet
in interfaceServletContext
-
addServlet
- Specified by:
addServlet
in interfaceServletContext
-
addServlet
public ServletRegistration.Dynamic addServlet(String servletName, Class<? extends Servlet> servletClass) - Specified by:
addServlet
in interfaceServletContext
-
createServlet
- Specified by:
createServlet
in interfaceServletContext
-
getServletRegistration
- Specified by:
getServletRegistration
in interfaceServletContext
-
getServletRegistrations
- Specified by:
getServletRegistrations
in interfaceServletContext
-
addFilter
- Specified by:
addFilter
in interfaceServletContext
-
addFilter
- Specified by:
addFilter
in interfaceServletContext
-
addFilter
- Specified by:
addFilter
in interfaceServletContext
-
createFilter
- Specified by:
createFilter
in interfaceServletContext
-
getFilterRegistration
- Specified by:
getFilterRegistration
in interfaceServletContext
-
getFilterRegistrations
- Specified by:
getFilterRegistrations
in interfaceServletContext
-
getSessionCookieConfig
- Specified by:
getSessionCookieConfig
in interfaceServletContext
-
setSessionTrackingModes
- Specified by:
setSessionTrackingModes
in interfaceServletContext
-
getDefaultSessionTrackingModes
- Specified by:
getDefaultSessionTrackingModes
in interfaceServletContext
-
getEffectiveSessionTrackingModes
- Specified by:
getEffectiveSessionTrackingModes
in interfaceServletContext
-
addListener
- Specified by:
addListener
in interfaceServletContext
-
addListener
- Specified by:
addListener
in interfaceServletContext
-
addListener
- Specified by:
addListener
in interfaceServletContext
-
createListener
- Specified by:
createListener
in interfaceServletContext
-
getJspConfigDescriptor
- Specified by:
getJspConfigDescriptor
in interfaceServletContext
-
getClassLoader
- Specified by:
getClassLoader
in interfaceServletContext
-
declareRoles
- Specified by:
declareRoles
in interfaceServletContext
-
getVirtualServerName
- Specified by:
getVirtualServerName
in interfaceServletContext
-