public class MockServletContext extends Object implements javax.servlet.ServletContext
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.
Constructor and Description |
---|
MockServletContext(Application application,
String path)
Create the mock object.
|
Modifier and Type | Method and Description |
---|---|
javax.servlet.FilterRegistration.Dynamic |
addFilter(String filterName,
Class<? extends javax.servlet.Filter> filterClass) |
javax.servlet.FilterRegistration.Dynamic |
addFilter(String filterName,
javax.servlet.Filter filter) |
javax.servlet.FilterRegistration.Dynamic |
addFilter(String filterName,
String className) |
void |
addInitParameter(String name,
String value)
Add an init parameter.
|
void |
addListener(Class<? extends EventListener> listenerClass) |
void |
addListener(String className) |
<T extends EventListener> |
addListener(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,
javax.servlet.Servlet servlet) |
javax.servlet.ServletRegistration.Dynamic |
addServlet(String servletName,
String className) |
<T extends javax.servlet.Filter> |
createFilter(Class<T> clazz) |
<T extends EventListener> |
createListener(Class<T> clazz) |
<T extends javax.servlet.Servlet> |
createServlet(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 path
|
String |
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) |
public MockServletContext(Application application, String path)
System.getProperty("user.dir") +
"/src/webapp"
application
- The application that this context is forpath
- The path to the root of the web applicationpublic void addInitParameter(String name, String value)
name
- The parameter namevalue
- The parameter valuepublic void addMimeType(String fileExtension, String mimeType)
fileExtension
- The file extension (e.g. "jpg")mimeType
- The mime type (e.g. "image/jpeg")public Object getAttribute(String name)
getAttribute
in interface javax.servlet.ServletContext
name
- The attribute namepublic Enumeration<String> getAttributeNames()
getAttributeNames
in interface javax.servlet.ServletContext
public javax.servlet.ServletContext getContext(String name)
getContext
in interface javax.servlet.ServletContext
name
- The url pathpublic String getInitParameter(String name)
getInitParameter
in interface javax.servlet.ServletContext
name
- The namepublic Enumeration<String> getInitParameterNames()
getInitParameterNames
in interface javax.servlet.ServletContext
public boolean setInitParameter(String name, String value)
setInitParameter
in interface javax.servlet.ServletContext
public String getMimeType(String name)
getMimeType
in interface javax.servlet.ServletContext
name
- The name to get the mime type forpublic int getMajorVersion()
getMajorVersion
in interface javax.servlet.ServletContext
public int getMinorVersion()
getMinorVersion
in interface javax.servlet.ServletContext
public int getEffectiveMajorVersion()
getEffectiveMajorVersion
in interface javax.servlet.ServletContext
public int getEffectiveMinorVersion()
getEffectiveMinorVersion
in interface javax.servlet.ServletContext
public javax.servlet.RequestDispatcher getNamedDispatcher(String name)
getNamedDispatcher
in interface javax.servlet.ServletContext
name
- The name of the servlet or JSPpublic String getRealPath(String name)
getRealPath
in interface javax.servlet.ServletContext
name
- The namepublic javax.servlet.RequestDispatcher getRequestDispatcher(String name)
getRequestDispatcher
in interface javax.servlet.ServletContext
name
- The name of the resource to get the dispatcher forpublic URL getResource(String name) throws MalformedURLException
getResource
in interface javax.servlet.ServletContext
name
- The name of the resource to getMalformedURLException
- If the URL is invalidpublic InputStream getResourceAsStream(String name)
getResourceAsStream
in interface javax.servlet.ServletContext
name
- The name of the resource to getpublic Set<String> getResourcePaths(String name)
getResourcePaths
in interface javax.servlet.ServletContext
name
- The starting namepublic String getServerInfo()
getServerInfo
in interface javax.servlet.ServletContext
public javax.servlet.Servlet getServlet(String name) throws javax.servlet.ServletException
getServlet
in interface javax.servlet.ServletContext
name
- Not usedjavax.servlet.ServletException
- Not usedpublic String getServletContextName()
getServletContextName
in interface javax.servlet.ServletContext
public javax.servlet.ServletRegistration.Dynamic addServlet(String servletName, String className)
addServlet
in interface javax.servlet.ServletContext
public javax.servlet.ServletRegistration.Dynamic addServlet(String servletName, javax.servlet.Servlet servlet)
addServlet
in interface javax.servlet.ServletContext
public javax.servlet.ServletRegistration.Dynamic addServlet(String servletName, Class<? extends javax.servlet.Servlet> servletClass)
addServlet
in interface javax.servlet.ServletContext
public <T extends javax.servlet.Servlet> T createServlet(Class<T> clazz) throws javax.servlet.ServletException
createServlet
in interface javax.servlet.ServletContext
javax.servlet.ServletException
public javax.servlet.ServletRegistration getServletRegistration(String servletName)
getServletRegistration
in interface javax.servlet.ServletContext
public Map<String,? extends javax.servlet.ServletRegistration> getServletRegistrations()
getServletRegistrations
in interface javax.servlet.ServletContext
public javax.servlet.FilterRegistration.Dynamic addFilter(String filterName, String className)
addFilter
in interface javax.servlet.ServletContext
public javax.servlet.FilterRegistration.Dynamic addFilter(String filterName, javax.servlet.Filter filter)
addFilter
in interface javax.servlet.ServletContext
public javax.servlet.FilterRegistration.Dynamic addFilter(String filterName, Class<? extends javax.servlet.Filter> filterClass)
addFilter
in interface javax.servlet.ServletContext
public <T extends javax.servlet.Filter> T createFilter(Class<T> clazz) throws javax.servlet.ServletException
createFilter
in interface javax.servlet.ServletContext
javax.servlet.ServletException
public javax.servlet.FilterRegistration getFilterRegistration(String filterName)
getFilterRegistration
in interface javax.servlet.ServletContext
public Map<String,? extends javax.servlet.FilterRegistration> getFilterRegistrations()
getFilterRegistrations
in interface javax.servlet.ServletContext
public javax.servlet.SessionCookieConfig getSessionCookieConfig()
getSessionCookieConfig
in interface javax.servlet.ServletContext
public void setSessionTrackingModes(Set<javax.servlet.SessionTrackingMode> sessionTrackingModes)
setSessionTrackingModes
in interface javax.servlet.ServletContext
public Set<javax.servlet.SessionTrackingMode> getDefaultSessionTrackingModes()
getDefaultSessionTrackingModes
in interface javax.servlet.ServletContext
public Set<javax.servlet.SessionTrackingMode> getEffectiveSessionTrackingModes()
getEffectiveSessionTrackingModes
in interface javax.servlet.ServletContext
public void addListener(String className)
addListener
in interface javax.servlet.ServletContext
public <T extends EventListener> void addListener(T t)
addListener
in interface javax.servlet.ServletContext
public void addListener(Class<? extends EventListener> listenerClass)
addListener
in interface javax.servlet.ServletContext
public <T extends EventListener> T createListener(Class<T> clazz) throws javax.servlet.ServletException
createListener
in interface javax.servlet.ServletContext
javax.servlet.ServletException
public javax.servlet.descriptor.JspConfigDescriptor getJspConfigDescriptor()
getJspConfigDescriptor
in interface javax.servlet.ServletContext
public ClassLoader getClassLoader()
getClassLoader
in interface javax.servlet.ServletContext
public void declareRoles(String... roleNames)
declareRoles
in interface javax.servlet.ServletContext
public String getVirtualServerName()
getVirtualServerName
in interface javax.servlet.ServletContext
public Enumeration<String> getServletNames()
getServletNames
in interface javax.servlet.ServletContext
public Enumeration<javax.servlet.Servlet> getServlets()
getServlets
in interface javax.servlet.ServletContext
public void log(Exception e, String msg)
log
in interface javax.servlet.ServletContext
e
- The exception to logmsg
- The message to logpublic void log(String msg)
log
in interface javax.servlet.ServletContext
msg
- The message to logpublic void log(String msg, Throwable cause)
log
in interface javax.servlet.ServletContext
msg
- The message to logcause
- The cause exceptionpublic void removeAttribute(String name)
removeAttribute
in interface javax.servlet.ServletContext
name
- The namepublic void setAttribute(String name, Object o)
setAttribute
in interface javax.servlet.ServletContext
name
- The name of the attributeo
- The valuepublic String getContextPath()
getContextPath
in interface javax.servlet.ServletContext
Copyright © 2006–2022 Apache Software Foundation. All rights reserved.