KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > webflow > test > MockRequestControlContext


1 /*
2  * Copyright 2002-2006 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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 implements RequestControlContext {
40
41     /**
42      * Creates a new mock request control context for controlling a mock execution of the
43      * provided flow definition.
44      */

45     public MockRequestControlContext(Flow rootFlow) {
46         super(rootFlow);
47     }
48     
49     // implementing RequestControlContext
50

51     public void setCurrentState(State state) {
52         State previousState = (State)getCurrentState();
53         getMockFlowExecutionContext().getMockActiveSession().setState(state);
54         if (previousState == null) {
55             getMockFlowExecutionContext().getMockActiveSession().setStatus(FlowSessionStatus.ACTIVE);
56         }
57     }
58
59     public ViewSelection start(Flow flow, MutableAttributeMap input) throws IllegalStateException JavaDoc {
60         getMockFlowExecutionContext().setActiveSession(new MockFlowSession(flow, input));
61         getMockFlowExecutionContext().getMockActiveSession().setStatus(FlowSessionStatus.STARTING);
62         ViewSelection selectedView = flow.start(this, input);
63         return selectedView;
64     }
65
66     public ViewSelection signalEvent(Event event) {
67         setLastEvent(event);
68         ViewSelection selectedView = ((Flow)getActiveFlow()).onEvent(this);
69         return selectedView;
70     }
71
72     public FlowSession endActiveFlowSession(MutableAttributeMap output) throws IllegalStateException JavaDoc {
73         MockFlowSession endingSession = getMockFlowExecutionContext().getMockActiveSession();
74         endingSession.getDefinitionInternal().end(this, output);
75         endingSession.setStatus(FlowSessionStatus.ENDED);
76         getMockFlowExecutionContext().setActiveSession(null);
77         return endingSession;
78     }
79
80     public ViewSelection execute(Transition transition) {
81         return transition.execute((TransitionableState)getCurrentState(), this);
82     }
83 }
Popular Tags