org.jboss.jsfunit.context
Class JSFUnitExternalContext

java.lang.Object
  extended by javax.faces.context.ExternalContext
      extended by org.jboss.jsfunit.context.JSFUnitExternalContext

public class JSFUnitExternalContext
extends javax.faces.context.ExternalContext

The JSFUnitExternalContext is created at the end of the JSF lifecycle. It caches as much as possible from the "real" ExternalContext. Because the Servlet container is allowed to recycle request and response objects that the ExternalContext relies upon, a few methods could yield unexpected results. These methods are noted in the javadoc.

Since:
1.0
Author:
Stan Silvert

Field Summary
 
Fields inherited from class javax.faces.context.ExternalContext
BASIC_AUTH, CLIENT_CERT_AUTH, DIGEST_AUTH, FORM_AUTH
 
Constructor Summary
JSFUnitExternalContext(javax.faces.context.ExternalContext delegate)
           
 
Method Summary
 void addResponseCookie(String name, String value, Map<String,Object> properties)
          Warning: Calling this method from a JSFUnit test could yield unexpected results.
 void addResponseHeader(String name, String value)
           
 void dispatch(String string)
          Unsupported method.
 String encodeActionURL(String url)
          Return the url unchanged.
 String encodeBookmarkableURL(String baseUrl, Map<String,List<String>> parameters)
           
 String encodeNamespace(String string)
           
 String encodePartialActionURL(String url)
           
 String encodeRedirectURL(String baseUrl, Map<String,List<String>> parameters)
           
 String encodeResourceURL(String url)
          Return the url unchanged.
 Map getApplicationMap()
          Warning: The write-through capabilities of this Map are disabled for JSFUnit tests.
 String getAuthType()
           
 Object getContext()
           
 String getContextName()
           
 javax.faces.context.Flash getFlash()
           
 String getInitParameter(String string)
           
 Map getInitParameterMap()
           
 String getMimeType(String file)
           
 String getRealPath(String path)
           
 String getRemoteUser()
           
 Object getRequest()
          Warning: Calling this method from a JSFUnit test could yield unexpected results.
 String getRequestCharacterEncoding()
           
 int getRequestContentLength()
           
 String getRequestContentType()
           
 String getRequestContextPath()
           
 Map getRequestCookieMap()
           
 Map getRequestHeaderMap()
           
 Map getRequestHeaderValuesMap()
           
 Locale getRequestLocale()
           
 Iterator getRequestLocales()
           
 Map getRequestMap()
           
 Map getRequestParameterMap()
           
 Iterator getRequestParameterNames()
           
 Map getRequestParameterValuesMap()
           
 String getRequestPathInfo()
           
 String getRequestScheme()
           
 String getRequestServerName()
           
 int getRequestServerPort()
           
 String getRequestServletPath()
           
 URL getResource(String string)
           
 InputStream getResourceAsStream(String string)
           
 Set getResourcePaths(String string)
           
 Object getResponse()
          Warning: Calling this method from a JSFUnit test could yield unexpected results.
 int getResponseBufferSize()
           
 String getResponseCharacterEncoding()
          Warning: Calling this method from a JSFUnit test could yield unexpected results.
 String getResponseContentType()
          Warning: Calling this method from a JSFUnit test could yield unexpected results.
 OutputStream getResponseOutputStream()
          Warning: Calling this method from a JSFUnit test could yield unexpected results.
 Writer getResponseOutputWriter()
           
 Object getSession(boolean b)
           
 Map getSessionMap()
          Warning: The write-through capabilities of this Map are disabled for JSFUnit tests.
 Principal getUserPrincipal()
           
 void invalidateSession()
           
 boolean isResponseCommitted()
           
 boolean isUserInRole(String string)
          Warning: Calling this method from a JSFUnit test could yield unexpected results.
 void log(String string)
           
 void log(String string, Throwable throwable)
           
 void redirect(String string)
          Unsupported method.
 void responseFlushBuffer()
           
 void responseReset()
           
 void responseSendError(int statusCode, String message)
           
 void setRequest(Object request)
          Warning: Calling this method from a JSFUnit test could yield unexpected results.
 void setRequestCharacterEncoding(String encoding)
          Warning: Calling this method from a JSFUnit test could yield unexpected results.
 void setResponse(Object response)
          Warning: Calling this method from a JSFUnit test could yield unexpected results.
 void setResponseBufferSize(int size)
           
 void setResponseCharacterEncoding(String encoding)
          Warning: Calling this method from a JSFUnit test could yield unexpected results.
 void setResponseContentLength(int length)
           
 void setResponseContentType(String contentType)
          Warning: Calling this method from a JSFUnit test could yield unexpected results.
 void setResponseHeader(String name, String value)
           
 void setResponseStatus(int statusCode)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

