KickJava   Java API By Example, From Geeks To Geeks.

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


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.AttributeMap;
19 import org.springframework.webflow.core.collection.LocalAttributeMap;
20 import org.springframework.webflow.core.collection.MutableAttributeMap;
21 import org.springframework.webflow.definition.FlowDefinition;
22 import org.springframework.webflow.definition.StateDefinition;
23 import org.springframework.webflow.engine.Flow;
24 import org.springframework.webflow.engine.State;
25 import org.springframework.webflow.engine.ViewState;
26 import org.springframework.webflow.execution.FlowSession;
27 import org.springframework.webflow.execution.FlowSessionStatus;
28
29 /**
30  * Mock implementation of the {@link FlowSession} interface.
31  *
32  * @see FlowSession
33  *
34  * @author Erwin Vervaet
35  */

36 public class MockFlowSession implements FlowSession {
37
38     private Flow definition;
39
40     private State state;
41
42     private FlowSessionStatus status = FlowSessionStatus.CREATED;
43
44     private MutableAttributeMap scope = new LocalAttributeMap();
45
46     private MutableAttributeMap flashMap = new LocalAttributeMap();
47
48     private FlowSession parent;
49
50     /**
51      * Creates a new mock flow session that sets a flow with id "mockFlow" as
52      * the 'active flow' in state "mockState". This session marks itself active.
53      */

54     public MockFlowSession() {
55         setDefinition(new Flow("mockFlow"));
56         State state = new ViewState(definition, "mockState");
57         setStatus(FlowSessionStatus.ACTIVE);
58         setState(state);
59     }
60
61     /**
62      * Creates a new mock session in a created state for the specified flow
63      * definition.
64      */

65     public MockFlowSession(Flow flow) {
66         setDefinition(flow);
67     }
68
69     /**
70      * Creates a new mock session in {@link FlowSessionStatus#CREATED} state
71      * for the specified flow definition.
72      * @param flow the flow definition for the session
73      * @param input initial contents of 'flow scope'
74      */

75     public MockFlowSession(Flow flow, AttributeMap input) {
76         setDefinition(flow);
77         scope.putAll(input);
78     }
79     
80     // implementing FlowSession
81

82     public FlowDefinition getDefinition() {
83         return definition;
84     }
85
86     public StateDefinition getState() {
87         return state;
88     }
89
90     public FlowSessionStatus getStatus() {
91         return status;
92     }
93
94     public MutableAttributeMap getScope() {
95         return scope;
96     }
97
98     public MutableAttributeMap getFlashMap() {
99         return flashMap;
100     }
101
102     public FlowSession getParent() {
103         return parent;
104     }
105
106     public boolean isRoot() {
107         return parent == null;
108     }
109     
110     // mutators
111

112     /**
113      * Set the flow associated with this flow session.
114      */

115     public void setDefinition(Flow flow) {
116         this.definition = flow;
117     }
118
119     /**
120      * Set the currently active state.
121      */

122     public void setState(State state) {
123         this.state = state;
124     }
125
126     /**
127      * Set the status of this flow session.
128      */

129     public void setStatus(FlowSessionStatus status) {
130         this.status = status;
131     }
132
133     /**
134      * Set the scope data maintained by this flow session. This will be the flow
135      * scope data of the ongoing flow execution.
136      */

137     public void setScope(MutableAttributeMap scope) {
138         this.scope = scope;
139     }
140
141     /**
142      * Set the parent flow session of this flow session in the ongoing flow
143      * execution.
144      */

145     public void setParent(FlowSession parent) {
146         this.parent = parent;
147     }
148
149     // conveniece accessors
150

151     /**
152      * Returns the flow definition of this session.
153      */

154     public Flow getDefinitionInternal() {
155         return definition;
156     }
157     
158     /**
159      * Returns the current state of this session.
160      */

161     public State getStateInternal() {
162         return state;
163     }
164 }
Popular Tags