Class MockServletContext
- java.lang.Object
-
- org.apache.wicket.protocol.http.mock.MockServletContext
-
- All Implemented Interfaces:
javax.servlet.ServletContext
public class MockServletContext extends java.lang.Object implements javax.servlet.ServletContextMock 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, java.lang.String path)Create the mock object.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description javax.servlet.FilterRegistration.DynamicaddFilter(java.lang.String filterName, java.lang.Class<? extends javax.servlet.Filter> filterClass)javax.servlet.FilterRegistration.DynamicaddFilter(java.lang.String filterName, java.lang.String className)javax.servlet.FilterRegistration.DynamicaddFilter(java.lang.String filterName, javax.servlet.Filter filter)voidaddInitParameter(java.lang.String name, java.lang.String value)Add an init parameter.voidaddListener(java.lang.Class<? extends java.util.EventListener> listenerClass)voidaddListener(java.lang.String className)<T extends java.util.EventListener>
voidaddListener(T t)voidaddMimeType(java.lang.String fileExtension, java.lang.String mimeType)Add a new recognized mime type.javax.servlet.ServletRegistration.DynamicaddServlet(java.lang.String servletName, java.lang.Class<? extends javax.servlet.Servlet> servletClass)javax.servlet.ServletRegistration.DynamicaddServlet(java.lang.String servletName, java.lang.String className)javax.servlet.ServletRegistration.DynamicaddServlet(java.lang.String servletName, javax.servlet.Servlet servlet)<T extends javax.servlet.Filter>
TcreateFilter(java.lang.Class<T> clazz)<T extends java.util.EventListener>
TcreateListener(java.lang.Class<T> clazz)<T extends javax.servlet.Servlet>
TcreateServlet(java.lang.Class<T> clazz)voiddeclareRoles(java.lang.String... roleNames)java.lang.ObjectgetAttribute(java.lang.String name)Get an attribute with the given name.java.util.Enumeration<java.lang.String>getAttributeNames()Get all of the attribute names.java.lang.ClassLoadergetClassLoader()javax.servlet.ServletContextgetContext(java.lang.String name)Get the context for the given URL pathjava.lang.StringgetContextPath()java.util.Set<javax.servlet.SessionTrackingMode>getDefaultSessionTrackingModes()intgetEffectiveMajorVersion()intgetEffectiveMinorVersion()java.util.Set<javax.servlet.SessionTrackingMode>getEffectiveSessionTrackingModes()javax.servlet.FilterRegistrationgetFilterRegistration(java.lang.String filterName)java.util.Map<java.lang.String,? extends javax.servlet.FilterRegistration>getFilterRegistrations()java.lang.StringgetInitParameter(java.lang.String name)Get the init parameter with the given name.java.util.Enumeration<java.lang.String>getInitParameterNames()Get the name of all of the init parameters.javax.servlet.descriptor.JspConfigDescriptorgetJspConfigDescriptor()intgetMajorVersion()java.lang.StringgetMimeType(java.lang.String name)Get the mime type for the given file.intgetMinorVersion()javax.servlet.RequestDispatchergetNamedDispatcher(java.lang.String name)Wicket does not use the RequestDispatcher, so this implementation just returns a dummy value.java.lang.StringgetRealPath(java.lang.String name)Get the real file path of the given resource name.javax.servlet.RequestDispatchergetRequestDispatcher(java.lang.String name)Wicket does not use the RequestDispatcher, so this implementation just returns a dummy value.java.net.URLgetResource(java.lang.String name)Get the URL for a particular resource that is relative to the web app root directory.java.io.InputStreamgetResourceAsStream(java.lang.String name)Get an input stream for a particular resource that is relative to the web app root directory.java.util.Set<java.lang.String>getResourcePaths(java.lang.String name)Get the resource paths starting from the web app root directory and then relative to the the given name.java.lang.StringgetServerInfo()Get the server info.javax.servlet.ServletgetServlet(java.lang.String name)NOT USED - Servlet Spec requires that this always returns null.java.lang.StringgetServletContextName()Return the name of the servlet context.java.util.Enumeration<java.lang.String>getServletNames()NOT USED - Servlet spec requires that this always returns null.javax.servlet.ServletRegistrationgetServletRegistration(java.lang.String servletName)java.util.Map<java.lang.String,? extends javax.servlet.ServletRegistration>getServletRegistrations()java.util.Enumeration<javax.servlet.Servlet>getServlets()NOT USED - Servlet spec requires that this always returns null.javax.servlet.SessionCookieConfiggetSessionCookieConfig()java.lang.StringgetVirtualServerName()voidlog(java.lang.Exception e, java.lang.String msg)As part of testing we always log to the console.voidlog(java.lang.String msg)As part of testing we always log to the console.voidlog(java.lang.String msg, java.lang.Throwable cause)As part of testing we always log to the console.voidremoveAttribute(java.lang.String name)Remove an attribute with the given name.voidsetAttribute(java.lang.String name, java.lang.Object o)Set an attribute.booleansetInitParameter(java.lang.String name, java.lang.String value)voidsetSessionTrackingModes(java.util.Set<javax.servlet.SessionTrackingMode> sessionTrackingModes)
-
-
-
Constructor Detail
-
MockServletContext
public MockServletContext(Application application, java.lang.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(java.lang.String name, java.lang.String value)Add an init parameter.- Parameters:
name- The parameter namevalue- The parameter value
-
addMimeType
public void addMimeType(java.lang.String fileExtension, java.lang.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 java.lang.Object getAttribute(java.lang.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 java.util.Enumeration<java.lang.String> getAttributeNames()
Get all of the attribute names.- Specified by:
getAttributeNamesin interfacejavax.servlet.ServletContext- Returns:
- The attribute names
-
getContext
public javax.servlet.ServletContext getContext(java.lang.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
-
getInitParameter
public java.lang.String getInitParameter(java.lang.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 java.util.Enumeration<java.lang.String> getInitParameterNames()
Get the name of all of the init parameters.- Specified by:
getInitParameterNamesin interfacejavax.servlet.ServletContext- Returns:
- The init parameter names
-
setInitParameter
public boolean setInitParameter(java.lang.String name, java.lang.String value)- Specified by:
setInitParameterin interfacejavax.servlet.ServletContext
-
getMimeType
public java.lang.String getMimeType(java.lang.String name)
Get the mime type for the given file. Uses a hardcoded map of mime types set at Initialization time.- Specified by:
getMimeTypein interfacejavax.servlet.ServletContext- Parameters:
name- The name to get the mime type for- Returns:
- The mime type
-
getMajorVersion
public int getMajorVersion()
- Specified by:
getMajorVersionin interfacejavax.servlet.ServletContext
-
getMinorVersion
public int getMinorVersion()
- Specified by:
getMinorVersionin interfacejavax.servlet.ServletContext
-
getEffectiveMajorVersion
public int getEffectiveMajorVersion()
- Specified by:
getEffectiveMajorVersionin interfacejavax.servlet.ServletContext
-
getEffectiveMinorVersion
public int getEffectiveMinorVersion()
- Specified by:
getEffectiveMinorVersionin interfacejavax.servlet.ServletContext
-
getNamedDispatcher
public javax.servlet.RequestDispatcher getNamedDispatcher(java.lang.String name)
Wicket does not use the RequestDispatcher, so this implementation just returns a dummy value.- Specified by:
getNamedDispatcherin interfacejavax.servlet.ServletContext- Parameters:
name- The name of the servlet or JSP- Returns:
- The dispatcher
-
getRealPath
public java.lang.String getRealPath(java.lang.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(java.lang.String name)
Wicket does not use the RequestDispatcher, so this implementation just returns a dummy value.- Specified by:
getRequestDispatcherin interfacejavax.servlet.ServletContext- Parameters:
name- The name of the resource to get the dispatcher for- Returns:
- The dispatcher
-
getResource
public java.net.URL getResource(java.lang.String name) throws java.net.MalformedURLExceptionGet 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:
java.net.MalformedURLException- If the URL is invalid
-
getResourceAsStream
public java.io.InputStream getResourceAsStream(java.lang.String name)
Get an input stream for a particular resource that is relative to the web app root directory.- Specified by:
getResourceAsStreamin 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 java.util.Set<java.lang.String> getResourcePaths(java.lang.String name)
Get the resource paths starting from the web app root directory and then relative to the 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 java.lang.String getServerInfo()
Get the server info.- Specified by:
getServerInfoin interfacejavax.servlet.ServletContext- Returns:
- The server info
-
getServlet
public javax.servlet.Servlet getServlet(java.lang.String name) throws javax.servlet.ServletExceptionNOT USED - Servlet Spec requires that this always returns null.- Specified by:
getServletin interfacejavax.servlet.ServletContext- Parameters:
name- Not used- Returns:
- null
- Throws:
javax.servlet.ServletException- Not used
-
getServletContextName
public java.lang.String getServletContextName()
Return the name of the servlet context.- Specified by:
getServletContextNamein interfacejavax.servlet.ServletContext- Returns:
- The name
-
addServlet
public javax.servlet.ServletRegistration.Dynamic addServlet(java.lang.String servletName, java.lang.String className)- Specified by:
addServletin interfacejavax.servlet.ServletContext
-
addServlet
public javax.servlet.ServletRegistration.Dynamic addServlet(java.lang.String servletName, javax.servlet.Servlet servlet)- Specified by:
addServletin interfacejavax.servlet.ServletContext
-
addServlet
public javax.servlet.ServletRegistration.Dynamic addServlet(java.lang.String servletName, java.lang.Class<? extends javax.servlet.Servlet> servletClass)- Specified by:
addServletin interfacejavax.servlet.ServletContext
-
createServlet
public <T extends javax.servlet.Servlet> T createServlet(java.lang.Class<T> clazz) throws javax.servlet.ServletException- Specified by:
createServletin interfacejavax.servlet.ServletContext- Throws:
javax.servlet.ServletException
-
getServletRegistration
public javax.servlet.ServletRegistration getServletRegistration(java.lang.String servletName)
- Specified by:
getServletRegistrationin interfacejavax.servlet.ServletContext
-
getServletRegistrations
public java.util.Map<java.lang.String,? extends javax.servlet.ServletRegistration> getServletRegistrations()
- Specified by:
getServletRegistrationsin interfacejavax.servlet.ServletContext
-
addFilter
public javax.servlet.FilterRegistration.Dynamic addFilter(java.lang.String filterName, java.lang.String className)- Specified by:
addFilterin interfacejavax.servlet.ServletContext
-
addFilter
public javax.servlet.FilterRegistration.Dynamic addFilter(java.lang.String filterName, javax.servlet.Filter filter)- Specified by:
addFilterin interfacejavax.servlet.ServletContext
-
addFilter
public javax.servlet.FilterRegistration.Dynamic addFilter(java.lang.String filterName, java.lang.Class<? extends javax.servlet.Filter> filterClass)- Specified by:
addFilterin interfacejavax.servlet.ServletContext
-
createFilter
public <T extends javax.servlet.Filter> T createFilter(java.lang.Class<T> clazz) throws javax.servlet.ServletException- Specified by:
createFilterin interfacejavax.servlet.ServletContext- Throws:
javax.servlet.ServletException
-
getFilterRegistration
public javax.servlet.FilterRegistration getFilterRegistration(java.lang.String filterName)
- Specified by:
getFilterRegistrationin interfacejavax.servlet.ServletContext
-
getFilterRegistrations
public java.util.Map<java.lang.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(java.util.Set<javax.servlet.SessionTrackingMode> sessionTrackingModes)
- Specified by:
setSessionTrackingModesin interfacejavax.servlet.ServletContext
-
getDefaultSessionTrackingModes
public java.util.Set<javax.servlet.SessionTrackingMode> getDefaultSessionTrackingModes()
- Specified by:
getDefaultSessionTrackingModesin interfacejavax.servlet.ServletContext
-
getEffectiveSessionTrackingModes
public java.util.Set<javax.servlet.SessionTrackingMode> getEffectiveSessionTrackingModes()
- Specified by:
getEffectiveSessionTrackingModesin interfacejavax.servlet.ServletContext
-
addListener
public void addListener(java.lang.String className)
- Specified by:
addListenerin interfacejavax.servlet.ServletContext
-
addListener
public <T extends java.util.EventListener> void addListener(T t)
- Specified by:
addListenerin interfacejavax.servlet.ServletContext
-
addListener
public void addListener(java.lang.Class<? extends java.util.EventListener> listenerClass)
- Specified by:
addListenerin interfacejavax.servlet.ServletContext
-
createListener
public <T extends java.util.EventListener> T createListener(java.lang.Class<T> clazz) throws javax.servlet.ServletException- Specified by:
createListenerin interfacejavax.servlet.ServletContext- Throws:
javax.servlet.ServletException
-
getJspConfigDescriptor
public javax.servlet.descriptor.JspConfigDescriptor getJspConfigDescriptor()
- Specified by:
getJspConfigDescriptorin interfacejavax.servlet.ServletContext
-
getClassLoader
public java.lang.ClassLoader getClassLoader()
- Specified by:
getClassLoaderin interfacejavax.servlet.ServletContext
-
declareRoles
public void declareRoles(java.lang.String... roleNames)
- Specified by:
declareRolesin interfacejavax.servlet.ServletContext
-
getVirtualServerName
public java.lang.String getVirtualServerName()
- Specified by:
getVirtualServerNamein interfacejavax.servlet.ServletContext
-
getServletNames
public java.util.Enumeration<java.lang.String> getServletNames()
NOT USED - Servlet spec requires that this always returns null.- Specified by:
getServletNamesin interfacejavax.servlet.ServletContext- Returns:
- null
-
getServlets
public java.util.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(java.lang.Exception e, java.lang.String msg)As part of testing we always log to the console.- Specified by:
login interfacejavax.servlet.ServletContext- Parameters:
e- The exception to logmsg- The message to log
-
log
public void log(java.lang.String msg)
As part of testing we always log to the console.- Specified by:
login interfacejavax.servlet.ServletContext- Parameters:
msg- The message to log
-
log
public void log(java.lang.String msg, java.lang.Throwable cause)As part of testing we always log to the console.- Specified by:
login interfacejavax.servlet.ServletContext- Parameters:
msg- The message to logcause- The cause exception
-
removeAttribute
public void removeAttribute(java.lang.String name)
Remove an attribute with the given name.- Specified by:
removeAttributein interfacejavax.servlet.ServletContext- Parameters:
name- The name
-
setAttribute
public void setAttribute(java.lang.String name, java.lang.Object o)Set an attribute.- Specified by:
setAttributein interfacejavax.servlet.ServletContext- Parameters:
name- The name of the attributeo- The value
-
getContextPath
public java.lang.String getContextPath()
- Specified by:
getContextPathin interfacejavax.servlet.ServletContext- Returns:
- context path
-
-