mock transition criteria

This commit is contained in:
Keith Donald
2008-05-13 15:05:31 +00:00
parent b0fd959c94
commit 90cdec76e7
9 changed files with 19 additions and 161 deletions

View File

@@ -33,12 +33,10 @@ import org.springframework.webflow.execution.RequestContext;
* <p>
* This converter supports the following encoded forms:
* <ul>
* <li>"*" - will result in a TransitionCriteria object that matches on everything ({@link org.springframework.webflow.engine.WildcardTransitionCriteria})
* </li>
* <li>"eventId" - will result in a TransitionCriteria object that matches given event id ({@link org.springframework.webflow.engine.support.EventIdTransitionCriteria})
* </li>
* <li>"${...}" - will result in a TransitionCriteria object that evaluates given condition, expressed as an expression ({@link org.springframework.webflow.engine.support.DefaultTransitionCriteria})
* </li>
* <li>"*" - will result in a TransitionCriteria object that matches on everything. </li>
* <li>"eventId" - will result in a TransitionCriteria object that matches given event id. </li>
* <li>"${...}" - will result in a TransitionCriteria object that evaluates given condition, expressed as an
* expression. </li>
* </ul>
*
* @see org.springframework.webflow.engine.TransitionCriteria

View File

@@ -18,7 +18,7 @@ package org.springframework.webflow.engine;
import junit.framework.TestCase;
import org.springframework.webflow.engine.support.DefaultTargetStateResolver;
import org.springframework.webflow.engine.support.EventIdTransitionCriteria;
import org.springframework.webflow.engine.support.MockTransitionCriteria;
import org.springframework.webflow.execution.Action;
import org.springframework.webflow.execution.TestAction;
import org.springframework.webflow.test.MockRequestControlContext;
@@ -81,7 +81,7 @@ public class ActionStateTests extends TestCase {
}
protected TransitionCriteria on(String event) {
return new EventIdTransitionCriteria(event);
return new MockTransitionCriteria(event);
}
protected TargetStateResolver to(String stateId) {

View File

@@ -18,7 +18,7 @@ package org.springframework.webflow.engine;
import junit.framework.TestCase;
import org.springframework.webflow.engine.support.DefaultTargetStateResolver;
import org.springframework.webflow.engine.support.EventIdTransitionCriteria;
import org.springframework.webflow.engine.support.MockTransitionCriteria;
import org.springframework.webflow.execution.Event;
import org.springframework.webflow.test.MockRequestControlContext;
@@ -32,7 +32,7 @@ public class DecisionStateTests extends TestCase {
public void testIfDecision() {
Flow flow = new Flow("flow");
DecisionState state = new DecisionState(flow, "decisionState");
state.getTransitionSet().add(new Transition(new EventIdTransitionCriteria("foo"), to("target")));
state.getTransitionSet().add(new Transition(new MockTransitionCriteria("foo"), to("target")));
new EndState(flow, "target");
MockRequestControlContext context = new MockRequestControlContext(flow);
context.setCurrentEvent(new Event(this, "foo"));
@@ -43,7 +43,7 @@ public class DecisionStateTests extends TestCase {
public void testElseDecision() {
Flow flow = new Flow("flow");
DecisionState state = new DecisionState(flow, "decisionState");
state.getTransitionSet().add(new Transition(new EventIdTransitionCriteria("foo"), to("invalid")));
state.getTransitionSet().add(new Transition(new MockTransitionCriteria("foo"), to("invalid")));
state.getTransitionSet().add(new Transition(to("target")));
new EndState(flow, "target");
MockRequestControlContext context = new MockRequestControlContext(flow);
@@ -55,8 +55,8 @@ public class DecisionStateTests extends TestCase {
public void testCannotDecide() {
Flow flow = new Flow("flow");
DecisionState state = new DecisionState(flow, "decisionState");
state.getTransitionSet().add(new Transition(new EventIdTransitionCriteria("foo"), to("invalid")));
state.getTransitionSet().add(new Transition(new EventIdTransitionCriteria("bar"), to("invalid")));
state.getTransitionSet().add(new Transition(new MockTransitionCriteria("foo"), to("invalid")));
state.getTransitionSet().add(new Transition(new MockTransitionCriteria("bar"), to("invalid")));
MockRequestControlContext context = new MockRequestControlContext(flow);
context.setCurrentEvent(new Event(this, "bogus"));
try {

View File

@@ -26,7 +26,7 @@ import org.springframework.binding.mapping.impl.DefaultMapper;
import org.springframework.binding.mapping.impl.DefaultMapping;
import org.springframework.webflow.core.collection.MutableAttributeMap;
import org.springframework.webflow.engine.support.DefaultTargetStateResolver;
import org.springframework.webflow.engine.support.EventIdTransitionCriteria;
import org.springframework.webflow.engine.support.MockTransitionCriteria;
import org.springframework.webflow.execution.Action;
import org.springframework.webflow.execution.Event;
import org.springframework.webflow.execution.FlowExecutionException;
@@ -103,7 +103,7 @@ public class EndStateTests extends TestCase {
}
protected static TransitionCriteria on(String event) {
return new EventIdTransitionCriteria(event);
return new MockTransitionCriteria(event);
}
protected static TargetStateResolver to(String stateId) {

View File

@@ -30,7 +30,7 @@ import org.springframework.webflow.core.collection.AttributeMap;
import org.springframework.webflow.core.collection.LocalAttributeMap;
import org.springframework.webflow.core.collection.MutableAttributeMap;
import org.springframework.webflow.engine.support.DefaultTargetStateResolver;
import org.springframework.webflow.engine.support.EventIdTransitionCriteria;
import org.springframework.webflow.engine.support.MockTransitionCriteria;
import org.springframework.webflow.engine.support.TransitionExecutingFlowExecutionExceptionHandler;
import org.springframework.webflow.execution.Event;
import org.springframework.webflow.execution.FlowExecutionException;
@@ -349,7 +349,7 @@ public class FlowTests extends TestCase {
}
public TransitionCriteria on(String eventId) {
return new EventIdTransitionCriteria(eventId);
return new MockTransitionCriteria(eventId);
}
protected TargetStateResolver to(String stateId) {

View File

@@ -28,7 +28,7 @@ import org.springframework.webflow.core.collection.AttributeMap;
import org.springframework.webflow.core.collection.LocalAttributeMap;
import org.springframework.webflow.core.collection.MutableAttributeMap;
import org.springframework.webflow.engine.support.DefaultTargetStateResolver;
import org.springframework.webflow.engine.support.EventIdTransitionCriteria;
import org.springframework.webflow.engine.support.MockTransitionCriteria;
import org.springframework.webflow.execution.FlowExecutionException;
import org.springframework.webflow.execution.RequestContext;
import org.springframework.webflow.test.MockRequestControlContext;
@@ -118,7 +118,7 @@ public class SubflowStateTests extends TestCase {
}
protected TransitionCriteria on(String event) {
return new EventIdTransitionCriteria(event);
return new MockTransitionCriteria(event);
}
protected TargetStateResolver to(String stateId) {

View File

@@ -20,7 +20,7 @@ import junit.framework.TestCase;
import org.springframework.webflow.TestBean;
import org.springframework.webflow.engine.support.ActionTransitionCriteria;
import org.springframework.webflow.engine.support.DefaultTargetStateResolver;
import org.springframework.webflow.engine.support.EventIdTransitionCriteria;
import org.springframework.webflow.engine.support.MockTransitionCriteria;
import org.springframework.webflow.execution.RequestContext;
import org.springframework.webflow.execution.TestAction;
import org.springframework.webflow.test.MockRequestControlContext;
@@ -212,7 +212,7 @@ public class ViewStateTests extends TestCase {
}
protected TransitionCriteria on(String event) {
return new EventIdTransitionCriteria(event);
return new MockTransitionCriteria(event);
}
protected TargetStateResolver to(String stateId) {

View File

@@ -1,84 +0,0 @@
/*
* Copyright 2004-2008 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.webflow.engine.support;
import java.io.Serializable;
import org.springframework.util.Assert;
import org.springframework.webflow.engine.TransitionCriteria;
import org.springframework.webflow.execution.Event;
import org.springframework.webflow.execution.RequestContext;
/**
* Simple transition criteria that matches on an eventId and nothing else. Specifically, if the id of the last event
* that occurred equals {@link #getEventId()} this criteria will return true.
*
* @see RequestContext#getCurrentEvent()
*
* @author Erwin Vervaet
* @author Keith Donald
*/
public class EventIdTransitionCriteria implements TransitionCriteria, Serializable {
/**
* The event id to match.
*/
private String eventId;
/**
* Whether or not to match case sensitively. Default is true.
*/
private boolean caseSensitive = true;
/**
* Create a new event id matching criteria object.
* @param eventId the event id
*/
public EventIdTransitionCriteria(String eventId) {
Assert.hasText(eventId, "The event id is required");
this.eventId = eventId;
}
/**
* Returns the event id to match.
*/
public String getEventId() {
return eventId;
}
/**
* Set whether or not the event id should be matched in a case sensitive manner. Defaults to true.
*/
public void setCaseSensitive(boolean caseSensitive) {
this.caseSensitive = caseSensitive;
}
public boolean test(RequestContext context) {
Event currentEvent = context.getCurrentEvent();
if (currentEvent == null) {
return false;
}
if (caseSensitive) {
return eventId.equals(currentEvent.getId());
} else {
return eventId.equalsIgnoreCase(currentEvent.getId());
}
}
public String toString() {
return "[eventId = '" + eventId + "']";
}
}

View File

@@ -1,56 +0,0 @@
/*
* Copyright 2004-2008 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.webflow.engine.support;
import junit.framework.TestCase;
import org.springframework.webflow.execution.Event;
import org.springframework.webflow.test.MockRequestContext;
public class EventIdTransitionCriteriaTests extends TestCase {
public void testTestCriteria() {
EventIdTransitionCriteria c = new EventIdTransitionCriteria("foo");
MockRequestContext context = new MockRequestContext();
context.setCurrentEvent(new Event(this, "foo"));
assertEquals(true, c.test(context));
context.setCurrentEvent(new Event(this, "FOO"));
assertEquals(false, c.test(context)); // case sensitive
context.setCurrentEvent(new Event(this, "bar"));
assertEquals(false, c.test(context));
}
public void testNullLastEventId() {
EventIdTransitionCriteria c = new EventIdTransitionCriteria("foo");
MockRequestContext context = new MockRequestContext();
context.setCurrentEvent(null);
assertEquals(false, c.test(context));
}
public void testIllegalArg() {
try {
new EventIdTransitionCriteria(null);
fail("was null");
} catch (IllegalArgumentException e) {
}
try {
new EventIdTransitionCriteria("");
fail("was blank");
} catch (IllegalArgumentException e) {
}
}
}