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 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(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)
          Warning: Calling this method from a JSFUnit test could yield unexpected results.
 void dispatch(String string)
          Unsupported method.
 String encodeActionURL(String url)
          Return the url unchanged.
 String encodeNamespace(String string)
           
 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()
           
 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.
 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.
 Object getSession(boolean b)
           
 Map getSessionMap()
          Warning: The write-through capabilities of this Map are disabled for JSFUnit tests.
 Principal getUserPrincipal()
           
 void invalidateSession()
           
 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 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 setResponseCharacterEncoding(String encoding)
          Warning: Calling this method from a JSFUnit test could yield unexpected results.
 void setResponseContentType(String contentType)
          Warning: Calling this method from a JSFUnit test could yield unexpected results.
 void setResponseHeader(String name, String value)
          Warning: Calling this method from a JSFUnit test could yield unexpected results.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

JSFUnitExternalContext

public JSFUnitExternalContext(ExternalContext delegate)
Method Detail

getRequestCookieMap

public Map getRequestCookieMap()
Specified by:
getRequestCookieMap in class ExternalContext

getRequestContextPath

public String getRequestContextPath()
Specified by:
getRequestContextPath in class ExternalContext

getRemoteUser

public String getRemoteUser()
Specified by:
getRemoteUser in class ExternalContext

getInitParameter

public String getInitParameter(String string)
Specified by:
getInitParameter in class ExternalContext

getInitParameterMap

public Map getInitParameterMap()
Specified by:
getInitParameterMap in class ExternalContext

getContext

public Object getContext()
Specified by:
getContext in class ExternalContext

getAuthType

public String getAuthType()
Specified by:
getAuthType in class 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 ExternalContext

getRequestHeaderMap

public Map getRequestHeaderMap()
Specified by:
getRequestHeaderMap in class ExternalContext

getRequestHeaderValuesMap

public Map getRequestHeaderValuesMap()
Specified by:
getRequestHeaderValuesMap in class ExternalContext

getRequestLocale

public Locale getRequestLocale()
Specified by:
getRequestLocale in class ExternalContext

getRequestLocales

public Iterator getRequestLocales()
Specified by:
getRequestLocales in class ExternalContext

getRequestMap

public Map getRequestMap()
Specified by:
getRequestMap in class ExternalContext

getRequestParameterMap

public Map getRequestParameterMap()
Specified by:
getRequestParameterMap in class ExternalContext

getRequestParameterNames

public Iterator getRequestParameterNames()
Specified by:
getRequestParameterNames in class ExternalContext

getRequestParameterValuesMap

public Map getRequestParameterValuesMap()
Specified by:
getRequestParameterValuesMap in class ExternalContext

getRequestPathInfo

public String getRequestPathInfo()
Specified by:
getRequestPathInfo in class ExternalContext

getRequestServletPath

public String getRequestServletPath()
Specified by:
getRequestServletPath in class ExternalContext

getSession

public Object getSession(boolean b)
Specified by:
getSession in class 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 ExternalContext

getUserPrincipal

public Principal getUserPrincipal()
Specified by:
getUserPrincipal in class ExternalContext

getResource

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

getResourceAsStream

public InputStream getResourceAsStream(String string)
Specified by:
getResourceAsStream in class ExternalContext

getResourcePaths

public Set getResourcePaths(String string)
Specified by:
getResourcePaths in class ExternalContext

log

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

log

public void log(String string)
Specified by:
log in class ExternalContext

encodeNamespace

public String encodeNamespace(String string)
Specified by:
encodeNamespace in class 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 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 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.


getMimeType

public String getMimeType(String file)

getRealPath

public String getRealPath(String path)

getRequestCharacterEncoding

public String getRequestCharacterEncoding()

getRequestContentType

public String getRequestContentType()

getRequestScheme

public String getRequestScheme()

getRequestServerName

public String getRequestServerName()

getRequestServerPort

public int getRequestServerPort()

getResponseCharacterEncoding

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


getResponseContentType

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


getResponseOutputStream

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

Throws:
IOException

invalidateSession

public void invalidateSession()

setRequest

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


setRequestCharacterEncoding

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

Throws:
UnsupportedEncodingException

setResponse

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


setResponseCharacterEncoding

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


setResponseContentType

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


getContextName

public String getContextName()

getRequestContentLength

public int getRequestContentLength()

addResponseHeader

public void addResponseHeader(String name,
                              String value)
Warning: Calling this method from a JSFUnit test could yield unexpected results.


setResponseHeader

public void setResponseHeader(String name,
                              String value)
Warning: Calling this method from a JSFUnit test could yield unexpected results.


isUserInRole

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

Specified by:
isUserInRole in class ExternalContext

getResponse

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

Specified by:
getResponse in class ExternalContext

getRequest

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

Specified by:
getRequest in class 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 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 ExternalContext
Throws:
UnsupportedOperationException - if this method is called during a JSFUnit test.
IOException


Copyright © 2007-2009 JBoss Inc.. All Rights Reserved.