test
This commit is contained in:
@@ -15,7 +15,6 @@
|
||||
*/
|
||||
package org.springframework.webflow.engine.support;
|
||||
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.webflow.engine.ActionExecutor;
|
||||
import org.springframework.webflow.engine.TransitionCriteria;
|
||||
import org.springframework.webflow.execution.Action;
|
||||
@@ -39,7 +38,7 @@ public class ActionTransitionCriteria implements TransitionCriteria {
|
||||
/**
|
||||
* The result event id that should map to a <code>true</code> return value.
|
||||
*/
|
||||
private String trueEventId = "success";
|
||||
private String[] trueEventIds = new String[] { "success", "yes", "true" };
|
||||
|
||||
/**
|
||||
* The action to execute when the criteria is tested, annotated with usage attributes.
|
||||
@@ -55,21 +54,20 @@ public class ActionTransitionCriteria implements TransitionCriteria {
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the action result <code>eventId</code> that should cause this criteria to return true (it will return
|
||||
* Returns the action result <code>eventIds</code> that should cause this criteria to return true (it will return
|
||||
* false otherwise). Defaults to "success".
|
||||
*/
|
||||
public String getTrueEventId() {
|
||||
return trueEventId;
|
||||
public String[] getTrueEventIds() {
|
||||
return trueEventIds;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the action result <code>eventId</code> that should cause this precondition to return true (it will return
|
||||
* Sets the action result <code>eventIds</code> that should cause this precondition to return true (it will return
|
||||
* false otherwise).
|
||||
* @param trueEventId the true result event ID
|
||||
* @param trueEventIds the true result event IDs
|
||||
*/
|
||||
public void setTrueEventId(String trueEventId) {
|
||||
Assert.notNull(trueEventId, "The trueEventId is required");
|
||||
this.trueEventId = trueEventId;
|
||||
public void setTrueEventIds(String[] trueEventIds) {
|
||||
this.trueEventIds = trueEventIds;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -82,6 +80,15 @@ public class ActionTransitionCriteria implements TransitionCriteria {
|
||||
|
||||
public boolean test(RequestContext context) {
|
||||
Event result = ActionExecutor.execute(getAction(), context);
|
||||
return result != null && getTrueEventId().equals(result.getId());
|
||||
return result != null && isTrueEvent(result.getId());
|
||||
}
|
||||
|
||||
private boolean isTrueEvent(String eventId) {
|
||||
for (int i = 0; i < trueEventIds.length; i++) {
|
||||
if (trueEventIds[i].equals(eventId)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -17,50 +17,41 @@ package org.springframework.webflow.engine.support;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.easymock.EasyMock;
|
||||
import org.springframework.webflow.execution.Action;
|
||||
import org.springframework.webflow.execution.Event;
|
||||
import org.springframework.webflow.test.MockAction;
|
||||
import org.springframework.webflow.test.MockRequestContext;
|
||||
|
||||
/**
|
||||
* Unit tests for the ActionTransitionCriteria class.
|
||||
*
|
||||
* @author Ulrik Sandberg
|
||||
*/
|
||||
public class ActionTransitionCriteriaTests extends TestCase {
|
||||
|
||||
private Action actionMock;
|
||||
private MockAction action;
|
||||
|
||||
private ActionTransitionCriteria tested;
|
||||
private ActionTransitionCriteria criteria;
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
actionMock = (Action) EasyMock.createMock(Action.class);
|
||||
tested = new ActionTransitionCriteria(actionMock);
|
||||
action = new MockAction();
|
||||
criteria = new ActionTransitionCriteria(action);
|
||||
}
|
||||
|
||||
public void testGetTrueEventId() {
|
||||
String id = tested.getTrueEventId();
|
||||
assertEquals("success", id);
|
||||
public void testExecuteSuccessResult() throws Exception {
|
||||
MockRequestContext context = new MockRequestContext();
|
||||
assertTrue(criteria.test(context));
|
||||
}
|
||||
|
||||
public void testSetTrueEventId() {
|
||||
tested.setTrueEventId("something");
|
||||
String id = tested.getTrueEventId();
|
||||
assertEquals("something", id);
|
||||
public void testExecuteTrueResult() throws Exception {
|
||||
action.setResultEventId("true");
|
||||
MockRequestContext context = new MockRequestContext();
|
||||
assertTrue(criteria.test(context));
|
||||
}
|
||||
|
||||
public void testGetAction() {
|
||||
Action action = tested.getAction();
|
||||
assertSame(actionMock, action);
|
||||
public void testExecuteYesResult() throws Exception {
|
||||
action.setResultEventId("yes");
|
||||
MockRequestContext context = new MockRequestContext();
|
||||
assertTrue(criteria.test(context));
|
||||
}
|
||||
|
||||
public void testTest() throws Exception {
|
||||
MockRequestContext mockRequestContext = new MockRequestContext();
|
||||
EasyMock.expect(actionMock.execute(mockRequestContext)).andReturn(new Event(this, "success"));
|
||||
EasyMock.replay(new Object[] { actionMock });
|
||||
boolean result = tested.test(mockRequestContext);
|
||||
EasyMock.verify(new Object[] { actionMock });
|
||||
assertEquals(true, result);
|
||||
public void testExecuteErrorResult() throws Exception {
|
||||
action.setResultEventId("whatever");
|
||||
MockRequestContext context = new MockRequestContext();
|
||||
assertFalse(criteria.test(context));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user