01: /*
02: * Copyright 2004-2007 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16: package org.springframework.webflow.test;
17:
18: import org.springframework.webflow.core.collection.MutableAttributeMap;
19: import org.springframework.webflow.engine.Flow;
20: import org.springframework.webflow.engine.RequestControlContext;
21: import org.springframework.webflow.engine.State;
22: import org.springframework.webflow.engine.Transition;
23: import org.springframework.webflow.engine.TransitionableState;
24: import org.springframework.webflow.execution.Event;
25: import org.springframework.webflow.execution.FlowSession;
26: import org.springframework.webflow.execution.FlowSessionStatus;
27: import org.springframework.webflow.execution.ViewSelection;
28:
29: /**
30: * Mock implementation of the {@link RequestControlContext} interface to
31: * facilitate standalone Flow and State unit tests.
32: *
33: * @see org.springframework.webflow.execution.RequestContext
34: * @see org.springframework.webflow.execution.FlowSession
35: * @see org.springframework.webflow.engine.State
36: *
37: * @author Keith Donald
38: */
39: public class MockRequestControlContext extends MockRequestContext
40: implements RequestControlContext {
41:
42: /**
43: * Creates a new mock request control context for controlling a mock execution of the
44: * provided flow definition.
45: */
46: public MockRequestControlContext(Flow rootFlow) {
47: super (rootFlow);
48: }
49:
50: // implementing RequestControlContext
51:
52: public void setCurrentState(State state) {
53: State previousState = (State) getCurrentState();
54: getMockFlowExecutionContext().getMockActiveSession().setState(
55: state);
56: if (previousState == null) {
57: getMockFlowExecutionContext().getMockActiveSession()
58: .setStatus(FlowSessionStatus.ACTIVE);
59: }
60: }
61:
62: public ViewSelection start(Flow flow, MutableAttributeMap input)
63: throws IllegalStateException {
64: getMockFlowExecutionContext().setActiveSession(
65: new MockFlowSession(flow, input));
66: getMockFlowExecutionContext().getMockActiveSession().setStatus(
67: FlowSessionStatus.STARTING);
68: ViewSelection selectedView = flow.start(this , input);
69: return selectedView;
70: }
71:
72: public ViewSelection signalEvent(Event event) {
73: setLastEvent(event);
74: ViewSelection selectedView = ((Flow) getActiveFlow())
75: .onEvent(this );
76: return selectedView;
77: }
78:
79: public FlowSession endActiveFlowSession(MutableAttributeMap output)
80: throws IllegalStateException {
81: MockFlowSession endingSession = getMockFlowExecutionContext()
82: .getMockActiveSession();
83: endingSession.getDefinitionInternal().end(this , output);
84: endingSession.setStatus(FlowSessionStatus.ENDED);
85: getMockFlowExecutionContext().setActiveSession(null);
86: return endingSession;
87: }
88:
89: public ViewSelection execute(Transition transition) {
90: return transition.execute(
91: (TransitionableState) getCurrentState(), this);
92: }
93: }
|