JSFUnitExternalContext

public JSFUnitExternalContext(javax.faces.context.ExternalContext delegate)
Method Detail

getRequestCookieMap

public Map getRequestCookieMap()
Specified by:
getRequestCookieMap in class javax.faces.context.ExternalContext

getRequestContextPath

public String getRequestContextPath()
Specified by:
getRequestContextPath in class javax.faces.context.ExternalContext

getRemoteUser

public String getRemoteUser()
Specified by:
getRemoteUser in class javax.faces.context.ExternalContext

getInitParameter

public String getInitParameter(String string)
Specified by:
getInitParameter in class javax.faces.context.ExternalContext

getInitParameterMap

public Map getInitParameterMap()
Specified by:
getInitParameterMap in class javax.faces.context.ExternalContext

getContext

public Object getContext()
Specified by:
getContext in class javax.faces.context.ExternalContext

getAuthType

public String getAuthType()
Specified by:
getAuthType in class javax.faces.context.ExternalContext

getApplicationMap

public Map getApplicationMap()
Warning: The write-through capabilities of this Map are disabled for JSFUnit tests. You can still modify the Application attributes through the ServletContext. It is not recommended to do this in a JSFUnit test. Ideally, JSFUnit tests should only examine the state of the system, not change it.
But if you must, here is how to do it:
HttpSession session = (HttpSession)externalContext.getSession();
ServletContext appContext = session.getServletContext();
appContext.setAttribute("documentsByPath", "bar");

Specified by:
getApplicationMap in class javax.faces.context.ExternalContext

getRequestHeaderMap

public Map getRequestHeaderMap()
Specified by:
getRequestHeaderMap in class javax.faces.context.ExternalContext

getRequestHeaderValuesMap

public Map getRequestHeaderValuesMap()
Specified by:
getRequestHeaderValuesMap in class javax.faces.context.ExternalContext

getRequestLocale

public Locale getRequestLocale()
Specified by:
getRequestLocale in class javax.faces.context.ExternalContext

getRequestLocales

public Iterator getRequestLocales()
Specified by:
getRequestLocales in class javax.faces.context.ExternalContext

getRequestMap

public Map getRequestMap()
Specified by:
getRequestMap in class javax.faces.context.ExternalContext

getRequestParameterMap

public Map getRequestParameterMap()
Specified by:
getRequestParameterMap in class javax.faces.context.ExternalContext

getRequestParameterNames

public Iterator getRequestParameterNames()
Specified by:
getRequestParameterNames in class javax.faces.context.ExternalContext

getRequestParameterValuesMap

public Map getRequestParameterValuesMap()
Specified by:
getRequestParameterValuesMap in class javax.faces.context.ExternalContext

getRequestPathInfo

public String getRequestPathInfo()
Specified by:
getRequestPathInfo in class javax.faces.context.ExternalContext

getRequestServletPath

public String getRequestServletPath()
Specified by:
getRequestServletPath in class javax.faces.context.ExternalContext

getSession

public Object getSession(boolean b)
Specified by:
getSession in class javax.faces.context.ExternalContext

getSessionMap

public Map getSessionMap()
Warning: The write-through capabilities of this Map are disabled for JSFUnit tests. In other words, modifications to the Map do not actually affect the HttpSession. You can still modify the Session attributes using the 'live' HttpSession obtained from the getSession() method. It is not recommended to do this in a JSFUnit test. Ideally, JSFUnit tests should only examine the state of the system, not change it.
But if you must, here is how to do it:
HttpSession session = (HttpSession)externalContext.getSession();
session.setAttribute("documentsByPath", "bar");

Specified by:
getSessionMap in class javax.faces.context.ExternalContext

getUserPrincipal

public Principal getUserPrincipal()
Specified by:
getUserPrincipal in class javax.faces.context.ExternalContext

