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.definition.StateDefinition;
023: import org.springframework.webflow.engine.Flow;
024: import org.springframework.webflow.engine.State;
025: import org.springframework.webflow.engine.ViewState;
026: import org.springframework.webflow.execution.FlowSession;
027: import org.springframework.webflow.execution.FlowSessionStatus;
028:
029: /**
030: * Mock implementation of the {@link FlowSession} interface.
031: *
032: * @see FlowSession
033: *
034: * @author Erwin Vervaet
035: */
036: public class MockFlowSession implements FlowSession {
037:
038: private Flow definition;
039:
040: private State state;
041:
042: private FlowSessionStatus status = FlowSessionStatus.CREATED;
043:
044: private MutableAttributeMap scope = new LocalAttributeMap();
045:
046: private MutableAttributeMap flashMap = new LocalAttributeMap();
047:
048: private FlowSession parent;
049:
050: /**
051: * Creates a new mock flow session that sets a flow with id "mockFlow" as
052: * the 'active flow' in state "mockState". This session marks itself active.
053: */
054: public MockFlowSession() {
055: setDefinition(new Flow("mockFlow"));
056: State state = new ViewState(definition, "mockState");
057: setStatus(FlowSessionStatus.ACTIVE);
058: setState(state);
059: }
060:
061: /**
062: * Creates a new mock session in a created state for the specified flow
063: * definition.
064: */
065: public MockFlowSession(Flow flow) {
066: setDefinition(flow);
067: }
068:
069: /**
070: * Creates a new mock session in {@link FlowSessionStatus#CREATED} state
071: * for the specified flow definition.
072: * @param flow the flow definition for the session
073: * @param input initial contents of 'flow scope'
074: */
075: public MockFlowSession(Flow flow, AttributeMap input) {
076: setDefinition(flow);
077: scope.putAll(input);
078: }
079:
080: // implementing FlowSession
081:
082: public FlowDefinition getDefinition() {
083: return definition;
084: }
085:
086: public StateDefinition getState() {
087: return state;
088: }
089:
090: public FlowSessionStatus getStatus() {
091: return status;
092: }
093:
094: public MutableAttributeMap getScope() {
095: return scope;
096: }
097:
098: public MutableAttributeMap getFlashMap() {
099: 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: }
|