[jboss-cvs] jboss-seam/src/main/org/jboss/seam/mock ...
Peter Muir
peter at bleepbleep.org.uk
Tue Jan 8 14:05:48 EST 2008
User: pmuir
Date: 08/01/08 14:05:48
Modified: src/main/org/jboss/seam/mock SeamTest.java
BaseSeamTest.java
Log:
JBSEAM-2424
Revision Changes Path
1.77 +43 -10 jboss-seam/src/main/org/jboss/seam/mock/SeamTest.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: SeamTest.java
===================================================================
RCS file: /cvsroot/jboss/jboss-seam/src/main/org/jboss/seam/mock/SeamTest.java,v
retrieving revision 1.76
retrieving revision 1.77
diff -u -b -r1.76 -r1.77
--- SeamTest.java 15 Jun 2007 23:36:54 -0000 1.76
+++ SeamTest.java 8 Jan 2008 19:05:48 -0000 1.77
@@ -8,8 +8,10 @@
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
+import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.BeforeTest;
/**
* Provides BaseSeamTest functionality for TestNG integration tests.
@@ -17,6 +19,7 @@
* @author Gavin King
* @author <a href="mailto:theute at jboss.org">Thomas Heute</a>
* @author Mike Youngstrom
+ * @author Pete Muir
*/
public class SeamTest extends BaseSeamTest
{
@@ -35,23 +38,52 @@
super.end();
}
- @BeforeClass
@Override
+ @Deprecated
public void init() throws Exception
{
super.init();
}
- @AfterClass
@Override
+ @Deprecated
public void cleanup() throws Exception
{
super.cleanup();
}
+ @Override
+ @BeforeTest
+ public void startSeam() throws Exception
+ {
+ super.startSeam();
+ }
+
+ @Override
+ @AfterTest
+ public void stopSeam() throws Exception
+ {
+ super.stopSeam();
+ }
+
+ @Override
+ @BeforeClass
+ public void setupClass() throws Exception
+ {
+ super.setupClass();
+ }
+
+ @Override
+ @AfterClass
+ protected void cleanupClass() throws Exception
+ {
+ super.cleanupClass();
+ }
+
/**
- * A pass through to BaseSeamTest.FacesRequest. Perhaps these should be
- * deprecated?
+ * A pass through to BaseSeamTest.FacesRequest.
+ *
+ * Deprecated, use BaseSeamTest.FacesRequest instead
*/
public class FacesRequest extends BaseSeamTest.FacesRequest
{
@@ -74,8 +106,9 @@
}
/**
- * A pass through to BaseSeamTest.NonFacesRequest. Perhaps these should be
- * deprecated?
+ * A pass through to BaseSeamTest.NonFacesRequest.
+ *
+ * Deprecated, use BaseSeamTest.NonFacesRequest instead
*/
public class NonFacesRequest extends BaseSeamTest.NonFacesRequest
{
@@ -98,8 +131,9 @@
}
/**
- * A pass through to BaseSeamTest.Request. Perhaps these should be
- * deprecated?
+ * A pass through to BaseSeamTest.Request.
+ *
+ * Deprecated, use BaseSeamTest.Request instead
*/
public abstract class Request extends BaseSeamTest.Request
{
@@ -117,9 +151,8 @@
}
/**
- * @deprecated Use FacesRequest or NonFacesRequest instead
+ * @deprecated Use BaseSeamTest.FacesRequest or BaseSeamTest.NonFacesRequest instead
*/
- @Deprecated
public abstract class Script extends BaseSeamTest.Script
{
1.38 +203 -165 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.37
retrieving revision 1.38
diff -u -b -r1.37 -r1.38
--- BaseSeamTest.java 8 Nov 2007 02:44:00 -0000 1.37
+++ BaseSeamTest.java 8 Jan 2008 19:05:48 -0000 1.38
@@ -123,7 +123,9 @@
*/
public abstract class Script extends Request
{
- public Script() {}
+ public Script()
+ {
+ }
public Script(String conversationId)
{
@@ -161,7 +163,7 @@
public void run() throws Exception
{
- TestLifecycle.beginTest( servletContext, new ServletSessionMap(session) );
+ TestLifecycle.beginTest(servletContext, new ServletSessionMap(session));
try
{
testComponents();
@@ -210,12 +212,12 @@
protected Map<String, String[]> getParameters()
{
- return ( (MockHttpServletRequest) externalContext.getRequest() ).getParameters();
+ return ((MockHttpServletRequest) externalContext.getRequest()).getParameters();
}
protected Map<String, String[]> getHeaders()
{
- return ( (MockHttpServletRequest) externalContext.getRequest() ).getHeaders();
+ return ((MockHttpServletRequest) externalContext.getRequest()).getHeaders();
}
/**
@@ -266,7 +268,9 @@
* A script for a JSF interaction with no existing long-running
* conversation.
*/
- protected Request() {}
+ protected Request()
+ {
+ }
/**
* A script for a JSF interaction in the scope of an existing long-running
@@ -306,25 +310,33 @@
* Override to implement the interactions between the JSF page and your
* components that occurs during the apply request values phase.
*/
- protected void applyRequestValues() throws Exception {}
+ protected void applyRequestValues() throws Exception
+ {
+ }
/**
* Override to implement the interactions between the JSF page and your
* components that occurs during the process validations phase.
*/
- protected void processValidations() throws Exception {}
+ protected void processValidations() throws Exception
+ {
+ }
/**
* Override to implement the interactions between the JSF page and your
* components that occurs during the update model values phase.
*/
- protected void updateModelValues() throws Exception {}
+ protected void updateModelValues() throws Exception
+ {
+ }
/**
* Override to implement the interactions between the JSF page and your
* components that occurs during the invoke application phase.
*/
- protected void invokeApplication() throws Exception {}
+ protected void invokeApplication() throws Exception
+ {
+ }
/**
* Set the outcome of the INVOKE_APPLICATION phase
@@ -354,19 +366,25 @@
* Override to implement the interactions between the JSF page and your
* components that occurs during the render response phase.
*/
- protected void renderResponse() throws Exception {}
+ protected void renderResponse() throws Exception
+ {
+ }
/**
* Override to set up any request parameters for the request.
*
* @deprecated use beforeRequest()
*/
- protected void setup() {}
+ protected void setup()
+ {
+ }
/**
* Make some assertions, after the end of the request.
*/
- protected void afterRequest() {}
+ protected void afterRequest()
+ {
+ }
/**
* Do anything you like, after the start of the request. Especially, set
@@ -405,8 +423,7 @@
if (ivs.length > 0)
{
validationFailed = true;
- FacesMessage message = FacesMessages.createFacesMessage(FacesMessage.SEVERITY_WARN,
- ivs[0].getMessage());
+ FacesMessage message = FacesMessages.createFacesMessage(FacesMessage.SEVERITY_WARN, ivs[0].getMessage());
FacesContext.getCurrentInstance().addMessage(property, /* TODO */message);
FacesContext.getCurrentInstance().renderResponse();
}
@@ -443,8 +460,7 @@
*/
protected void setValue(String valueExpression, Object value)
{
- application.getExpressionFactory().createValueExpression(facesContext.getELContext(),
- valueExpression, Object.class).setValue(facesContext.getELContext(), value);
+ application.getExpressionFactory().createValueExpression(facesContext.getELContext(), valueExpression, Object.class).setValue(facesContext.getELContext(), value);
}
/**
@@ -453,13 +469,12 @@
*/
protected boolean validateValue(String valueExpression, Object value)
{
- ValueExpression ve = application.getExpressionFactory()
- .createValueExpression(facesContext.getELContext(), valueExpression, Object.class);
+ ValueExpression ve = application.getExpressionFactory().createValueExpression(facesContext.getELContext(), valueExpression, Object.class);
InvalidValue[] ivs = Validators.instance().validate(ve, facesContext.getELContext(), value);
if (ivs.length > 0)
{
validationFailed = true;
- facesContext.addMessage( null, FacesMessages.createFacesMessage(FacesMessage.SEVERITY_ERROR, ivs[0].getMessage()) );
+ facesContext.addMessage(null, FacesMessages.createFacesMessage(FacesMessage.SEVERITY_ERROR, ivs[0].getMessage()));
return false;
}
else
@@ -478,9 +493,7 @@
*/
protected Object invokeMethod(String methodExpression)
{
- return application.getExpressionFactory().createMethodExpression(
- facesContext.getELContext(), methodExpression, Object.class, new Class[0])
- .invoke(facesContext.getELContext(), null);
+ return application.getExpressionFactory().createMethodExpression(facesContext.getELContext(), methodExpression, Object.class, new Class[0]).invoke(facesContext.getELContext(), null);
}
/**
@@ -490,17 +503,16 @@
{
action = actionMethodExpression;
Object result = invokeMethod(actionMethodExpression);
- if (result!=null)
+ if (result != null)
{
- setOutcome( result.toString() );
+ setOutcome(result.toString());
}
return result;
}
/**
* @return the conversation id
- * @throws Exception
- * to fail the test
+ * @throws Exception to fail the test
*/
public String run() throws Exception
{
@@ -509,12 +521,13 @@
init();
beforeRequest();
setStandardJspVariables();
- seamFilter.doFilter(request, response, new FilterChain() {
+ seamFilter.doFilter(request, response, new FilterChain()
+ {
public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException
{
try
{
- if ( emulateJsfLifecycle() )
+ if (emulateJsfLifecycle())
{
saveConversationViewRoot();
}
@@ -525,7 +538,7 @@
throw new ServletException(e);
}
}
- } );
+ });
seamFilter.destroy();
afterRequest();
return conversationId;
@@ -543,7 +556,7 @@
private void saveConversationViewRoot()
{
Map renderedViewRootAttributes = facesContext.getViewRoot().getAttributes();
- if (renderedViewRootAttributes!=null && conversationId!=null )
+ if (renderedViewRootAttributes != null && conversationId != null)
{
Map conversationState = new HashMap();
conversationState.putAll(renderedViewRootAttributes);
@@ -553,7 +566,7 @@
protected void init()
{
- Cookie[] cookieArray = getCookies().toArray( new Cookie[]{} );
+ Cookie[] cookieArray = getCookies().toArray(new Cookie[] {});
request = new MockHttpServletRequest(session, getPrincipalName(), getPrincipalRoles(), cookieArray, isGetRequest() ? "GET" : "POST");
response = new MockHttpServletResponse();
externalContext = new MockExternalContext(servletContext, request, response);
@@ -563,10 +576,10 @@
private void setStandardJspVariables()
{
- //TODO: looks like we should also set request, session, application, page...
+ // 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())
+ for (Map.Entry<String, String[]> e : ((Map<String, String[]>) request.getParameterMap()).entrySet())
{
if (e.getValue().length == 1)
{
@@ -582,16 +595,16 @@
private boolean emulateJsfLifecycle() throws Exception
{
restoreViewPhase();
- if ( !isGetRequest() && !skipToRender() )
+ if (!isGetRequest() && !skipToRender())
{
applyRequestValuesPhase();
if (!skipToRender())
{
processValidationsPhase();
- if ( !skipToRender() )
+ if (!skipToRender())
{
updateModelValuesPhase();
- if ( !skipToRender() )
+ if (!skipToRender())
{
invokeApplicationPhase();
}
@@ -599,7 +612,7 @@
}
}
- if ( skipRender() )
+ if (skipRender())
{
// we really should look at redirect parameters here!
return false;
@@ -613,8 +626,7 @@
private void renderResponsePhase() throws Exception
{
- phases.beforePhase(new PhaseEvent(facesContext, PhaseId.RENDER_RESPONSE,
- MockLifecycle.INSTANCE));
+ phases.beforePhase(new PhaseEvent(facesContext, PhaseId.RENDER_RESPONSE, MockLifecycle.INSTANCE));
try
{
@@ -632,15 +644,13 @@
}
finally
{
- phases.afterPhase(new PhaseEvent(facesContext, PhaseId.RENDER_RESPONSE,
- MockLifecycle.INSTANCE));
+ phases.afterPhase(new PhaseEvent(facesContext, PhaseId.RENDER_RESPONSE, MockLifecycle.INSTANCE));
}
}
private void invokeApplicationPhase() throws Exception
{
- phases.beforePhase(new PhaseEvent(facesContext, PhaseId.INVOKE_APPLICATION,
- MockLifecycle.INSTANCE));
+ phases.beforePhase(new PhaseEvent(facesContext, PhaseId.INVOKE_APPLICATION, MockLifecycle.INSTANCE));
try
{
updateConversationId();
@@ -652,8 +662,7 @@
invokeApplicationComplete = true;
String outcome = getInvokeApplicationOutcome();
- facesContext.getApplication().getNavigationHandler()
- .handleNavigation(facesContext, action, outcome);
+ facesContext.getApplication().getNavigationHandler().handleNavigation(facesContext, action, outcome);
viewId = getRenderedViewId();
@@ -661,15 +670,13 @@
}
finally
{
- phases.afterPhase(new PhaseEvent(facesContext, PhaseId.INVOKE_APPLICATION,
- MockLifecycle.INSTANCE));
+ 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));
+ phases.beforePhase(new PhaseEvent(facesContext, PhaseId.UPDATE_MODEL_VALUES, MockLifecycle.INSTANCE));
try
{
updateConversationId();
@@ -680,15 +687,13 @@
}
finally
{
- phases.afterPhase(new PhaseEvent(facesContext, PhaseId.UPDATE_MODEL_VALUES,
- MockLifecycle.INSTANCE));
+ phases.afterPhase(new PhaseEvent(facesContext, PhaseId.UPDATE_MODEL_VALUES, MockLifecycle.INSTANCE));
}
}
private void processValidationsPhase() throws Exception
{
- phases.beforePhase(new PhaseEvent(facesContext, PhaseId.PROCESS_VALIDATIONS,
- MockLifecycle.INSTANCE));
+ phases.beforePhase(new PhaseEvent(facesContext, PhaseId.PROCESS_VALIDATIONS, MockLifecycle.INSTANCE));
try
{
updateConversationId();
@@ -697,22 +702,20 @@
updateConversationId();
- if ( isValidationFailure() )
+ if (isValidationFailure())
{
facesContext.renderResponse();
}
}
finally
{
- phases.afterPhase(new PhaseEvent(facesContext, PhaseId.PROCESS_VALIDATIONS,
- MockLifecycle.INSTANCE));
+ 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));
+ phases.beforePhase(new PhaseEvent(facesContext, PhaseId.APPLY_REQUEST_VALUES, MockLifecycle.INSTANCE));
try
{
updateConversationId();
@@ -723,24 +726,21 @@
}
finally
{
- phases.afterPhase(new PhaseEvent(facesContext, PhaseId.APPLY_REQUEST_VALUES,
- MockLifecycle.INSTANCE));
+ phases.afterPhase(new PhaseEvent(facesContext, PhaseId.APPLY_REQUEST_VALUES, MockLifecycle.INSTANCE));
}
}
private void restoreViewPhase()
{
- phases.beforePhase(new PhaseEvent(facesContext, PhaseId.RESTORE_VIEW,
- MockLifecycle.INSTANCE));
+ phases.beforePhase(new PhaseEvent(facesContext, PhaseId.RESTORE_VIEW, MockLifecycle.INSTANCE));
try
{
- UIViewRoot viewRoot = facesContext.getApplication().getViewHandler().createView(
- facesContext, getViewId());
+ UIViewRoot viewRoot = facesContext.getApplication().getViewHandler().createView(facesContext, getViewId());
facesContext.setViewRoot(viewRoot);
Map restoredViewRootAttributes = facesContext.getViewRoot().getAttributes();
if (conversationId != null)
{
- if ( isGetRequest() )
+ if (isGetRequest())
{
setParameter(Manager.instance().getConversationIdParameter(), conversationId);
// TODO: what about conversationIsLongRunning????
@@ -756,7 +756,7 @@
}
}
}
- if ( isGetRequest() )
+ if (isGetRequest())
{
facesContext.renderResponse();
}
@@ -767,17 +767,14 @@
}
finally
{
- phases.afterPhase(new PhaseEvent(facesContext, PhaseId.RESTORE_VIEW,
- MockLifecycle.INSTANCE));
+ phases.afterPhase(new PhaseEvent(facesContext, PhaseId.RESTORE_VIEW, MockLifecycle.INSTANCE));
}
}
private void updateConversationId()
{
Manager manager = Manager.instance();
- conversationId = manager.isLongRunningConversation() ?
- manager.getCurrentConversationId() :
- manager.getParentConversationId() ;
+ conversationId = manager.isLongRunningConversation() ? manager.getCurrentConversationId() : manager.getParentConversationId();
}
private boolean skipRender()
@@ -787,8 +784,7 @@
private boolean skipToRender()
{
- return FacesContext.getCurrentInstance().getRenderResponse()
- || FacesContext.getCurrentInstance().getResponseComplete();
+ return FacesContext.getCurrentInstance().getRenderResponse() || FacesContext.getCurrentInstance().getResponseComplete();
}
protected boolean isInvokeApplicationBegun()
@@ -823,11 +819,12 @@
public class NonFacesRequest extends Request
{
- public NonFacesRequest() {}
+ public NonFacesRequest()
+ {
+ }
/**
- * @param viewId
- * the view id to be rendered
+ * @param viewId the view id to be rendered
*/
public NonFacesRequest(String viewId)
{
@@ -835,10 +832,8 @@
}
/**
- * @param viewId
- * the view id to be rendered
- * @param conversationId
- * the conversation id
+ * @param viewId the view id to be rendered
+ * @param conversationId the conversation id
*/
public NonFacesRequest(String viewId, String conversationId)
{
@@ -881,11 +876,12 @@
public class FacesRequest extends Request
{
- public FacesRequest() {}
+ public FacesRequest()
+ {
+ }
/**
- * @param viewId
- * the view id of the form that was submitted
+ * @param viewId the view id of the form that was submitted
*/
public FacesRequest(String viewId)
{
@@ -893,10 +889,8 @@
}
/**
- * @param viewId
- * the view id of the form that was submitted
- * @param conversationId
- * the conversation id
+ * @param viewId the view id of the form that was submitted
+ * @param conversationId the conversation id
*/
public FacesRequest(String viewId, String conversationId)
{
@@ -924,18 +918,50 @@
session = null;
}
+ @Deprecated
public void init() throws Exception
{
- startJbossEmbeddedIfNecessary();
-
- application = new SeamApplication( new MockApplication() );
- phases = new SeamPhaseListener();
+ startSeam();
+ setupClass();
+ }
+ /**
+ * Boot Seam. Can be used at class, test group or suite level (e.g.
+ * @BeforeClass, @BeforeTest, @BeforeSuite)
+ * Use in conjunction with {@link #stopSeam()}.
+ * @throws Exception
+ */
+ protected void startSeam() throws Exception
+ {
+ startJbossEmbeddedIfNecessary();
servletContext = new MockServletContext();
- initServletContext( servletContext.getInitParameters() );
+ initServletContext(servletContext.getInitParameters());
ServletLifecycle.beginApplication(servletContext);
new Initialization(servletContext).create().init();
- ( (Init) servletContext.getAttribute( Seam.getComponentName(Init.class) ) ).setDebug(false);
+ ((Init) servletContext.getAttribute(Seam.getComponentName(Init.class))).setDebug(false);
+ }
+
+ /**
+ * Shutdown Seam. Can be used at class, test group or suite level (e.g
+ * @AfterClass, @AfterTest, @AfterSuite)
+ * Use in conjunction with {@link #startSeam()}.
+ * @throws Exception
+ */
+ protected void stopSeam() throws Exception
+ {
+ ServletLifecycle.endApplication();
+ }
+
+ /**
+ * Setup this test class instance
+ * Must be run for each test class instance (e.g. @BeforeClass)
+ * @throws Exception
+ */
+ protected void setupClass() throws Exception
+ {
+ servletContext = (MockServletContext) ServletLifecycle.getServletContext();
+ application = new SeamApplication(new MockApplication());
+ phases = new SeamPhaseListener();
conversationViewRootAttributes = new HashMap<String, Map>();
seamFilter = createSeamFilter();
@@ -943,27 +969,38 @@
{
application.addELResolver(elResolver);
}
-
}
- public void cleanup() throws Exception
+ /**
+ * Cleanup this test class instance
+ * Must be run for each test class instance (e.g. @AfterClass)
+ */
+ protected void cleanupClass() throws Exception
{
seamFilter.destroy();
- ServletLifecycle.endApplication();
conversationViewRootAttributes = null;
}
+ @Deprecated
+ public void cleanup() throws Exception
+ {
+ cleanupClass();
+ stopSeam();
+ }
+
protected Filter createSeamFilter() throws ServletException
{
SeamFilter seamFilter = new SeamFilter();
- seamFilter.init( new MockFilterConfig(servletContext) );
+ seamFilter.init(new MockFilterConfig(servletContext));
return seamFilter;
}
/**
* Override to set up any servlet context attributes.
*/
- public void initServletContext(Map initParams) {}
+ public void initServletContext(Map initParams)
+ {
+ }
protected InitialContext getInitialContext() throws NamingException
{
@@ -981,7 +1018,8 @@
protected Object getField(Object object, String fieldName)
{
Field field = Reflections.getField(object.getClass(), fieldName);
- if (!field.isAccessible()) field.setAccessible(true);
+ if (!field.isAccessible())
+ field.setAccessible(true);
return Reflections.getAndWrap(field, object);
}
@@ -991,14 +1029,14 @@
protected void setField(Object object, String fieldName, Object value)
{
Field field = Reflections.getField(object.getClass(), fieldName);
- if (!field.isAccessible()) field.setAccessible(true);
+ if (!field.isAccessible())
+ field.setAccessible(true);
Reflections.setAndWrap(field, object, value);
}
private static boolean started;
- protected void startJbossEmbeddedIfNecessary()
- throws Exception
+ protected void startJbossEmbeddedIfNecessary() throws Exception
{
if (!started && embeddedJBossAvailable())
{
More information about the jboss-cvs-commits
mailing list