java.lang.Object
org.apache.wicket.protocol.http.mock.MockServletContext
- All Implemented Interfaces:
jakarta.servlet.ServletContext
Mock implementation of the servlet context for testing purposes. This implementation supports all
of the standard context methods except that request dispatching just indicates what is being
dispatched to, rather than doing the actual dispatch.
The context can be configured with a path parameter that should point to an absolute directory location that represents the place where the contents of the WAR bundle are located. Setting this value allows all of the resource location functionality to work as in a fully functioning web application. This value is not set then not resource location functionality will work and instead null will always be returned.
- Author:
- Chris Turner
-
Field Summary
Fields inherited from interface jakarta.servlet.ServletContext
ORDERED_LIBS, TEMPDIR
-
Constructor Summary
ConstructorDescriptionMockServletContext
(Application application, String path) Create the mock object. -
Method Summary
Modifier and TypeMethodDescriptionjakarta.servlet.FilterRegistration.Dynamic
jakarta.servlet.FilterRegistration.Dynamic
jakarta.servlet.FilterRegistration.Dynamic
void
addInitParameter
(String name, String value) Add an init parameter.jakarta.servlet.ServletRegistration.Dynamic
addJspFile
(String s, String s1) 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.jakarta.servlet.ServletRegistration.Dynamic
addServlet
(String servletName, jakarta.servlet.Servlet servlet) jakarta.servlet.ServletRegistration.Dynamic
addServlet
(String servletName, Class<? extends jakarta.servlet.Servlet> servletClass) jakarta.servlet.ServletRegistration.Dynamic
addServlet
(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) void
declareRoles
(String... roleNames) getAttribute
(String name) Get an attribute with the given name.Get all of the attribute names.jakarta.servlet.ServletContext
getContext
(String name) Get the context for the given URL pathSet<jakarta.servlet.SessionTrackingMode>
int
int
Set<jakarta.servlet.SessionTrackingMode>
jakarta.servlet.FilterRegistration
getFilterRegistration
(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.JspConfigDescriptor
int
getMimeType
(String name) Get the mime type for the given file.int
jakarta.servlet.RequestDispatcher
getNamedDispatcher
(String name) Wicket does not use the RequestDispatcher, so this implementation just returns a dummy value.getRealPath
(String name) Get the real file path of the given resource name.jakarta.servlet.RequestDispatcher
getRequestDispatcher
(String name) Wicket does not use the RequestDispatcher, so this implementation just returns a dummy value.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.getResourcePaths
(String name) Get the resource paths starting from the web app root directory and then relative to the the given name.Get the server info.Return the name of the servlet context.jakarta.servlet.ServletRegistration
getServletRegistration
(String servletName) jakarta.servlet.SessionCookieConfig
int
void
As part of testing we always log to the console.void
As part of testing we always log to the console.void
removeAttribute
(String name) Remove an attribute with the given name.void
setAttribute
(String name, Object o) Set an attribute.boolean
setInitParameter
(String name, String value) void
setRequestCharacterEncoding
(String requestCharacterEncoding) void
setResponseCharacterEncoding
(String responseCharacterEncoding) void
setSessionTimeout
(int sessionTimeout) void
setSessionTrackingModes
(Set<jakarta.servlet.SessionTrackingMode> sessionTrackingModes)
-
Constructor Details
-
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"
- Parameters:
application
- The application that this context is forpath
- The path to the root of the web application
-
-
Method Details
-
addInitParameter
Add an init parameter.- Parameters:
name
- The parameter namevalue
- The parameter value
-
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 interfacejakarta.servlet.ServletContext
- Parameters:
name
- The attribute name- Returns:
- The value, or null
-
getAttributeNames
Get all of the attribute names.- Specified by:
getAttributeNames
in interfacejakarta.servlet.ServletContext
- Returns:
- The attribute names
-
getContext
Get the context for the given URL path- Specified by:
getContext
in interfacejakarta.servlet.ServletContext
- Parameters:
name
- The url path- Returns:
- Always returns this
-
getInitParameter
Get the init parameter with the given name.- Specified by:
getInitParameter
in interfacejakarta.servlet.ServletContext
- 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 interfacejakarta.servlet.ServletContext
- Returns:
- The init parameter names
-
setInitParameter
- Specified by:
setInitParameter
in interfacejakarta.servlet.ServletContext
-
getMimeType
Get the mime type for the given file. Uses a hardcoded map of mime types set at Initialization time.- Specified by:
getMimeType
in interfacejakarta.servlet.ServletContext
- Parameters:
name
- The name to get the mime type for- Returns:
- The mime type
-
getMajorVersion
- Specified by:
getMajorVersion
in interfacejakarta.servlet.ServletContext
-
getMinorVersion
- Specified by:
getMinorVersion
in interfacejakarta.servlet.ServletContext
-
getEffectiveMajorVersion
- Specified by:
getEffectiveMajorVersion
in interfacejakarta.servlet.ServletContext
-
getEffectiveMinorVersion
- Specified by:
getEffectiveMinorVersion
in interfacejakarta.servlet.ServletContext
-
getNamedDispatcher
Wicket does not use the RequestDispatcher, so this implementation just returns a dummy value.- Specified by:
getNamedDispatcher
in interfacejakarta.servlet.ServletContext
- Parameters:
name
- The name of the servlet or JSP- Returns:
- The dispatcher
-
getRealPath
Get the real file path of the given resource name.- Specified by:
getRealPath
in interfacejakarta.servlet.ServletContext
- Parameters:
name
- The name- Returns:
- The real path or null
-
getRequestDispatcher
Wicket does not use the RequestDispatcher, so this implementation just returns a dummy value.- Specified by:
getRequestDispatcher
in interfacejakarta.servlet.ServletContext
- Parameters:
name
- The name of the resource to get the dispatcher for- Returns:
- The dispatcher
-
getResource
Get the URL for a particular resource that is relative to the web app root directory.- Specified by:
getResource
in interfacejakarta.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
Get an input stream for a particular resource that is relative to the web app root directory.- Specified by:
getResourceAsStream
in interfacejakarta.servlet.ServletContext
- Parameters:
name
- The name of the resource to get- Returns:
- The input stream for the resource, or null of resource is not found
-
getResourcePaths
Get the resource paths starting from the web app root directory and then relative to the the given name.- Specified by:
getResourcePaths
in interfacejakarta.servlet.ServletContext
- Parameters:
name
- The starting name- Returns:
- The set of resource paths at this location
-
getServerInfo
Get the server info.- Specified by:
getServerInfo
in interfacejakarta.servlet.ServletContext
- Returns:
- The server info
-
getServletContextName
Return the name of the servlet context.- Specified by:
getServletContextName
in interfacejakarta.servlet.ServletContext
- Returns:
- The name
-
addServlet
- Specified by:
addServlet
in interfacejakarta.servlet.ServletContext
-
addServlet
public jakarta.servlet.ServletRegistration.Dynamic addServlet(String servletName, jakarta.servlet.Servlet servlet) - Specified by:
addServlet
in interfacejakarta.servlet.ServletContext
-
addServlet
public jakarta.servlet.ServletRegistration.Dynamic addServlet(String servletName, Class<? extends jakarta.servlet.Servlet> servletClass) - Specified by:
addServlet
in interfacejakarta.servlet.ServletContext
-
addJspFile
- Specified by:
addJspFile
in interfacejakarta.servlet.ServletContext
-
createServlet
public <T extends jakarta.servlet.Servlet> T createServlet(Class<T> clazz) throws jakarta.servlet.ServletException - Specified by:
createServlet
in interfacejakarta.servlet.ServletContext
- Throws:
jakarta.servlet.ServletException
-
getServletRegistration
- Specified by:
getServletRegistration
in interfacejakarta.servlet.ServletContext
-
getServletRegistrations
- Specified by:
getServletRegistrations
in interfacejakarta.servlet.ServletContext
-
addFilter
- Specified by:
addFilter
in interfacejakarta.servlet.ServletContext
-
addFilter
public jakarta.servlet.FilterRegistration.Dynamic addFilter(String filterName, jakarta.servlet.Filter filter) - Specified by:
addFilter
in interfacejakarta.servlet.ServletContext
-
addFilter
public jakarta.servlet.FilterRegistration.Dynamic addFilter(String filterName, Class<? extends jakarta.servlet.Filter> filterClass) - Specified by:
addFilter
in interfacejakarta.servlet.ServletContext
-
createFilter
public <T extends jakarta.servlet.Filter> T createFilter(Class<T> clazz) throws jakarta.servlet.ServletException - Specified by:
createFilter
in interfacejakarta.servlet.ServletContext
- Throws:
jakarta.servlet.ServletException
-
getFilterRegistration
- Specified by:
getFilterRegistration
in interfacejakarta.servlet.ServletContext
-
getFilterRegistrations
- Specified by:
getFilterRegistrations
in interfacejakarta.servlet.ServletContext
-
getSessionCookieConfig
- Specified by:
getSessionCookieConfig
in interfacejakarta.servlet.ServletContext
-
setSessionTrackingModes
- Specified by:
setSessionTrackingModes
in interfacejakarta.servlet.ServletContext
-
getDefaultSessionTrackingModes
- Specified by:
getDefaultSessionTrackingModes
in interfacejakarta.servlet.ServletContext
-
getEffectiveSessionTrackingModes
- Specified by:
getEffectiveSessionTrackingModes
in interfacejakarta.servlet.ServletContext
-
addListener
- Specified by:
addListener
in interfacejakarta.servlet.ServletContext
-
addListener
- Specified by:
addListener
in interfacejakarta.servlet.ServletContext
-
addListener
- Specified by:
addListener
in interfacejakarta.servlet.ServletContext
-
createListener
public <T extends EventListener> T createListener(Class<T> clazz) throws jakarta.servlet.ServletException - Specified by:
createListener
in interfacejakarta.servlet.ServletContext
- Throws:
jakarta.servlet.ServletException
-
getJspConfigDescriptor
- Specified by:
getJspConfigDescriptor
in interfacejakarta.servlet.ServletContext
-
getClassLoader
- Specified by:
getClassLoader
in interfacejakarta.servlet.ServletContext
-
declareRoles
- Specified by:
declareRoles
in interfacejakarta.servlet.ServletContext
-
getVirtualServerName
- Specified by:
getVirtualServerName
in interfacejakarta.servlet.ServletContext
-
getSessionTimeout
- Specified by:
getSessionTimeout
in interfacejakarta.servlet.ServletContext
-
setSessionTimeout
- Specified by:
setSessionTimeout
in interfacejakarta.servlet.ServletContext
-
getRequestCharacterEncoding
- Specified by:
getRequestCharacterEncoding
in interfacejakarta.servlet.ServletContext
-
setRequestCharacterEncoding
- Specified by:
setRequestCharacterEncoding
in interfacejakarta.servlet.ServletContext
-
getResponseCharacterEncoding
- Specified by:
getResponseCharacterEncoding
in interfacejakarta.servlet.ServletContext
-
setResponseCharacterEncoding
- Specified by:
setResponseCharacterEncoding
in interfacejakarta.servlet.ServletContext
-
log
As part of testing we always log to the console.- Specified by:
log
in interfacejakarta.servlet.ServletContext
- Parameters:
msg
- The message to log
-
log
As part of testing we always log to the console.- Specified by:
log
in interfacejakarta.servlet.ServletContext
- Parameters:
msg
- The message to logcause
- The cause exception
-
removeAttribute
Remove an attribute with the given name.- Specified by:
removeAttribute
in interfacejakarta.servlet.ServletContext
- Parameters:
name
- The name
-
setAttribute
Set an attribute.- Specified by:
setAttribute
in interfacejakarta.servlet.ServletContext
- Parameters:
name
- The name of the attributeo
- The value
-
getContextPath
- Specified by:
getContextPath
in interfacejakarta.servlet.ServletContext
- Returns:
- context path
-