Class MockServletContext
- java.lang.Object
-
- org.apache.wicket.protocol.http.mock.MockServletContext
-
- All Implemented Interfaces:
javax.servlet.ServletContext
public class MockServletContext extends Object implements javax.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
-
-
Constructor Summary
Constructors Constructor Description MockServletContext(Application application, String path)
Create the mock object.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description javax.servlet.FilterRegistration.Dynamic
addFilter(String filterName, Class<? extends javax.servlet.Filter> filterClass)
javax.servlet.FilterRegistration.Dynamic
addFilter(String filterName, String className)
javax.servlet.FilterRegistration.Dynamic
addFilter(String filterName, javax.servlet.Filter filter)
void
addInitParameter(String name, String value)
Add an init parameter.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.javax.servlet.ServletRegistration.Dynamic
addServlet(String servletName, Class<? extends javax.servlet.Servlet> servletClass)
javax.servlet.ServletRegistration.Dynamic
addServlet(String servletName, String className)
javax.servlet.ServletRegistration.Dynamic
addServlet(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)
void
declareRoles(String... roleNames)
Object
getAttribute(String name)
Get an attribute with the given name.Enumeration<String>
getAttributeNames()
Get all of the attribute names.ClassLoader
getClassLoader()
javax.servlet.ServletContext
getContext(String name)
Get the context for the given URL pathString
getContextPath()
Set<javax.servlet.SessionTrackingMode>
getDefaultSessionTrackingModes()
int
getEffectiveMajorVersion()
int
getEffectiveMinorVersion()
Set<javax.servlet.SessionTrackingMode>
getEffectiveSessionTrackingModes()
javax.servlet.FilterRegistration
getFilterRegistration(String filterName)
Map<String,? extends javax.servlet.FilterRegistration>
getFilterRegistrations()
String
getInitParameter(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.JspConfigDescriptor
getJspConfigDescriptor()
int
getMajorVersion()
String
getMimeType(String name)
Get the mime type for the given file.int
getMinorVersion()
javax.servlet.RequestDispatcher
getNamedDispatcher(String name)
Wicket does not use the RequestDispatcher, so this implementation just returns a dummy value.String
getRealPath(String name)
Get the real file path of the given resource name.javax.servlet.RequestDispatcher
getRequestDispatcher(String name)
Wicket does not use the RequestDispatcher, so this implementation just returns a dummy value.URL
getResource(String name)
Get the URL for a particular resource that is relative to the web app root directory.InputStream
getResourceAsStream(String name)
Get an input stream for a particular resource that is relative to the web app root directory.Set<String>
getResourcePaths(String name)
Get the resource paths starting from the web app root directory and then relative to the the given name.String
getServerInfo()
Get the server info.javax.servlet.Servlet
getServlet(String name)
NOT USED - Servlet Spec requires that this always returns null.String
getServletContextName()
Return the name of the servlet context.Enumeration<String>
getServletNames()
NOT USED - Servlet spec requires that this always returns null.javax.servlet.ServletRegistration
getServletRegistration(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.SessionCookieConfig
getSessionCookieConfig()
String
getVirtualServerName()
void
log(Exception e, String msg)
As part of testing we always log to the console.void
log(String msg)
As part of testing we always log to the console.void
log(String msg, Throwable cause)
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
setSessionTrackingModes(Set<javax.servlet.SessionTrackingMode> sessionTrackingModes)
-
-
-
Constructor Detail
-
MockServletContext
public MockServletContext(Application application, String path)
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 Detail
-
addInitParameter
public void addInitParameter(String name, String value)
Add an init parameter.- Parameters:
name
- The parameter namevalue
- The parameter value
-
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:
getAttribute
in 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:
getAttributeNames
in 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:
getContext
in interfacejavax.servlet.ServletContext
- Parameters:
name
- The url path- Returns:
- Always returns this
-
getInitParameter
public String getInitParameter(String name)
Get the init parameter with the given name.- Specified by:
getInitParameter
in 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:
getInitParameterNames
in interfacejavax.servlet.ServletContext
- Returns:
- The init parameter names
-
setInitParameter
public boolean setInitParameter(String name, String value)
- Specified by:
setInitParameter
in interfacejavax.servlet.ServletContext
-
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.- Specified by:
getMimeType
in interfacejavax.servlet.ServletContext
- Parameters:
name
- The name to get the mime type for- Returns:
- The mime type
-
getMajorVersion
public int getMajorVersion()
- Specified by:
getMajorVersion
in interfacejavax.servlet.ServletContext
-
getMinorVersion
public int getMinorVersion()
- Specified by:
getMinorVersion
in interfacejavax.servlet.ServletContext
-
getEffectiveMajorVersion
public int getEffectiveMajorVersion()
- Specified by:
getEffectiveMajorVersion
in interfacejavax.servlet.ServletContext
-
getEffectiveMinorVersion
public int getEffectiveMinorVersion()
- Specified by:
getEffectiveMinorVersion
in interfacejavax.servlet.ServletContext
-
getNamedDispatcher
public javax.servlet.RequestDispatcher getNamedDispatcher(String name)
Wicket does not use the RequestDispatcher, so this implementation just returns a dummy value.- Specified by:
getNamedDispatcher
in interfacejavax.servlet.ServletContext
- Parameters:
name
- The name of the servlet or JSP- Returns:
- The dispatcher
-
getRealPath
public String getRealPath(String name)
Get the real file path of the given resource name.- Specified by:
getRealPath
in interfacejavax.servlet.ServletContext
- Parameters:
name
- The name- Returns:
- The real path or null
-
getRequestDispatcher
public javax.servlet.RequestDispatcher getRequestDispatcher(String name)
Wicket does not use the RequestDispatcher, so this implementation just returns a dummy value.- Specified by:
getRequestDispatcher
in interfacejavax.servlet.ServletContext
- Parameters:
name
- The name of the resource to get the dispatcher for- Returns:
- The dispatcher
-
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:
getResource
in 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.- Specified by:
getResourceAsStream
in interfacejavax.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
public Set<String> getResourcePaths(String name)
Get the resource paths starting from the web app root directory and then relative to the the given name.- Specified by:
getResourcePaths
in 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:
getServerInfo
in interfacejavax.servlet.ServletContext
- Returns:
- The server info
-
getServlet
public javax.servlet.Servlet getServlet(String name) throws javax.servlet.ServletException
NOT USED - Servlet Spec requires that this always returns null.- Specified by:
getServlet
in interfacejavax.servlet.ServletContext
- Parameters:
name
- Not used- Returns:
- null
- Throws:
javax.servlet.ServletException
- Not used
-
getServletContextName
public String getServletContextName()
Return the name of the servlet context.- Specified by:
getServletContextName
in interfacejavax.servlet.ServletContext
- Returns:
- The name
-
addServlet
public javax.servlet.ServletRegistration.Dynamic addServlet(String servletName, String className)
- Specified by:
addServlet
in interfacejavax.servlet.ServletContext
-
addServlet
public javax.servlet.ServletRegistration.Dynamic addServlet(String servletName, javax.servlet.Servlet servlet)
- Specified by:
addServlet
in interfacejavax.servlet.ServletContext
-
addServlet
public javax.servlet.ServletRegistration.Dynamic addServlet(String servletName, Class<? extends javax.servlet.Servlet> servletClass)
- Specified by:
addServlet
in interfacejavax.servlet.ServletContext
-
createServlet
public <T extends javax.servlet.Servlet> T createServlet(Class<T> clazz) throws javax.servlet.ServletException
- Specified by:
createServlet
in interfacejavax.servlet.ServletContext
- Throws:
javax.servlet.ServletException
-
getServletRegistration
public javax.servlet.ServletRegistration getServletRegistration(String servletName)
- Specified by:
getServletRegistration
in interfacejavax.servlet.ServletContext
-
getServletRegistrations
public Map<String,? extends javax.servlet.ServletRegistration> getServletRegistrations()
- Specified by:
getServletRegistrations
in interfacejavax.servlet.ServletContext
-
addFilter
public javax.servlet.FilterRegistration.Dynamic addFilter(String filterName, String className)
- Specified by:
addFilter
in interfacejavax.servlet.ServletContext
-
addFilter
public javax.servlet.FilterRegistration.Dynamic addFilter(String filterName, javax.servlet.Filter filter)
- Specified by:
addFilter
in interfacejavax.servlet.ServletContext
-
addFilter
public javax.servlet.FilterRegistration.Dynamic addFilter(String filterName, Class<? extends javax.servlet.Filter> filterClass)
- Specified by:
addFilter
in interfacejavax.servlet.ServletContext
-
createFilter
public <T extends javax.servlet.Filter> T createFilter(Class<T> clazz) throws javax.servlet.ServletException
- Specified by:
createFilter
in interfacejavax.servlet.ServletContext
- Throws:
javax.servlet.ServletException
-
getFilterRegistration
public javax.servlet.FilterRegistration getFilterRegistration(String filterName)
- Specified by:
getFilterRegistration
in interfacejavax.servlet.ServletContext
-
getFilterRegistrations
public Map<String,? extends javax.servlet.FilterRegistration> getFilterRegistrations()
- Specified by:
getFilterRegistrations
in interfacejavax.servlet.ServletContext
-
getSessionCookieConfig
public javax.servlet.SessionCookieConfig getSessionCookieConfig()
- Specified by:
getSessionCookieConfig
in interfacejavax.servlet.ServletContext
-
setSessionTrackingModes
public void setSessionTrackingModes(Set<javax.servlet.SessionTrackingMode> sessionTrackingModes)
- Specified by:
setSessionTrackingModes
in interfacejavax.servlet.ServletContext
-
getDefaultSessionTrackingModes
public Set<javax.servlet.SessionTrackingMode> getDefaultSessionTrackingModes()
- Specified by:
getDefaultSessionTrackingModes
in interfacejavax.servlet.ServletContext
-
getEffectiveSessionTrackingModes
public Set<javax.servlet.SessionTrackingMode> getEffectiveSessionTrackingModes()
- Specified by:
getEffectiveSessionTrackingModes
in interfacejavax.servlet.ServletContext
-
addListener
public void addListener(String className)
- Specified by:
addListener
in interfacejavax.servlet.ServletContext
-
addListener
public <T extends EventListener> void addListener(T t)
- Specified by:
addListener
in interfacejavax.servlet.ServletContext
-
addListener
public void addListener(Class<? extends EventListener> listenerClass)
- Specified by:
addListener
in interfacejavax.servlet.ServletContext
-
createListener
public <T extends EventListener> T createListener(Class<T> clazz) throws javax.servlet.ServletException
- Specified by:
createListener
in interfacejavax.servlet.ServletContext
- Throws:
javax.servlet.ServletException
-
getJspConfigDescriptor
public javax.servlet.descriptor.JspConfigDescriptor getJspConfigDescriptor()
- Specified by:
getJspConfigDescriptor
in interfacejavax.servlet.ServletContext
-
getClassLoader
public ClassLoader getClassLoader()
- Specified by:
getClassLoader
in interfacejavax.servlet.ServletContext
-
declareRoles
public void declareRoles(String... roleNames)
- Specified by:
declareRoles
in interfacejavax.servlet.ServletContext
-
getVirtualServerName
public String getVirtualServerName()
- Specified by:
getVirtualServerName
in interfacejavax.servlet.ServletContext
-
getServletNames
public Enumeration<String> getServletNames()
NOT USED - Servlet spec requires that this always returns null.- Specified by:
getServletNames
in interfacejavax.servlet.ServletContext
- Returns:
- null
-
getServlets
public Enumeration<javax.servlet.Servlet> getServlets()
NOT USED - Servlet spec requires that this always returns null.- Specified by:
getServlets
in interfacejavax.servlet.ServletContext
- Returns:
- null
-
log
public void log(Exception e, String msg)
As part of testing we always log to the console.- Specified by:
log
in interfacejavax.servlet.ServletContext
- Parameters:
e
- The exception to logmsg
- The message to log
-
log
public void log(String msg)
As part of testing we always log to the console.- Specified by:
log
in interfacejavax.servlet.ServletContext
- Parameters:
msg
- The message to log
-
log
public void log(String msg, Throwable cause)
As part of testing we always log to the console.- Specified by:
log
in 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:
removeAttribute
in interfacejavax.servlet.ServletContext
- Parameters:
name
- The name
-
setAttribute
public void setAttribute(String name, Object o)
Set an attribute.- Specified by:
setAttribute
in interfacejavax.servlet.ServletContext
- Parameters:
name
- The name of the attributeo
- The value
-
getContextPath
public String getContextPath()
- Specified by:
getContextPath
in interfacejavax.servlet.ServletContext
- Returns:
- context path
-
-