KickJava   Java API By Example, From Geeks To Geeks.

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


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.context.ExternalContext;
19 import org.springframework.webflow.core.collection.AttributeMap;
20 import org.springframework.webflow.core.collection.LocalAttributeMap;
21 import org.springframework.webflow.core.collection.MutableAttributeMap;
22 import org.springframework.webflow.core.collection.ParameterMap;
23 import org.springframework.webflow.definition.FlowDefinition;
24 import org.springframework.webflow.definition.StateDefinition;
25 import org.springframework.webflow.definition.TransitionDefinition;
26 import org.springframework.webflow.engine.Flow;
27 import org.springframework.webflow.engine.Transition;
28 import org.springframework.webflow.execution.Event;
29 import org.springframework.webflow.execution.FlowExecutionContext;
30 import org.springframework.webflow.execution.FlowSession;
31 import org.springframework.webflow.execution.RequestContext;
32
33 /**
34  * Mock implementation of the <code>RequestContext</code> interface to
35  * facilitate standalone flow artifact (e.g. action) unit tests.
36  *
37  * @see org.springframework.webflow.execution.RequestContext
38  * @see org.springframework.webflow.execution.Action
39  *
40  * @author Keith Donald
41  * @author Erwin Vervaet
42  */

43 public class MockRequestContext implements RequestContext {
44
45     private FlowExecutionContext flowExecutionContext = new MockFlowExecutionContext();
46
47     private ExternalContext externalContext = new MockExternalContext();
48
49     private MutableAttributeMap requestScope = new LocalAttributeMap();
50
51     private Event lastEvent;
52
53     private Transition lastTransition;
54
55     private MutableAttributeMap attributes = new LocalAttributeMap();
56
57     /**
58      * Creates a new mock request context with the following defaults:
59      * <ul>
60      * <li>A flow execution context with a active session of flow "mockFlow" in
61      * state "mockState".
62      * <li>A mock external context with no request parameters set.
63      * </ul>
64      * To add request parameters to this request, use the
65      * {@link #putRequestParameter(String, String)} method.
66      */

67     public MockRequestContext() {
68     }
69
70     /**
71      * Creates a new mock request context with the following defaults:
72      * <ul>
73      * <li>A flow execution context with an active session for the specified flow.
74      * <li>A mock external context with no request parameters set.
75      * </ul>
76      * To add request parameters to this request, use the
77      * {@link #putRequestParameter(String, String)} method.
78      */

79     public MockRequestContext(Flow flow) {
80         flowExecutionContext = new MockFlowExecutionContext(flow);
81     }
82     
83     /**
84      * Creates a new mock request context with the following defaults:
85      * <ul>
86      * <li>A flow execution context with a active session of flow "mockFlow" in
87      * state "mockState".
88      * <li>A mock external context with the provided parameters set.
89      * </ul>
90      */

91     public MockRequestContext(ParameterMap requestParameterMap) {
92         externalContext = new MockExternalContext(requestParameterMap);
93     }
94
95     // implementing RequestContext
96

97     public FlowDefinition getActiveFlow() {
98         return getFlowExecutionContext().getActiveSession().getDefinition();
99     }
100
101     public StateDefinition getCurrentState() {
102         return getFlowExecutionContext().getActiveSession().getState();
103     }
104
105     public MutableAttributeMap getRequestScope() {
106         return requestScope;
107     }
108
109     public MutableAttributeMap getFlashScope() {
110         return getMockFlowExecutionContext().getActiveSession().getFlashMap();
111     }
112
113     public MutableAttributeMap getFlowScope() {
114         return getFlowExecutionContext().getActiveSession().getScope();
115     }
116
117     public MutableAttributeMap getConversationScope() {
118         return getMockFlowExecutionContext().getConversationScope();
119     }
120
121     public ParameterMap getRequestParameters() {
122         return externalContext.getRequestParameterMap();
123     }
124
125     public ExternalContext getExternalContext() {
126         return externalContext;
127     }
128
129     public FlowExecutionContext getFlowExecutionContext() {
130         return flowExecutionContext;
131     }
132
133     public Event getLastEvent() {
134         return lastEvent;
135     }
136
137     public TransitionDefinition getLastTransition() {
138         return lastTransition;
139     }
140
141     public AttributeMap getAttributes() {
142         return attributes;
143     }
144
145     public void setAttributes(AttributeMap attributes) {
146         this.attributes.replaceWith(attributes);
147     }
148
149     public AttributeMap getModel() {
150         return getConversationScope().union(getFlowScope()).union(getFlashScope()).union(getRequestScope());
151     }
152     
153     // mutators
154

155     /**
156      * Sets the active flow session of the executing flow associated with this
157      * request. This will influence {@link #getActiveFlow()} and {@link #getCurrentState()},
158      * as well as {@link #getFlowScope()} and {@link #getFlashScope()}.
159      */

160     public void setActiveSession(FlowSession flowSession) {
161         getMockFlowExecutionContext().setActiveSession(flowSession);
162     }
163
164     /**
165      * Sets the external context.
166      */

167     public void setExternalContext(ExternalContext externalContext) {
168         this.externalContext = externalContext;
169     }
170
171     /**
172      * Sets the flow execution context.
173      */

174     public void setFlowExecutionContext(FlowExecutionContext flowExecutionContext) {
175         this.flowExecutionContext = flowExecutionContext;
176     }
177
178     /**
179      * Set the last event that occured in this request context.
180      * @param lastEvent the event to set
181      */

182     public void setLastEvent(Event lastEvent) {
183         this.lastEvent = lastEvent;
184     }
185
186     /**
187      * Set the last transition that executed in this request context.
188      * @param lastTransition the last transition to set
189      */

190     public void setLastTransition(Transition lastTransition) {
191         this.lastTransition = lastTransition;
192     }
193
194     /**
195      * Set a request context attribute.
196      * @param attributeName the attribute name
197      * @param attributeValue the attribute value
198      */

199     public void setAttribute(String JavaDoc attributeName, Object JavaDoc attributeValue) {
200         attributes.put(attributeName, attributeValue);
201     }
202
203     /**
204      * Remove a request context attribute.
205      * @param attributeName the attribute name
206      */

207     public void removeAttribute(String JavaDoc attributeName) {
208         attributes.remove(attributeName);
209     }
210         
211     // convenience accessors
212

213     /**
214      * Returns the contained mutable context {@link AttributeMap attribute map}
215      * allowing setting of mock context attributes.
216      * @return the attribute map
217      */

218     public MutableAttributeMap getAttributeMap() {
219         return attributes;
220     }
221
222     /**
223      * Returns the flow execution context as a {@link MockFlowExecutionContext}.
224      */

225     public MockFlowExecutionContext getMockFlowExecutionContext() {
226         return (MockFlowExecutionContext)flowExecutionContext;
227     }
228
229     /**
230      * Returns the external context as a {@link MockExternalContext}.
231      */

232     public MockExternalContext getMockExternalContext() {
233         return (MockExternalContext)externalContext;
234     }
235
236     /**
237      * Adds a request parameter to the configured external context.
238      * @param parameterName the parameter name
239      * @param parameterValue the parameter value
240      */

241     public void putRequestParameter(String JavaDoc parameterName, String JavaDoc parameterValue) {
242         getMockExternalContext().putRequestParameter(parameterName, parameterValue);
243     }
244
245     /**
246      * Adds a multi-valued request parameter to the configured external context.
247      * @param parameterName the parameter name
248      * @param parameterValues the parameter values
249      */

250     public void putRequestParameter(String JavaDoc parameterName, String JavaDoc[] parameterValues) {
251         getMockExternalContext().putRequestParameter(parameterName, parameterValues);
252     }
253 }
Popular Tags