001: /*
002: * Copyright 2004-2007 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.springframework.webflow.test;
017:
018: import org.springframework.webflow.context.ExternalContext;
019: import org.springframework.webflow.core.collection.AttributeMap;
020: import org.springframework.webflow.core.collection.LocalAttributeMap;
021: import org.springframework.webflow.core.collection.MutableAttributeMap;
022: import org.springframework.webflow.core.collection.ParameterMap;
023: import org.springframework.webflow.definition.FlowDefinition;
024: import org.springframework.webflow.definition.StateDefinition;
025: import org.springframework.webflow.definition.TransitionDefinition;
026: import org.springframework.webflow.engine.Flow;
027: import org.springframework.webflow.engine.Transition;
028: import org.springframework.webflow.execution.Event;
029: import org.springframework.webflow.execution.FlowExecutionContext;
030: import org.springframework.webflow.execution.FlowSession;
031: import org.springframework.webflow.execution.RequestContext;
032:
033: /**
034: * Mock implementation of the <code>RequestContext</code> interface to
035: * facilitate standalone flow artifact (e.g. action) unit tests.
036: *
037: * @see org.springframework.webflow.execution.RequestContext
038: * @see org.springframework.webflow.execution.Action
039: *
040: * @author Keith Donald
041: * @author Erwin Vervaet
042: */
043: public class MockRequestContext implements RequestContext {
044:
045: private FlowExecutionContext flowExecutionContext = new MockFlowExecutionContext();
046:
047: private ExternalContext externalContext = new MockExternalContext();
048:
049: private MutableAttributeMap requestScope = new LocalAttributeMap();
050:
051: private Event lastEvent;
052:
053: private Transition lastTransition;
054:
055: private MutableAttributeMap attributes = new LocalAttributeMap();
056:
057: /**
058: * Creates a new mock request context with the following defaults:
059: * <ul>
060: * <li>A flow execution context with a active session of flow "mockFlow" in
061: * state "mockState".
062: * <li>A mock external context with no request parameters set.
063: * </ul>
064: * To add request parameters to this request, use the
065: * {@link #putRequestParameter(String, String)} method.
066: */
067: public MockRequestContext() {
068: }
069:
070: /**
071: * Creates a new mock request context with the following defaults:
072: * <ul>
073: * <li>A flow execution context with an active session for the specified flow.
074: * <li>A mock external context with no request parameters set.
075: * </ul>
076: * To add request parameters to this request, use the
077: * {@link #putRequestParameter(String, String)} method.
078: */
079: public MockRequestContext(Flow flow) {
080: flowExecutionContext = new MockFlowExecutionContext(flow);
081: }
082:
083: /**
084: * Creates a new mock request context with the following defaults:
085: * <ul>
086: * <li>A flow execution context with a active session of flow "mockFlow" in
087: * state "mockState".
088: * <li>A mock external context with the provided parameters set.
089: * </ul>
090: */
091: public MockRequestContext(ParameterMap requestParameterMap) {
092: externalContext = new MockExternalContext(requestParameterMap);
093: }
094:
095: // implementing RequestContext
096:
097: public FlowDefinition getActiveFlow() {
098: return getFlowExecutionContext().getActiveSession()
099: .getDefinition();
100: }
101:
102: public StateDefinition getCurrentState() {
103: return getFlowExecutionContext().getActiveSession().getState();
104: }
105:
106: public MutableAttributeMap getRequestScope() {
107: return requestScope;
108: }
109:
110: public MutableAttributeMap getFlashScope() {
111: return getMockFlowExecutionContext().getActiveSession()
112: .getFlashMap();
113: }
114:
115: public MutableAttributeMap getFlowScope() {
116: return getFlowExecutionContext().getActiveSession().getScope();
117: }
118:
119: public MutableAttributeMap getConversationScope() {
120: return getMockFlowExecutionContext().getConversationScope();
121: }
122:
123: public ParameterMap getRequestParameters() {
124: return externalContext.getRequestParameterMap();
125: }
126:
127: public ExternalContext getExternalContext() {
128: return externalContext;
129: }
130:
131: public FlowExecutionContext getFlowExecutionContext() {
132: return flowExecutionContext;
133: }
134:
135: public Event getLastEvent() {
136: return lastEvent;
137: }
138:
139: public TransitionDefinition getLastTransition() {
140: return lastTransition;
141: }
142:
143: public AttributeMap getAttributes() {
144: return attributes;
145: }
146:
147: public void setAttributes(AttributeMap attributes) {
148: this .attributes.replaceWith(attributes);
149: }
150:
151: public AttributeMap getModel() {
152: return getConversationScope().union(getFlowScope()).union(
153: getFlashScope()).union(getRequestScope());
154: }
155:
156: // mutators
157:
158: /**
159: * Sets the active flow session of the executing flow associated with this
160: * request. This will influence {@link #getActiveFlow()} and {@link #getCurrentState()},
161: * as well as {@link #getFlowScope()} and {@link #getFlashScope()}.
162: */
163: public void setActiveSession(FlowSession flowSession) {
164: getMockFlowExecutionContext().setActiveSession(flowSession);
165: }
166:
167: /**
168: * Sets the external context.
169: */
170: public void setExternalContext(ExternalContext externalContext) {
171: this .externalContext = externalContext;
172: }
173:
174: /**
175: * Sets the flow execution context.
176: */
177: public void setFlowExecutionContext(
178: FlowExecutionContext flowExecutionContext) {
179: this .flowExecutionContext = flowExecutionContext;
180: }
181:
182: /**
183: * Set the last event that occured in this request context.
184: * @param lastEvent the event to set
185: */
186: public void setLastEvent(Event lastEvent) {
187: this .lastEvent = lastEvent;
188: }
189:
190: /**
191: * Set the last transition that executed in this request context.
192: * @param lastTransition the last transition to set
193: */
194: public void setLastTransition(Transition lastTransition) {
195: this .lastTransition = lastTransition;
196: }
197:
198: /**
199: * Set a request context attribute.
200: * @param attributeName the attribute name
201: * @param attributeValue the attribute value
202: */
203: public void setAttribute(String attributeName, Object attributeValue) {
204: attributes.put(attributeName, attributeValue);
205: }
206:
207: /**
208: * Remove a request context attribute.
209: * @param attributeName the attribute name
210: */
211: public void removeAttribute(String attributeName) {
212: attributes.remove(attributeName);
213: }
214:
215: // convenience accessors
216:
217: /**
218: * Returns the contained mutable context {@link AttributeMap attribute map}
219: * allowing setting of mock context attributes.
220: * @return the attribute map
221: */
222: public MutableAttributeMap getAttributeMap() {
223: return attributes;
224: }
225:
226: /**
227: * Returns the flow execution context as a {@link MockFlowExecutionContext}.
228: */
229: public MockFlowExecutionContext getMockFlowExecutionContext() {
230: return (MockFlowExecutionContext) flowExecutionContext;
231: }
232:
233: /**
234: * Returns the external context as a {@link MockExternalContext}.
235: */
236: public MockExternalContext getMockExternalContext() {
237: return (MockExternalContext) externalContext;
238: }
239:
240: /**
241: * Adds a request parameter to the configured external context.
242: * @param parameterName the parameter name
243: * @param parameterValue the parameter value
244: */
245: public void putRequestParameter(String parameterName,
246: String parameterValue) {
247: getMockExternalContext().putRequestParameter(parameterName,
248: parameterValue);
249: }
250:
251: /**
252: * Adds a multi-valued request parameter to the configured external context.
253: * @param parameterName the parameter name
254: * @param parameterValues the parameter values
255: */
256: public void putRequestParameter(String parameterName,
257: String[] parameterValues) {
258: getMockExternalContext().putRequestParameter(parameterName,
259: parameterValues);
260: }
261: }
|