[jboss-cvs] jboss-seam/src/main/org/jboss/seam/mock ...
Gavin King
gavin.king at jboss.com
Fri Jun 8 15:26:43 EDT 2007
User: gavin
Date: 07/06/08 15:26:43
Modified: src/main/org/jboss/seam/mock BaseSeamTest.java
Log:
major cleanup of SeamTest
Revision Changes Path
1.5 +205 -172 jboss-seam/src/main/org/jboss/seam/mock/BaseSeamTest.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: BaseSeamTest.java
===================================================================
RCS file: /cvsroot/jboss/jboss-seam/src/main/org/jboss/seam/mock/BaseSeamTest.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -b -r1.4 -r1.5
--- BaseSeamTest.java 4 Jun 2007 17:57:53 -0000 1.4
+++ BaseSeamTest.java 8 Jun 2007 19:26:43 -0000 1.5
@@ -49,53 +49,25 @@
*
* @author Gavin King
* @author <a href="mailto:theute at jboss.org">Thomas Heute</a>
- * @version $Revision: 1.4 $
+ * @version $Revision: 1.5 $
*/
public class BaseSeamTest
{
- private MockExternalContext externalContext;
-
+ private Application application;
private MockServletContext servletContext;
-
- private MockApplication application;
-
private AbstractSeamPhaseListener phases;
-
private MockHttpSession session;
-
private Map<String, Map> conversationViewRootAttributes;
- private Map<String, Object> pageParameters = new HashMap<String, Object>();
-
- protected void setParameter(String name, String value)
- {
- getParameters().put(name, new String[] { value });
- }
-
- protected void setPageParameter(String name, Object value)
- {
- pageParameters.put(name, value);
- }
-
- protected Map<String, String[]> getParameters()
- {
- return ((MockHttpServletRequest) externalContext.getRequest()).getParameters();
- }
-
- protected Map<String, String[]> getHeaders()
+ protected boolean isSessionInvalid()
{
- return ((MockHttpServletRequest) externalContext.getRequest()).getHeaders();
+ return session.isInvalid();
}
protected HttpSession getSession()
{
- return (HttpSession) externalContext.getSession(true);
- }
-
- protected boolean isSessionInvalid()
- {
- return ((MockHttpSession) getSession()).isInvalid();
+ return session;
}
/**
@@ -155,24 +127,39 @@
abstract class Request
{
private String conversationId;
-
private String outcome;
-
private boolean validationFailed;
-
- private MockFacesContext facesContext;
-
private String viewId;
private boolean renderResponseBegun;
-
private boolean renderResponseComplete;
-
private boolean invokeApplicationBegun;
-
private boolean invokeApplicationComplete;
- private Application application;
+ private HttpServletRequest request;
+ private MockFacesContext facesContext;
+ private MockExternalContext externalContext;
+ private Map<String, Object> pageParameters = new HashMap<String, Object>();
+
+ protected void setPageParameter(String name, Object value)
+ {
+ pageParameters.put(name, value);
+ }
+
+ protected void setParameter(String name, String value)
+ {
+ getParameters().put(name, new String[] { value });
+ }
+
+ protected Map<String, String[]> getParameters()
+ {
+ return ( (MockHttpServletRequest) externalContext.getRequest() ).getParameters();
+ }
+
+ protected Map<String, String[]> getHeaders()
+ {
+ return ( (MockHttpServletRequest) externalContext.getRequest() ).getHeaders();
+ }
/**
* Override to define the name of the current principal
@@ -448,15 +435,51 @@
*/
public String run() throws Exception
{
- HttpServletRequest request = new MockHttpServletRequest(session, getPrincipalName(),
- getPrincipalRoles(), getCookies().toArray(new Cookie[] {}));
+ try
+ {
+ init();
+ beforeRequest();
+ setStandardJspVariables();
+ if ( emulateJsfLifecycle() )
+ {
+ saveConversationViewRoot();
+ }
+ afterRequest();
+ return conversationId;
+ }
+ finally
+ {
+ if (Contexts.isEventContextActive())
+ {
+ Lifecycle.endRequest(externalContext);
+ }
+ }
+
+ }
+
+ private void saveConversationViewRoot()
+ {
+ Map renderedViewRootAttributes = facesContext.getViewRoot().getAttributes();
+ if (renderedViewRootAttributes!=null && conversationId!=null )
+ {
+ Map conversationState = new HashMap();
+ conversationState.putAll(renderedViewRootAttributes);
+ conversationViewRootAttributes.put(conversationId, conversationState);
+ }
+ }
+
+ private void init()
+ {
+ Cookie[] cookieArray = getCookies().toArray( new Cookie[]{} );
+ request = new MockHttpServletRequest(session, getPrincipalName(), getPrincipalRoles(), cookieArray);
externalContext = new MockExternalContext(servletContext, request);
- application = new SeamApplication(BaseSeamTest.this.application);
facesContext = new MockFacesContext(externalContext, application);
facesContext.setCurrent();
+ }
- beforeRequest();
-
+ private void setStandardJspVariables()
+ {
+ //TODO: looks like we should also set request, session, application, page...
Map<String, String> params = new HashMap<String, String>();
for (Map.Entry<String, String[]> e : ((Map<String, String[]>) request.getParameterMap())
.entrySet())
@@ -467,93 +490,64 @@
}
}
request.setAttribute("param", params);
+ }
- phases.beforePhase(new PhaseEvent(facesContext, PhaseId.RESTORE_VIEW,
- MockLifecycle.INSTANCE));
-
- UIViewRoot viewRoot = facesContext.getApplication().getViewHandler().createView(
- facesContext, getViewId());
- facesContext.setViewRoot(viewRoot);
- Map restoredViewRootAttributes = facesContext.getViewRoot().getAttributes();
- if (conversationId != null)
+ /**
+ * @return true if a response was rendered
+ */
+ private boolean emulateJsfLifecycle() throws Exception
{
- if (isGetRequest())
+ restoreViewPhase();
+ if ( !isGetRequest() && !skipToRender() )
{
- setParameter(Manager.instance().getConversationIdParameter(), conversationId);
- // TODO: what about conversationIsLongRunning????
- }
- else
+ applyRequestValuesPhase();
+ if (!skipToRender())
{
- if (conversationViewRootAttributes.containsKey(conversationId))
+ processValidationsPhase();
+ if ( !skipToRender() )
{
- // should really only do this if the view id matches (not
- // really possible to implement)
- Map state = conversationViewRootAttributes.get(conversationId);
- restoredViewRootAttributes.putAll(state);
+ updateModelValuesPhase();
+ if ( !skipToRender() )
+ {
+ invokeApplicationPhase();
}
}
}
- if (isGetRequest())
+ }
+
+ if ( skipRender() )
{
- facesContext.renderResponse();
+ // we really should look at redirect parameters here!
+ return false;
}
else
{
- restoredViewRootAttributes.putAll(pageParameters);
+ renderResponsePhase();
+ return true;
+ }
}
- updateConversationId();
-
- phases.afterPhase(new PhaseEvent(facesContext, PhaseId.RESTORE_VIEW,
- MockLifecycle.INSTANCE));
-
- if (!isGetRequest() && !skipToRender())
- {
-
- phases.beforePhase(new PhaseEvent(facesContext, PhaseId.APPLY_REQUEST_VALUES,
- MockLifecycle.INSTANCE));
-
- applyRequestValues();
-
- updateConversationId();
-
- phases.afterPhase(new PhaseEvent(facesContext, PhaseId.APPLY_REQUEST_VALUES,
- MockLifecycle.INSTANCE));
-
- if (!skipToRender())
+ private void renderResponsePhase() throws Exception
{
-
- phases.beforePhase(new PhaseEvent(facesContext, PhaseId.PROCESS_VALIDATIONS,
+ phases.beforePhase(new PhaseEvent(facesContext, PhaseId.RENDER_RESPONSE,
MockLifecycle.INSTANCE));
- processValidations();
-
- updateConversationId();
-
- if ( isValidationFailure() )
- {
- facesContext.renderResponse();
- }
-
- phases.afterPhase(new PhaseEvent(facesContext, PhaseId.PROCESS_VALIDATIONS,
- MockLifecycle.INSTANCE));
+ renderResponseBegun = true;
- if (!skipToRender())
- {
+ renderResponse();
- phases.beforePhase(new PhaseEvent(facesContext, PhaseId.UPDATE_MODEL_VALUES,
- MockLifecycle.INSTANCE));
+ renderResponseComplete = true;
- updateModelValues();
+ facesContext.getApplication().getStateManager().saveView(facesContext);
updateConversationId();
- phases.afterPhase(new PhaseEvent(facesContext, PhaseId.UPDATE_MODEL_VALUES,
+ phases.afterPhase(new PhaseEvent(facesContext, PhaseId.RENDER_RESPONSE,
MockLifecycle.INSTANCE));
+ }
- if (!skipToRender())
+ private void invokeApplicationPhase() throws Exception
{
-
phases.beforePhase(new PhaseEvent(facesContext, PhaseId.INVOKE_APPLICATION,
MockLifecycle.INSTANCE));
@@ -573,56 +567,100 @@
phases.afterPhase(new PhaseEvent(facesContext, PhaseId.INVOKE_APPLICATION,
MockLifecycle.INSTANCE));
-
}
- }
+ private void updateModelValuesPhase() throws Exception
+ {
+ phases.beforePhase(new PhaseEvent(facesContext, PhaseId.UPDATE_MODEL_VALUES,
+ MockLifecycle.INSTANCE));
- }
+ updateModelValues();
- }
+ updateConversationId();
- if (skipRender())
- {
- // we really should look at redirect parameters here!
+ phases.afterPhase(new PhaseEvent(facesContext, PhaseId.UPDATE_MODEL_VALUES,
+ MockLifecycle.INSTANCE));
}
- else
- {
- phases.beforePhase(new PhaseEvent(facesContext, PhaseId.RENDER_RESPONSE,
+ private void processValidationsPhase() throws Exception
+ {
+ phases.beforePhase(new PhaseEvent(facesContext, PhaseId.PROCESS_VALIDATIONS,
MockLifecycle.INSTANCE));
- renderResponseBegun = true;
+ processValidations();
- renderResponse();
+ updateConversationId();
- renderResponseComplete = true;
+ if ( isValidationFailure() )
+ {
+ facesContext.renderResponse();
+ }
- facesContext.getApplication().getStateManager().saveView(facesContext);
+ phases.afterPhase(new PhaseEvent(facesContext, PhaseId.PROCESS_VALIDATIONS,
+ MockLifecycle.INSTANCE));
+ }
+
+ private void applyRequestValuesPhase() throws Exception
+ {
+ phases.beforePhase(new PhaseEvent(facesContext, PhaseId.APPLY_REQUEST_VALUES,
+ MockLifecycle.INSTANCE));
+
+ applyRequestValues();
updateConversationId();
- phases.afterPhase(new PhaseEvent(facesContext, PhaseId.RENDER_RESPONSE,
+ phases.afterPhase(new PhaseEvent(facesContext, PhaseId.APPLY_REQUEST_VALUES,
MockLifecycle.INSTANCE));
+ }
- Map renderedViewRootAttributes = facesContext.getViewRoot().getAttributes();
- if (renderedViewRootAttributes != null)
+ private void restoreViewPhase()
{
- Map conversationState = new HashMap();
- conversationState.putAll(renderedViewRootAttributes);
- conversationViewRootAttributes.put(conversationId, conversationState);
- }
+ phases.beforePhase(new PhaseEvent(facesContext, PhaseId.RESTORE_VIEW,
+ MockLifecycle.INSTANCE));
+ UIViewRoot viewRoot = facesContext.getApplication().getViewHandler().createView(
+ facesContext, getViewId());
+ facesContext.setViewRoot(viewRoot);
+ Map restoredViewRootAttributes = facesContext.getViewRoot().getAttributes();
+ if (conversationId != null)
+ {
+ if ( isGetRequest() )
+ {
+ setParameter(Manager.instance().getConversationIdParameter(), conversationId);
+ // TODO: what about conversationIsLongRunning????
+ }
+ else
+ {
+ if (conversationViewRootAttributes.containsKey(conversationId))
+ {
+ // should really only do this if the view id matches (not
+ // really possible to implement)
+ Map state = conversationViewRootAttributes.get(conversationId);
+ restoredViewRootAttributes.putAll(state);
+ }
+ }
+ }
+ if ( isGetRequest() )
+ {
+ facesContext.renderResponse();
+ }
+ else
+ {
+ restoredViewRootAttributes.putAll(pageParameters);
}
- afterRequest();
+ updateConversationId();
- return conversationId;
+ phases.afterPhase(new PhaseEvent(facesContext, PhaseId.RESTORE_VIEW,
+ MockLifecycle.INSTANCE));
}
private void updateConversationId()
{
- conversationId = Manager.instance().getCurrentConversationId();
+ Manager manager = Manager.instance();
+ conversationId = manager.isLongRunningConversation() ?
+ manager.getCurrentConversationId() :
+ manager.getParentConversationId() ;
}
private boolean skipRender()
@@ -754,10 +792,6 @@
public void end()
{
- if (Contexts.isEventContextActive())
- {
- Lifecycle.endRequest(externalContext);
- }
Lifecycle.endSession(servletContext, new ServletSessionImpl(session));
session = null;
}
@@ -773,7 +807,7 @@
public void init() throws Exception
{
- application = new MockApplication();
+ application = new SeamApplication( new MockApplication() );
phases = createPhaseListener();
servletContext = new MockServletContext();
@@ -787,7 +821,6 @@
public void cleanup() throws Exception
{
Lifecycle.endApplication(servletContext);
- externalContext = null;
conversationViewRootAttributes = null;
}
More information about the jboss-cvs-commits
mailing list