Class MockServletContext
- java.lang.Object
-
- com.avoka.tm.test.MockServletContext
-
- All Implemented Interfaces:
javax.servlet.ServletContext
public class MockServletContext extends Object implements javax.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 Summary
Fields Modifier and Type Field Description static StringDEFAULT_CONTEXT_PATHThe servlet context default context path, "/mock".
-
Constructor Summary
Constructors Constructor Description MockServletContext()Default 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
All Methods Instance Methods Concrete Methods Modifier and Type Method Description javax.servlet.FilterRegistration.DynamicaddFilter(String filterName, Class<? extends javax.servlet.Filter> filterClass)javax.servlet.FilterRegistration.DynamicaddFilter(String filterName, String className)javax.servlet.FilterRegistration.DynamicaddFilter(String filterName, javax.servlet.Filter filter)voidaddInitParameter(String name, String value)Add an init parameter.voidaddInitParameters(Map<String,String> initParameters)Add the map of init parameters.javax.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.javax.servlet.ServletRegistration.DynamicaddServlet(String servletName, Class<? extends javax.servlet.Servlet> servletClass)javax.servlet.ServletRegistration.DynamicaddServlet(String servletName, String className)javax.servlet.ServletRegistration.DynamicaddServlet(String servletName, javax.servlet.Servlet servlet)<T extends javax.servlet.Filter>
TcreateFilter(Class<T> clazz)<T extends EventListener>
TcreateListener(Class<T> clazz)<T extends javax.servlet.Servlet>
TcreateServlet(Class<T> clazz)voidcreateTempDir()Creates a temporary directory as specified bygetTempPath().voidcreateWebappRoot()Creates the web application root FilegetWebappRoot().voiddeclareRoles(String... roleNames)ObjectgetAttribute(String name)Get an attribute with the given name.Enumeration<String>getAttributeNames()Get all of the attribute names.ClassLoadergetClassLoader()javax.servlet.ServletContextgetContext(String name)Get the context for the given URL path.StringgetContextPath()Return the servlet context path.Set<javax.servlet.SessionTrackingMode>getDefaultSessionTrackingModes()intgetEffectiveMajorVersion()intgetEffectiveMinorVersion()Set<javax.servlet.SessionTrackingMode>getEffectiveSessionTrackingModes()javax.servlet.FilterRegistrationgetFilterRegistration(String filterName)Map<String,? extends javax.servlet.FilterRegistration>getFilterRegistrations()StringgetInitParameter(String name)Get the init parameter with the given name.Enumeration<String>getInitParameterNames()Get the name of all of the init parameters.javax.servlet.descriptor.JspConfigDescriptorgetJspConfigDescriptor()intgetMajorVersion()Return the major version of the Servlet spec that this package supports, defaults to 2.StringgetMimeType(String name)Get the mime type for the given file.intgetMinorVersion()Return the minor version of the Servlet spec that this package supports, defaults to 3.javax.servlet.RequestDispatchergetNamedDispatcher(String name)Returns a RequestDispatcher for the specified name.StringgetRealPath(String name)Get the real file path of the given resource name.StringgetRequestCharacterEncoding()javax.servlet.RequestDispatchergetRequestDispatcher(String path)Returns a RequestDispatcher for the specified path.URLgetResource(String name)Get the URL for a particular resource that is relative to the web app root directory.InputStreamgetResourceAsStream(String name)Get an input stream for a particular resource that is relative to the web app root directory or the current classpath.Set<String>getResourcePaths(String name)Get the resource paths starting from the web app root directory and then relative to the given name.StringgetResponseCharacterEncoding()StringgetServerInfo()Get the server info.javax.servlet.ServletgetServlet(String name)NOT USED - Servlet Spec requires that this always returns null.StringgetServletContextName()Return the name of the servlet context.Enumeration<String>getServletNames()NOT USED - Servlet spec requires that this always returns null.javax.servlet.ServletRegistrationgetServletRegistration(String servletName)Map<String,? extends javax.servlet.ServletRegistration>getServletRegistrations()Enumeration<javax.servlet.Servlet>getServlets()NOT USED - Servlet spec requires that this always returns null.javax.servlet.SessionCookieConfiggetSessionCookieConfig()intgetSessionTimeout()StringgetTempPath()Return the temporary path where files are stored during test runs.StringgetVirtualServerName()StringgetWebappPath()Return the web application path where resources like javascript, css and images can be picked up.FilegetWebappRoot()Return the web application root File where resources like javascript, css and images can be picked up.voidlog(Exception e, String msg)Log the exception to System.err and the message to System.out.voidlog(String msg)Log the message to System.out.voidlog(String msg, Throwable cause)Log 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<javax.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 Detail
-
DEFAULT_CONTEXT_PATH
public static final String DEFAULT_CONTEXT_PATH
The servlet context default context path, "/mock".- See Also:
- Constant Field Values
-
-
Constructor Detail
-
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
public MockServletContext(String contextPath, String webappPath)
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
public MockServletContext(String contextPath, String webappPath, String tempPath)
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 Detail
-
createWebappRoot
public void createWebappRoot()
Creates the web application root FilegetWebappRoot().
-
createTempDir
public void createTempDir()
Creates a temporary directory as specified bygetTempPath().
-
setServletContextName
public void setServletContextName(String servletContextName)
Set the servlet context name to the specified value.- Parameters:
servletContextName- the servlet context name
-
getTempPath
public String getTempPath()
Return the temporary path where files are stored during test runs.- Returns:
- the temporary path where files are stored during test runs
-
setTempPath
public void setTempPath(String tempPath)
Set the temporary path where files are stored during test runs.- Parameters:
tempPath- the temporary path where files are stored during test runs
-
getWebappPath
public String getWebappPath()
Return the web application path where resources like javascript, css and images can be picked up.- Returns:
- the web application path
-
getWebappRoot
public File 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
public void setWebappPath(String webappPath)
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
public void addInitParameter(String name, String value)
Add an init parameter.- Parameters:
name- The parameter namevalue- The parameter value
-
addInitParameters
public void addInitParameters(Map<String,String> initParameters)
Add the map of init parameters.- Parameters:
initParameters- A map of init parameters
-
addMimeType
public void addMimeType(String fileExtension, String mimeType)
Add a new recognized mime type.- Parameters:
fileExtension- The file extension (e.g. "jpg")mimeType- The mime type (e.g. "image/jpeg")
-
getAttribute
public Object getAttribute(String name)
Get an attribute with the given name.- Specified by:
getAttributein interfacejavax.servlet.ServletContext- Parameters:
name- The attribute name- Returns:
- The value, or null
-
getAttributeNames
public Enumeration<String> getAttributeNames()
Get all of the attribute names.- Specified by:
getAttributeNamesin interfacejavax.servlet.ServletContext- Returns:
- The attribute names
-
getContext
public javax.servlet.ServletContext getContext(String name)
Get the context for the given URL path.- Specified by:
getContextin interfacejavax.servlet.ServletContext- Parameters:
name- The url path- Returns:
- Always returns this
-
getContextPath
public String getContextPath()
Return the servlet context path.- Specified by:
getContextPathin interfacejavax.servlet.ServletContext- Returns:
- the servletContext path
-
setContextPath
public void setContextPath(String contextPath)
Sets the servlet context path.- Parameters:
contextPath- the servlet context path
-
getInitParameter
public String getInitParameter(String name)
Get the init parameter with the given name.- Specified by:
getInitParameterin interfacejavax.servlet.ServletContext- Parameters:
name- The name- Returns:
- The parameter, or null if no such parameter
-
getInitParameterNames
public Enumeration<String> getInitParameterNames()
Get the name of all of the init parameters.- Specified by:
getInitParameterNamesin interfacejavax.servlet.ServletContext- 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:
getMajorVersionin interfacejavax.servlet.ServletContext- Returns:
- the major version of the Servlet spec that this package supports, defaults to 2.
-
getMimeType
public String getMimeType(String name)
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:
getMimeTypein interfacejavax.servlet.ServletContext- 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:
getMinorVersionin interfacejavax.servlet.ServletContext- Returns:
- the minor version of the Servlet spec that this package supports, defaults to 3.
-
getRealPath
public String getRealPath(String name)
Get the real file path of the given resource name.- Specified by:
getRealPathin interfacejavax.servlet.ServletContext- Parameters:
name- The name- Returns:
- The real path or null
-
getRequestDispatcher
public javax.servlet.RequestDispatcher getRequestDispatcher(String path)
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:
getRequestDispatcherin interfacejavax.servlet.ServletContext- Parameters:
path- a String specifying the pathname to the resource- Returns:
- a dispatcher for the specified path
-
getNamedDispatcher
public javax.servlet.RequestDispatcher getNamedDispatcher(String name)
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:
getNamedDispatcherin interfacejavax.servlet.ServletContext- Parameters:
name- a String specifying the name of a servlet to wrap- Returns:
- a dispatcher for the specified name
-
getResource
public URL getResource(String name) throws MalformedURLException
Get the URL for a particular resource that is relative to the web app root directory.- Specified by:
getResourcein interfacejavax.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
-
getResourceAsStream
public InputStream getResourceAsStream(String name)
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:
getResourceAsStreamin interfacejavax.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
-
getResourcePaths
public Set<String> getResourcePaths(String name)
Get the resource paths starting from the web app root directory and then relative to the given name.- Specified by:
getResourcePathsin interfacejavax.servlet.ServletContext- Parameters:
name- The starting name- Returns:
- The set of resource paths at this location
-
getServerInfo
public String getServerInfo()
Get the server info.- Specified by:
getServerInfoin interfacejavax.servlet.ServletContext- Returns:
- The server info
-
getServlet
public javax.servlet.Servlet getServlet(String name)
NOT USED - Servlet Spec requires that this always returns null.- Specified by:
getServletin interfacejavax.servlet.ServletContext- Parameters:
name- Not used- Returns:
- null
-
getServletContextName
public String getServletContextName()
Return the name of the servlet context.- Specified by:
getServletContextNamein interfacejavax.servlet.ServletContext- Returns:
- The name
-
getServletNames
public Enumeration<String> getServletNames()
NOT USED - Servlet spec requires that this always returns null.- Specified by:
getServletNamesin interfacejavax.servlet.ServletContext- Returns:
- null
-
getServlets
public Enumeration<javax.servlet.Servlet> getServlets()
NOT USED - Servlet spec requires that this always returns null.- Specified by:
getServletsin interfacejavax.servlet.ServletContext- Returns:
- null
-
log
public void log(Exception e, String msg)
Log the exception to System.err and the message to System.out.- Specified by:
login interfacejavax.servlet.ServletContext- Parameters:
e- The exception to logmsg- The message to log
-
log
public void log(String msg)
Log the message to System.out.- Specified by:
login interfacejavax.servlet.ServletContext- Parameters:
msg- The message to log
-
log
public void log(String msg, Throwable cause)
Log the cause to System.err and the message to System.out.- Specified by:
login interfacejavax.servlet.ServletContext- Parameters:
msg- The message to logcause- The cause exception
-
removeAttribute
public void removeAttribute(String name)
Remove an attribute with the given name.- Specified by:
removeAttributein interfacejavax.servlet.ServletContext- Parameters:
name- The name
-
setAttribute
public void setAttribute(String name, Object o)
Set an attribute.- Specified by:
setAttributein interfacejavax.servlet.ServletContext- Parameters:
name- The name of the attributeo- The value
-
setResponseCharacterEncoding
public void setResponseCharacterEncoding(String encoding)
- Specified by:
setResponseCharacterEncodingin interfacejavax.servlet.ServletContext
-
getResponseCharacterEncoding
public String getResponseCharacterEncoding()
- Specified by:
getResponseCharacterEncodingin interfacejavax.servlet.ServletContext
-
setRequestCharacterEncoding
public void setRequestCharacterEncoding(String encoding)
- Specified by:
setRequestCharacterEncodingin interfacejavax.servlet.ServletContext
-
getRequestCharacterEncoding
public String getRequestCharacterEncoding()
- Specified by:
getRequestCharacterEncodingin interfacejavax.servlet.ServletContext
-
setSessionTimeout
public void setSessionTimeout(int time)
- Specified by:
setSessionTimeoutin interfacejavax.servlet.ServletContext
-
getSessionTimeout
public int getSessionTimeout()
- Specified by:
getSessionTimeoutin interfacejavax.servlet.ServletContext
-
addJspFile
public javax.servlet.ServletRegistration.Dynamic addJspFile(String a, String b)
- Specified by:
addJspFilein interfacejavax.servlet.ServletContext
-
getEffectiveMajorVersion
public int getEffectiveMajorVersion()
- Specified by:
getEffectiveMajorVersionin interfacejavax.servlet.ServletContext
-
getEffectiveMinorVersion
public int getEffectiveMinorVersion()
- Specified by:
getEffectiveMinorVersionin interfacejavax.servlet.ServletContext
-
setInitParameter
public boolean setInitParameter(String name, String value)
- Specified by:
setInitParameterin interfacejavax.servlet.ServletContext
-
addServlet
public javax.servlet.ServletRegistration.Dynamic addServlet(String servletName, String className)
- Specified by:
addServletin interfacejavax.servlet.ServletContext
-
addServlet
public javax.servlet.ServletRegistration.Dynamic addServlet(String servletName, javax.servlet.Servlet servlet)
- Specified by:
addServletin interfacejavax.servlet.ServletContext
-
addServlet
public javax.servlet.ServletRegistration.Dynamic addServlet(String servletName, Class<? extends javax.servlet.Servlet> servletClass)
- Specified by:
addServletin interfacejavax.servlet.ServletContext
-
createServlet
public <T extends javax.servlet.Servlet> T createServlet(Class<T> clazz)
- Specified by:
createServletin interfacejavax.servlet.ServletContext
-
getServletRegistration
public javax.servlet.ServletRegistration getServletRegistration(String servletName)
- Specified by:
getServletRegistrationin interfacejavax.servlet.ServletContext
-
getServletRegistrations
public Map<String,? extends javax.servlet.ServletRegistration> getServletRegistrations()
- Specified by:
getServletRegistrationsin interfacejavax.servlet.ServletContext
-
addFilter
public javax.servlet.FilterRegistration.Dynamic addFilter(String filterName, String className)
- Specified by:
addFilterin interfacejavax.servlet.ServletContext
-
addFilter
public javax.servlet.FilterRegistration.Dynamic addFilter(String filterName, javax.servlet.Filter filter)
- Specified by:
addFilterin interfacejavax.servlet.ServletContext
-
addFilter
public javax.servlet.FilterRegistration.Dynamic addFilter(String filterName, Class<? extends javax.servlet.Filter> filterClass)
- Specified by:
addFilterin interfacejavax.servlet.ServletContext
-
createFilter
public <T extends javax.servlet.Filter> T createFilter(Class<T> clazz)
- Specified by:
createFilterin interfacejavax.servlet.ServletContext
-
getFilterRegistration
public javax.servlet.FilterRegistration getFilterRegistration(String filterName)
- Specified by:
getFilterRegistrationin interfacejavax.servlet.ServletContext
-
getFilterRegistrations
public Map<String,? extends javax.servlet.FilterRegistration> getFilterRegistrations()
- Specified by:
getFilterRegistrationsin interfacejavax.servlet.ServletContext
-
getSessionCookieConfig
public javax.servlet.SessionCookieConfig getSessionCookieConfig()
- Specified by:
getSessionCookieConfigin interfacejavax.servlet.ServletContext
-
setSessionTrackingModes
public void setSessionTrackingModes(Set<javax.servlet.SessionTrackingMode> sessionTrackingModes)
- Specified by:
setSessionTrackingModesin interfacejavax.servlet.ServletContext
-
getDefaultSessionTrackingModes
public Set<javax.servlet.SessionTrackingMode> getDefaultSessionTrackingModes()
- Specified by:
getDefaultSessionTrackingModesin interfacejavax.servlet.ServletContext
-
getEffectiveSessionTrackingModes
public Set<javax.servlet.SessionTrackingMode> getEffectiveSessionTrackingModes()
- Specified by:
getEffectiveSessionTrackingModesin interfacejavax.servlet.ServletContext
-
addListener
public void addListener(String className)
- Specified by:
addListenerin interfacejavax.servlet.ServletContext
-
addListener
public <T extends EventListener> void addListener(T t)
- Specified by:
addListenerin interfacejavax.servlet.ServletContext
-
addListener
public void addListener(Class<? extends EventListener> listenerClass)
- Specified by:
addListenerin interfacejavax.servlet.ServletContext
-
createListener
public <T extends EventListener> T createListener(Class<T> clazz)
- Specified by:
createListenerin interfacejavax.servlet.ServletContext
-
getJspConfigDescriptor
public javax.servlet.descriptor.JspConfigDescriptor getJspConfigDescriptor()
- Specified by:
getJspConfigDescriptorin interfacejavax.servlet.ServletContext
-
getClassLoader
public ClassLoader getClassLoader()
- Specified by:
getClassLoaderin interfacejavax.servlet.ServletContext
-
declareRoles
public void declareRoles(String... roleNames)
- Specified by:
declareRolesin interfacejavax.servlet.ServletContext
-
getVirtualServerName
public String getVirtualServerName()
- Specified by:
getVirtualServerNamein interfacejavax.servlet.ServletContext
-
-