getResource

public URL getResource(String string)
                throws MalformedURLException
Specified by:
getResource in class javax.faces.context.ExternalContext
Throws:
MalformedURLException

getResourceAsStream

public InputStream getResourceAsStream(String string)
Specified by:
getResourceAsStream in class javax.faces.context.ExternalContext

getResourcePaths

public Set getResourcePaths(String string)
Specified by:
getResourcePaths in class javax.faces.context.ExternalContext

log

public void log(String string,
                Throwable throwable)
Specified by:
log in class javax.faces.context.ExternalContext

log

public void log(String string)
Specified by:
log in class javax.faces.context.ExternalContext

encodeNamespace

public String encodeNamespace(String string)
Specified by:
encodeNamespace in class javax.faces.context.ExternalContext

encodeResourceURL

public String encodeResourceURL(String url)
Return the url unchanged. This is OK in JSFUnit because we know that we are using cookies for the jsessionid.

Specified by:
encodeResourceURL in class javax.faces.context.ExternalContext
Parameters:
url - The url to encode.
Returns:
The url unchanged.
Throws:
NullPointerException - if the url is null

encodeActionURL

public String encodeActionURL(String url)
Return the url unchanged. This is OK in JSFUnit because we know that we are using cookies for the jsessionid.

Specified by:
encodeActionURL in class javax.faces.context.ExternalContext
Parameters:
url - The url to encode.
Returns:
The url unchanged.
Throws:
NullPointerException - if the url is null

addResponseCookie

public void addResponseCookie(String name,
                              String value,
                              Map<String,Object> properties)
Warning: Calling this method from a JSFUnit test could yield unexpected results.

Overrides:
addResponseCookie in class javax.faces.context.ExternalContext

getMimeType

public String getMimeType(String file)
Overrides:
getMimeType in class javax.faces.context.ExternalContext

getRealPath

public String getRealPath(String path)
Overrides:
getRealPath in class javax.faces.context.ExternalContext

getRequestCharacterEncoding

public String getRequestCharacterEncoding()
Overrides:
getRequestCharacterEncoding in class javax.faces.context.ExternalContext

getRequestContentType

public String getRequestContentType()
Overrides:
getRequestContentType in class javax.faces.context.ExternalContext

getRequestScheme

public String getRequestScheme()
Overrides:
getRequestScheme in class javax.faces.context.ExternalContext

getRequestServerName

public String getRequestServerName()
Overrides:
getRequestServerName in class javax.faces.context.ExternalContext

getRequestServerPort

public int getRequestServerPort()
Overrides:
getRequestServerPort in class javax.faces.context.ExternalContext

getResponseCharacterEncoding

public String getResponseCharacterEncoding()
Warning: Calling this method from a JSFUnit test could yield unexpected results.

Overrides:
getResponseCharacterEncoding in class javax.faces.context.ExternalContext

getResponseContentType

public String getResponseContentType()
Warning: Calling this method from a JSFUnit test could yield unexpected results.

Overrides:
getResponseContentType in class javax.faces.context.ExternalContext

getResponseOutputStream

public OutputStream getResponseOutputStream()
                                     throws IOException
Warning: Calling this method from a JSFUnit test could yield unexpected results.

Overrides:
getResponseOutputStream in class javax.faces.context.ExternalContext
Throws:
IOException

invalidateSession

public void invalidateSession()
Overrides:
invalidateSession in class javax.faces.context.ExternalContext

setRequest

public void setRequest(Object request)
Warning: Calling this method from a JSFUnit test could yield unexpected results.

Overrides:
setRequest in class javax.faces.context.ExternalContext

setRequestCharacterEncoding

public void setRequestCharacterEncoding(String encoding)
                                 throws UnsupportedEncodingException
Warning: Calling this method from a JSFUnit test could yield unexpected results.

Overrides:
setRequestCharacterEncoding in class javax.faces.context.ExternalContext
Throws:
UnsupportedEncodingException

setResponse

public void setResponse(Object response)
Warning: Calling this method from a JSFUnit test could yield unexpected results.

Overrides:
setResponse in class javax.faces.context.ExternalContext

setResponseCharacterEncoding

public void setResponseCharacterEncoding(String encoding)
Warning: Calling this method from a JSFUnit test could yield unexpected results.

