Class MockContext

java.lang.Object
com.vaadin.browserless.mocks.MockContext
All Implemented Interfaces:
jakarta.servlet.ServletContext, Serializable

public class MockContext extends Object implements jakarta.servlet.ServletContext, Serializable
A standalone ServletContext, backed by the build output of the module under test.

It models the three things Vaadin asks a context for: init parameters and attributes, which are held in memory and are where the Lookup and the ApplicationConfiguration end up; resource lookup, which serves the files a deployed war would have; and logging, which goes to SLF4J.

Resources are resolved the way a war layout would resolve them, in this order: the realPathRoots of the current module, then META-INF/resources on the classpath (which is where a jar contributes static files), then, for a /VAADIN/ path, the classpath root. A /VAADIN/../ path is normalized and refused, as a container refuses to serve outside the exposed directory.

Everything a test has no use for — servlet, filter and listener registration, request dispatch, session tracking configuration — throws UnsupportedOperationException rather than pretending to succeed, so a test that needs one fails loudly instead of running against a fiction. The reported servlet API version is 3.0, which is what Vaadin's own version checks accept.

For internal use only. May be renamed or removed in a future release.

See Also:
  • Constructor Details

    • MockContext

      public MockContext()
      Creates an empty context.
  • Method Details

    • createServlet

      public <T extends jakarta.servlet.Servlet> T createServlet(Class<T> clazz)
      Specified by:
      createServlet in interface jakarta.servlet.ServletContext
    • getEffectiveMajorVersion

      public int getEffectiveMajorVersion()
      Specified by:
      getEffectiveMajorVersion in interface jakarta.servlet.ServletContext
    • getResource

      public URL getResource(String path) throws MalformedURLException
      Specified by:
      getResource in interface jakarta.servlet.ServletContext
      Throws:
      MalformedURLException
    • addListener

      public void addListener(String className)
      Specified by:
      addListener in interface jakarta.servlet.ServletContext
    • addListener

      public <T extends EventListener> void addListener(T t)
      Specified by:
      addListener in interface jakarta.servlet.ServletContext
    • addListener

      public void addListener(Class<? extends EventListener> listenerClass)
      Specified by:
      addListener in interface jakarta.servlet.ServletContext
    • getClassLoader

      public ClassLoader getClassLoader()
      Specified by:
      getClassLoader in interface jakarta.servlet.ServletContext
    • getAttributeNames

      public Enumeration<String> getAttributeNames()
      Specified by:
      getAttributeNames in interface jakarta.servlet.ServletContext
    • getMajorVersion

      public int getMajorVersion()
      Specified by:
      getMajorVersion in interface jakarta.servlet.ServletContext
    • log

      public void log(String msg)
      Specified by:
      log in interface jakarta.servlet.ServletContext
    • log

      public void log(String message, Throwable throwable)
      Specified by:
      log in interface jakarta.servlet.ServletContext
    • getFilterRegistration

      public jakarta.servlet.FilterRegistration getFilterRegistration(String filterName)
      Specified by:
      getFilterRegistration in interface jakarta.servlet.ServletContext
    • setSessionTrackingModes

      public void setSessionTrackingModes(Set<jakarta.servlet.SessionTrackingMode> sessionTrackingModes)
      Specified by:
      setSessionTrackingModes in interface jakarta.servlet.ServletContext
    • setInitParameter

      public boolean setInitParameter(String name, String value)
      Specified by:
      setInitParameter in interface jakarta.servlet.ServletContext
    • getResourceAsStream

      public InputStream getResourceAsStream(String path)
      Specified by:
      getResourceAsStream in interface jakarta.servlet.ServletContext
    • getNamedDispatcher

      public jakarta.servlet.RequestDispatcher getNamedDispatcher(String name)
      Specified by:
      getNamedDispatcher in interface jakarta.servlet.ServletContext
    • getFilterRegistrations

      public Map<String,? extends jakarta.servlet.FilterRegistration> getFilterRegistrations()
      Specified by:
      getFilterRegistrations in interface jakarta.servlet.ServletContext
    • getDefaultSessionTrackingModes

      public Set<jakarta.servlet.SessionTrackingMode> getDefaultSessionTrackingModes()
      Specified by:
      getDefaultSessionTrackingModes in interface jakarta.servlet.ServletContext
    • getMimeType

      public String getMimeType(String file)
      Specified by:
      getMimeType in interface jakarta.servlet.ServletContext
    • declareRoles

      public void declareRoles(String... roleNames)
      Specified by:
      declareRoles in interface jakarta.servlet.ServletContext
    • createFilter

      public <T extends jakarta.servlet.Filter> T createFilter(Class<T> clazz)
      Specified by:
      createFilter in interface jakarta.servlet.ServletContext
    • getRealPathRoots

      public List<String> getRealPathRoots()
      Returns the module-relative folders getRealPath(String) resolves a path against, in order; the first one holding the file wins.

      These stand in for the war root of a deployed application. Defaults to src/main/webapp/frontend and src/main/webapp.

      Returns:
      the folders searched for a real path
    • setRealPathRoots

      public void setRealPathRoots(List<String> realPathRoots)
      Replaces the folders getRealPath(String) resolves against. Point them elsewhere for a project that keeps its static files in another place.
      Parameters:
      realPathRoots - the module-relative folders to search, in order
    • getRealPath

      public String getRealPath(String path)
      Specified by:
      getRealPath in interface jakarta.servlet.ServletContext
    • getInitParameters

      public Map<String,String> getInitParameters()
      Returns the context init parameters, live and directly modifiable.

      Writing here is not the same as calling setInitParameter(String, String), which follows the servlet contract and refuses to overwrite a parameter that is already set. Most parameters are read once, when ApplicationConfiguration is first created and cached, so a value that has to be seen must be in place before anything else touches the context.

      Returns:
      the init parameters
    • getInitParameter

      public String getInitParameter(String name)
      Specified by:
      getInitParameter in interface jakarta.servlet.ServletContext
    • getMinorVersion

      public int getMinorVersion()
      Specified by:
      getMinorVersion in interface jakarta.servlet.ServletContext
    • getJspConfigDescriptor

      public jakarta.servlet.descriptor.JspConfigDescriptor getJspConfigDescriptor()
      Specified by:
      getJspConfigDescriptor in interface jakarta.servlet.ServletContext
    • removeAttribute

      public void removeAttribute(String name)
      Specified by:
      removeAttribute in interface jakarta.servlet.ServletContext
    • getServletContextName

      public String getServletContextName()
      Specified by:
      getServletContextName in interface jakarta.servlet.ServletContext
    • addFilter

      public jakarta.servlet.FilterRegistration.Dynamic addFilter(String filterName, String className)
      Specified by:
      addFilter in interface jakarta.servlet.ServletContext
    • addFilter

      public jakarta.servlet.FilterRegistration.Dynamic addFilter(String filterName, jakarta.servlet.Filter filter)
      Specified by:
      addFilter in interface jakarta.servlet.ServletContext
    • addFilter

      public jakarta.servlet.FilterRegistration.Dynamic addFilter(String filterName, Class<? extends jakarta.servlet.Filter> filterClass)
      Specified by:
      addFilter in interface jakarta.servlet.ServletContext
    • getContextPath

      public String getContextPath()
      Specified by:
      getContextPath in interface jakarta.servlet.ServletContext
    • getSessionCookieConfig

      public jakarta.servlet.SessionCookieConfig getSessionCookieConfig()
      Specified by:
      getSessionCookieConfig in interface jakarta.servlet.ServletContext
    • getVirtualServerName

      public String getVirtualServerName()
      Specified by:
      getVirtualServerName in interface jakarta.servlet.ServletContext
    • getSessionTimeout

      public int getSessionTimeout()
      Specified by:
      getSessionTimeout in interface jakarta.servlet.ServletContext
    • setSessionTimeout

      public void setSessionTimeout(int sessionTimeout)
      Specified by:
      setSessionTimeout in interface jakarta.servlet.ServletContext
    • getRequestCharacterEncoding

      public String getRequestCharacterEncoding()
      Specified by:
      getRequestCharacterEncoding in interface jakarta.servlet.ServletContext
    • setRequestCharacterEncoding

      public void setRequestCharacterEncoding(String encoding)
      Specified by:
      setRequestCharacterEncoding in interface jakarta.servlet.ServletContext
    • getResponseCharacterEncoding

      public String getResponseCharacterEncoding()
      Specified by:
      getResponseCharacterEncoding in interface jakarta.servlet.ServletContext
    • setResponseCharacterEncoding

      public void setResponseCharacterEncoding(String encoding)
      Specified by:
      setResponseCharacterEncoding in interface jakarta.servlet.ServletContext
    • getContext

      public jakarta.servlet.ServletContext getContext(String uripath)
      Specified by:
      getContext in interface jakarta.servlet.ServletContext
    • getRequestDispatcher

      public jakarta.servlet.RequestDispatcher getRequestDispatcher(String path)
      Specified by:
      getRequestDispatcher in interface jakarta.servlet.ServletContext
    • getAttribute

      public Object getAttribute(String name)
      Specified by:
      getAttribute in interface jakarta.servlet.ServletContext
    • setAttribute

      public void setAttribute(String name, Object value)
      Specified by:
      setAttribute in interface jakarta.servlet.ServletContext
    • getServletRegistration

      public jakarta.servlet.ServletRegistration getServletRegistration(String servletName)
      Specified by:
      getServletRegistration in interface jakarta.servlet.ServletContext
    • createListener

      public <T extends EventListener> T createListener(Class<T> clazz)
      Specified by:
      createListener in interface jakarta.servlet.ServletContext
    • addServlet

      public jakarta.servlet.ServletRegistration.Dynamic addServlet(String servletName, String className)
      Specified by:
      addServlet in interface jakarta.servlet.ServletContext
    • addServlet

      public jakarta.servlet.ServletRegistration.Dynamic addServlet(String servletName, jakarta.servlet.Servlet servlet)
      Specified by:
      addServlet in interface jakarta.servlet.ServletContext
    • addServlet

      public jakarta.servlet.ServletRegistration.Dynamic addServlet(String servletName, Class<? extends jakarta.servlet.Servlet> servletClass)
      Specified by:
      addServlet in interface jakarta.servlet.ServletContext
    • addJspFile

      public jakarta.servlet.ServletRegistration.Dynamic addJspFile(String servletName, String jspFile)
      Specified by:
      addJspFile in interface jakarta.servlet.ServletContext
    • getEffectiveMinorVersion

      public int getEffectiveMinorVersion()
      Specified by:
      getEffectiveMinorVersion in interface jakarta.servlet.ServletContext
    • getServletRegistrations

      public Map<String,? extends jakarta.servlet.ServletRegistration> getServletRegistrations()
      Specified by:
      getServletRegistrations in interface jakarta.servlet.ServletContext
    • getResourcePaths

      public Set<String> getResourcePaths(String path)
      Specified by:
      getResourcePaths in interface jakarta.servlet.ServletContext
    • getInitParameterNames

      public Enumeration<String> getInitParameterNames()
      Specified by:
      getInitParameterNames in interface jakarta.servlet.ServletContext
    • getServerInfo

      public String getServerInfo()
      Specified by:
      getServerInfo in interface jakarta.servlet.ServletContext
    • getEffectiveSessionTrackingModes

      public Set<jakarta.servlet.SessionTrackingMode> getEffectiveSessionTrackingModes()
      Specified by:
      getEffectiveSessionTrackingModes in interface jakarta.servlet.ServletContext