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.core.collection.AttributeMap;
019: import org.springframework.webflow.core.collection.LocalAttributeMap;
020: import org.springframework.webflow.core.collection.MutableAttributeMap;
021: import org.springframework.webflow.definition.FlowDefinition;
022: import org.springframework.webflow.engine.Flow;
023: import org.springframework.webflow.execution.FlowExecutionContext;
024: import org.springframework.webflow.execution.FlowSession;
025:
026: /**
027: * A stub implementation of the flow execution context interface.
028: *
029: * @see FlowExecutionContext
030: *
031: * @author Keith Donald
032: */
033: public class MockFlowExecutionContext implements FlowExecutionContext {
034:
035: private FlowDefinition flow;
036:
037: private FlowSession activeSession;
038:
039: private MutableAttributeMap conversationScope = new LocalAttributeMap();
040:
041: private MutableAttributeMap attributes = new LocalAttributeMap();
042:
043: /**
044: * Creates a new mock flow execution context -- automatically installs a root
045: * flow definition and active flow session.
046: */
047: public MockFlowExecutionContext() {
048: activeSession = new MockFlowSession();
049: this .flow = activeSession.getDefinition();
050: }
051:
052: /**
053: * Creates a new mock flow execution context for the specified root flow
054: * definition.
055: */
056: public MockFlowExecutionContext(Flow rootFlow) {
057: this .flow = rootFlow;
058: activeSession = new MockFlowSession(rootFlow);
059: }
060:
061: public String getCaption() {
062: return "Mock flow execution context";
063: }
064:
065: // implementing flow execution context
066:
067: public FlowDefinition getDefinition() {
068: return flow;
069: }
070:
071: public boolean isActive() {
072: return activeSession != null;
073: }
074:
075: public FlowSession getActiveSession() throws IllegalStateException {
076: if (activeSession == null) {
077: throw new IllegalStateException("No flow session is active");
078: }
079: return activeSession;
080: }
081:
082: public MutableAttributeMap getConversationScope() {
083: return conversationScope;
084: }
085:
086: public AttributeMap getAttributes() {
087: return attributes;
088: }
089:
090: // mutators
091:
092: /**
093: * Sets the top-level flow definition.
094: */
095: public void setFlow(Flow rootFlow) {
096: this .flow = rootFlow;
097: }
098:
099: /**
100: * Sets the mock session to be the <i>active session</i>.
101: */
102: public void setActiveSession(FlowSession activeSession) {
103: this .activeSession = activeSession;
104: }
105:
106: /**
107: * Sets flow execution (conversational) scope.
108: */
109: public void setConversationScope(MutableAttributeMap scope) {
110: this .conversationScope = scope;
111: }
112:
113: // convenience accessors
114:
115: /**
116: * Returns the mock active flow session.
117: */
118: public MockFlowSession getMockActiveSession() {
119: return (MockFlowSession) activeSession;
120: }
121:
122: /**
123: * Returns the mutable execution attribute map.
124: * @return the execution attribute map
125: */
126: public MutableAttributeMap getAttributeMap() {
127: return attributes;
128: }
129:
130: /**
131: * Puts a execution attribute into the context.
132: * @param attributeName the attribute name
133: * @param attributeValue the attribute value
134: */
135: public void putAttribute(String attributeName, Object attributeValue) {
136: attributes.put(attributeName, attributeValue);
137: }
138: }
|