Overrides:
setResponseCharacterEncoding in class javax.faces.context.ExternalContext

setResponseContentType

public void setResponseContentType(String contentType)
Warning: Calling this method from a JSFUnit test could yield unexpected results.

Overrides:
setResponseContentType in class javax.faces.context.ExternalContext

getFlash

public javax.faces.context.Flash getFlash()
Overrides:
getFlash in class javax.faces.context.ExternalContext

addResponseHeader

public void addResponseHeader(String name,
                              String value)
Overrides:
addResponseHeader in class javax.faces.context.ExternalContext

encodeBookmarkableURL

public String encodeBookmarkableURL(String baseUrl,
                                    Map<String,List<String>> parameters)
Overrides:
encodeBookmarkableURL in class javax.faces.context.ExternalContext

encodePartialActionURL

public String encodePartialActionURL(String url)
Overrides:
encodePartialActionURL in class javax.faces.context.ExternalContext

encodeRedirectURL

public String encodeRedirectURL(String baseUrl,
                                Map<String,List<String>> parameters)
Overrides:
encodeRedirectURL in class javax.faces.context.ExternalContext

getContextName

public String getContextName()
Overrides:
getContextName in class javax.faces.context.ExternalContext

getRequestContentLength

public int getRequestContentLength()
Overrides:
getRequestContentLength in class javax.faces.context.ExternalContext

getResponseBufferSize

public int getResponseBufferSize()
Overrides:
getResponseBufferSize in class javax.faces.context.ExternalContext

getResponseOutputWriter

public Writer getResponseOutputWriter()
                               throws IOException
Overrides:
getResponseOutputWriter in class javax.faces.context.ExternalContext
Throws:
IOException

isResponseCommitted

public boolean isResponseCommitted()
Overrides:
isResponseCommitted in class javax.faces.context.ExternalContext

responseFlushBuffer

public void responseFlushBuffer()
                         throws IOException
Overrides:
responseFlushBuffer in class javax.faces.context.ExternalContext
Throws:
IOException

responseReset

public void responseReset()
Overrides:
responseReset in class javax.faces.context.ExternalContext

responseSendError

public void responseSendError(int statusCode,
                              String message)
                       throws IOException
Overrides:
responseSendError in class javax.faces.context.ExternalContext
Throws:
IOException

setResponseBufferSize

public void setResponseBufferSize(int size)
Overrides:
setResponseBufferSize in class javax.faces.context.ExternalContext

setResponseContentLength

public void setResponseContentLength(int length)
Overrides:
setResponseContentLength in class javax.faces.context.ExternalContext

setResponseHeader

public void setResponseHeader(String name,
                              String value)
Overrides:
setResponseHeader in class javax.faces.context.ExternalContext

setResponseStatus

public void setResponseStatus(int statusCode)
Overrides:
setResponseStatus in class javax.faces.context.ExternalContext

isUserInRole

public boolean isUserInRole(String string)
Warning: Calling this method from a JSFUnit test could yield unexpected results.

Specified by:
isUserInRole in class javax.faces.context.ExternalContext

getResponse

public Object getResponse()
Warning: Calling this method from a JSFUnit test could yield unexpected results.

Specified by:
getResponse in class javax.faces.context.ExternalContext

getRequest

public Object getRequest()
Warning: Calling this method from a JSFUnit test could yield unexpected results.

Specified by:
getRequest in class javax.faces.context.ExternalContext

dispatch

public void dispatch(String string)
              throws IOException
Unsupported method. Since the JSFUnitExternalContext is not active until the request is over, it doesn't make sense to do a dispatch by calling this method in a JSFUnit test.

Specified by:
dispatch in class javax.faces.context.ExternalContext
Throws:
UnsupportedOperationException - if this method is called during a JSFUnit test.
IOException

redirect

public void redirect(String string)
              throws IOException
Unsupported method. Since the JSFUnitExternalContext is not active until the request is over, it doesn't make sense to do a dispatch by calling this method in a JSFUnit test.

Specified by:
redirect in class javax.faces.context.ExternalContext
Throws:
UnsupportedOperationException - if this method is called during a JSFUnit test.
IOException


Copyright © 2007-2010 JBoss, a division of Red Hat, Inc.. All Rights Reserved.