Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * JBoss, Home of Professional Open Source.
   * Copyright 2009, Red Hat Middleware LLC, and individual contributors
   * as indicated by the @author tags. See the copyright.txt file in the
   * distribution for a full listing of individual contributors.
   *
   * This is free software; you can redistribute it and/or modify it
   * under the terms of the GNU Lesser General Public License as
   * published by the Free Software Foundation; either version 2.1 of
  * the License, or (at your option) any later version.
  *
  * This software is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this software; if not, write to the Free
  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  */
 
 package org.jboss.jsfunit.jsfsession;
 
 import  javax.faces.application.FacesMessage;
 import  javax.faces.component.UIComponent;
 import  javax.faces.context.ExternalContext;
 import  javax.faces.context.FacesContext;
The JSFServerSession provides a simplified API that wraps parts of the JSF API for things that you would commonly do in testing.

Author(s):
Stan Silvert
Since:
1.0
 
 public class JSFServerSession implements RequestListener
 {
    private ClientIDs clientIDs;
    private FacesContext currentFacesContext;
   
   
Create a new JSFServerSession.

Parameters:
client The JSFClientSession for the current web conversation.
 
    {
       pageCreated();
    }
   
   
Get the immutable ClientIDs object. This is typically used only by JSFUnit.

Returns:
The ClientIDs object
See also:
org.jboss.jsfunit.jsfsession.ClientIDs
 
    public ClientIDs getClientIDs()
    {
       return this.;
    }
   
   
Return the current view ID from the component tree.

Returns:
The current View ID.
 
    public String getCurrentViewID()
    {
       return getFacesContext().getViewRoot().getViewId();
    }
   
   
Get the FacesContext object used in the last request.

Returns:
The FacesContext object used in the last request.
 
    public FacesContext getFacesContext()
    {
       return FacesContextBridge.getCurrentInstance();
    }
   
   
Find a component in the JSF component tree.

Parameters:
componentID The JSF component ID or client ID suffix.
Returns:
The component.
Throws:
ComponentIDNotFoundException if the component can not be found
DuplicateClientIDException if more than one client ID matches the componentID suffix
   public UIComponent findComponent(String componentID)
   {
      return .findComponent(componentID);
   }
   
   
Find a component in the JSF component tree and return its value. Note that the found component must implement ValueHolder.

Parameters:
componentID The JSF component ID or client ID suffix.
Returns:
The value contained in the component.
Throws:
ComponentIDNotFoundException if the component can not be found
DuplicateClientIDException if more than one client ID matches the componentID suffix
ClassCastException if the found component does not implement ValueHolder
   public Object getComponentValue(String componentID)
   {
      return .getComponentValue(componentID);
   }
   
   
Evaluate an EL ValueExpression and return the value.

Parameters:
elExpression The expression.
Returns:
The value. Return null if the managed bean is request, session, or application scope and does not yet exist.
   public Object getManagedBeanValue(String elExpression)
   {
      FacesContext facesContext = getFacesContext();
      
      try
      {
         setELRunning(truefacesContext.getExternalContext());
         return facesContext.getApplication()
                            .createValueBinding(elExpression)
                            .getValue(facesContext);
      }
      catch (RuntimeException re)
      {  // JSFUNIT-164
         Throwable chain = re;
         do
         {
            if (chain instanceof NoNewEntryMap.NewEntryNotAllowedException)
            {
               return null;
            }
            chain = chain.getCause();
         }
         while (chain != null);
         
         throw re;
      }
      finally
      {
         setELRunning(falsefacesContext.getExternalContext());
      }
   }
   
   // This tells the Map to throw an exception if an EL expression tries to
   // create a new managed bean in request, session, or applicaiton scope.
   // In a JSF request, we want the bean to be created by EL.  But in a test we 
   // want to know that the previous request DID NOT create it.
   private void setELRunning(boolean isELRunning, ExternalContext extContext)
   {
      NoNewEntryMap requestMap = (NoNewEntryMap)extContext.getRequestMap();
      requestMap.setELRunning(isELRunning);
      
      NoNewEntryMap sessionMap = (NoNewEntryMap)extContext.getSessionMap();
      sessionMap.setELRunning(isELRunning);
      
      NoNewEntryMap applicationMap = (NoNewEntryMap)extContext.getApplicationMap();
      applicationMap.setELRunning(isELRunning);
   }
   
   
Return all the FacesMessages generated with the last JSF request.

Parameters:
componentID The JSF component ID or client ID suffix.
Returns:
The FacesMessages
   public Iterator<FacesMessage> getFacesMessages()
   {
      return getFacesContext().getMessages();
   }
   
   
Return all the FacesMessages generated for a component in the last JSF request.

Parameters:
componentID The JSF component ID or client ID suffix.
Returns:
the FacesMessages.
Throws:
ComponentIDNotFoundException if the component can not be found
DuplicateClientIDException if more than one client ID matches the componentID suffix
   public Iterator<FacesMessage> getFacesMessages(String componentID)
   {
      String clientID = this..findClientID(componentID);
      return getFacesContext().getMessages(clientID);
   }
   private void pageCreated()
   {
      // Note that the FacesContextBridge not only provides us with the FacesContext, 
      // it also associates the FacesContext with the JSFUnit thread so that 
      // FacesContext.getCurrentInstance() will work.
      JSFUnitFacesContext facesContext = (JSFUnitFacesContext)FacesContextBridge.getCurrentInstance();
      
      // if no FacesContext exists, we can're get the Client IDs
      if (facesContext == nullreturn;
      
      // Peformance optimization.  If the FacesContext instance didn're change,
      // there is no need to re-create the ClientIDs.
      if (this. != facesContext)
      {
         this. = new ClientIDs();  
         this. = facesContext;
      }
   }
   //----------- Implementation of RequestListener
   public void beforeRequest(WebRequestSettings webRequestSettings)
   {
   }
   public void afterRequest(WebResponse webResponse)
   {
      pageCreated();
   }
   
New to GrepCode? Check out our FAQ X