1 16 package org.springframework.webflow.test.execution; 17 18 import java.util.Collection ; 19 import java.util.Map ; 20 21 import junit.framework.TestCase; 22 23 import org.springframework.binding.expression.ExpressionParser; 24 import org.springframework.core.style.StylerUtils; 25 import org.springframework.util.Assert; 26 import org.springframework.webflow.context.ExternalContext; 27 import org.springframework.webflow.core.DefaultExpressionParserFactory; 28 import org.springframework.webflow.core.collection.MutableAttributeMap; 29 import org.springframework.webflow.core.collection.ParameterMap; 30 import org.springframework.webflow.definition.FlowDefinition; 31 import org.springframework.webflow.engine.impl.FlowExecutionImplFactory; 32 import org.springframework.webflow.execution.FlowExecution; 33 import org.springframework.webflow.execution.FlowExecutionException; 34 import org.springframework.webflow.execution.FlowExecutionFactory; 35 import org.springframework.webflow.execution.ViewSelection; 36 import org.springframework.webflow.execution.support.ApplicationView; 37 import org.springframework.webflow.execution.support.ExternalRedirect; 38 import org.springframework.webflow.execution.support.FlowDefinitionRedirect; 39 import org.springframework.webflow.execution.support.FlowExecutionRedirect; 40 import org.springframework.webflow.test.MockExternalContext; 41 42 78 public abstract class AbstractFlowExecutionTests extends TestCase { 79 80 83 private FlowExecutionFactory flowExecutionFactory; 84 85 89 private ExpressionParser expressionParser = DefaultExpressionParserFactory.getExpressionParser(); 90 91 95 private FlowExecution flowExecution; 96 97 101 public void setExpressionParser(ExpressionParser expressionParser) { 102 Assert.notNull(expressionParser, "The expression parser is required"); 103 this.expressionParser = expressionParser; 104 } 105 106 112 protected FlowExecutionFactory getFlowExecutionFactory() { 113 if (flowExecutionFactory == null) { 114 flowExecutionFactory = createFlowExecutionFactory(); 115 } 116 return flowExecutionFactory; 117 } 118 119 127 protected ExternalContext createExternalContext(ParameterMap requestParameters) { 128 return new MockExternalContext(requestParameters); 129 } 130 131 146 protected ViewSelection startFlow() throws FlowExecutionException { 147 return startFlow(null, createExternalContext(null)); 148 } 149 150 168 protected ViewSelection startFlow(MutableAttributeMap input) throws FlowExecutionException { 169 return startFlow(input, createExternalContext(null)); 170 } 171 172 193 protected ViewSelection startFlow(MutableAttributeMap input, ExternalContext context) throws FlowExecutionException { 194 flowExecution = getFlowExecutionFactory().createFlowExecution(getFlowDefinition()); 195 return flowExecution.start(input, context); 196 } 197 198 205 protected ViewSelection signalEvent(String eventId) throws FlowExecutionException { 206 return signalEvent(eventId, createExternalContext(null)); 207 } 208 209 218 protected ViewSelection signalEvent(String eventId, ParameterMap requestParameters) throws FlowExecutionException { 219 return signalEvent(eventId, createExternalContext(requestParameters)); 220 } 221 222 265 protected ViewSelection signalEvent(String eventId, ExternalContext context) throws FlowExecutionException { 266 Assert.state(flowExecution != null, "The flow execution to test is [null]; " 267 + "you must start the flow execution before you can signal an event against it!"); 268 return flowExecution.signalEvent(eventId, context); 269 } 270 271 279 protected ViewSelection refresh() throws FlowExecutionException { 280 return refresh(createExternalContext(null)); 281 } 282 283 294 protected ViewSelection refresh(ExternalContext context) throws FlowExecutionException { 295 Assert.state(flowExecution != null, 296 "The flow execution to test is [null]; you must start the flow execution before you can refresh it!"); 297 return flowExecution.refresh(context); 298 } 299 300 302 307 protected FlowExecution getFlowExecution() throws IllegalStateException { 308 Assert.state(flowExecution != null, 309 "The flow execution to test is [null]; you must start the flow execution before you can query it!"); 310 return flowExecution; 311 } 312 313 319 protected Object getConversationAttribute(String attributeName) { 320 return getFlowExecution().getConversationScope().get(attributeName); 321 } 322 323 331 protected Object getRequiredConversationAttribute(String attributeName) throws IllegalStateException { 332 return getFlowExecution().getConversationScope().getRequired(attributeName); 333 } 334 335 344 protected Object getRequiredConversationAttribute(String attributeName, Class requiredType) 345 throws IllegalStateException { 346 return getFlowExecution().getConversationScope().getRequired(attributeName, requiredType); 347 } 348 349 355 protected Object getFlowAttribute(String attributeName) { 356 return getFlowExecution().getActiveSession().getScope().get(attributeName); 357 } 358 359 366 protected Object getRequiredFlowAttribute(String attributeName) throws IllegalStateException { 367 return getFlowExecution().getActiveSession().getScope().getRequired(attributeName); 368 } 369 370 379 protected Object getRequiredFlowAttribute(String attributeName, Class requiredType) throws IllegalStateException { 380 return getFlowExecution().getActiveSession().getScope().getRequired(attributeName, requiredType); 381 } 382 383 385 390 protected void assertActiveFlowEquals(String expectedActiveFlowId) { 391 assertEquals("The active flow id '" + getFlowExecution().getActiveSession().getDefinition().getId() 392 + "' does not equal the expected active flow id '" + expectedActiveFlowId + "'", expectedActiveFlowId, 393 getFlowExecution().getActiveSession().getDefinition().getId()); 394 } 395 396 400 protected void assertFlowExecutionActive() { 401 assertTrue("The flow execution is not active but it should be", getFlowExecution().isActive()); 402 } 403 404 408 protected void assertFlowExecutionEnded() { 409 assertTrue("The flow execution is still active but it should have ended", !getFlowExecution().isActive()); 410 } 411 412 417 protected void assertCurrentStateEquals(String expectedCurrentStateId) { 418 assertEquals("The current state '" + getFlowExecution().getActiveSession().getState().getId() 419 + "' does not equal the expected state '" + expectedCurrentStateId + "'", expectedCurrentStateId, 420 getFlowExecution().getActiveSession().getState().getId()); 421 } 422 423 428 protected void assertViewNameEquals(String expectedViewName, ApplicationView viewSelection) { 429 assertEquals("The view name is wrong:", expectedViewName, viewSelection.getViewName()); 430 } 431 432 440 protected void assertModelAttributeEquals(Object expectedValue, String attributeName, ApplicationView viewSelection) { 441 assertEquals("The model attribute '" + attributeName + "' value is wrong:", expectedValue, 442 evaluateModelAttributeExpression(attributeName, viewSelection.getModel())); 443 } 444 445 453 protected void assertModelAttributeCollectionSize(int expectedSize, String attributeName, 454 ApplicationView viewSelection) { 455 assertModelAttributeNotNull(attributeName, viewSelection); 456 Collection c = (Collection )evaluateModelAttributeExpression(attributeName, viewSelection.getModel()); 457 assertEquals("The model attribute '" + attributeName + "' collection size is wrong:", expectedSize, c.size()); 458 } 459 460 466 protected void assertModelAttributeNotNull(String attributeName, ApplicationView viewSelection) { 467 assertNotNull("The model attribute '" + attributeName + "' is null but should not be; model contents are " 468 + StylerUtils.style(viewSelection.getModel()), evaluateModelAttributeExpression(attributeName, 469 viewSelection.getModel())); 470 } 471 472 479 protected void assertModelAttributeNull(String attributeName, ApplicationView viewSelection) { 480 assertNull("The model attribute '" + attributeName + "' is not null but should be; model contents are " 481 + StylerUtils.style(viewSelection.getModel()), evaluateModelAttributeExpression(attributeName, 482 viewSelection.getModel())); 483 } 484 485 487 492 protected ApplicationView applicationView(ViewSelection viewSelection) { 493 Assert.isInstanceOf(ApplicationView.class, viewSelection, "Unexpected class of view selection: "); 494 return (ApplicationView)viewSelection; 495 } 496 497 502 protected FlowExecutionRedirect flowExecutionRedirect(ViewSelection viewSelection) { 503 Assert.isInstanceOf(FlowExecutionRedirect.class, viewSelection, "Unexpected class of view selection: "); 504 return (FlowExecutionRedirect)viewSelection; 505 } 506 507 512 protected FlowDefinitionRedirect flowDefinitionRedirect(ViewSelection viewSelection) { 513 Assert.isInstanceOf(FlowDefinitionRedirect.class, viewSelection, "Unexpected class of view selection: "); 514 return (FlowDefinitionRedirect)viewSelection; 515 } 516 517 522 protected ExternalRedirect externalRedirect(ViewSelection viewSelection) { 523 Assert.isInstanceOf(ExternalRedirect.class, viewSelection, "Unexpected class of view selection: "); 524 return (ExternalRedirect)viewSelection; 525 } 526 527 532 protected void nullView(ViewSelection viewSelection) { 533 assertEquals("Not the null view selection:", viewSelection, ViewSelection.NULL_VIEW); 534 } 535 536 542 protected Object evaluateModelAttributeExpression(String attributeName, Map model) { 543 return expressionParser.parseExpression(attributeName).evaluate(model, null); 544 } 545 546 553 protected FlowExecutionFactory createFlowExecutionFactory() { 554 return new FlowExecutionImplFactory(); 555 } 556 557 564 protected void updateFlowExecution(FlowExecution flowExecution) { 565 this.flowExecution = flowExecution; 566 } 567 568 572 protected abstract FlowDefinition getFlowDefinition(); 573 } | Popular Tags |