org.apache.wicket.protocol.http
Class MockServletContext

java.lang.Object
  extended by org.apache.wicket.protocol.http.MockServletContext
All Implemented Interfaces:
javax.servlet.ServletContext

public class MockServletContext
extends java.lang.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
MockServletContext(Application application, java.lang.String path)
          Create the mock object.
 
Method Summary
 void addInitParameter(java.lang.String name, java.lang.String value)
          Add an init parameter.
 void addMimeType(java.lang.String fileExtension, java.lang.String mimeType)
          Add a new recognized mime type.
 java.lang.Object getAttribute(java.lang.String name)
          Get an attribute with the given name.
 java.util.Enumeration getAttributeNames()
          Get all of the attribute names.
 javax.servlet.ServletContext getContext(java.lang.String name)
          Get the context for the given URL path
 java.lang.String getContextPath()
           
 java.lang.String getInitParameter(java.lang.String name)
          Get the init parameter with the given name.
 java.util.Enumeration getInitParameterNames()
          Get the name of all of the init parameters.
 int getMajorVersion()
           
 java.lang.String getMimeType(java.lang.String name)
          Get the mime type for the given file.
 int getMinorVersion()
           
 javax.servlet.RequestDispatcher getNamedDispatcher(java.lang.String name)
          Wicket does not use the RequestDispatcher, so this implementation just returns a dummy value.
 java.lang.String getRealPath(java.lang.String name)
          Get the real file path of the given resource name.
 javax.servlet.RequestDispatcher getRequestDispatcher(java.lang.String name)
          Wicket does not use the RequestDispatcher, so this implementation just returns a dummy value.
 java.net.URL getResource(java.lang.String name)
          Get the URL for a particular resource that is relative to the web app root directory.
 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.
 java.util.Set 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.String getServerInfo()
          Get the server info.
 javax.servlet.Servlet getServlet(java.lang.String name)
          NOT USED - Servlet Spec requires that this always returns null.
 java.lang.String getServletContextName()
          Return the name of the servlet context.
 java.util.Enumeration getServletNames()
          NOT USED - Servlet spec requires that this always returns null.
 java.util.Enumeration getServlets()
          NOT USED - Servlet spec requires that this always returns null.
 void log(java.lang.Exception e, java.lang.String msg)
          As part of testing we always log to the console.
 void log(java.lang.String msg)
          As part of testing we always log to the console.
 void log(java.lang.String msg, java.lang.Throwable cause)
          As part of testing we always log to the console.
 void removeAttribute(java.lang.String name)
          Remove an attribute with the given name.
 void setAttribute(java.lang.String name, java.lang.Object o)
          Set an attribute.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

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 for
path - 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 name
value - 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:
getAttribute in interface javax.servlet.ServletContext
Parameters:
name - The attribute name
Returns:
The value, or null

getAttributeNames

public java.util.Enumeration getAttributeNames()
Get all of the attribute names.

Specified by:
getAttributeNames in interface javax.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:
getContext in interface javax.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:
getInitParameter in interface javax.servlet.ServletContext
Parameters:
name - The name
Returns:
The parameter, or null if no such parameter

getInitParameterNames

public java.util.Enumeration getInitParameterNames()
Get the name of all of the init parameters.

Specified by:
getInitParameterNames in interface javax.servlet.ServletContext
Returns:
The init parameter names

getMajorVersion

public int getMajorVersion()
Specified by:
getMajorVersion in interface javax.servlet.ServletContext
Returns:
Always 2

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:
getMimeType in interface javax.servlet.ServletContext
Parameters:
name - The name to get the mime type for
Returns:
The mime type

getMinorVersion

public int getMinorVersion()
Specified by:
getMinorVersion in interface javax.servlet.ServletContext
Returns:
Always 3

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:
getNamedDispatcher in interface javax.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:
getRealPath in interface javax.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:
getRequestDispatcher in interface javax.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.MalformedURLException
Get the URL for a particular resource that is relative to the web app root directory.

Specified by:
getResource in interface javax.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:
getResourceAsStream in interface javax.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 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:
getResourcePaths in interface javax.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:
getServerInfo in interface javax.servlet.ServletContext
Returns:
The server info

getServlet

public javax.servlet.Servlet getServlet(java.lang.String name)
                                 throws javax.servlet.ServletException
NOT USED - Servlet Spec requires that this always returns null.

Specified by:
getServlet in interface javax.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:
getServletContextName in interface javax.servlet.ServletContext
Returns:
The name

getServletNames

public java.util.Enumeration getServletNames()
NOT USED - Servlet spec requires that this always returns null.

Specified by:
getServletNames in interface javax.servlet.ServletContext
Returns:
null

getServlets

public java.util.Enumeration getServlets()
NOT USED - Servlet spec requires that this always returns null.

Specified by:
getServlets in interface javax.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:
log in interface javax.servlet.ServletContext
Parameters:
e - The exception to log
msg - The message to log

log

public void log(java.lang.String msg)
As part of testing we always log to the console.

Specified by:
log in interface javax.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:
log in interface javax.servlet.ServletContext
Parameters:
msg - The message to log
cause - The cause exception

removeAttribute

public void removeAttribute(java.lang.String name)
Remove an attribute with the given name.

Specified by:
removeAttribute in interface javax.servlet.ServletContext
Parameters:
name - The name

setAttribute

public void setAttribute(java.lang.String name,
                         java.lang.Object o)
Set an attribute.

Specified by:
setAttribute in interface javax.servlet.ServletContext
Parameters:
name - The name of the attribute
o - The value

getContextPath

public java.lang.String getContextPath()
Specified by:
getContextPath in interface javax.servlet.ServletContext


Copyright © 2004-2010 Apache Software Foundation. All Rights Reserved.